blob: e0336472605600526cd213f86574dc85e9566ef6 [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);
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +053067static int sde_crtc_idle_interrupt_handler(struct drm_crtc *crtc_drm,
68 bool en, struct sde_irq_callback *idle_irq);
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070069
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070070static struct sde_crtc_custom_events custom_events[] = {
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -070071 {DRM_EVENT_AD_BACKLIGHT, sde_cp_ad_interrupt},
Benjamin Chan90139102017-06-21 16:00:39 -040072 {DRM_EVENT_CRTC_POWER, sde_crtc_power_interrupt_handler},
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +053073 {DRM_EVENT_IDLE_NOTIFY, sde_crtc_idle_interrupt_handler}
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -070074};
75
Clarence Ipcae1bb62016-07-07 12:07:13 -040076/* default input fence timeout, in ms */
Dhaval Patelb9850c02017-08-07 22:55:47 -070077#define SDE_CRTC_INPUT_FENCE_TIMEOUT 10000
Clarence Ipcae1bb62016-07-07 12:07:13 -040078
Dhaval Patel4e574842016-08-23 15:11:37 -070079/*
80 * The default input fence timeout is 2 seconds while max allowed
81 * range is 10 seconds. Any value above 10 seconds adds glitches beyond
82 * tolerance limit.
83 */
84#define SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT 10000
85
Dhaval Patel48c76022016-09-01 17:51:23 -070086/* layer mixer index on sde_crtc */
87#define LEFT_MIXER 0
88#define RIGHT_MIXER 1
89
Dhaval Patelf9245d62017-03-28 16:24:00 -070090#define MISR_BUFF_SIZE 256
91
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -040092static inline struct sde_kms *_sde_crtc_get_kms(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -040093{
Clarence Ip7f70ce42017-03-20 06:53:46 -070094 struct msm_drm_private *priv;
95
96 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
97 SDE_ERROR("invalid crtc\n");
98 return NULL;
99 }
100 priv = crtc->dev->dev_private;
101 if (!priv || !priv->kms) {
102 SDE_ERROR("invalid kms\n");
103 return NULL;
104 }
Abhijit Kulkarni40e38162016-06-26 22:12:09 -0400105
Ben Chan78647cd2016-06-26 22:02:47 -0400106 return to_sde_kms(priv->kms);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400107}
108
Dhaval Patelf9245d62017-03-28 16:24:00 -0700109static inline int _sde_crtc_power_enable(struct sde_crtc *sde_crtc, bool enable)
110{
111 struct drm_crtc *crtc;
112 struct msm_drm_private *priv;
113 struct sde_kms *sde_kms;
114
115 if (!sde_crtc) {
116 SDE_ERROR("invalid sde crtc\n");
117 return -EINVAL;
118 }
119
120 crtc = &sde_crtc->base;
121 if (!crtc->dev || !crtc->dev->dev_private) {
122 SDE_ERROR("invalid drm device\n");
123 return -EINVAL;
124 }
125
126 priv = crtc->dev->dev_private;
127 if (!priv->kms) {
128 SDE_ERROR("invalid kms\n");
129 return -EINVAL;
130 }
131
132 sde_kms = to_sde_kms(priv->kms);
133
134 return sde_power_resource_enable(&priv->phandle, sde_kms->core_client,
135 enable);
136}
137
Alan Kwongcdb2f282017-03-18 13:42:06 -0700138/**
139 * _sde_crtc_rp_to_crtc - get crtc from resource pool object
140 * @rp: Pointer to resource pool
141 * return: Pointer to drm crtc if success; null otherwise
142 */
143static struct drm_crtc *_sde_crtc_rp_to_crtc(struct sde_crtc_respool *rp)
144{
145 if (!rp)
146 return NULL;
147
148 return container_of(rp, struct sde_crtc_state, rp)->base.crtc;
149}
150
151/**
152 * _sde_crtc_rp_reclaim - reclaim unused, or all if forced, resources in pool
153 * @rp: Pointer to resource pool
154 * @force: True to reclaim all resources; otherwise, reclaim only unused ones
155 * return: None
156 */
157static void _sde_crtc_rp_reclaim(struct sde_crtc_respool *rp, bool force)
158{
159 struct sde_crtc_res *res, *next;
160 struct drm_crtc *crtc;
161
162 crtc = _sde_crtc_rp_to_crtc(rp);
163 if (!crtc) {
164 SDE_ERROR("invalid crtc\n");
165 return;
166 }
167
168 SDE_DEBUG("crtc%d.%u %s\n", crtc->base.id, rp->sequence_id,
169 force ? "destroy" : "free_unused");
170
171 list_for_each_entry_safe(res, next, &rp->res_list, list) {
172 if (!force && !(res->flags & SDE_CRTC_RES_FLAG_FREE))
173 continue;
174 SDE_DEBUG("crtc%d.%u reclaim res:0x%x/0x%llx/%pK/%d\n",
175 crtc->base.id, rp->sequence_id,
176 res->type, res->tag, res->val,
177 atomic_read(&res->refcount));
178 list_del(&res->list);
179 if (res->ops.put)
180 res->ops.put(res->val);
181 kfree(res);
182 }
183}
184
185/**
186 * _sde_crtc_rp_free_unused - free unused resource in pool
187 * @rp: Pointer to resource pool
188 * return: none
189 */
190static void _sde_crtc_rp_free_unused(struct sde_crtc_respool *rp)
191{
Alan Kwong310e9b02017-08-03 02:04:07 -0400192 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700193 _sde_crtc_rp_reclaim(rp, false);
Alan Kwong310e9b02017-08-03 02:04:07 -0400194 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700195}
196
197/**
198 * _sde_crtc_rp_destroy - destroy resource pool
199 * @rp: Pointer to resource pool
200 * return: None
201 */
202static void _sde_crtc_rp_destroy(struct sde_crtc_respool *rp)
203{
Alan Kwong310e9b02017-08-03 02:04:07 -0400204 mutex_lock(rp->rp_lock);
205 list_del_init(&rp->rp_list);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700206 _sde_crtc_rp_reclaim(rp, true);
Alan Kwong310e9b02017-08-03 02:04:07 -0400207 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700208}
209
210/**
211 * _sde_crtc_hw_blk_get - get callback for hardware block
212 * @val: Resource handle
213 * @type: Resource type
214 * @tag: Search tag for given resource
215 * return: Resource handle
216 */
217static void *_sde_crtc_hw_blk_get(void *val, u32 type, u64 tag)
218{
219 SDE_DEBUG("res:%d/0x%llx/%pK\n", type, tag, val);
220 return sde_hw_blk_get(val, type, tag);
221}
222
223/**
224 * _sde_crtc_hw_blk_put - put callback for hardware block
225 * @val: Resource handle
226 * return: None
227 */
228static void _sde_crtc_hw_blk_put(void *val)
229{
230 SDE_DEBUG("res://%pK\n", val);
231 sde_hw_blk_put(val);
232}
233
234/**
235 * _sde_crtc_rp_duplicate - duplicate resource pool and reset reference count
236 * @rp: Pointer to original resource pool
237 * @dup_rp: Pointer to duplicated resource pool
238 * return: None
239 */
240static void _sde_crtc_rp_duplicate(struct sde_crtc_respool *rp,
241 struct sde_crtc_respool *dup_rp)
242{
243 struct sde_crtc_res *res, *dup_res;
244 struct drm_crtc *crtc;
245
Alan Kwong310e9b02017-08-03 02:04:07 -0400246 if (!rp || !dup_rp || !rp->rp_head) {
Alan Kwongcdb2f282017-03-18 13:42:06 -0700247 SDE_ERROR("invalid resource pool\n");
248 return;
249 }
250
251 crtc = _sde_crtc_rp_to_crtc(rp);
252 if (!crtc) {
253 SDE_ERROR("invalid crtc\n");
254 return;
255 }
256
257 SDE_DEBUG("crtc%d.%u duplicate\n", crtc->base.id, rp->sequence_id);
258
Alan Kwong310e9b02017-08-03 02:04:07 -0400259 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700260 dup_rp->sequence_id = rp->sequence_id + 1;
261 INIT_LIST_HEAD(&dup_rp->res_list);
262 dup_rp->ops = rp->ops;
263 list_for_each_entry(res, &rp->res_list, list) {
264 dup_res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
Alan Kwong310e9b02017-08-03 02:04:07 -0400265 if (!dup_res) {
266 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700267 return;
Alan Kwong310e9b02017-08-03 02:04:07 -0400268 }
Alan Kwongcdb2f282017-03-18 13:42:06 -0700269 INIT_LIST_HEAD(&dup_res->list);
270 atomic_set(&dup_res->refcount, 0);
271 dup_res->type = res->type;
272 dup_res->tag = res->tag;
273 dup_res->val = res->val;
274 dup_res->ops = res->ops;
275 dup_res->flags = SDE_CRTC_RES_FLAG_FREE;
276 SDE_DEBUG("crtc%d.%u dup res:0x%x/0x%llx/%pK/%d\n",
277 crtc->base.id, dup_rp->sequence_id,
278 dup_res->type, dup_res->tag, dup_res->val,
279 atomic_read(&dup_res->refcount));
280 list_add_tail(&dup_res->list, &dup_rp->res_list);
281 if (dup_res->ops.get)
282 dup_res->ops.get(dup_res->val, 0, -1);
283 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400284
285 dup_rp->rp_lock = rp->rp_lock;
286 dup_rp->rp_head = rp->rp_head;
287 INIT_LIST_HEAD(&dup_rp->rp_list);
288 list_add_tail(&dup_rp->rp_list, rp->rp_head);
289 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700290}
291
292/**
293 * _sde_crtc_rp_reset - reset resource pool after allocation
294 * @rp: Pointer to original resource pool
Alan Kwong310e9b02017-08-03 02:04:07 -0400295 * @rp_lock: Pointer to serialization resource pool lock
296 * @rp_head: Pointer to crtc resource pool head
Alan Kwongcdb2f282017-03-18 13:42:06 -0700297 * return: None
298 */
Alan Kwong310e9b02017-08-03 02:04:07 -0400299static void _sde_crtc_rp_reset(struct sde_crtc_respool *rp,
300 struct mutex *rp_lock, struct list_head *rp_head)
Alan Kwongcdb2f282017-03-18 13:42:06 -0700301{
Alan Kwong310e9b02017-08-03 02:04:07 -0400302 if (!rp || !rp_lock || !rp_head) {
Alan Kwongcdb2f282017-03-18 13:42:06 -0700303 SDE_ERROR("invalid resource pool\n");
304 return;
305 }
306
Alan Kwong310e9b02017-08-03 02:04:07 -0400307 mutex_lock(rp_lock);
308 rp->rp_lock = rp_lock;
309 rp->rp_head = rp_head;
310 INIT_LIST_HEAD(&rp->rp_list);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700311 rp->sequence_id = 0;
312 INIT_LIST_HEAD(&rp->res_list);
313 rp->ops.get = _sde_crtc_hw_blk_get;
314 rp->ops.put = _sde_crtc_hw_blk_put;
Alan Kwong310e9b02017-08-03 02:04:07 -0400315 list_add_tail(&rp->rp_list, rp->rp_head);
316 mutex_unlock(rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700317}
318
319/**
Alan Kwong310e9b02017-08-03 02:04:07 -0400320 * _sde_crtc_rp_add_no_lock - add given resource to resource pool without lock
Alan Kwongcdb2f282017-03-18 13:42:06 -0700321 * @rp: Pointer to original resource pool
322 * @type: Resource type
323 * @tag: Search tag for given resource
324 * @val: Resource handle
325 * @ops: Resource callback operations
326 * return: 0 if success; error code otherwise
327 */
Alan Kwong310e9b02017-08-03 02:04:07 -0400328static int _sde_crtc_rp_add_no_lock(struct sde_crtc_respool *rp, u32 type,
329 u64 tag, void *val, struct sde_crtc_res_ops *ops)
Alan Kwongcdb2f282017-03-18 13:42:06 -0700330{
331 struct sde_crtc_res *res;
332 struct drm_crtc *crtc;
333
334 if (!rp || !ops) {
335 SDE_ERROR("invalid resource pool/ops\n");
336 return -EINVAL;
337 }
338
339 crtc = _sde_crtc_rp_to_crtc(rp);
340 if (!crtc) {
341 SDE_ERROR("invalid crtc\n");
342 return -EINVAL;
343 }
344
345 list_for_each_entry(res, &rp->res_list, list) {
346 if (res->type != type || res->tag != tag)
347 continue;
348 SDE_ERROR("crtc%d.%u already exist res:0x%x/0x%llx/%pK/%d\n",
349 crtc->base.id, rp->sequence_id,
350 res->type, res->tag, res->val,
351 atomic_read(&res->refcount));
352 return -EEXIST;
353 }
354 res = kzalloc(sizeof(struct sde_crtc_res), GFP_KERNEL);
355 if (!res)
356 return -ENOMEM;
357 INIT_LIST_HEAD(&res->list);
358 atomic_set(&res->refcount, 1);
359 res->type = type;
360 res->tag = tag;
361 res->val = val;
362 res->ops = *ops;
363 list_add_tail(&res->list, &rp->res_list);
364 SDE_DEBUG("crtc%d.%u added res:0x%x/0x%llx\n",
365 crtc->base.id, rp->sequence_id, type, tag);
366 return 0;
367}
368
369/**
Alan Kwong310e9b02017-08-03 02:04:07 -0400370 * _sde_crtc_rp_add - add given resource to resource pool
371 * @rp: Pointer to original resource pool
372 * @type: Resource type
373 * @tag: Search tag for given resource
374 * @val: Resource handle
375 * @ops: Resource callback operations
376 * return: 0 if success; error code otherwise
377 */
378static int _sde_crtc_rp_add(struct sde_crtc_respool *rp, u32 type, u64 tag,
379 void *val, struct sde_crtc_res_ops *ops)
380{
381 int rc;
382
383 if (!rp) {
384 SDE_ERROR("invalid resource pool\n");
385 return -EINVAL;
386 }
387
388 mutex_lock(rp->rp_lock);
389 rc = _sde_crtc_rp_add_no_lock(rp, type, tag, val, ops);
390 mutex_unlock(rp->rp_lock);
391 return rc;
392}
393
394/**
Alan Kwongcdb2f282017-03-18 13:42:06 -0700395 * _sde_crtc_rp_get - lookup the resource from given resource pool and obtain
396 * if available; otherwise, obtain resource from global pool
397 * @rp: Pointer to original resource pool
398 * @type: Resource type
399 * @tag: Search tag for given resource
400 * return: Resource handle if success; pointer error or null otherwise
401 */
402static void *_sde_crtc_rp_get(struct sde_crtc_respool *rp, u32 type, u64 tag)
403{
Alan Kwong310e9b02017-08-03 02:04:07 -0400404 struct sde_crtc_respool *old_rp;
Alan Kwongcdb2f282017-03-18 13:42:06 -0700405 struct sde_crtc_res *res;
406 void *val = NULL;
407 int rc;
408 struct drm_crtc *crtc;
409
410 if (!rp) {
411 SDE_ERROR("invalid resource pool\n");
412 return NULL;
413 }
414
415 crtc = _sde_crtc_rp_to_crtc(rp);
416 if (!crtc) {
417 SDE_ERROR("invalid crtc\n");
418 return NULL;
419 }
420
Alan Kwong310e9b02017-08-03 02:04:07 -0400421 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700422 list_for_each_entry(res, &rp->res_list, list) {
423 if (res->type != type || res->tag != tag)
424 continue;
425 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
426 crtc->base.id, rp->sequence_id,
427 res->type, res->tag, res->val,
428 atomic_read(&res->refcount));
429 atomic_inc(&res->refcount);
430 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
Alan Kwong310e9b02017-08-03 02:04:07 -0400431 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700432 return res->val;
433 }
434 list_for_each_entry(res, &rp->res_list, list) {
435 if (res->type != type || !(res->flags & SDE_CRTC_RES_FLAG_FREE))
436 continue;
437 SDE_DEBUG("crtc%d.%u retag res:0x%x/0x%llx/%pK/%d\n",
438 crtc->base.id, rp->sequence_id,
439 res->type, res->tag, res->val,
440 atomic_read(&res->refcount));
441 atomic_inc(&res->refcount);
442 res->tag = tag;
443 res->flags &= ~SDE_CRTC_RES_FLAG_FREE;
Alan Kwong310e9b02017-08-03 02:04:07 -0400444 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700445 return res->val;
446 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400447 /* not in this rp, try to grab from global pool */
Alan Kwongcdb2f282017-03-18 13:42:06 -0700448 if (rp->ops.get)
449 val = rp->ops.get(NULL, type, -1);
Alan Kwong310e9b02017-08-03 02:04:07 -0400450 if (!IS_ERR_OR_NULL(val))
451 goto add_res;
452 /*
453 * Search older resource pools for hw blk with matching type,
454 * necessary when resource is being used by this object,
455 * but in previous states not yet cleaned up.
456 *
457 * This enables searching of all resources currently owned
458 * by this crtc even though the resource might not be used
459 * in the current atomic state. This allows those resources
460 * to be re-acquired by the new atomic state immediately
461 * without waiting for the resources to be fully released.
462 */
463 else if (IS_ERR_OR_NULL(val) && (type < SDE_HW_BLK_MAX)) {
464 list_for_each_entry(old_rp, rp->rp_head, rp_list) {
465 if (old_rp == rp)
466 continue;
467
468 list_for_each_entry(res, &old_rp->res_list, list) {
469 if (res->type != type)
470 continue;
471 SDE_DEBUG(
472 "crtc%d.%u found res:0x%x//%pK/ in crtc%d.%d\n",
473 crtc->base.id,
474 rp->sequence_id,
475 res->type, res->val,
476 crtc->base.id,
477 old_rp->sequence_id);
478 SDE_EVT32_VERBOSE(crtc->base.id,
479 rp->sequence_id,
480 res->type, res->val,
481 crtc->base.id,
482 old_rp->sequence_id);
483 if (res->ops.get)
484 res->ops.get(res->val, 0, -1);
485 val = res->val;
486 break;
487 }
488
489 if (!IS_ERR_OR_NULL(val))
490 break;
491 }
492 }
Alan Kwongcdb2f282017-03-18 13:42:06 -0700493 if (IS_ERR_OR_NULL(val)) {
Alan Kwong42e35052017-05-05 06:52:51 -0700494 SDE_DEBUG("crtc%d.%u failed to get res:0x%x//\n",
Alan Kwongcdb2f282017-03-18 13:42:06 -0700495 crtc->base.id, rp->sequence_id, type);
Alan Kwong310e9b02017-08-03 02:04:07 -0400496 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700497 return NULL;
498 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400499add_res:
500 rc = _sde_crtc_rp_add_no_lock(rp, type, tag, val, &rp->ops);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700501 if (rc) {
502 SDE_ERROR("crtc%d.%u failed to add res:0x%x/0x%llx\n",
503 crtc->base.id, rp->sequence_id, type, tag);
504 if (rp->ops.put)
505 rp->ops.put(val);
506 val = NULL;
507 }
Alan Kwong310e9b02017-08-03 02:04:07 -0400508 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700509 return val;
510}
511
512/**
513 * _sde_crtc_rp_put - return given resource to resource pool
514 * @rp: Pointer to original resource pool
515 * @type: Resource type
516 * @tag: Search tag for given resource
517 * return: None
518 */
519static void _sde_crtc_rp_put(struct sde_crtc_respool *rp, u32 type, u64 tag)
520{
521 struct sde_crtc_res *res, *next;
522 struct drm_crtc *crtc;
523
524 if (!rp) {
525 SDE_ERROR("invalid resource pool\n");
526 return;
527 }
528
529 crtc = _sde_crtc_rp_to_crtc(rp);
530 if (!crtc) {
531 SDE_ERROR("invalid crtc\n");
532 return;
533 }
534
Alan Kwong310e9b02017-08-03 02:04:07 -0400535 mutex_lock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700536 list_for_each_entry_safe(res, next, &rp->res_list, list) {
537 if (res->type != type || res->tag != tag)
538 continue;
539 SDE_DEBUG("crtc%d.%u found res:0x%x/0x%llx/%pK/%d\n",
540 crtc->base.id, rp->sequence_id,
541 res->type, res->tag, res->val,
542 atomic_read(&res->refcount));
543 if (res->flags & SDE_CRTC_RES_FLAG_FREE)
544 SDE_ERROR(
545 "crtc%d.%u already free res:0x%x/0x%llx/%pK/%d\n",
546 crtc->base.id, rp->sequence_id,
547 res->type, res->tag, res->val,
548 atomic_read(&res->refcount));
549 else if (atomic_dec_return(&res->refcount) == 0)
550 res->flags |= SDE_CRTC_RES_FLAG_FREE;
551
Alan Kwong310e9b02017-08-03 02:04:07 -0400552 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700553 return;
554 }
555 SDE_ERROR("crtc%d.%u not found res:0x%x/0x%llx\n",
556 crtc->base.id, rp->sequence_id, type, tag);
Alan Kwong310e9b02017-08-03 02:04:07 -0400557 mutex_unlock(rp->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -0700558}
559
560int sde_crtc_res_add(struct drm_crtc_state *state, u32 type, u64 tag,
561 void *val, struct sde_crtc_res_ops *ops)
562{
563 struct sde_crtc_respool *rp;
564
565 if (!state) {
566 SDE_ERROR("invalid parameters\n");
567 return -EINVAL;
568 }
569
570 rp = &to_sde_crtc_state(state)->rp;
571 return _sde_crtc_rp_add(rp, type, tag, val, ops);
572}
573
574void *sde_crtc_res_get(struct drm_crtc_state *state, u32 type, u64 tag)
575{
576 struct sde_crtc_respool *rp;
577 void *val;
578
579 if (!state) {
580 SDE_ERROR("invalid parameters\n");
581 return NULL;
582 }
583
584 rp = &to_sde_crtc_state(state)->rp;
585 val = _sde_crtc_rp_get(rp, type, tag);
586 if (IS_ERR(val)) {
587 SDE_ERROR("failed to get res type:0x%x:0x%llx\n",
588 type, tag);
589 return NULL;
590 }
591
592 return val;
593}
594
595void sde_crtc_res_put(struct drm_crtc_state *state, u32 type, u64 tag)
596{
597 struct sde_crtc_respool *rp;
598
599 if (!state) {
600 SDE_ERROR("invalid parameters\n");
601 return;
602 }
603
604 rp = &to_sde_crtc_state(state)->rp;
605 _sde_crtc_rp_put(rp, type, tag);
606}
607
Clarence Ipa18d4832017-03-13 12:35:44 -0700608static void _sde_crtc_deinit_events(struct sde_crtc *sde_crtc)
609{
610 if (!sde_crtc)
611 return;
Clarence Ipa18d4832017-03-13 12:35:44 -0700612}
613
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700614static void sde_crtc_destroy(struct drm_crtc *crtc)
615{
616 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
617
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400618 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -0400619
620 if (!crtc)
621 return;
622
Dhaval Patele4a5dda2016-10-13 19:29:30 -0700623 if (sde_crtc->blob_info)
624 drm_property_unreference_blob(sde_crtc->blob_info);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400625 msm_property_destroy(&sde_crtc->property_info);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -0700626 sde_cp_crtc_destroy_properties(crtc);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -0700627
Clarence Ip24f80662016-06-13 19:05:32 -0400628 sde_fence_deinit(&sde_crtc->output_fence);
Clarence Ipa18d4832017-03-13 12:35:44 -0700629 _sde_crtc_deinit_events(sde_crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -0400630
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700631 drm_crtc_cleanup(crtc);
Clarence Ip7f70ce42017-03-20 06:53:46 -0700632 mutex_destroy(&sde_crtc->crtc_lock);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700633 kfree(sde_crtc);
634}
635
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700636static bool sde_crtc_mode_fixup(struct drm_crtc *crtc,
637 const struct drm_display_mode *mode,
638 struct drm_display_mode *adjusted_mode)
639{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400640 SDE_DEBUG("\n");
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400641
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -0400642 if (msm_is_mode_seamless(adjusted_mode) &&
643 (!crtc->enabled || crtc->state->active_changed)) {
644 SDE_ERROR("crtc state prevents seamless transition\n");
645 return false;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -0400646 }
647
Narendra Muppalla1b0b3352015-09-29 10:16:51 -0700648 return true;
649}
650
Dhaval Patel48c76022016-09-01 17:51:23 -0700651static void _sde_crtc_setup_blend_cfg(struct sde_crtc_mixer *mixer,
652 struct sde_plane_state *pstate, struct sde_format *format)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -0400653{
Dhaval Patel48c76022016-09-01 17:51:23 -0700654 uint32_t blend_op, fg_alpha, bg_alpha;
655 uint32_t blend_type;
Dhaval Patel44f12472016-08-29 12:19:47 -0700656 struct sde_hw_mixer *lm = mixer->hw_lm;
657
Dhaval Patel48c76022016-09-01 17:51:23 -0700658 /* default to opaque blending */
659 fg_alpha = sde_plane_get_property(pstate, PLANE_PROP_ALPHA);
660 bg_alpha = 0xFF - fg_alpha;
661 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST | SDE_BLEND_BG_ALPHA_BG_CONST;
662 blend_type = sde_plane_get_property(pstate, PLANE_PROP_BLEND_OP);
Dhaval Patel44f12472016-08-29 12:19:47 -0700663
Dhaval Patel48c76022016-09-01 17:51:23 -0700664 SDE_DEBUG("blend type:0x%x blend alpha:0x%x\n", blend_type, fg_alpha);
665
666 switch (blend_type) {
667
668 case SDE_DRM_BLEND_OP_OPAQUE:
669 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
670 SDE_BLEND_BG_ALPHA_BG_CONST;
671 break;
672
673 case SDE_DRM_BLEND_OP_PREMULTIPLIED:
674 if (format->alpha_enable) {
675 blend_op = SDE_BLEND_FG_ALPHA_FG_CONST |
676 SDE_BLEND_BG_ALPHA_FG_PIXEL;
677 if (fg_alpha != 0xff) {
678 bg_alpha = fg_alpha;
679 blend_op |= SDE_BLEND_BG_MOD_ALPHA |
680 SDE_BLEND_BG_INV_MOD_ALPHA;
681 } else {
682 blend_op |= SDE_BLEND_BG_INV_ALPHA;
683 }
684 }
685 break;
686
687 case SDE_DRM_BLEND_OP_COVERAGE:
688 if (format->alpha_enable) {
689 blend_op = SDE_BLEND_FG_ALPHA_FG_PIXEL |
690 SDE_BLEND_BG_ALPHA_FG_PIXEL;
691 if (fg_alpha != 0xff) {
692 bg_alpha = fg_alpha;
693 blend_op |= SDE_BLEND_FG_MOD_ALPHA |
694 SDE_BLEND_FG_INV_MOD_ALPHA |
695 SDE_BLEND_BG_MOD_ALPHA |
696 SDE_BLEND_BG_INV_MOD_ALPHA;
697 } else {
698 blend_op |= SDE_BLEND_BG_INV_ALPHA;
699 }
700 }
701 break;
702 default:
703 /* do nothing */
704 break;
Clarence Ipd9f9fa62016-09-09 13:42:32 -0400705 }
Dhaval Patel48c76022016-09-01 17:51:23 -0700706
707 lm->ops.setup_blend_config(lm, pstate->stage, fg_alpha,
708 bg_alpha, blend_op);
Dhaval Patel6c666622017-03-21 23:02:59 -0700709 SDE_DEBUG(
710 "format: %4.4s, alpha_enable %u fg alpha:0x%x bg alpha:0x%x blend_op:0x%x\n",
711 (char *) &format->base.pixel_format,
Dhaval Patel48c76022016-09-01 17:51:23 -0700712 format->alpha_enable, fg_alpha, bg_alpha, blend_op);
713}
714
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800715static void _sde_crtc_setup_dim_layer_cfg(struct drm_crtc *crtc,
716 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer,
717 struct sde_hw_dim_layer *dim_layer)
718{
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500719 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800720 struct sde_hw_mixer *lm;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800721 struct sde_hw_dim_layer split_dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800722 int i;
723
724 if (!dim_layer->rect.w || !dim_layer->rect.h) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700725 SDE_DEBUG("empty dim_layer\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800726 return;
727 }
728
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500729 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800730
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700731 SDE_DEBUG("dim_layer - flags:%d, stage:%d\n",
732 dim_layer->flags, dim_layer->stage);
733
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800734 split_dim_layer.stage = dim_layer->stage;
735 split_dim_layer.color_fill = dim_layer->color_fill;
736
737 /*
738 * traverse through the layer mixers attached to crtc and find the
739 * intersecting dim layer rect in each LM and program accordingly.
740 */
741 for (i = 0; i < sde_crtc->num_mixers; i++) {
742 split_dim_layer.flags = dim_layer->flags;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800743
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500744 sde_kms_rect_intersect(&cstate->lm_bounds[i], &dim_layer->rect,
Lloyd Atkinsone0e11e22017-01-17 12:08:48 -0500745 &split_dim_layer.rect);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500746 if (sde_kms_rect_is_null(&split_dim_layer.rect)) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800747 /*
748 * no extra programming required for non-intersecting
749 * layer mixers with INCLUSIVE dim layer
750 */
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500751 if (split_dim_layer.flags & SDE_DRM_DIM_LAYER_INCLUSIVE)
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800752 continue;
753
754 /*
755 * program the other non-intersecting layer mixers with
756 * INCLUSIVE dim layer of full size for uniformity
757 * with EXCLUSIVE dim layer config.
758 */
759 split_dim_layer.flags &= ~SDE_DRM_DIM_LAYER_EXCLUSIVE;
760 split_dim_layer.flags |= SDE_DRM_DIM_LAYER_INCLUSIVE;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500761 memcpy(&split_dim_layer.rect, &cstate->lm_bounds[i],
762 sizeof(split_dim_layer.rect));
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800763
764 } else {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -0500765 split_dim_layer.rect.x =
766 split_dim_layer.rect.x -
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700767 cstate->lm_bounds[i].x;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800768 }
769
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -0700770 SDE_DEBUG("split_dim_layer - LM:%d, rect:{%d,%d,%d,%d}}\n",
771 i, split_dim_layer.rect.x, split_dim_layer.rect.y,
772 split_dim_layer.rect.w, split_dim_layer.rect.h);
773
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -0800774 lm = mixer[i].hw_lm;
775 mixer[i].mixer_op_mode |= 1 << split_dim_layer.stage;
776 lm->ops.setup_dim_layer(lm, &split_dim_layer);
777 }
778}
779
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400780void sde_crtc_get_crtc_roi(struct drm_crtc_state *state,
781 const struct sde_rect **crtc_roi)
782{
783 struct sde_crtc_state *crtc_state;
784
785 if (!state || !crtc_roi)
786 return;
787
788 crtc_state = to_sde_crtc_state(state);
789 *crtc_roi = &crtc_state->crtc_roi;
790}
791
792static int _sde_crtc_set_roi_v1(struct drm_crtc_state *state,
793 void *usr_ptr)
794{
795 struct drm_crtc *crtc;
796 struct sde_crtc_state *cstate;
797 struct sde_drm_roi_v1 roi_v1;
798 int i;
799
800 if (!state) {
801 SDE_ERROR("invalid args\n");
802 return -EINVAL;
803 }
804
805 cstate = to_sde_crtc_state(state);
806 crtc = cstate->base.crtc;
807
808 memset(&cstate->user_roi_list, 0, sizeof(cstate->user_roi_list));
809
810 if (!usr_ptr) {
811 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
812 return 0;
813 }
814
815 if (copy_from_user(&roi_v1, usr_ptr, sizeof(roi_v1))) {
816 SDE_ERROR("crtc%d: failed to copy roi_v1 data\n", DRMID(crtc));
817 return -EINVAL;
818 }
819
820 SDE_DEBUG("crtc%d: num_rects %d\n", DRMID(crtc), roi_v1.num_rects);
821
822 if (roi_v1.num_rects == 0) {
823 SDE_DEBUG("crtc%d: rois cleared\n", DRMID(crtc));
824 return 0;
825 }
826
827 if (roi_v1.num_rects > SDE_MAX_ROI_V1) {
828 SDE_ERROR("crtc%d: too many rects specified: %d\n", DRMID(crtc),
829 roi_v1.num_rects);
830 return -EINVAL;
831 }
832
833 cstate->user_roi_list.num_rects = roi_v1.num_rects;
834 for (i = 0; i < roi_v1.num_rects; ++i) {
835 cstate->user_roi_list.roi[i] = roi_v1.roi[i];
836 SDE_DEBUG("crtc%d: roi%d: roi (%d,%d) (%d,%d)\n",
837 DRMID(crtc), i,
838 cstate->user_roi_list.roi[i].x1,
839 cstate->user_roi_list.roi[i].y1,
840 cstate->user_roi_list.roi[i].x2,
841 cstate->user_roi_list.roi[i].y2);
842 }
843
844 return 0;
845}
846
Ingrid Gallardo83532222017-06-02 16:48:51 -0700847static bool _sde_crtc_setup_is_3dmux_dsc(struct drm_crtc_state *state)
848{
849 int i;
850 struct sde_crtc_state *cstate;
851 bool is_3dmux_dsc = false;
852
853 cstate = to_sde_crtc_state(state);
854
855 for (i = 0; i < cstate->num_connectors; i++) {
856 struct drm_connector *conn = cstate->connectors[i];
857
858 if (sde_connector_get_topology_name(conn) ==
859 SDE_RM_TOPOLOGY_DUALPIPE_3DMERGE_DSC)
860 is_3dmux_dsc = true;
861 }
862
863 return is_3dmux_dsc;
864}
865
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400866static int _sde_crtc_set_crtc_roi(struct drm_crtc *crtc,
867 struct drm_crtc_state *state)
868{
869 struct drm_connector *conn;
870 struct drm_connector_state *conn_state;
871 struct sde_crtc *sde_crtc;
872 struct sde_crtc_state *crtc_state;
873 struct sde_rect *crtc_roi;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400874 int i, num_attached_conns = 0;
875
876 if (!crtc || !state)
877 return -EINVAL;
878
879 sde_crtc = to_sde_crtc(crtc);
880 crtc_state = to_sde_crtc_state(state);
881 crtc_roi = &crtc_state->crtc_roi;
882
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400883 for_each_connector_in_state(state->state, conn, conn_state, i) {
884 struct sde_connector_state *sde_conn_state;
885
886 if (!conn_state || conn_state->crtc != crtc)
887 continue;
888
889 if (num_attached_conns) {
890 SDE_ERROR(
891 "crtc%d: unsupported: roi on crtc w/ >1 connectors\n",
892 DRMID(crtc));
893 return -EINVAL;
894 }
895 ++num_attached_conns;
896
897 sde_conn_state = to_sde_connector_state(conn_state);
898
Ingrid Gallardo83532222017-06-02 16:48:51 -0700899 /*
900 * current driver only supports same connector and crtc size,
901 * but if support for different sizes is added, driver needs
902 * to check the connector roi here to make sure is full screen
903 * for dsc 3d-mux topology that doesn't support partial update.
904 */
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400905 if (memcmp(&sde_conn_state->rois, &crtc_state->user_roi_list,
906 sizeof(crtc_state->user_roi_list))) {
907 SDE_ERROR("%s: crtc -> conn roi scaling unsupported\n",
908 sde_crtc->name);
909 return -EINVAL;
910 }
911 }
912
Lloyd Atkinsonc2baf412017-04-19 17:53:09 -0400913 sde_kms_rect_merge_rectangles(&crtc_state->user_roi_list, crtc_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400914
Ingrid Gallardo83532222017-06-02 16:48:51 -0700915 /*
916 * for 3dmux dsc, make sure is full ROI, since current driver doesn't
917 * support partial update for this configuration.
918 */
919 if (!sde_kms_rect_is_null(crtc_roi) &&
920 _sde_crtc_setup_is_3dmux_dsc(state)) {
921 struct drm_display_mode *adj_mode = &state->adjusted_mode;
922
923 if (crtc_roi->w != adj_mode->hdisplay ||
924 crtc_roi->h != adj_mode->vdisplay) {
925 SDE_ERROR("%s: unsupported top roi[%d %d] wxh[%d %d]\n",
926 sde_crtc->name, crtc_roi->w, crtc_roi->h,
927 adj_mode->hdisplay, adj_mode->vdisplay);
928 return -EINVAL;
929 }
930 }
931
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400932 SDE_DEBUG("%s: crtc roi (%d,%d,%d,%d)\n", sde_crtc->name,
933 crtc_roi->x, crtc_roi->y, crtc_roi->w, crtc_roi->h);
934
935 return 0;
936}
937
Lloyd Atkinson77382202017-02-01 14:59:43 -0500938static int _sde_crtc_check_autorefresh(struct drm_crtc *crtc,
939 struct drm_crtc_state *state)
940{
941 struct sde_crtc *sde_crtc;
942 struct sde_crtc_state *crtc_state;
943 struct drm_connector *conn;
944 struct drm_connector_state *conn_state;
945 int i;
946
947 if (!crtc || !state)
948 return -EINVAL;
949
950 sde_crtc = to_sde_crtc(crtc);
951 crtc_state = to_sde_crtc_state(state);
952
953 if (sde_kms_rect_is_null(&crtc_state->crtc_roi))
954 return 0;
955
956 /* partial update active, check if autorefresh is also requested */
957 for_each_connector_in_state(state->state, conn, conn_state, i) {
958 uint64_t autorefresh;
959
960 if (!conn_state || conn_state->crtc != crtc)
961 continue;
962
963 autorefresh = sde_connector_get_property(conn_state,
964 CONNECTOR_PROP_AUTOREFRESH);
965 if (autorefresh) {
966 SDE_ERROR(
967 "%s: autorefresh & partial crtc roi incompatible %llu\n",
968 sde_crtc->name, autorefresh);
969 return -EINVAL;
970 }
971 }
972
973 return 0;
974}
975
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400976static int _sde_crtc_set_lm_roi(struct drm_crtc *crtc,
977 struct drm_crtc_state *state, int lm_idx)
978{
979 struct sde_crtc *sde_crtc;
980 struct sde_crtc_state *crtc_state;
981 const struct sde_rect *crtc_roi;
982 const struct sde_rect *lm_bounds;
983 struct sde_rect *lm_roi;
984
985 if (!crtc || !state || lm_idx >= ARRAY_SIZE(crtc_state->lm_bounds))
986 return -EINVAL;
987
988 sde_crtc = to_sde_crtc(crtc);
989 crtc_state = to_sde_crtc_state(state);
990 crtc_roi = &crtc_state->crtc_roi;
991 lm_bounds = &crtc_state->lm_bounds[lm_idx];
992 lm_roi = &crtc_state->lm_roi[lm_idx];
993
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500994 if (sde_kms_rect_is_null(crtc_roi))
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400995 memcpy(lm_roi, lm_bounds, sizeof(*lm_roi));
Lloyd Atkinson73fb8092017-02-08 16:02:55 -0500996 else
997 sde_kms_rect_intersect(crtc_roi, lm_bounds, lm_roi);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -0400998
999 SDE_DEBUG("%s: lm%d roi (%d,%d,%d,%d)\n", sde_crtc->name, lm_idx,
1000 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1001
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001002 /* if any dimension is zero, clear all dimensions for clarity */
1003 if (sde_kms_rect_is_null(lm_roi))
1004 memset(lm_roi, 0, sizeof(*lm_roi));
1005
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001006 return 0;
1007}
1008
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001009static u32 _sde_crtc_get_displays_affected(struct drm_crtc *crtc,
1010 struct drm_crtc_state *state)
1011{
1012 struct sde_crtc *sde_crtc;
1013 struct sde_crtc_state *crtc_state;
1014 u32 disp_bitmask = 0;
1015 int i;
1016
1017 sde_crtc = to_sde_crtc(crtc);
1018 crtc_state = to_sde_crtc_state(state);
1019
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001020 /* pingpong split: one ROI, one LM, two physical displays */
1021 if (crtc_state->is_ppsplit) {
1022 u32 lm_split_width = crtc_state->lm_bounds[0].w / 2;
1023 struct sde_rect *roi = &crtc_state->lm_roi[0];
1024
1025 if (sde_kms_rect_is_null(roi))
1026 disp_bitmask = 0;
1027 else if ((u32)roi->x + (u32)roi->w <= lm_split_width)
1028 disp_bitmask = BIT(0); /* left only */
1029 else if (roi->x >= lm_split_width)
1030 disp_bitmask = BIT(1); /* right only */
1031 else
1032 disp_bitmask = BIT(0) | BIT(1); /* left and right */
1033 } else {
1034 for (i = 0; i < sde_crtc->num_mixers; i++) {
1035 if (!sde_kms_rect_is_null(&crtc_state->lm_roi[i]))
1036 disp_bitmask |= BIT(i);
1037 }
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001038 }
1039
1040 SDE_DEBUG("affected displays 0x%x\n", disp_bitmask);
1041
1042 return disp_bitmask;
1043}
1044
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001045static int _sde_crtc_check_rois_centered_and_symmetric(struct drm_crtc *crtc,
1046 struct drm_crtc_state *state)
1047{
1048 struct sde_crtc *sde_crtc;
1049 struct sde_crtc_state *crtc_state;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001050 const struct sde_rect *roi[CRTC_DUAL_MIXERS];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001051
1052 if (!crtc || !state)
1053 return -EINVAL;
1054
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001055 sde_crtc = to_sde_crtc(crtc);
1056 crtc_state = to_sde_crtc_state(state);
1057
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001058 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1059 SDE_ERROR("%s: unsupported number of mixers: %d\n",
1060 sde_crtc->name, sde_crtc->num_mixers);
1061 return -EINVAL;
1062 }
1063
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001064 /*
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001065 * If using pingpong split: one ROI, one LM, two physical displays
1066 * then the ROI must be centered on the panel split boundary and
1067 * be of equal width across the split.
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001068 */
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001069 if (crtc_state->is_ppsplit) {
1070 u16 panel_split_width;
1071 u32 display_mask;
1072
1073 roi[0] = &crtc_state->lm_roi[0];
1074
1075 if (sde_kms_rect_is_null(roi[0]))
1076 return 0;
1077
1078 display_mask = _sde_crtc_get_displays_affected(crtc, state);
1079 if (display_mask != (BIT(0) | BIT(1)))
1080 return 0;
1081
1082 panel_split_width = crtc_state->lm_bounds[0].w / 2;
1083 if (roi[0]->x + roi[0]->w / 2 != panel_split_width) {
1084 SDE_ERROR("%s: roi x %d w %d split %d\n",
1085 sde_crtc->name, roi[0]->x, roi[0]->w,
1086 panel_split_width);
1087 return -EINVAL;
1088 }
1089
1090 return 0;
1091 }
1092
1093 /*
1094 * On certain HW, if using 2 LM, ROIs must be split evenly between the
1095 * LMs and be of equal width.
1096 */
Clarence Ipffb87422017-06-30 13:37:48 -04001097 if (sde_crtc->num_mixers < 2)
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05001098 return 0;
1099
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001100 roi[0] = &crtc_state->lm_roi[0];
1101 roi[1] = &crtc_state->lm_roi[1];
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001102
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001103 /* if one of the roi is null it's a left/right-only update */
1104 if (sde_kms_rect_is_null(roi[0]) || sde_kms_rect_is_null(roi[1]))
1105 return 0;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001106
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001107 /* check lm rois are equal width & first roi ends at 2nd roi */
1108 if (roi[0]->x + roi[0]->w != roi[1]->x || roi[0]->w != roi[1]->w) {
1109 SDE_ERROR(
1110 "%s: rois not centered and symmetric: roi0 x %d w %d roi1 x %d w %d\n",
1111 sde_crtc->name, roi[0]->x, roi[0]->w,
1112 roi[1]->x, roi[1]->w);
1113 return -EINVAL;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001114 }
1115
1116 return 0;
1117}
1118
1119static int _sde_crtc_check_planes_within_crtc_roi(struct drm_crtc *crtc,
1120 struct drm_crtc_state *state)
1121{
1122 struct sde_crtc *sde_crtc;
1123 struct sde_crtc_state *crtc_state;
1124 const struct sde_rect *crtc_roi;
1125 struct drm_plane_state *pstate;
1126 struct drm_plane *plane;
1127
1128 if (!crtc || !state)
1129 return -EINVAL;
1130
1131 /*
1132 * Reject commit if a Plane CRTC destination coordinates fall outside
1133 * the partial CRTC ROI. LM output is determined via connector ROIs,
1134 * if they are specified, not Plane CRTC ROIs.
1135 */
1136
1137 sde_crtc = to_sde_crtc(crtc);
1138 crtc_state = to_sde_crtc_state(state);
1139 crtc_roi = &crtc_state->crtc_roi;
1140
1141 if (sde_kms_rect_is_null(crtc_roi))
1142 return 0;
1143
1144 drm_atomic_crtc_state_for_each_plane(plane, state) {
1145 struct sde_rect plane_roi, intersection;
1146
1147 pstate = drm_atomic_get_plane_state(state->state, plane);
1148 if (IS_ERR_OR_NULL(pstate)) {
1149 int rc = PTR_ERR(pstate);
1150
1151 SDE_ERROR("%s: failed to get plane%d state, %d\n",
1152 sde_crtc->name, plane->base.id, rc);
1153 return rc;
1154 }
1155
1156 plane_roi.x = pstate->crtc_x;
1157 plane_roi.y = pstate->crtc_y;
1158 plane_roi.w = pstate->crtc_w;
1159 plane_roi.h = pstate->crtc_h;
1160 sde_kms_rect_intersect(crtc_roi, &plane_roi, &intersection);
1161 if (!sde_kms_rect_is_equal(&plane_roi, &intersection)) {
1162 SDE_ERROR(
1163 "%s: plane%d crtc roi (%d,%d,%d,%d) outside crtc roi (%d,%d,%d,%d)\n",
1164 sde_crtc->name, plane->base.id,
1165 plane_roi.x, plane_roi.y,
1166 plane_roi.w, plane_roi.h,
1167 crtc_roi->x, crtc_roi->y,
1168 crtc_roi->w, crtc_roi->h);
1169 return -E2BIG;
1170 }
1171 }
1172
1173 return 0;
1174}
1175
1176static int _sde_crtc_check_rois(struct drm_crtc *crtc,
1177 struct drm_crtc_state *state)
1178{
1179 struct sde_crtc *sde_crtc;
1180 int lm_idx;
1181 int rc;
1182
1183 if (!crtc || !state)
1184 return -EINVAL;
1185
1186 sde_crtc = to_sde_crtc(crtc);
1187
1188 rc = _sde_crtc_set_crtc_roi(crtc, state);
1189 if (rc)
1190 return rc;
1191
Lloyd Atkinson77382202017-02-01 14:59:43 -05001192 rc = _sde_crtc_check_autorefresh(crtc, state);
1193 if (rc)
1194 return rc;
1195
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001196 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1197 rc = _sde_crtc_set_lm_roi(crtc, state, lm_idx);
1198 if (rc)
1199 return rc;
1200 }
1201
1202 rc = _sde_crtc_check_rois_centered_and_symmetric(crtc, state);
1203 if (rc)
1204 return rc;
1205
1206 rc = _sde_crtc_check_planes_within_crtc_roi(crtc, state);
1207 if (rc)
1208 return rc;
1209
1210 return 0;
1211}
1212
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001213static void _sde_crtc_program_lm_output_roi(struct drm_crtc *crtc)
1214{
1215 struct sde_crtc *sde_crtc;
1216 struct sde_crtc_state *crtc_state;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001217 const struct sde_rect *lm_roi;
1218 struct sde_hw_mixer *hw_lm;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001219 int lm_idx, lm_horiz_position;
1220
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001221 if (!crtc)
1222 return;
1223
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001224 sde_crtc = to_sde_crtc(crtc);
1225 crtc_state = to_sde_crtc_state(crtc->state);
1226
1227 lm_horiz_position = 0;
1228 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001229 struct sde_hw_mixer_cfg cfg;
1230
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001231 lm_roi = &crtc_state->lm_roi[lm_idx];
1232 hw_lm = sde_crtc->mixers[lm_idx].hw_lm;
1233
1234 SDE_EVT32(DRMID(crtc_state->base.crtc), lm_idx,
1235 lm_roi->x, lm_roi->y, lm_roi->w, lm_roi->h);
1236
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001237 if (sde_kms_rect_is_null(lm_roi))
1238 continue;
1239
Ping Lif41c2ef2017-05-04 14:40:45 -07001240 hw_lm->cfg.out_width = lm_roi->w;
1241 hw_lm->cfg.out_height = lm_roi->h;
1242 hw_lm->cfg.right_mixer = lm_horiz_position;
1243
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001244 cfg.out_width = lm_roi->w;
1245 cfg.out_height = lm_roi->h;
1246 cfg.right_mixer = lm_horiz_position++;
1247 cfg.flags = 0;
1248 hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
1249 }
1250}
1251
Dhaval Patel48c76022016-09-01 17:51:23 -07001252static void _sde_crtc_blend_setup_mixer(struct drm_crtc *crtc,
1253 struct sde_crtc *sde_crtc, struct sde_crtc_mixer *mixer)
1254{
1255 struct drm_plane *plane;
Dhaval Patel6c666622017-03-21 23:02:59 -07001256 struct drm_framebuffer *fb;
1257 struct drm_plane_state *state;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001258 struct sde_crtc_state *cstate;
Dhaval Patel48c76022016-09-01 17:51:23 -07001259 struct sde_plane_state *pstate = NULL;
1260 struct sde_format *format;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001261 struct sde_hw_ctl *ctl;
1262 struct sde_hw_mixer *lm;
1263 struct sde_hw_stage_cfg *stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001264 struct sde_rect plane_crtc_roi;
Dhaval Patel48c76022016-09-01 17:51:23 -07001265
Clarence Ip7e5f0002017-05-29 18:46:56 -04001266 u32 flush_mask, flush_sbuf, flush_tmp;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001267 uint32_t stage_idx, lm_idx;
1268 int zpos_cnt[SDE_STAGE_MAX + 1] = { 0 };
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001269 int i;
Dhaval Patel572cfd22017-06-12 19:33:39 -07001270 bool bg_alpha_enable = false;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001271 u32 prefill = 0;
Dhaval Patel48c76022016-09-01 17:51:23 -07001272
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001273 if (!sde_crtc || !mixer) {
1274 SDE_ERROR("invalid sde_crtc or mixer\n");
1275 return;
1276 }
1277
1278 ctl = mixer->hw_ctl;
1279 lm = mixer->hw_lm;
1280 stage_cfg = &sde_crtc->stage_cfg;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001281 cstate = to_sde_crtc_state(crtc->state);
Clarence Ip7e5f0002017-05-29 18:46:56 -04001282 flush_sbuf = 0x0;
Dhaval Patel44f12472016-08-29 12:19:47 -07001283
Clarence Ip7eb90452017-05-23 11:41:19 -04001284 cstate->sbuf_prefill_line = 0;
Clarence Ip95f530b2017-09-06 17:31:41 -04001285 cstate->is_sbuf = false;
Clarence Ip7eb90452017-05-23 11:41:19 -04001286
Dhaval Patel44f12472016-08-29 12:19:47 -07001287 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel6c666622017-03-21 23:02:59 -07001288 state = plane->state;
1289 if (!state)
1290 continue;
Dhaval Patel48c76022016-09-01 17:51:23 -07001291
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001292 plane_crtc_roi.x = state->crtc_x;
1293 plane_crtc_roi.y = state->crtc_y;
1294 plane_crtc_roi.w = state->crtc_w;
1295 plane_crtc_roi.h = state->crtc_h;
1296
Dhaval Patel6c666622017-03-21 23:02:59 -07001297 pstate = to_sde_plane_state(state);
1298 fb = state->fb;
Dhaval Patel44f12472016-08-29 12:19:47 -07001299
Alan Kwong4dd64c82017-02-04 18:41:51 -08001300 if (sde_plane_is_sbuf_mode(plane, &prefill))
Clarence Ip95f530b2017-09-06 17:31:41 -04001301 cstate->is_sbuf = true;
Clarence Ip7eb90452017-05-23 11:41:19 -04001302 if (prefill > cstate->sbuf_prefill_line)
1303 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001304
Clarence Ip7e5f0002017-05-29 18:46:56 -04001305 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001306
Clarence Ip7e5f0002017-05-29 18:46:56 -04001307 /* persist rotator flush bit(s) for one more commit */
1308 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1309 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001310
1311 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001312 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001313 pstate->stage,
1314 plane->base.id,
1315 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001316 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001317
Dhaval Patel48c76022016-09-01 17:51:23 -07001318 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07001319 if (!format) {
1320 SDE_ERROR("invalid format\n");
1321 return;
1322 }
1323
Dhaval Patel572cfd22017-06-12 19:33:39 -07001324 if (pstate->stage == SDE_STAGE_BASE && format->alpha_enable)
1325 bg_alpha_enable = true;
Dhaval Patel44f12472016-08-29 12:19:47 -07001326
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001327 SDE_EVT32(DRMID(crtc), DRMID(plane),
1328 state->fb ? state->fb->base.id : -1,
1329 state->src_x >> 16, state->src_y >> 16,
1330 state->src_w >> 16, state->src_h >> 16,
1331 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001332 state->crtc_w, state->crtc_h,
Clarence Ip95f530b2017-09-06 17:31:41 -04001333 flush_tmp ? cstate->is_sbuf : 0);
Dhaval Patel6c666622017-03-21 23:02:59 -07001334
Dhaval Patel572cfd22017-06-12 19:33:39 -07001335 stage_idx = zpos_cnt[pstate->stage]++;
1336 stage_cfg->stage[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001337 sde_plane_pipe(plane);
Dhaval Patel572cfd22017-06-12 19:33:39 -07001338 stage_cfg->multirect_index[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001339 pstate->multirect_index;
1340
Dhaval Patel572cfd22017-06-12 19:33:39 -07001341 SDE_EVT32(DRMID(crtc), DRMID(plane), stage_idx,
1342 sde_plane_pipe(plane) - SSPP_VIG0, pstate->stage,
1343 pstate->multirect_index, pstate->multirect_mode,
1344 format->base.pixel_format, fb ? fb->modifier[0] : 0);
1345
1346 /* blend config update */
1347 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1348 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
1349 format);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001350 mixer[lm_idx].flush_mask |= flush_mask;
1351
Dhaval Patel572cfd22017-06-12 19:33:39 -07001352 if (bg_alpha_enable && !format->alpha_enable)
1353 mixer[lm_idx].mixer_op_mode = 0;
1354 else
1355 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001356 1 << pstate->stage;
Dhaval Patel48c76022016-09-01 17:51:23 -07001357 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001358 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001359
Clarence Ip7e5f0002017-05-29 18:46:56 -04001360 cstate->sbuf_flush_mask = flush_sbuf;
1361
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001362 if (lm && lm->ops.setup_dim_layer) {
1363 cstate = to_sde_crtc_state(crtc->state);
1364 for (i = 0; i < cstate->num_dim_layers; i++)
1365 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1366 mixer, &cstate->dim_layer[i]);
1367 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001368
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001369 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001370}
1371
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001372static void _sde_crtc_swap_mixers_for_right_partial_update(
1373 struct drm_crtc *crtc)
1374{
1375 struct sde_crtc *sde_crtc;
1376 struct sde_crtc_state *cstate;
1377 struct drm_encoder *drm_enc;
1378 bool is_right_only;
1379 bool encoder_in_dsc_merge = false;
1380
1381 if (!crtc || !crtc->state)
1382 return;
1383
1384 sde_crtc = to_sde_crtc(crtc);
1385 cstate = to_sde_crtc_state(crtc->state);
1386
1387 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1388 return;
1389
1390 drm_for_each_encoder(drm_enc, crtc->dev) {
1391 if (drm_enc->crtc == crtc &&
1392 sde_encoder_is_dsc_merge(drm_enc)) {
1393 encoder_in_dsc_merge = true;
1394 break;
1395 }
1396 }
1397
1398 /**
1399 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1400 * This is due to two reasons:
1401 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1402 * the left DSC must be used, right DSC cannot be used alone.
1403 * For right-only partial update, this means swap layer mixers to map
1404 * Left LM to Right INTF. On later HW this was relaxed.
1405 * - In DSC Merge mode, the physical encoder has already registered
1406 * PP0 as the master, to switch to right-only we would have to
1407 * reprogram to be driven by PP1 instead.
1408 * To support both cases, we prefer to support the mixer swap solution.
1409 */
1410 if (!encoder_in_dsc_merge)
1411 return;
1412
1413 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1414 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1415
1416 if (is_right_only && !sde_crtc->mixers_swapped) {
1417 /* right-only update swap mixers */
1418 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1419 sde_crtc->mixers_swapped = true;
1420 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1421 /* left-only or full update, swap back */
1422 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1423 sde_crtc->mixers_swapped = false;
1424 }
1425
1426 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1427 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1428 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1429 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1430 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1431 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1432 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1433}
1434
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001435/**
1436 * _sde_crtc_blend_setup - configure crtc mixers
1437 * @crtc: Pointer to drm crtc structure
1438 */
1439static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001440{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001441 struct sde_crtc *sde_crtc;
1442 struct sde_crtc_state *sde_crtc_state;
1443 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001444 struct sde_hw_ctl *ctl;
1445 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001446
1447 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001448
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001449 if (!crtc)
1450 return;
1451
1452 sde_crtc = to_sde_crtc(crtc);
1453 sde_crtc_state = to_sde_crtc_state(crtc->state);
1454 mixer = sde_crtc->mixers;
1455
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001456 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001457
Dhaval Patel48c76022016-09-01 17:51:23 -07001458 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1459 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1460 return;
1461 }
1462
1463 for (i = 0; i < sde_crtc->num_mixers; i++) {
1464 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1465 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1466 return;
1467 }
1468 mixer[i].mixer_op_mode = 0;
1469 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001470 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1471 mixer[i].hw_ctl->ops.clear_all_blendstages(
1472 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001473
1474 /* clear dim_layer settings */
1475 lm = mixer[i].hw_lm;
1476 if (lm->ops.clear_dim_layer)
1477 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001478 }
1479
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001480 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1481
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001482 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001483 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001484
Dhaval Patel48c76022016-09-01 17:51:23 -07001485 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1486
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001487 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001488 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1489
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001490 ctl = mixer[i].hw_ctl;
1491 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001492
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001493 if (sde_kms_rect_is_null(lm_roi)) {
1494 SDE_DEBUG(
1495 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1496 sde_crtc->name, lm->idx - LM_0,
1497 ctl->idx - CTL_0);
1498 continue;
1499 }
1500
Dhaval Patel48c76022016-09-01 17:51:23 -07001501 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001502
Dhaval Patel48c76022016-09-01 17:51:23 -07001503 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001504 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001505
1506 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001507 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1508
Clarence Ip8e69ad02016-12-09 09:43:57 -05001509 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1510 mixer[i].hw_lm->idx - LM_0,
1511 mixer[i].mixer_op_mode,
1512 ctl->idx - CTL_0,
1513 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001514
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001515 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel572cfd22017-06-12 19:33:39 -07001516 &sde_crtc->stage_cfg);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001517 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001518
1519 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001520}
1521
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001522static int _sde_crtc_find_plane_fb_modes(struct drm_crtc_state *state,
1523 uint32_t *fb_ns,
1524 uint32_t *fb_sec,
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001525 uint32_t *fb_sec_dir)
1526{
1527 struct drm_plane *plane;
1528 const struct drm_plane_state *pstate;
1529 struct sde_plane_state *sde_pstate;
1530 uint32_t mode = 0;
1531 int rc;
1532
1533 if (!state) {
1534 SDE_ERROR("invalid state\n");
1535 return -EINVAL;
1536 }
1537
1538 *fb_ns = 0;
1539 *fb_sec = 0;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001540 *fb_sec_dir = 0;
1541 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
1542 if (IS_ERR_OR_NULL(pstate)) {
1543 rc = PTR_ERR(pstate);
1544 SDE_ERROR("crtc%d failed to get plane%d state%d\n",
1545 state->crtc->base.id,
1546 plane->base.id, rc);
1547 return rc;
1548 }
1549 sde_pstate = to_sde_plane_state(pstate);
1550 mode = sde_plane_get_property(sde_pstate,
1551 PLANE_PROP_FB_TRANSLATION_MODE);
1552 switch (mode) {
1553 case SDE_DRM_FB_NON_SEC:
1554 (*fb_ns)++;
1555 break;
1556 case SDE_DRM_FB_SEC:
1557 (*fb_sec)++;
1558 break;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001559 case SDE_DRM_FB_SEC_DIR_TRANS:
1560 (*fb_sec_dir)++;
1561 break;
1562 default:
1563 SDE_ERROR("Error: Plane[%d], fb_trans_mode:%d",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001564 plane->base.id, mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001565 return -EINVAL;
1566 }
1567 }
1568 return 0;
1569}
1570
1571/**
1572 * sde_crtc_get_secure_transition_ops - determines the operations that
1573 * need to be performed before transitioning to secure state
1574 * This function should be called after swapping the new state
1575 * @crtc: Pointer to drm crtc structure
1576 * Returns the bitmask of operations need to be performed, -Error in
1577 * case of error cases
1578 */
1579int sde_crtc_get_secure_transition_ops(struct drm_crtc *crtc,
1580 struct drm_crtc_state *old_crtc_state,
1581 bool old_valid_fb)
1582{
1583 struct drm_plane *plane;
1584 struct drm_encoder *encoder;
1585 struct sde_crtc *sde_crtc;
1586 struct sde_crtc_state *cstate;
1587 struct sde_crtc_smmu_state_data *smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001588 uint32_t translation_mode = 0, secure_level;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001589 int ops = 0;
1590 bool post_commit = false;
1591
1592 if (!crtc || !crtc->state) {
1593 SDE_ERROR("invalid crtc\n");
1594 return -EINVAL;
1595 }
1596
1597 sde_crtc = to_sde_crtc(crtc);
1598 cstate = to_sde_crtc_state(crtc->state);
1599 smmu_state = &sde_crtc->smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001600 secure_level = sde_crtc_get_secure_level(crtc, crtc->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001601
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001602 SDE_DEBUG("crtc%d, secure_level%d old_valid_fb%d\n",
1603 crtc->base.id, secure_level, old_valid_fb);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001604
1605 /**
1606 * SMMU operations need to be delayed in case of
1607 * video mode panels when switching back to non_secure
1608 * mode
1609 */
1610 drm_for_each_encoder(encoder, crtc->dev) {
1611 if (encoder->crtc != crtc)
1612 continue;
1613
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05301614 post_commit &= sde_encoder_check_mode(encoder,
1615 MSM_DISPLAY_CAP_VID_MODE);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001616 }
1617
1618 drm_atomic_crtc_for_each_plane(plane, crtc) {
1619 if (!plane->state)
1620 continue;
1621
1622 translation_mode = sde_plane_get_property(
1623 to_sde_plane_state(plane->state),
1624 PLANE_PROP_FB_TRANSLATION_MODE);
1625 if (translation_mode > SDE_DRM_FB_SEC_DIR_TRANS) {
1626 SDE_ERROR("crtc%d, invalid translation_mode%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001627 crtc->base.id, translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001628 return -EINVAL;
1629 }
1630
1631 /**
1632 * we can break if we find sec_fir or non_sec_dir
1633 * plane
1634 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001635 if (translation_mode == SDE_DRM_FB_SEC_DIR_TRANS)
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001636 break;
1637 }
1638
1639 switch (translation_mode) {
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001640 case SDE_DRM_FB_SEC_DIR_TRANS:
1641 /* secure display usecase */
1642 if ((smmu_state->state == ATTACHED) &&
1643 (secure_level == SDE_DRM_SEC_ONLY)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001644 smmu_state->state = DETACH_ALL_REQ;
1645 smmu_state->transition_type = PRE_COMMIT;
1646 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1647 if (old_valid_fb) {
1648 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1649 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1650 }
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001651 /* secure camera usecase */
1652 } else if (smmu_state->state == ATTACHED) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001653 smmu_state->state = DETACH_SEC_REQ;
1654 smmu_state->transition_type = PRE_COMMIT;
1655 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1656 }
1657 break;
1658 case SDE_DRM_FB_SEC:
1659 case SDE_DRM_FB_NON_SEC:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001660 if ((smmu_state->state == DETACHED_SEC) ||
1661 (smmu_state->state == DETACH_SEC_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001662 smmu_state->state = ATTACH_SEC_REQ;
1663 smmu_state->transition_type = post_commit ?
1664 POST_COMMIT : PRE_COMMIT;
1665 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1666 if (translation_mode == SDE_DRM_FB_SEC)
1667 ops |= SDE_KMS_OPS_PREPARE_PLANE_FB;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001668 if (old_valid_fb)
1669 ops |= SDE_KMS_OPS_WAIT_FOR_TX_DONE;
1670 } else if ((smmu_state->state == DETACHED) ||
1671 (smmu_state->state == DETACH_ALL_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001672 smmu_state->state = ATTACH_ALL_REQ;
1673 smmu_state->transition_type = post_commit ?
1674 POST_COMMIT : PRE_COMMIT;
1675 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE |
1676 SDE_KMS_OPS_PREPARE_PLANE_FB;
1677 if (old_valid_fb)
1678 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1679 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1680 }
1681 break;
1682 default:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001683 SDE_ERROR("invalid plane fb_mode:%d\n", translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001684 ops = 0;
1685 return -EINVAL;
1686 }
1687
1688 SDE_DEBUG("SMMU State:%d, type:%d ops:%x\n", smmu_state->state,
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001689 smmu_state->transition_type, ops);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001690 return ops;
1691}
1692
1693/**
1694 * _sde_crtc_scm_call - makes secure channel call to switch the VMIDs
1695 * @vimd: switch the stage 2 translation to this VMID.
1696 */
1697static int _sde_crtc_scm_call(int vmid)
1698{
1699 struct scm_desc desc = {0};
1700 uint32_t num_sids;
1701 uint32_t *sec_sid;
1702 uint32_t mem_protect_sd_ctrl_id = MEM_PROTECT_SD_CTRL_SWITCH;
1703 int ret = 0;
1704
1705 /* This info should be queried from catalog */
1706 num_sids = SEC_SID_CNT;
1707 sec_sid = kcalloc(num_sids, sizeof(uint32_t), GFP_KERNEL);
1708 if (!sec_sid)
1709 return -ENOMEM;
1710
1711 /**
1712 * derive this info from device tree/catalog, this is combination of
1713 * smr mask and SID for secure
1714 */
1715 sec_sid[0] = SEC_SID_MASK_0;
1716 sec_sid[1] = SEC_SID_MASK_1;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001717 dmac_flush_range(sec_sid, sec_sid + num_sids);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001718
1719 SDE_DEBUG("calling scm_call for vmid %d", vmid);
1720
1721 desc.arginfo = SCM_ARGS(4, SCM_VAL, SCM_RW, SCM_VAL, SCM_VAL);
1722 desc.args[0] = MDP_DEVICE_ID;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001723 desc.args[1] = SCM_BUFFER_PHYS(sec_sid);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001724 desc.args[2] = sizeof(uint32_t) * num_sids;
1725 desc.args[3] = vmid;
1726
1727 ret = scm_call2(SCM_SIP_FNID(SCM_SVC_MP,
1728 mem_protect_sd_ctrl_id), &desc);
1729 if (ret) {
1730 SDE_ERROR("Error:scm_call2, vmid (%lld): ret%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001731 desc.args[3], ret);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001732 }
1733
1734 kfree(sec_sid);
1735 return ret;
1736}
1737
1738/**
1739 * sde_crtc_secure_ctrl - Initiates the operations to swtich between secure
1740 * and non-secure mode
1741 * @crtc: Pointer to crtc
1742 * @post_commit: if this operation is triggered after commit
1743 */
1744int sde_crtc_secure_ctrl(struct drm_crtc *crtc, bool post_commit)
1745{
1746 struct sde_crtc *sde_crtc;
1747 struct sde_crtc_state *cstate;
1748 struct sde_kms *sde_kms;
1749 struct sde_crtc_smmu_state_data *smmu_state;
1750 int ret = 0;
1751 int old_smmu_state;
1752
1753 if (!crtc || !crtc->state) {
1754 SDE_ERROR("invalid crtc\n");
1755 return -EINVAL;
1756 }
1757
1758 sde_kms = _sde_crtc_get_kms(crtc);
1759 if (!sde_kms) {
1760 SDE_ERROR("invalid kms\n");
1761 return -EINVAL;
1762 }
1763
1764 sde_crtc = to_sde_crtc(crtc);
1765 cstate = to_sde_crtc_state(crtc->state);
1766 smmu_state = &sde_crtc->smmu_state;
1767 old_smmu_state = smmu_state->state;
1768
1769 if ((!smmu_state->transition_type) ||
1770 ((smmu_state->transition_type == POST_COMMIT) && !post_commit))
1771 /* Bail out */
1772 return 0;
1773
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001774 /* Secure UI use case enable */
1775 switch (smmu_state->state) {
1776 case DETACH_ALL_REQ:
1777 /* detach_all_contexts */
1778 ret = sde_kms_mmu_detach(sde_kms, false);
1779 if (ret) {
1780 SDE_ERROR("crtc: %d, failed to detach %d\n",
1781 crtc->base.id, ret);
1782 goto error;
1783 }
1784
1785 ret = _sde_crtc_scm_call(VMID_CP_SEC_DISPLAY);
1786 if (ret)
1787 goto error;
1788
1789 smmu_state->state = DETACHED;
1790 break;
1791 /* Secure UI use case disable */
1792 case ATTACH_ALL_REQ:
1793 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1794 if (ret)
1795 goto error;
1796
1797 /* attach_all_contexts */
1798 ret = sde_kms_mmu_attach(sde_kms, false);
1799 if (ret) {
1800 SDE_ERROR("crtc: %d, failed to attach %d\n",
1801 crtc->base.id,
1802 ret);
1803 goto error;
1804 }
1805
1806 smmu_state->state = ATTACHED;
1807
1808 break;
1809 /* Secure preview enable */
1810 case DETACH_SEC_REQ:
1811 /* detach secure_context */
1812 ret = sde_kms_mmu_detach(sde_kms, true);
1813 if (ret) {
1814 SDE_ERROR("crtc: %d, failed to detach %d\n",
1815 crtc->base.id,
1816 ret);
1817 goto error;
1818 }
1819
1820 smmu_state->state = DETACHED_SEC;
1821 ret = _sde_crtc_scm_call(VMID_CP_CAMERA_PREVIEW);
1822 if (ret)
1823 goto error;
1824
1825 break;
1826
1827 /* Secure preview disable */
1828 case ATTACH_SEC_REQ:
1829 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1830 if (ret)
1831 goto error;
1832
1833 ret = sde_kms_mmu_attach(sde_kms, true);
1834 if (ret) {
1835 SDE_ERROR("crtc: %d, failed to attach %d\n",
1836 crtc->base.id,
1837 ret);
1838 goto error;
1839 }
1840 smmu_state->state = ATTACHED;
1841 break;
1842 default:
1843 break;
1844 }
1845
1846 SDE_DEBUG("crtc: %d, old_state %d new_state %d\n", crtc->base.id,
1847 old_smmu_state,
1848 smmu_state->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001849 smmu_state->transition_type = NONE;
1850
1851error:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001852 smmu_state->transition_error = ret ? true : false;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001853 return ret;
1854}
1855
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001856void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1857 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001858{
1859 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001860 struct sde_crtc_state *cstate;
1861 struct drm_connector *conn;
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001862 struct sde_crtc_retire_event *retire_event = NULL;
1863 unsigned long flags;
1864 int i;
Clarence Ip24f80662016-06-13 19:05:32 -04001865
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001866 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001867 SDE_ERROR("invalid crtc\n");
1868 return;
1869 }
1870
1871 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001872 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001873 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001874
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001875 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001876 cstate->num_connectors = 0;
1877
1878 drm_for_each_connector(conn, crtc->dev)
1879 if (conn->state && conn->state->crtc == crtc &&
1880 cstate->num_connectors < MAX_CONNECTORS) {
1881 cstate->connectors[cstate->num_connectors++] = conn;
1882 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001883 }
1884
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001885 for (i = 0; i < SDE_CRTC_FRAME_EVENT_SIZE; i++) {
1886 retire_event = &sde_crtc->retire_events[i];
1887 if (list_empty(&retire_event->list))
1888 break;
1889 retire_event = NULL;
1890 }
1891
1892 if (retire_event) {
1893 retire_event->num_connectors = cstate->num_connectors;
1894 for (i = 0; i < cstate->num_connectors; i++)
1895 retire_event->connectors[i] = cstate->connectors[i];
1896
1897 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1898 list_add_tail(&retire_event->list,
1899 &sde_crtc->retire_event_list);
1900 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1901 } else {
1902 SDE_ERROR("crtc%d retire event overflow\n", crtc->base.id);
1903 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_ERROR);
1904 }
1905
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001906 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001907 sde_fence_prepare(&sde_crtc->output_fence);
1908}
1909
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001910/**
1911 * _sde_crtc_complete_flip - signal pending page_flip events
1912 * Any pending vblank events are added to the vblank_event_list
1913 * so that the next vblank interrupt shall signal them.
1914 * However PAGE_FLIP events are not handled through the vblank_event_list.
1915 * This API signals any pending PAGE_FLIP events requested through
1916 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1917 * if file!=NULL, this is preclose potential cancel-flip path
1918 * @crtc: Pointer to drm crtc structure
1919 * @file: Pointer to drm file
1920 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001921static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1922 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001923{
1924 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1925 struct drm_device *dev = crtc->dev;
1926 struct drm_pending_vblank_event *event;
1927 unsigned long flags;
1928
1929 spin_lock_irqsave(&dev->event_lock, flags);
1930 event = sde_crtc->event;
1931 if (event) {
1932 /* if regular vblank case (!file) or if cancel-flip from
1933 * preclose on file that requested flip, then send the
1934 * event:
1935 */
1936 if (!file || (event->base.file_priv == file)) {
1937 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001938 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001939 sde_crtc->name, event);
Dhaval Patela5f75952017-07-25 11:17:41 -07001940 SDE_EVT32_VERBOSE(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001941 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001942 }
1943 }
1944 spin_unlock_irqrestore(&dev->event_lock, flags);
1945}
1946
Alan Kwong3e985f02017-02-12 15:08:44 -08001947enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1948{
1949 struct drm_encoder *encoder;
1950
1951 if (!crtc || !crtc->dev) {
1952 SDE_ERROR("invalid crtc\n");
1953 return INTF_MODE_NONE;
1954 }
1955
1956 drm_for_each_encoder(encoder, crtc->dev)
1957 if (encoder->crtc == crtc)
1958 return sde_encoder_get_intf_mode(encoder);
1959
1960 return INTF_MODE_NONE;
1961}
1962
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001963static void sde_crtc_vblank_cb(void *data)
1964{
1965 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001966 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1967
1968 /* keep statistics on vblank callback - with auto reset via debugfs */
1969 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1970 sde_crtc->vblank_cb_time = ktime_get();
1971 else
1972 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001973 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001974 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001975 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001976 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001977}
1978
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001979static void _sde_crtc_retire_event(struct drm_crtc *crtc, ktime_t ts)
1980{
1981 struct sde_crtc_retire_event *retire_event;
1982 struct sde_crtc *sde_crtc;
1983 unsigned long flags;
1984 int i;
1985
1986 if (!crtc) {
1987 SDE_ERROR("invalid param\n");
1988 return;
1989 }
1990
1991 sde_crtc = to_sde_crtc(crtc);
1992 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1993 retire_event = list_first_entry_or_null(&sde_crtc->retire_event_list,
1994 struct sde_crtc_retire_event, list);
1995 if (retire_event)
1996 list_del_init(&retire_event->list);
1997 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1998
1999 if (!retire_event) {
2000 SDE_ERROR("crtc%d retire event without kickoff\n",
2001 crtc->base.id);
2002 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_ERROR);
2003 return;
2004 }
2005
2006 SDE_ATRACE_BEGIN("signal_retire_fence");
2007 for (i = 0; (i < retire_event->num_connectors) &&
2008 retire_event->connectors[i]; ++i)
2009 sde_connector_complete_commit(
2010 retire_event->connectors[i], ts);
2011 SDE_ATRACE_END("signal_retire_fence");
2012}
2013
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002014/* _sde_crtc_idle_notify - signal idle timeout to client */
2015static void _sde_crtc_idle_notify(struct sde_crtc *sde_crtc)
2016{
2017 struct drm_crtc *crtc;
2018 struct drm_event event;
2019 int ret = 0;
2020
2021 if (!sde_crtc) {
2022 SDE_ERROR("invalid sde crtc\n");
2023 return;
2024 }
2025
2026 crtc = &sde_crtc->base;
2027 event.type = DRM_EVENT_IDLE_NOTIFY;
2028 event.length = sizeof(u32);
2029 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2030 (u8 *)&ret);
2031
2032 SDE_DEBUG("crtc:%d idle timeout notified\n", crtc->base.id);
2033}
2034
2035/*
2036 * sde_crtc_handle_event - crtc frame event handle.
2037 * This API must manage only non-IRQ context events.
2038 */
2039static bool _sde_crtc_handle_event(struct sde_crtc *sde_crtc, u32 event)
2040{
2041 bool event_processed = false;
2042
2043 /**
2044 * idle events are originated from commit thread and can be processed
2045 * in same context
2046 */
2047 if (event & SDE_ENCODER_FRAME_EVENT_IDLE) {
2048 _sde_crtc_idle_notify(sde_crtc);
2049 event_processed = true;
2050 }
2051
2052 return event_processed;
2053}
2054
Alan Kwong628d19e2016-10-31 13:50:13 -04002055static void sde_crtc_frame_event_work(struct kthread_work *work)
2056{
Alan Kwong67a3f792016-11-01 23:16:53 -04002057 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04002058 struct sde_crtc_frame_event *fevent;
2059 struct drm_crtc *crtc;
2060 struct sde_crtc *sde_crtc;
2061 struct sde_kms *sde_kms;
2062 unsigned long flags;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002063 bool frame_done = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002064
2065 if (!work) {
2066 SDE_ERROR("invalid work handle\n");
2067 return;
2068 }
2069
2070 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07002071 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002072 SDE_ERROR("invalid crtc\n");
2073 return;
2074 }
2075
2076 crtc = fevent->crtc;
2077 sde_crtc = to_sde_crtc(crtc);
2078
2079 sde_kms = _sde_crtc_get_kms(crtc);
2080 if (!sde_kms) {
2081 SDE_ERROR("invalid kms handle\n");
2082 return;
2083 }
Alan Kwong67a3f792016-11-01 23:16:53 -04002084 priv = sde_kms->dev->dev_private;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002085 SDE_ATRACE_BEGIN("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04002086
2087 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
2088 ktime_to_ns(fevent->ts));
2089
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002090 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_ENTRY);
2091
2092 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
2093 | SDE_ENCODER_FRAME_EVENT_ERROR
2094 | SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002095
2096 if (atomic_read(&sde_crtc->frame_pending) < 1) {
2097 /* this should not happen */
2098 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
2099 crtc->base.id,
2100 ktime_to_ns(fevent->ts),
2101 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07002102 SDE_EVT32(DRMID(crtc), fevent->event,
2103 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002104 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
2105 /* release bandwidth and other resources */
2106 SDE_DEBUG("crtc%d ts:%lld last pending\n",
2107 crtc->base.id,
2108 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07002109 SDE_EVT32(DRMID(crtc), fevent->event,
2110 SDE_EVTLOG_FUNC_CASE2);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002111 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002112 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07002113 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
2114 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04002115 }
Alan Kwonga1939682017-05-05 11:30:08 -07002116
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002117 if (fevent->event & SDE_ENCODER_FRAME_EVENT_DONE)
Alan Kwonga1939682017-05-05 11:30:08 -07002118 sde_core_perf_crtc_update(crtc, 0, false);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002119
2120 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
2121 | SDE_ENCODER_FRAME_EVENT_ERROR))
2122 frame_done = true;
2123 }
2124
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002125 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE) {
2126 SDE_ATRACE_BEGIN("signal_release_fence");
Dhaval Patelfd8f7742017-08-10 13:11:22 -07002127 sde_fence_signal(&sde_crtc->output_fence, fevent->ts, false);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002128 SDE_ATRACE_END("signal_release_fence");
2129 }
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002130
Dhaval Patel5023c3c2017-08-22 12:40:11 -07002131 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE)
2132 /* this api should be called without spin_lock */
2133 _sde_crtc_retire_event(crtc, fevent->ts);
Alan Kwong628d19e2016-10-31 13:50:13 -04002134
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05002135 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
2136 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
2137 crtc->base.id, ktime_to_ns(fevent->ts));
2138
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002139 if (frame_done)
2140 complete_all(&sde_crtc->frame_done_comp);
2141
Alan Kwong628d19e2016-10-31 13:50:13 -04002142 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2143 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
2144 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002145 SDE_ATRACE_END("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04002146}
2147
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002148/*
2149 * sde_crtc_frame_event_cb - crtc frame event callback API. CRTC module
2150 * registers this API to encoder for all frame event callbacks like
2151 * release_fence, retire_fence, frame_error, frame_done, idle_timeout,
2152 * etc. Encoder may call different events from different context - IRQ,
2153 * user thread, commit_thread, etc. Each event should be carefully
2154 * reviewed and should be processed in proper task context to avoid scheduling
2155 * delay or properly manage the irq context's bottom half processing.
2156 */
Alan Kwong628d19e2016-10-31 13:50:13 -04002157static void sde_crtc_frame_event_cb(void *data, u32 event)
2158{
2159 struct drm_crtc *crtc = (struct drm_crtc *)data;
2160 struct sde_crtc *sde_crtc;
2161 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04002162 struct sde_crtc_frame_event *fevent;
2163 unsigned long flags;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002164 u32 crtc_id;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002165 bool event_processed = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002166
2167 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2168 SDE_ERROR("invalid parameters\n");
2169 return;
2170 }
2171 sde_crtc = to_sde_crtc(crtc);
2172 priv = crtc->dev->dev_private;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002173 crtc_id = drm_crtc_index(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002174
2175 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07002176 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04002177
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002178 /* try to process the event in caller context */
2179 event_processed = _sde_crtc_handle_event(sde_crtc, event);
2180 if (event_processed)
2181 return;
2182
Alan Kwong628d19e2016-10-31 13:50:13 -04002183 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002184 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
2185 struct sde_crtc_frame_event, list);
2186 if (fevent)
2187 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04002188 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2189
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002190 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002191 SDE_ERROR("crtc%d event %d overflow\n",
2192 crtc->base.id, event);
2193 SDE_EVT32(DRMID(crtc), event);
2194 return;
2195 }
2196
Alan Kwong628d19e2016-10-31 13:50:13 -04002197 fevent->event = event;
2198 fevent->crtc = crtc;
2199 fevent->ts = ktime_get();
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002200 kthread_queue_work(&priv->event_thread[crtc_id].worker, &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04002201}
2202
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002203void sde_crtc_complete_commit(struct drm_crtc *crtc,
2204 struct drm_crtc_state *old_state)
2205{
2206 struct sde_crtc *sde_crtc;
2207 struct sde_crtc_smmu_state_data *smmu_state;
2208
2209 if (!crtc || !crtc->state) {
2210 SDE_ERROR("invalid crtc\n");
2211 return;
2212 }
2213
2214 sde_crtc = to_sde_crtc(crtc);
2215 SDE_EVT32_VERBOSE(DRMID(crtc));
2216 smmu_state = &sde_crtc->smmu_state;
2217
2218 /* complete secure transitions if any */
2219 if (smmu_state->transition_type == POST_COMMIT)
2220 sde_crtc_secure_ctrl(crtc, true);
2221}
2222
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002223/* _sde_crtc_set_idle_timeout - update idle timeout wait duration */
2224static void _sde_crtc_set_idle_timeout(struct drm_crtc *crtc, u64 val)
2225{
2226 struct drm_encoder *encoder;
2227
2228 if (!crtc) {
2229 SDE_ERROR("invalid crtc\n");
2230 return;
2231 }
2232
2233 drm_for_each_encoder(encoder, crtc->dev) {
2234 if (encoder->crtc != crtc)
2235 continue;
2236
2237 sde_encoder_set_idle_timeout(encoder, (u32) val);
2238 }
2239}
2240
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002241/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002242 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
2243 * @cstate: Pointer to sde crtc state
2244 */
2245static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
2246{
2247 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002248 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002249 return;
2250 }
2251 cstate->input_fence_timeout_ns =
2252 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
2253 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
2254}
2255
2256/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002257 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
2258 * @cstate: Pointer to sde crtc state
2259 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
2260 */
2261static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
2262 void *usr_ptr)
2263{
2264 struct sde_drm_dim_layer_v1 dim_layer_v1;
2265 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002266 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002267 u32 count, i;
2268
2269 if (!cstate) {
2270 SDE_ERROR("invalid cstate\n");
2271 return;
2272 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002273 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002274
2275 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002276 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002277 return;
2278 }
2279
2280 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002281 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002282 return;
2283 }
2284
2285 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002286 if (count > SDE_MAX_DIM_LAYERS) {
2287 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002288 return;
2289 }
2290
2291 /* populate from user space */
2292 cstate->num_dim_layers = count;
2293 for (i = 0; i < count; i++) {
2294 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002295
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002296 dim_layer[i].flags = user_cfg->flags;
2297 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002298
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002299 dim_layer[i].rect.x = user_cfg->rect.x1;
2300 dim_layer[i].rect.y = user_cfg->rect.y1;
2301 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
2302 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
2303
2304 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002305 user_cfg->color_fill.color_0,
2306 user_cfg->color_fill.color_1,
2307 user_cfg->color_fill.color_2,
2308 user_cfg->color_fill.color_3,
2309 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002310
2311 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
2312 i, dim_layer[i].flags, dim_layer[i].stage);
2313 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
2314 dim_layer[i].rect.x, dim_layer[i].rect.y,
2315 dim_layer[i].rect.w, dim_layer[i].rect.h,
2316 dim_layer[i].color_fill.color_0,
2317 dim_layer[i].color_fill.color_1,
2318 dim_layer[i].color_fill.color_2,
2319 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002320 }
2321}
2322
2323/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002324 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
2325 * @crtc: Pointer to CRTC object
2326 */
2327static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
2328{
2329 struct drm_plane *plane = NULL;
2330 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04002331 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08002332 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002333
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002334 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002335
2336 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002337 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002338 return;
2339 }
2340
2341 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04002342 kt_end = ktime_add_ns(ktime_get(),
2343 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002344
2345 /*
2346 * Wait for fences sequentially, as all of them need to be signalled
2347 * before we can proceed.
2348 *
2349 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
2350 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
2351 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08002352 * if its fence has timed out. Call input fence wait multiple times if
2353 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04002354 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07002355 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002356 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08002357 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04002358 kt_wait = ktime_sub(kt_end, ktime_get());
2359 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
2360 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002361 else
2362 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08002363
2364 rc = sde_plane_wait_input_fence(plane, wait_ms);
2365 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002366 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07002367 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002368}
2369
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002370static void _sde_crtc_setup_mixer_for_encoder(
2371 struct drm_crtc *crtc,
2372 struct drm_encoder *enc)
2373{
2374 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002375 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002376 struct sde_rm *rm = &sde_kms->rm;
2377 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002378 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002379 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002380 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002381
2382 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
2383 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002384 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002385
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002386 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002387 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
2388 mixer = &sde_crtc->mixers[i];
2389
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002390 if (!sde_rm_get_hw(rm, &lm_iter))
2391 break;
2392 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
2393
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002394 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
2395 if (!sde_rm_get_hw(rm, &ctl_iter)) {
2396 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002397 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002398 mixer->hw_ctl = last_valid_ctl;
2399 } else {
2400 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
2401 last_valid_ctl = mixer->hw_ctl;
2402 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002403
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002404 /* Shouldn't happen, mixers are always >= ctls */
2405 if (!mixer->hw_ctl) {
2406 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002407 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002408 return;
2409 }
2410
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002411 /* Dspp may be null */
2412 (void) sde_rm_get_hw(rm, &dspp_iter);
2413 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
2414
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002415 mixer->encoder = enc;
2416
2417 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002418 SDE_DEBUG("setup mixer %d: lm %d\n",
2419 i, mixer->hw_lm->idx - LM_0);
2420 SDE_DEBUG("setup mixer %d: ctl %d\n",
2421 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002422 }
2423}
2424
2425static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
2426{
2427 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
2428 struct drm_encoder *enc;
2429
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002430 sde_crtc->num_mixers = 0;
2431 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2432
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002433 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002434 /* Check for mixers on all encoders attached to this crtc */
2435 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
2436 if (enc->crtc != crtc)
2437 continue;
2438
2439 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
2440 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002441
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002442 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002443}
2444
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002445static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
2446{
2447 int i;
2448 struct sde_crtc_state *cstate;
2449
2450 cstate = to_sde_crtc_state(state);
2451
2452 cstate->is_ppsplit = false;
2453 for (i = 0; i < cstate->num_connectors; i++) {
2454 struct drm_connector *conn = cstate->connectors[i];
2455
2456 if (sde_connector_get_topology_name(conn) ==
2457 SDE_RM_TOPOLOGY_PPSPLIT)
2458 cstate->is_ppsplit = true;
2459 }
2460}
2461
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002462static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
2463 struct drm_crtc_state *state)
2464{
2465 struct sde_crtc *sde_crtc;
2466 struct sde_crtc_state *cstate;
2467 struct drm_display_mode *adj_mode;
2468 u32 crtc_split_width;
2469 int i;
2470
2471 if (!crtc || !state) {
2472 SDE_ERROR("invalid args\n");
2473 return;
2474 }
2475
2476 sde_crtc = to_sde_crtc(crtc);
2477 cstate = to_sde_crtc_state(state);
2478
2479 adj_mode = &state->adjusted_mode;
2480 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
2481
2482 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002483 cstate->lm_bounds[i].x = crtc_split_width * i;
2484 cstate->lm_bounds[i].y = 0;
2485 cstate->lm_bounds[i].w = crtc_split_width;
2486 cstate->lm_bounds[i].h = adj_mode->vdisplay;
2487 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
2488 sizeof(cstate->lm_roi[i]));
Dhaval Patela5f75952017-07-25 11:17:41 -07002489 SDE_EVT32_VERBOSE(DRMID(crtc), i,
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002490 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
2491 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
2492 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
2493 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
2494 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002495 }
2496
2497 drm_mode_debug_printmodeline(adj_mode);
2498}
2499
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002500static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04002501 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002502{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002503 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07002504 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002505 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002506 unsigned long flags;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002507 struct sde_crtc_smmu_state_data *smmu_state;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002508
Clarence Ipcae1bb62016-07-07 12:07:13 -04002509 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002510 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002511 return;
2512 }
2513
Alan Kwong163d2612016-11-03 00:56:56 -04002514 if (!crtc->state->enable) {
2515 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
2516 crtc->base.id, crtc->state->enable);
2517 return;
2518 }
2519
2520 SDE_DEBUG("crtc%d\n", crtc->base.id);
2521
Clarence Ipcae1bb62016-07-07 12:07:13 -04002522 sde_crtc = to_sde_crtc(crtc);
2523 dev = crtc->dev;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002524 smmu_state = &sde_crtc->smmu_state;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002525
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002526 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002527 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002528 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002529 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
2530 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002531
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002532 if (sde_crtc->event) {
2533 WARN_ON(sde_crtc->event);
2534 } else {
2535 spin_lock_irqsave(&dev->event_lock, flags);
2536 sde_crtc->event = crtc->state->event;
2537 spin_unlock_irqrestore(&dev->event_lock, flags);
2538 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002539
Dhaval Patel0e558f42017-04-30 00:51:40 -07002540 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2541 if (encoder->crtc != crtc)
2542 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002543
Dhaval Patel0e558f42017-04-30 00:51:40 -07002544 /* encoder will trigger pending mask now */
2545 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002546 }
2547
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002548 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002549 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002550 * it means we are trying to flush a CRTC whose state is disabled:
2551 * nothing else needs to be done.
2552 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002553 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002554 return;
2555
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002556 _sde_crtc_blend_setup(crtc);
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002557
2558 /*
2559 * Since CP properties use AXI buffer to program the
2560 * HW, check if context bank is in attached
2561 * state,
2562 * apply color processing properties only if
2563 * smmu state is attached,
2564 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07002565 if ((smmu_state->state != DETACHED) &&
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002566 (smmu_state->state != DETACH_ALL_REQ))
2567 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002568
2569 /*
2570 * PP_DONE irq is only used by command mode for now.
2571 * It is better to request pending before FLUSH and START trigger
2572 * to make sure no pp_done irq missed.
2573 * This is safe because no pp_done will happen before SW trigger
2574 * in command mode.
2575 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002576}
2577
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002578static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
2579 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002580{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002581 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002582 struct sde_crtc *sde_crtc;
2583 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002584 struct drm_plane *plane;
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302585 struct msm_drm_private *priv;
2586 struct msm_drm_thread *event_thread;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002587 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002588 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002589
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302590 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002591 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002592 return;
2593 }
2594
Alan Kwong163d2612016-11-03 00:56:56 -04002595 if (!crtc->state->enable) {
2596 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
2597 crtc->base.id, crtc->state->enable);
2598 return;
2599 }
2600
2601 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002602
2603 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002604 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002605 dev = crtc->dev;
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302606 priv = dev->dev_private;
2607
2608 if (crtc->index >= ARRAY_SIZE(priv->event_thread)) {
2609 SDE_ERROR("invalid crtc index[%d]\n", crtc->index);
2610 return;
2611 }
2612
2613 event_thread = &priv->event_thread[crtc->index];
Clarence Ipcae1bb62016-07-07 12:07:13 -04002614
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002615 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002616 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002617 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002618 spin_lock_irqsave(&dev->event_lock, flags);
2619 sde_crtc->event = crtc->state->event;
2620 spin_unlock_irqrestore(&dev->event_lock, flags);
2621 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002622
2623 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002624 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002625 * it means we are trying to flush a CRTC whose state is disabled:
2626 * nothing else needs to be done.
2627 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002628 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002629 return;
2630
Alan Kwong346223e2017-06-30 15:29:22 -04002631 /*
2632 * For planes without commit update, drm framework will not add
2633 * those planes to current state since hardware update is not
2634 * required. However, if those planes were power collapsed since
2635 * last commit cycle, driver has to restore the hardware state
2636 * of those planes explicitly here prior to plane flush.
2637 */
2638 drm_atomic_crtc_for_each_plane(plane, crtc)
2639 sde_plane_restore(plane);
2640
Clarence Ipcae1bb62016-07-07 12:07:13 -04002641 /* wait for acquire fences before anything else is done */
2642 _sde_crtc_wait_for_fences(crtc);
2643
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002644 if (!cstate->rsc_update) {
2645 drm_for_each_encoder(encoder, dev) {
2646 if (encoder->crtc != crtc)
2647 continue;
2648
2649 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002650 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002651 }
2652 cstate->rsc_update = true;
2653 }
2654
Alan Kwong9aa061c2016-11-06 21:17:12 -05002655 /* update performance setting before crtc kickoff */
2656 sde_core_perf_crtc_update(crtc, 1, false);
2657
Clarence Ipcae1bb62016-07-07 12:07:13 -04002658 /*
2659 * Final plane updates: Give each plane a chance to complete all
2660 * required writes/flushing before crtc's "flush
2661 * everything" call below.
2662 */
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002663 drm_atomic_crtc_for_each_plane(plane, crtc) {
2664 if (sde_crtc->smmu_state.transition_error)
2665 sde_plane_set_error(plane, true);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002666 sde_plane_flush(plane);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002667 }
Clarence Ipcae1bb62016-07-07 12:07:13 -04002668
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002669 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002670}
2671
Clarence Ip7a753bb2016-07-07 11:47:44 -04002672/**
2673 * sde_crtc_destroy_state - state destroy hook
2674 * @crtc: drm CRTC
2675 * @state: CRTC state object to release
2676 */
2677static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2678 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002679{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002680 struct sde_crtc *sde_crtc;
2681 struct sde_crtc_state *cstate;
2682
2683 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002684 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002685 return;
2686 }
2687
2688 sde_crtc = to_sde_crtc(crtc);
2689 cstate = to_sde_crtc_state(state);
2690
Alan Kwong163d2612016-11-03 00:56:56 -04002691 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002692
Alan Kwongcdb2f282017-03-18 13:42:06 -07002693 _sde_crtc_rp_destroy(&cstate->rp);
2694
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002695 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002696
2697 /* destroy value helper */
2698 msm_property_destroy_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002699 &cstate->property_state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002700}
2701
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002702static int _sde_crtc_wait_for_frame_done(struct drm_crtc *crtc)
2703{
2704 struct sde_crtc *sde_crtc;
2705 int ret, rc = 0;
2706
2707 if (!crtc) {
2708 SDE_ERROR("invalid argument\n");
2709 return -EINVAL;
2710 }
2711 sde_crtc = to_sde_crtc(crtc);
2712
2713 if (!atomic_read(&sde_crtc->frame_pending)) {
2714 SDE_DEBUG("no frames pending\n");
2715 return 0;
2716 }
2717
Dhaval Patela5f75952017-07-25 11:17:41 -07002718 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_ENTRY);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002719 ret = wait_for_completion_timeout(&sde_crtc->frame_done_comp,
2720 msecs_to_jiffies(SDE_FRAME_DONE_TIMEOUT));
2721 if (!ret) {
2722 SDE_ERROR("frame done completion wait timed out, ret:%d\n",
2723 ret);
2724 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FATAL);
2725 rc = -ETIMEDOUT;
2726 }
Dhaval Patela5f75952017-07-25 11:17:41 -07002727 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_EXIT);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002728
2729 return rc;
2730}
2731
Clarence Ip95f530b2017-09-06 17:31:41 -04002732static void _sde_crtc_commit_kickoff_rot(struct drm_crtc *crtc,
2733 struct sde_crtc_state *cstate)
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002734{
Clarence Ipeb39cce2017-07-19 14:12:43 -04002735 struct drm_plane *plane;
Clarence Ip95f530b2017-09-06 17:31:41 -04002736 struct sde_crtc *sde_crtc;
2737 struct sde_hw_ctl *ctl, *master_ctl;
2738 int i;
2739
2740 if (!crtc || !cstate)
2741 return;
2742
2743 sde_crtc = to_sde_crtc(crtc);
2744
2745 SDE_ATRACE_BEGIN("crtc_kickoff_rot");
2746
2747 drm_atomic_crtc_for_each_plane(plane, crtc)
2748 sde_plane_kickoff(plane);
2749
2750 master_ctl = NULL;
2751 for (i = 0; i < sde_crtc->num_mixers; i++) {
2752 ctl = sde_crtc->mixers[i].hw_ctl;
2753 if (!ctl || !ctl->ops.setup_sbuf_cfg)
2754 continue;
2755
2756 if (!master_ctl || master_ctl->idx > ctl->idx)
2757 master_ctl = ctl;
2758
2759 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
2760 }
2761
2762 if (cstate->sbuf_cfg.rot_op_mode == SDE_CTL_ROT_OP_MODE_INLINE_ASYNC &&
2763 master_ctl && master_ctl->ops.trigger_rot_start)
2764 master_ctl->ops.trigger_rot_start(master_ctl);
2765
2766 SDE_ATRACE_END("crtc_kickoff_rot");
2767}
2768
2769void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2770{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002771 struct drm_encoder *encoder;
2772 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002773 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002774 struct msm_drm_private *priv;
2775 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002776 struct sde_crtc_state *cstate;
Clarence Ip95f530b2017-09-06 17:31:41 -04002777 int ret;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002778
2779 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002780 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002781 return;
2782 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002783 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002784 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002785 sde_kms = _sde_crtc_get_kms(crtc);
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07002786
2787 if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
2788 SDE_ERROR("invalid argument\n");
2789 return;
2790 }
2791
Alan Kwong67a3f792016-11-01 23:16:53 -04002792 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002793 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002794
Clarence Ip90b282d2017-05-04 10:00:32 -07002795 /*
2796 * If no mixers has been allocated in sde_crtc_atomic_check(),
2797 * it means we are trying to start a CRTC whose state is disabled:
2798 * nothing else needs to be done.
2799 */
2800 if (unlikely(!sde_crtc->num_mixers))
2801 return;
2802
Narendra Muppalla77b32932017-05-10 13:53:11 -07002803 SDE_ATRACE_BEGIN("crtc_commit");
Clarence Ip95f530b2017-09-06 17:31:41 -04002804
2805 /* default to ASYNC mode for inline rotation */
2806 cstate->sbuf_cfg.rot_op_mode = cstate->is_sbuf ?
2807 SDE_CTL_ROT_OP_MODE_INLINE_ASYNC : SDE_CTL_ROT_OP_MODE_OFFLINE;
2808
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002809 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002810 struct sde_encoder_kickoff_params params = { 0 };
2811
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002812 if (encoder->crtc != crtc)
2813 continue;
2814
2815 /*
2816 * Encoder will flush/start now, unless it has a tx pending.
2817 * If so, it may delay and flush at an irq event (e.g. ppdone)
2818 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002819 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002820 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2821 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002822 sde_encoder_prepare_for_kickoff(encoder, &params);
Clarence Ip95f530b2017-09-06 17:31:41 -04002823
2824 /*
2825 * For inline ASYNC modes, the flush bits are not written
2826 * to hardware atomically, so avoid using it if a video
2827 * mode encoder is active on this CRTC.
2828 */
2829 if (cstate->sbuf_cfg.rot_op_mode ==
2830 SDE_CTL_ROT_OP_MODE_INLINE_ASYNC &&
2831 sde_encoder_get_intf_mode(encoder) ==
2832 INTF_MODE_VIDEO)
2833 cstate->sbuf_cfg.rot_op_mode =
2834 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
Alan Kwong628d19e2016-10-31 13:50:13 -04002835 }
2836
Clarence Ip95f530b2017-09-06 17:31:41 -04002837 /*
2838 * For ASYNC inline modes, kick off the rotator now so that the H/W
2839 * can start as soon as it's ready.
2840 */
2841 if (cstate->sbuf_cfg.rot_op_mode == SDE_CTL_ROT_OP_MODE_INLINE_ASYNC)
2842 _sde_crtc_commit_kickoff_rot(crtc, cstate);
2843
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002844 /* wait for frame_event_done completion */
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002845 SDE_ATRACE_BEGIN("wait_for_frame_done_event");
2846 ret = _sde_crtc_wait_for_frame_done(crtc);
2847 SDE_ATRACE_END("wait_for_frame_done_event");
2848 if (ret) {
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002849 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2850 crtc->base.id,
2851 atomic_read(&sde_crtc->frame_pending));
Narendra Muppalla77b32932017-05-10 13:53:11 -07002852 goto end;
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002853 }
2854
2855 if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002856 /* acquire bandwidth and other resources */
2857 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
Clarence Ip95f530b2017-09-06 17:31:41 -04002858 SDE_EVT32(DRMID(crtc), cstate->sbuf_cfg.rot_op_mode,
2859 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002860 } else {
2861 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
Clarence Ip95f530b2017-09-06 17:31:41 -04002862 SDE_EVT32(DRMID(crtc), cstate->sbuf_cfg.rot_op_mode,
2863 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong628d19e2016-10-31 13:50:13 -04002864 }
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07002865 sde_crtc->play_count++;
Alan Kwong628d19e2016-10-31 13:50:13 -04002866
Clarence Ip95f530b2017-09-06 17:31:41 -04002867 /*
2868 * For SYNC inline modes, delay the kick off until after the
2869 * wait for frame done in case the wait times out.
2870 */
2871 if (cstate->sbuf_cfg.rot_op_mode == SDE_CTL_ROT_OP_MODE_INLINE_SYNC)
2872 _sde_crtc_commit_kickoff_rot(crtc, cstate);
Clarence Ipf6b530a2017-08-21 19:39:18 -04002873
Clarence Ip980405d2017-08-08 18:33:44 -04002874 sde_vbif_clear_errors(sde_kms);
2875
Alan Kwong628d19e2016-10-31 13:50:13 -04002876 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2877 if (encoder->crtc != crtc)
2878 continue;
2879
2880 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002881 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002882
Narendra Muppalla77b32932017-05-10 13:53:11 -07002883end:
Dhaval Patelb9850c02017-08-07 22:55:47 -07002884 reinit_completion(&sde_crtc->frame_done_comp);
Narendra Muppalla77b32932017-05-10 13:53:11 -07002885 SDE_ATRACE_END("crtc_commit");
2886 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002887}
2888
Clarence Ip7a753bb2016-07-07 11:47:44 -04002889/**
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002890 * _sde_crtc_vblank_enable_no_lock - update power resource and vblank request
Clarence Ip7f70ce42017-03-20 06:53:46 -07002891 * @sde_crtc: Pointer to sde crtc structure
2892 * @enable: Whether to enable/disable vblanks
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002893 *
2894 * @Return: error code
Clarence Ip7f70ce42017-03-20 06:53:46 -07002895 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002896static int _sde_crtc_vblank_enable_no_lock(
Clarence Ip7f70ce42017-03-20 06:53:46 -07002897 struct sde_crtc *sde_crtc, bool enable)
2898{
2899 struct drm_device *dev;
2900 struct drm_crtc *crtc;
2901 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002902
2903 if (!sde_crtc) {
2904 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002905 return -EINVAL;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002906 }
2907
2908 crtc = &sde_crtc->base;
2909 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002910
2911 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002912 int ret;
2913
2914 /* drop lock since power crtc cb may try to re-acquire lock */
2915 mutex_unlock(&sde_crtc->crtc_lock);
2916 ret = _sde_crtc_power_enable(sde_crtc, true);
2917 mutex_lock(&sde_crtc->crtc_lock);
2918 if (ret)
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002919 return ret;
Dhaval Patelf9245d62017-03-28 16:24:00 -07002920
Clarence Ip7f70ce42017-03-20 06:53:46 -07002921 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2922 if (enc->crtc != crtc)
2923 continue;
2924
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002925 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2926 sde_crtc->enabled,
2927 sde_crtc->suspend,
2928 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002929
2930 sde_encoder_register_vblank_callback(enc,
2931 sde_crtc_vblank_cb, (void *)crtc);
2932 }
2933 } else {
2934 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2935 if (enc->crtc != crtc)
2936 continue;
2937
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002938 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2939 sde_crtc->enabled,
2940 sde_crtc->suspend,
2941 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002942
2943 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2944 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002945
2946 /* drop lock since power crtc cb may try to re-acquire lock */
2947 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002948 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002949 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002950 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002951
2952 return 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002953}
2954
2955/**
2956 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2957 * @crtc: Pointer to drm crtc object
2958 * @enable: true to enable suspend, false to indicate resume
2959 */
2960static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2961{
2962 struct sde_crtc *sde_crtc;
2963 struct msm_drm_private *priv;
2964 struct sde_kms *sde_kms;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002965 int ret = 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002966
2967 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2968 SDE_ERROR("invalid crtc\n");
2969 return;
2970 }
2971 sde_crtc = to_sde_crtc(crtc);
2972 priv = crtc->dev->dev_private;
2973
2974 if (!priv->kms) {
2975 SDE_ERROR("invalid crtc kms\n");
2976 return;
2977 }
2978 sde_kms = to_sde_kms(priv->kms);
2979
2980 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002981 SDE_EVT32_VERBOSE(DRMID(crtc), enable);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002982
2983 mutex_lock(&sde_crtc->crtc_lock);
2984
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002985 /*
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002986 * If the vblank is enabled, release a power reference on suspend
2987 * and take it back during resume (if it is still enabled).
Clarence Ip7f70ce42017-03-20 06:53:46 -07002988 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002989 SDE_EVT32(DRMID(&sde_crtc->base), enable, sde_crtc->enabled,
2990 sde_crtc->suspend, sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002991 if (sde_crtc->suspend == enable)
2992 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2993 crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002994 else if (sde_crtc->enabled && sde_crtc->vblank_requested) {
2995 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, !enable);
2996 if (ret)
2997 SDE_ERROR("%s vblank enable failed: %d\n",
2998 sde_crtc->name, ret);
2999 }
Clarence Ip7f70ce42017-03-20 06:53:46 -07003000
3001 sde_crtc->suspend = enable;
Clarence Ip7f70ce42017-03-20 06:53:46 -07003002 mutex_unlock(&sde_crtc->crtc_lock);
3003}
3004
3005/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04003006 * sde_crtc_duplicate_state - state duplicate hook
3007 * @crtc: Pointer to drm crtc structure
3008 * @Returns: Pointer to new drm_crtc_state structure
3009 */
3010static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
3011{
3012 struct sde_crtc *sde_crtc;
3013 struct sde_crtc_state *cstate, *old_cstate;
3014
3015 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003016 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003017 return NULL;
3018 }
3019
3020 sde_crtc = to_sde_crtc(crtc);
3021 old_cstate = to_sde_crtc_state(crtc->state);
3022 cstate = msm_property_alloc_state(&sde_crtc->property_info);
3023 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003024 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003025 return NULL;
3026 }
3027
3028 /* duplicate value helper */
3029 msm_property_duplicate_state(&sde_crtc->property_info,
3030 old_cstate, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003031 &cstate->property_state, cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003032
3033 /* duplicate base helper */
3034 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
3035
Alan Kwongcdb2f282017-03-18 13:42:06 -07003036 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
3037
Clarence Ip7a753bb2016-07-07 11:47:44 -04003038 return &cstate->base;
3039}
3040
3041/**
3042 * sde_crtc_reset - reset hook for CRTCs
3043 * Resets the atomic state for @crtc by freeing the state pointer (which might
3044 * be NULL, e.g. at driver load time) and allocating a new empty state object.
3045 * @crtc: Pointer to drm crtc structure
3046 */
3047static void sde_crtc_reset(struct drm_crtc *crtc)
3048{
3049 struct sde_crtc *sde_crtc;
3050 struct sde_crtc_state *cstate;
3051
3052 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003053 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003054 return;
3055 }
3056
Clarence Ip7f70ce42017-03-20 06:53:46 -07003057 /* revert suspend actions, if necessary */
Clarence Ipd86f6e42017-08-08 18:31:00 -04003058 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07003059 _sde_crtc_set_suspend(crtc, false);
3060
Clarence Ip7a753bb2016-07-07 11:47:44 -04003061 /* remove previous state, if present */
3062 if (crtc->state) {
3063 sde_crtc_destroy_state(crtc, crtc->state);
3064 crtc->state = 0;
3065 }
3066
3067 sde_crtc = to_sde_crtc(crtc);
3068 cstate = msm_property_alloc_state(&sde_crtc->property_info);
3069 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003070 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003071 return;
3072 }
3073
3074 /* reset value helper */
3075 msm_property_reset_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003076 &cstate->property_state,
3077 cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003078
Clarence Ipcae1bb62016-07-07 12:07:13 -04003079 _sde_crtc_set_input_fence_timeout(cstate);
3080
Alan Kwong310e9b02017-08-03 02:04:07 -04003081 _sde_crtc_rp_reset(&cstate->rp, &sde_crtc->rp_lock,
3082 &sde_crtc->rp_head);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003083
Clarence Ip7a753bb2016-07-07 11:47:44 -04003084 cstate->base.crtc = crtc;
3085 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003086}
3087
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003088static void sde_crtc_handle_power_event(u32 event_type, void *arg)
3089{
3090 struct drm_crtc *crtc = arg;
3091 struct sde_crtc *sde_crtc;
Dhaval Patel010f5172017-08-01 22:40:09 -07003092 struct drm_plane *plane;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003093 struct drm_encoder *encoder;
Dhaval Patel010f5172017-08-01 22:40:09 -07003094 struct sde_crtc_mixer *m;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07003095 u32 i, misr_status;
Ping Licc868fc2017-08-11 16:56:44 -07003096 unsigned long flags;
3097 struct sde_crtc_irq_info *node = NULL;
3098 int ret = 0;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003099
3100 if (!crtc) {
3101 SDE_ERROR("invalid crtc\n");
3102 return;
3103 }
3104 sde_crtc = to_sde_crtc(crtc);
3105
3106 mutex_lock(&sde_crtc->crtc_lock);
3107
3108 SDE_EVT32(DRMID(crtc), event_type);
3109
Dhaval Patel010f5172017-08-01 22:40:09 -07003110 switch (event_type) {
3111 case SDE_POWER_EVENT_POST_ENABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003112 /* restore encoder; crtc will be programmed during commit */
3113 drm_for_each_encoder(encoder, crtc->dev) {
3114 if (encoder->crtc != crtc)
3115 continue;
3116
3117 sde_encoder_virt_restore(encoder);
3118 }
Ping Licc868fc2017-08-11 16:56:44 -07003119
3120 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3121 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3122 ret = 0;
3123 if (node->func)
3124 ret = node->func(crtc, true, &node->irq);
3125 if (ret)
3126 SDE_ERROR("%s failed to enable event %x\n",
3127 sde_crtc->name, node->event);
3128 }
3129 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3130
Ping Lie505f3b2017-06-19 14:19:08 -07003131 sde_cp_crtc_post_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003132
Dhaval Patel010f5172017-08-01 22:40:09 -07003133 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3134 m = &sde_crtc->mixers[i];
3135 if (!m->hw_lm || !m->hw_lm->ops.setup_misr ||
3136 !sde_crtc->misr_enable)
3137 continue;
3138
3139 m->hw_lm->ops.setup_misr(m->hw_lm, true,
3140 sde_crtc->misr_frame_count);
3141 }
3142 break;
3143 case SDE_POWER_EVENT_PRE_DISABLE:
3144 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3145 m = &sde_crtc->mixers[i];
3146 if (!m->hw_lm || !m->hw_lm->ops.collect_misr ||
3147 !sde_crtc->misr_enable)
3148 continue;
3149
3150 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
3151 sde_crtc->misr_data[i] = misr_status ? misr_status :
3152 sde_crtc->misr_data[i];
3153 }
Ping Licc868fc2017-08-11 16:56:44 -07003154
3155 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3156 node = NULL;
3157 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3158 ret = 0;
3159 if (node->func)
3160 ret = node->func(crtc, false, &node->irq);
3161 if (ret)
3162 SDE_ERROR("%s failed to disable event %x\n",
3163 sde_crtc->name, node->event);
3164 }
3165 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3166
Dhaval Patel010f5172017-08-01 22:40:09 -07003167 sde_cp_crtc_pre_ipc(crtc);
3168 break;
3169 case SDE_POWER_EVENT_POST_DISABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003170 /*
3171 * set revalidate flag in planes, so it will be re-programmed
3172 * in the next frame update
3173 */
3174 drm_atomic_crtc_for_each_plane(plane, crtc)
3175 sde_plane_set_revalidate(plane, true);
Alan Kwong8a9b38a2017-06-22 11:30:52 -04003176
Gopikrishnaiah Anandandb90fa12017-05-09 17:56:08 -07003177 sde_cp_crtc_suspend(crtc);
Dhaval Patel010f5172017-08-01 22:40:09 -07003178 break;
3179 default:
3180 SDE_DEBUG("event:%d not handled\n", event_type);
3181 break;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003182 }
3183
3184 mutex_unlock(&sde_crtc->crtc_lock);
3185}
3186
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003187static void sde_crtc_disable(struct drm_crtc *crtc)
3188{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003189 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003190 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04003191 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003192 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003193 unsigned long flags;
3194 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003195 struct drm_event event;
3196 u32 power_on;
Dhaval Patelfd8f7742017-08-10 13:11:22 -07003197 int ret, i;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003198
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003199 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003200 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003201 return;
3202 }
3203 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003204 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003205 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003206
Alan Kwong163d2612016-11-03 00:56:56 -04003207 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003208
Clarence Ipd86f6e42017-08-08 18:31:00 -04003209 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07003210 _sde_crtc_set_suspend(crtc, true);
3211
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003212 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003213 SDE_EVT32_VERBOSE(DRMID(crtc));
Alan Kwong628d19e2016-10-31 13:50:13 -04003214
Ping Lic5c2e0b2017-08-02 15:17:59 -07003215 /* update color processing on suspend */
3216 event.type = DRM_EVENT_CRTC_POWER;
3217 event.length = sizeof(u32);
3218 sde_cp_crtc_suspend(crtc);
3219 power_on = 0;
3220 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3221 (u8 *)&power_on);
3222
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003223 /* wait for frame_event_done completion */
3224 if (_sde_crtc_wait_for_frame_done(crtc))
3225 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
3226 crtc->base.id,
3227 atomic_read(&sde_crtc->frame_pending));
3228
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003229 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3230 sde_crtc->vblank_requested);
3231 if (sde_crtc->enabled && !sde_crtc->suspend &&
3232 sde_crtc->vblank_requested) {
3233 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, false);
3234 if (ret)
3235 SDE_ERROR("%s vblank enable failed: %d\n",
3236 sde_crtc->name, ret);
Alan Kwong07da0982016-11-04 12:57:45 -04003237 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003238 sde_crtc->enabled = false;
Alan Kwong07da0982016-11-04 12:57:45 -04003239
Alan Kwong628d19e2016-10-31 13:50:13 -04003240 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07003241 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
3242 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003243 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04003244 atomic_set(&sde_crtc->frame_pending, 0);
3245 }
3246
Ping Li6d5bf542017-06-27 11:40:28 -07003247 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3248 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3249 ret = 0;
3250 if (node->func)
3251 ret = node->func(crtc, false, &node->irq);
3252 if (ret)
3253 SDE_ERROR("%s failed to disable event %x\n",
3254 sde_crtc->name, node->event);
3255 }
3256 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3257
Alan Kwong9aa061c2016-11-06 21:17:12 -05003258 sde_core_perf_crtc_update(crtc, 0, true);
3259
Alan Kwong628d19e2016-10-31 13:50:13 -04003260 drm_for_each_encoder(encoder, crtc->dev) {
3261 if (encoder->crtc != crtc)
3262 continue;
3263 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003264 cstate->rsc_client = NULL;
3265 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04003266 }
3267
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003268 if (sde_crtc->power_event)
3269 sde_power_handle_unregister_event(&priv->phandle,
3270 sde_crtc->power_event);
3271
Dhaval Patelfd8f7742017-08-10 13:11:22 -07003272 /**
3273 * All callbacks are unregistered and frame done waits are complete
3274 * at this point. No buffers are accessed by hardware.
3275 * reset the fence timeline if there is any issue.
3276 */
3277 sde_fence_signal(&sde_crtc->output_fence, ktime_get(), true);
3278 for (i = 0; i < cstate->num_connectors; ++i)
3279 sde_connector_commit_reset(cstate->connectors[i], ktime_get());
3280
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003281 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
3282 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003283
Alan Kwong8411a9112017-06-06 19:29:01 -04003284 /* disable clk & bw control until clk & bw properties are set */
3285 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07003286 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04003287
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003288 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003289}
3290
3291static void sde_crtc_enable(struct drm_crtc *crtc)
3292{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003293 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04003294 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003295 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003296 unsigned long flags;
3297 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003298 struct drm_event event;
3299 u32 power_on;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003300 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003301
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003302 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003303 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003304 return;
3305 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003306 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003307
Alan Kwong163d2612016-11-03 00:56:56 -04003308 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003309 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04003310 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003311
Alan Kwong628d19e2016-10-31 13:50:13 -04003312 drm_for_each_encoder(encoder, crtc->dev) {
3313 if (encoder->crtc != crtc)
3314 continue;
3315 sde_encoder_register_frame_event_callback(encoder,
3316 sde_crtc_frame_event_cb, (void *)crtc);
3317 }
3318
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003319 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003320 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3321 sde_crtc->vblank_requested);
3322 if (!sde_crtc->enabled && !sde_crtc->suspend &&
3323 sde_crtc->vblank_requested) {
3324 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, true);
3325 if (ret)
3326 SDE_ERROR("%s vblank enable failed: %d\n",
3327 sde_crtc->name, ret);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003328 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003329 sde_crtc->enabled = true;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003330
3331 /* update color processing on resume */
3332 event.type = DRM_EVENT_CRTC_POWER;
3333 event.length = sizeof(u32);
3334 sde_cp_crtc_resume(crtc);
3335 power_on = 1;
3336 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3337 (u8 *)&power_on);
3338
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003339 mutex_unlock(&sde_crtc->crtc_lock);
3340
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003341 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3342 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3343 ret = 0;
3344 if (node->func)
3345 ret = node->func(crtc, true, &node->irq);
3346 if (ret)
3347 SDE_ERROR("%s failed to enable event %x\n",
3348 sde_crtc->name, node->event);
3349 }
3350 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003351
3352 sde_crtc->power_event = sde_power_handle_register_event(
3353 &priv->phandle,
Ping Lie505f3b2017-06-19 14:19:08 -07003354 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE |
3355 SDE_POWER_EVENT_PRE_DISABLE,
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003356 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003357}
3358
3359struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003360 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003361 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003362 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003363 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003364};
3365
Clarence Ipc47a0692016-10-11 10:54:17 -04003366static int pstate_cmp(const void *a, const void *b)
3367{
3368 struct plane_state *pa = (struct plane_state *)a;
3369 struct plane_state *pb = (struct plane_state *)b;
3370 int rc = 0;
3371 int pa_zpos, pb_zpos;
3372
3373 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
3374 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
3375
3376 if (pa_zpos != pb_zpos)
3377 rc = pa_zpos - pb_zpos;
3378 else
3379 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
3380
3381 return rc;
3382}
3383
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003384static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
3385 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
3386{
3387 struct sde_rect dst_rect, intersect;
3388 int i, rc = -EINVAL;
3389 const struct drm_plane_state *pstate;
3390
3391 /* start checking from next plane */
3392 for (i = curr_cnt; i < cnt; i++) {
3393 pstate = pstates[i].drm_pstate;
3394 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
Veera Sundaram Sankaran9d9ff912017-06-20 10:41:21 -07003395 pstate->crtc_w, pstate->crtc_h, false);
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003396 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
3397
3398 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
3399 /* next plane may be on same z-order */
3400 && z_pos != pstates[i].stage) {
3401 rc = 0;
3402 goto end;
3403 }
3404 }
3405
3406 SDE_ERROR("excl rect does not find top overlapping rect\n");
3407end:
3408 return rc;
3409}
3410
3411/* no input validation - caller API has all the checks */
3412static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
3413 struct plane_state pstates[], int cnt)
3414{
3415 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
3416 struct drm_display_mode *mode = &state->adjusted_mode;
3417 const struct drm_plane_state *pstate;
3418 struct sde_plane_state *sde_pstate;
3419 int rc = 0, i;
3420
3421 /* Check dim layer rect bounds and stage */
3422 for (i = 0; i < cstate->num_dim_layers; i++) {
3423 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
3424 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
3425 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
3426 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
3427 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
3428 (!cstate->dim_layer[i].rect.w) ||
3429 (!cstate->dim_layer[i].rect.h)) {
3430 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
3431 cstate->dim_layer[i].rect.x,
3432 cstate->dim_layer[i].rect.y,
3433 cstate->dim_layer[i].rect.w,
3434 cstate->dim_layer[i].rect.h,
3435 cstate->dim_layer[i].stage);
3436 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
3437 mode->vdisplay);
3438 rc = -E2BIG;
3439 goto end;
3440 }
3441 }
3442
3443 /* this is traversing on sorted z-order pstates */
3444 for (i = 0; i < cnt; i++) {
3445 pstate = pstates[i].drm_pstate;
3446 sde_pstate = to_sde_plane_state(pstate);
3447 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
3448 /* check overlap on all top z-order */
3449 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
3450 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
3451 if (rc)
3452 goto end;
3453 }
3454 }
3455
3456end:
3457 return rc;
3458}
3459
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003460static int _sde_crtc_check_secure_state(struct drm_crtc *crtc,
3461 struct drm_crtc_state *state)
3462{
3463 struct drm_encoder *encoder;
3464 struct sde_crtc_state *cstate;
3465 uint32_t secure;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003466 uint32_t fb_ns = 0, fb_sec = 0, fb_sec_dir = 0;
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003467 int encoder_cnt = 0;
3468 int rc;
3469
3470 if (!crtc || !state) {
3471 SDE_ERROR("invalid arguments\n");
3472 return -EINVAL;
3473 }
3474
3475 cstate = to_sde_crtc_state(state);
3476
3477 secure = sde_crtc_get_property(cstate,
3478 CRTC_PROP_SECURITY_LEVEL);
3479
3480 rc = _sde_crtc_find_plane_fb_modes(state,
3481 &fb_ns,
3482 &fb_sec,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003483 &fb_sec_dir);
3484 if (rc)
3485 return rc;
3486
3487 /**
3488 * validate planes
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003489 * fb_sec_dir is for secure camera preview and secure display use case,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003490 * fb_sec is for secure video playback,
3491 * fb_ns is for normal non secure use cases.
3492 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003493 if ((secure == SDE_DRM_SEC_ONLY) &&
3494 (fb_ns || fb_sec || (fb_sec && fb_sec_dir))) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003495 SDE_ERROR(
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003496 "crtc%d: invalid planes fb_modes Sec:%d, NS:%d, Sec_Dir:%d\n",
3497 crtc->base.id, fb_sec, fb_ns, fb_sec_dir);
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003498 return -EINVAL;
3499 }
3500
3501 /**
3502 * secure_crtc is not allowed in a shared toppolgy
3503 * across different encoders.
3504 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003505 if (fb_sec_dir) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003506 drm_for_each_encoder(encoder, crtc->dev)
3507 if (encoder->crtc == crtc)
3508 encoder_cnt++;
3509
3510 if (encoder_cnt >
3511 MAX_ALLOWED_ENCODER_CNT_PER_SECURE_CRTC) {
3512 SDE_ERROR(
3513 "crtc%d, invalid virtual encoder crtc%d\n",
3514 crtc->base.id,
3515 encoder_cnt);
3516 return -EINVAL;
3517
3518 }
3519 }
3520 SDE_DEBUG("crtc:%d Secure validation successful\n", crtc->base.id);
3521 return 0;
3522}
3523
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003524static int sde_crtc_atomic_check(struct drm_crtc *crtc,
3525 struct drm_crtc_state *state)
3526{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003527 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003528 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003529 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003530
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003531 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003532 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003533 struct drm_display_mode *mode;
3534
3535 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003536
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003537 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
3538 int multirect_count = 0;
3539 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003540 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003541
Clarence Ipcae1bb62016-07-07 12:07:13 -04003542 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003543 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003544 return -EINVAL;
3545 }
3546
Alan Kwongcdb2f282017-03-18 13:42:06 -07003547 sde_crtc = to_sde_crtc(crtc);
3548 cstate = to_sde_crtc_state(state);
3549
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003550 if (!state->enable || !state->active) {
3551 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
3552 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003553 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003554 }
3555
Dhaval Patelec10fad2016-08-22 14:40:48 -07003556 mode = &state->adjusted_mode;
3557 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003558
Clarence Ip90b282d2017-05-04 10:00:32 -07003559 /* force a full mode set if active state changed */
3560 if (state->active_changed)
3561 state->mode_changed = true;
3562
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003563 memset(pipe_staged, 0, sizeof(pipe_staged));
3564
Dhaval Patelec10fad2016-08-22 14:40:48 -07003565 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003566
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05003567 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003568 _sde_crtc_setup_lm_bounds(crtc, state);
3569
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003570 rc = _sde_crtc_check_secure_state(crtc, state);
3571 if (rc)
3572 return rc;
3573
Dhaval Patelec10fad2016-08-22 14:40:48 -07003574 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003575 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003576 if (IS_ERR_OR_NULL(pstate)) {
3577 rc = PTR_ERR(pstate);
3578 SDE_ERROR("%s: failed to get plane%d state, %d\n",
3579 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04003580 goto end;
3581 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003582 if (cnt >= ARRAY_SIZE(pstates))
3583 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003584
Dhaval Patelec10fad2016-08-22 14:40:48 -07003585 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
3586 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003587 pstates[cnt].stage = sde_plane_get_property(
3588 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003589 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003590
3591 /* check dim layer stage with every plane */
3592 for (i = 0; i < cstate->num_dim_layers; i++) {
Veera Sundaram Sankaranb9ed6bd2017-07-11 19:18:03 -07003593 if (cstate->dim_layer[i].stage
3594 == (pstates[cnt].stage + SDE_STAGE_0)) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003595 SDE_ERROR(
3596 "plane:%d/dim_layer:%i-same stage:%d\n",
3597 plane->base.id, i,
3598 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003599 rc = -EINVAL;
3600 goto end;
3601 }
3602 }
3603
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003604 if (pipe_staged[pstates[cnt].pipe_id]) {
3605 multirect_plane[multirect_count].r0 =
3606 pipe_staged[pstates[cnt].pipe_id];
3607 multirect_plane[multirect_count].r1 = pstate;
3608 multirect_count++;
3609
3610 pipe_staged[pstates[cnt].pipe_id] = NULL;
3611 } else {
3612 pipe_staged[pstates[cnt].pipe_id] = pstate;
3613 }
3614
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003615 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003616
3617 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
3618 mode->vdisplay) ||
3619 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
3620 mode->hdisplay)) {
3621 SDE_ERROR("invalid vertical/horizontal destination\n");
3622 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
3623 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
3624 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
3625 rc = -E2BIG;
3626 goto end;
3627 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003628 }
3629
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003630 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003631 if (pipe_staged[i]) {
3632 sde_plane_clear_multirect(pipe_staged[i]);
3633
3634 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003635 SDE_ERROR(
3636 "r1 only virt plane:%d not supported\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003637 pipe_staged[i]->plane->base.id);
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003638 rc = -EINVAL;
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003639 goto end;
3640 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003641 }
3642 }
3643
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003644 /* assign mixer stages based on sorted zpos property */
3645 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
3646
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003647 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
3648 if (rc)
3649 goto end;
3650
Clarence Ipc47a0692016-10-11 10:54:17 -04003651 if (!sde_is_custom_client()) {
3652 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003653
Clarence Ipc47a0692016-10-11 10:54:17 -04003654 z_pos = 0;
3655 for (i = 0; i < cnt; i++) {
3656 if (stage_old != pstates[i].stage)
3657 ++z_pos;
3658 stage_old = pstates[i].stage;
3659 pstates[i].stage = z_pos;
3660 }
3661 }
3662
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003663 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04003664 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003665 /* reset counts at every new blend stage */
3666 if (pstates[i].stage != z_pos) {
3667 left_zpos_cnt = 0;
3668 right_zpos_cnt = 0;
3669 z_pos = pstates[i].stage;
3670 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003671
3672 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04003673 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003674 SDE_ERROR("> %d plane stages assigned\n",
3675 SDE_STAGE_MAX - SDE_STAGE_0);
3676 rc = -EINVAL;
3677 goto end;
3678 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003679 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003680 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003681 z_pos);
3682 rc = -EINVAL;
3683 goto end;
3684 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003685 left_zpos_cnt++;
3686
Dhaval Patelec10fad2016-08-22 14:40:48 -07003687 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003688 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003689 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003690 z_pos);
3691 rc = -EINVAL;
3692 goto end;
3693 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003694 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003695 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003696
Clarence Ipc47a0692016-10-11 10:54:17 -04003697 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003698 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003699 }
3700
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003701 for (i = 0; i < multirect_count; i++) {
3702 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
3703 SDE_ERROR(
3704 "multirect validation failed for planes (%d - %d)\n",
3705 multirect_plane[i].r0->plane->base.id,
3706 multirect_plane[i].r1->plane->base.id);
3707 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003708 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003709 }
3710 }
3711
Alan Kwong9aa061c2016-11-06 21:17:12 -05003712 rc = sde_core_perf_crtc_check(crtc, state);
3713 if (rc) {
3714 SDE_ERROR("crtc%d failed performance check %d\n",
3715 crtc->base.id, rc);
3716 goto end;
3717 }
3718
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003719 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003720 * use pstates sorted by stage to check planes on same stage
3721 * we assume that all pipes are in source split so its valid to compare
3722 * without taking into account left/right mixer placement
3723 */
3724 for (i = 1; i < cnt; i++) {
3725 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003726 struct sde_rect left_rect, right_rect;
3727 int32_t left_pid, right_pid;
3728 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003729
3730 prv_pstate = &pstates[i - 1];
3731 cur_pstate = &pstates[i];
3732 if (prv_pstate->stage != cur_pstate->stage)
3733 continue;
3734
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003735 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003736
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003737 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
3738 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
3739 prv_pstate->drm_pstate->crtc_y,
3740 prv_pstate->drm_pstate->crtc_w,
3741 prv_pstate->drm_pstate->crtc_h, false);
3742
3743 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
3744 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
3745 cur_pstate->drm_pstate->crtc_y,
3746 cur_pstate->drm_pstate->crtc_w,
3747 cur_pstate->drm_pstate->crtc_h, false);
3748
3749 if (right_rect.x < left_rect.x) {
3750 swap(left_pid, right_pid);
3751 swap(left_rect, right_rect);
3752 }
3753
3754 /**
3755 * - planes are enumerated in pipe-priority order such that
3756 * planes with lower drm_id must be left-most in a shared
3757 * blend-stage when using source split.
3758 * - planes in source split must be contiguous in width
3759 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003760 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003761 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003762 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003763 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
3764 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003765 rc = -EINVAL;
3766 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003767 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003768 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003769 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
3770 stage, left_rect.x, left_rect.w,
3771 right_rect.x, right_rect.w);
3772 rc = -EINVAL;
3773 goto end;
3774 } else if ((left_rect.y != right_rect.y) ||
3775 (left_rect.h != right_rect.h)) {
3776 SDE_ERROR(
3777 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
3778 stage, left_rect.y, right_rect.y,
3779 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003780 rc = -EINVAL;
3781 goto end;
3782 }
3783 }
3784
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003785 rc = _sde_crtc_check_rois(crtc, state);
3786 if (rc) {
3787 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
3788 goto end;
3789 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003790
Dhaval Patelec10fad2016-08-22 14:40:48 -07003791end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07003792 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07003793 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003794}
3795
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003796int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003797{
Clarence Ip7f70ce42017-03-20 06:53:46 -07003798 struct sde_crtc *sde_crtc;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003799 int ret;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003800
Clarence Ip7f70ce42017-03-20 06:53:46 -07003801 if (!crtc) {
3802 SDE_ERROR("invalid crtc\n");
3803 return -EINVAL;
3804 }
3805 sde_crtc = to_sde_crtc(crtc);
3806
3807 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003808 SDE_EVT32(DRMID(&sde_crtc->base), en, sde_crtc->enabled,
3809 sde_crtc->suspend, sde_crtc->vblank_requested);
3810 if (sde_crtc->enabled && !sde_crtc->suspend) {
3811 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, en);
3812 if (ret)
3813 SDE_ERROR("%s vblank enable failed: %d\n",
3814 sde_crtc->name, ret);
3815 }
3816 sde_crtc->vblank_requested = en;
Clarence Ip7f70ce42017-03-20 06:53:46 -07003817 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003818
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003819 return 0;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003820}
3821
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003822void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
3823{
3824 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
3825
Alan Kwong163d2612016-11-03 00:56:56 -04003826 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003827 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003828}
3829
Clarence Ip7a753bb2016-07-07 11:47:44 -04003830/**
3831 * sde_crtc_install_properties - install all drm properties for crtc
3832 * @crtc: Pointer to drm crtc structure
3833 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003834static void sde_crtc_install_properties(struct drm_crtc *crtc,
3835 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003836{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003837 struct sde_crtc *sde_crtc;
3838 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003839 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003840 struct sde_kms *sde_kms;
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003841 static const struct drm_prop_enum_list e_secure_level[] = {
3842 {SDE_DRM_SEC_NON_SEC, "sec_and_non_sec"},
3843 {SDE_DRM_SEC_ONLY, "sec_only"},
3844 };
Clarence Ip7a753bb2016-07-07 11:47:44 -04003845
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003846 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003847
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003848 if (!crtc || !catalog) {
3849 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003850 return;
3851 }
3852
3853 sde_crtc = to_sde_crtc(crtc);
3854 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003855 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003856
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07003857 if (!sde_kms) {
3858 SDE_ERROR("invalid argument\n");
3859 return;
3860 }
3861
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003862 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3863 if (!info) {
3864 SDE_ERROR("failed to allocate info memory\n");
3865 return;
3866 }
3867
Clarence Ip7a753bb2016-07-07 11:47:44 -04003868 /* range properties */
3869 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003870 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3871 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3872
3873 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3874 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003875
3876 msm_property_install_range(&sde_crtc->property_info,
3877 "output_fence_offset", 0x0, 0, 1, 0,
3878 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003879
Alan Kwong9aa061c2016-11-06 21:17:12 -05003880 msm_property_install_range(&sde_crtc->property_info,
3881 "core_clk", 0x0, 0, U64_MAX,
3882 sde_kms->perf.max_core_clk_rate,
3883 CRTC_PROP_CORE_CLK);
3884 msm_property_install_range(&sde_crtc->property_info,
3885 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003886 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003887 CRTC_PROP_CORE_AB);
3888 msm_property_install_range(&sde_crtc->property_info,
3889 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003890 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003891 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003892 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003893 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003894 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003895 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003896 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003897 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003898 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003899 CRTC_PROP_LLCC_IB);
3900 msm_property_install_range(&sde_crtc->property_info,
3901 "dram_ab", 0x0, 0, U64_MAX,
3902 catalog->perf.max_bw_high * 1000ULL,
3903 CRTC_PROP_DRAM_AB);
3904 msm_property_install_range(&sde_crtc->property_info,
3905 "dram_ib", 0x0, 0, U64_MAX,
3906 catalog->perf.max_bw_high * 1000ULL,
3907 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003908 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003909 "rot_prefill_bw", 0, 0, U64_MAX,
3910 catalog->perf.max_bw_high * 1000ULL,
3911 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003912 msm_property_install_range(&sde_crtc->property_info,
3913 "rot_clk", 0, 0, U64_MAX,
3914 sde_kms->perf.max_core_clk_rate,
3915 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003916
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05303917 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patele17e0ee2017-08-23 18:01:42 -07003918 "idle_timeout", IDLE_TIMEOUT, 0, U64_MAX, 0,
3919 CRTC_PROP_IDLE_TIMEOUT);
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05303920
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003921 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3922 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003923
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003924 msm_property_install_volatile_range(&sde_crtc->property_info,
3925 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3926
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003927 msm_property_install_enum(&sde_crtc->property_info, "security_level",
3928 0x0, 0, e_secure_level,
3929 ARRAY_SIZE(e_secure_level),
3930 CRTC_PROP_SECURITY_LEVEL);
3931
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003932 sde_kms_info_reset(info);
3933
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003934 if (catalog->has_dim_layer) {
3935 msm_property_install_volatile_range(&sde_crtc->property_info,
3936 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3937 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3938 SDE_MAX_DIM_LAYERS);
3939 }
3940
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003941 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3942 sde_kms_info_add_keyint(info, "max_linewidth",
3943 catalog->max_mixer_width);
3944 sde_kms_info_add_keyint(info, "max_blendstages",
3945 catalog->max_mixer_blendstages);
3946 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3947 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3948 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3949 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003950
3951 if (sde_is_custom_client()) {
3952 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3953 sde_kms_info_add_keystr(info,
3954 "smart_dma_rev", "smart_dma_v1");
3955 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3956 sde_kms_info_add_keystr(info,
3957 "smart_dma_rev", "smart_dma_v2");
3958 }
3959
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003960 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003961 if (catalog->perf.max_bw_low)
3962 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003963 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003964 if (catalog->perf.max_bw_high)
3965 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003966 catalog->perf.max_bw_high * 1000LL);
Narendra Muppallaa50934b2017-08-15 19:43:37 -07003967 if (catalog->perf.min_core_ib)
3968 sde_kms_info_add_keyint(info, "min_core_ib",
3969 catalog->perf.min_core_ib * 1000LL);
3970 if (catalog->perf.min_llcc_ib)
3971 sde_kms_info_add_keyint(info, "min_llcc_ib",
3972 catalog->perf.min_llcc_ib * 1000LL);
3973 if (catalog->perf.min_dram_ib)
3974 sde_kms_info_add_keyint(info, "min_dram_ib",
3975 catalog->perf.min_dram_ib * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003976 if (sde_kms->perf.max_core_clk_rate)
3977 sde_kms_info_add_keyint(info, "max_mdp_clk",
3978 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003979 sde_kms_info_add_keystr(info, "core_ib_ff",
3980 catalog->perf.core_ib_ff);
3981 sde_kms_info_add_keystr(info, "core_clk_ff",
3982 catalog->perf.core_clk_ff);
3983 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3984 catalog->perf.comp_ratio_rt);
3985 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3986 catalog->perf.comp_ratio_nrt);
3987 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3988 catalog->perf.dest_scale_prefill_lines);
3989 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3990 catalog->perf.undersized_prefill_lines);
3991 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3992 catalog->perf.macrotile_prefill_lines);
3993 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3994 catalog->perf.yuv_nv12_prefill_lines);
3995 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3996 catalog->perf.linear_prefill_lines);
3997 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3998 catalog->perf.downscaling_prefill_lines);
3999 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
4000 catalog->perf.xtra_prefill_lines);
4001 sde_kms_info_add_keyint(info, "amortizable_threshold",
4002 catalog->perf.amortizable_threshold);
4003 sde_kms_info_add_keyint(info, "min_prefill_lines",
4004 catalog->perf.min_prefill_lines);
4005
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004006 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07004007 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004008
4009 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004010}
4011
4012/**
4013 * sde_crtc_atomic_set_property - atomically set a crtc drm property
4014 * @crtc: Pointer to drm crtc structure
4015 * @state: Pointer to drm crtc state structure
4016 * @property: Pointer to targeted drm property
4017 * @val: Updated property value
4018 * @Returns: Zero on success
4019 */
4020static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
4021 struct drm_crtc_state *state,
4022 struct drm_property *property,
4023 uint64_t val)
4024{
4025 struct sde_crtc *sde_crtc;
4026 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04004027 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04004028
4029 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07004030 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004031 } else {
4032 sde_crtc = to_sde_crtc(crtc);
4033 cstate = to_sde_crtc_state(state);
4034 ret = msm_property_atomic_set(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04004035 &cstate->property_state, property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04004036 if (!ret) {
4037 idx = msm_property_index(&sde_crtc->property_info,
4038 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08004039 switch (idx) {
4040 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04004041 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08004042 break;
4043 case CRTC_PROP_DIM_LAYER_V1:
4044 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
4045 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04004046 case CRTC_PROP_ROI_V1:
4047 ret = _sde_crtc_set_roi_v1(state, (void *)val);
4048 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04004049 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07004050 case CRTC_PROP_CORE_AB:
4051 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07004052 cstate->bw_control = true;
4053 break;
Alan Kwong0230a102017-05-16 11:36:44 -07004054 case CRTC_PROP_LLCC_AB:
4055 case CRTC_PROP_LLCC_IB:
4056 case CRTC_PROP_DRAM_AB:
4057 case CRTC_PROP_DRAM_IB:
4058 cstate->bw_control = true;
4059 cstate->bw_split_vote = true;
4060 break;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07004061 case CRTC_PROP_IDLE_TIMEOUT:
4062 _sde_crtc_set_idle_timeout(crtc, val);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08004063 default:
4064 /* nothing to do */
4065 break;
4066 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004067 } else {
4068 ret = sde_cp_crtc_set_property(crtc,
4069 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04004070 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004071 if (ret)
4072 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07004073
4074 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
4075 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004076 }
4077
4078 return ret;
4079}
4080
4081/**
4082 * sde_crtc_set_property - set a crtc drm property
4083 * @crtc: Pointer to drm crtc structure
4084 * @property: Pointer to targeted drm property
4085 * @val: Updated property value
4086 * @Returns: Zero on success
4087 */
4088static int sde_crtc_set_property(struct drm_crtc *crtc,
4089 struct drm_property *property, uint64_t val)
4090{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04004091 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004092
4093 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
4094}
4095
4096/**
4097 * sde_crtc_atomic_get_property - retrieve a crtc drm property
4098 * @crtc: Pointer to drm crtc structure
4099 * @state: Pointer to drm crtc state structure
4100 * @property: Pointer to targeted drm property
4101 * @val: Pointer to variable for receiving property value
4102 * @Returns: Zero on success
4103 */
4104static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
4105 const struct drm_crtc_state *state,
4106 struct drm_property *property,
4107 uint64_t *val)
4108{
4109 struct sde_crtc *sde_crtc;
4110 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004111 struct drm_encoder *encoder;
Clarence Ip24f80662016-06-13 19:05:32 -04004112 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004113 bool conn_offset = 0;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004114 bool is_cmd = true;
Clarence Ip7a753bb2016-07-07 11:47:44 -04004115
4116 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07004117 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004118 } else {
4119 sde_crtc = to_sde_crtc(crtc);
4120 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004121
4122 for (i = 0; i < cstate->num_connectors; ++i) {
4123 conn_offset = sde_connector_needs_offset(
4124 cstate->connectors[i]);
4125 if (conn_offset)
4126 break;
4127 }
4128
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004129 /**
4130 * set the cmd flag only when all the encoders attached
4131 * to the crtc are in cmd mode. Consider all other cases
4132 * as video mode.
4133 */
4134 drm_for_each_encoder(encoder, crtc->dev) {
4135 if (encoder->crtc == crtc)
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05304136 is_cmd = sde_encoder_check_mode(encoder,
4137 MSM_DISPLAY_CAP_CMD_MODE);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004138 }
4139
Clarence Ip24f80662016-06-13 19:05:32 -04004140 i = msm_property_index(&sde_crtc->property_info, property);
4141 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08004142 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04004143 CRTC_PROP_OUTPUT_FENCE_OFFSET);
4144
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004145 /**
4146 * set the offset to 0 only for cmd mode panels, so
4147 * the release fence for the current frame can be
4148 * triggered right after PP_DONE interrupt.
4149 */
4150 offset = is_cmd ? 0 : (offset + conn_offset);
4151
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004152 ret = sde_fence_create(&sde_crtc->output_fence, val,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004153 offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04004154 if (ret)
4155 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04004156 } else {
4157 ret = msm_property_atomic_get(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04004158 &cstate->property_state,
4159 property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004160 if (ret)
4161 ret = sde_cp_crtc_get_property(crtc,
4162 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04004163 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004164 if (ret)
4165 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004166 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04004167 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004168}
4169
Alan Kwong67a3f792016-11-01 23:16:53 -04004170#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004171static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004172{
4173 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004174 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004175 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004176
4177 struct drm_crtc *crtc;
4178 struct drm_plane *plane;
4179 struct drm_display_mode *mode;
4180 struct drm_framebuffer *fb;
4181 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004182 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004183
4184 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004185
4186 if (!s || !s->private)
4187 return -EINVAL;
4188
4189 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004190 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004191 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004192
4193 mutex_lock(&sde_crtc->crtc_lock);
4194 mode = &crtc->state->adjusted_mode;
4195 out_width = sde_crtc_mixer_width(sde_crtc, mode);
4196
4197 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
4198 mode->hdisplay, mode->vdisplay);
4199
4200 seq_puts(s, "\n");
4201
Clarence Ip8f7366c2016-07-05 12:15:26 -04004202 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04004203 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004204 if (!m->hw_lm)
4205 seq_printf(s, "\tmixer[%d] has no lm\n", i);
4206 else if (!m->hw_ctl)
4207 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
4208 else
4209 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
4210 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
4211 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004212 }
Dhaval Patel44f12472016-08-29 12:19:47 -07004213
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004214 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07004215
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004216 for (i = 0; i < cstate->num_dim_layers; i++) {
4217 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
4218
4219 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
4220 i, dim_layer->stage, dim_layer->flags);
4221 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
4222 dim_layer->rect.x, dim_layer->rect.y,
4223 dim_layer->rect.w, dim_layer->rect.h);
4224 seq_printf(s,
4225 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
4226 dim_layer->color_fill.color_0,
4227 dim_layer->color_fill.color_1,
4228 dim_layer->color_fill.color_2,
4229 dim_layer->color_fill.color_3);
4230 seq_puts(s, "\n");
4231 }
4232
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004233 drm_atomic_crtc_for_each_plane(plane, crtc) {
4234 pstate = to_sde_plane_state(plane->state);
4235 state = plane->state;
4236
4237 if (!pstate || !state)
4238 continue;
4239
4240 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
4241 pstate->stage);
4242
4243 if (plane->state->fb) {
4244 fb = plane->state->fb;
4245
4246 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
4247 fb->base.id, (char *) &fb->pixel_format,
4248 fb->width, fb->height, fb->bits_per_pixel);
4249
4250 seq_puts(s, "\t");
4251 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
4252 seq_printf(s, "modifier[%d]:%8llu ", i,
4253 fb->modifier[i]);
4254 seq_puts(s, "\n");
4255
4256 seq_puts(s, "\t");
4257 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
4258 seq_printf(s, "pitches[%d]:%8u ", i,
4259 fb->pitches[i]);
4260 seq_puts(s, "\n");
4261
4262 seq_puts(s, "\t");
4263 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
4264 seq_printf(s, "offsets[%d]:%8u ", i,
4265 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07004266 seq_puts(s, "\n");
4267 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004268
4269 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
4270 state->src_x, state->src_y, state->src_w, state->src_h);
4271
4272 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
4273 state->crtc_x, state->crtc_y, state->crtc_w,
4274 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07004275 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
4276 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07004277
4278 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
4279 pstate->excl_rect.x, pstate->excl_rect.y,
4280 pstate->excl_rect.w, pstate->excl_rect.h);
4281
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004282 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04004283 }
Alan Kwong07da0982016-11-04 12:57:45 -04004284
4285 if (sde_crtc->vblank_cb_count) {
4286 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
4287 s64 diff_ms = ktime_to_ms(diff);
4288 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
4289 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
4290
4291 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07004292 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
4293 fps, sde_crtc->vblank_cb_count,
4294 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04004295
4296 /* reset time & count for next measurement */
4297 sde_crtc->vblank_cb_count = 0;
4298 sde_crtc->vblank_cb_time = ktime_set(0, 0);
4299 }
4300
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04004301 seq_printf(s, "vblank_enable:%d\n", sde_crtc->vblank_requested);
Alan Kwong07da0982016-11-04 12:57:45 -04004302
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004303 mutex_unlock(&sde_crtc->crtc_lock);
4304
Clarence Ip8f7366c2016-07-05 12:15:26 -04004305 return 0;
4306}
4307
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004308static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004309{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004310 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004311}
4312
Dhaval Patelf9245d62017-03-28 16:24:00 -07004313static ssize_t _sde_crtc_misr_setup(struct file *file,
4314 const char __user *user_buf, size_t count, loff_t *ppos)
4315{
4316 struct sde_crtc *sde_crtc;
4317 struct sde_crtc_mixer *m;
4318 int i = 0, rc;
4319 char buf[MISR_BUFF_SIZE + 1];
4320 u32 frame_count, enable;
4321 size_t buff_copy;
4322
4323 if (!file || !file->private_data)
4324 return -EINVAL;
4325
4326 sde_crtc = file->private_data;
4327 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
4328 if (copy_from_user(buf, user_buf, buff_copy)) {
4329 SDE_ERROR("buffer copy failed\n");
4330 return -EINVAL;
4331 }
4332
4333 buf[buff_copy] = 0; /* end of string */
4334
4335 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
4336 return -EINVAL;
4337
4338 rc = _sde_crtc_power_enable(sde_crtc, true);
4339 if (rc)
4340 return rc;
4341
4342 mutex_lock(&sde_crtc->crtc_lock);
4343 sde_crtc->misr_enable = enable;
Dhaval Patel010f5172017-08-01 22:40:09 -07004344 sde_crtc->misr_frame_count = frame_count;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004345 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Dhaval Patel010f5172017-08-01 22:40:09 -07004346 sde_crtc->misr_data[i] = 0;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004347 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004348 if (!m->hw_lm || !m->hw_lm->ops.setup_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004349 continue;
4350
4351 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
4352 }
4353 mutex_unlock(&sde_crtc->crtc_lock);
4354 _sde_crtc_power_enable(sde_crtc, false);
4355
4356 return count;
4357}
4358
4359static ssize_t _sde_crtc_misr_read(struct file *file,
4360 char __user *user_buff, size_t count, loff_t *ppos)
4361{
4362 struct sde_crtc *sde_crtc;
4363 struct sde_crtc_mixer *m;
4364 int i = 0, rc;
Dhaval Patel010f5172017-08-01 22:40:09 -07004365 u32 misr_status;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004366 ssize_t len = 0;
4367 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
4368
4369 if (*ppos)
4370 return 0;
4371
4372 if (!file || !file->private_data)
4373 return -EINVAL;
4374
4375 sde_crtc = file->private_data;
4376 rc = _sde_crtc_power_enable(sde_crtc, true);
4377 if (rc)
4378 return rc;
4379
4380 mutex_lock(&sde_crtc->crtc_lock);
4381 if (!sde_crtc->misr_enable) {
4382 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
4383 "disabled\n");
4384 goto buff_check;
4385 }
4386
4387 for (i = 0; i < sde_crtc->num_mixers; ++i) {
4388 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004389 if (!m->hw_lm || !m->hw_lm->ops.collect_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004390 continue;
4391
Dhaval Patel010f5172017-08-01 22:40:09 -07004392 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
4393 sde_crtc->misr_data[i] = misr_status ? misr_status :
4394 sde_crtc->misr_data[i];
Dhaval Patelf9245d62017-03-28 16:24:00 -07004395 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
4396 m->hw_lm->idx - LM_0);
4397 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
Dhaval Patel010f5172017-08-01 22:40:09 -07004398 sde_crtc->misr_data[i]);
Dhaval Patelf9245d62017-03-28 16:24:00 -07004399 }
4400
4401buff_check:
4402 if (count <= len) {
4403 len = 0;
4404 goto end;
4405 }
4406
4407 if (copy_to_user(user_buff, buf, len)) {
4408 len = -EFAULT;
4409 goto end;
4410 }
4411
4412 *ppos += len; /* increase offset */
4413
4414end:
4415 mutex_unlock(&sde_crtc->crtc_lock);
4416 _sde_crtc_power_enable(sde_crtc, false);
4417 return len;
4418}
4419
4420#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04004421static int __prefix ## _open(struct inode *inode, struct file *file) \
4422{ \
4423 return single_open(file, __prefix ## _show, inode->i_private); \
4424} \
4425static const struct file_operations __prefix ## _fops = { \
4426 .owner = THIS_MODULE, \
4427 .open = __prefix ## _open, \
4428 .release = single_release, \
4429 .read = seq_read, \
4430 .llseek = seq_lseek, \
4431}
4432
4433static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
4434{
4435 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04004436 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04004437 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004438 struct sde_crtc_res *res;
Alan Kwong310e9b02017-08-03 02:04:07 -04004439 struct sde_crtc_respool *rp;
Alan Kwong0230a102017-05-16 11:36:44 -07004440 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04004441
4442 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08004443 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08004444 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04004445 seq_printf(s, "core_clk_rate: %llu\n",
4446 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07004447 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
4448 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
4449 seq_printf(s, "bw_ctl[%s]: %llu\n",
4450 sde_power_handle_get_dbus_name(i),
4451 sde_crtc->cur_perf.bw_ctl[i]);
4452 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
4453 sde_power_handle_get_dbus_name(i),
4454 sde_crtc->cur_perf.max_per_pipe_ib[i]);
4455 }
Alan Kwong67a3f792016-11-01 23:16:53 -04004456
Alan Kwong310e9b02017-08-03 02:04:07 -04004457 mutex_lock(&sde_crtc->rp_lock);
4458 list_for_each_entry(rp, &sde_crtc->rp_head, rp_list) {
4459 seq_printf(s, "rp.%d: ", rp->sequence_id);
4460 list_for_each_entry(res, &rp->res_list, list)
4461 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
4462 res->type, res->tag, res->val,
4463 atomic_read(&res->refcount));
4464 seq_puts(s, "\n");
4465 }
4466 mutex_unlock(&sde_crtc->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004467
Alan Kwong67a3f792016-11-01 23:16:53 -04004468 return 0;
4469}
4470DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
4471
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004472static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004473{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004474 struct sde_crtc *sde_crtc;
4475 struct sde_kms *sde_kms;
4476
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004477 static const struct file_operations debugfs_status_fops = {
4478 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04004479 .read = seq_read,
4480 .llseek = seq_lseek,
4481 .release = single_release,
4482 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07004483 static const struct file_operations debugfs_misr_fops = {
4484 .open = simple_open,
4485 .read = _sde_crtc_misr_read,
4486 .write = _sde_crtc_misr_setup,
4487 };
Alan Kwong67a3f792016-11-01 23:16:53 -04004488
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004489 if (!crtc)
4490 return -EINVAL;
4491 sde_crtc = to_sde_crtc(crtc);
4492
4493 sde_kms = _sde_crtc_get_kms(crtc);
4494 if (!sde_kms)
4495 return -EINVAL;
4496
4497 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07004498 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004499 if (!sde_crtc->debugfs_root)
4500 return -ENOMEM;
4501
4502 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004503 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004504 sde_crtc->debugfs_root,
4505 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004506 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004507 sde_crtc->debugfs_root,
4508 &sde_crtc->base,
4509 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004510 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07004511 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004512
4513 return 0;
4514}
4515
4516static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4517{
4518 struct sde_crtc *sde_crtc;
4519
4520 if (!crtc)
4521 return;
4522 sde_crtc = to_sde_crtc(crtc);
4523 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004524}
Alan Kwong67a3f792016-11-01 23:16:53 -04004525#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004526static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04004527{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004528 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04004529}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004530
4531static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4532{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004533}
4534#endif /* CONFIG_DEBUG_FS */
4535
4536static int sde_crtc_late_register(struct drm_crtc *crtc)
4537{
4538 return _sde_crtc_init_debugfs(crtc);
4539}
4540
4541static void sde_crtc_early_unregister(struct drm_crtc *crtc)
4542{
4543 _sde_crtc_destroy_debugfs(crtc);
4544}
4545
4546static const struct drm_crtc_funcs sde_crtc_funcs = {
4547 .set_config = drm_atomic_helper_set_config,
4548 .destroy = sde_crtc_destroy,
4549 .page_flip = drm_atomic_helper_page_flip,
4550 .set_property = sde_crtc_set_property,
4551 .atomic_set_property = sde_crtc_atomic_set_property,
4552 .atomic_get_property = sde_crtc_atomic_get_property,
4553 .reset = sde_crtc_reset,
4554 .atomic_duplicate_state = sde_crtc_duplicate_state,
4555 .atomic_destroy_state = sde_crtc_destroy_state,
4556 .late_register = sde_crtc_late_register,
4557 .early_unregister = sde_crtc_early_unregister,
4558};
4559
4560static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
4561 .mode_fixup = sde_crtc_mode_fixup,
4562 .disable = sde_crtc_disable,
4563 .enable = sde_crtc_enable,
4564 .atomic_check = sde_crtc_atomic_check,
4565 .atomic_begin = sde_crtc_atomic_begin,
4566 .atomic_flush = sde_crtc_atomic_flush,
4567};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004568
Clarence Ipa18d4832017-03-13 12:35:44 -07004569static void _sde_crtc_event_cb(struct kthread_work *work)
4570{
4571 struct sde_crtc_event *event;
4572 struct sde_crtc *sde_crtc;
4573 unsigned long irq_flags;
4574
4575 if (!work) {
4576 SDE_ERROR("invalid work item\n");
4577 return;
4578 }
4579
4580 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004581
4582 /* set sde_crtc to NULL for static work structures */
4583 sde_crtc = event->sde_crtc;
4584 if (!sde_crtc)
4585 return;
4586
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004587 if (event->cb_func)
4588 event->cb_func(&sde_crtc->base, event->usr);
4589
Clarence Ipa18d4832017-03-13 12:35:44 -07004590 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4591 list_add_tail(&event->list, &sde_crtc->event_free_list);
4592 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4593}
4594
4595int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004596 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07004597{
4598 unsigned long irq_flags;
4599 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004600 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07004601 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004602 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07004603
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004604 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
4605 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07004606 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004607 }
Clarence Ipa18d4832017-03-13 12:35:44 -07004608 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004609 priv = crtc->dev->dev_private;
4610 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07004611
4612 /*
4613 * Obtain an event struct from the private cache. This event
4614 * queue may be called from ISR contexts, so use a private
4615 * cache to avoid calling any memory allocation functions.
4616 */
4617 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4618 if (!list_empty(&sde_crtc->event_free_list)) {
4619 event = list_first_entry(&sde_crtc->event_free_list,
4620 struct sde_crtc_event, list);
4621 list_del_init(&event->list);
4622 }
4623 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4624
4625 if (!event)
4626 return -ENOMEM;
4627
4628 /* populate event node */
4629 event->sde_crtc = sde_crtc;
4630 event->cb_func = func;
4631 event->usr = usr;
4632
4633 /* queue new event request */
4634 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004635 kthread_queue_work(&priv->event_thread[crtc_id].worker,
4636 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004637
4638 return 0;
4639}
4640
4641static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
4642{
4643 int i, rc = 0;
4644
4645 if (!sde_crtc) {
4646 SDE_ERROR("invalid crtc\n");
4647 return -EINVAL;
4648 }
4649
4650 spin_lock_init(&sde_crtc->event_lock);
4651
4652 INIT_LIST_HEAD(&sde_crtc->event_free_list);
4653 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
4654 list_add_tail(&sde_crtc->event_cache[i].list,
4655 &sde_crtc->event_free_list);
4656
Dhaval Patel5023c3c2017-08-22 12:40:11 -07004657 INIT_LIST_HEAD(&sde_crtc->retire_event_list);
4658 for (i = 0; i < ARRAY_SIZE(sde_crtc->retire_events); i++)
4659 INIT_LIST_HEAD(&sde_crtc->retire_events[i].list);
4660
Clarence Ipa18d4832017-03-13 12:35:44 -07004661 return rc;
4662}
4663
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004664/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04004665struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004666{
4667 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004668 struct sde_crtc *sde_crtc = NULL;
4669 struct msm_drm_private *priv = NULL;
4670 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07004671 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004672
Clarence Ip8f7366c2016-07-05 12:15:26 -04004673 priv = dev->dev_private;
4674 kms = to_sde_kms(priv->kms);
4675
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004676 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
4677 if (!sde_crtc)
4678 return ERR_PTR(-ENOMEM);
4679
4680 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004681 crtc->dev = dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004682
Clarence Ip7f70ce42017-03-20 06:53:46 -07004683 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04004684 spin_lock_init(&sde_crtc->spin_lock);
4685 atomic_set(&sde_crtc->frame_pending, 0);
4686
Alan Kwong310e9b02017-08-03 02:04:07 -04004687 mutex_init(&sde_crtc->rp_lock);
4688 INIT_LIST_HEAD(&sde_crtc->rp_head);
4689
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07004690 init_completion(&sde_crtc->frame_done_comp);
4691
Alan Kwong628d19e2016-10-31 13:50:13 -04004692 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004693 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04004694 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
4695 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
4696 list_add(&sde_crtc->frame_events[i].list,
4697 &sde_crtc->frame_event_list);
4698 kthread_init_work(&sde_crtc->frame_events[i].work,
4699 sde_crtc_frame_event_work);
4700 }
4701
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07004702 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
4703 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004704
4705 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004706 plane->crtc = crtc;
4707
Clarence Ip8f7366c2016-07-05 12:15:26 -04004708 /* save user friendly CRTC name for later */
4709 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
4710
Clarence Ipa18d4832017-03-13 12:35:44 -07004711 /* initialize event handling */
4712 rc = _sde_crtc_init_events(sde_crtc);
4713 if (rc) {
4714 drm_crtc_cleanup(crtc);
4715 kfree(sde_crtc);
4716 return ERR_PTR(rc);
4717 }
4718
Clarence Ip9a74a442016-08-25 18:29:03 -04004719 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04004720 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04004721
Clarence Ip7a753bb2016-07-07 11:47:44 -04004722 /* create CRTC properties */
4723 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
4724 priv->crtc_property, sde_crtc->property_data,
4725 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
4726 sizeof(struct sde_crtc_state));
4727
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004728 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004729
4730 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004731 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004732 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004733
Dhaval Patelec10fad2016-08-22 14:40:48 -07004734 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004735 return crtc;
4736}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004737
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004738static int _sde_crtc_event_enable(struct sde_kms *kms,
4739 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004740{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004741 struct sde_crtc *crtc = NULL;
4742 struct sde_crtc_irq_info *node;
4743 struct msm_drm_private *priv;
4744 unsigned long flags;
4745 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004746 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004747
4748 crtc = to_sde_crtc(crtc_drm);
4749 spin_lock_irqsave(&crtc->spin_lock, flags);
4750 list_for_each_entry(node, &crtc->user_event_list, list) {
4751 if (node->event == event) {
4752 found = true;
4753 break;
4754 }
4755 }
4756 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4757
4758 /* event already enabled */
4759 if (found)
4760 return 0;
4761
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004762 node = NULL;
4763 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
4764 if (custom_events[i].event == event &&
4765 custom_events[i].func) {
4766 node = kzalloc(sizeof(*node), GFP_KERNEL);
4767 if (!node)
4768 return -ENOMEM;
4769 node->event = event;
4770 INIT_LIST_HEAD(&node->list);
4771 node->func = custom_events[i].func;
4772 node->event = event;
4773 break;
4774 }
4775 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004776
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004777 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004778 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004779 return -EINVAL;
4780 }
4781
4782 priv = kms->dev->dev_private;
4783 ret = 0;
4784 if (crtc_drm->enabled) {
4785 sde_power_resource_enable(&priv->phandle, kms->core_client,
4786 true);
Xu Yang37752282017-08-21 13:50:23 +08004787 INIT_LIST_HEAD(&node->irq.list);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004788 ret = node->func(crtc_drm, true, &node->irq);
4789 sde_power_resource_enable(&priv->phandle, kms->core_client,
4790 false);
4791 }
4792
4793 if (!ret) {
4794 spin_lock_irqsave(&crtc->spin_lock, flags);
4795 list_add_tail(&node->list, &crtc->user_event_list);
4796 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4797 } else {
4798 kfree(node);
4799 }
4800
4801 return ret;
4802}
4803
4804static int _sde_crtc_event_disable(struct sde_kms *kms,
4805 struct drm_crtc *crtc_drm, u32 event)
4806{
4807 struct sde_crtc *crtc = NULL;
4808 struct sde_crtc_irq_info *node = NULL;
4809 struct msm_drm_private *priv;
4810 unsigned long flags;
4811 bool found = false;
4812 int ret;
4813
4814 crtc = to_sde_crtc(crtc_drm);
4815 spin_lock_irqsave(&crtc->spin_lock, flags);
4816 list_for_each_entry(node, &crtc->user_event_list, list) {
4817 if (node->event == event) {
4818 list_del(&node->list);
4819 found = true;
4820 break;
4821 }
4822 }
4823 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4824
4825 /* event already disabled */
4826 if (!found)
4827 return 0;
4828
4829 /**
4830 * crtc is disabled interrupts are cleared remove from the list,
4831 * no need to disable/de-register.
4832 */
4833 if (!crtc_drm->enabled) {
4834 kfree(node);
4835 return 0;
4836 }
4837 priv = kms->dev->dev_private;
4838 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
4839 ret = node->func(crtc_drm, false, &node->irq);
4840 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
4841 return ret;
4842}
4843
4844int sde_crtc_register_custom_event(struct sde_kms *kms,
4845 struct drm_crtc *crtc_drm, u32 event, bool en)
4846{
4847 struct sde_crtc *crtc = NULL;
4848 int ret;
4849
4850 crtc = to_sde_crtc(crtc_drm);
4851 if (!crtc || !kms || !kms->dev) {
4852 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
4853 kms, ((kms) ? (kms->dev) : NULL));
4854 return -EINVAL;
4855 }
4856
4857 if (en)
4858 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
4859 else
4860 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
4861
4862 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004863}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07004864
4865static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
4866 bool en, struct sde_irq_callback *irq)
4867{
4868 return 0;
4869}
Benjamin Chan90139102017-06-21 16:00:39 -04004870
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05304871static int sde_crtc_idle_interrupt_handler(struct drm_crtc *crtc_drm,
4872 bool en, struct sde_irq_callback *irq)
4873{
4874 return 0;
4875}