blob: 458a00e0c161835e598c44259e1b57f586a98a7b [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"
Clarence Ip980405d2017-08-08 18:33:44 -040037#include "sde_vbif.h"
Alan Kwong67a3f792016-11-01 23:16:53 -040038#include "sde_power_handle.h"
Alan Kwong9aa061c2016-11-06 21:17:12 -050039#include "sde_core_perf.h"
Narendra Muppalla77b32932017-05-10 13:53:11 -070040#include "sde_trace.h"
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -070041#include <soc/qcom/scm.h>
42#include "soc/qcom/secure_buffer.h"
43
44/* defines for secure channel call */
45#define SEC_SID_CNT 2
46#define SEC_SID_MASK_0 0x80881
47#define SEC_SID_MASK_1 0x80C81
48#define MEM_PROTECT_SD_CTRL_SWITCH 0x18
49#define MDP_DEVICE_ID 0x1A
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040050
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -070051struct sde_crtc_irq_info {
52 struct sde_irq_callback irq;
53 u32 event;
54 int (*func)(struct drm_crtc *crtc, bool en,
55 struct sde_irq_callback *irq);
56 struct list_head list;
57};
58
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070059struct sde_crtc_custom_events {
60 u32 event;
61 int (*func)(struct drm_crtc *crtc, bool en,
62 struct sde_irq_callback *irq);
63};
64
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070065static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
66 bool en, struct sde_irq_callback *ad_irq);
67
Benjamin Chan90139102017-06-21 16:00:39 -040068static int sde_crtc_pm_event_handler(struct drm_crtc *crtc_drm,
69 bool en, struct sde_irq_callback *noirq);
70
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070071static struct sde_crtc_custom_events custom_events[] = {
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070072 {DRM_EVENT_AD_BACKLIGHT, sde_cp_ad_interrupt},
Benjamin Chan90139102017-06-21 16:00:39 -040073 {DRM_EVENT_CRTC_POWER, sde_crtc_power_interrupt_handler},
74 {DRM_EVENT_SDE_POWER, sde_crtc_pm_event_handler},
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070075};
76
Clarence Ipcae1bb62016-07-07 12:07:13 -040077/* default input fence timeout, in ms */
Dhaval Patelb9850c02017-08-07 22:55:47 -070078#define SDE_CRTC_INPUT_FENCE_TIMEOUT 10000
Clarence Ipcae1bb62016-07-07 12:07:13 -040079
Dhaval Patel4e574842016-08-23 15:11:37 -070080/*
81 * The default input fence timeout is 2 seconds while max allowed
82 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
83 * tolerance limit.
84 */
85#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
86
Dhaval Patel48c76022016-09-01 17:51:23 -070087/* layer mixer index on sde_crtc */
88#define LEFT_MIXER 0
89#define RIGHT_MIXER 1
90
Dhaval Patelf9245d62017-03-28 16:24:00 -070091#define MISR_BUFF_SIZE 256
92
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040093static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040094{
Clarence Ip7f70ce42017-03-20 06:53:46 -070095 struct msm_drm_private *priv;
96
97 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
98 SDE_ERROR("invalid crtc\n");
99 return NULL;
100 }
101 priv = crtc->dev->dev_private;
102 if (!priv || !priv->kms) {
103 SDE_ERROR("invalid kms\n");
104 return NULL;
105 }
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400106
Ben Chan78647cd2016-06-26 22:02:47 -0400107 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400108}
109
Dhaval Patelf9245d62017-03-28 16:24:00 -0700110static inline int _sde_crtc_power_enable(struct sde_crtc *sde_crtc, bool enable)
111{
112 struct drm_crtc *crtc;
113 struct msm_drm_private *priv;
114 struct sde_kms *sde_kms;
115
116 if (!sde_crtc) {
117 SDE_ERROR("invalid sde crtc\n");
118 return -EINVAL;
119 }
120
121 crtc = &sde_crtc->base;
122 if (!crtc->dev || !crtc->dev->dev_private) {
123 SDE_ERROR("invalid drm device\n");
124 return -EINVAL;
125 }
126
127 priv = crtc->dev->dev_private;
128 if (!priv->kms) {
129 SDE_ERROR("invalid kms\n");
130 return -EINVAL;
131 }
132
133 sde_kms = to_sde_kms(priv->kms);
134
135 return sde_power_resource_enable(&priv->phandle, sde_kms->core_client,
136 enable);
137}
138
Alan Kwongcdb2f282017-03-18 13:42:06 -0700139/**
140 * _sde_crtc_rp_to_crtc - get crtc from resource pool object
141 * @rp: Pointer to resource pool
142 * return: Pointer to drm crtc if success; null otherwise
143 */
144static struct drm_crtc *_sde_crtc_rp_to_crtc(struct sde_crtc_respool *rp)
145{
146 if (!rp)
147 return NULL;
148
149 return container_of(rp, struct sde_crtc_state, rp)->base.crtc;
150}
151
152/**
153 * _sde_crtc_rp_reclaim - reclaim unused, or all if forced, resources in pool
154 * @rp: Pointer to resource pool
155 * @force: True to reclaim all resources; otherwise, reclaim only unused ones
156 * return: None
157 */
158static void _sde_crtc_rp_reclaim(struct sde_crtc_respool *rp, bool force)
159{
160 struct sde_crtc_res *res, *next;
161 struct drm_crtc *crtc;
162
163 crtc = _sde_crtc_rp_to_crtc(rp);
164 if (!crtc) {
165 SDE_ERROR("invalid crtc\n");
166 return;
167 }
168
169 SDE_DEBUG("crtc%d.%u %s\n", crtc->base.id, rp->sequence_id,
170 force ? "destroy" : "free_unused");
171
172 list_for_each_entry_safe(res, next, &rp->res_list, list) {
173 if (!force && !(res->flags & SDE_CRTC_RES_FLAG_FREE))
174 continue;
175 SDE_DEBUG("crtc%d.%u reclaim res:0x%x/0x%llx/%pK/%d\n",
176 crtc->base.id, rp->sequence_id,
177 res->type, res->tag, res->val,
178 atomic_read(&res->refcount));
179 list_del(&res->list);
180 if (res->ops.put)
181 res->ops.put(res->val);
182 kfree(res);
183 }
184}
185
186/**
187 * _sde_crtc_rp_free_unused - free unused resource in pool
188 * @rp: Pointer to resource pool
189 * return: none
190 */
191static void _sde_crtc_rp_free_unused(struct sde_crtc_respool *rp)
192{
Alan Kwong310e9b02017-08-03 02:04:07 -0400193 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700194 _sde_crtc_rp_reclaim(rp, false);
Alan Kwong310e9b02017-08-03 02:04:07 -0400195 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700196}
197
198/**
199 * _sde_crtc_rp_destroy - destroy resource pool
200 * @rp: Pointer to resource pool
201 * return: None
202 */
203static void _sde_crtc_rp_destroy(struct sde_crtc_respool *rp)
204{
Alan Kwong310e9b02017-08-03 02:04:07 -0400205 mutex_lock(rp->rp_lock);
206 list_del_init(&rp->rp_list);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700207 _sde_crtc_rp_reclaim(rp, true);
Alan Kwong310e9b02017-08-03 02:04:07 -0400208 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700209}
210
211/**
212 * _sde_crtc_hw_blk_get - get callback for hardware block
213 * @val: Resource handle
214 * @type: Resource type
215 * @tag: Search tag for given resource
216 * return: Resource handle
217 */
218static void *_sde_crtc_hw_blk_get(void *val, u32 type, u64 tag)
219{
220 SDE_DEBUG("res:%d/0x%llx/%pK\n", type, tag, val);
221 return sde_hw_blk_get(val, type, tag);
222}
223
224/**
225 * _sde_crtc_hw_blk_put - put callback for hardware block
226 * @val: Resource handle
227 * return: None
228 */
229static void _sde_crtc_hw_blk_put(void *val)
230{
231 SDE_DEBUG("res://%pK\n", val);
232 sde_hw_blk_put(val);
233}
234
235/**
236 * _sde_crtc_rp_duplicate - duplicate resource pool and reset reference count
237 * @rp: Pointer to original resource pool
238 * @dup_rp: Pointer to duplicated resource pool
239 * return: None
240 */
241static void _sde_crtc_rp_duplicate(struct sde_crtc_respool *rp,
242 struct sde_crtc_respool *dup_rp)
243{
244 struct sde_crtc_res *res, *dup_res;
245 struct drm_crtc *crtc;
246
Alan Kwong310e9b02017-08-03 02:04:07 -0400247 if (!rp || !dup_rp || !rp->rp_head) {
Alan Kwongcdb2f282017-03-18 13:42:06 -0700248 SDE_ERROR("invalid resource pool\n");
249 return;
250 }
251
252 crtc = _sde_crtc_rp_to_crtc(rp);
253 if (!crtc) {
254 SDE_ERROR("invalid crtc\n");
255 return;
256 }
257
258 SDE_DEBUG("crtc%d.%u duplicate\n", crtc->base.id, rp->sequence_id);
259
Alan Kwong310e9b02017-08-03 02:04:07 -0400260 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700261 dup_rp->sequence_id = rp->sequence_id + 1;
262 INIT_LIST_HEAD(&dup_rp->res_list);
263 dup_rp->ops = rp->ops;
264 list_for_each_entry(res, &rp->res_list, list) {
265 dup_res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
Alan Kwong310e9b02017-08-03 02:04:07 -0400266 if (!dup_res) {
267 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700268 return;
Alan Kwong310e9b02017-08-03 02:04:07 -0400269 }
Alan Kwongcdb2f282017-03-18 13:42:06 -0700270 INIT_LIST_HEAD(&dup_res->list);
271 atomic_set(&dup_res->refcount, 0);
272 dup_res->type = res->type;
273 dup_res->tag = res->tag;
274 dup_res->val = res->val;
275 dup_res->ops = res->ops;
276 dup_res->flags = SDE_CRTC_RES_FLAG_FREE;
277 SDE_DEBUG("crtc%d.%u dup res:0x%x/0x%llx/%pK/%d\n",
278 crtc->base.id, dup_rp->sequence_id,
279 dup_res->type, dup_res->tag, dup_res->val,
280 atomic_read(&dup_res->refcount));
281 list_add_tail(&dup_res->list, &dup_rp->res_list);
282 if (dup_res->ops.get)
283 dup_res->ops.get(dup_res->val, 0, -1);
284 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400285
286 dup_rp->rp_lock = rp->rp_lock;
287 dup_rp->rp_head = rp->rp_head;
288 INIT_LIST_HEAD(&dup_rp->rp_list);
289 list_add_tail(&dup_rp->rp_list, rp->rp_head);
290 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700291}
292
293/**
294 * _sde_crtc_rp_reset - reset resource pool after allocation
295 * @rp: Pointer to original resource pool
Alan Kwong310e9b02017-08-03 02:04:07 -0400296 * @rp_lock: Pointer to serialization resource pool lock
297 * @rp_head: Pointer to crtc resource pool head
Alan Kwongcdb2f282017-03-18 13:42:06 -0700298 * return: None
299 */
Alan Kwong310e9b02017-08-03 02:04:07 -0400300static void _sde_crtc_rp_reset(struct sde_crtc_respool *rp,
301 struct mutex *rp_lock, struct list_head *rp_head)
Alan Kwongcdb2f282017-03-18 13:42:06 -0700302{
Alan Kwong310e9b02017-08-03 02:04:07 -0400303 if (!rp || !rp_lock || !rp_head) {
Alan Kwongcdb2f282017-03-18 13:42:06 -0700304 SDE_ERROR("invalid resource pool\n");
305 return;
306 }
307
Alan Kwong310e9b02017-08-03 02:04:07 -0400308 mutex_lock(rp_lock);
309 rp->rp_lock = rp_lock;
310 rp->rp_head = rp_head;
311 INIT_LIST_HEAD(&rp->rp_list);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700312 rp->sequence_id = 0;
313 INIT_LIST_HEAD(&rp->res_list);
314 rp->ops.get = _sde_crtc_hw_blk_get;
315 rp->ops.put = _sde_crtc_hw_blk_put;
Alan Kwong310e9b02017-08-03 02:04:07 -0400316 list_add_tail(&rp->rp_list, rp->rp_head);
317 mutex_unlock(rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700318}
319
320/**
Alan Kwong310e9b02017-08-03 02:04:07 -0400321 * _sde_crtc_rp_add_no_lock - add given resource to resource pool without lock
Alan Kwongcdb2f282017-03-18 13:42:06 -0700322 * @rp: Pointer to original resource pool
323 * @type: Resource type
324 * @tag: Search tag for given resource
325 * @val: Resource handle
326 * @ops: Resource callback operations
327 * return: 0 if success; error code otherwise
328 */
Alan Kwong310e9b02017-08-03 02:04:07 -0400329static int _sde_crtc_rp_add_no_lock(struct sde_crtc_respool *rp, u32 type,
330 u64 tag, void *val, struct sde_crtc_res_ops *ops)
Alan Kwongcdb2f282017-03-18 13:42:06 -0700331{
332 struct sde_crtc_res *res;
333 struct drm_crtc *crtc;
334
335 if (!rp || !ops) {
336 SDE_ERROR("invalid resource pool/ops\n");
337 return -EINVAL;
338 }
339
340 crtc = _sde_crtc_rp_to_crtc(rp);
341 if (!crtc) {
342 SDE_ERROR("invalid crtc\n");
343 return -EINVAL;
344 }
345
346 list_for_each_entry(res, &rp->res_list, list) {
347 if (res->type != type || res->tag != tag)
348 continue;
349 SDE_ERROR("crtc%d.%u already exist res:0x%x/0x%llx/%pK/%d\n",
350 crtc->base.id, rp->sequence_id,
351 res->type, res->tag, res->val,
352 atomic_read(&res->refcount));
353 return -EEXIST;
354 }
355 res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
356 if (!res)
357 return -ENOMEM;
358 INIT_LIST_HEAD(&res->list);
359 atomic_set(&res->refcount, 1);
360 res->type = type;
361 res->tag = tag;
362 res->val = val;
363 res->ops = *ops;
364 list_add_tail(&res->list, &rp->res_list);
365 SDE_DEBUG("crtc%d.%u added res:0x%x/0x%llx\n",
366 crtc->base.id, rp->sequence_id, type, tag);
367 return 0;
368}
369
370/**
Alan Kwong310e9b02017-08-03 02:04:07 -0400371 * _sde_crtc_rp_add - add given resource to resource pool
372 * @rp: Pointer to original resource pool
373 * @type: Resource type
374 * @tag: Search tag for given resource
375 * @val: Resource handle
376 * @ops: Resource callback operations
377 * return: 0 if success; error code otherwise
378 */
379static int _sde_crtc_rp_add(struct sde_crtc_respool *rp, u32 type, u64 tag,
380 void *val, struct sde_crtc_res_ops *ops)
381{
382 int rc;
383
384 if (!rp) {
385 SDE_ERROR("invalid resource pool\n");
386 return -EINVAL;
387 }
388
389 mutex_lock(rp->rp_lock);
390 rc = _sde_crtc_rp_add_no_lock(rp, type, tag, val, ops);
391 mutex_unlock(rp->rp_lock);
392 return rc;
393}
394
395/**
Alan Kwongcdb2f282017-03-18 13:42:06 -0700396 * _sde_crtc_rp_get - lookup the resource from given resource pool and obtain
397 * if available; otherwise, obtain resource from global pool
398 * @rp: Pointer to original resource pool
399 * @type: Resource type
400 * @tag: Search tag for given resource
401 * return: Resource handle if success; pointer error or null otherwise
402 */
403static void *_sde_crtc_rp_get(struct sde_crtc_respool *rp, u32 type, u64 tag)
404{
Alan Kwong310e9b02017-08-03 02:04:07 -0400405 struct sde_crtc_respool *old_rp;
Alan Kwongcdb2f282017-03-18 13:42:06 -0700406 struct sde_crtc_res *res;
407 void *val = NULL;
408 int rc;
409 struct drm_crtc *crtc;
410
411 if (!rp) {
412 SDE_ERROR("invalid resource pool\n");
413 return NULL;
414 }
415
416 crtc = _sde_crtc_rp_to_crtc(rp);
417 if (!crtc) {
418 SDE_ERROR("invalid crtc\n");
419 return NULL;
420 }
421
Alan Kwong310e9b02017-08-03 02:04:07 -0400422 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700423 list_for_each_entry(res, &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 atomic_inc(&res->refcount);
431 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
Alan Kwong310e9b02017-08-03 02:04:07 -0400432 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700433 return res->val;
434 }
435 list_for_each_entry(res, &rp->res_list, list) {
436 if (res->type != type || !(res->flags & SDE_CRTC_RES_FLAG_FREE))
437 continue;
438 SDE_DEBUG("crtc%d.%u retag res:0x%x/0x%llx/%pK/%d\n",
439 crtc->base.id, rp->sequence_id,
440 res->type, res->tag, res->val,
441 atomic_read(&res->refcount));
442 atomic_inc(&res->refcount);
443 res->tag = tag;
444 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
Alan Kwong310e9b02017-08-03 02:04:07 -0400445 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700446 return res->val;
447 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400448 /* not in this rp, try to grab from global pool */
Alan Kwongcdb2f282017-03-18 13:42:06 -0700449 if (rp->ops.get)
450 val = rp->ops.get(NULL, type, -1);
Alan Kwong310e9b02017-08-03 02:04:07 -0400451 if (!IS_ERR_OR_NULL(val))
452 goto add_res;
453 /*
454 * Search older resource pools for hw blk with matching type,
455 * necessary when resource is being used by this object,
456 * but in previous states not yet cleaned up.
457 *
458 * This enables searching of all resources currently owned
459 * by this crtc even though the resource might not be used
460 * in the current atomic state. This allows those resources
461 * to be re-acquired by the new atomic state immediately
462 * without waiting for the resources to be fully released.
463 */
464 else if (IS_ERR_OR_NULL(val) && (type < SDE_HW_BLK_MAX)) {
465 list_for_each_entry(old_rp, rp->rp_head, rp_list) {
466 if (old_rp == rp)
467 continue;
468
469 list_for_each_entry(res, &old_rp->res_list, list) {
470 if (res->type != type)
471 continue;
472 SDE_DEBUG(
473 "crtc%d.%u found res:0x%x//%pK/ in crtc%d.%d\n",
474 crtc->base.id,
475 rp->sequence_id,
476 res->type, res->val,
477 crtc->base.id,
478 old_rp->sequence_id);
479 SDE_EVT32_VERBOSE(crtc->base.id,
480 rp->sequence_id,
481 res->type, res->val,
482 crtc->base.id,
483 old_rp->sequence_id);
484 if (res->ops.get)
485 res->ops.get(res->val, 0, -1);
486 val = res->val;
487 break;
488 }
489
490 if (!IS_ERR_OR_NULL(val))
491 break;
492 }
493 }
Alan Kwongcdb2f282017-03-18 13:42:06 -0700494 if (IS_ERR_OR_NULL(val)) {
Alan Kwong42e35052017-05-05 06:52:51 -0700495 SDE_DEBUG("crtc%d.%u failed to get res:0x%x//\n",
Alan Kwongcdb2f282017-03-18 13:42:06 -0700496 crtc->base.id, rp->sequence_id, type);
Alan Kwong310e9b02017-08-03 02:04:07 -0400497 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700498 return NULL;
499 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400500add_res:
501 rc = _sde_crtc_rp_add_no_lock(rp, type, tag, val, &rp->ops);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700502 if (rc) {
503 SDE_ERROR("crtc%d.%u failed to add res:0x%x/0x%llx\n",
504 crtc->base.id, rp->sequence_id, type, tag);
505 if (rp->ops.put)
506 rp->ops.put(val);
507 val = NULL;
508 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400509 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700510 return val;
511}
512
513/**
514 * _sde_crtc_rp_put - return given resource to resource pool
515 * @rp: Pointer to original resource pool
516 * @type: Resource type
517 * @tag: Search tag for given resource
518 * return: None
519 */
520static void _sde_crtc_rp_put(struct sde_crtc_respool *rp, u32 type, u64 tag)
521{
522 struct sde_crtc_res *res, *next;
523 struct drm_crtc *crtc;
524
525 if (!rp) {
526 SDE_ERROR("invalid resource pool\n");
527 return;
528 }
529
530 crtc = _sde_crtc_rp_to_crtc(rp);
531 if (!crtc) {
532 SDE_ERROR("invalid crtc\n");
533 return;
534 }
535
Alan Kwong310e9b02017-08-03 02:04:07 -0400536 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700537 list_for_each_entry_safe(res, next, &rp->res_list, list) {
538 if (res->type != type || res->tag != tag)
539 continue;
540 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
541 crtc->base.id, rp->sequence_id,
542 res->type, res->tag, res->val,
543 atomic_read(&res->refcount));
544 if (res->flags & SDE_CRTC_RES_FLAG_FREE)
545 SDE_ERROR(
546 "crtc%d.%u already free res:0x%x/0x%llx/%pK/%d\n",
547 crtc->base.id, rp->sequence_id,
548 res->type, res->tag, res->val,
549 atomic_read(&res->refcount));
550 else if (atomic_dec_return(&res->refcount) == 0)
551 res->flags |= SDE_CRTC_RES_FLAG_FREE;
552
Alan Kwong310e9b02017-08-03 02:04:07 -0400553 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700554 return;
555 }
556 SDE_ERROR("crtc%d.%u not found res:0x%x/0x%llx\n",
557 crtc->base.id, rp->sequence_id, type, tag);
Alan Kwong310e9b02017-08-03 02:04:07 -0400558 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700559}
560
561int sde_crtc_res_add(struct drm_crtc_state *state, u32 type, u64 tag,
562 void *val, struct sde_crtc_res_ops *ops)
563{
564 struct sde_crtc_respool *rp;
565
566 if (!state) {
567 SDE_ERROR("invalid parameters\n");
568 return -EINVAL;
569 }
570
571 rp = &to_sde_crtc_state(state)->rp;
572 return _sde_crtc_rp_add(rp, type, tag, val, ops);
573}
574
575void *sde_crtc_res_get(struct drm_crtc_state *state, u32 type, u64 tag)
576{
577 struct sde_crtc_respool *rp;
578 void *val;
579
580 if (!state) {
581 SDE_ERROR("invalid parameters\n");
582 return NULL;
583 }
584
585 rp = &to_sde_crtc_state(state)->rp;
586 val = _sde_crtc_rp_get(rp, type, tag);
587 if (IS_ERR(val)) {
588 SDE_ERROR("failed to get res type:0x%x:0x%llx\n",
589 type, tag);
590 return NULL;
591 }
592
593 return val;
594}
595
596void sde_crtc_res_put(struct drm_crtc_state *state, u32 type, u64 tag)
597{
598 struct sde_crtc_respool *rp;
599
600 if (!state) {
601 SDE_ERROR("invalid parameters\n");
602 return;
603 }
604
605 rp = &to_sde_crtc_state(state)->rp;
606 _sde_crtc_rp_put(rp, type, tag);
607}
608
Clarence Ipa18d4832017-03-13 12:35:44 -0700609static void _sde_crtc_deinit_events(struct sde_crtc *sde_crtc)
610{
611 if (!sde_crtc)
612 return;
Clarence Ipa18d4832017-03-13 12:35:44 -0700613}
614
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700615static void sde_crtc_destroy(struct drm_crtc *crtc)
616{
617 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
618
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400619 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400620
621 if (!crtc)
622 return;
623
Dhaval Patele4a5dda2016-10-13 19:29:30 -0700624 if (sde_crtc->blob_info)
625 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400626 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700627 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700628
Clarence Ip24f80662016-06-13 19:05:32 -0400629 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ipa18d4832017-03-13 12:35:44 -0700630 _sde_crtc_deinit_events(sde_crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400631
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700632 drm_crtc_cleanup(crtc);
Clarence Ip7f70ce42017-03-20 06:53:46 -0700633 mutex_destroy(&sde_crtc->crtc_lock);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700634 kfree(sde_crtc);
635}
636
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700637static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
638 const struct drm_display_mode *mode,
639 struct drm_display_mode *adjusted_mode)
640{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400641 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400642
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400643 if (msm_is_mode_seamless(adjusted_mode) &&
644 (!crtc->enabled || crtc->state->active_changed)) {
645 SDE_ERROR("crtc state prevents seamless transition\n");
646 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400647 }
648
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700649 return true;
650}
651
Dhaval Patel48c76022016-09-01 17:51:23 -0700652static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
653 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400654{
Dhaval Patel48c76022016-09-01 17:51:23 -0700655 uint32_t blend_op, fg_alpha, bg_alpha;
656 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700657 struct sde_hw_mixer *lm = mixer->hw_lm;
658
Dhaval Patel48c76022016-09-01 17:51:23 -0700659 /* default to opaque blending */
660 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
661 bg_alpha = 0xFF - fg_alpha;
662 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
663 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700664
Dhaval Patel48c76022016-09-01 17:51:23 -0700665 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
666
667 switch (blend_type) {
668
669 case SDE_DRM_BLEND_OP_OPAQUE:
670 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
671 SDE_BLEND_BG_ALPHA_BG_CONST;
672 break;
673
674 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
675 if (format->alpha_enable) {
676 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
677 SDE_BLEND_BG_ALPHA_FG_PIXEL;
678 if (fg_alpha != 0xff) {
679 bg_alpha = fg_alpha;
680 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
681 SDE_BLEND_BG_INV_MOD_ALPHA;
682 } else {
683 blend_op |= SDE_BLEND_BG_INV_ALPHA;
684 }
685 }
686 break;
687
688 case SDE_DRM_BLEND_OP_COVERAGE:
689 if (format->alpha_enable) {
690 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
691 SDE_BLEND_BG_ALPHA_FG_PIXEL;
692 if (fg_alpha != 0xff) {
693 bg_alpha = fg_alpha;
694 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
695 SDE_BLEND_FG_INV_MOD_ALPHA |
696 SDE_BLEND_BG_MOD_ALPHA |
697 SDE_BLEND_BG_INV_MOD_ALPHA;
698 } else {
699 blend_op |= SDE_BLEND_BG_INV_ALPHA;
700 }
701 }
702 break;
703 default:
704 /* do nothing */
705 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400706 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700707
708 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
709 bg_alpha, blend_op);
Dhaval Patel6c666622017-03-21 23:02:59 -0700710 SDE_DEBUG(
711 "format: %4.4s, alpha_enable %u fg alpha:0x%x bg alpha:0x%x blend_op:0x%x\n",
712 (char *) &format->base.pixel_format,
Dhaval Patel48c76022016-09-01 17:51:23 -0700713 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
714}
715
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800716static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
717 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
718 struct sde_hw_dim_layer *dim_layer)
719{
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500720 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800721 struct sde_hw_mixer *lm;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800722 struct sde_hw_dim_layer split_dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800723 int i;
724
725 if (!dim_layer->rect.w || !dim_layer->rect.h) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700726 SDE_DEBUG("empty dim_layer\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800727 return;
728 }
729
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500730 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800731
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700732 SDE_DEBUG("dim_layer - flags:%d, stage:%d\n",
733 dim_layer->flags, dim_layer->stage);
734
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800735 split_dim_layer.stage = dim_layer->stage;
736 split_dim_layer.color_fill = dim_layer->color_fill;
737
738 /*
739 * traverse through the layer mixers attached to crtc and find the
740 * intersecting dim layer rect in each LM and program accordingly.
741 */
742 for (i = 0; i < sde_crtc->num_mixers; i++) {
743 split_dim_layer.flags = dim_layer->flags;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800744
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500745 sde_kms_rect_intersect(&cstate->lm_bounds[i], &dim_layer->rect,
Lloyd Atkinsone0e11e22017-01-17 12:08:48 -0500746 &split_dim_layer.rect);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500747 if (sde_kms_rect_is_null(&split_dim_layer.rect)) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800748 /*
749 * no extra programming required for non-intersecting
750 * layer mixers with INCLUSIVE dim layer
751 */
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500752 if (split_dim_layer.flags & SDE_DRM_DIM_LAYER_INCLUSIVE)
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800753 continue;
754
755 /*
756 * program the other non-intersecting layer mixers with
757 * INCLUSIVE dim layer of full size for uniformity
758 * with EXCLUSIVE dim layer config.
759 */
760 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
761 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500762 memcpy(&split_dim_layer.rect, &cstate->lm_bounds[i],
763 sizeof(split_dim_layer.rect));
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800764
765 } else {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500766 split_dim_layer.rect.x =
767 split_dim_layer.rect.x -
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700768 cstate->lm_bounds[i].x;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800769 }
770
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700771 SDE_DEBUG("split_dim_layer - LM:%d, rect:{%d,%d,%d,%d}}\n",
772 i, split_dim_layer.rect.x, split_dim_layer.rect.y,
773 split_dim_layer.rect.w, split_dim_layer.rect.h);
774
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800775 lm = mixer[i].hw_lm;
776 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
777 lm->ops.setup_dim_layer(lm, &split_dim_layer);
778 }
779}
780
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400781void sde_crtc_get_crtc_roi(struct drm_crtc_state *state,
782 const struct sde_rect **crtc_roi)
783{
784 struct sde_crtc_state *crtc_state;
785
786 if (!state || !crtc_roi)
787 return;
788
789 crtc_state = to_sde_crtc_state(state);
790 *crtc_roi = &crtc_state->crtc_roi;
791}
792
793static int _sde_crtc_set_roi_v1(struct drm_crtc_state *state,
794 void *usr_ptr)
795{
796 struct drm_crtc *crtc;
797 struct sde_crtc_state *cstate;
798 struct sde_drm_roi_v1 roi_v1;
799 int i;
800
801 if (!state) {
802 SDE_ERROR("invalid args\n");
803 return -EINVAL;
804 }
805
806 cstate = to_sde_crtc_state(state);
807 crtc = cstate->base.crtc;
808
809 memset(&cstate->user_roi_list, 0, sizeof(cstate->user_roi_list));
810
811 if (!usr_ptr) {
812 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
813 return 0;
814 }
815
816 if (copy_from_user(&roi_v1, usr_ptr, sizeof(roi_v1))) {
817 SDE_ERROR("crtc%d: failed to copy roi_v1 data\n", DRMID(crtc));
818 return -EINVAL;
819 }
820
821 SDE_DEBUG("crtc%d: num_rects %d\n", DRMID(crtc), roi_v1.num_rects);
822
823 if (roi_v1.num_rects == 0) {
824 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
825 return 0;
826 }
827
828 if (roi_v1.num_rects > SDE_MAX_ROI_V1) {
829 SDE_ERROR("crtc%d: too many rects specified: %d\n", DRMID(crtc),
830 roi_v1.num_rects);
831 return -EINVAL;
832 }
833
834 cstate->user_roi_list.num_rects = roi_v1.num_rects;
835 for (i = 0; i < roi_v1.num_rects; ++i) {
836 cstate->user_roi_list.roi[i] = roi_v1.roi[i];
837 SDE_DEBUG("crtc%d: roi%d: roi (%d,%d) (%d,%d)\n",
838 DRMID(crtc), i,
839 cstate->user_roi_list.roi[i].x1,
840 cstate->user_roi_list.roi[i].y1,
841 cstate->user_roi_list.roi[i].x2,
842 cstate->user_roi_list.roi[i].y2);
843 }
844
845 return 0;
846}
847
Ingrid Gallardo83532222017-06-02 16:48:51 -0700848static bool _sde_crtc_setup_is_3dmux_dsc(struct drm_crtc_state *state)
849{
850 int i;
851 struct sde_crtc_state *cstate;
852 bool is_3dmux_dsc = false;
853
854 cstate = to_sde_crtc_state(state);
855
856 for (i = 0; i < cstate->num_connectors; i++) {
857 struct drm_connector *conn = cstate->connectors[i];
858
859 if (sde_connector_get_topology_name(conn) ==
860 SDE_RM_TOPOLOGY_DUALPIPE_3DMERGE_DSC)
861 is_3dmux_dsc = true;
862 }
863
864 return is_3dmux_dsc;
865}
866
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400867static int _sde_crtc_set_crtc_roi(struct drm_crtc *crtc,
868 struct drm_crtc_state *state)
869{
870 struct drm_connector *conn;
871 struct drm_connector_state *conn_state;
872 struct sde_crtc *sde_crtc;
873 struct sde_crtc_state *crtc_state;
874 struct sde_rect *crtc_roi;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400875 int i, num_attached_conns = 0;
876
877 if (!crtc || !state)
878 return -EINVAL;
879
880 sde_crtc = to_sde_crtc(crtc);
881 crtc_state = to_sde_crtc_state(state);
882 crtc_roi = &crtc_state->crtc_roi;
883
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400884 for_each_connector_in_state(state->state, conn, conn_state, i) {
885 struct sde_connector_state *sde_conn_state;
886
887 if (!conn_state || conn_state->crtc != crtc)
888 continue;
889
890 if (num_attached_conns) {
891 SDE_ERROR(
892 "crtc%d: unsupported: roi on crtc w/ >1 connectors\n",
893 DRMID(crtc));
894 return -EINVAL;
895 }
896 ++num_attached_conns;
897
898 sde_conn_state = to_sde_connector_state(conn_state);
899
Ingrid Gallardo83532222017-06-02 16:48:51 -0700900 /*
901 * current driver only supports same connector and crtc size,
902 * but if support for different sizes is added, driver needs
903 * to check the connector roi here to make sure is full screen
904 * for dsc 3d-mux topology that doesn't support partial update.
905 */
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400906 if (memcmp(&sde_conn_state->rois, &crtc_state->user_roi_list,
907 sizeof(crtc_state->user_roi_list))) {
908 SDE_ERROR("%s: crtc -> conn roi scaling unsupported\n",
909 sde_crtc->name);
910 return -EINVAL;
911 }
912 }
913
Lloyd Atkinsonc2baf412017-04-19 17:53:09 -0400914 sde_kms_rect_merge_rectangles(&crtc_state->user_roi_list, crtc_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400915
Ingrid Gallardo83532222017-06-02 16:48:51 -0700916 /*
917 * for 3dmux dsc, make sure is full ROI, since current driver doesn't
918 * support partial update for this configuration.
919 */
920 if (!sde_kms_rect_is_null(crtc_roi) &&
921 _sde_crtc_setup_is_3dmux_dsc(state)) {
922 struct drm_display_mode *adj_mode = &state->adjusted_mode;
923
924 if (crtc_roi->w != adj_mode->hdisplay ||
925 crtc_roi->h != adj_mode->vdisplay) {
926 SDE_ERROR("%s: unsupported top roi[%d %d] wxh[%d %d]\n",
927 sde_crtc->name, crtc_roi->w, crtc_roi->h,
928 adj_mode->hdisplay, adj_mode->vdisplay);
929 return -EINVAL;
930 }
931 }
932
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400933 SDE_DEBUG("%s: crtc roi (%d,%d,%d,%d)\n", sde_crtc->name,
934 crtc_roi->x, crtc_roi->y, crtc_roi->w, crtc_roi->h);
935
936 return 0;
937}
938
Lloyd Atkinson77382202017-02-01 14:59:43 -0500939static int _sde_crtc_check_autorefresh(struct drm_crtc *crtc,
940 struct drm_crtc_state *state)
941{
942 struct sde_crtc *sde_crtc;
943 struct sde_crtc_state *crtc_state;
944 struct drm_connector *conn;
945 struct drm_connector_state *conn_state;
946 int i;
947
948 if (!crtc || !state)
949 return -EINVAL;
950
951 sde_crtc = to_sde_crtc(crtc);
952 crtc_state = to_sde_crtc_state(state);
953
954 if (sde_kms_rect_is_null(&crtc_state->crtc_roi))
955 return 0;
956
957 /* partial update active, check if autorefresh is also requested */
958 for_each_connector_in_state(state->state, conn, conn_state, i) {
959 uint64_t autorefresh;
960
961 if (!conn_state || conn_state->crtc != crtc)
962 continue;
963
964 autorefresh = sde_connector_get_property(conn_state,
965 CONNECTOR_PROP_AUTOREFRESH);
966 if (autorefresh) {
967 SDE_ERROR(
968 "%s: autorefresh & partial crtc roi incompatible %llu\n",
969 sde_crtc->name, autorefresh);
970 return -EINVAL;
971 }
972 }
973
974 return 0;
975}
976
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400977static int _sde_crtc_set_lm_roi(struct drm_crtc *crtc,
978 struct drm_crtc_state *state, int lm_idx)
979{
980 struct sde_crtc *sde_crtc;
981 struct sde_crtc_state *crtc_state;
982 const struct sde_rect *crtc_roi;
983 const struct sde_rect *lm_bounds;
984 struct sde_rect *lm_roi;
985
986 if (!crtc || !state || lm_idx >= ARRAY_SIZE(crtc_state->lm_bounds))
987 return -EINVAL;
988
989 sde_crtc = to_sde_crtc(crtc);
990 crtc_state = to_sde_crtc_state(state);
991 crtc_roi = &crtc_state->crtc_roi;
992 lm_bounds = &crtc_state->lm_bounds[lm_idx];
993 lm_roi = &crtc_state->lm_roi[lm_idx];
994
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500995 if (sde_kms_rect_is_null(crtc_roi))
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400996 memcpy(lm_roi, lm_bounds, sizeof(*lm_roi));
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500997 else
998 sde_kms_rect_intersect(crtc_roi, lm_bounds, lm_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400999
1000 SDE_DEBUG("%s: lm%d roi (%d,%d,%d,%d)\n", sde_crtc->name, lm_idx,
1001 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1002
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001003 /* if any dimension is zero, clear all dimensions for clarity */
1004 if (sde_kms_rect_is_null(lm_roi))
1005 memset(lm_roi, 0, sizeof(*lm_roi));
1006
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001007 return 0;
1008}
1009
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001010static u32 _sde_crtc_get_displays_affected(struct drm_crtc *crtc,
1011 struct drm_crtc_state *state)
1012{
1013 struct sde_crtc *sde_crtc;
1014 struct sde_crtc_state *crtc_state;
1015 u32 disp_bitmask = 0;
1016 int i;
1017
1018 sde_crtc = to_sde_crtc(crtc);
1019 crtc_state = to_sde_crtc_state(state);
1020
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001021 /* pingpong split: one ROI, one LM, two physical displays */
1022 if (crtc_state->is_ppsplit) {
1023 u32 lm_split_width = crtc_state->lm_bounds[0].w / 2;
1024 struct sde_rect *roi = &crtc_state->lm_roi[0];
1025
1026 if (sde_kms_rect_is_null(roi))
1027 disp_bitmask = 0;
1028 else if ((u32)roi->x + (u32)roi->w <= lm_split_width)
1029 disp_bitmask = BIT(0); /* left only */
1030 else if (roi->x >= lm_split_width)
1031 disp_bitmask = BIT(1); /* right only */
1032 else
1033 disp_bitmask = BIT(0) | BIT(1); /* left and right */
1034 } else {
1035 for (i = 0; i < sde_crtc->num_mixers; i++) {
1036 if (!sde_kms_rect_is_null(&crtc_state->lm_roi[i]))
1037 disp_bitmask |= BIT(i);
1038 }
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001039 }
1040
1041 SDE_DEBUG("affected displays 0x%x\n", disp_bitmask);
1042
1043 return disp_bitmask;
1044}
1045
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001046static int _sde_crtc_check_rois_centered_and_symmetric(struct drm_crtc *crtc,
1047 struct drm_crtc_state *state)
1048{
1049 struct sde_crtc *sde_crtc;
1050 struct sde_crtc_state *crtc_state;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001051 const struct sde_rect *roi[CRTC_DUAL_MIXERS];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001052
1053 if (!crtc || !state)
1054 return -EINVAL;
1055
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001056 sde_crtc = to_sde_crtc(crtc);
1057 crtc_state = to_sde_crtc_state(state);
1058
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001059 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1060 SDE_ERROR("%s: unsupported number of mixers: %d\n",
1061 sde_crtc->name, sde_crtc->num_mixers);
1062 return -EINVAL;
1063 }
1064
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001065 /*
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001066 * If using pingpong split: one ROI, one LM, two physical displays
1067 * then the ROI must be centered on the panel split boundary and
1068 * be of equal width across the split.
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001069 */
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001070 if (crtc_state->is_ppsplit) {
1071 u16 panel_split_width;
1072 u32 display_mask;
1073
1074 roi[0] = &crtc_state->lm_roi[0];
1075
1076 if (sde_kms_rect_is_null(roi[0]))
1077 return 0;
1078
1079 display_mask = _sde_crtc_get_displays_affected(crtc, state);
1080 if (display_mask != (BIT(0) | BIT(1)))
1081 return 0;
1082
1083 panel_split_width = crtc_state->lm_bounds[0].w / 2;
1084 if (roi[0]->x + roi[0]->w / 2 != panel_split_width) {
1085 SDE_ERROR("%s: roi x %d w %d split %d\n",
1086 sde_crtc->name, roi[0]->x, roi[0]->w,
1087 panel_split_width);
1088 return -EINVAL;
1089 }
1090
1091 return 0;
1092 }
1093
1094 /*
1095 * On certain HW, if using 2 LM, ROIs must be split evenly between the
1096 * LMs and be of equal width.
1097 */
Clarence Ipffb87422017-06-30 13:37:48 -04001098 if (sde_crtc->num_mixers < 2)
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001099 return 0;
1100
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001101 roi[0] = &crtc_state->lm_roi[0];
1102 roi[1] = &crtc_state->lm_roi[1];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001103
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001104 /* if one of the roi is null it's a left/right-only update */
1105 if (sde_kms_rect_is_null(roi[0]) || sde_kms_rect_is_null(roi[1]))
1106 return 0;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001107
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001108 /* check lm rois are equal width & first roi ends at 2nd roi */
1109 if (roi[0]->x + roi[0]->w != roi[1]->x || roi[0]->w != roi[1]->w) {
1110 SDE_ERROR(
1111 "%s: rois not centered and symmetric: roi0 x %d w %d roi1 x %d w %d\n",
1112 sde_crtc->name, roi[0]->x, roi[0]->w,
1113 roi[1]->x, roi[1]->w);
1114 return -EINVAL;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001115 }
1116
1117 return 0;
1118}
1119
1120static int _sde_crtc_check_planes_within_crtc_roi(struct drm_crtc *crtc,
1121 struct drm_crtc_state *state)
1122{
1123 struct sde_crtc *sde_crtc;
1124 struct sde_crtc_state *crtc_state;
1125 const struct sde_rect *crtc_roi;
1126 struct drm_plane_state *pstate;
1127 struct drm_plane *plane;
1128
1129 if (!crtc || !state)
1130 return -EINVAL;
1131
1132 /*
1133 * Reject commit if a Plane CRTC destination coordinates fall outside
1134 * the partial CRTC ROI. LM output is determined via connector ROIs,
1135 * if they are specified, not Plane CRTC ROIs.
1136 */
1137
1138 sde_crtc = to_sde_crtc(crtc);
1139 crtc_state = to_sde_crtc_state(state);
1140 crtc_roi = &crtc_state->crtc_roi;
1141
1142 if (sde_kms_rect_is_null(crtc_roi))
1143 return 0;
1144
1145 drm_atomic_crtc_state_for_each_plane(plane, state) {
1146 struct sde_rect plane_roi, intersection;
1147
1148 pstate = drm_atomic_get_plane_state(state->state, plane);
1149 if (IS_ERR_OR_NULL(pstate)) {
1150 int rc = PTR_ERR(pstate);
1151
1152 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1153 sde_crtc->name, plane->base.id, rc);
1154 return rc;
1155 }
1156
1157 plane_roi.x = pstate->crtc_x;
1158 plane_roi.y = pstate->crtc_y;
1159 plane_roi.w = pstate->crtc_w;
1160 plane_roi.h = pstate->crtc_h;
1161 sde_kms_rect_intersect(crtc_roi, &plane_roi, &intersection);
1162 if (!sde_kms_rect_is_equal(&plane_roi, &intersection)) {
1163 SDE_ERROR(
1164 "%s: plane%d crtc roi (%d,%d,%d,%d) outside crtc roi (%d,%d,%d,%d)\n",
1165 sde_crtc->name, plane->base.id,
1166 plane_roi.x, plane_roi.y,
1167 plane_roi.w, plane_roi.h,
1168 crtc_roi->x, crtc_roi->y,
1169 crtc_roi->w, crtc_roi->h);
1170 return -E2BIG;
1171 }
1172 }
1173
1174 return 0;
1175}
1176
1177static int _sde_crtc_check_rois(struct drm_crtc *crtc,
1178 struct drm_crtc_state *state)
1179{
1180 struct sde_crtc *sde_crtc;
1181 int lm_idx;
1182 int rc;
1183
1184 if (!crtc || !state)
1185 return -EINVAL;
1186
1187 sde_crtc = to_sde_crtc(crtc);
1188
1189 rc = _sde_crtc_set_crtc_roi(crtc, state);
1190 if (rc)
1191 return rc;
1192
Lloyd Atkinson77382202017-02-01 14:59:43 -05001193 rc = _sde_crtc_check_autorefresh(crtc, state);
1194 if (rc)
1195 return rc;
1196
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001197 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1198 rc = _sde_crtc_set_lm_roi(crtc, state, lm_idx);
1199 if (rc)
1200 return rc;
1201 }
1202
1203 rc = _sde_crtc_check_rois_centered_and_symmetric(crtc, state);
1204 if (rc)
1205 return rc;
1206
1207 rc = _sde_crtc_check_planes_within_crtc_roi(crtc, state);
1208 if (rc)
1209 return rc;
1210
1211 return 0;
1212}
1213
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001214static void _sde_crtc_program_lm_output_roi(struct drm_crtc *crtc)
1215{
1216 struct sde_crtc *sde_crtc;
1217 struct sde_crtc_state *crtc_state;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001218 const struct sde_rect *lm_roi;
1219 struct sde_hw_mixer *hw_lm;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001220 int lm_idx, lm_horiz_position;
1221
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001222 if (!crtc)
1223 return;
1224
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001225 sde_crtc = to_sde_crtc(crtc);
1226 crtc_state = to_sde_crtc_state(crtc->state);
1227
1228 lm_horiz_position = 0;
1229 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001230 struct sde_hw_mixer_cfg cfg;
1231
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001232 lm_roi = &crtc_state->lm_roi[lm_idx];
1233 hw_lm = sde_crtc->mixers[lm_idx].hw_lm;
1234
1235 SDE_EVT32(DRMID(crtc_state->base.crtc), lm_idx,
1236 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1237
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001238 if (sde_kms_rect_is_null(lm_roi))
1239 continue;
1240
Ping Lif41c2ef2017-05-04 14:40:45 -07001241 hw_lm->cfg.out_width = lm_roi->w;
1242 hw_lm->cfg.out_height = lm_roi->h;
1243 hw_lm->cfg.right_mixer = lm_horiz_position;
1244
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001245 cfg.out_width = lm_roi->w;
1246 cfg.out_height = lm_roi->h;
1247 cfg.right_mixer = lm_horiz_position++;
1248 cfg.flags = 0;
1249 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
1250 }
1251}
1252
Dhaval Patel48c76022016-09-01 17:51:23 -07001253static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
1254 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
1255{
1256 struct drm_plane *plane;
Dhaval Patel6c666622017-03-21 23:02:59 -07001257 struct drm_framebuffer *fb;
1258 struct drm_plane_state *state;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001259 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -07001260 struct sde_plane_state *pstate = NULL;
1261 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001262 struct sde_hw_ctl *ctl;
1263 struct sde_hw_mixer *lm;
1264 struct sde_hw_stage_cfg *stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001265 struct sde_rect plane_crtc_roi;
Dhaval Patel48c76022016-09-01 17:51:23 -07001266
Clarence Ip7e5f0002017-05-29 18:46:56 -04001267 u32 flush_mask, flush_sbuf, flush_tmp;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001268 uint32_t stage_idx, lm_idx;
1269 int zpos_cnt[SDE_STAGE_MAX + 1] = { 0 };
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001270 int i;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001271 bool bg_alpha_enable = false;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001272 u32 prefill = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001273
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001274 if (!sde_crtc || !mixer) {
1275 SDE_ERROR("invalid sde_crtc or mixer\n");
1276 return;
1277 }
1278
1279 ctl = mixer->hw_ctl;
1280 lm = mixer->hw_lm;
1281 stage_cfg = &sde_crtc->stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001282 cstate = to_sde_crtc_state(crtc->state);
Clarence Ip7e5f0002017-05-29 18:46:56 -04001283 flush_sbuf = 0x0;
Dhaval Patel44f12472016-08-29 12:19:47 -07001284
Clarence Ip7eb90452017-05-23 11:41:19 -04001285 cstate->sbuf_cfg.rot_op_mode = SDE_CTL_ROT_OP_MODE_OFFLINE;
1286 cstate->sbuf_prefill_line = 0;
1287
Dhaval Patel44f12472016-08-29 12:19:47 -07001288 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel6c666622017-03-21 23:02:59 -07001289 state = plane->state;
1290 if (!state)
1291 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001292
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001293 plane_crtc_roi.x = state->crtc_x;
1294 plane_crtc_roi.y = state->crtc_y;
1295 plane_crtc_roi.w = state->crtc_w;
1296 plane_crtc_roi.h = state->crtc_h;
1297
Dhaval Patel6c666622017-03-21 23:02:59 -07001298 pstate = to_sde_plane_state(state);
1299 fb = state->fb;
Dhaval Patel44f12472016-08-29 12:19:47 -07001300
Alan Kwong4dd64c82017-02-04 18:41:51 -08001301 if (sde_plane_is_sbuf_mode(plane, &prefill))
Clarence Ip7eb90452017-05-23 11:41:19 -04001302 cstate->sbuf_cfg.rot_op_mode =
1303 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
1304 if (prefill > cstate->sbuf_prefill_line)
1305 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001306
Clarence Ip7e5f0002017-05-29 18:46:56 -04001307 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001308
Clarence Ip7e5f0002017-05-29 18:46:56 -04001309 /* persist rotator flush bit(s) for one more commit */
1310 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1311 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001312
1313 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001314 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001315 pstate->stage,
1316 plane->base.id,
1317 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001318 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001319
Dhaval Patel48c76022016-09-01 17:51:23 -07001320 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07001321 if (!format) {
1322 SDE_ERROR("invalid format\n");
1323 return;
1324 }
1325
Dhaval Patel572cfd22017-06-12 19:33:39 -07001326 if (pstate->stage == SDE_STAGE_BASE && format->alpha_enable)
1327 bg_alpha_enable = true;
Dhaval Patel44f12472016-08-29 12:19:47 -07001328
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001329 SDE_EVT32(DRMID(crtc), DRMID(plane),
1330 state->fb ? state->fb->base.id : -1,
1331 state->src_x >> 16, state->src_y >> 16,
1332 state->src_w >> 16, state->src_h >> 16,
1333 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001334 state->crtc_w, state->crtc_h,
Clarence Ipeb39cce2017-07-19 14:12:43 -04001335 flush_tmp ? cstate->sbuf_cfg.rot_op_mode :
1336 SDE_CTL_ROT_OP_MODE_OFFLINE);
Dhaval Patel6c666622017-03-21 23:02:59 -07001337
Dhaval Patel572cfd22017-06-12 19:33:39 -07001338 stage_idx = zpos_cnt[pstate->stage]++;
1339 stage_cfg->stage[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001340 sde_plane_pipe(plane);
Dhaval Patel572cfd22017-06-12 19:33:39 -07001341 stage_cfg->multirect_index[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001342 pstate->multirect_index;
1343
Dhaval Patel572cfd22017-06-12 19:33:39 -07001344 SDE_EVT32(DRMID(crtc), DRMID(plane), stage_idx,
1345 sde_plane_pipe(plane) - SSPP_VIG0, pstate->stage,
1346 pstate->multirect_index, pstate->multirect_mode,
1347 format->base.pixel_format, fb ? fb->modifier[0] : 0);
1348
1349 /* blend config update */
1350 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1351 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
1352 format);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001353 mixer[lm_idx].flush_mask |= flush_mask;
1354
Dhaval Patel572cfd22017-06-12 19:33:39 -07001355 if (bg_alpha_enable && !format->alpha_enable)
1356 mixer[lm_idx].mixer_op_mode = 0;
1357 else
1358 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001359 1 << pstate->stage;
Dhaval Patel48c76022016-09-01 17:51:23 -07001360 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001361 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001362
Clarence Ip7e5f0002017-05-29 18:46:56 -04001363 cstate->sbuf_flush_mask = flush_sbuf;
1364
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001365 if (lm && lm->ops.setup_dim_layer) {
1366 cstate = to_sde_crtc_state(crtc->state);
1367 for (i = 0; i < cstate->num_dim_layers; i++)
1368 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1369 mixer, &cstate->dim_layer[i]);
1370 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001371
Clarence Ip7eb90452017-05-23 11:41:19 -04001372 if (ctl->ops.setup_sbuf_cfg)
Alan Kwong4dd64c82017-02-04 18:41:51 -08001373 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001374
1375 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001376}
1377
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001378static void _sde_crtc_swap_mixers_for_right_partial_update(
1379 struct drm_crtc *crtc)
1380{
1381 struct sde_crtc *sde_crtc;
1382 struct sde_crtc_state *cstate;
1383 struct drm_encoder *drm_enc;
1384 bool is_right_only;
1385 bool encoder_in_dsc_merge = false;
1386
1387 if (!crtc || !crtc->state)
1388 return;
1389
1390 sde_crtc = to_sde_crtc(crtc);
1391 cstate = to_sde_crtc_state(crtc->state);
1392
1393 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1394 return;
1395
1396 drm_for_each_encoder(drm_enc, crtc->dev) {
1397 if (drm_enc->crtc == crtc &&
1398 sde_encoder_is_dsc_merge(drm_enc)) {
1399 encoder_in_dsc_merge = true;
1400 break;
1401 }
1402 }
1403
1404 /**
1405 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1406 * This is due to two reasons:
1407 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1408 * the left DSC must be used, right DSC cannot be used alone.
1409 * For right-only partial update, this means swap layer mixers to map
1410 * Left LM to Right INTF. On later HW this was relaxed.
1411 * - In DSC Merge mode, the physical encoder has already registered
1412 * PP0 as the master, to switch to right-only we would have to
1413 * reprogram to be driven by PP1 instead.
1414 * To support both cases, we prefer to support the mixer swap solution.
1415 */
1416 if (!encoder_in_dsc_merge)
1417 return;
1418
1419 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1420 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1421
1422 if (is_right_only && !sde_crtc->mixers_swapped) {
1423 /* right-only update swap mixers */
1424 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1425 sde_crtc->mixers_swapped = true;
1426 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1427 /* left-only or full update, swap back */
1428 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1429 sde_crtc->mixers_swapped = false;
1430 }
1431
1432 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1433 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1434 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1435 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1436 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1437 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1438 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1439}
1440
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001441/**
1442 * _sde_crtc_blend_setup - configure crtc mixers
1443 * @crtc: Pointer to drm crtc structure
1444 */
1445static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001446{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001447 struct sde_crtc *sde_crtc;
1448 struct sde_crtc_state *sde_crtc_state;
1449 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001450 struct sde_hw_ctl *ctl;
1451 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001452
1453 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001454
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001455 if (!crtc)
1456 return;
1457
1458 sde_crtc = to_sde_crtc(crtc);
1459 sde_crtc_state = to_sde_crtc_state(crtc->state);
1460 mixer = sde_crtc->mixers;
1461
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001462 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001463
Dhaval Patel48c76022016-09-01 17:51:23 -07001464 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1465 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1466 return;
1467 }
1468
1469 for (i = 0; i < sde_crtc->num_mixers; i++) {
1470 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1471 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1472 return;
1473 }
1474 mixer[i].mixer_op_mode = 0;
1475 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001476 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1477 mixer[i].hw_ctl->ops.clear_all_blendstages(
1478 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001479
1480 /* clear dim_layer settings */
1481 lm = mixer[i].hw_lm;
1482 if (lm->ops.clear_dim_layer)
1483 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001484 }
1485
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001486 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1487
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001488 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001489 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001490
Dhaval Patel48c76022016-09-01 17:51:23 -07001491 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1492
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001493 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001494 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1495
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001496 ctl = mixer[i].hw_ctl;
1497 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001498
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001499 if (sde_kms_rect_is_null(lm_roi)) {
1500 SDE_DEBUG(
1501 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1502 sde_crtc->name, lm->idx - LM_0,
1503 ctl->idx - CTL_0);
1504 continue;
1505 }
1506
Dhaval Patel48c76022016-09-01 17:51:23 -07001507 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001508
Dhaval Patel48c76022016-09-01 17:51:23 -07001509 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001510 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001511
1512 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001513 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1514
Clarence Ip8e69ad02016-12-09 09:43:57 -05001515 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1516 mixer[i].hw_lm->idx - LM_0,
1517 mixer[i].mixer_op_mode,
1518 ctl->idx - CTL_0,
1519 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001520
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001521 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel572cfd22017-06-12 19:33:39 -07001522 &sde_crtc->stage_cfg);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001523 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001524
1525 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001526}
1527
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001528static int _sde_crtc_find_plane_fb_modes(struct drm_crtc_state *state,
1529 uint32_t *fb_ns,
1530 uint32_t *fb_sec,
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001531 uint32_t *fb_sec_dir)
1532{
1533 struct drm_plane *plane;
1534 const struct drm_plane_state *pstate;
1535 struct sde_plane_state *sde_pstate;
1536 uint32_t mode = 0;
1537 int rc;
1538
1539 if (!state) {
1540 SDE_ERROR("invalid state\n");
1541 return -EINVAL;
1542 }
1543
1544 *fb_ns = 0;
1545 *fb_sec = 0;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001546 *fb_sec_dir = 0;
1547 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
1548 if (IS_ERR_OR_NULL(pstate)) {
1549 rc = PTR_ERR(pstate);
1550 SDE_ERROR("crtc%d failed to get plane%d state%d\n",
1551 state->crtc->base.id,
1552 plane->base.id, rc);
1553 return rc;
1554 }
1555 sde_pstate = to_sde_plane_state(pstate);
1556 mode = sde_plane_get_property(sde_pstate,
1557 PLANE_PROP_FB_TRANSLATION_MODE);
1558 switch (mode) {
1559 case SDE_DRM_FB_NON_SEC:
1560 (*fb_ns)++;
1561 break;
1562 case SDE_DRM_FB_SEC:
1563 (*fb_sec)++;
1564 break;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001565 case SDE_DRM_FB_SEC_DIR_TRANS:
1566 (*fb_sec_dir)++;
1567 break;
1568 default:
1569 SDE_ERROR("Error: Plane[%d], fb_trans_mode:%d",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001570 plane->base.id, mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001571 return -EINVAL;
1572 }
1573 }
1574 return 0;
1575}
1576
1577/**
1578 * sde_crtc_get_secure_transition_ops - determines the operations that
1579 * need to be performed before transitioning to secure state
1580 * This function should be called after swapping the new state
1581 * @crtc: Pointer to drm crtc structure
1582 * Returns the bitmask of operations need to be performed, -Error in
1583 * case of error cases
1584 */
1585int sde_crtc_get_secure_transition_ops(struct drm_crtc *crtc,
1586 struct drm_crtc_state *old_crtc_state,
1587 bool old_valid_fb)
1588{
1589 struct drm_plane *plane;
1590 struct drm_encoder *encoder;
1591 struct sde_crtc *sde_crtc;
1592 struct sde_crtc_state *cstate;
1593 struct sde_crtc_smmu_state_data *smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001594 uint32_t translation_mode = 0, secure_level;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001595 int ops = 0;
1596 bool post_commit = false;
1597
1598 if (!crtc || !crtc->state) {
1599 SDE_ERROR("invalid crtc\n");
1600 return -EINVAL;
1601 }
1602
1603 sde_crtc = to_sde_crtc(crtc);
1604 cstate = to_sde_crtc_state(crtc->state);
1605 smmu_state = &sde_crtc->smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001606 secure_level = sde_crtc_get_secure_level(crtc, crtc->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001607
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001608 SDE_DEBUG("crtc%d, secure_level%d old_valid_fb%d\n",
1609 crtc->base.id, secure_level, old_valid_fb);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001610
1611 /**
1612 * SMMU operations need to be delayed in case of
1613 * video mode panels when switching back to non_secure
1614 * mode
1615 */
1616 drm_for_each_encoder(encoder, crtc->dev) {
1617 if (encoder->crtc != crtc)
1618 continue;
1619
1620 post_commit &= !sde_encoder_is_cmd_mode(encoder);
1621 }
1622
1623 drm_atomic_crtc_for_each_plane(plane, crtc) {
1624 if (!plane->state)
1625 continue;
1626
1627 translation_mode = sde_plane_get_property(
1628 to_sde_plane_state(plane->state),
1629 PLANE_PROP_FB_TRANSLATION_MODE);
1630 if (translation_mode > SDE_DRM_FB_SEC_DIR_TRANS) {
1631 SDE_ERROR("crtc%d, invalid translation_mode%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001632 crtc->base.id, translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001633 return -EINVAL;
1634 }
1635
1636 /**
1637 * we can break if we find sec_fir or non_sec_dir
1638 * plane
1639 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001640 if (translation_mode == SDE_DRM_FB_SEC_DIR_TRANS)
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001641 break;
1642 }
1643
1644 switch (translation_mode) {
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001645 case SDE_DRM_FB_SEC_DIR_TRANS:
1646 /* secure display usecase */
1647 if ((smmu_state->state == ATTACHED) &&
1648 (secure_level == SDE_DRM_SEC_ONLY)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001649 smmu_state->state = DETACH_ALL_REQ;
1650 smmu_state->transition_type = PRE_COMMIT;
1651 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1652 if (old_valid_fb) {
1653 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1654 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1655 }
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001656 /* secure camera usecase */
1657 } else if (smmu_state->state == ATTACHED) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001658 smmu_state->state = DETACH_SEC_REQ;
1659 smmu_state->transition_type = PRE_COMMIT;
1660 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1661 }
1662 break;
1663 case SDE_DRM_FB_SEC:
1664 case SDE_DRM_FB_NON_SEC:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001665 if ((smmu_state->state == DETACHED_SEC) ||
1666 (smmu_state->state == DETACH_SEC_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001667 smmu_state->state = ATTACH_SEC_REQ;
1668 smmu_state->transition_type = post_commit ?
1669 POST_COMMIT : PRE_COMMIT;
1670 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001671 if (old_valid_fb)
1672 ops |= SDE_KMS_OPS_WAIT_FOR_TX_DONE;
1673 } else if ((smmu_state->state == DETACHED) ||
1674 (smmu_state->state == DETACH_ALL_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001675 smmu_state->state = ATTACH_ALL_REQ;
1676 smmu_state->transition_type = post_commit ?
1677 POST_COMMIT : PRE_COMMIT;
Veera Sundaram Sankaran47e718f2017-09-13 16:47:23 -07001678 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001679 if (old_valid_fb)
1680 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1681 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1682 }
1683 break;
1684 default:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001685 SDE_ERROR("invalid plane fb_mode:%d\n", translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001686 ops = 0;
1687 return -EINVAL;
1688 }
1689
1690 SDE_DEBUG("SMMU State:%d, type:%d ops:%x\n", smmu_state->state,
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001691 smmu_state->transition_type, ops);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001692 return ops;
1693}
1694
1695/**
1696 * _sde_crtc_scm_call - makes secure channel call to switch the VMIDs
1697 * @vimd: switch the stage 2 translation to this VMID.
1698 */
1699static int _sde_crtc_scm_call(int vmid)
1700{
1701 struct scm_desc desc = {0};
1702 uint32_t num_sids;
1703 uint32_t *sec_sid;
1704 uint32_t mem_protect_sd_ctrl_id = MEM_PROTECT_SD_CTRL_SWITCH;
1705 int ret = 0;
1706
1707 /* This info should be queried from catalog */
1708 num_sids = SEC_SID_CNT;
1709 sec_sid = kcalloc(num_sids, sizeof(uint32_t), GFP_KERNEL);
1710 if (!sec_sid)
1711 return -ENOMEM;
1712
1713 /**
1714 * derive this info from device tree/catalog, this is combination of
1715 * smr mask and SID for secure
1716 */
1717 sec_sid[0] = SEC_SID_MASK_0;
1718 sec_sid[1] = SEC_SID_MASK_1;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001719 dmac_flush_range(sec_sid, sec_sid + num_sids);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001720
1721 SDE_DEBUG("calling scm_call for vmid %d", vmid);
1722
1723 desc.arginfo = SCM_ARGS(4, SCM_VAL, SCM_RW, SCM_VAL, SCM_VAL);
1724 desc.args[0] = MDP_DEVICE_ID;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001725 desc.args[1] = SCM_BUFFER_PHYS(sec_sid);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001726 desc.args[2] = sizeof(uint32_t) * num_sids;
1727 desc.args[3] = vmid;
1728
1729 ret = scm_call2(SCM_SIP_FNID(SCM_SVC_MP,
1730 mem_protect_sd_ctrl_id), &desc);
1731 if (ret) {
1732 SDE_ERROR("Error:scm_call2, vmid (%lld): ret%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001733 desc.args[3], ret);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001734 }
1735
1736 kfree(sec_sid);
1737 return ret;
1738}
1739
1740/**
1741 * sde_crtc_secure_ctrl - Initiates the operations to swtich between secure
1742 * and non-secure mode
1743 * @crtc: Pointer to crtc
1744 * @post_commit: if this operation is triggered after commit
1745 */
1746int sde_crtc_secure_ctrl(struct drm_crtc *crtc, bool post_commit)
1747{
1748 struct sde_crtc *sde_crtc;
1749 struct sde_crtc_state *cstate;
1750 struct sde_kms *sde_kms;
1751 struct sde_crtc_smmu_state_data *smmu_state;
1752 int ret = 0;
1753 int old_smmu_state;
1754
1755 if (!crtc || !crtc->state) {
1756 SDE_ERROR("invalid crtc\n");
1757 return -EINVAL;
1758 }
1759
1760 sde_kms = _sde_crtc_get_kms(crtc);
1761 if (!sde_kms) {
1762 SDE_ERROR("invalid kms\n");
1763 return -EINVAL;
1764 }
1765
1766 sde_crtc = to_sde_crtc(crtc);
1767 cstate = to_sde_crtc_state(crtc->state);
1768 smmu_state = &sde_crtc->smmu_state;
1769 old_smmu_state = smmu_state->state;
1770
1771 if ((!smmu_state->transition_type) ||
1772 ((smmu_state->transition_type == POST_COMMIT) && !post_commit))
1773 /* Bail out */
1774 return 0;
1775
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001776 /* Secure UI use case enable */
1777 switch (smmu_state->state) {
1778 case DETACH_ALL_REQ:
1779 /* detach_all_contexts */
1780 ret = sde_kms_mmu_detach(sde_kms, false);
1781 if (ret) {
1782 SDE_ERROR("crtc: %d, failed to detach %d\n",
1783 crtc->base.id, ret);
1784 goto error;
1785 }
1786
1787 ret = _sde_crtc_scm_call(VMID_CP_SEC_DISPLAY);
1788 if (ret)
1789 goto error;
1790
1791 smmu_state->state = DETACHED;
1792 break;
1793 /* Secure UI use case disable */
1794 case ATTACH_ALL_REQ:
1795 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1796 if (ret)
1797 goto error;
1798
1799 /* attach_all_contexts */
1800 ret = sde_kms_mmu_attach(sde_kms, false);
1801 if (ret) {
1802 SDE_ERROR("crtc: %d, failed to attach %d\n",
1803 crtc->base.id,
1804 ret);
1805 goto error;
1806 }
1807
1808 smmu_state->state = ATTACHED;
1809
1810 break;
1811 /* Secure preview enable */
1812 case DETACH_SEC_REQ:
1813 /* detach secure_context */
1814 ret = sde_kms_mmu_detach(sde_kms, true);
1815 if (ret) {
1816 SDE_ERROR("crtc: %d, failed to detach %d\n",
1817 crtc->base.id,
1818 ret);
1819 goto error;
1820 }
1821
1822 smmu_state->state = DETACHED_SEC;
1823 ret = _sde_crtc_scm_call(VMID_CP_CAMERA_PREVIEW);
1824 if (ret)
1825 goto error;
1826
1827 break;
1828
1829 /* Secure preview disable */
1830 case ATTACH_SEC_REQ:
1831 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1832 if (ret)
1833 goto error;
1834
1835 ret = sde_kms_mmu_attach(sde_kms, true);
1836 if (ret) {
1837 SDE_ERROR("crtc: %d, failed to attach %d\n",
1838 crtc->base.id,
1839 ret);
1840 goto error;
1841 }
1842 smmu_state->state = ATTACHED;
1843 break;
1844 default:
1845 break;
1846 }
1847
1848 SDE_DEBUG("crtc: %d, old_state %d new_state %d\n", crtc->base.id,
1849 old_smmu_state,
1850 smmu_state->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001851 smmu_state->transition_type = NONE;
1852
1853error:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001854 smmu_state->transition_error = ret ? true : false;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001855 return ret;
1856}
1857
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001858void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1859 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001860{
1861 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001862 struct sde_crtc_state *cstate;
1863 struct drm_connector *conn;
Clarence Ip24f80662016-06-13 19:05:32 -04001864
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001865 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001866 SDE_ERROR("invalid crtc\n");
1867 return;
1868 }
1869
1870 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001871 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001872 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001873
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001874 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001875 cstate->num_connectors = 0;
1876
1877 drm_for_each_connector(conn, crtc->dev)
1878 if (conn->state && conn->state->crtc == crtc &&
1879 cstate->num_connectors < MAX_CONNECTORS) {
1880 cstate->connectors[cstate->num_connectors++] = conn;
1881 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001882 }
1883
1884 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001885 sde_fence_prepare(&sde_crtc->output_fence);
1886}
1887
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001888/**
1889 * _sde_crtc_complete_flip - signal pending page_flip events
1890 * Any pending vblank events are added to the vblank_event_list
1891 * so that the next vblank interrupt shall signal them.
1892 * However PAGE_FLIP events are not handled through the vblank_event_list.
1893 * This API signals any pending PAGE_FLIP events requested through
1894 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1895 * if file!=NULL, this is preclose potential cancel-flip path
1896 * @crtc: Pointer to drm crtc structure
1897 * @file: Pointer to drm file
1898 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001899static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1900 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001901{
1902 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1903 struct drm_device *dev = crtc->dev;
1904 struct drm_pending_vblank_event *event;
1905 unsigned long flags;
1906
1907 spin_lock_irqsave(&dev->event_lock, flags);
1908 event = sde_crtc->event;
1909 if (event) {
1910 /* if regular vblank case (!file) or if cancel-flip from
1911 * preclose on file that requested flip, then send the
1912 * event:
1913 */
1914 if (!file || (event->base.file_priv == file)) {
1915 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001916 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001917 sde_crtc->name, event);
Dhaval Patela5f75952017-07-25 11:17:41 -07001918 SDE_EVT32_VERBOSE(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001919 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001920 }
1921 }
1922 spin_unlock_irqrestore(&dev->event_lock, flags);
1923}
1924
Alan Kwong3e985f02017-02-12 15:08:44 -08001925enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1926{
1927 struct drm_encoder *encoder;
1928
1929 if (!crtc || !crtc->dev) {
1930 SDE_ERROR("invalid crtc\n");
1931 return INTF_MODE_NONE;
1932 }
1933
1934 drm_for_each_encoder(encoder, crtc->dev)
1935 if (encoder->crtc == crtc)
1936 return sde_encoder_get_intf_mode(encoder);
1937
1938 return INTF_MODE_NONE;
1939}
1940
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001941static void sde_crtc_vblank_cb(void *data)
1942{
1943 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001944 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1945
1946 /* keep statistics on vblank callback - with auto reset via debugfs */
1947 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1948 sde_crtc->vblank_cb_time = ktime_get();
1949 else
1950 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001951 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001952 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001953 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001954 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001955}
1956
Alan Kwong628d19e2016-10-31 13:50:13 -04001957static void sde_crtc_frame_event_work(struct kthread_work *work)
1958{
Alan Kwong67a3f792016-11-01 23:16:53 -04001959 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04001960 struct sde_crtc_frame_event *fevent;
1961 struct drm_crtc *crtc;
1962 struct sde_crtc *sde_crtc;
Alan Kwonga1939682017-05-05 11:30:08 -07001963 struct sde_crtc_state *cstate;
Alan Kwong628d19e2016-10-31 13:50:13 -04001964 struct sde_kms *sde_kms;
1965 unsigned long flags;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001966 bool frame_done = false;
1967 int i;
Alan Kwong628d19e2016-10-31 13:50:13 -04001968
1969 if (!work) {
1970 SDE_ERROR("invalid work handle\n");
1971 return;
1972 }
1973
1974 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07001975 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04001976 SDE_ERROR("invalid crtc\n");
1977 return;
1978 }
1979
1980 crtc = fevent->crtc;
1981 sde_crtc = to_sde_crtc(crtc);
Alan Kwonga1939682017-05-05 11:30:08 -07001982 cstate = to_sde_crtc_state(crtc->state);
Alan Kwong628d19e2016-10-31 13:50:13 -04001983
1984 sde_kms = _sde_crtc_get_kms(crtc);
1985 if (!sde_kms) {
1986 SDE_ERROR("invalid kms handle\n");
1987 return;
1988 }
Alan Kwong67a3f792016-11-01 23:16:53 -04001989 priv = sde_kms->dev->dev_private;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07001990 SDE_ATRACE_BEGIN("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04001991
1992 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
1993 ktime_to_ns(fevent->ts));
1994
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001995 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_ENTRY);
1996
1997 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
1998 | SDE_ENCODER_FRAME_EVENT_ERROR
1999 | SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002000
2001 if (atomic_read(&sde_crtc->frame_pending) < 1) {
2002 /* this should not happen */
2003 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
2004 crtc->base.id,
2005 ktime_to_ns(fevent->ts),
2006 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07002007 SDE_EVT32(DRMID(crtc), fevent->event,
2008 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002009 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
2010 /* release bandwidth and other resources */
2011 SDE_DEBUG("crtc%d ts:%lld last pending\n",
2012 crtc->base.id,
2013 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07002014 SDE_EVT32(DRMID(crtc), fevent->event,
2015 SDE_EVTLOG_FUNC_CASE2);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002016 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002017 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07002018 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
2019 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04002020 }
Alan Kwonga1939682017-05-05 11:30:08 -07002021
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002022 if (fevent->event & SDE_ENCODER_FRAME_EVENT_DONE)
Alan Kwonga1939682017-05-05 11:30:08 -07002023 sde_core_perf_crtc_update(crtc, 0, false);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002024
2025 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
2026 | SDE_ENCODER_FRAME_EVENT_ERROR))
2027 frame_done = true;
2028 }
2029
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002030 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE) {
2031 SDE_ATRACE_BEGIN("signal_release_fence");
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002032 sde_fence_signal(&sde_crtc->output_fence, fevent->ts, 0);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002033 SDE_ATRACE_END("signal_release_fence");
2034 }
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002035
2036 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE) {
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002037 SDE_ATRACE_BEGIN("signal_retire_fence");
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002038 for (i = 0; i < cstate->num_connectors; ++i)
2039 sde_connector_complete_commit(cstate->connectors[i],
2040 fevent->ts);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002041 SDE_ATRACE_END("signal_retire_fence");
Alan Kwong628d19e2016-10-31 13:50:13 -04002042 }
2043
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05002044 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
2045 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
2046 crtc->base.id, ktime_to_ns(fevent->ts));
2047
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002048 if (frame_done)
2049 complete_all(&sde_crtc->frame_done_comp);
2050
Alan Kwong628d19e2016-10-31 13:50:13 -04002051 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2052 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
2053 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002054 SDE_ATRACE_END("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04002055}
2056
2057static void sde_crtc_frame_event_cb(void *data, u32 event)
2058{
2059 struct drm_crtc *crtc = (struct drm_crtc *)data;
2060 struct sde_crtc *sde_crtc;
2061 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04002062 struct sde_crtc_frame_event *fevent;
2063 unsigned long flags;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002064 u32 crtc_id;
Alan Kwong628d19e2016-10-31 13:50:13 -04002065
2066 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2067 SDE_ERROR("invalid parameters\n");
2068 return;
2069 }
2070 sde_crtc = to_sde_crtc(crtc);
2071 priv = crtc->dev->dev_private;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002072 crtc_id = drm_crtc_index(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002073
2074 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07002075 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04002076
2077 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002078 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
2079 struct sde_crtc_frame_event, list);
2080 if (fevent)
2081 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04002082 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2083
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002084 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002085 SDE_ERROR("crtc%d event %d overflow\n",
2086 crtc->base.id, event);
2087 SDE_EVT32(DRMID(crtc), event);
2088 return;
2089 }
2090
Alan Kwong628d19e2016-10-31 13:50:13 -04002091 fevent->event = event;
2092 fevent->crtc = crtc;
2093 fevent->ts = ktime_get();
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002094 kthread_queue_work(&priv->event_thread[crtc_id].worker, &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04002095}
2096
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002097void sde_crtc_complete_commit(struct drm_crtc *crtc,
2098 struct drm_crtc_state *old_state)
2099{
2100 struct sde_crtc *sde_crtc;
2101 struct sde_crtc_smmu_state_data *smmu_state;
2102
2103 if (!crtc || !crtc->state) {
2104 SDE_ERROR("invalid crtc\n");
2105 return;
2106 }
2107
2108 sde_crtc = to_sde_crtc(crtc);
2109 SDE_EVT32_VERBOSE(DRMID(crtc));
2110 smmu_state = &sde_crtc->smmu_state;
2111
2112 /* complete secure transitions if any */
2113 if (smmu_state->transition_type == POST_COMMIT)
2114 sde_crtc_secure_ctrl(crtc, true);
2115}
2116
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002117/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002118 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
2119 * @cstate: Pointer to sde crtc state
2120 */
2121static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
2122{
2123 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002124 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002125 return;
2126 }
2127 cstate->input_fence_timeout_ns =
2128 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
2129 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
2130}
2131
2132/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002133 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
2134 * @cstate: Pointer to sde crtc state
2135 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
2136 */
2137static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
2138 void *usr_ptr)
2139{
2140 struct sde_drm_dim_layer_v1 dim_layer_v1;
2141 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002142 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002143 u32 count, i;
2144
2145 if (!cstate) {
2146 SDE_ERROR("invalid cstate\n");
2147 return;
2148 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002149 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002150
2151 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002152 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002153 return;
2154 }
2155
2156 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002157 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002158 return;
2159 }
2160
2161 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002162 if (count > SDE_MAX_DIM_LAYERS) {
2163 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002164 return;
2165 }
2166
2167 /* populate from user space */
2168 cstate->num_dim_layers = count;
2169 for (i = 0; i < count; i++) {
2170 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002171
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002172 dim_layer[i].flags = user_cfg->flags;
2173 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002174
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002175 dim_layer[i].rect.x = user_cfg->rect.x1;
2176 dim_layer[i].rect.y = user_cfg->rect.y1;
2177 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
2178 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
2179
2180 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002181 user_cfg->color_fill.color_0,
2182 user_cfg->color_fill.color_1,
2183 user_cfg->color_fill.color_2,
2184 user_cfg->color_fill.color_3,
2185 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002186
2187 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
2188 i, dim_layer[i].flags, dim_layer[i].stage);
2189 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
2190 dim_layer[i].rect.x, dim_layer[i].rect.y,
2191 dim_layer[i].rect.w, dim_layer[i].rect.h,
2192 dim_layer[i].color_fill.color_0,
2193 dim_layer[i].color_fill.color_1,
2194 dim_layer[i].color_fill.color_2,
2195 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002196 }
2197}
2198
2199/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002200 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
2201 * @crtc: Pointer to CRTC object
2202 */
2203static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
2204{
2205 struct drm_plane *plane = NULL;
2206 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04002207 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08002208 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002209
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002210 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002211
2212 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002213 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002214 return;
2215 }
2216
2217 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04002218 kt_end = ktime_add_ns(ktime_get(),
2219 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002220
2221 /*
2222 * Wait for fences sequentially, as all of them need to be signalled
2223 * before we can proceed.
2224 *
2225 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
2226 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
2227 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08002228 * if its fence has timed out. Call input fence wait multiple times if
2229 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04002230 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07002231 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002232 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08002233 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04002234 kt_wait = ktime_sub(kt_end, ktime_get());
2235 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
2236 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002237 else
2238 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08002239
2240 rc = sde_plane_wait_input_fence(plane, wait_ms);
2241 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002242 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07002243 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002244}
2245
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002246static void _sde_crtc_setup_mixer_for_encoder(
2247 struct drm_crtc *crtc,
2248 struct drm_encoder *enc)
2249{
2250 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002251 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002252 struct sde_rm *rm = &sde_kms->rm;
2253 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002254 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002255 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002256 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002257
2258 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
2259 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002260 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002261
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002262 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002263 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
2264 mixer = &sde_crtc->mixers[i];
2265
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002266 if (!sde_rm_get_hw(rm, &lm_iter))
2267 break;
2268 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
2269
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002270 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
2271 if (!sde_rm_get_hw(rm, &ctl_iter)) {
2272 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002273 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002274 mixer->hw_ctl = last_valid_ctl;
2275 } else {
2276 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
2277 last_valid_ctl = mixer->hw_ctl;
2278 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002279
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002280 /* Shouldn't happen, mixers are always >= ctls */
2281 if (!mixer->hw_ctl) {
2282 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002283 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002284 return;
2285 }
2286
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002287 /* Dspp may be null */
2288 (void) sde_rm_get_hw(rm, &dspp_iter);
2289 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
2290
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002291 mixer->encoder = enc;
2292
2293 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002294 SDE_DEBUG("setup mixer %d: lm %d\n",
2295 i, mixer->hw_lm->idx - LM_0);
2296 SDE_DEBUG("setup mixer %d: ctl %d\n",
2297 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002298 }
2299}
2300
2301static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
2302{
2303 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
2304 struct drm_encoder *enc;
2305
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002306 sde_crtc->num_mixers = 0;
2307 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2308
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002309 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002310 /* Check for mixers on all encoders attached to this crtc */
2311 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
2312 if (enc->crtc != crtc)
2313 continue;
2314
2315 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
2316 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002317
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002318 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002319}
2320
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002321static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
2322{
2323 int i;
2324 struct sde_crtc_state *cstate;
2325
2326 cstate = to_sde_crtc_state(state);
2327
2328 cstate->is_ppsplit = false;
2329 for (i = 0; i < cstate->num_connectors; i++) {
2330 struct drm_connector *conn = cstate->connectors[i];
2331
2332 if (sde_connector_get_topology_name(conn) ==
2333 SDE_RM_TOPOLOGY_PPSPLIT)
2334 cstate->is_ppsplit = true;
2335 }
2336}
2337
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002338static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
2339 struct drm_crtc_state *state)
2340{
2341 struct sde_crtc *sde_crtc;
2342 struct sde_crtc_state *cstate;
2343 struct drm_display_mode *adj_mode;
2344 u32 crtc_split_width;
2345 int i;
2346
2347 if (!crtc || !state) {
2348 SDE_ERROR("invalid args\n");
2349 return;
2350 }
2351
2352 sde_crtc = to_sde_crtc(crtc);
2353 cstate = to_sde_crtc_state(state);
2354
2355 adj_mode = &state->adjusted_mode;
2356 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
2357
2358 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002359 cstate->lm_bounds[i].x = crtc_split_width * i;
2360 cstate->lm_bounds[i].y = 0;
2361 cstate->lm_bounds[i].w = crtc_split_width;
2362 cstate->lm_bounds[i].h = adj_mode->vdisplay;
2363 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
2364 sizeof(cstate->lm_roi[i]));
Dhaval Patela5f75952017-07-25 11:17:41 -07002365 SDE_EVT32_VERBOSE(DRMID(crtc), i,
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002366 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
2367 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
2368 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
2369 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
2370 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002371 }
2372
2373 drm_mode_debug_printmodeline(adj_mode);
2374}
2375
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002376static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04002377 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002378{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002379 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07002380 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002381 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002382 unsigned long flags;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002383 struct sde_crtc_smmu_state_data *smmu_state;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002384
Clarence Ipcae1bb62016-07-07 12:07:13 -04002385 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002386 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002387 return;
2388 }
2389
Alan Kwong163d2612016-11-03 00:56:56 -04002390 if (!crtc->state->enable) {
2391 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
2392 crtc->base.id, crtc->state->enable);
2393 return;
2394 }
2395
2396 SDE_DEBUG("crtc%d\n", crtc->base.id);
2397
Clarence Ipcae1bb62016-07-07 12:07:13 -04002398 sde_crtc = to_sde_crtc(crtc);
2399 dev = crtc->dev;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002400 smmu_state = &sde_crtc->smmu_state;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002401
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002402 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002403 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002404 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002405 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
2406 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002407
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002408 if (sde_crtc->event) {
2409 WARN_ON(sde_crtc->event);
2410 } else {
2411 spin_lock_irqsave(&dev->event_lock, flags);
2412 sde_crtc->event = crtc->state->event;
2413 spin_unlock_irqrestore(&dev->event_lock, flags);
2414 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002415
Dhaval Patel0e558f42017-04-30 00:51:40 -07002416 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2417 if (encoder->crtc != crtc)
2418 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002419
Dhaval Patel0e558f42017-04-30 00:51:40 -07002420 /* encoder will trigger pending mask now */
2421 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002422 }
2423
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002424 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002425 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002426 * it means we are trying to flush a CRTC whose state is disabled:
2427 * nothing else needs to be done.
2428 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002429 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002430 return;
2431
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002432 _sde_crtc_blend_setup(crtc);
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002433
2434 /*
2435 * Since CP properties use AXI buffer to program the
2436 * HW, check if context bank is in attached
2437 * state,
2438 * apply color processing properties only if
2439 * smmu state is attached,
2440 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07002441 if ((smmu_state->state != DETACHED) &&
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002442 (smmu_state->state != DETACH_ALL_REQ))
2443 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002444
2445 /*
2446 * PP_DONE irq is only used by command mode for now.
2447 * It is better to request pending before FLUSH and START trigger
2448 * to make sure no pp_done irq missed.
2449 * This is safe because no pp_done will happen before SW trigger
2450 * in command mode.
2451 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002452}
2453
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002454static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
2455 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002456{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002457 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002458 struct sde_crtc *sde_crtc;
2459 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002460 struct drm_plane *plane;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002461 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002462 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002463
Clarence Ipcae1bb62016-07-07 12:07:13 -04002464 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002465 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002466 return;
2467 }
2468
Alan Kwong163d2612016-11-03 00:56:56 -04002469 if (!crtc->state->enable) {
2470 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
2471 crtc->base.id, crtc->state->enable);
2472 return;
2473 }
2474
2475 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002476
2477 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002478 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002479 dev = crtc->dev;
2480
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002481 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002482 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002483 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002484 spin_lock_irqsave(&dev->event_lock, flags);
2485 sde_crtc->event = crtc->state->event;
2486 spin_unlock_irqrestore(&dev->event_lock, flags);
2487 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002488
2489 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002490 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002491 * it means we are trying to flush a CRTC whose state is disabled:
2492 * nothing else needs to be done.
2493 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002494 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002495 return;
2496
Alan Kwong346223e2017-06-30 15:29:22 -04002497 /*
2498 * For planes without commit update, drm framework will not add
2499 * those planes to current state since hardware update is not
2500 * required. However, if those planes were power collapsed since
2501 * last commit cycle, driver has to restore the hardware state
2502 * of those planes explicitly here prior to plane flush.
2503 */
2504 drm_atomic_crtc_for_each_plane(plane, crtc)
2505 sde_plane_restore(plane);
2506
Clarence Ipcae1bb62016-07-07 12:07:13 -04002507 /* wait for acquire fences before anything else is done */
2508 _sde_crtc_wait_for_fences(crtc);
2509
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002510 if (!cstate->rsc_update) {
2511 drm_for_each_encoder(encoder, dev) {
2512 if (encoder->crtc != crtc)
2513 continue;
2514
2515 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002516 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002517 }
2518 cstate->rsc_update = true;
2519 }
2520
Alan Kwong9aa061c2016-11-06 21:17:12 -05002521 /* update performance setting before crtc kickoff */
2522 sde_core_perf_crtc_update(crtc, 1, false);
2523
Clarence Ipcae1bb62016-07-07 12:07:13 -04002524 /*
2525 * Final plane updates: Give each plane a chance to complete all
2526 * required writes/flushing before crtc's "flush
2527 * everything" call below.
2528 */
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002529 drm_atomic_crtc_for_each_plane(plane, crtc) {
2530 if (sde_crtc->smmu_state.transition_error)
2531 sde_plane_set_error(plane, true);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002532 sde_plane_flush(plane);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002533 }
Clarence Ipcae1bb62016-07-07 12:07:13 -04002534
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002535 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002536}
2537
Clarence Ip7a753bb2016-07-07 11:47:44 -04002538/**
2539 * sde_crtc_destroy_state - state destroy hook
2540 * @crtc: drm CRTC
2541 * @state: CRTC state object to release
2542 */
2543static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2544 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002545{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002546 struct sde_crtc *sde_crtc;
2547 struct sde_crtc_state *cstate;
2548
2549 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002550 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002551 return;
2552 }
2553
2554 sde_crtc = to_sde_crtc(crtc);
2555 cstate = to_sde_crtc_state(state);
2556
Alan Kwong163d2612016-11-03 00:56:56 -04002557 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002558
Alan Kwongcdb2f282017-03-18 13:42:06 -07002559 _sde_crtc_rp_destroy(&cstate->rp);
2560
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002561 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002562
2563 /* destroy value helper */
2564 msm_property_destroy_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002565 &cstate->property_state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002566}
2567
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002568static int _sde_crtc_wait_for_frame_done(struct drm_crtc *crtc)
2569{
2570 struct sde_crtc *sde_crtc;
2571 int ret, rc = 0;
2572
2573 if (!crtc) {
2574 SDE_ERROR("invalid argument\n");
2575 return -EINVAL;
2576 }
2577 sde_crtc = to_sde_crtc(crtc);
2578
2579 if (!atomic_read(&sde_crtc->frame_pending)) {
2580 SDE_DEBUG("no frames pending\n");
2581 return 0;
2582 }
2583
Dhaval Patela5f75952017-07-25 11:17:41 -07002584 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_ENTRY);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002585 ret = wait_for_completion_timeout(&sde_crtc->frame_done_comp,
2586 msecs_to_jiffies(SDE_FRAME_DONE_TIMEOUT));
2587 if (!ret) {
2588 SDE_ERROR("frame done completion wait timed out, ret:%d\n",
2589 ret);
2590 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FATAL);
2591 rc = -ETIMEDOUT;
2592 }
Dhaval Patela5f75952017-07-25 11:17:41 -07002593 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_EXIT);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002594
2595 return rc;
2596}
2597
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002598void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2599{
Clarence Ipeb39cce2017-07-19 14:12:43 -04002600 struct drm_plane *plane;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002601 struct drm_encoder *encoder;
2602 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002603 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002604 struct msm_drm_private *priv;
2605 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002606 struct sde_crtc_state *cstate;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002607 int ret;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002608
2609 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002610 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002611 return;
2612 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002613 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002614 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002615 sde_kms = _sde_crtc_get_kms(crtc);
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07002616
2617 if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
2618 SDE_ERROR("invalid argument\n");
2619 return;
2620 }
2621
Alan Kwong67a3f792016-11-01 23:16:53 -04002622 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002623 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002624
Clarence Ip90b282d2017-05-04 10:00:32 -07002625 /*
2626 * If no mixers has been allocated in sde_crtc_atomic_check(),
2627 * it means we are trying to start a CRTC whose state is disabled:
2628 * nothing else needs to be done.
2629 */
2630 if (unlikely(!sde_crtc->num_mixers))
2631 return;
2632
Narendra Muppalla77b32932017-05-10 13:53:11 -07002633 SDE_ATRACE_BEGIN("crtc_commit");
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002635 struct sde_encoder_kickoff_params params = { 0 };
2636
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002637 if (encoder->crtc != crtc)
2638 continue;
2639
2640 /*
2641 * Encoder will flush/start now, unless it has a tx pending.
2642 * If so, it may delay and flush at an irq event (e.g. ppdone)
2643 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002644 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002645 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2646 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002647 sde_encoder_prepare_for_kickoff(encoder, &params);
Alan Kwong628d19e2016-10-31 13:50:13 -04002648 }
2649
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002650 /* wait for frame_event_done completion */
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002651 SDE_ATRACE_BEGIN("wait_for_frame_done_event");
2652 ret = _sde_crtc_wait_for_frame_done(crtc);
2653 SDE_ATRACE_END("wait_for_frame_done_event");
2654 if (ret) {
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002655 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2656 crtc->base.id,
2657 atomic_read(&sde_crtc->frame_pending));
Narendra Muppalla77b32932017-05-10 13:53:11 -07002658 goto end;
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002659 }
2660
2661 if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002662 /* acquire bandwidth and other resources */
2663 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002664 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002665 } else {
2666 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002667 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE2);
Alan Kwong628d19e2016-10-31 13:50:13 -04002668 }
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07002669 sde_crtc->play_count++;
Alan Kwong628d19e2016-10-31 13:50:13 -04002670
Clarence Ipeb39cce2017-07-19 14:12:43 -04002671 if (cstate->sbuf_cfg.rot_op_mode != SDE_CTL_ROT_OP_MODE_OFFLINE)
2672 drm_atomic_crtc_for_each_plane(plane, crtc)
2673 sde_plane_kickoff(plane);
2674
Clarence Ip980405d2017-08-08 18:33:44 -04002675 sde_vbif_clear_errors(sde_kms);
2676
Alan Kwong628d19e2016-10-31 13:50:13 -04002677 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2678 if (encoder->crtc != crtc)
2679 continue;
2680
2681 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002682 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002683
Narendra Muppalla77b32932017-05-10 13:53:11 -07002684end:
Dhaval Patelb9850c02017-08-07 22:55:47 -07002685 reinit_completion(&sde_crtc->frame_done_comp);
Narendra Muppalla77b32932017-05-10 13:53:11 -07002686 SDE_ATRACE_END("crtc_commit");
2687 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002688}
2689
Clarence Ip7a753bb2016-07-07 11:47:44 -04002690/**
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002691 * _sde_crtc_vblank_enable_no_lock - update power resource and vblank request
Clarence Ip7f70ce42017-03-20 06:53:46 -07002692 * @sde_crtc: Pointer to sde crtc structure
2693 * @enable: Whether to enable/disable vblanks
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002694 *
2695 * @Return: error code
Clarence Ip7f70ce42017-03-20 06:53:46 -07002696 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002697static int _sde_crtc_vblank_enable_no_lock(
Clarence Ip7f70ce42017-03-20 06:53:46 -07002698 struct sde_crtc *sde_crtc, bool enable)
2699{
2700 struct drm_device *dev;
2701 struct drm_crtc *crtc;
2702 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002703
2704 if (!sde_crtc) {
2705 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002706 return -EINVAL;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002707 }
2708
2709 crtc = &sde_crtc->base;
2710 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002711
2712 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002713 int ret;
2714
2715 /* drop lock since power crtc cb may try to re-acquire lock */
2716 mutex_unlock(&sde_crtc->crtc_lock);
2717 ret = _sde_crtc_power_enable(sde_crtc, true);
2718 mutex_lock(&sde_crtc->crtc_lock);
2719 if (ret)
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002720 return ret;
Dhaval Patelf9245d62017-03-28 16:24:00 -07002721
Clarence Ip7f70ce42017-03-20 06:53:46 -07002722 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2723 if (enc->crtc != crtc)
2724 continue;
2725
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002726 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2727 sde_crtc->enabled,
2728 sde_crtc->suspend,
2729 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002730
2731 sde_encoder_register_vblank_callback(enc,
2732 sde_crtc_vblank_cb, (void *)crtc);
2733 }
2734 } else {
2735 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2736 if (enc->crtc != crtc)
2737 continue;
2738
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002739 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2740 sde_crtc->enabled,
2741 sde_crtc->suspend,
2742 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002743
2744 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2745 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002746
2747 /* drop lock since power crtc cb may try to re-acquire lock */
2748 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002749 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002750 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002751 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002752
2753 return 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002754}
2755
2756/**
2757 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2758 * @crtc: Pointer to drm crtc object
2759 * @enable: true to enable suspend, false to indicate resume
2760 */
2761static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2762{
2763 struct sde_crtc *sde_crtc;
2764 struct msm_drm_private *priv;
2765 struct sde_kms *sde_kms;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002766 int ret = 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002767
2768 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2769 SDE_ERROR("invalid crtc\n");
2770 return;
2771 }
2772 sde_crtc = to_sde_crtc(crtc);
2773 priv = crtc->dev->dev_private;
2774
2775 if (!priv->kms) {
2776 SDE_ERROR("invalid crtc kms\n");
2777 return;
2778 }
2779 sde_kms = to_sde_kms(priv->kms);
2780
2781 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002782 SDE_EVT32_VERBOSE(DRMID(crtc), enable);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002783
2784 mutex_lock(&sde_crtc->crtc_lock);
2785
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002786 /*
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002787 * If the vblank is enabled, release a power reference on suspend
2788 * and take it back during resume (if it is still enabled).
Clarence Ip7f70ce42017-03-20 06:53:46 -07002789 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002790 SDE_EVT32(DRMID(&sde_crtc->base), enable, sde_crtc->enabled,
2791 sde_crtc->suspend, sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002792 if (sde_crtc->suspend == enable)
2793 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2794 crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002795 else if (sde_crtc->enabled && sde_crtc->vblank_requested) {
2796 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, !enable);
2797 if (ret)
2798 SDE_ERROR("%s vblank enable failed: %d\n",
2799 sde_crtc->name, ret);
2800 }
Clarence Ip7f70ce42017-03-20 06:53:46 -07002801
2802 sde_crtc->suspend = enable;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002803 mutex_unlock(&sde_crtc->crtc_lock);
2804}
2805
2806/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002807 * sde_crtc_duplicate_state - state duplicate hook
2808 * @crtc: Pointer to drm crtc structure
2809 * @Returns: Pointer to new drm_crtc_state structure
2810 */
2811static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2812{
2813 struct sde_crtc *sde_crtc;
2814 struct sde_crtc_state *cstate, *old_cstate;
2815
2816 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002817 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002818 return NULL;
2819 }
2820
2821 sde_crtc = to_sde_crtc(crtc);
2822 old_cstate = to_sde_crtc_state(crtc->state);
2823 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2824 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002825 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002826 return NULL;
2827 }
2828
2829 /* duplicate value helper */
2830 msm_property_duplicate_state(&sde_crtc->property_info,
2831 old_cstate, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002832 &cstate->property_state, cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002833
2834 /* duplicate base helper */
2835 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2836
Alan Kwongcdb2f282017-03-18 13:42:06 -07002837 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2838
Clarence Ip7a753bb2016-07-07 11:47:44 -04002839 return &cstate->base;
2840}
2841
2842/**
2843 * sde_crtc_reset - reset hook for CRTCs
2844 * Resets the atomic state for @crtc by freeing the state pointer (which might
2845 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2846 * @crtc: Pointer to drm crtc structure
2847 */
2848static void sde_crtc_reset(struct drm_crtc *crtc)
2849{
2850 struct sde_crtc *sde_crtc;
2851 struct sde_crtc_state *cstate;
2852
2853 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002854 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002855 return;
2856 }
2857
Clarence Ip7f70ce42017-03-20 06:53:46 -07002858 /* revert suspend actions, if necessary */
Clarence Ipd86f6e42017-08-08 18:31:00 -04002859 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07002860 _sde_crtc_set_suspend(crtc, false);
2861
Clarence Ip7a753bb2016-07-07 11:47:44 -04002862 /* remove previous state, if present */
2863 if (crtc->state) {
2864 sde_crtc_destroy_state(crtc, crtc->state);
2865 crtc->state = 0;
2866 }
2867
2868 sde_crtc = to_sde_crtc(crtc);
2869 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2870 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002871 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002872 return;
2873 }
2874
2875 /* reset value helper */
2876 msm_property_reset_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002877 &cstate->property_state,
2878 cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002879
Clarence Ipcae1bb62016-07-07 12:07:13 -04002880 _sde_crtc_set_input_fence_timeout(cstate);
2881
Alan Kwong310e9b02017-08-03 02:04:07 -04002882 _sde_crtc_rp_reset(&cstate->rp, &sde_crtc->rp_lock,
2883 &sde_crtc->rp_head);
Alan Kwongcdb2f282017-03-18 13:42:06 -07002884
Clarence Ip7a753bb2016-07-07 11:47:44 -04002885 cstate->base.crtc = crtc;
2886 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002887}
2888
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002889static void sde_crtc_handle_power_event(u32 event_type, void *arg)
2890{
2891 struct drm_crtc *crtc = arg;
2892 struct sde_crtc *sde_crtc;
Dhaval Patel010f5172017-08-01 22:40:09 -07002893 struct drm_plane *plane;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002894 struct drm_encoder *encoder;
Dhaval Patel010f5172017-08-01 22:40:09 -07002895 struct sde_crtc_mixer *m;
Benjamin Chan90139102017-06-21 16:00:39 -04002896 struct drm_event event;
Dhaval Patel010f5172017-08-01 22:40:09 -07002897 u32 power_on = 0, i, misr_status;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002898
2899 if (!crtc) {
2900 SDE_ERROR("invalid crtc\n");
2901 return;
2902 }
2903 sde_crtc = to_sde_crtc(crtc);
2904
2905 mutex_lock(&sde_crtc->crtc_lock);
2906
2907 SDE_EVT32(DRMID(crtc), event_type);
2908
Dhaval Patel010f5172017-08-01 22:40:09 -07002909 switch (event_type) {
2910 case SDE_POWER_EVENT_POST_ENABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002911 /* restore encoder; crtc will be programmed during commit */
2912 drm_for_each_encoder(encoder, crtc->dev) {
2913 if (encoder->crtc != crtc)
2914 continue;
2915
2916 sde_encoder_virt_restore(encoder);
2917 }
Ping Lie505f3b2017-06-19 14:19:08 -07002918 sde_cp_crtc_post_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002919
Benjamin Chan90139102017-06-21 16:00:39 -04002920 event.type = DRM_EVENT_SDE_POWER;
2921 event.length = sizeof(power_on);
2922 power_on = 1;
2923 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2924 (u8 *)&power_on);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002925
Dhaval Patel010f5172017-08-01 22:40:09 -07002926 for (i = 0; i < sde_crtc->num_mixers; ++i) {
2927 m = &sde_crtc->mixers[i];
2928 if (!m->hw_lm || !m->hw_lm->ops.setup_misr ||
2929 !sde_crtc->misr_enable)
2930 continue;
2931
2932 m->hw_lm->ops.setup_misr(m->hw_lm, true,
2933 sde_crtc->misr_frame_count);
2934 }
2935 break;
2936 case SDE_POWER_EVENT_PRE_DISABLE:
2937 for (i = 0; i < sde_crtc->num_mixers; ++i) {
2938 m = &sde_crtc->mixers[i];
2939 if (!m->hw_lm || !m->hw_lm->ops.collect_misr ||
2940 !sde_crtc->misr_enable)
2941 continue;
2942
2943 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
2944 sde_crtc->misr_data[i] = misr_status ? misr_status :
2945 sde_crtc->misr_data[i];
2946 }
2947 sde_cp_crtc_pre_ipc(crtc);
2948 break;
2949 case SDE_POWER_EVENT_POST_DISABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002950 /*
2951 * set revalidate flag in planes, so it will be re-programmed
2952 * in the next frame update
2953 */
2954 drm_atomic_crtc_for_each_plane(plane, crtc)
2955 sde_plane_set_revalidate(plane, true);
Alan Kwong8a9b38a2017-06-22 11:30:52 -04002956
Gopikrishnaiah Anandandb90fa12017-05-09 17:56:08 -07002957 sde_cp_crtc_suspend(crtc);
Benjamin Chan90139102017-06-21 16:00:39 -04002958
2959 event.type = DRM_EVENT_SDE_POWER;
2960 event.length = sizeof(power_on);
2961 power_on = 0;
2962 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2963 (u8 *)&power_on);
Dhaval Patel010f5172017-08-01 22:40:09 -07002964 break;
2965 default:
2966 SDE_DEBUG("event:%d not handled\n", event_type);
2967 break;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002968 }
2969
2970 mutex_unlock(&sde_crtc->crtc_lock);
2971}
2972
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002973static void sde_crtc_disable(struct drm_crtc *crtc)
2974{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002975 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002976 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04002977 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002978 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002979 unsigned long flags;
2980 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07002981 struct drm_event event;
2982 u32 power_on;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07002983 int ret;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002984
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002985 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002986 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002987 return;
2988 }
2989 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002990 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07002991 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002992
Alan Kwong163d2612016-11-03 00:56:56 -04002993 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002994
Clarence Ipd86f6e42017-08-08 18:31:00 -04002995 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07002996 _sde_crtc_set_suspend(crtc, true);
2997
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002998 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002999 SDE_EVT32_VERBOSE(DRMID(crtc));
Alan Kwong628d19e2016-10-31 13:50:13 -04003000
Ping Lic5c2e0b2017-08-02 15:17:59 -07003001 /* update color processing on suspend */
3002 event.type = DRM_EVENT_CRTC_POWER;
3003 event.length = sizeof(u32);
3004 sde_cp_crtc_suspend(crtc);
3005 power_on = 0;
3006 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3007 (u8 *)&power_on);
3008
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003009 /* wait for frame_event_done completion */
3010 if (_sde_crtc_wait_for_frame_done(crtc))
3011 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
3012 crtc->base.id,
3013 atomic_read(&sde_crtc->frame_pending));
3014
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003015 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3016 sde_crtc->vblank_requested);
3017 if (sde_crtc->enabled && !sde_crtc->suspend &&
3018 sde_crtc->vblank_requested) {
3019 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, false);
3020 if (ret)
3021 SDE_ERROR("%s vblank enable failed: %d\n",
3022 sde_crtc->name, ret);
Alan Kwong07da0982016-11-04 12:57:45 -04003023 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003024 sde_crtc->enabled = false;
Alan Kwong07da0982016-11-04 12:57:45 -04003025
Alan Kwong628d19e2016-10-31 13:50:13 -04003026 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07003027 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
3028 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003029 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04003030 atomic_set(&sde_crtc->frame_pending, 0);
3031 }
3032
Ping Li6d5bf542017-06-27 11:40:28 -07003033 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3034 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3035 ret = 0;
3036 if (node->func)
3037 ret = node->func(crtc, false, &node->irq);
3038 if (ret)
3039 SDE_ERROR("%s failed to disable event %x\n",
3040 sde_crtc->name, node->event);
3041 }
3042 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3043
Alan Kwong9aa061c2016-11-06 21:17:12 -05003044 sde_core_perf_crtc_update(crtc, 0, true);
3045
Alan Kwong628d19e2016-10-31 13:50:13 -04003046 drm_for_each_encoder(encoder, crtc->dev) {
3047 if (encoder->crtc != crtc)
3048 continue;
3049 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003050 cstate->rsc_client = NULL;
3051 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04003052 }
3053
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003054 if (sde_crtc->power_event)
3055 sde_power_handle_unregister_event(&priv->phandle,
3056 sde_crtc->power_event);
3057
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003058 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
3059 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003060
Alan Kwong8411a9112017-06-06 19:29:01 -04003061 /* disable clk & bw control until clk & bw properties are set */
3062 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07003063 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04003064
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003065 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003066}
3067
3068static void sde_crtc_enable(struct drm_crtc *crtc)
3069{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003070 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04003071 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003072 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003073 unsigned long flags;
3074 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003075 struct drm_event event;
3076 u32 power_on;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003077 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003078
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003079 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003080 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003081 return;
3082 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003083 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003084
Alan Kwong163d2612016-11-03 00:56:56 -04003085 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003086 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04003087 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003088
Alan Kwong628d19e2016-10-31 13:50:13 -04003089 drm_for_each_encoder(encoder, crtc->dev) {
3090 if (encoder->crtc != crtc)
3091 continue;
3092 sde_encoder_register_frame_event_callback(encoder,
3093 sde_crtc_frame_event_cb, (void *)crtc);
3094 }
3095
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003096 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003097 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3098 sde_crtc->vblank_requested);
3099 if (!sde_crtc->enabled && !sde_crtc->suspend &&
3100 sde_crtc->vblank_requested) {
3101 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, true);
3102 if (ret)
3103 SDE_ERROR("%s vblank enable failed: %d\n",
3104 sde_crtc->name, ret);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003105 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003106 sde_crtc->enabled = true;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003107
3108 /* update color processing on resume */
3109 event.type = DRM_EVENT_CRTC_POWER;
3110 event.length = sizeof(u32);
3111 sde_cp_crtc_resume(crtc);
3112 power_on = 1;
3113 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3114 (u8 *)&power_on);
3115
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003116 mutex_unlock(&sde_crtc->crtc_lock);
3117
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003118 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3119 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3120 ret = 0;
3121 if (node->func)
3122 ret = node->func(crtc, true, &node->irq);
3123 if (ret)
3124 SDE_ERROR("%s failed to enable event %x\n",
3125 sde_crtc->name, node->event);
3126 }
3127 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003128
3129 sde_crtc->power_event = sde_power_handle_register_event(
3130 &priv->phandle,
Ping Lie505f3b2017-06-19 14:19:08 -07003131 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE |
3132 SDE_POWER_EVENT_PRE_DISABLE,
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003133 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003134}
3135
3136struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003137 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003138 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003139 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003140 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003141};
3142
Clarence Ipc47a0692016-10-11 10:54:17 -04003143static int pstate_cmp(const void *a, const void *b)
3144{
3145 struct plane_state *pa = (struct plane_state *)a;
3146 struct plane_state *pb = (struct plane_state *)b;
3147 int rc = 0;
3148 int pa_zpos, pb_zpos;
3149
3150 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
3151 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
3152
3153 if (pa_zpos != pb_zpos)
3154 rc = pa_zpos - pb_zpos;
3155 else
3156 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
3157
3158 return rc;
3159}
3160
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003161static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
3162 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
3163{
3164 struct sde_rect dst_rect, intersect;
3165 int i, rc = -EINVAL;
3166 const struct drm_plane_state *pstate;
3167
3168 /* start checking from next plane */
3169 for (i = curr_cnt; i < cnt; i++) {
3170 pstate = pstates[i].drm_pstate;
3171 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
Veera Sundaram Sankaran9d9ff912017-06-20 10:41:21 -07003172 pstate->crtc_w, pstate->crtc_h, false);
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003173 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
3174
3175 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
3176 /* next plane may be on same z-order */
3177 && z_pos != pstates[i].stage) {
3178 rc = 0;
3179 goto end;
3180 }
3181 }
3182
3183 SDE_ERROR("excl rect does not find top overlapping rect\n");
3184end:
3185 return rc;
3186}
3187
3188/* no input validation - caller API has all the checks */
3189static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
3190 struct plane_state pstates[], int cnt)
3191{
3192 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
3193 struct drm_display_mode *mode = &state->adjusted_mode;
3194 const struct drm_plane_state *pstate;
3195 struct sde_plane_state *sde_pstate;
3196 int rc = 0, i;
3197
3198 /* Check dim layer rect bounds and stage */
3199 for (i = 0; i < cstate->num_dim_layers; i++) {
3200 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
3201 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
3202 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
3203 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
3204 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
3205 (!cstate->dim_layer[i].rect.w) ||
3206 (!cstate->dim_layer[i].rect.h)) {
3207 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
3208 cstate->dim_layer[i].rect.x,
3209 cstate->dim_layer[i].rect.y,
3210 cstate->dim_layer[i].rect.w,
3211 cstate->dim_layer[i].rect.h,
3212 cstate->dim_layer[i].stage);
3213 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
3214 mode->vdisplay);
3215 rc = -E2BIG;
3216 goto end;
3217 }
3218 }
3219
3220 /* this is traversing on sorted z-order pstates */
3221 for (i = 0; i < cnt; i++) {
3222 pstate = pstates[i].drm_pstate;
3223 sde_pstate = to_sde_plane_state(pstate);
3224 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
3225 /* check overlap on all top z-order */
3226 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
3227 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
3228 if (rc)
3229 goto end;
3230 }
3231 }
3232
3233end:
3234 return rc;
3235}
3236
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003237static int _sde_crtc_check_secure_state(struct drm_crtc *crtc,
3238 struct drm_crtc_state *state)
3239{
3240 struct drm_encoder *encoder;
3241 struct sde_crtc_state *cstate;
3242 uint32_t secure;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003243 uint32_t fb_ns = 0, fb_sec = 0, fb_sec_dir = 0;
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003244 int encoder_cnt = 0;
3245 int rc;
3246
3247 if (!crtc || !state) {
3248 SDE_ERROR("invalid arguments\n");
3249 return -EINVAL;
3250 }
3251
3252 cstate = to_sde_crtc_state(state);
3253
3254 secure = sde_crtc_get_property(cstate,
3255 CRTC_PROP_SECURITY_LEVEL);
3256
3257 rc = _sde_crtc_find_plane_fb_modes(state,
3258 &fb_ns,
3259 &fb_sec,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003260 &fb_sec_dir);
3261 if (rc)
3262 return rc;
3263
3264 /**
3265 * validate planes
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003266 * fb_sec_dir is for secure camera preview and secure display use case,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003267 * fb_sec is for secure video playback,
3268 * fb_ns is for normal non secure use cases.
3269 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003270 if ((secure == SDE_DRM_SEC_ONLY) &&
3271 (fb_ns || fb_sec || (fb_sec && fb_sec_dir))) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003272 SDE_ERROR(
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003273 "crtc%d: invalid planes fb_modes Sec:%d, NS:%d, Sec_Dir:%d\n",
3274 crtc->base.id, fb_sec, fb_ns, fb_sec_dir);
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003275 return -EINVAL;
3276 }
3277
3278 /**
3279 * secure_crtc is not allowed in a shared toppolgy
3280 * across different encoders.
3281 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003282 if (fb_sec_dir) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003283 drm_for_each_encoder(encoder, crtc->dev)
3284 if (encoder->crtc == crtc)
3285 encoder_cnt++;
3286
3287 if (encoder_cnt >
3288 MAX_ALLOWED_ENCODER_CNT_PER_SECURE_CRTC) {
3289 SDE_ERROR(
3290 "crtc%d, invalid virtual encoder crtc%d\n",
3291 crtc->base.id,
3292 encoder_cnt);
3293 return -EINVAL;
3294
3295 }
3296 }
3297 SDE_DEBUG("crtc:%d Secure validation successful\n", crtc->base.id);
3298 return 0;
3299}
3300
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003301static int sde_crtc_atomic_check(struct drm_crtc *crtc,
3302 struct drm_crtc_state *state)
3303{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003304 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003305 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003306 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003307
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003308 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003309 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003310 struct drm_display_mode *mode;
3311
3312 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003313
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003314 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
3315 int multirect_count = 0;
3316 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003317 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003318
Clarence Ipcae1bb62016-07-07 12:07:13 -04003319 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003320 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003321 return -EINVAL;
3322 }
3323
Alan Kwongcdb2f282017-03-18 13:42:06 -07003324 sde_crtc = to_sde_crtc(crtc);
3325 cstate = to_sde_crtc_state(state);
3326
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003327 if (!state->enable || !state->active) {
3328 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
3329 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003330 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003331 }
3332
Dhaval Patelec10fad2016-08-22 14:40:48 -07003333 mode = &state->adjusted_mode;
3334 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003335
Clarence Ip90b282d2017-05-04 10:00:32 -07003336 /* force a full mode set if active state changed */
3337 if (state->active_changed)
3338 state->mode_changed = true;
3339
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003340 memset(pipe_staged, 0, sizeof(pipe_staged));
3341
Dhaval Patelec10fad2016-08-22 14:40:48 -07003342 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003343
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05003344 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003345 _sde_crtc_setup_lm_bounds(crtc, state);
3346
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003347 rc = _sde_crtc_check_secure_state(crtc, state);
3348 if (rc)
3349 return rc;
3350
Dhaval Patelec10fad2016-08-22 14:40:48 -07003351 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003352 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003353 if (IS_ERR_OR_NULL(pstate)) {
3354 rc = PTR_ERR(pstate);
3355 SDE_ERROR("%s: failed to get plane%d state, %d\n",
3356 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04003357 goto end;
3358 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003359 if (cnt >= ARRAY_SIZE(pstates))
3360 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003361
Dhaval Patelec10fad2016-08-22 14:40:48 -07003362 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
3363 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003364 pstates[cnt].stage = sde_plane_get_property(
3365 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003366 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003367
3368 /* check dim layer stage with every plane */
3369 for (i = 0; i < cstate->num_dim_layers; i++) {
Veera Sundaram Sankaranb9ed6bd2017-07-11 19:18:03 -07003370 if (cstate->dim_layer[i].stage
3371 == (pstates[cnt].stage + SDE_STAGE_0)) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003372 SDE_ERROR(
3373 "plane:%d/dim_layer:%i-same stage:%d\n",
3374 plane->base.id, i,
3375 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003376 rc = -EINVAL;
3377 goto end;
3378 }
3379 }
3380
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003381 if (pipe_staged[pstates[cnt].pipe_id]) {
3382 multirect_plane[multirect_count].r0 =
3383 pipe_staged[pstates[cnt].pipe_id];
3384 multirect_plane[multirect_count].r1 = pstate;
3385 multirect_count++;
3386
3387 pipe_staged[pstates[cnt].pipe_id] = NULL;
3388 } else {
3389 pipe_staged[pstates[cnt].pipe_id] = pstate;
3390 }
3391
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003392 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003393
3394 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
3395 mode->vdisplay) ||
3396 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
3397 mode->hdisplay)) {
3398 SDE_ERROR("invalid vertical/horizontal destination\n");
3399 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
3400 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
3401 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
3402 rc = -E2BIG;
3403 goto end;
3404 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003405 }
3406
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003407 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003408 if (pipe_staged[i]) {
3409 sde_plane_clear_multirect(pipe_staged[i]);
3410
3411 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003412 SDE_ERROR(
3413 "r1 only virt plane:%d not supported\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003414 pipe_staged[i]->plane->base.id);
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003415 rc = -EINVAL;
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003416 goto end;
3417 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003418 }
3419 }
3420
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003421 /* assign mixer stages based on sorted zpos property */
3422 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
3423
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003424 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
3425 if (rc)
3426 goto end;
3427
Clarence Ipc47a0692016-10-11 10:54:17 -04003428 if (!sde_is_custom_client()) {
3429 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003430
Clarence Ipc47a0692016-10-11 10:54:17 -04003431 z_pos = 0;
3432 for (i = 0; i < cnt; i++) {
3433 if (stage_old != pstates[i].stage)
3434 ++z_pos;
3435 stage_old = pstates[i].stage;
3436 pstates[i].stage = z_pos;
3437 }
3438 }
3439
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003440 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04003441 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003442 /* reset counts at every new blend stage */
3443 if (pstates[i].stage != z_pos) {
3444 left_zpos_cnt = 0;
3445 right_zpos_cnt = 0;
3446 z_pos = pstates[i].stage;
3447 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003448
3449 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04003450 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003451 SDE_ERROR("> %d plane stages assigned\n",
3452 SDE_STAGE_MAX - SDE_STAGE_0);
3453 rc = -EINVAL;
3454 goto end;
3455 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003456 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003457 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003458 z_pos);
3459 rc = -EINVAL;
3460 goto end;
3461 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003462 left_zpos_cnt++;
3463
Dhaval Patelec10fad2016-08-22 14:40:48 -07003464 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003465 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003466 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003467 z_pos);
3468 rc = -EINVAL;
3469 goto end;
3470 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003471 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003472 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003473
Clarence Ipc47a0692016-10-11 10:54:17 -04003474 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003475 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003476 }
3477
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003478 for (i = 0; i < multirect_count; i++) {
3479 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
3480 SDE_ERROR(
3481 "multirect validation failed for planes (%d - %d)\n",
3482 multirect_plane[i].r0->plane->base.id,
3483 multirect_plane[i].r1->plane->base.id);
3484 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003485 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003486 }
3487 }
3488
Alan Kwong9aa061c2016-11-06 21:17:12 -05003489 rc = sde_core_perf_crtc_check(crtc, state);
3490 if (rc) {
3491 SDE_ERROR("crtc%d failed performance check %d\n",
3492 crtc->base.id, rc);
3493 goto end;
3494 }
3495
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003496 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003497 * use pstates sorted by stage to check planes on same stage
3498 * we assume that all pipes are in source split so its valid to compare
3499 * without taking into account left/right mixer placement
3500 */
3501 for (i = 1; i < cnt; i++) {
3502 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003503 struct sde_rect left_rect, right_rect;
3504 int32_t left_pid, right_pid;
3505 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003506
3507 prv_pstate = &pstates[i - 1];
3508 cur_pstate = &pstates[i];
3509 if (prv_pstate->stage != cur_pstate->stage)
3510 continue;
3511
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003512 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003513
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003514 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
3515 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
3516 prv_pstate->drm_pstate->crtc_y,
3517 prv_pstate->drm_pstate->crtc_w,
3518 prv_pstate->drm_pstate->crtc_h, false);
3519
3520 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
3521 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
3522 cur_pstate->drm_pstate->crtc_y,
3523 cur_pstate->drm_pstate->crtc_w,
3524 cur_pstate->drm_pstate->crtc_h, false);
3525
3526 if (right_rect.x < left_rect.x) {
3527 swap(left_pid, right_pid);
3528 swap(left_rect, right_rect);
3529 }
3530
3531 /**
3532 * - planes are enumerated in pipe-priority order such that
3533 * planes with lower drm_id must be left-most in a shared
3534 * blend-stage when using source split.
3535 * - planes in source split must be contiguous in width
3536 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003537 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003538 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003539 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003540 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
3541 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003542 rc = -EINVAL;
3543 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003544 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003545 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003546 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
3547 stage, left_rect.x, left_rect.w,
3548 right_rect.x, right_rect.w);
3549 rc = -EINVAL;
3550 goto end;
3551 } else if ((left_rect.y != right_rect.y) ||
3552 (left_rect.h != right_rect.h)) {
3553 SDE_ERROR(
3554 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
3555 stage, left_rect.y, right_rect.y,
3556 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003557 rc = -EINVAL;
3558 goto end;
3559 }
3560 }
3561
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003562 rc = _sde_crtc_check_rois(crtc, state);
3563 if (rc) {
3564 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
3565 goto end;
3566 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003567
Dhaval Patelec10fad2016-08-22 14:40:48 -07003568end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07003569 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07003570 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003571}
3572
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003573int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003574{
Clarence Ip7f70ce42017-03-20 06:53:46 -07003575 struct sde_crtc *sde_crtc;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003576 int ret;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003577
Clarence Ip7f70ce42017-03-20 06:53:46 -07003578 if (!crtc) {
3579 SDE_ERROR("invalid crtc\n");
3580 return -EINVAL;
3581 }
3582 sde_crtc = to_sde_crtc(crtc);
3583
3584 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003585 SDE_EVT32(DRMID(&sde_crtc->base), en, sde_crtc->enabled,
3586 sde_crtc->suspend, sde_crtc->vblank_requested);
3587 if (sde_crtc->enabled && !sde_crtc->suspend) {
3588 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, en);
3589 if (ret)
3590 SDE_ERROR("%s vblank enable failed: %d\n",
3591 sde_crtc->name, ret);
3592 }
3593 sde_crtc->vblank_requested = en;
Clarence Ip7f70ce42017-03-20 06:53:46 -07003594 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003595
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003596 return 0;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003597}
3598
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003599void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
3600{
3601 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
3602
Alan Kwong163d2612016-11-03 00:56:56 -04003603 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003604 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003605}
3606
Clarence Ip7a753bb2016-07-07 11:47:44 -04003607/**
3608 * sde_crtc_install_properties - install all drm properties for crtc
3609 * @crtc: Pointer to drm crtc structure
3610 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003611static void sde_crtc_install_properties(struct drm_crtc *crtc,
3612 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003613{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003614 struct sde_crtc *sde_crtc;
3615 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003616 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003617 struct sde_kms *sde_kms;
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003618 static const struct drm_prop_enum_list e_secure_level[] = {
3619 {SDE_DRM_SEC_NON_SEC, "sec_and_non_sec"},
3620 {SDE_DRM_SEC_ONLY, "sec_only"},
3621 };
Clarence Ip7a753bb2016-07-07 11:47:44 -04003622
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003623 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003624
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003625 if (!crtc || !catalog) {
3626 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003627 return;
3628 }
3629
3630 sde_crtc = to_sde_crtc(crtc);
3631 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003632 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003633
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07003634 if (!sde_kms) {
3635 SDE_ERROR("invalid argument\n");
3636 return;
3637 }
3638
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003639 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3640 if (!info) {
3641 SDE_ERROR("failed to allocate info memory\n");
3642 return;
3643 }
3644
Clarence Ip7a753bb2016-07-07 11:47:44 -04003645 /* range properties */
3646 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003647 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3648 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3649
3650 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3651 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003652
3653 msm_property_install_range(&sde_crtc->property_info,
3654 "output_fence_offset", 0x0, 0, 1, 0,
3655 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003656
Alan Kwong9aa061c2016-11-06 21:17:12 -05003657 msm_property_install_range(&sde_crtc->property_info,
3658 "core_clk", 0x0, 0, U64_MAX,
3659 sde_kms->perf.max_core_clk_rate,
3660 CRTC_PROP_CORE_CLK);
3661 msm_property_install_range(&sde_crtc->property_info,
3662 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003663 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003664 CRTC_PROP_CORE_AB);
3665 msm_property_install_range(&sde_crtc->property_info,
3666 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003667 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003668 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003669 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003670 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003671 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003672 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003673 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003674 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003675 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003676 CRTC_PROP_LLCC_IB);
3677 msm_property_install_range(&sde_crtc->property_info,
3678 "dram_ab", 0x0, 0, U64_MAX,
3679 catalog->perf.max_bw_high * 1000ULL,
3680 CRTC_PROP_DRAM_AB);
3681 msm_property_install_range(&sde_crtc->property_info,
3682 "dram_ib", 0x0, 0, U64_MAX,
3683 catalog->perf.max_bw_high * 1000ULL,
3684 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003685 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003686 "rot_prefill_bw", 0, 0, U64_MAX,
3687 catalog->perf.max_bw_high * 1000ULL,
3688 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003689 msm_property_install_range(&sde_crtc->property_info,
3690 "rot_clk", 0, 0, U64_MAX,
3691 sde_kms->perf.max_core_clk_rate,
3692 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003693
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003694 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3695 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003696
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003697 msm_property_install_volatile_range(&sde_crtc->property_info,
3698 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3699
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003700 msm_property_install_enum(&sde_crtc->property_info, "security_level",
3701 0x0, 0, e_secure_level,
3702 ARRAY_SIZE(e_secure_level),
3703 CRTC_PROP_SECURITY_LEVEL);
3704
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003705 sde_kms_info_reset(info);
3706
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003707 if (catalog->has_dim_layer) {
3708 msm_property_install_volatile_range(&sde_crtc->property_info,
3709 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3710 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3711 SDE_MAX_DIM_LAYERS);
3712 }
3713
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003714 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3715 sde_kms_info_add_keyint(info, "max_linewidth",
3716 catalog->max_mixer_width);
3717 sde_kms_info_add_keyint(info, "max_blendstages",
3718 catalog->max_mixer_blendstages);
3719 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3720 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3721 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3722 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003723
3724 if (sde_is_custom_client()) {
3725 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3726 sde_kms_info_add_keystr(info,
3727 "smart_dma_rev", "smart_dma_v1");
3728 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3729 sde_kms_info_add_keystr(info,
3730 "smart_dma_rev", "smart_dma_v2");
3731 }
3732
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003733 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003734 if (catalog->perf.max_bw_low)
3735 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003736 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003737 if (catalog->perf.max_bw_high)
3738 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003739 catalog->perf.max_bw_high * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003740 if (sde_kms->perf.max_core_clk_rate)
3741 sde_kms_info_add_keyint(info, "max_mdp_clk",
3742 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003743 sde_kms_info_add_keystr(info, "core_ib_ff",
3744 catalog->perf.core_ib_ff);
3745 sde_kms_info_add_keystr(info, "core_clk_ff",
3746 catalog->perf.core_clk_ff);
3747 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3748 catalog->perf.comp_ratio_rt);
3749 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3750 catalog->perf.comp_ratio_nrt);
3751 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3752 catalog->perf.dest_scale_prefill_lines);
3753 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3754 catalog->perf.undersized_prefill_lines);
3755 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3756 catalog->perf.macrotile_prefill_lines);
3757 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3758 catalog->perf.yuv_nv12_prefill_lines);
3759 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3760 catalog->perf.linear_prefill_lines);
3761 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3762 catalog->perf.downscaling_prefill_lines);
3763 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3764 catalog->perf.xtra_prefill_lines);
3765 sde_kms_info_add_keyint(info, "amortizable_threshold",
3766 catalog->perf.amortizable_threshold);
3767 sde_kms_info_add_keyint(info, "min_prefill_lines",
3768 catalog->perf.min_prefill_lines);
3769
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003770 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07003771 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003772
3773 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003774}
3775
3776/**
3777 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3778 * @crtc: Pointer to drm crtc structure
3779 * @state: Pointer to drm crtc state structure
3780 * @property: Pointer to targeted drm property
3781 * @val: Updated property value
3782 * @Returns: Zero on success
3783 */
3784static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3785 struct drm_crtc_state *state,
3786 struct drm_property *property,
3787 uint64_t val)
3788{
3789 struct sde_crtc *sde_crtc;
3790 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003791 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003792
3793 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003794 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003795 } else {
3796 sde_crtc = to_sde_crtc(crtc);
3797 cstate = to_sde_crtc_state(state);
3798 ret = msm_property_atomic_set(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003799 &cstate->property_state, property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003800 if (!ret) {
3801 idx = msm_property_index(&sde_crtc->property_info,
3802 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003803 switch (idx) {
3804 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003805 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003806 break;
3807 case CRTC_PROP_DIM_LAYER_V1:
3808 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3809 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003810 case CRTC_PROP_ROI_V1:
3811 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3812 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04003813 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003814 case CRTC_PROP_CORE_AB:
3815 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003816 cstate->bw_control = true;
3817 break;
Alan Kwong0230a102017-05-16 11:36:44 -07003818 case CRTC_PROP_LLCC_AB:
3819 case CRTC_PROP_LLCC_IB:
3820 case CRTC_PROP_DRAM_AB:
3821 case CRTC_PROP_DRAM_IB:
3822 cstate->bw_control = true;
3823 cstate->bw_split_vote = true;
3824 break;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003825 default:
3826 /* nothing to do */
3827 break;
3828 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003829 } else {
3830 ret = sde_cp_crtc_set_property(crtc,
3831 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003832 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003833 if (ret)
3834 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07003835
3836 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
3837 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003838 }
3839
3840 return ret;
3841}
3842
3843/**
3844 * sde_crtc_set_property - set a crtc drm property
3845 * @crtc: Pointer to drm crtc structure
3846 * @property: Pointer to targeted drm property
3847 * @val: Updated property value
3848 * @Returns: Zero on success
3849 */
3850static int sde_crtc_set_property(struct drm_crtc *crtc,
3851 struct drm_property *property, uint64_t val)
3852{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003853 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003854
3855 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
3856}
3857
3858/**
3859 * sde_crtc_atomic_get_property - retrieve a crtc drm property
3860 * @crtc: Pointer to drm crtc structure
3861 * @state: Pointer to drm crtc state structure
3862 * @property: Pointer to targeted drm property
3863 * @val: Pointer to variable for receiving property value
3864 * @Returns: Zero on success
3865 */
3866static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
3867 const struct drm_crtc_state *state,
3868 struct drm_property *property,
3869 uint64_t *val)
3870{
3871 struct sde_crtc *sde_crtc;
3872 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003873 struct drm_encoder *encoder;
Clarence Ip24f80662016-06-13 19:05:32 -04003874 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003875 bool conn_offset = 0;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003876 bool is_cmd = true;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003877
3878 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003879 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003880 } else {
3881 sde_crtc = to_sde_crtc(crtc);
3882 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003883
3884 for (i = 0; i < cstate->num_connectors; ++i) {
3885 conn_offset = sde_connector_needs_offset(
3886 cstate->connectors[i]);
3887 if (conn_offset)
3888 break;
3889 }
3890
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003891 /**
3892 * set the cmd flag only when all the encoders attached
3893 * to the crtc are in cmd mode. Consider all other cases
3894 * as video mode.
3895 */
3896 drm_for_each_encoder(encoder, crtc->dev) {
3897 if (encoder->crtc == crtc)
3898 is_cmd &= sde_encoder_is_cmd_mode(encoder);
3899 }
3900
Clarence Ip24f80662016-06-13 19:05:32 -04003901 i = msm_property_index(&sde_crtc->property_info, property);
3902 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08003903 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04003904 CRTC_PROP_OUTPUT_FENCE_OFFSET);
3905
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003906 /**
3907 * set the offset to 0 only for cmd mode panels, so
3908 * the release fence for the current frame can be
3909 * triggered right after PP_DONE interrupt.
3910 */
3911 offset = is_cmd ? 0 : (offset + conn_offset);
3912
Dhaval Patel5cb59be2017-04-20 20:00:56 -07003913 ret = sde_fence_create(&sde_crtc->output_fence, val,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07003914 offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003915 if (ret)
3916 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04003917 } else {
3918 ret = msm_property_atomic_get(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003919 &cstate->property_state,
3920 property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003921 if (ret)
3922 ret = sde_cp_crtc_get_property(crtc,
3923 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04003924 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07003925 if (ret)
3926 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003927 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04003928 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003929}
3930
Alan Kwong67a3f792016-11-01 23:16:53 -04003931#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003932static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04003933{
3934 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003935 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003936 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003937
3938 struct drm_crtc *crtc;
3939 struct drm_plane *plane;
3940 struct drm_display_mode *mode;
3941 struct drm_framebuffer *fb;
3942 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003943 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003944
3945 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04003946
3947 if (!s || !s->private)
3948 return -EINVAL;
3949
3950 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003951 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003952 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003953
3954 mutex_lock(&sde_crtc->crtc_lock);
3955 mode = &crtc->state->adjusted_mode;
3956 out_width = sde_crtc_mixer_width(sde_crtc, mode);
3957
3958 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
3959 mode->hdisplay, mode->vdisplay);
3960
3961 seq_puts(s, "\n");
3962
Clarence Ip8f7366c2016-07-05 12:15:26 -04003963 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04003964 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003965 if (!m->hw_lm)
3966 seq_printf(s, "\tmixer[%d] has no lm\n", i);
3967 else if (!m->hw_ctl)
3968 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
3969 else
3970 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
3971 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
3972 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04003973 }
Dhaval Patel44f12472016-08-29 12:19:47 -07003974
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003975 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07003976
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003977 for (i = 0; i < cstate->num_dim_layers; i++) {
3978 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
3979
3980 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
3981 i, dim_layer->stage, dim_layer->flags);
3982 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
3983 dim_layer->rect.x, dim_layer->rect.y,
3984 dim_layer->rect.w, dim_layer->rect.h);
3985 seq_printf(s,
3986 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
3987 dim_layer->color_fill.color_0,
3988 dim_layer->color_fill.color_1,
3989 dim_layer->color_fill.color_2,
3990 dim_layer->color_fill.color_3);
3991 seq_puts(s, "\n");
3992 }
3993
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003994 drm_atomic_crtc_for_each_plane(plane, crtc) {
3995 pstate = to_sde_plane_state(plane->state);
3996 state = plane->state;
3997
3998 if (!pstate || !state)
3999 continue;
4000
4001 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
4002 pstate->stage);
4003
4004 if (plane->state->fb) {
4005 fb = plane->state->fb;
4006
4007 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
4008 fb->base.id, (char *) &fb->pixel_format,
4009 fb->width, fb->height, fb->bits_per_pixel);
4010
4011 seq_puts(s, "\t");
4012 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
4013 seq_printf(s, "modifier[%d]:%8llu ", i,
4014 fb->modifier[i]);
4015 seq_puts(s, "\n");
4016
4017 seq_puts(s, "\t");
4018 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
4019 seq_printf(s, "pitches[%d]:%8u ", i,
4020 fb->pitches[i]);
4021 seq_puts(s, "\n");
4022
4023 seq_puts(s, "\t");
4024 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
4025 seq_printf(s, "offsets[%d]:%8u ", i,
4026 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07004027 seq_puts(s, "\n");
4028 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004029
4030 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
4031 state->src_x, state->src_y, state->src_w, state->src_h);
4032
4033 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
4034 state->crtc_x, state->crtc_y, state->crtc_w,
4035 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07004036 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
4037 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07004038
4039 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
4040 pstate->excl_rect.x, pstate->excl_rect.y,
4041 pstate->excl_rect.w, pstate->excl_rect.h);
4042
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004043 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04004044 }
Alan Kwong07da0982016-11-04 12:57:45 -04004045
4046 if (sde_crtc->vblank_cb_count) {
4047 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
4048 s64 diff_ms = ktime_to_ms(diff);
4049 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
4050 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
4051
4052 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07004053 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
4054 fps, sde_crtc->vblank_cb_count,
4055 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04004056
4057 /* reset time & count for next measurement */
4058 sde_crtc->vblank_cb_count = 0;
4059 sde_crtc->vblank_cb_time = ktime_set(0, 0);
4060 }
4061
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04004062 seq_printf(s, "vblank_enable:%d\n", sde_crtc->vblank_requested);
Alan Kwong07da0982016-11-04 12:57:45 -04004063
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004064 mutex_unlock(&sde_crtc->crtc_lock);
4065
Clarence Ip8f7366c2016-07-05 12:15:26 -04004066 return 0;
4067}
4068
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004069static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004070{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004071 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004072}
4073
Dhaval Patelf9245d62017-03-28 16:24:00 -07004074static ssize_t _sde_crtc_misr_setup(struct file *file,
4075 const char __user *user_buf, size_t count, loff_t *ppos)
4076{
4077 struct sde_crtc *sde_crtc;
4078 struct sde_crtc_mixer *m;
4079 int i = 0, rc;
4080 char buf[MISR_BUFF_SIZE + 1];
4081 u32 frame_count, enable;
4082 size_t buff_copy;
4083
4084 if (!file || !file->private_data)
4085 return -EINVAL;
4086
4087 sde_crtc = file->private_data;
4088 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
4089 if (copy_from_user(buf, user_buf, buff_copy)) {
4090 SDE_ERROR("buffer copy failed\n");
4091 return -EINVAL;
4092 }
4093
4094 buf[buff_copy] = 0; /* end of string */
4095
4096 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
4097 return -EINVAL;
4098
4099 rc = _sde_crtc_power_enable(sde_crtc, true);
4100 if (rc)
4101 return rc;
4102
4103 mutex_lock(&sde_crtc->crtc_lock);
4104 sde_crtc->misr_enable = enable;
Dhaval Patel010f5172017-08-01 22:40:09 -07004105 sde_crtc->misr_frame_count = frame_count;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004106 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Dhaval Patel010f5172017-08-01 22:40:09 -07004107 sde_crtc->misr_data[i] = 0;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004108 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004109 if (!m->hw_lm || !m->hw_lm->ops.setup_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004110 continue;
4111
4112 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
4113 }
4114 mutex_unlock(&sde_crtc->crtc_lock);
4115 _sde_crtc_power_enable(sde_crtc, false);
4116
4117 return count;
4118}
4119
4120static ssize_t _sde_crtc_misr_read(struct file *file,
4121 char __user *user_buff, size_t count, loff_t *ppos)
4122{
4123 struct sde_crtc *sde_crtc;
4124 struct sde_crtc_mixer *m;
4125 int i = 0, rc;
Dhaval Patel010f5172017-08-01 22:40:09 -07004126 u32 misr_status;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004127 ssize_t len = 0;
4128 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
4129
4130 if (*ppos)
4131 return 0;
4132
4133 if (!file || !file->private_data)
4134 return -EINVAL;
4135
4136 sde_crtc = file->private_data;
4137 rc = _sde_crtc_power_enable(sde_crtc, true);
4138 if (rc)
4139 return rc;
4140
4141 mutex_lock(&sde_crtc->crtc_lock);
4142 if (!sde_crtc->misr_enable) {
4143 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
4144 "disabled\n");
4145 goto buff_check;
4146 }
4147
4148 for (i = 0; i < sde_crtc->num_mixers; ++i) {
4149 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004150 if (!m->hw_lm || !m->hw_lm->ops.collect_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004151 continue;
4152
Dhaval Patel010f5172017-08-01 22:40:09 -07004153 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
4154 sde_crtc->misr_data[i] = misr_status ? misr_status :
4155 sde_crtc->misr_data[i];
Dhaval Patelf9245d62017-03-28 16:24:00 -07004156 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
4157 m->hw_lm->idx - LM_0);
4158 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
Dhaval Patel010f5172017-08-01 22:40:09 -07004159 sde_crtc->misr_data[i]);
Dhaval Patelf9245d62017-03-28 16:24:00 -07004160 }
4161
4162buff_check:
4163 if (count <= len) {
4164 len = 0;
4165 goto end;
4166 }
4167
4168 if (copy_to_user(user_buff, buf, len)) {
4169 len = -EFAULT;
4170 goto end;
4171 }
4172
4173 *ppos += len; /* increase offset */
4174
4175end:
4176 mutex_unlock(&sde_crtc->crtc_lock);
4177 _sde_crtc_power_enable(sde_crtc, false);
4178 return len;
4179}
4180
4181#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04004182static int __prefix ## _open(struct inode *inode, struct file *file) \
4183{ \
4184 return single_open(file, __prefix ## _show, inode->i_private); \
4185} \
4186static const struct file_operations __prefix ## _fops = { \
4187 .owner = THIS_MODULE, \
4188 .open = __prefix ## _open, \
4189 .release = single_release, \
4190 .read = seq_read, \
4191 .llseek = seq_lseek, \
4192}
4193
4194static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
4195{
4196 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04004197 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04004198 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004199 struct sde_crtc_res *res;
Alan Kwong310e9b02017-08-03 02:04:07 -04004200 struct sde_crtc_respool *rp;
Alan Kwong0230a102017-05-16 11:36:44 -07004201 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04004202
4203 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08004204 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08004205 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04004206 seq_printf(s, "core_clk_rate: %llu\n",
4207 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07004208 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
4209 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
4210 seq_printf(s, "bw_ctl[%s]: %llu\n",
4211 sde_power_handle_get_dbus_name(i),
4212 sde_crtc->cur_perf.bw_ctl[i]);
4213 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
4214 sde_power_handle_get_dbus_name(i),
4215 sde_crtc->cur_perf.max_per_pipe_ib[i]);
4216 }
Alan Kwong67a3f792016-11-01 23:16:53 -04004217
Alan Kwong310e9b02017-08-03 02:04:07 -04004218 mutex_lock(&sde_crtc->rp_lock);
4219 list_for_each_entry(rp, &sde_crtc->rp_head, rp_list) {
4220 seq_printf(s, "rp.%d: ", rp->sequence_id);
4221 list_for_each_entry(res, &rp->res_list, list)
4222 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
4223 res->type, res->tag, res->val,
4224 atomic_read(&res->refcount));
4225 seq_puts(s, "\n");
4226 }
4227 mutex_unlock(&sde_crtc->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004228
Alan Kwong67a3f792016-11-01 23:16:53 -04004229 return 0;
4230}
4231DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
4232
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004233static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004234{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004235 struct sde_crtc *sde_crtc;
4236 struct sde_kms *sde_kms;
4237
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004238 static const struct file_operations debugfs_status_fops = {
4239 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04004240 .read = seq_read,
4241 .llseek = seq_lseek,
4242 .release = single_release,
4243 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07004244 static const struct file_operations debugfs_misr_fops = {
4245 .open = simple_open,
4246 .read = _sde_crtc_misr_read,
4247 .write = _sde_crtc_misr_setup,
4248 };
Alan Kwong67a3f792016-11-01 23:16:53 -04004249
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004250 if (!crtc)
4251 return -EINVAL;
4252 sde_crtc = to_sde_crtc(crtc);
4253
4254 sde_kms = _sde_crtc_get_kms(crtc);
4255 if (!sde_kms)
4256 return -EINVAL;
4257
4258 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07004259 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004260 if (!sde_crtc->debugfs_root)
4261 return -ENOMEM;
4262
4263 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004264 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004265 sde_crtc->debugfs_root,
4266 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004267 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004268 sde_crtc->debugfs_root,
4269 &sde_crtc->base,
4270 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004271 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07004272 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004273
4274 return 0;
4275}
4276
4277static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4278{
4279 struct sde_crtc *sde_crtc;
4280
4281 if (!crtc)
4282 return;
4283 sde_crtc = to_sde_crtc(crtc);
4284 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004285}
Alan Kwong67a3f792016-11-01 23:16:53 -04004286#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004287static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04004288{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004289 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04004290}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004291
4292static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4293{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004294}
4295#endif /* CONFIG_DEBUG_FS */
4296
4297static int sde_crtc_late_register(struct drm_crtc *crtc)
4298{
4299 return _sde_crtc_init_debugfs(crtc);
4300}
4301
4302static void sde_crtc_early_unregister(struct drm_crtc *crtc)
4303{
4304 _sde_crtc_destroy_debugfs(crtc);
4305}
4306
4307static const struct drm_crtc_funcs sde_crtc_funcs = {
4308 .set_config = drm_atomic_helper_set_config,
4309 .destroy = sde_crtc_destroy,
4310 .page_flip = drm_atomic_helper_page_flip,
4311 .set_property = sde_crtc_set_property,
4312 .atomic_set_property = sde_crtc_atomic_set_property,
4313 .atomic_get_property = sde_crtc_atomic_get_property,
4314 .reset = sde_crtc_reset,
4315 .atomic_duplicate_state = sde_crtc_duplicate_state,
4316 .atomic_destroy_state = sde_crtc_destroy_state,
4317 .late_register = sde_crtc_late_register,
4318 .early_unregister = sde_crtc_early_unregister,
4319};
4320
4321static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
4322 .mode_fixup = sde_crtc_mode_fixup,
4323 .disable = sde_crtc_disable,
4324 .enable = sde_crtc_enable,
4325 .atomic_check = sde_crtc_atomic_check,
4326 .atomic_begin = sde_crtc_atomic_begin,
4327 .atomic_flush = sde_crtc_atomic_flush,
4328};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004329
Clarence Ipa18d4832017-03-13 12:35:44 -07004330static void _sde_crtc_event_cb(struct kthread_work *work)
4331{
4332 struct sde_crtc_event *event;
4333 struct sde_crtc *sde_crtc;
4334 unsigned long irq_flags;
4335
4336 if (!work) {
4337 SDE_ERROR("invalid work item\n");
4338 return;
4339 }
4340
4341 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004342
4343 /* set sde_crtc to NULL for static work structures */
4344 sde_crtc = event->sde_crtc;
4345 if (!sde_crtc)
4346 return;
4347
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004348 if (event->cb_func)
4349 event->cb_func(&sde_crtc->base, event->usr);
4350
Clarence Ipa18d4832017-03-13 12:35:44 -07004351 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4352 list_add_tail(&event->list, &sde_crtc->event_free_list);
4353 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4354}
4355
4356int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004357 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07004358{
4359 unsigned long irq_flags;
4360 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004361 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07004362 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004363 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07004364
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004365 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
4366 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07004367 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004368 }
Clarence Ipa18d4832017-03-13 12:35:44 -07004369 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004370 priv = crtc->dev->dev_private;
4371 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07004372
4373 /*
4374 * Obtain an event struct from the private cache. This event
4375 * queue may be called from ISR contexts, so use a private
4376 * cache to avoid calling any memory allocation functions.
4377 */
4378 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4379 if (!list_empty(&sde_crtc->event_free_list)) {
4380 event = list_first_entry(&sde_crtc->event_free_list,
4381 struct sde_crtc_event, list);
4382 list_del_init(&event->list);
4383 }
4384 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4385
4386 if (!event)
4387 return -ENOMEM;
4388
4389 /* populate event node */
4390 event->sde_crtc = sde_crtc;
4391 event->cb_func = func;
4392 event->usr = usr;
4393
4394 /* queue new event request */
4395 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004396 kthread_queue_work(&priv->event_thread[crtc_id].worker,
4397 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004398
4399 return 0;
4400}
4401
4402static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
4403{
4404 int i, rc = 0;
4405
4406 if (!sde_crtc) {
4407 SDE_ERROR("invalid crtc\n");
4408 return -EINVAL;
4409 }
4410
4411 spin_lock_init(&sde_crtc->event_lock);
4412
4413 INIT_LIST_HEAD(&sde_crtc->event_free_list);
4414 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
4415 list_add_tail(&sde_crtc->event_cache[i].list,
4416 &sde_crtc->event_free_list);
4417
Clarence Ipa18d4832017-03-13 12:35:44 -07004418 return rc;
4419}
4420
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004421/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04004422struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004423{
4424 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004425 struct sde_crtc *sde_crtc = NULL;
4426 struct msm_drm_private *priv = NULL;
4427 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07004428 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004429
Clarence Ip8f7366c2016-07-05 12:15:26 -04004430 priv = dev->dev_private;
4431 kms = to_sde_kms(priv->kms);
4432
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004433 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
4434 if (!sde_crtc)
4435 return ERR_PTR(-ENOMEM);
4436
4437 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004438 crtc->dev = dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004439
Clarence Ip7f70ce42017-03-20 06:53:46 -07004440 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04004441 spin_lock_init(&sde_crtc->spin_lock);
4442 atomic_set(&sde_crtc->frame_pending, 0);
4443
Alan Kwong310e9b02017-08-03 02:04:07 -04004444 mutex_init(&sde_crtc->rp_lock);
4445 INIT_LIST_HEAD(&sde_crtc->rp_head);
4446
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07004447 init_completion(&sde_crtc->frame_done_comp);
4448
Alan Kwong628d19e2016-10-31 13:50:13 -04004449 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004450 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04004451 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
4452 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
4453 list_add(&sde_crtc->frame_events[i].list,
4454 &sde_crtc->frame_event_list);
4455 kthread_init_work(&sde_crtc->frame_events[i].work,
4456 sde_crtc_frame_event_work);
4457 }
4458
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07004459 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
4460 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004461
4462 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004463 plane->crtc = crtc;
4464
Clarence Ip8f7366c2016-07-05 12:15:26 -04004465 /* save user friendly CRTC name for later */
4466 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
4467
Clarence Ipa18d4832017-03-13 12:35:44 -07004468 /* initialize event handling */
4469 rc = _sde_crtc_init_events(sde_crtc);
4470 if (rc) {
4471 drm_crtc_cleanup(crtc);
4472 kfree(sde_crtc);
4473 return ERR_PTR(rc);
4474 }
4475
Clarence Ip9a74a442016-08-25 18:29:03 -04004476 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04004477 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04004478
Clarence Ip7a753bb2016-07-07 11:47:44 -04004479 /* create CRTC properties */
4480 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
4481 priv->crtc_property, sde_crtc->property_data,
4482 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
4483 sizeof(struct sde_crtc_state));
4484
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004485 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004486
4487 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004488 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004489 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004490
Dhaval Patelec10fad2016-08-22 14:40:48 -07004491 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004492 return crtc;
4493}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004494
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004495static int _sde_crtc_event_enable(struct sde_kms *kms,
4496 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004497{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004498 struct sde_crtc *crtc = NULL;
4499 struct sde_crtc_irq_info *node;
4500 struct msm_drm_private *priv;
4501 unsigned long flags;
4502 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004503 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004504
4505 crtc = to_sde_crtc(crtc_drm);
4506 spin_lock_irqsave(&crtc->spin_lock, flags);
4507 list_for_each_entry(node, &crtc->user_event_list, list) {
4508 if (node->event == event) {
4509 found = true;
4510 break;
4511 }
4512 }
4513 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4514
4515 /* event already enabled */
4516 if (found)
4517 return 0;
4518
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004519 node = NULL;
4520 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
4521 if (custom_events[i].event == event &&
4522 custom_events[i].func) {
4523 node = kzalloc(sizeof(*node), GFP_KERNEL);
4524 if (!node)
4525 return -ENOMEM;
4526 node->event = event;
4527 INIT_LIST_HEAD(&node->list);
4528 node->func = custom_events[i].func;
4529 node->event = event;
4530 break;
4531 }
4532 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004533
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004534 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004535 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004536 return -EINVAL;
4537 }
4538
4539 priv = kms->dev->dev_private;
4540 ret = 0;
4541 if (crtc_drm->enabled) {
4542 sde_power_resource_enable(&priv->phandle, kms->core_client,
4543 true);
4544 ret = node->func(crtc_drm, true, &node->irq);
4545 sde_power_resource_enable(&priv->phandle, kms->core_client,
4546 false);
4547 }
4548
4549 if (!ret) {
4550 spin_lock_irqsave(&crtc->spin_lock, flags);
4551 list_add_tail(&node->list, &crtc->user_event_list);
4552 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4553 } else {
4554 kfree(node);
4555 }
4556
4557 return ret;
4558}
4559
4560static int _sde_crtc_event_disable(struct sde_kms *kms,
4561 struct drm_crtc *crtc_drm, u32 event)
4562{
4563 struct sde_crtc *crtc = NULL;
4564 struct sde_crtc_irq_info *node = NULL;
4565 struct msm_drm_private *priv;
4566 unsigned long flags;
4567 bool found = false;
4568 int ret;
4569
4570 crtc = to_sde_crtc(crtc_drm);
4571 spin_lock_irqsave(&crtc->spin_lock, flags);
4572 list_for_each_entry(node, &crtc->user_event_list, list) {
4573 if (node->event == event) {
4574 list_del(&node->list);
4575 found = true;
4576 break;
4577 }
4578 }
4579 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4580
4581 /* event already disabled */
4582 if (!found)
4583 return 0;
4584
4585 /**
4586 * crtc is disabled interrupts are cleared remove from the list,
4587 * no need to disable/de-register.
4588 */
4589 if (!crtc_drm->enabled) {
4590 kfree(node);
4591 return 0;
4592 }
4593 priv = kms->dev->dev_private;
4594 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
4595 ret = node->func(crtc_drm, false, &node->irq);
4596 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
4597 return ret;
4598}
4599
4600int sde_crtc_register_custom_event(struct sde_kms *kms,
4601 struct drm_crtc *crtc_drm, u32 event, bool en)
4602{
4603 struct sde_crtc *crtc = NULL;
4604 int ret;
4605
4606 crtc = to_sde_crtc(crtc_drm);
4607 if (!crtc || !kms || !kms->dev) {
4608 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
4609 kms, ((kms) ? (kms->dev) : NULL));
4610 return -EINVAL;
4611 }
4612
4613 if (en)
4614 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
4615 else
4616 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
4617
4618 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004619}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07004620
4621static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
4622 bool en, struct sde_irq_callback *irq)
4623{
4624 return 0;
4625}
Benjamin Chan90139102017-06-21 16:00:39 -04004626
4627static int sde_crtc_pm_event_handler(struct drm_crtc *crtc, bool en,
4628 struct sde_irq_callback *noirq)
4629{
4630 /*
4631 * IRQ object noirq is not being used here since there is
4632 * no crtc irq from pm event.
4633 */
4634 return 0;
4635}