blob: 30cf3df2a59f2dbf70e8b55f0c95940fbc456526 [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_cfg.rot_op_mode = SDE_CTL_ROT_OP_MODE_OFFLINE;
1285 cstate->sbuf_prefill_line = 0;
1286
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 Ip7eb90452017-05-23 11:41:19 -04001301 cstate->sbuf_cfg.rot_op_mode =
1302 SDE_CTL_ROT_OP_MODE_INLINE_SYNC;
1303 if (prefill > cstate->sbuf_prefill_line)
1304 cstate->sbuf_prefill_line = prefill;
Alan Kwong4dd64c82017-02-04 18:41:51 -08001305
Clarence Ip7e5f0002017-05-29 18:46:56 -04001306 sde_plane_get_ctl_flush(plane, ctl, &flush_mask, &flush_tmp);
Dhaval Patel44f12472016-08-29 12:19:47 -07001307
Clarence Ip7e5f0002017-05-29 18:46:56 -04001308 /* persist rotator flush bit(s) for one more commit */
1309 flush_mask |= cstate->sbuf_flush_mask | flush_tmp;
1310 flush_sbuf |= flush_tmp;
Dhaval Patel48c76022016-09-01 17:51:23 -07001311
1312 SDE_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001313 crtc->base.id,
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001314 pstate->stage,
1315 plane->base.id,
1316 sde_plane_pipe(plane) - SSPP_VIG0,
Dhaval Patel6c666622017-03-21 23:02:59 -07001317 state->fb ? state->fb->base.id : -1);
Dhaval Patel44f12472016-08-29 12:19:47 -07001318
Dhaval Patel48c76022016-09-01 17:51:23 -07001319 format = to_sde_format(msm_framebuffer_format(pstate->base.fb));
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07001320 if (!format) {
1321 SDE_ERROR("invalid format\n");
1322 return;
1323 }
1324
Dhaval Patel572cfd22017-06-12 19:33:39 -07001325 if (pstate->stage == SDE_STAGE_BASE && format->alpha_enable)
1326 bg_alpha_enable = true;
Dhaval Patel44f12472016-08-29 12:19:47 -07001327
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001328 SDE_EVT32(DRMID(crtc), DRMID(plane),
1329 state->fb ? state->fb->base.id : -1,
1330 state->src_x >> 16, state->src_y >> 16,
1331 state->src_w >> 16, state->src_h >> 16,
1332 state->crtc_x, state->crtc_y,
Clarence Ip7eb90452017-05-23 11:41:19 -04001333 state->crtc_w, state->crtc_h,
Clarence Ipeb39cce2017-07-19 14:12:43 -04001334 flush_tmp ? cstate->sbuf_cfg.rot_op_mode :
1335 SDE_CTL_ROT_OP_MODE_OFFLINE);
Dhaval Patel6c666622017-03-21 23:02:59 -07001336
Dhaval Patel572cfd22017-06-12 19:33:39 -07001337 stage_idx = zpos_cnt[pstate->stage]++;
1338 stage_cfg->stage[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001339 sde_plane_pipe(plane);
Dhaval Patel572cfd22017-06-12 19:33:39 -07001340 stage_cfg->multirect_index[pstate->stage][stage_idx] =
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001341 pstate->multirect_index;
1342
Dhaval Patel572cfd22017-06-12 19:33:39 -07001343 SDE_EVT32(DRMID(crtc), DRMID(plane), stage_idx,
1344 sde_plane_pipe(plane) - SSPP_VIG0, pstate->stage,
1345 pstate->multirect_index, pstate->multirect_mode,
1346 format->base.pixel_format, fb ? fb->modifier[0] : 0);
1347
1348 /* blend config update */
1349 for (lm_idx = 0; lm_idx < sde_crtc->num_mixers; lm_idx++) {
1350 _sde_crtc_setup_blend_cfg(mixer + lm_idx, pstate,
1351 format);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001352 mixer[lm_idx].flush_mask |= flush_mask;
1353
Dhaval Patel572cfd22017-06-12 19:33:39 -07001354 if (bg_alpha_enable && !format->alpha_enable)
1355 mixer[lm_idx].mixer_op_mode = 0;
1356 else
1357 mixer[lm_idx].mixer_op_mode |=
Dhaval Patel48c76022016-09-01 17:51:23 -07001358 1 << pstate->stage;
Dhaval Patel48c76022016-09-01 17:51:23 -07001359 }
Dhaval Patel44f12472016-08-29 12:19:47 -07001360 }
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001361
Clarence Ip7e5f0002017-05-29 18:46:56 -04001362 cstate->sbuf_flush_mask = flush_sbuf;
1363
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001364 if (lm && lm->ops.setup_dim_layer) {
1365 cstate = to_sde_crtc_state(crtc->state);
1366 for (i = 0; i < cstate->num_dim_layers; i++)
1367 _sde_crtc_setup_dim_layer_cfg(crtc, sde_crtc,
1368 mixer, &cstate->dim_layer[i]);
1369 }
Alan Kwong4dd64c82017-02-04 18:41:51 -08001370
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05001371 _sde_crtc_program_lm_output_roi(crtc);
Dhaval Patel44f12472016-08-29 12:19:47 -07001372}
1373
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001374static void _sde_crtc_swap_mixers_for_right_partial_update(
1375 struct drm_crtc *crtc)
1376{
1377 struct sde_crtc *sde_crtc;
1378 struct sde_crtc_state *cstate;
1379 struct drm_encoder *drm_enc;
1380 bool is_right_only;
1381 bool encoder_in_dsc_merge = false;
1382
1383 if (!crtc || !crtc->state)
1384 return;
1385
1386 sde_crtc = to_sde_crtc(crtc);
1387 cstate = to_sde_crtc_state(crtc->state);
1388
1389 if (sde_crtc->num_mixers != CRTC_DUAL_MIXERS)
1390 return;
1391
1392 drm_for_each_encoder(drm_enc, crtc->dev) {
1393 if (drm_enc->crtc == crtc &&
1394 sde_encoder_is_dsc_merge(drm_enc)) {
1395 encoder_in_dsc_merge = true;
1396 break;
1397 }
1398 }
1399
1400 /**
1401 * For right-only partial update with DSC merge, we swap LM0 & LM1.
1402 * This is due to two reasons:
1403 * - On 8996, there is a DSC HW requirement that in DSC Merge Mode,
1404 * the left DSC must be used, right DSC cannot be used alone.
1405 * For right-only partial update, this means swap layer mixers to map
1406 * Left LM to Right INTF. On later HW this was relaxed.
1407 * - In DSC Merge mode, the physical encoder has already registered
1408 * PP0 as the master, to switch to right-only we would have to
1409 * reprogram to be driven by PP1 instead.
1410 * To support both cases, we prefer to support the mixer swap solution.
1411 */
1412 if (!encoder_in_dsc_merge)
1413 return;
1414
1415 is_right_only = sde_kms_rect_is_null(&cstate->lm_roi[0]) &&
1416 !sde_kms_rect_is_null(&cstate->lm_roi[1]);
1417
1418 if (is_right_only && !sde_crtc->mixers_swapped) {
1419 /* right-only update swap mixers */
1420 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1421 sde_crtc->mixers_swapped = true;
1422 } else if (!is_right_only && sde_crtc->mixers_swapped) {
1423 /* left-only or full update, swap back */
1424 swap(sde_crtc->mixers[0], sde_crtc->mixers[1]);
1425 sde_crtc->mixers_swapped = false;
1426 }
1427
1428 SDE_DEBUG("%s: right_only %d swapped %d, mix0->lm%d, mix1->lm%d\n",
1429 sde_crtc->name, is_right_only, sde_crtc->mixers_swapped,
1430 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1431 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1432 SDE_EVT32(DRMID(crtc), is_right_only, sde_crtc->mixers_swapped,
1433 sde_crtc->mixers[0].hw_lm->idx - LM_0,
1434 sde_crtc->mixers[1].hw_lm->idx - LM_0);
1435}
1436
Clarence Ipd9f9fa62016-09-09 13:42:32 -04001437/**
1438 * _sde_crtc_blend_setup - configure crtc mixers
1439 * @crtc: Pointer to drm crtc structure
1440 */
1441static void _sde_crtc_blend_setup(struct drm_crtc *crtc)
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001442{
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001443 struct sde_crtc *sde_crtc;
1444 struct sde_crtc_state *sde_crtc_state;
1445 struct sde_crtc_mixer *mixer;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001446 struct sde_hw_ctl *ctl;
1447 struct sde_hw_mixer *lm;
Dhaval Patel44f12472016-08-29 12:19:47 -07001448
1449 int i;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001450
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001451 if (!crtc)
1452 return;
1453
1454 sde_crtc = to_sde_crtc(crtc);
1455 sde_crtc_state = to_sde_crtc_state(crtc->state);
1456 mixer = sde_crtc->mixers;
1457
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001458 SDE_DEBUG("%s\n", sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001459
Dhaval Patel48c76022016-09-01 17:51:23 -07001460 if (sde_crtc->num_mixers > CRTC_DUAL_MIXERS) {
1461 SDE_ERROR("invalid number mixers: %d\n", sde_crtc->num_mixers);
1462 return;
1463 }
1464
1465 for (i = 0; i < sde_crtc->num_mixers; i++) {
1466 if (!mixer[i].hw_lm || !mixer[i].hw_ctl) {
1467 SDE_ERROR("invalid lm or ctl assigned to mixer\n");
1468 return;
1469 }
1470 mixer[i].mixer_op_mode = 0;
1471 mixer[i].flush_mask = 0;
Lloyd Atkinsone5ec30d2016-08-23 14:32:32 -04001472 if (mixer[i].hw_ctl->ops.clear_all_blendstages)
1473 mixer[i].hw_ctl->ops.clear_all_blendstages(
1474 mixer[i].hw_ctl);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08001475
1476 /* clear dim_layer settings */
1477 lm = mixer[i].hw_lm;
1478 if (lm->ops.clear_dim_layer)
1479 lm->ops.clear_dim_layer(lm);
Dhaval Patel48c76022016-09-01 17:51:23 -07001480 }
1481
Lloyd Atkinson094780d2017-04-24 17:25:08 -04001482 _sde_crtc_swap_mixers_for_right_partial_update(crtc);
1483
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001484 /* initialize stage cfg */
Clarence Ip8f7366c2016-07-05 12:15:26 -04001485 memset(&sde_crtc->stage_cfg, 0, sizeof(struct sde_hw_stage_cfg));
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001486
Dhaval Patel48c76022016-09-01 17:51:23 -07001487 _sde_crtc_blend_setup_mixer(crtc, sde_crtc, mixer);
1488
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001489 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001490 const struct sde_rect *lm_roi = &sde_crtc_state->lm_roi[i];
1491
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001492 ctl = mixer[i].hw_ctl;
1493 lm = mixer[i].hw_lm;
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001494
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05001495 if (sde_kms_rect_is_null(lm_roi)) {
1496 SDE_DEBUG(
1497 "%s: lm%d leave ctl%d mask 0 since null roi\n",
1498 sde_crtc->name, lm->idx - LM_0,
1499 ctl->idx - CTL_0);
1500 continue;
1501 }
1502
Dhaval Patel48c76022016-09-01 17:51:23 -07001503 lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001504
Dhaval Patel48c76022016-09-01 17:51:23 -07001505 mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
Abhijit Kulkarni71002ba2016-06-24 18:36:28 -04001506 mixer[i].hw_lm->idx);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04001507
1508 /* stage config flush mask */
Dhaval Patel48c76022016-09-01 17:51:23 -07001509 ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
1510
Clarence Ip8e69ad02016-12-09 09:43:57 -05001511 SDE_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
1512 mixer[i].hw_lm->idx - LM_0,
1513 mixer[i].mixer_op_mode,
1514 ctl->idx - CTL_0,
1515 mixer[i].flush_mask);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001516
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001517 ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
Dhaval Patel572cfd22017-06-12 19:33:39 -07001518 &sde_crtc->stage_cfg);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001519 }
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04001520
1521 _sde_crtc_program_lm_output_roi(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04001522}
1523
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001524static int _sde_crtc_find_plane_fb_modes(struct drm_crtc_state *state,
1525 uint32_t *fb_ns,
1526 uint32_t *fb_sec,
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001527 uint32_t *fb_sec_dir)
1528{
1529 struct drm_plane *plane;
1530 const struct drm_plane_state *pstate;
1531 struct sde_plane_state *sde_pstate;
1532 uint32_t mode = 0;
1533 int rc;
1534
1535 if (!state) {
1536 SDE_ERROR("invalid state\n");
1537 return -EINVAL;
1538 }
1539
1540 *fb_ns = 0;
1541 *fb_sec = 0;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001542 *fb_sec_dir = 0;
1543 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
1544 if (IS_ERR_OR_NULL(pstate)) {
1545 rc = PTR_ERR(pstate);
1546 SDE_ERROR("crtc%d failed to get plane%d state%d\n",
1547 state->crtc->base.id,
1548 plane->base.id, rc);
1549 return rc;
1550 }
1551 sde_pstate = to_sde_plane_state(pstate);
1552 mode = sde_plane_get_property(sde_pstate,
1553 PLANE_PROP_FB_TRANSLATION_MODE);
1554 switch (mode) {
1555 case SDE_DRM_FB_NON_SEC:
1556 (*fb_ns)++;
1557 break;
1558 case SDE_DRM_FB_SEC:
1559 (*fb_sec)++;
1560 break;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001561 case SDE_DRM_FB_SEC_DIR_TRANS:
1562 (*fb_sec_dir)++;
1563 break;
1564 default:
1565 SDE_ERROR("Error: Plane[%d], fb_trans_mode:%d",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001566 plane->base.id, mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001567 return -EINVAL;
1568 }
1569 }
1570 return 0;
1571}
1572
1573/**
1574 * sde_crtc_get_secure_transition_ops - determines the operations that
1575 * need to be performed before transitioning to secure state
1576 * This function should be called after swapping the new state
1577 * @crtc: Pointer to drm crtc structure
1578 * Returns the bitmask of operations need to be performed, -Error in
1579 * case of error cases
1580 */
1581int sde_crtc_get_secure_transition_ops(struct drm_crtc *crtc,
1582 struct drm_crtc_state *old_crtc_state,
1583 bool old_valid_fb)
1584{
1585 struct drm_plane *plane;
1586 struct drm_encoder *encoder;
1587 struct sde_crtc *sde_crtc;
1588 struct sde_crtc_state *cstate;
1589 struct sde_crtc_smmu_state_data *smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001590 uint32_t translation_mode = 0, secure_level;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001591 int ops = 0;
1592 bool post_commit = false;
1593
1594 if (!crtc || !crtc->state) {
1595 SDE_ERROR("invalid crtc\n");
1596 return -EINVAL;
1597 }
1598
1599 sde_crtc = to_sde_crtc(crtc);
1600 cstate = to_sde_crtc_state(crtc->state);
1601 smmu_state = &sde_crtc->smmu_state;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001602 secure_level = sde_crtc_get_secure_level(crtc, crtc->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001603
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001604 SDE_DEBUG("crtc%d, secure_level%d old_valid_fb%d\n",
1605 crtc->base.id, secure_level, old_valid_fb);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001606
1607 /**
1608 * SMMU operations need to be delayed in case of
1609 * video mode panels when switching back to non_secure
1610 * mode
1611 */
1612 drm_for_each_encoder(encoder, crtc->dev) {
1613 if (encoder->crtc != crtc)
1614 continue;
1615
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05301616 post_commit &= sde_encoder_check_mode(encoder,
1617 MSM_DISPLAY_CAP_VID_MODE);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001618 }
1619
1620 drm_atomic_crtc_for_each_plane(plane, crtc) {
1621 if (!plane->state)
1622 continue;
1623
1624 translation_mode = sde_plane_get_property(
1625 to_sde_plane_state(plane->state),
1626 PLANE_PROP_FB_TRANSLATION_MODE);
1627 if (translation_mode > SDE_DRM_FB_SEC_DIR_TRANS) {
1628 SDE_ERROR("crtc%d, invalid translation_mode%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001629 crtc->base.id, translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001630 return -EINVAL;
1631 }
1632
1633 /**
1634 * we can break if we find sec_fir or non_sec_dir
1635 * plane
1636 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001637 if (translation_mode == SDE_DRM_FB_SEC_DIR_TRANS)
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001638 break;
1639 }
1640
1641 switch (translation_mode) {
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001642 case SDE_DRM_FB_SEC_DIR_TRANS:
1643 /* secure display usecase */
1644 if ((smmu_state->state == ATTACHED) &&
1645 (secure_level == SDE_DRM_SEC_ONLY)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001646 smmu_state->state = DETACH_ALL_REQ;
1647 smmu_state->transition_type = PRE_COMMIT;
1648 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1649 if (old_valid_fb) {
1650 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1651 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1652 }
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001653 /* secure camera usecase */
1654 } else if (smmu_state->state == ATTACHED) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001655 smmu_state->state = DETACH_SEC_REQ;
1656 smmu_state->transition_type = PRE_COMMIT;
1657 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1658 }
1659 break;
1660 case SDE_DRM_FB_SEC:
1661 case SDE_DRM_FB_NON_SEC:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001662 if ((smmu_state->state == DETACHED_SEC) ||
1663 (smmu_state->state == DETACH_SEC_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001664 smmu_state->state = ATTACH_SEC_REQ;
1665 smmu_state->transition_type = post_commit ?
1666 POST_COMMIT : PRE_COMMIT;
1667 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE;
1668 if (translation_mode == SDE_DRM_FB_SEC)
1669 ops |= SDE_KMS_OPS_PREPARE_PLANE_FB;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001670 if (old_valid_fb)
1671 ops |= SDE_KMS_OPS_WAIT_FOR_TX_DONE;
1672 } else if ((smmu_state->state == DETACHED) ||
1673 (smmu_state->state == DETACH_ALL_REQ)) {
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001674 smmu_state->state = ATTACH_ALL_REQ;
1675 smmu_state->transition_type = post_commit ?
1676 POST_COMMIT : PRE_COMMIT;
1677 ops |= SDE_KMS_OPS_CRTC_SECURE_STATE_CHANGE |
1678 SDE_KMS_OPS_PREPARE_PLANE_FB;
1679 if (old_valid_fb)
1680 ops |= (SDE_KMS_OPS_WAIT_FOR_TX_DONE |
1681 SDE_KMS_OPS_CLEANUP_PLANE_FB);
1682 }
1683 break;
1684 default:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001685 SDE_ERROR("invalid plane fb_mode:%d\n", translation_mode);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001686 ops = 0;
1687 return -EINVAL;
1688 }
1689
1690 SDE_DEBUG("SMMU State:%d, type:%d ops:%x\n", smmu_state->state,
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001691 smmu_state->transition_type, ops);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001692 return ops;
1693}
1694
1695/**
1696 * _sde_crtc_scm_call - makes secure channel call to switch the VMIDs
1697 * @vimd: switch the stage 2 translation to this VMID.
1698 */
1699static int _sde_crtc_scm_call(int vmid)
1700{
1701 struct scm_desc desc = {0};
1702 uint32_t num_sids;
1703 uint32_t *sec_sid;
1704 uint32_t mem_protect_sd_ctrl_id = MEM_PROTECT_SD_CTRL_SWITCH;
1705 int ret = 0;
1706
1707 /* This info should be queried from catalog */
1708 num_sids = SEC_SID_CNT;
1709 sec_sid = kcalloc(num_sids, sizeof(uint32_t), GFP_KERNEL);
1710 if (!sec_sid)
1711 return -ENOMEM;
1712
1713 /**
1714 * derive this info from device tree/catalog, this is combination of
1715 * smr mask and SID for secure
1716 */
1717 sec_sid[0] = SEC_SID_MASK_0;
1718 sec_sid[1] = SEC_SID_MASK_1;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001719 dmac_flush_range(sec_sid, sec_sid + num_sids);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001720
1721 SDE_DEBUG("calling scm_call for vmid %d", vmid);
1722
1723 desc.arginfo = SCM_ARGS(4, SCM_VAL, SCM_RW, SCM_VAL, SCM_VAL);
1724 desc.args[0] = MDP_DEVICE_ID;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001725 desc.args[1] = SCM_BUFFER_PHYS(sec_sid);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001726 desc.args[2] = sizeof(uint32_t) * num_sids;
1727 desc.args[3] = vmid;
1728
1729 ret = scm_call2(SCM_SIP_FNID(SCM_SVC_MP,
1730 mem_protect_sd_ctrl_id), &desc);
1731 if (ret) {
1732 SDE_ERROR("Error:scm_call2, vmid (%lld): ret%d\n",
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001733 desc.args[3], ret);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001734 }
1735
1736 kfree(sec_sid);
1737 return ret;
1738}
1739
1740/**
1741 * sde_crtc_secure_ctrl - Initiates the operations to swtich between secure
1742 * and non-secure mode
1743 * @crtc: Pointer to crtc
1744 * @post_commit: if this operation is triggered after commit
1745 */
1746int sde_crtc_secure_ctrl(struct drm_crtc *crtc, bool post_commit)
1747{
1748 struct sde_crtc *sde_crtc;
1749 struct sde_crtc_state *cstate;
1750 struct sde_kms *sde_kms;
1751 struct sde_crtc_smmu_state_data *smmu_state;
1752 int ret = 0;
1753 int old_smmu_state;
1754
1755 if (!crtc || !crtc->state) {
1756 SDE_ERROR("invalid crtc\n");
1757 return -EINVAL;
1758 }
1759
1760 sde_kms = _sde_crtc_get_kms(crtc);
1761 if (!sde_kms) {
1762 SDE_ERROR("invalid kms\n");
1763 return -EINVAL;
1764 }
1765
1766 sde_crtc = to_sde_crtc(crtc);
1767 cstate = to_sde_crtc_state(crtc->state);
1768 smmu_state = &sde_crtc->smmu_state;
1769 old_smmu_state = smmu_state->state;
1770
1771 if ((!smmu_state->transition_type) ||
1772 ((smmu_state->transition_type == POST_COMMIT) && !post_commit))
1773 /* Bail out */
1774 return 0;
1775
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001776 /* Secure UI use case enable */
1777 switch (smmu_state->state) {
1778 case DETACH_ALL_REQ:
1779 /* detach_all_contexts */
1780 ret = sde_kms_mmu_detach(sde_kms, false);
1781 if (ret) {
1782 SDE_ERROR("crtc: %d, failed to detach %d\n",
1783 crtc->base.id, ret);
1784 goto error;
1785 }
1786
1787 ret = _sde_crtc_scm_call(VMID_CP_SEC_DISPLAY);
1788 if (ret)
1789 goto error;
1790
1791 smmu_state->state = DETACHED;
1792 break;
1793 /* Secure UI use case disable */
1794 case ATTACH_ALL_REQ:
1795 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1796 if (ret)
1797 goto error;
1798
1799 /* attach_all_contexts */
1800 ret = sde_kms_mmu_attach(sde_kms, false);
1801 if (ret) {
1802 SDE_ERROR("crtc: %d, failed to attach %d\n",
1803 crtc->base.id,
1804 ret);
1805 goto error;
1806 }
1807
1808 smmu_state->state = ATTACHED;
1809
1810 break;
1811 /* Secure preview enable */
1812 case DETACH_SEC_REQ:
1813 /* detach secure_context */
1814 ret = sde_kms_mmu_detach(sde_kms, true);
1815 if (ret) {
1816 SDE_ERROR("crtc: %d, failed to detach %d\n",
1817 crtc->base.id,
1818 ret);
1819 goto error;
1820 }
1821
1822 smmu_state->state = DETACHED_SEC;
1823 ret = _sde_crtc_scm_call(VMID_CP_CAMERA_PREVIEW);
1824 if (ret)
1825 goto error;
1826
1827 break;
1828
1829 /* Secure preview disable */
1830 case ATTACH_SEC_REQ:
1831 ret = _sde_crtc_scm_call(VMID_CP_PIXEL);
1832 if (ret)
1833 goto error;
1834
1835 ret = sde_kms_mmu_attach(sde_kms, true);
1836 if (ret) {
1837 SDE_ERROR("crtc: %d, failed to attach %d\n",
1838 crtc->base.id,
1839 ret);
1840 goto error;
1841 }
1842 smmu_state->state = ATTACHED;
1843 break;
1844 default:
1845 break;
1846 }
1847
1848 SDE_DEBUG("crtc: %d, old_state %d new_state %d\n", crtc->base.id,
1849 old_smmu_state,
1850 smmu_state->state);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001851 smmu_state->transition_type = NONE;
1852
1853error:
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07001854 smmu_state->transition_error = ret ? true : false;
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07001855 return ret;
1856}
1857
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001858void sde_crtc_prepare_commit(struct drm_crtc *crtc,
1859 struct drm_crtc_state *old_state)
Clarence Ip24f80662016-06-13 19:05:32 -04001860{
1861 struct sde_crtc *sde_crtc;
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001862 struct sde_crtc_state *cstate;
1863 struct drm_connector *conn;
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001864 struct sde_crtc_retire_event *retire_event = NULL;
1865 unsigned long flags;
1866 int i;
Clarence Ip24f80662016-06-13 19:05:32 -04001867
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001868 if (!crtc || !crtc->state) {
Clarence Ip24f80662016-06-13 19:05:32 -04001869 SDE_ERROR("invalid crtc\n");
1870 return;
1871 }
1872
1873 sde_crtc = to_sde_crtc(crtc);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001874 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel6c666622017-03-21 23:02:59 -07001875 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ip24f80662016-06-13 19:05:32 -04001876
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001877 /* identify connectors attached to this crtc */
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001878 cstate->num_connectors = 0;
1879
1880 drm_for_each_connector(conn, crtc->dev)
1881 if (conn->state && conn->state->crtc == crtc &&
1882 cstate->num_connectors < MAX_CONNECTORS) {
1883 cstate->connectors[cstate->num_connectors++] = conn;
1884 sde_connector_prepare_fence(conn);
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001885 }
1886
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001887 for (i = 0; i < SDE_CRTC_FRAME_EVENT_SIZE; i++) {
1888 retire_event = &sde_crtc->retire_events[i];
1889 if (list_empty(&retire_event->list))
1890 break;
1891 retire_event = NULL;
1892 }
1893
1894 if (retire_event) {
1895 retire_event->num_connectors = cstate->num_connectors;
1896 for (i = 0; i < cstate->num_connectors; i++)
1897 retire_event->connectors[i] = cstate->connectors[i];
1898
1899 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1900 list_add_tail(&retire_event->list,
1901 &sde_crtc->retire_event_list);
1902 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
1903 } else {
1904 SDE_ERROR("crtc%d retire event overflow\n", crtc->base.id);
1905 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_ERROR);
1906 }
1907
Clarence Ip0d0e96d2016-10-24 18:13:13 -04001908 /* prepare main output fence */
Clarence Ip24f80662016-06-13 19:05:32 -04001909 sde_fence_prepare(&sde_crtc->output_fence);
1910}
1911
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001912/**
1913 * _sde_crtc_complete_flip - signal pending page_flip events
1914 * Any pending vblank events are added to the vblank_event_list
1915 * so that the next vblank interrupt shall signal them.
1916 * However PAGE_FLIP events are not handled through the vblank_event_list.
1917 * This API signals any pending PAGE_FLIP events requested through
1918 * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the sde_crtc->event.
1919 * if file!=NULL, this is preclose potential cancel-flip path
1920 * @crtc: Pointer to drm crtc structure
1921 * @file: Pointer to drm file
1922 */
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001923static void _sde_crtc_complete_flip(struct drm_crtc *crtc,
1924 struct drm_file *file)
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001925{
1926 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1927 struct drm_device *dev = crtc->dev;
1928 struct drm_pending_vblank_event *event;
1929 unsigned long flags;
1930
1931 spin_lock_irqsave(&dev->event_lock, flags);
1932 event = sde_crtc->event;
1933 if (event) {
1934 /* if regular vblank case (!file) or if cancel-flip from
1935 * preclose on file that requested flip, then send the
1936 * event:
1937 */
1938 if (!file || (event->base.file_priv == file)) {
1939 sde_crtc->event = NULL;
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04001940 DRM_DEBUG_VBL("%s: send event: %pK\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07001941 sde_crtc->name, event);
Dhaval Patela5f75952017-07-25 11:17:41 -07001942 SDE_EVT32_VERBOSE(DRMID(crtc));
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001943 drm_crtc_send_vblank_event(crtc, event);
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001944 }
1945 }
1946 spin_unlock_irqrestore(&dev->event_lock, flags);
1947}
1948
Alan Kwong3e985f02017-02-12 15:08:44 -08001949enum sde_intf_mode sde_crtc_get_intf_mode(struct drm_crtc *crtc)
1950{
1951 struct drm_encoder *encoder;
1952
1953 if (!crtc || !crtc->dev) {
1954 SDE_ERROR("invalid crtc\n");
1955 return INTF_MODE_NONE;
1956 }
1957
1958 drm_for_each_encoder(encoder, crtc->dev)
1959 if (encoder->crtc == crtc)
1960 return sde_encoder_get_intf_mode(encoder);
1961
1962 return INTF_MODE_NONE;
1963}
1964
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001965static void sde_crtc_vblank_cb(void *data)
1966{
1967 struct drm_crtc *crtc = (struct drm_crtc *)data;
Alan Kwong07da0982016-11-04 12:57:45 -04001968 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
1969
1970 /* keep statistics on vblank callback - with auto reset via debugfs */
1971 if (ktime_equal(sde_crtc->vblank_cb_time, ktime_set(0, 0)))
1972 sde_crtc->vblank_cb_time = ktime_get();
1973 else
1974 sde_crtc->vblank_cb_count++;
Abhinav Kumarf2e94b52017-02-09 20:27:24 -08001975 _sde_crtc_complete_flip(crtc, NULL);
Lloyd Atkinsonac933642016-09-14 11:52:00 -04001976 drm_crtc_handle_vblank(crtc);
Lloyd Atkinson9eabe7a2016-09-14 13:39:15 -04001977 DRM_DEBUG_VBL("crtc%d\n", crtc->base.id);
Dhaval Patel6c666622017-03-21 23:02:59 -07001978 SDE_EVT32_VERBOSE(DRMID(crtc));
Abhijit Kulkarni40e38162016-06-26 22:12:09 -04001979}
1980
Dhaval Patel5023c3c2017-08-22 12:40:11 -07001981static void _sde_crtc_retire_event(struct drm_crtc *crtc, ktime_t ts)
1982{
1983 struct sde_crtc_retire_event *retire_event;
1984 struct sde_crtc *sde_crtc;
1985 unsigned long flags;
1986 int i;
1987
1988 if (!crtc) {
1989 SDE_ERROR("invalid param\n");
1990 return;
1991 }
1992
1993 sde_crtc = to_sde_crtc(crtc);
1994 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
1995 retire_event = list_first_entry_or_null(&sde_crtc->retire_event_list,
1996 struct sde_crtc_retire_event, list);
1997 if (retire_event)
1998 list_del_init(&retire_event->list);
1999 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2000
2001 if (!retire_event) {
2002 SDE_ERROR("crtc%d retire event without kickoff\n",
2003 crtc->base.id);
2004 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_ERROR);
2005 return;
2006 }
2007
2008 SDE_ATRACE_BEGIN("signal_retire_fence");
2009 for (i = 0; (i < retire_event->num_connectors) &&
2010 retire_event->connectors[i]; ++i)
2011 sde_connector_complete_commit(
2012 retire_event->connectors[i], ts);
2013 SDE_ATRACE_END("signal_retire_fence");
2014}
2015
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002016/* _sde_crtc_idle_notify - signal idle timeout to client */
2017static void _sde_crtc_idle_notify(struct sde_crtc *sde_crtc)
2018{
2019 struct drm_crtc *crtc;
2020 struct drm_event event;
2021 int ret = 0;
2022
2023 if (!sde_crtc) {
2024 SDE_ERROR("invalid sde crtc\n");
2025 return;
2026 }
2027
2028 crtc = &sde_crtc->base;
2029 event.type = DRM_EVENT_IDLE_NOTIFY;
2030 event.length = sizeof(u32);
2031 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
2032 (u8 *)&ret);
2033
2034 SDE_DEBUG("crtc:%d idle timeout notified\n", crtc->base.id);
2035}
2036
2037/*
2038 * sde_crtc_handle_event - crtc frame event handle.
2039 * This API must manage only non-IRQ context events.
2040 */
2041static bool _sde_crtc_handle_event(struct sde_crtc *sde_crtc, u32 event)
2042{
2043 bool event_processed = false;
2044
2045 /**
2046 * idle events are originated from commit thread and can be processed
2047 * in same context
2048 */
2049 if (event & SDE_ENCODER_FRAME_EVENT_IDLE) {
2050 _sde_crtc_idle_notify(sde_crtc);
2051 event_processed = true;
2052 }
2053
2054 return event_processed;
2055}
2056
Alan Kwong628d19e2016-10-31 13:50:13 -04002057static void sde_crtc_frame_event_work(struct kthread_work *work)
2058{
Alan Kwong67a3f792016-11-01 23:16:53 -04002059 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04002060 struct sde_crtc_frame_event *fevent;
2061 struct drm_crtc *crtc;
2062 struct sde_crtc *sde_crtc;
2063 struct sde_kms *sde_kms;
2064 unsigned long flags;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002065 bool frame_done = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002066
2067 if (!work) {
2068 SDE_ERROR("invalid work handle\n");
2069 return;
2070 }
2071
2072 fevent = container_of(work, struct sde_crtc_frame_event, work);
Alan Kwonga1939682017-05-05 11:30:08 -07002073 if (!fevent->crtc || !fevent->crtc->state) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002074 SDE_ERROR("invalid crtc\n");
2075 return;
2076 }
2077
2078 crtc = fevent->crtc;
2079 sde_crtc = to_sde_crtc(crtc);
2080
2081 sde_kms = _sde_crtc_get_kms(crtc);
2082 if (!sde_kms) {
2083 SDE_ERROR("invalid kms handle\n");
2084 return;
2085 }
Alan Kwong67a3f792016-11-01 23:16:53 -04002086 priv = sde_kms->dev->dev_private;
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002087 SDE_ATRACE_BEGIN("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04002088
2089 SDE_DEBUG("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
2090 ktime_to_ns(fevent->ts));
2091
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002092 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event, SDE_EVTLOG_FUNC_ENTRY);
2093
2094 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
2095 | SDE_ENCODER_FRAME_EVENT_ERROR
2096 | SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002097
2098 if (atomic_read(&sde_crtc->frame_pending) < 1) {
2099 /* this should not happen */
2100 SDE_ERROR("crtc%d ts:%lld invalid frame_pending:%d\n",
2101 crtc->base.id,
2102 ktime_to_ns(fevent->ts),
2103 atomic_read(&sde_crtc->frame_pending));
Dhaval Patel6c666622017-03-21 23:02:59 -07002104 SDE_EVT32(DRMID(crtc), fevent->event,
2105 SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002106 } else if (atomic_dec_return(&sde_crtc->frame_pending) == 0) {
2107 /* release bandwidth and other resources */
2108 SDE_DEBUG("crtc%d ts:%lld last pending\n",
2109 crtc->base.id,
2110 ktime_to_ns(fevent->ts));
Dhaval Patel6c666622017-03-21 23:02:59 -07002111 SDE_EVT32(DRMID(crtc), fevent->event,
2112 SDE_EVTLOG_FUNC_CASE2);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002113 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002114 } else {
Dhaval Patel6c666622017-03-21 23:02:59 -07002115 SDE_EVT32_VERBOSE(DRMID(crtc), fevent->event,
2116 SDE_EVTLOG_FUNC_CASE3);
Alan Kwong628d19e2016-10-31 13:50:13 -04002117 }
Alan Kwonga1939682017-05-05 11:30:08 -07002118
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002119 if (fevent->event & SDE_ENCODER_FRAME_EVENT_DONE)
Alan Kwonga1939682017-05-05 11:30:08 -07002120 sde_core_perf_crtc_update(crtc, 0, false);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002121
2122 if (fevent->event & (SDE_ENCODER_FRAME_EVENT_DONE
2123 | SDE_ENCODER_FRAME_EVENT_ERROR))
2124 frame_done = true;
2125 }
2126
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002127 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE) {
2128 SDE_ATRACE_BEGIN("signal_release_fence");
Dhaval Patelfd8f7742017-08-10 13:11:22 -07002129 sde_fence_signal(&sde_crtc->output_fence, fevent->ts, false);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002130 SDE_ATRACE_END("signal_release_fence");
2131 }
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002132
Dhaval Patel5023c3c2017-08-22 12:40:11 -07002133 if (fevent->event & SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE)
2134 /* this api should be called without spin_lock */
2135 _sde_crtc_retire_event(crtc, fevent->ts);
Alan Kwong628d19e2016-10-31 13:50:13 -04002136
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05002137 if (fevent->event & SDE_ENCODER_FRAME_EVENT_PANEL_DEAD)
2138 SDE_ERROR("crtc%d ts:%lld received panel dead event\n",
2139 crtc->base.id, ktime_to_ns(fevent->ts));
2140
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07002141 if (frame_done)
2142 complete_all(&sde_crtc->frame_done_comp);
2143
Alan Kwong628d19e2016-10-31 13:50:13 -04002144 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
2145 list_add_tail(&fevent->list, &sde_crtc->frame_event_list);
2146 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002147 SDE_ATRACE_END("crtc_frame_event");
Alan Kwong628d19e2016-10-31 13:50:13 -04002148}
2149
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002150/*
2151 * sde_crtc_frame_event_cb - crtc frame event callback API. CRTC module
2152 * registers this API to encoder for all frame event callbacks like
2153 * release_fence, retire_fence, frame_error, frame_done, idle_timeout,
2154 * etc. Encoder may call different events from different context - IRQ,
2155 * user thread, commit_thread, etc. Each event should be carefully
2156 * reviewed and should be processed in proper task context to avoid scheduling
2157 * delay or properly manage the irq context's bottom half processing.
2158 */
Alan Kwong628d19e2016-10-31 13:50:13 -04002159static void sde_crtc_frame_event_cb(void *data, u32 event)
2160{
2161 struct drm_crtc *crtc = (struct drm_crtc *)data;
2162 struct sde_crtc *sde_crtc;
2163 struct msm_drm_private *priv;
Alan Kwong628d19e2016-10-31 13:50:13 -04002164 struct sde_crtc_frame_event *fevent;
2165 unsigned long flags;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002166 u32 crtc_id;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002167 bool event_processed = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04002168
2169 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2170 SDE_ERROR("invalid parameters\n");
2171 return;
2172 }
2173 sde_crtc = to_sde_crtc(crtc);
2174 priv = crtc->dev->dev_private;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002175 crtc_id = drm_crtc_index(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04002176
2177 SDE_DEBUG("crtc%d\n", crtc->base.id);
Ingrid Gallardo79b44392017-05-30 16:30:52 -07002178 SDE_EVT32_VERBOSE(DRMID(crtc), event);
Alan Kwong628d19e2016-10-31 13:50:13 -04002179
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002180 /* try to process the event in caller context */
2181 event_processed = _sde_crtc_handle_event(sde_crtc, event);
2182 if (event_processed)
2183 return;
2184
Alan Kwong628d19e2016-10-31 13:50:13 -04002185 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002186 fevent = list_first_entry_or_null(&sde_crtc->frame_event_list,
2187 struct sde_crtc_frame_event, list);
2188 if (fevent)
2189 list_del_init(&fevent->list);
Alan Kwong628d19e2016-10-31 13:50:13 -04002190 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
2191
Lloyd Atkinson78831f82016-12-09 11:24:56 -05002192 if (!fevent) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002193 SDE_ERROR("crtc%d event %d overflow\n",
2194 crtc->base.id, event);
2195 SDE_EVT32(DRMID(crtc), event);
2196 return;
2197 }
2198
Alan Kwong628d19e2016-10-31 13:50:13 -04002199 fevent->event = event;
2200 fevent->crtc = crtc;
2201 fevent->ts = ktime_get();
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07002202 kthread_queue_work(&priv->event_thread[crtc_id].worker, &fevent->work);
Alan Kwong628d19e2016-10-31 13:50:13 -04002203}
2204
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002205void sde_crtc_complete_commit(struct drm_crtc *crtc,
2206 struct drm_crtc_state *old_state)
2207{
2208 struct sde_crtc *sde_crtc;
2209 struct sde_crtc_smmu_state_data *smmu_state;
2210
2211 if (!crtc || !crtc->state) {
2212 SDE_ERROR("invalid crtc\n");
2213 return;
2214 }
2215
2216 sde_crtc = to_sde_crtc(crtc);
2217 SDE_EVT32_VERBOSE(DRMID(crtc));
2218 smmu_state = &sde_crtc->smmu_state;
2219
2220 /* complete secure transitions if any */
2221 if (smmu_state->transition_type == POST_COMMIT)
2222 sde_crtc_secure_ctrl(crtc, true);
2223}
2224
Dhaval Patele17e0ee2017-08-23 18:01:42 -07002225/* _sde_crtc_set_idle_timeout - update idle timeout wait duration */
2226static void _sde_crtc_set_idle_timeout(struct drm_crtc *crtc, u64 val)
2227{
2228 struct drm_encoder *encoder;
2229
2230 if (!crtc) {
2231 SDE_ERROR("invalid crtc\n");
2232 return;
2233 }
2234
2235 drm_for_each_encoder(encoder, crtc->dev) {
2236 if (encoder->crtc != crtc)
2237 continue;
2238
2239 sde_encoder_set_idle_timeout(encoder, (u32) val);
2240 }
2241}
2242
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002243/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002244 * _sde_crtc_set_input_fence_timeout - update ns version of in fence timeout
2245 * @cstate: Pointer to sde crtc state
2246 */
2247static void _sde_crtc_set_input_fence_timeout(struct sde_crtc_state *cstate)
2248{
2249 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002250 SDE_ERROR("invalid cstate\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002251 return;
2252 }
2253 cstate->input_fence_timeout_ns =
2254 sde_crtc_get_property(cstate, CRTC_PROP_INPUT_FENCE_TIMEOUT);
2255 cstate->input_fence_timeout_ns *= NSEC_PER_MSEC;
2256}
2257
2258/**
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002259 * _sde_crtc_set_dim_layer_v1 - copy dim layer settings from userspace
2260 * @cstate: Pointer to sde crtc state
2261 * @user_ptr: User ptr for sde_drm_dim_layer_v1 struct
2262 */
2263static void _sde_crtc_set_dim_layer_v1(struct sde_crtc_state *cstate,
2264 void *usr_ptr)
2265{
2266 struct sde_drm_dim_layer_v1 dim_layer_v1;
2267 struct sde_drm_dim_layer_cfg *user_cfg;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002268 struct sde_hw_dim_layer *dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002269 u32 count, i;
2270
2271 if (!cstate) {
2272 SDE_ERROR("invalid cstate\n");
2273 return;
2274 }
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002275 dim_layer = cstate->dim_layer;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002276
2277 if (!usr_ptr) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002278 SDE_DEBUG("dim_layer data removed\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002279 return;
2280 }
2281
2282 if (copy_from_user(&dim_layer_v1, usr_ptr, sizeof(dim_layer_v1))) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002283 SDE_ERROR("failed to copy dim_layer data\n");
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002284 return;
2285 }
2286
2287 count = dim_layer_v1.num_layers;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002288 if (count > SDE_MAX_DIM_LAYERS) {
2289 SDE_ERROR("invalid number of dim_layers:%d", count);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002290 return;
2291 }
2292
2293 /* populate from user space */
2294 cstate->num_dim_layers = count;
2295 for (i = 0; i < count; i++) {
2296 user_cfg = &dim_layer_v1.layer_cfg[i];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002297
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002298 dim_layer[i].flags = user_cfg->flags;
2299 dim_layer[i].stage = user_cfg->stage + SDE_STAGE_0;
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002300
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002301 dim_layer[i].rect.x = user_cfg->rect.x1;
2302 dim_layer[i].rect.y = user_cfg->rect.y1;
2303 dim_layer[i].rect.w = user_cfg->rect.x2 - user_cfg->rect.x1;
2304 dim_layer[i].rect.h = user_cfg->rect.y2 - user_cfg->rect.y1;
2305
2306 dim_layer[i].color_fill = (struct sde_mdss_color) {
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002307 user_cfg->color_fill.color_0,
2308 user_cfg->color_fill.color_1,
2309 user_cfg->color_fill.color_2,
2310 user_cfg->color_fill.color_3,
2311 };
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07002312
2313 SDE_DEBUG("dim_layer[%d] - flags:%d, stage:%d\n",
2314 i, dim_layer[i].flags, dim_layer[i].stage);
2315 SDE_DEBUG(" rect:{%d,%d,%d,%d}, color:{%d,%d,%d,%d}\n",
2316 dim_layer[i].rect.x, dim_layer[i].rect.y,
2317 dim_layer[i].rect.w, dim_layer[i].rect.h,
2318 dim_layer[i].color_fill.color_0,
2319 dim_layer[i].color_fill.color_1,
2320 dim_layer[i].color_fill.color_2,
2321 dim_layer[i].color_fill.color_3);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08002322 }
2323}
2324
2325/**
Clarence Ipcae1bb62016-07-07 12:07:13 -04002326 * _sde_crtc_wait_for_fences - wait for incoming framebuffer sync fences
2327 * @crtc: Pointer to CRTC object
2328 */
2329static void _sde_crtc_wait_for_fences(struct drm_crtc *crtc)
2330{
2331 struct drm_plane *plane = NULL;
2332 uint32_t wait_ms = 1;
Clarence Ip8dedc232016-09-09 16:41:00 -04002333 ktime_t kt_end, kt_wait;
Dhaval Patel39323d42017-03-01 23:48:24 -08002334 int rc = 0;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002335
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002336 SDE_DEBUG("\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002337
2338 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002339 SDE_ERROR("invalid crtc/state %pK\n", crtc);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002340 return;
2341 }
2342
2343 /* use monotonic timer to limit total fence wait time */
Clarence Ip8dedc232016-09-09 16:41:00 -04002344 kt_end = ktime_add_ns(ktime_get(),
2345 to_sde_crtc_state(crtc->state)->input_fence_timeout_ns);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002346
2347 /*
2348 * Wait for fences sequentially, as all of them need to be signalled
2349 * before we can proceed.
2350 *
2351 * Limit total wait time to INPUT_FENCE_TIMEOUT, but still call
2352 * sde_plane_wait_input_fence with wait_ms == 0 after the timeout so
2353 * that each plane can check its fence status and react appropriately
Dhaval Patel39323d42017-03-01 23:48:24 -08002354 * if its fence has timed out. Call input fence wait multiple times if
2355 * fence wait is interrupted due to interrupt call.
Clarence Ipcae1bb62016-07-07 12:07:13 -04002356 */
Narendra Muppalla77b32932017-05-10 13:53:11 -07002357 SDE_ATRACE_BEGIN("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002358 drm_atomic_crtc_for_each_plane(plane, crtc) {
Dhaval Patel39323d42017-03-01 23:48:24 -08002359 do {
Clarence Ip8dedc232016-09-09 16:41:00 -04002360 kt_wait = ktime_sub(kt_end, ktime_get());
2361 if (ktime_compare(kt_wait, ktime_set(0, 0)) >= 0)
2362 wait_ms = ktime_to_ms(kt_wait);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002363 else
2364 wait_ms = 0;
Dhaval Patel39323d42017-03-01 23:48:24 -08002365
2366 rc = sde_plane_wait_input_fence(plane, wait_ms);
2367 } while (wait_ms && rc == -ERESTARTSYS);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002368 }
Narendra Muppalla77b32932017-05-10 13:53:11 -07002369 SDE_ATRACE_END("plane_wait_input_fence");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002370}
2371
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002372static void _sde_crtc_setup_mixer_for_encoder(
2373 struct drm_crtc *crtc,
2374 struct drm_encoder *enc)
2375{
2376 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04002377 struct sde_kms *sde_kms = _sde_crtc_get_kms(crtc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002378 struct sde_rm *rm = &sde_kms->rm;
2379 struct sde_crtc_mixer *mixer;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002380 struct sde_hw_ctl *last_valid_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002381 int i;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002382 struct sde_rm_hw_iter lm_iter, ctl_iter, dspp_iter;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002383
2384 sde_rm_init_hw_iter(&lm_iter, enc->base.id, SDE_HW_BLK_LM);
2385 sde_rm_init_hw_iter(&ctl_iter, enc->base.id, SDE_HW_BLK_CTL);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002386 sde_rm_init_hw_iter(&dspp_iter, enc->base.id, SDE_HW_BLK_DSPP);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002387
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002388 /* Set up all the mixers and ctls reserved by this encoder */
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002389 for (i = sde_crtc->num_mixers; i < ARRAY_SIZE(sde_crtc->mixers); i++) {
2390 mixer = &sde_crtc->mixers[i];
2391
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002392 if (!sde_rm_get_hw(rm, &lm_iter))
2393 break;
2394 mixer->hw_lm = (struct sde_hw_mixer *)lm_iter.hw;
2395
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002396 /* CTL may be <= LMs, if <, multiple LMs controlled by 1 CTL */
2397 if (!sde_rm_get_hw(rm, &ctl_iter)) {
2398 SDE_DEBUG("no ctl assigned to lm %d, using previous\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002399 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002400 mixer->hw_ctl = last_valid_ctl;
2401 } else {
2402 mixer->hw_ctl = (struct sde_hw_ctl *)ctl_iter.hw;
2403 last_valid_ctl = mixer->hw_ctl;
2404 }
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002405
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002406 /* Shouldn't happen, mixers are always >= ctls */
2407 if (!mixer->hw_ctl) {
2408 SDE_ERROR("no valid ctls found for lm %d\n",
Clarence Ip8e69ad02016-12-09 09:43:57 -05002409 mixer->hw_lm->idx - LM_0);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002410 return;
2411 }
2412
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07002413 /* Dspp may be null */
2414 (void) sde_rm_get_hw(rm, &dspp_iter);
2415 mixer->hw_dspp = (struct sde_hw_dspp *)dspp_iter.hw;
2416
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002417 mixer->encoder = enc;
2418
2419 sde_crtc->num_mixers++;
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002420 SDE_DEBUG("setup mixer %d: lm %d\n",
2421 i, mixer->hw_lm->idx - LM_0);
2422 SDE_DEBUG("setup mixer %d: ctl %d\n",
2423 i, mixer->hw_ctl->idx - CTL_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002424 }
2425}
2426
2427static void _sde_crtc_setup_mixers(struct drm_crtc *crtc)
2428{
2429 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
2430 struct drm_encoder *enc;
2431
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002432 sde_crtc->num_mixers = 0;
2433 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
2434
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002435 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002436 /* Check for mixers on all encoders attached to this crtc */
2437 list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
2438 if (enc->crtc != crtc)
2439 continue;
2440
2441 _sde_crtc_setup_mixer_for_encoder(crtc, enc);
2442 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002443
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07002444 mutex_unlock(&sde_crtc->crtc_lock);
Lloyd Atkinson11f34442016-08-11 11:19:52 -04002445}
2446
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002447static void _sde_crtc_setup_is_ppsplit(struct drm_crtc_state *state)
2448{
2449 int i;
2450 struct sde_crtc_state *cstate;
2451
2452 cstate = to_sde_crtc_state(state);
2453
2454 cstate->is_ppsplit = false;
2455 for (i = 0; i < cstate->num_connectors; i++) {
2456 struct drm_connector *conn = cstate->connectors[i];
2457
2458 if (sde_connector_get_topology_name(conn) ==
2459 SDE_RM_TOPOLOGY_PPSPLIT)
2460 cstate->is_ppsplit = true;
2461 }
2462}
2463
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002464static void _sde_crtc_setup_lm_bounds(struct drm_crtc *crtc,
2465 struct drm_crtc_state *state)
2466{
2467 struct sde_crtc *sde_crtc;
2468 struct sde_crtc_state *cstate;
2469 struct drm_display_mode *adj_mode;
2470 u32 crtc_split_width;
2471 int i;
2472
2473 if (!crtc || !state) {
2474 SDE_ERROR("invalid args\n");
2475 return;
2476 }
2477
2478 sde_crtc = to_sde_crtc(crtc);
2479 cstate = to_sde_crtc_state(state);
2480
2481 adj_mode = &state->adjusted_mode;
2482 crtc_split_width = sde_crtc_mixer_width(sde_crtc, adj_mode);
2483
2484 for (i = 0; i < sde_crtc->num_mixers; i++) {
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002485 cstate->lm_bounds[i].x = crtc_split_width * i;
2486 cstate->lm_bounds[i].y = 0;
2487 cstate->lm_bounds[i].w = crtc_split_width;
2488 cstate->lm_bounds[i].h = adj_mode->vdisplay;
2489 memcpy(&cstate->lm_roi[i], &cstate->lm_bounds[i],
2490 sizeof(cstate->lm_roi[i]));
Dhaval Patela5f75952017-07-25 11:17:41 -07002491 SDE_EVT32_VERBOSE(DRMID(crtc), i,
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002492 cstate->lm_bounds[i].x, cstate->lm_bounds[i].y,
2493 cstate->lm_bounds[i].w, cstate->lm_bounds[i].h);
2494 SDE_DEBUG("%s: lm%d bnd&roi (%d,%d,%d,%d)\n", sde_crtc->name, i,
2495 cstate->lm_roi[i].x, cstate->lm_roi[i].y,
2496 cstate->lm_roi[i].w, cstate->lm_roi[i].h);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002497 }
2498
2499 drm_mode_debug_printmodeline(adj_mode);
2500}
2501
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002502static void sde_crtc_atomic_begin(struct drm_crtc *crtc,
Clarence Ip0d0e96d2016-10-24 18:13:13 -04002503 struct drm_crtc_state *old_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002504{
Clarence Ipcae1bb62016-07-07 12:07:13 -04002505 struct sde_crtc *sde_crtc;
Dhaval Patel0e558f42017-04-30 00:51:40 -07002506 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002507 struct drm_device *dev;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002508 unsigned long flags;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002509 struct sde_crtc_smmu_state_data *smmu_state;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002510
Clarence Ipcae1bb62016-07-07 12:07:13 -04002511 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002512 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002513 return;
2514 }
2515
Alan Kwong163d2612016-11-03 00:56:56 -04002516 if (!crtc->state->enable) {
2517 SDE_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
2518 crtc->base.id, crtc->state->enable);
2519 return;
2520 }
2521
2522 SDE_DEBUG("crtc%d\n", crtc->base.id);
2523
Clarence Ipcae1bb62016-07-07 12:07:13 -04002524 sde_crtc = to_sde_crtc(crtc);
2525 dev = crtc->dev;
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002526 smmu_state = &sde_crtc->smmu_state;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002527
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002528 if (!sde_crtc->num_mixers) {
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002529 _sde_crtc_setup_mixers(crtc);
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05002530 _sde_crtc_setup_is_ppsplit(crtc->state);
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04002531 _sde_crtc_setup_lm_bounds(crtc, crtc->state);
2532 }
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05002533
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002534 if (sde_crtc->event) {
2535 WARN_ON(sde_crtc->event);
2536 } else {
2537 spin_lock_irqsave(&dev->event_lock, flags);
2538 sde_crtc->event = crtc->state->event;
2539 spin_unlock_irqrestore(&dev->event_lock, flags);
2540 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002541
Dhaval Patel0e558f42017-04-30 00:51:40 -07002542 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2543 if (encoder->crtc != crtc)
2544 continue;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002545
Dhaval Patel0e558f42017-04-30 00:51:40 -07002546 /* encoder will trigger pending mask now */
2547 sde_encoder_trigger_kickoff_pending(encoder);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002548 }
2549
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002550 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002551 * If no mixers have been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002552 * it means we are trying to flush a CRTC whose state is disabled:
2553 * nothing else needs to be done.
2554 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002555 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002556 return;
2557
Clarence Ipd9f9fa62016-09-09 13:42:32 -04002558 _sde_crtc_blend_setup(crtc);
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002559
2560 /*
2561 * Since CP properties use AXI buffer to program the
2562 * HW, check if context bank is in attached
2563 * state,
2564 * apply color processing properties only if
2565 * smmu state is attached,
2566 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07002567 if ((smmu_state->state != DETACHED) &&
Abhijit Kulkarni12cef9c2017-07-13 11:19:03 -07002568 (smmu_state->state != DETACH_ALL_REQ))
2569 sde_cp_crtc_apply_properties(crtc);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002570
2571 /*
2572 * PP_DONE irq is only used by command mode for now.
2573 * It is better to request pending before FLUSH and START trigger
2574 * to make sure no pp_done irq missed.
2575 * This is safe because no pp_done will happen before SW trigger
2576 * in command mode.
2577 */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002578}
2579
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002580static void sde_crtc_atomic_flush(struct drm_crtc *crtc,
2581 struct drm_crtc_state *old_crtc_state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002582{
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002583 struct drm_encoder *encoder;
Clarence Ipcae1bb62016-07-07 12:07:13 -04002584 struct sde_crtc *sde_crtc;
2585 struct drm_device *dev;
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002586 struct drm_plane *plane;
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302587 struct msm_drm_private *priv;
2588 struct msm_drm_thread *event_thread;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002589 unsigned long flags;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002590 struct sde_crtc_state *cstate;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002591
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302592 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002593 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04002594 return;
2595 }
2596
Alan Kwong163d2612016-11-03 00:56:56 -04002597 if (!crtc->state->enable) {
2598 SDE_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
2599 crtc->base.id, crtc->state->enable);
2600 return;
2601 }
2602
2603 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002604
2605 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002606 cstate = to_sde_crtc_state(crtc->state);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002607 dev = crtc->dev;
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05302608 priv = dev->dev_private;
2609
2610 if (crtc->index >= ARRAY_SIZE(priv->event_thread)) {
2611 SDE_ERROR("invalid crtc index[%d]\n", crtc->index);
2612 return;
2613 }
2614
2615 event_thread = &priv->event_thread[crtc->index];
Clarence Ipcae1bb62016-07-07 12:07:13 -04002616
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002617 if (sde_crtc->event) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002618 SDE_DEBUG("already received sde_crtc->event\n");
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002619 } else {
Lloyd Atkinson265d2212016-05-30 13:12:01 -04002620 spin_lock_irqsave(&dev->event_lock, flags);
2621 sde_crtc->event = crtc->state->event;
2622 spin_unlock_irqrestore(&dev->event_lock, flags);
2623 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002624
2625 /*
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002626 * If no mixers has been allocated in sde_crtc_atomic_check(),
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002627 * it means we are trying to flush a CRTC whose state is disabled:
2628 * nothing else needs to be done.
2629 */
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04002630 if (unlikely(!sde_crtc->num_mixers))
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04002631 return;
2632
Alan Kwong346223e2017-06-30 15:29:22 -04002633 /*
2634 * For planes without commit update, drm framework will not add
2635 * those planes to current state since hardware update is not
2636 * required. However, if those planes were power collapsed since
2637 * last commit cycle, driver has to restore the hardware state
2638 * of those planes explicitly here prior to plane flush.
2639 */
2640 drm_atomic_crtc_for_each_plane(plane, crtc)
2641 sde_plane_restore(plane);
2642
Clarence Ipcae1bb62016-07-07 12:07:13 -04002643 /* wait for acquire fences before anything else is done */
2644 _sde_crtc_wait_for_fences(crtc);
2645
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002646 if (!cstate->rsc_update) {
2647 drm_for_each_encoder(encoder, dev) {
2648 if (encoder->crtc != crtc)
2649 continue;
2650
2651 cstate->rsc_client =
Dhaval Patel30fae8a2017-04-21 18:42:41 -07002652 sde_encoder_get_rsc_client(encoder);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08002653 }
2654 cstate->rsc_update = true;
2655 }
2656
Alan Kwong9aa061c2016-11-06 21:17:12 -05002657 /* update performance setting before crtc kickoff */
2658 sde_core_perf_crtc_update(crtc, 1, false);
2659
Clarence Ipcae1bb62016-07-07 12:07:13 -04002660 /*
2661 * Final plane updates: Give each plane a chance to complete all
2662 * required writes/flushing before crtc's "flush
2663 * everything" call below.
2664 */
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002665 drm_atomic_crtc_for_each_plane(plane, crtc) {
2666 if (sde_crtc->smmu_state.transition_error)
2667 sde_plane_set_error(plane, true);
Clarence Ipcae1bb62016-07-07 12:07:13 -04002668 sde_plane_flush(plane);
Abhijit Kulkarni1b3340c2017-06-22 12:39:37 -07002669 }
Clarence Ipcae1bb62016-07-07 12:07:13 -04002670
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002671 /* Kickoff will be scheduled by outer layer */
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002672}
2673
Clarence Ip7a753bb2016-07-07 11:47:44 -04002674/**
2675 * sde_crtc_destroy_state - state destroy hook
2676 * @crtc: drm CRTC
2677 * @state: CRTC state object to release
2678 */
2679static void sde_crtc_destroy_state(struct drm_crtc *crtc,
2680 struct drm_crtc_state *state)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07002681{
Clarence Ip7a753bb2016-07-07 11:47:44 -04002682 struct sde_crtc *sde_crtc;
2683 struct sde_crtc_state *cstate;
2684
2685 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002686 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002687 return;
2688 }
2689
2690 sde_crtc = to_sde_crtc(crtc);
2691 cstate = to_sde_crtc_state(state);
2692
Alan Kwong163d2612016-11-03 00:56:56 -04002693 SDE_DEBUG("crtc%d\n", crtc->base.id);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002694
Alan Kwongcdb2f282017-03-18 13:42:06 -07002695 _sde_crtc_rp_destroy(&cstate->rp);
2696
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07002697 __drm_atomic_helper_crtc_destroy_state(state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002698
2699 /* destroy value helper */
2700 msm_property_destroy_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002701 &cstate->property_state);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002702}
2703
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002704static int _sde_crtc_wait_for_frame_done(struct drm_crtc *crtc)
2705{
2706 struct sde_crtc *sde_crtc;
2707 int ret, rc = 0;
2708
2709 if (!crtc) {
2710 SDE_ERROR("invalid argument\n");
2711 return -EINVAL;
2712 }
2713 sde_crtc = to_sde_crtc(crtc);
2714
2715 if (!atomic_read(&sde_crtc->frame_pending)) {
2716 SDE_DEBUG("no frames pending\n");
2717 return 0;
2718 }
2719
Dhaval Patela5f75952017-07-25 11:17:41 -07002720 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_ENTRY);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002721 ret = wait_for_completion_timeout(&sde_crtc->frame_done_comp,
2722 msecs_to_jiffies(SDE_FRAME_DONE_TIMEOUT));
2723 if (!ret) {
2724 SDE_ERROR("frame done completion wait timed out, ret:%d\n",
2725 ret);
2726 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FATAL);
2727 rc = -ETIMEDOUT;
2728 }
Dhaval Patela5f75952017-07-25 11:17:41 -07002729 SDE_EVT32_VERBOSE(DRMID(crtc), SDE_EVTLOG_FUNC_EXIT);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002730
2731 return rc;
2732}
2733
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002734void sde_crtc_commit_kickoff(struct drm_crtc *crtc)
2735{
Clarence Ipeb39cce2017-07-19 14:12:43 -04002736 struct drm_plane *plane;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002737 struct drm_encoder *encoder;
2738 struct drm_device *dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002739 struct sde_crtc *sde_crtc;
Alan Kwong67a3f792016-11-01 23:16:53 -04002740 struct msm_drm_private *priv;
2741 struct sde_kms *sde_kms;
Alan Kwong4aacd532017-02-04 18:51:33 -08002742 struct sde_crtc_state *cstate;
Clarence Ipf6b530a2017-08-21 19:39:18 -04002743 struct sde_hw_ctl *ctl;
2744 int ret, i;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002745
2746 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002747 SDE_ERROR("invalid argument\n");
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002748 return;
2749 }
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002750 dev = crtc->dev;
Alan Kwong628d19e2016-10-31 13:50:13 -04002751 sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04002752 sde_kms = _sde_crtc_get_kms(crtc);
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07002753
2754 if (!sde_kms || !sde_kms->dev || !sde_kms->dev->dev_private) {
2755 SDE_ERROR("invalid argument\n");
2756 return;
2757 }
2758
Alan Kwong67a3f792016-11-01 23:16:53 -04002759 priv = sde_kms->dev->dev_private;
Alan Kwong4aacd532017-02-04 18:51:33 -08002760 cstate = to_sde_crtc_state(crtc->state);
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002761
Clarence Ip90b282d2017-05-04 10:00:32 -07002762 /*
2763 * If no mixers has been allocated in sde_crtc_atomic_check(),
2764 * it means we are trying to start a CRTC whose state is disabled:
2765 * nothing else needs to be done.
2766 */
2767 if (unlikely(!sde_crtc->num_mixers))
2768 return;
2769
Narendra Muppalla77b32932017-05-10 13:53:11 -07002770 SDE_ATRACE_BEGIN("crtc_commit");
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002771 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Alan Kwong4aacd532017-02-04 18:51:33 -08002772 struct sde_encoder_kickoff_params params = { 0 };
2773
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002774 if (encoder->crtc != crtc)
2775 continue;
2776
2777 /*
2778 * Encoder will flush/start now, unless it has a tx pending.
2779 * If so, it may delay and flush at an irq event (e.g. ppdone)
2780 */
Alan Kwong4aacd532017-02-04 18:51:33 -08002781 params.inline_rotate_prefill = cstate->sbuf_prefill_line;
Lloyd Atkinson73fb8092017-02-08 16:02:55 -05002782 params.affected_displays = _sde_crtc_get_displays_affected(crtc,
2783 crtc->state);
Alan Kwong4aacd532017-02-04 18:51:33 -08002784 sde_encoder_prepare_for_kickoff(encoder, &params);
Alan Kwong628d19e2016-10-31 13:50:13 -04002785 }
2786
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002787 /* wait for frame_event_done completion */
Veera Sundaram Sankarana90e1392017-07-06 15:00:09 -07002788 SDE_ATRACE_BEGIN("wait_for_frame_done_event");
2789 ret = _sde_crtc_wait_for_frame_done(crtc);
2790 SDE_ATRACE_END("wait_for_frame_done_event");
2791 if (ret) {
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002792 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
2793 crtc->base.id,
2794 atomic_read(&sde_crtc->frame_pending));
Narendra Muppalla77b32932017-05-10 13:53:11 -07002795 goto end;
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002796 }
2797
2798 if (atomic_inc_return(&sde_crtc->frame_pending) == 1) {
Alan Kwong628d19e2016-10-31 13:50:13 -04002799 /* acquire bandwidth and other resources */
2800 SDE_DEBUG("crtc%d first commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002801 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE1);
Alan Kwong628d19e2016-10-31 13:50:13 -04002802 } else {
2803 SDE_DEBUG("crtc%d commit\n", crtc->base.id);
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002804 SDE_EVT32(DRMID(crtc), SDE_EVTLOG_FUNC_CASE2);
Alan Kwong628d19e2016-10-31 13:50:13 -04002805 }
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07002806 sde_crtc->play_count++;
Alan Kwong628d19e2016-10-31 13:50:13 -04002807
Clarence Ip7327aa22017-08-24 16:21:39 -04002808 if (cstate->sbuf_cfg.rot_op_mode != SDE_CTL_ROT_OP_MODE_OFFLINE) {
2809 drm_atomic_crtc_for_each_plane(plane, crtc) {
Clarence Ipeb39cce2017-07-19 14:12:43 -04002810 sde_plane_kickoff(plane);
Clarence Ip7327aa22017-08-24 16:21:39 -04002811 }
2812 }
Clarence Ipeb39cce2017-07-19 14:12:43 -04002813
Clarence Ipf6b530a2017-08-21 19:39:18 -04002814 for (i = 0; i < sde_crtc->num_mixers; i++) {
2815 ctl = sde_crtc->mixers[i].hw_ctl;
2816 if (ctl && ctl->ops.setup_sbuf_cfg)
2817 ctl->ops.setup_sbuf_cfg(ctl, &cstate->sbuf_cfg);
2818 }
2819
Clarence Ip980405d2017-08-08 18:33:44 -04002820 sde_vbif_clear_errors(sde_kms);
2821
Alan Kwong628d19e2016-10-31 13:50:13 -04002822 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2823 if (encoder->crtc != crtc)
2824 continue;
2825
2826 sde_encoder_kickoff(encoder);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04002827 }
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07002828
Narendra Muppalla77b32932017-05-10 13:53:11 -07002829end:
Dhaval Patelb9850c02017-08-07 22:55:47 -07002830 reinit_completion(&sde_crtc->frame_done_comp);
Narendra Muppalla77b32932017-05-10 13:53:11 -07002831 SDE_ATRACE_END("crtc_commit");
2832 return;
Lloyd Atkinson5d722782016-05-30 14:09:41 -04002833}
2834
Clarence Ip7a753bb2016-07-07 11:47:44 -04002835/**
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002836 * _sde_crtc_vblank_enable_no_lock - update power resource and vblank request
Clarence Ip7f70ce42017-03-20 06:53:46 -07002837 * @sde_crtc: Pointer to sde crtc structure
2838 * @enable: Whether to enable/disable vblanks
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002839 *
2840 * @Return: error code
Clarence Ip7f70ce42017-03-20 06:53:46 -07002841 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002842static int _sde_crtc_vblank_enable_no_lock(
Clarence Ip7f70ce42017-03-20 06:53:46 -07002843 struct sde_crtc *sde_crtc, bool enable)
2844{
2845 struct drm_device *dev;
2846 struct drm_crtc *crtc;
2847 struct drm_encoder *enc;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002848
2849 if (!sde_crtc) {
2850 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002851 return -EINVAL;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002852 }
2853
2854 crtc = &sde_crtc->base;
2855 dev = crtc->dev;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002856
2857 if (enable) {
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002858 int ret;
2859
2860 /* drop lock since power crtc cb may try to re-acquire lock */
2861 mutex_unlock(&sde_crtc->crtc_lock);
2862 ret = _sde_crtc_power_enable(sde_crtc, true);
2863 mutex_lock(&sde_crtc->crtc_lock);
2864 if (ret)
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002865 return ret;
Dhaval Patelf9245d62017-03-28 16:24:00 -07002866
Clarence Ip7f70ce42017-03-20 06:53:46 -07002867 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2868 if (enc->crtc != crtc)
2869 continue;
2870
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002871 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2872 sde_crtc->enabled,
2873 sde_crtc->suspend,
2874 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002875
2876 sde_encoder_register_vblank_callback(enc,
2877 sde_crtc_vblank_cb, (void *)crtc);
2878 }
2879 } else {
2880 list_for_each_entry(enc, &dev->mode_config.encoder_list, head) {
2881 if (enc->crtc != crtc)
2882 continue;
2883
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002884 SDE_EVT32(DRMID(&sde_crtc->base), DRMID(enc), enable,
2885 sde_crtc->enabled,
2886 sde_crtc->suspend,
2887 sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002888
2889 sde_encoder_register_vblank_callback(enc, NULL, NULL);
2890 }
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002891
2892 /* drop lock since power crtc cb may try to re-acquire lock */
2893 mutex_unlock(&sde_crtc->crtc_lock);
Dhaval Patelf9245d62017-03-28 16:24:00 -07002894 _sde_crtc_power_enable(sde_crtc, false);
Lloyd Atkinson2c554eb2017-05-24 16:22:39 -04002895 mutex_lock(&sde_crtc->crtc_lock);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002896 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002897
2898 return 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002899}
2900
2901/**
2902 * _sde_crtc_set_suspend - notify crtc of suspend enable/disable
2903 * @crtc: Pointer to drm crtc object
2904 * @enable: true to enable suspend, false to indicate resume
2905 */
2906static void _sde_crtc_set_suspend(struct drm_crtc *crtc, bool enable)
2907{
2908 struct sde_crtc *sde_crtc;
2909 struct msm_drm_private *priv;
2910 struct sde_kms *sde_kms;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002911 int ret = 0;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002912
2913 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
2914 SDE_ERROR("invalid crtc\n");
2915 return;
2916 }
2917 sde_crtc = to_sde_crtc(crtc);
2918 priv = crtc->dev->dev_private;
2919
2920 if (!priv->kms) {
2921 SDE_ERROR("invalid crtc kms\n");
2922 return;
2923 }
2924 sde_kms = to_sde_kms(priv->kms);
2925
2926 SDE_DEBUG("crtc%d suspend = %d\n", crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002927 SDE_EVT32_VERBOSE(DRMID(crtc), enable);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002928
2929 mutex_lock(&sde_crtc->crtc_lock);
2930
Clarence Ip2f9beeb2017-03-16 11:04:53 -04002931 /*
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04002932 * If the vblank is enabled, release a power reference on suspend
2933 * and take it back during resume (if it is still enabled).
Clarence Ip7f70ce42017-03-20 06:53:46 -07002934 */
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002935 SDE_EVT32(DRMID(&sde_crtc->base), enable, sde_crtc->enabled,
2936 sde_crtc->suspend, sde_crtc->vblank_requested);
Clarence Ip7f70ce42017-03-20 06:53:46 -07002937 if (sde_crtc->suspend == enable)
2938 SDE_DEBUG("crtc%d suspend already set to %d, ignoring update\n",
2939 crtc->base.id, enable);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04002940 else if (sde_crtc->enabled && sde_crtc->vblank_requested) {
2941 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, !enable);
2942 if (ret)
2943 SDE_ERROR("%s vblank enable failed: %d\n",
2944 sde_crtc->name, ret);
2945 }
Clarence Ip7f70ce42017-03-20 06:53:46 -07002946
2947 sde_crtc->suspend = enable;
Clarence Ip7f70ce42017-03-20 06:53:46 -07002948 mutex_unlock(&sde_crtc->crtc_lock);
2949}
2950
2951/**
Clarence Ip7a753bb2016-07-07 11:47:44 -04002952 * sde_crtc_duplicate_state - state duplicate hook
2953 * @crtc: Pointer to drm crtc structure
2954 * @Returns: Pointer to new drm_crtc_state structure
2955 */
2956static struct drm_crtc_state *sde_crtc_duplicate_state(struct drm_crtc *crtc)
2957{
2958 struct sde_crtc *sde_crtc;
2959 struct sde_crtc_state *cstate, *old_cstate;
2960
2961 if (!crtc || !crtc->state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002962 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002963 return NULL;
2964 }
2965
2966 sde_crtc = to_sde_crtc(crtc);
2967 old_cstate = to_sde_crtc_state(crtc->state);
2968 cstate = msm_property_alloc_state(&sde_crtc->property_info);
2969 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002970 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04002971 return NULL;
2972 }
2973
2974 /* duplicate value helper */
2975 msm_property_duplicate_state(&sde_crtc->property_info,
2976 old_cstate, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04002977 &cstate->property_state, cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04002978
2979 /* duplicate base helper */
2980 __drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
2981
Alan Kwongcdb2f282017-03-18 13:42:06 -07002982 _sde_crtc_rp_duplicate(&old_cstate->rp, &cstate->rp);
2983
Clarence Ip7a753bb2016-07-07 11:47:44 -04002984 return &cstate->base;
2985}
2986
2987/**
2988 * sde_crtc_reset - reset hook for CRTCs
2989 * Resets the atomic state for @crtc by freeing the state pointer (which might
2990 * be NULL, e.g. at driver load time) and allocating a new empty state object.
2991 * @crtc: Pointer to drm crtc structure
2992 */
2993static void sde_crtc_reset(struct drm_crtc *crtc)
2994{
2995 struct sde_crtc *sde_crtc;
2996 struct sde_crtc_state *cstate;
2997
2998 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07002999 SDE_ERROR("invalid crtc\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003000 return;
3001 }
3002
Clarence Ip7f70ce42017-03-20 06:53:46 -07003003 /* revert suspend actions, if necessary */
Clarence Ipd86f6e42017-08-08 18:31:00 -04003004 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07003005 _sde_crtc_set_suspend(crtc, false);
3006
Clarence Ip7a753bb2016-07-07 11:47:44 -04003007 /* remove previous state, if present */
3008 if (crtc->state) {
3009 sde_crtc_destroy_state(crtc, crtc->state);
3010 crtc->state = 0;
3011 }
3012
3013 sde_crtc = to_sde_crtc(crtc);
3014 cstate = msm_property_alloc_state(&sde_crtc->property_info);
3015 if (!cstate) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003016 SDE_ERROR("failed to allocate state\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003017 return;
3018 }
3019
3020 /* reset value helper */
3021 msm_property_reset_state(&sde_crtc->property_info, cstate,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003022 &cstate->property_state,
3023 cstate->property_values);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003024
Clarence Ipcae1bb62016-07-07 12:07:13 -04003025 _sde_crtc_set_input_fence_timeout(cstate);
3026
Alan Kwong310e9b02017-08-03 02:04:07 -04003027 _sde_crtc_rp_reset(&cstate->rp, &sde_crtc->rp_lock,
3028 &sde_crtc->rp_head);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003029
Clarence Ip7a753bb2016-07-07 11:47:44 -04003030 cstate->base.crtc = crtc;
3031 crtc->state = &cstate->base;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003032}
3033
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003034static void sde_crtc_handle_power_event(u32 event_type, void *arg)
3035{
3036 struct drm_crtc *crtc = arg;
3037 struct sde_crtc *sde_crtc;
Dhaval Patel010f5172017-08-01 22:40:09 -07003038 struct drm_plane *plane;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003039 struct drm_encoder *encoder;
Dhaval Patel010f5172017-08-01 22:40:09 -07003040 struct sde_crtc_mixer *m;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07003041 u32 i, misr_status;
Ping Licc868fc2017-08-11 16:56:44 -07003042 unsigned long flags;
3043 struct sde_crtc_irq_info *node = NULL;
3044 int ret = 0;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003045
3046 if (!crtc) {
3047 SDE_ERROR("invalid crtc\n");
3048 return;
3049 }
3050 sde_crtc = to_sde_crtc(crtc);
3051
3052 mutex_lock(&sde_crtc->crtc_lock);
3053
3054 SDE_EVT32(DRMID(crtc), event_type);
3055
Dhaval Patel010f5172017-08-01 22:40:09 -07003056 switch (event_type) {
3057 case SDE_POWER_EVENT_POST_ENABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003058 /* restore encoder; crtc will be programmed during commit */
3059 drm_for_each_encoder(encoder, crtc->dev) {
3060 if (encoder->crtc != crtc)
3061 continue;
3062
3063 sde_encoder_virt_restore(encoder);
3064 }
Ping Licc868fc2017-08-11 16:56:44 -07003065
3066 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3067 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3068 ret = 0;
3069 if (node->func)
3070 ret = node->func(crtc, true, &node->irq);
3071 if (ret)
3072 SDE_ERROR("%s failed to enable event %x\n",
3073 sde_crtc->name, node->event);
3074 }
3075 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3076
Ping Lie505f3b2017-06-19 14:19:08 -07003077 sde_cp_crtc_post_ipc(crtc);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003078
Dhaval Patel010f5172017-08-01 22:40:09 -07003079 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3080 m = &sde_crtc->mixers[i];
3081 if (!m->hw_lm || !m->hw_lm->ops.setup_misr ||
3082 !sde_crtc->misr_enable)
3083 continue;
3084
3085 m->hw_lm->ops.setup_misr(m->hw_lm, true,
3086 sde_crtc->misr_frame_count);
3087 }
3088 break;
3089 case SDE_POWER_EVENT_PRE_DISABLE:
3090 for (i = 0; i < sde_crtc->num_mixers; ++i) {
3091 m = &sde_crtc->mixers[i];
3092 if (!m->hw_lm || !m->hw_lm->ops.collect_misr ||
3093 !sde_crtc->misr_enable)
3094 continue;
3095
3096 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
3097 sde_crtc->misr_data[i] = misr_status ? misr_status :
3098 sde_crtc->misr_data[i];
3099 }
Ping Licc868fc2017-08-11 16:56:44 -07003100
3101 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3102 node = NULL;
3103 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3104 ret = 0;
3105 if (node->func)
3106 ret = node->func(crtc, false, &node->irq);
3107 if (ret)
3108 SDE_ERROR("%s failed to disable event %x\n",
3109 sde_crtc->name, node->event);
3110 }
3111 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3112
Dhaval Patel010f5172017-08-01 22:40:09 -07003113 sde_cp_crtc_pre_ipc(crtc);
3114 break;
3115 case SDE_POWER_EVENT_POST_DISABLE:
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003116 /*
3117 * set revalidate flag in planes, so it will be re-programmed
3118 * in the next frame update
3119 */
3120 drm_atomic_crtc_for_each_plane(plane, crtc)
3121 sde_plane_set_revalidate(plane, true);
Alan Kwong8a9b38a2017-06-22 11:30:52 -04003122
Gopikrishnaiah Anandandb90fa12017-05-09 17:56:08 -07003123 sde_cp_crtc_suspend(crtc);
Dhaval Patel010f5172017-08-01 22:40:09 -07003124 break;
3125 default:
3126 SDE_DEBUG("event:%d not handled\n", event_type);
3127 break;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003128 }
3129
3130 mutex_unlock(&sde_crtc->crtc_lock);
3131}
3132
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003133static void sde_crtc_disable(struct drm_crtc *crtc)
3134{
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003135 struct sde_crtc *sde_crtc;
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003136 struct sde_crtc_state *cstate;
Alan Kwong07da0982016-11-04 12:57:45 -04003137 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003138 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003139 unsigned long flags;
3140 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003141 struct drm_event event;
3142 u32 power_on;
Dhaval Patelfd8f7742017-08-10 13:11:22 -07003143 int ret, i;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003144
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003145 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !crtc->state) {
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003146 SDE_ERROR("invalid crtc\n");
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003147 return;
3148 }
3149 sde_crtc = to_sde_crtc(crtc);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003150 cstate = to_sde_crtc_state(crtc->state);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003151 priv = crtc->dev->dev_private;
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003152
Alan Kwong163d2612016-11-03 00:56:56 -04003153 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003154
Clarence Ipd86f6e42017-08-08 18:31:00 -04003155 if (sde_kms_is_suspend_state(crtc->dev))
Clarence Ip7f70ce42017-03-20 06:53:46 -07003156 _sde_crtc_set_suspend(crtc, true);
3157
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003158 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003159 SDE_EVT32_VERBOSE(DRMID(crtc));
Alan Kwong628d19e2016-10-31 13:50:13 -04003160
Ping Lic5c2e0b2017-08-02 15:17:59 -07003161 /* update color processing on suspend */
3162 event.type = DRM_EVENT_CRTC_POWER;
3163 event.length = sizeof(u32);
3164 sde_cp_crtc_suspend(crtc);
3165 power_on = 0;
3166 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3167 (u8 *)&power_on);
3168
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07003169 /* wait for frame_event_done completion */
3170 if (_sde_crtc_wait_for_frame_done(crtc))
3171 SDE_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
3172 crtc->base.id,
3173 atomic_read(&sde_crtc->frame_pending));
3174
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003175 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3176 sde_crtc->vblank_requested);
3177 if (sde_crtc->enabled && !sde_crtc->suspend &&
3178 sde_crtc->vblank_requested) {
3179 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, false);
3180 if (ret)
3181 SDE_ERROR("%s vblank enable failed: %d\n",
3182 sde_crtc->name, ret);
Alan Kwong07da0982016-11-04 12:57:45 -04003183 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003184 sde_crtc->enabled = false;
Alan Kwong07da0982016-11-04 12:57:45 -04003185
Alan Kwong628d19e2016-10-31 13:50:13 -04003186 if (atomic_read(&sde_crtc->frame_pending)) {
Dhaval Patel6c666622017-03-21 23:02:59 -07003187 SDE_EVT32(DRMID(crtc), atomic_read(&sde_crtc->frame_pending),
3188 SDE_EVTLOG_FUNC_CASE2);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003189 sde_core_perf_crtc_release_bw(crtc);
Alan Kwong628d19e2016-10-31 13:50:13 -04003190 atomic_set(&sde_crtc->frame_pending, 0);
3191 }
3192
Ping Li6d5bf542017-06-27 11:40:28 -07003193 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3194 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3195 ret = 0;
3196 if (node->func)
3197 ret = node->func(crtc, false, &node->irq);
3198 if (ret)
3199 SDE_ERROR("%s failed to disable event %x\n",
3200 sde_crtc->name, node->event);
3201 }
3202 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
3203
Alan Kwong9aa061c2016-11-06 21:17:12 -05003204 sde_core_perf_crtc_update(crtc, 0, true);
3205
Alan Kwong628d19e2016-10-31 13:50:13 -04003206 drm_for_each_encoder(encoder, crtc->dev) {
3207 if (encoder->crtc != crtc)
3208 continue;
3209 sde_encoder_register_frame_event_callback(encoder, NULL, NULL);
Dhaval Patel82c8dbc2017-02-18 23:15:10 -08003210 cstate->rsc_client = NULL;
3211 cstate->rsc_update = false;
Alan Kwong628d19e2016-10-31 13:50:13 -04003212 }
3213
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003214 if (sde_crtc->power_event)
3215 sde_power_handle_unregister_event(&priv->phandle,
3216 sde_crtc->power_event);
3217
Dhaval Patelfd8f7742017-08-10 13:11:22 -07003218 /**
3219 * All callbacks are unregistered and frame done waits are complete
3220 * at this point. No buffers are accessed by hardware.
3221 * reset the fence timeline if there is any issue.
3222 */
3223 sde_fence_signal(&sde_crtc->output_fence, ktime_get(), true);
3224 for (i = 0; i < cstate->num_connectors; ++i)
3225 sde_connector_commit_reset(cstate->connectors[i], ktime_get());
3226
Lloyd Atkinsonc44a52e2016-08-16 16:40:17 -04003227 memset(sde_crtc->mixers, 0, sizeof(sde_crtc->mixers));
3228 sde_crtc->num_mixers = 0;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003229
Alan Kwong8411a9112017-06-06 19:29:01 -04003230 /* disable clk & bw control until clk & bw properties are set */
3231 cstate->bw_control = false;
Alan Kwong0230a102017-05-16 11:36:44 -07003232 cstate->bw_split_vote = false;
Alan Kwong8411a9112017-06-06 19:29:01 -04003233
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07003234 mutex_unlock(&sde_crtc->crtc_lock);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003235}
3236
3237static void sde_crtc_enable(struct drm_crtc *crtc)
3238{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003239 struct sde_crtc *sde_crtc;
Alan Kwong628d19e2016-10-31 13:50:13 -04003240 struct drm_encoder *encoder;
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003241 struct msm_drm_private *priv;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003242 unsigned long flags;
3243 struct sde_crtc_irq_info *node = NULL;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003244 struct drm_event event;
3245 u32 power_on;
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003246 int ret;
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003247
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003248 if (!crtc || !crtc->dev || !crtc->dev->dev_private) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003249 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003250 return;
3251 }
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003252 priv = crtc->dev->dev_private;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003253
Alan Kwong163d2612016-11-03 00:56:56 -04003254 SDE_DEBUG("crtc%d\n", crtc->base.id);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003255 SDE_EVT32_VERBOSE(DRMID(crtc));
Clarence Ipcae1bb62016-07-07 12:07:13 -04003256 sde_crtc = to_sde_crtc(crtc);
Lloyd Atkinsonaf7952d2016-06-26 22:41:26 -04003257
Alan Kwong628d19e2016-10-31 13:50:13 -04003258 drm_for_each_encoder(encoder, crtc->dev) {
3259 if (encoder->crtc != crtc)
3260 continue;
3261 sde_encoder_register_frame_event_callback(encoder,
3262 sde_crtc_frame_event_cb, (void *)crtc);
3263 }
3264
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003265 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003266 SDE_EVT32(DRMID(crtc), sde_crtc->enabled, sde_crtc->suspend,
3267 sde_crtc->vblank_requested);
3268 if (!sde_crtc->enabled && !sde_crtc->suspend &&
3269 sde_crtc->vblank_requested) {
3270 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, true);
3271 if (ret)
3272 SDE_ERROR("%s vblank enable failed: %d\n",
3273 sde_crtc->name, ret);
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003274 }
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003275 sde_crtc->enabled = true;
Ping Lic5c2e0b2017-08-02 15:17:59 -07003276
3277 /* update color processing on resume */
3278 event.type = DRM_EVENT_CRTC_POWER;
3279 event.length = sizeof(u32);
3280 sde_cp_crtc_resume(crtc);
3281 power_on = 1;
3282 msm_mode_object_event_notify(&crtc->base, crtc->dev, &event,
3283 (u8 *)&power_on);
3284
Lloyd Atkinsonb2be0c42017-07-17 16:41:00 -04003285 mutex_unlock(&sde_crtc->crtc_lock);
3286
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07003287 spin_lock_irqsave(&sde_crtc->spin_lock, flags);
3288 list_for_each_entry(node, &sde_crtc->user_event_list, list) {
3289 ret = 0;
3290 if (node->func)
3291 ret = node->func(crtc, true, &node->irq);
3292 if (ret)
3293 SDE_ERROR("%s failed to enable event %x\n",
3294 sde_crtc->name, node->event);
3295 }
3296 spin_unlock_irqrestore(&sde_crtc->spin_lock, flags);
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003297
3298 sde_crtc->power_event = sde_power_handle_register_event(
3299 &priv->phandle,
Ping Lie505f3b2017-06-19 14:19:08 -07003300 SDE_POWER_EVENT_POST_ENABLE | SDE_POWER_EVENT_POST_DISABLE |
3301 SDE_POWER_EVENT_PRE_DISABLE,
Veera Sundaram Sankaran82916e02017-03-29 18:44:22 -07003302 sde_crtc_handle_power_event, crtc, sde_crtc->name);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003303}
3304
3305struct plane_state {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003306 struct sde_plane_state *sde_pstate;
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003307 const struct drm_plane_state *drm_pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003308 int stage;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003309 u32 pipe_id;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003310};
3311
Clarence Ipc47a0692016-10-11 10:54:17 -04003312static int pstate_cmp(const void *a, const void *b)
3313{
3314 struct plane_state *pa = (struct plane_state *)a;
3315 struct plane_state *pb = (struct plane_state *)b;
3316 int rc = 0;
3317 int pa_zpos, pb_zpos;
3318
3319 pa_zpos = sde_plane_get_property(pa->sde_pstate, PLANE_PROP_ZPOS);
3320 pb_zpos = sde_plane_get_property(pb->sde_pstate, PLANE_PROP_ZPOS);
3321
3322 if (pa_zpos != pb_zpos)
3323 rc = pa_zpos - pb_zpos;
3324 else
3325 rc = pa->drm_pstate->crtc_x - pb->drm_pstate->crtc_x;
3326
3327 return rc;
3328}
3329
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003330static int _sde_crtc_excl_rect_overlap_check(struct plane_state pstates[],
3331 int cnt, int curr_cnt, struct sde_rect *excl_rect, int z_pos)
3332{
3333 struct sde_rect dst_rect, intersect;
3334 int i, rc = -EINVAL;
3335 const struct drm_plane_state *pstate;
3336
3337 /* start checking from next plane */
3338 for (i = curr_cnt; i < cnt; i++) {
3339 pstate = pstates[i].drm_pstate;
3340 POPULATE_RECT(&dst_rect, pstate->crtc_x, pstate->crtc_y,
Veera Sundaram Sankaran9d9ff912017-06-20 10:41:21 -07003341 pstate->crtc_w, pstate->crtc_h, false);
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003342 sde_kms_rect_intersect(&dst_rect, excl_rect, &intersect);
3343
3344 if (intersect.w == excl_rect->w && intersect.h == excl_rect->h
3345 /* next plane may be on same z-order */
3346 && z_pos != pstates[i].stage) {
3347 rc = 0;
3348 goto end;
3349 }
3350 }
3351
3352 SDE_ERROR("excl rect does not find top overlapping rect\n");
3353end:
3354 return rc;
3355}
3356
3357/* no input validation - caller API has all the checks */
3358static int _sde_crtc_excl_dim_layer_check(struct drm_crtc_state *state,
3359 struct plane_state pstates[], int cnt)
3360{
3361 struct sde_crtc_state *cstate = to_sde_crtc_state(state);
3362 struct drm_display_mode *mode = &state->adjusted_mode;
3363 const struct drm_plane_state *pstate;
3364 struct sde_plane_state *sde_pstate;
3365 int rc = 0, i;
3366
3367 /* Check dim layer rect bounds and stage */
3368 for (i = 0; i < cstate->num_dim_layers; i++) {
3369 if ((CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.y,
3370 cstate->dim_layer[i].rect.h, mode->vdisplay)) ||
3371 (CHECK_LAYER_BOUNDS(cstate->dim_layer[i].rect.x,
3372 cstate->dim_layer[i].rect.w, mode->hdisplay)) ||
3373 (cstate->dim_layer[i].stage >= SDE_STAGE_MAX) ||
3374 (!cstate->dim_layer[i].rect.w) ||
3375 (!cstate->dim_layer[i].rect.h)) {
3376 SDE_ERROR("invalid dim_layer:{%d,%d,%d,%d}, stage:%d\n",
3377 cstate->dim_layer[i].rect.x,
3378 cstate->dim_layer[i].rect.y,
3379 cstate->dim_layer[i].rect.w,
3380 cstate->dim_layer[i].rect.h,
3381 cstate->dim_layer[i].stage);
3382 SDE_ERROR("display: %dx%d\n", mode->hdisplay,
3383 mode->vdisplay);
3384 rc = -E2BIG;
3385 goto end;
3386 }
3387 }
3388
3389 /* this is traversing on sorted z-order pstates */
3390 for (i = 0; i < cnt; i++) {
3391 pstate = pstates[i].drm_pstate;
3392 sde_pstate = to_sde_plane_state(pstate);
3393 if (sde_pstate->excl_rect.w && sde_pstate->excl_rect.h) {
3394 /* check overlap on all top z-order */
3395 rc = _sde_crtc_excl_rect_overlap_check(pstates, cnt,
3396 i + 1, &sde_pstate->excl_rect, pstates[i].stage);
3397 if (rc)
3398 goto end;
3399 }
3400 }
3401
3402end:
3403 return rc;
3404}
3405
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003406static int _sde_crtc_check_secure_state(struct drm_crtc *crtc,
3407 struct drm_crtc_state *state)
3408{
3409 struct drm_encoder *encoder;
3410 struct sde_crtc_state *cstate;
3411 uint32_t secure;
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003412 uint32_t fb_ns = 0, fb_sec = 0, fb_sec_dir = 0;
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003413 int encoder_cnt = 0;
3414 int rc;
3415
3416 if (!crtc || !state) {
3417 SDE_ERROR("invalid arguments\n");
3418 return -EINVAL;
3419 }
3420
3421 cstate = to_sde_crtc_state(state);
3422
3423 secure = sde_crtc_get_property(cstate,
3424 CRTC_PROP_SECURITY_LEVEL);
3425
3426 rc = _sde_crtc_find_plane_fb_modes(state,
3427 &fb_ns,
3428 &fb_sec,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003429 &fb_sec_dir);
3430 if (rc)
3431 return rc;
3432
3433 /**
3434 * validate planes
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003435 * fb_sec_dir is for secure camera preview and secure display use case,
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003436 * fb_sec is for secure video playback,
3437 * fb_ns is for normal non secure use cases.
3438 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003439 if ((secure == SDE_DRM_SEC_ONLY) &&
3440 (fb_ns || fb_sec || (fb_sec && fb_sec_dir))) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003441 SDE_ERROR(
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003442 "crtc%d: invalid planes fb_modes Sec:%d, NS:%d, Sec_Dir:%d\n",
3443 crtc->base.id, fb_sec, fb_ns, fb_sec_dir);
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003444 return -EINVAL;
3445 }
3446
3447 /**
3448 * secure_crtc is not allowed in a shared toppolgy
3449 * across different encoders.
3450 */
Veera Sundaram Sankaranc5507b72017-08-25 15:25:31 -07003451 if (fb_sec_dir) {
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003452 drm_for_each_encoder(encoder, crtc->dev)
3453 if (encoder->crtc == crtc)
3454 encoder_cnt++;
3455
3456 if (encoder_cnt >
3457 MAX_ALLOWED_ENCODER_CNT_PER_SECURE_CRTC) {
3458 SDE_ERROR(
3459 "crtc%d, invalid virtual encoder crtc%d\n",
3460 crtc->base.id,
3461 encoder_cnt);
3462 return -EINVAL;
3463
3464 }
3465 }
3466 SDE_DEBUG("crtc:%d Secure validation successful\n", crtc->base.id);
3467 return 0;
3468}
3469
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003470static int sde_crtc_atomic_check(struct drm_crtc *crtc,
3471 struct drm_crtc_state *state)
3472{
Clarence Ipcae1bb62016-07-07 12:07:13 -04003473 struct sde_crtc *sde_crtc;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003474 struct plane_state pstates[SDE_STAGE_MAX * 4];
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003475 struct sde_crtc_state *cstate;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003476
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003477 const struct drm_plane_state *pstate;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003478 struct drm_plane *plane;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003479 struct drm_display_mode *mode;
3480
3481 int cnt = 0, rc = 0, mixer_width, i, z_pos;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003482
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003483 struct sde_multirect_plane_states multirect_plane[SDE_STAGE_MAX * 2];
3484 int multirect_count = 0;
3485 const struct drm_plane_state *pipe_staged[SSPP_MAX];
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003486 int left_zpos_cnt = 0, right_zpos_cnt = 0;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003487
Clarence Ipcae1bb62016-07-07 12:07:13 -04003488 if (!crtc) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003489 SDE_ERROR("invalid crtc\n");
Clarence Ipcae1bb62016-07-07 12:07:13 -04003490 return -EINVAL;
3491 }
3492
Alan Kwongcdb2f282017-03-18 13:42:06 -07003493 sde_crtc = to_sde_crtc(crtc);
3494 cstate = to_sde_crtc_state(state);
3495
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003496 if (!state->enable || !state->active) {
3497 SDE_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
3498 crtc->base.id, state->enable, state->active);
Alan Kwongcdb2f282017-03-18 13:42:06 -07003499 goto end;
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003500 }
3501
Dhaval Patelec10fad2016-08-22 14:40:48 -07003502 mode = &state->adjusted_mode;
3503 SDE_DEBUG("%s: check", sde_crtc->name);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003504
Clarence Ip90b282d2017-05-04 10:00:32 -07003505 /* force a full mode set if active state changed */
3506 if (state->active_changed)
3507 state->mode_changed = true;
3508
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003509 memset(pipe_staged, 0, sizeof(pipe_staged));
3510
Dhaval Patelec10fad2016-08-22 14:40:48 -07003511 mixer_width = sde_crtc_mixer_width(sde_crtc, mode);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003512
Lloyd Atkinson66e7dde2017-02-08 15:52:53 -05003513 _sde_crtc_setup_is_ppsplit(state);
Lloyd Atkinsona9d7e752017-01-17 16:31:43 -05003514 _sde_crtc_setup_lm_bounds(crtc, state);
3515
Abhijit Kulkarni7444a7d2017-06-21 18:53:36 -07003516 rc = _sde_crtc_check_secure_state(crtc, state);
3517 if (rc)
3518 return rc;
3519
Dhaval Patelec10fad2016-08-22 14:40:48 -07003520 /* get plane state for all drm planes associated with crtc state */
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07003521 drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003522 if (IS_ERR_OR_NULL(pstate)) {
3523 rc = PTR_ERR(pstate);
3524 SDE_ERROR("%s: failed to get plane%d state, %d\n",
3525 sde_crtc->name, plane->base.id, rc);
Alan Kwong85767282016-10-03 18:03:37 -04003526 goto end;
3527 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003528 if (cnt >= ARRAY_SIZE(pstates))
3529 continue;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003530
Dhaval Patelec10fad2016-08-22 14:40:48 -07003531 pstates[cnt].sde_pstate = to_sde_plane_state(pstate);
3532 pstates[cnt].drm_pstate = pstate;
Clarence Ipc47a0692016-10-11 10:54:17 -04003533 pstates[cnt].stage = sde_plane_get_property(
3534 pstates[cnt].sde_pstate, PLANE_PROP_ZPOS);
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003535 pstates[cnt].pipe_id = sde_plane_pipe(plane);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003536
3537 /* check dim layer stage with every plane */
3538 for (i = 0; i < cstate->num_dim_layers; i++) {
Veera Sundaram Sankaranb9ed6bd2017-07-11 19:18:03 -07003539 if (cstate->dim_layer[i].stage
3540 == (pstates[cnt].stage + SDE_STAGE_0)) {
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003541 SDE_ERROR(
3542 "plane:%d/dim_layer:%i-same stage:%d\n",
3543 plane->base.id, i,
3544 cstate->dim_layer[i].stage);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003545 rc = -EINVAL;
3546 goto end;
3547 }
3548 }
3549
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003550 if (pipe_staged[pstates[cnt].pipe_id]) {
3551 multirect_plane[multirect_count].r0 =
3552 pipe_staged[pstates[cnt].pipe_id];
3553 multirect_plane[multirect_count].r1 = pstate;
3554 multirect_count++;
3555
3556 pipe_staged[pstates[cnt].pipe_id] = NULL;
3557 } else {
3558 pipe_staged[pstates[cnt].pipe_id] = pstate;
3559 }
3560
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003561 cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003562
3563 if (CHECK_LAYER_BOUNDS(pstate->crtc_y, pstate->crtc_h,
3564 mode->vdisplay) ||
3565 CHECK_LAYER_BOUNDS(pstate->crtc_x, pstate->crtc_w,
3566 mode->hdisplay)) {
3567 SDE_ERROR("invalid vertical/horizontal destination\n");
3568 SDE_ERROR("y:%d h:%d vdisp:%d x:%d w:%d hdisp:%d\n",
3569 pstate->crtc_y, pstate->crtc_h, mode->vdisplay,
3570 pstate->crtc_x, pstate->crtc_w, mode->hdisplay);
3571 rc = -E2BIG;
3572 goto end;
3573 }
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003574 }
3575
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003576 for (i = 1; i < SSPP_MAX; i++) {
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003577 if (pipe_staged[i]) {
3578 sde_plane_clear_multirect(pipe_staged[i]);
3579
3580 if (is_sde_plane_virtual(pipe_staged[i]->plane)) {
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003581 SDE_ERROR(
3582 "r1 only virt plane:%d not supported\n",
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003583 pipe_staged[i]->plane->base.id);
Veera Sundaram Sankaran372596d2017-06-21 17:57:25 -07003584 rc = -EINVAL;
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07003585 goto end;
3586 }
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003587 }
3588 }
3589
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003590 /* assign mixer stages based on sorted zpos property */
3591 sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
3592
Dhaval Patela8d6bc62017-05-10 17:40:18 -07003593 rc = _sde_crtc_excl_dim_layer_check(state, pstates, cnt);
3594 if (rc)
3595 goto end;
3596
Clarence Ipc47a0692016-10-11 10:54:17 -04003597 if (!sde_is_custom_client()) {
3598 int stage_old = pstates[0].stage;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003599
Clarence Ipc47a0692016-10-11 10:54:17 -04003600 z_pos = 0;
3601 for (i = 0; i < cnt; i++) {
3602 if (stage_old != pstates[i].stage)
3603 ++z_pos;
3604 stage_old = pstates[i].stage;
3605 pstates[i].stage = z_pos;
3606 }
3607 }
3608
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003609 z_pos = -1;
Clarence Ipc47a0692016-10-11 10:54:17 -04003610 for (i = 0; i < cnt; i++) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003611 /* reset counts at every new blend stage */
3612 if (pstates[i].stage != z_pos) {
3613 left_zpos_cnt = 0;
3614 right_zpos_cnt = 0;
3615 z_pos = pstates[i].stage;
3616 }
Clarence Ipc47a0692016-10-11 10:54:17 -04003617
3618 /* verify z_pos setting before using it */
Clarence Ip649989a2016-10-21 14:28:34 -04003619 if (z_pos >= SDE_STAGE_MAX - SDE_STAGE_0) {
Clarence Ipc47a0692016-10-11 10:54:17 -04003620 SDE_ERROR("> %d plane stages assigned\n",
3621 SDE_STAGE_MAX - SDE_STAGE_0);
3622 rc = -EINVAL;
3623 goto end;
3624 } else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003625 if (left_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003626 SDE_ERROR("> 2 planes @ stage %d on left\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003627 z_pos);
3628 rc = -EINVAL;
3629 goto end;
3630 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003631 left_zpos_cnt++;
3632
Dhaval Patelec10fad2016-08-22 14:40:48 -07003633 } else {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003634 if (right_zpos_cnt == 2) {
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003635 SDE_ERROR("> 2 planes @ stage %d on right\n",
Dhaval Patelec10fad2016-08-22 14:40:48 -07003636 z_pos);
3637 rc = -EINVAL;
3638 goto end;
3639 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003640 right_zpos_cnt++;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003641 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003642
Clarence Ipc47a0692016-10-11 10:54:17 -04003643 pstates[i].sde_pstate->stage = z_pos + SDE_STAGE_0;
Dhaval Patelec10fad2016-08-22 14:40:48 -07003644 SDE_DEBUG("%s: zpos %d", sde_crtc->name, z_pos);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003645 }
3646
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003647 for (i = 0; i < multirect_count; i++) {
3648 if (sde_plane_validate_multirect_v2(&multirect_plane[i])) {
3649 SDE_ERROR(
3650 "multirect validation failed for planes (%d - %d)\n",
3651 multirect_plane[i].r0->plane->base.id,
3652 multirect_plane[i].r1->plane->base.id);
3653 rc = -EINVAL;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003654 goto end;
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003655 }
3656 }
3657
Alan Kwong9aa061c2016-11-06 21:17:12 -05003658 rc = sde_core_perf_crtc_check(crtc, state);
3659 if (rc) {
3660 SDE_ERROR("crtc%d failed performance check %d\n",
3661 crtc->base.id, rc);
3662 goto end;
3663 }
3664
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003665 /* validate source split:
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003666 * use pstates sorted by stage to check planes on same stage
3667 * we assume that all pipes are in source split so its valid to compare
3668 * without taking into account left/right mixer placement
3669 */
3670 for (i = 1; i < cnt; i++) {
3671 struct plane_state *prv_pstate, *cur_pstate;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003672 struct sde_rect left_rect, right_rect;
3673 int32_t left_pid, right_pid;
3674 int32_t stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003675
3676 prv_pstate = &pstates[i - 1];
3677 cur_pstate = &pstates[i];
3678 if (prv_pstate->stage != cur_pstate->stage)
3679 continue;
3680
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003681 stage = cur_pstate->stage;
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003682
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003683 left_pid = prv_pstate->sde_pstate->base.plane->base.id;
3684 POPULATE_RECT(&left_rect, prv_pstate->drm_pstate->crtc_x,
3685 prv_pstate->drm_pstate->crtc_y,
3686 prv_pstate->drm_pstate->crtc_w,
3687 prv_pstate->drm_pstate->crtc_h, false);
3688
3689 right_pid = cur_pstate->sde_pstate->base.plane->base.id;
3690 POPULATE_RECT(&right_rect, cur_pstate->drm_pstate->crtc_x,
3691 cur_pstate->drm_pstate->crtc_y,
3692 cur_pstate->drm_pstate->crtc_w,
3693 cur_pstate->drm_pstate->crtc_h, false);
3694
3695 if (right_rect.x < left_rect.x) {
3696 swap(left_pid, right_pid);
3697 swap(left_rect, right_rect);
3698 }
3699
3700 /**
3701 * - planes are enumerated in pipe-priority order such that
3702 * planes with lower drm_id must be left-most in a shared
3703 * blend-stage when using source split.
3704 * - planes in source split must be contiguous in width
3705 * - planes in source split must have same dest yoff and height
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003706 */
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003707 if (right_pid < left_pid) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003708 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003709 "invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
3710 stage, left_pid, right_pid);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003711 rc = -EINVAL;
3712 goto end;
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003713 } else if (right_rect.x != (left_rect.x + left_rect.w)) {
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003714 SDE_ERROR(
Jeykumar Sankaranaaaa0712017-06-12 17:59:16 -07003715 "non-contiguous coordinates for src split. stage: %d left: %d - %d right: %d - %d\n",
3716 stage, left_rect.x, left_rect.w,
3717 right_rect.x, right_rect.w);
3718 rc = -EINVAL;
3719 goto end;
3720 } else if ((left_rect.y != right_rect.y) ||
3721 (left_rect.h != right_rect.h)) {
3722 SDE_ERROR(
3723 "source split at stage: %d. invalid yoff/height: l_y: %d r_y: %d l_h: %d r_h: %d\n",
3724 stage, left_rect.y, right_rect.y,
3725 left_rect.h, right_rect.h);
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003726 rc = -EINVAL;
3727 goto end;
3728 }
3729 }
3730
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003731 rc = _sde_crtc_check_rois(crtc, state);
3732 if (rc) {
3733 SDE_ERROR("crtc%d failed roi check %d\n", crtc->base.id, rc);
3734 goto end;
3735 }
Lloyd Atkinson629ce1f2016-10-27 16:50:26 -04003736
Dhaval Patelec10fad2016-08-22 14:40:48 -07003737end:
Alan Kwongcdb2f282017-03-18 13:42:06 -07003738 _sde_crtc_rp_free_unused(&cstate->rp);
Dhaval Patelec10fad2016-08-22 14:40:48 -07003739 return rc;
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04003740}
3741
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003742int sde_crtc_vblank(struct drm_crtc *crtc, bool en)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003743{
Clarence Ip7f70ce42017-03-20 06:53:46 -07003744 struct sde_crtc *sde_crtc;
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003745 int ret;
Abhijit Kulkarni7acb3262016-07-05 15:27:25 -04003746
Clarence Ip7f70ce42017-03-20 06:53:46 -07003747 if (!crtc) {
3748 SDE_ERROR("invalid crtc\n");
3749 return -EINVAL;
3750 }
3751 sde_crtc = to_sde_crtc(crtc);
3752
3753 mutex_lock(&sde_crtc->crtc_lock);
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003754 SDE_EVT32(DRMID(&sde_crtc->base), en, sde_crtc->enabled,
3755 sde_crtc->suspend, sde_crtc->vblank_requested);
3756 if (sde_crtc->enabled && !sde_crtc->suspend) {
3757 ret = _sde_crtc_vblank_enable_no_lock(sde_crtc, en);
3758 if (ret)
3759 SDE_ERROR("%s vblank enable failed: %d\n",
3760 sde_crtc->name, ret);
3761 }
3762 sde_crtc->vblank_requested = en;
Clarence Ip7f70ce42017-03-20 06:53:46 -07003763 mutex_unlock(&sde_crtc->crtc_lock);
Clarence Ip9728a1d2017-04-18 22:22:13 -04003764
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04003765 return 0;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003766}
3767
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003768void sde_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
3769{
3770 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
3771
Alan Kwong163d2612016-11-03 00:56:56 -04003772 SDE_DEBUG("%s: cancel: %p\n", sde_crtc->name, file);
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003773 _sde_crtc_complete_flip(crtc, file);
Lloyd Atkinson5217336c2016-09-15 18:21:18 -04003774}
3775
Clarence Ip7a753bb2016-07-07 11:47:44 -04003776/**
3777 * sde_crtc_install_properties - install all drm properties for crtc
3778 * @crtc: Pointer to drm crtc structure
3779 */
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003780static void sde_crtc_install_properties(struct drm_crtc *crtc,
3781 struct sde_mdss_cfg *catalog)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07003782{
Clarence Ip7a753bb2016-07-07 11:47:44 -04003783 struct sde_crtc *sde_crtc;
3784 struct drm_device *dev;
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003785 struct sde_kms_info *info;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003786 struct sde_kms *sde_kms;
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003787 static const struct drm_prop_enum_list e_secure_level[] = {
3788 {SDE_DRM_SEC_NON_SEC, "sec_and_non_sec"},
3789 {SDE_DRM_SEC_ONLY, "sec_only"},
3790 };
Clarence Ip7a753bb2016-07-07 11:47:44 -04003791
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04003792 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003793
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003794 if (!crtc || !catalog) {
3795 SDE_ERROR("invalid crtc or catalog\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003796 return;
3797 }
3798
3799 sde_crtc = to_sde_crtc(crtc);
3800 dev = crtc->dev;
Alan Kwong9aa061c2016-11-06 21:17:12 -05003801 sde_kms = _sde_crtc_get_kms(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003802
Narendra Muppallaec11a0a2017-06-15 15:35:17 -07003803 if (!sde_kms) {
3804 SDE_ERROR("invalid argument\n");
3805 return;
3806 }
3807
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003808 info = kzalloc(sizeof(struct sde_kms_info), GFP_KERNEL);
3809 if (!info) {
3810 SDE_ERROR("failed to allocate info memory\n");
3811 return;
3812 }
3813
Clarence Ip7a753bb2016-07-07 11:47:44 -04003814 /* range properties */
3815 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patel4e574842016-08-23 15:11:37 -07003816 "input_fence_timeout", 0x0, 0, SDE_CRTC_MAX_INPUT_FENCE_TIMEOUT,
3817 SDE_CRTC_INPUT_FENCE_TIMEOUT, CRTC_PROP_INPUT_FENCE_TIMEOUT);
3818
3819 msm_property_install_range(&sde_crtc->property_info, "output_fence",
3820 0x0, 0, INR_OPEN_MAX, 0x0, CRTC_PROP_OUTPUT_FENCE);
Clarence Ip1d9728b2016-09-01 11:10:54 -04003821
3822 msm_property_install_range(&sde_crtc->property_info,
3823 "output_fence_offset", 0x0, 0, 1, 0,
3824 CRTC_PROP_OUTPUT_FENCE_OFFSET);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003825
Alan Kwong9aa061c2016-11-06 21:17:12 -05003826 msm_property_install_range(&sde_crtc->property_info,
3827 "core_clk", 0x0, 0, U64_MAX,
3828 sde_kms->perf.max_core_clk_rate,
3829 CRTC_PROP_CORE_CLK);
3830 msm_property_install_range(&sde_crtc->property_info,
3831 "core_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003832 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003833 CRTC_PROP_CORE_AB);
3834 msm_property_install_range(&sde_crtc->property_info,
3835 "core_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003836 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong9aa061c2016-11-06 21:17:12 -05003837 CRTC_PROP_CORE_IB);
Alan Kwong4aacd532017-02-04 18:51:33 -08003838 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003839 "llcc_ab", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003840 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003841 CRTC_PROP_LLCC_AB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003842 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong0230a102017-05-16 11:36:44 -07003843 "llcc_ib", 0x0, 0, U64_MAX,
Alan Kwongff30f4a2017-05-23 12:02:00 -07003844 catalog->perf.max_bw_high * 1000ULL,
Alan Kwong0230a102017-05-16 11:36:44 -07003845 CRTC_PROP_LLCC_IB);
3846 msm_property_install_range(&sde_crtc->property_info,
3847 "dram_ab", 0x0, 0, U64_MAX,
3848 catalog->perf.max_bw_high * 1000ULL,
3849 CRTC_PROP_DRAM_AB);
3850 msm_property_install_range(&sde_crtc->property_info,
3851 "dram_ib", 0x0, 0, U64_MAX,
3852 catalog->perf.max_bw_high * 1000ULL,
3853 CRTC_PROP_DRAM_IB);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003854 msm_property_install_range(&sde_crtc->property_info,
Alan Kwong4aacd532017-02-04 18:51:33 -08003855 "rot_prefill_bw", 0, 0, U64_MAX,
3856 catalog->perf.max_bw_high * 1000ULL,
3857 CRTC_PROP_ROT_PREFILL_BW);
Alan Kwong8c176bf2017-02-09 19:34:32 -08003858 msm_property_install_range(&sde_crtc->property_info,
3859 "rot_clk", 0, 0, U64_MAX,
3860 sde_kms->perf.max_core_clk_rate,
3861 CRTC_PROP_ROT_CLK);
Alan Kwong9aa061c2016-11-06 21:17:12 -05003862
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05303863 msm_property_install_range(&sde_crtc->property_info,
Dhaval Patele17e0ee2017-08-23 18:01:42 -07003864 "idle_timeout", IDLE_TIMEOUT, 0, U64_MAX, 0,
3865 CRTC_PROP_IDLE_TIMEOUT);
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05303866
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003867 msm_property_install_blob(&sde_crtc->property_info, "capabilities",
3868 DRM_MODE_PROP_IMMUTABLE, CRTC_PROP_INFO);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003869
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003870 msm_property_install_volatile_range(&sde_crtc->property_info,
3871 "sde_drm_roi_v1", 0x0, 0, ~0, 0, CRTC_PROP_ROI_V1);
3872
Abhijit Kulkarni50d69442017-04-11 19:50:47 -07003873 msm_property_install_enum(&sde_crtc->property_info, "security_level",
3874 0x0, 0, e_secure_level,
3875 ARRAY_SIZE(e_secure_level),
3876 CRTC_PROP_SECURITY_LEVEL);
3877
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003878 sde_kms_info_reset(info);
3879
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07003880 if (catalog->has_dim_layer) {
3881 msm_property_install_volatile_range(&sde_crtc->property_info,
3882 "dim_layer_v1", 0x0, 0, ~0, 0, CRTC_PROP_DIM_LAYER_V1);
3883 sde_kms_info_add_keyint(info, "dim_layer_v1_max_layers",
3884 SDE_MAX_DIM_LAYERS);
3885 }
3886
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003887 sde_kms_info_add_keyint(info, "hw_version", catalog->hwversion);
3888 sde_kms_info_add_keyint(info, "max_linewidth",
3889 catalog->max_mixer_width);
3890 sde_kms_info_add_keyint(info, "max_blendstages",
3891 catalog->max_mixer_blendstages);
3892 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED2)
3893 sde_kms_info_add_keystr(info, "qseed_type", "qseed2");
3894 if (catalog->qseed_type == SDE_SSPP_SCALER_QSEED3)
3895 sde_kms_info_add_keystr(info, "qseed_type", "qseed3");
Jeykumar Sankaran2e655032017-02-04 14:05:45 -08003896
3897 if (sde_is_custom_client()) {
3898 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V1)
3899 sde_kms_info_add_keystr(info,
3900 "smart_dma_rev", "smart_dma_v1");
3901 if (catalog->smart_dma_rev == SDE_SSPP_SMART_DMA_V2)
3902 sde_kms_info_add_keystr(info,
3903 "smart_dma_rev", "smart_dma_v2");
3904 }
3905
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003906 sde_kms_info_add_keyint(info, "has_src_split", catalog->has_src_split);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003907 if (catalog->perf.max_bw_low)
3908 sde_kms_info_add_keyint(info, "max_bandwidth_low",
Alan Kwong6259a382017-04-04 06:18:02 -07003909 catalog->perf.max_bw_low * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003910 if (catalog->perf.max_bw_high)
3911 sde_kms_info_add_keyint(info, "max_bandwidth_high",
Alan Kwong6259a382017-04-04 06:18:02 -07003912 catalog->perf.max_bw_high * 1000LL);
Narendra Muppallaa50934b2017-08-15 19:43:37 -07003913 if (catalog->perf.min_core_ib)
3914 sde_kms_info_add_keyint(info, "min_core_ib",
3915 catalog->perf.min_core_ib * 1000LL);
3916 if (catalog->perf.min_llcc_ib)
3917 sde_kms_info_add_keyint(info, "min_llcc_ib",
3918 catalog->perf.min_llcc_ib * 1000LL);
3919 if (catalog->perf.min_dram_ib)
3920 sde_kms_info_add_keyint(info, "min_dram_ib",
3921 catalog->perf.min_dram_ib * 1000LL);
Alan Kwong2f84f8a2016-12-29 13:07:47 -05003922 if (sde_kms->perf.max_core_clk_rate)
3923 sde_kms_info_add_keyint(info, "max_mdp_clk",
3924 sde_kms->perf.max_core_clk_rate);
Alan Kwong6259a382017-04-04 06:18:02 -07003925 sde_kms_info_add_keystr(info, "core_ib_ff",
3926 catalog->perf.core_ib_ff);
3927 sde_kms_info_add_keystr(info, "core_clk_ff",
3928 catalog->perf.core_clk_ff);
3929 sde_kms_info_add_keystr(info, "comp_ratio_rt",
3930 catalog->perf.comp_ratio_rt);
3931 sde_kms_info_add_keystr(info, "comp_ratio_nrt",
3932 catalog->perf.comp_ratio_nrt);
3933 sde_kms_info_add_keyint(info, "dest_scale_prefill_lines",
3934 catalog->perf.dest_scale_prefill_lines);
3935 sde_kms_info_add_keyint(info, "undersized_prefill_lines",
3936 catalog->perf.undersized_prefill_lines);
3937 sde_kms_info_add_keyint(info, "macrotile_prefill_lines",
3938 catalog->perf.macrotile_prefill_lines);
3939 sde_kms_info_add_keyint(info, "yuv_nv12_prefill_lines",
3940 catalog->perf.yuv_nv12_prefill_lines);
3941 sde_kms_info_add_keyint(info, "linear_prefill_lines",
3942 catalog->perf.linear_prefill_lines);
3943 sde_kms_info_add_keyint(info, "downscaling_prefill_lines",
3944 catalog->perf.downscaling_prefill_lines);
3945 sde_kms_info_add_keyint(info, "xtra_prefill_lines",
3946 catalog->perf.xtra_prefill_lines);
3947 sde_kms_info_add_keyint(info, "amortizable_threshold",
3948 catalog->perf.amortizable_threshold);
3949 sde_kms_info_add_keyint(info, "min_prefill_lines",
3950 catalog->perf.min_prefill_lines);
3951
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003952 msm_property_set_blob(&sde_crtc->property_info, &sde_crtc->blob_info,
Narendra Muppalla22d17252017-05-31 15:13:39 -07003953 info->data, SDE_KMS_INFO_DATALEN(info), CRTC_PROP_INFO);
Dhaval Patele4a5dda2016-10-13 19:29:30 -07003954
3955 kfree(info);
Clarence Ip7a753bb2016-07-07 11:47:44 -04003956}
3957
3958/**
3959 * sde_crtc_atomic_set_property - atomically set a crtc drm property
3960 * @crtc: Pointer to drm crtc structure
3961 * @state: Pointer to drm crtc state structure
3962 * @property: Pointer to targeted drm property
3963 * @val: Updated property value
3964 * @Returns: Zero on success
3965 */
3966static int sde_crtc_atomic_set_property(struct drm_crtc *crtc,
3967 struct drm_crtc_state *state,
3968 struct drm_property *property,
3969 uint64_t val)
3970{
3971 struct sde_crtc *sde_crtc;
3972 struct sde_crtc_state *cstate;
Clarence Ipcae1bb62016-07-07 12:07:13 -04003973 int idx, ret = -EINVAL;
Clarence Ip7a753bb2016-07-07 11:47:44 -04003974
3975 if (!crtc || !state || !property) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07003976 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04003977 } else {
3978 sde_crtc = to_sde_crtc(crtc);
3979 cstate = to_sde_crtc_state(state);
3980 ret = msm_property_atomic_set(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04003981 &cstate->property_state, property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04003982 if (!ret) {
3983 idx = msm_property_index(&sde_crtc->property_info,
3984 property);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003985 switch (idx) {
3986 case CRTC_PROP_INPUT_FENCE_TIMEOUT:
Clarence Ipcae1bb62016-07-07 12:07:13 -04003987 _sde_crtc_set_input_fence_timeout(cstate);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08003988 break;
3989 case CRTC_PROP_DIM_LAYER_V1:
3990 _sde_crtc_set_dim_layer_v1(cstate, (void *)val);
3991 break;
Lloyd Atkinson8ba47032017-03-22 17:13:32 -04003992 case CRTC_PROP_ROI_V1:
3993 ret = _sde_crtc_set_roi_v1(state, (void *)val);
3994 break;
Alan Kwong8411a9112017-06-06 19:29:01 -04003995 case CRTC_PROP_CORE_CLK:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003996 case CRTC_PROP_CORE_AB:
3997 case CRTC_PROP_CORE_IB:
Alan Kwongff30f4a2017-05-23 12:02:00 -07003998 cstate->bw_control = true;
3999 break;
Alan Kwong0230a102017-05-16 11:36:44 -07004000 case CRTC_PROP_LLCC_AB:
4001 case CRTC_PROP_LLCC_IB:
4002 case CRTC_PROP_DRAM_AB:
4003 case CRTC_PROP_DRAM_IB:
4004 cstate->bw_control = true;
4005 cstate->bw_split_vote = true;
4006 break;
Dhaval Patele17e0ee2017-08-23 18:01:42 -07004007 case CRTC_PROP_IDLE_TIMEOUT:
4008 _sde_crtc_set_idle_timeout(crtc, val);
Veera Sundaram Sankaran3171ff82017-01-04 14:34:47 -08004009 default:
4010 /* nothing to do */
4011 break;
4012 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004013 } else {
4014 ret = sde_cp_crtc_set_property(crtc,
4015 property, val);
Clarence Ipcae1bb62016-07-07 12:07:13 -04004016 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004017 if (ret)
4018 DRM_ERROR("failed to set the property\n");
Alan Kwongcdb2f282017-03-18 13:42:06 -07004019
4020 SDE_DEBUG("crtc%d %s[%d] <= 0x%llx ret=%d\n", crtc->base.id,
4021 property->name, property->base.id, val, ret);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004022 }
4023
4024 return ret;
4025}
4026
4027/**
4028 * sde_crtc_set_property - set a crtc drm property
4029 * @crtc: Pointer to drm crtc structure
4030 * @property: Pointer to targeted drm property
4031 * @val: Updated property value
4032 * @Returns: Zero on success
4033 */
4034static int sde_crtc_set_property(struct drm_crtc *crtc,
4035 struct drm_property *property, uint64_t val)
4036{
Lloyd Atkinson4f1c8692016-09-14 14:04:25 -04004037 SDE_DEBUG("\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004038
4039 return sde_crtc_atomic_set_property(crtc, crtc->state, property, val);
4040}
4041
4042/**
4043 * sde_crtc_atomic_get_property - retrieve a crtc drm property
4044 * @crtc: Pointer to drm crtc structure
4045 * @state: Pointer to drm crtc state structure
4046 * @property: Pointer to targeted drm property
4047 * @val: Pointer to variable for receiving property value
4048 * @Returns: Zero on success
4049 */
4050static int sde_crtc_atomic_get_property(struct drm_crtc *crtc,
4051 const struct drm_crtc_state *state,
4052 struct drm_property *property,
4053 uint64_t *val)
4054{
4055 struct sde_crtc *sde_crtc;
4056 struct sde_crtc_state *cstate;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004057 struct drm_encoder *encoder;
Clarence Ip24f80662016-06-13 19:05:32 -04004058 int i, ret = -EINVAL;
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004059 bool conn_offset = 0;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004060 bool is_cmd = true;
Clarence Ip7a753bb2016-07-07 11:47:44 -04004061
4062 if (!crtc || !state) {
Dhaval Patelec10fad2016-08-22 14:40:48 -07004063 SDE_ERROR("invalid argument(s)\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004064 } else {
4065 sde_crtc = to_sde_crtc(crtc);
4066 cstate = to_sde_crtc_state(state);
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004067
4068 for (i = 0; i < cstate->num_connectors; ++i) {
4069 conn_offset = sde_connector_needs_offset(
4070 cstate->connectors[i]);
4071 if (conn_offset)
4072 break;
4073 }
4074
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004075 /**
4076 * set the cmd flag only when all the encoders attached
4077 * to the crtc are in cmd mode. Consider all other cases
4078 * as video mode.
4079 */
4080 drm_for_each_encoder(encoder, crtc->dev) {
4081 if (encoder->crtc == crtc)
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05304082 is_cmd = sde_encoder_check_mode(encoder,
4083 MSM_DISPLAY_CAP_CMD_MODE);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004084 }
4085
Clarence Ip24f80662016-06-13 19:05:32 -04004086 i = msm_property_index(&sde_crtc->property_info, property);
4087 if (i == CRTC_PROP_OUTPUT_FENCE) {
Dhaval Patel39323d42017-03-01 23:48:24 -08004088 uint32_t offset = sde_crtc_get_property(cstate,
Clarence Ip1d9728b2016-09-01 11:10:54 -04004089 CRTC_PROP_OUTPUT_FENCE_OFFSET);
4090
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004091 /**
4092 * set the offset to 0 only for cmd mode panels, so
4093 * the release fence for the current frame can be
4094 * triggered right after PP_DONE interrupt.
4095 */
4096 offset = is_cmd ? 0 : (offset + conn_offset);
4097
Dhaval Patel5cb59be2017-04-20 20:00:56 -07004098 ret = sde_fence_create(&sde_crtc->output_fence, val,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07004099 offset);
Clarence Ip1d9728b2016-09-01 11:10:54 -04004100 if (ret)
4101 SDE_ERROR("fence create failed\n");
Clarence Ip24f80662016-06-13 19:05:32 -04004102 } else {
4103 ret = msm_property_atomic_get(&sde_crtc->property_info,
Clarence Ip4a2955d2017-07-04 18:04:33 -04004104 &cstate->property_state,
4105 property, val);
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004106 if (ret)
4107 ret = sde_cp_crtc_get_property(crtc,
4108 property, val);
Clarence Ip24f80662016-06-13 19:05:32 -04004109 }
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004110 if (ret)
4111 DRM_ERROR("get property failed\n");
Clarence Ip7a753bb2016-07-07 11:47:44 -04004112 }
Clarence Ip7a753bb2016-07-07 11:47:44 -04004113 return ret;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004114}
4115
Alan Kwong67a3f792016-11-01 23:16:53 -04004116#ifdef CONFIG_DEBUG_FS
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004117static int _sde_debugfs_status_show(struct seq_file *s, void *data)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004118{
4119 struct sde_crtc *sde_crtc;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004120 struct sde_plane_state *pstate = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004121 struct sde_crtc_mixer *m;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004122
4123 struct drm_crtc *crtc;
4124 struct drm_plane *plane;
4125 struct drm_display_mode *mode;
4126 struct drm_framebuffer *fb;
4127 struct drm_plane_state *state;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004128 struct sde_crtc_state *cstate;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004129
4130 int i, out_width;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004131
4132 if (!s || !s->private)
4133 return -EINVAL;
4134
4135 sde_crtc = s->private;
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004136 crtc = &sde_crtc->base;
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004137 cstate = to_sde_crtc_state(crtc->state);
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004138
4139 mutex_lock(&sde_crtc->crtc_lock);
4140 mode = &crtc->state->adjusted_mode;
4141 out_width = sde_crtc_mixer_width(sde_crtc, mode);
4142
4143 seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
4144 mode->hdisplay, mode->vdisplay);
4145
4146 seq_puts(s, "\n");
4147
Clarence Ip8f7366c2016-07-05 12:15:26 -04004148 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04004149 m = &sde_crtc->mixers[i];
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004150 if (!m->hw_lm)
4151 seq_printf(s, "\tmixer[%d] has no lm\n", i);
4152 else if (!m->hw_ctl)
4153 seq_printf(s, "\tmixer[%d] has no ctl\n", i);
4154 else
4155 seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
4156 m->hw_lm->idx - LM_0, m->hw_ctl->idx - CTL_0,
4157 out_width, mode->vdisplay);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004158 }
Dhaval Patel44f12472016-08-29 12:19:47 -07004159
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004160 seq_puts(s, "\n");
Dhaval Patel48c76022016-09-01 17:51:23 -07004161
Veera Sundaram Sankaran2cb064f2017-05-05 14:12:17 -07004162 for (i = 0; i < cstate->num_dim_layers; i++) {
4163 struct sde_hw_dim_layer *dim_layer = &cstate->dim_layer[i];
4164
4165 seq_printf(s, "\tdim_layer:%d] stage:%d flags:%d\n",
4166 i, dim_layer->stage, dim_layer->flags);
4167 seq_printf(s, "\tdst_x:%d dst_y:%d dst_w:%d dst_h:%d\n",
4168 dim_layer->rect.x, dim_layer->rect.y,
4169 dim_layer->rect.w, dim_layer->rect.h);
4170 seq_printf(s,
4171 "\tcolor_0:%d color_1:%d color_2:%d color_3:%d\n",
4172 dim_layer->color_fill.color_0,
4173 dim_layer->color_fill.color_1,
4174 dim_layer->color_fill.color_2,
4175 dim_layer->color_fill.color_3);
4176 seq_puts(s, "\n");
4177 }
4178
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004179 drm_atomic_crtc_for_each_plane(plane, crtc) {
4180 pstate = to_sde_plane_state(plane->state);
4181 state = plane->state;
4182
4183 if (!pstate || !state)
4184 continue;
4185
4186 seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
4187 pstate->stage);
4188
4189 if (plane->state->fb) {
4190 fb = plane->state->fb;
4191
4192 seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u bpp:%d\n",
4193 fb->base.id, (char *) &fb->pixel_format,
4194 fb->width, fb->height, fb->bits_per_pixel);
4195
4196 seq_puts(s, "\t");
4197 for (i = 0; i < ARRAY_SIZE(fb->modifier); i++)
4198 seq_printf(s, "modifier[%d]:%8llu ", i,
4199 fb->modifier[i]);
4200 seq_puts(s, "\n");
4201
4202 seq_puts(s, "\t");
4203 for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
4204 seq_printf(s, "pitches[%d]:%8u ", i,
4205 fb->pitches[i]);
4206 seq_puts(s, "\n");
4207
4208 seq_puts(s, "\t");
4209 for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
4210 seq_printf(s, "offsets[%d]:%8u ", i,
4211 fb->offsets[i]);
Dhaval Patel48c76022016-09-01 17:51:23 -07004212 seq_puts(s, "\n");
4213 }
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004214
4215 seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
4216 state->src_x, state->src_y, state->src_w, state->src_h);
4217
4218 seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
4219 state->crtc_x, state->crtc_y, state->crtc_w,
4220 state->crtc_h);
Jeykumar Sankarane964dc72017-05-10 19:26:43 -07004221 seq_printf(s, "\tmultirect: mode: %d index: %d\n",
4222 pstate->multirect_mode, pstate->multirect_index);
Veera Sundaram Sankaran58e12812017-05-05 11:51:09 -07004223
4224 seq_printf(s, "\texcl_rect: x:%4d y:%4d w:%4d h:%4d\n",
4225 pstate->excl_rect.x, pstate->excl_rect.y,
4226 pstate->excl_rect.w, pstate->excl_rect.h);
4227
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004228 seq_puts(s, "\n");
Clarence Ip8f7366c2016-07-05 12:15:26 -04004229 }
Alan Kwong07da0982016-11-04 12:57:45 -04004230
4231 if (sde_crtc->vblank_cb_count) {
4232 ktime_t diff = ktime_sub(ktime_get(), sde_crtc->vblank_cb_time);
4233 s64 diff_ms = ktime_to_ms(diff);
4234 s64 fps = diff_ms ? DIV_ROUND_CLOSEST(
4235 sde_crtc->vblank_cb_count * 1000, diff_ms) : 0;
4236
4237 seq_printf(s,
Dhaval Pateld67cf4a2017-06-14 18:08:32 -07004238 "vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
4239 fps, sde_crtc->vblank_cb_count,
4240 ktime_to_ms(diff), sde_crtc->play_count);
Alan Kwong07da0982016-11-04 12:57:45 -04004241
4242 /* reset time & count for next measurement */
4243 sde_crtc->vblank_cb_count = 0;
4244 sde_crtc->vblank_cb_time = ktime_set(0, 0);
4245 }
4246
Lloyd Atkinsondcb1c4a2017-07-27 10:52:09 -04004247 seq_printf(s, "vblank_enable:%d\n", sde_crtc->vblank_requested);
Alan Kwong07da0982016-11-04 12:57:45 -04004248
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004249 mutex_unlock(&sde_crtc->crtc_lock);
4250
Clarence Ip8f7366c2016-07-05 12:15:26 -04004251 return 0;
4252}
4253
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004254static int _sde_debugfs_status_open(struct inode *inode, struct file *file)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004255{
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004256 return single_open(file, _sde_debugfs_status_show, inode->i_private);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004257}
4258
Dhaval Patelf9245d62017-03-28 16:24:00 -07004259static ssize_t _sde_crtc_misr_setup(struct file *file,
4260 const char __user *user_buf, size_t count, loff_t *ppos)
4261{
4262 struct sde_crtc *sde_crtc;
4263 struct sde_crtc_mixer *m;
4264 int i = 0, rc;
4265 char buf[MISR_BUFF_SIZE + 1];
4266 u32 frame_count, enable;
4267 size_t buff_copy;
4268
4269 if (!file || !file->private_data)
4270 return -EINVAL;
4271
4272 sde_crtc = file->private_data;
4273 buff_copy = min_t(size_t, count, MISR_BUFF_SIZE);
4274 if (copy_from_user(buf, user_buf, buff_copy)) {
4275 SDE_ERROR("buffer copy failed\n");
4276 return -EINVAL;
4277 }
4278
4279 buf[buff_copy] = 0; /* end of string */
4280
4281 if (sscanf(buf, "%u %u", &enable, &frame_count) != 2)
4282 return -EINVAL;
4283
4284 rc = _sde_crtc_power_enable(sde_crtc, true);
4285 if (rc)
4286 return rc;
4287
4288 mutex_lock(&sde_crtc->crtc_lock);
4289 sde_crtc->misr_enable = enable;
Dhaval Patel010f5172017-08-01 22:40:09 -07004290 sde_crtc->misr_frame_count = frame_count;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004291 for (i = 0; i < sde_crtc->num_mixers; ++i) {
Dhaval Patel010f5172017-08-01 22:40:09 -07004292 sde_crtc->misr_data[i] = 0;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004293 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004294 if (!m->hw_lm || !m->hw_lm->ops.setup_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004295 continue;
4296
4297 m->hw_lm->ops.setup_misr(m->hw_lm, enable, frame_count);
4298 }
4299 mutex_unlock(&sde_crtc->crtc_lock);
4300 _sde_crtc_power_enable(sde_crtc, false);
4301
4302 return count;
4303}
4304
4305static ssize_t _sde_crtc_misr_read(struct file *file,
4306 char __user *user_buff, size_t count, loff_t *ppos)
4307{
4308 struct sde_crtc *sde_crtc;
4309 struct sde_crtc_mixer *m;
4310 int i = 0, rc;
Dhaval Patel010f5172017-08-01 22:40:09 -07004311 u32 misr_status;
Dhaval Patelf9245d62017-03-28 16:24:00 -07004312 ssize_t len = 0;
4313 char buf[MISR_BUFF_SIZE + 1] = {'\0'};
4314
4315 if (*ppos)
4316 return 0;
4317
4318 if (!file || !file->private_data)
4319 return -EINVAL;
4320
4321 sde_crtc = file->private_data;
4322 rc = _sde_crtc_power_enable(sde_crtc, true);
4323 if (rc)
4324 return rc;
4325
4326 mutex_lock(&sde_crtc->crtc_lock);
4327 if (!sde_crtc->misr_enable) {
4328 len += snprintf(buf + len, MISR_BUFF_SIZE - len,
4329 "disabled\n");
4330 goto buff_check;
4331 }
4332
4333 for (i = 0; i < sde_crtc->num_mixers; ++i) {
4334 m = &sde_crtc->mixers[i];
Dhaval Patel010f5172017-08-01 22:40:09 -07004335 if (!m->hw_lm || !m->hw_lm->ops.collect_misr)
Dhaval Patelf9245d62017-03-28 16:24:00 -07004336 continue;
4337
Dhaval Patel010f5172017-08-01 22:40:09 -07004338 misr_status = m->hw_lm->ops.collect_misr(m->hw_lm);
4339 sde_crtc->misr_data[i] = misr_status ? misr_status :
4340 sde_crtc->misr_data[i];
Dhaval Patelf9245d62017-03-28 16:24:00 -07004341 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "lm idx:%d\n",
4342 m->hw_lm->idx - LM_0);
4343 len += snprintf(buf + len, MISR_BUFF_SIZE - len, "0x%x\n",
Dhaval Patel010f5172017-08-01 22:40:09 -07004344 sde_crtc->misr_data[i]);
Dhaval Patelf9245d62017-03-28 16:24:00 -07004345 }
4346
4347buff_check:
4348 if (count <= len) {
4349 len = 0;
4350 goto end;
4351 }
4352
4353 if (copy_to_user(user_buff, buf, len)) {
4354 len = -EFAULT;
4355 goto end;
4356 }
4357
4358 *ppos += len; /* increase offset */
4359
4360end:
4361 mutex_unlock(&sde_crtc->crtc_lock);
4362 _sde_crtc_power_enable(sde_crtc, false);
4363 return len;
4364}
4365
4366#define DEFINE_SDE_DEBUGFS_SEQ_FOPS(__prefix) \
Alan Kwong67a3f792016-11-01 23:16:53 -04004367static int __prefix ## _open(struct inode *inode, struct file *file) \
4368{ \
4369 return single_open(file, __prefix ## _show, inode->i_private); \
4370} \
4371static const struct file_operations __prefix ## _fops = { \
4372 .owner = THIS_MODULE, \
4373 .open = __prefix ## _open, \
4374 .release = single_release, \
4375 .read = seq_read, \
4376 .llseek = seq_lseek, \
4377}
4378
4379static int sde_crtc_debugfs_state_show(struct seq_file *s, void *v)
4380{
4381 struct drm_crtc *crtc = (struct drm_crtc *) s->private;
Alan Kwong751cf462017-06-08 10:26:46 -04004382 struct sde_crtc *sde_crtc = to_sde_crtc(crtc);
Alan Kwong67a3f792016-11-01 23:16:53 -04004383 struct sde_crtc_state *cstate = to_sde_crtc_state(crtc->state);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004384 struct sde_crtc_res *res;
Alan Kwong310e9b02017-08-03 02:04:07 -04004385 struct sde_crtc_respool *rp;
Alan Kwong0230a102017-05-16 11:36:44 -07004386 int i;
Alan Kwong67a3f792016-11-01 23:16:53 -04004387
4388 seq_printf(s, "num_connectors: %d\n", cstate->num_connectors);
Dhaval Patel4d424602017-02-18 19:40:14 -08004389 seq_printf(s, "client type: %d\n", sde_crtc_get_client_type(crtc));
Alan Kwong3e985f02017-02-12 15:08:44 -08004390 seq_printf(s, "intf_mode: %d\n", sde_crtc_get_intf_mode(crtc));
Alan Kwong751cf462017-06-08 10:26:46 -04004391 seq_printf(s, "core_clk_rate: %llu\n",
4392 sde_crtc->cur_perf.core_clk_rate);
Alan Kwong0230a102017-05-16 11:36:44 -07004393 for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
4394 i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
4395 seq_printf(s, "bw_ctl[%s]: %llu\n",
4396 sde_power_handle_get_dbus_name(i),
4397 sde_crtc->cur_perf.bw_ctl[i]);
4398 seq_printf(s, "max_per_pipe_ib[%s]: %llu\n",
4399 sde_power_handle_get_dbus_name(i),
4400 sde_crtc->cur_perf.max_per_pipe_ib[i]);
4401 }
Alan Kwong67a3f792016-11-01 23:16:53 -04004402
Alan Kwong310e9b02017-08-03 02:04:07 -04004403 mutex_lock(&sde_crtc->rp_lock);
4404 list_for_each_entry(rp, &sde_crtc->rp_head, rp_list) {
4405 seq_printf(s, "rp.%d: ", rp->sequence_id);
4406 list_for_each_entry(res, &rp->res_list, list)
4407 seq_printf(s, "0x%x/0x%llx/%pK/%d ",
4408 res->type, res->tag, res->val,
4409 atomic_read(&res->refcount));
4410 seq_puts(s, "\n");
4411 }
4412 mutex_unlock(&sde_crtc->rp_lock);
Alan Kwongcdb2f282017-03-18 13:42:06 -07004413
Alan Kwong67a3f792016-11-01 23:16:53 -04004414 return 0;
4415}
4416DEFINE_SDE_DEBUGFS_SEQ_FOPS(sde_crtc_debugfs_state);
4417
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004418static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Clarence Ip8f7366c2016-07-05 12:15:26 -04004419{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004420 struct sde_crtc *sde_crtc;
4421 struct sde_kms *sde_kms;
4422
Dhaval Patel3fbe6bf2016-10-20 20:00:41 -07004423 static const struct file_operations debugfs_status_fops = {
4424 .open = _sde_debugfs_status_open,
Clarence Ip8f7366c2016-07-05 12:15:26 -04004425 .read = seq_read,
4426 .llseek = seq_lseek,
4427 .release = single_release,
4428 };
Dhaval Patelf9245d62017-03-28 16:24:00 -07004429 static const struct file_operations debugfs_misr_fops = {
4430 .open = simple_open,
4431 .read = _sde_crtc_misr_read,
4432 .write = _sde_crtc_misr_setup,
4433 };
Alan Kwong67a3f792016-11-01 23:16:53 -04004434
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004435 if (!crtc)
4436 return -EINVAL;
4437 sde_crtc = to_sde_crtc(crtc);
4438
4439 sde_kms = _sde_crtc_get_kms(crtc);
4440 if (!sde_kms)
4441 return -EINVAL;
4442
4443 sde_crtc->debugfs_root = debugfs_create_dir(sde_crtc->name,
Lloyd Atkinson09e64bf2017-04-13 14:09:59 -07004444 crtc->dev->primary->debugfs_root);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004445 if (!sde_crtc->debugfs_root)
4446 return -ENOMEM;
4447
4448 /* don't error check these */
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004449 debugfs_create_file("status", 0400,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004450 sde_crtc->debugfs_root,
4451 sde_crtc, &debugfs_status_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004452 debugfs_create_file("state", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004453 sde_crtc->debugfs_root,
4454 &sde_crtc->base,
4455 &sde_crtc_debugfs_state_fops);
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04004456 debugfs_create_file("misr_data", 0600, sde_crtc->debugfs_root,
Dhaval Patelf9245d62017-03-28 16:24:00 -07004457 sde_crtc, &debugfs_misr_fops);
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004458
4459 return 0;
4460}
4461
4462static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4463{
4464 struct sde_crtc *sde_crtc;
4465
4466 if (!crtc)
4467 return;
4468 sde_crtc = to_sde_crtc(crtc);
4469 debugfs_remove_recursive(sde_crtc->debugfs_root);
Clarence Ip8f7366c2016-07-05 12:15:26 -04004470}
Alan Kwong67a3f792016-11-01 23:16:53 -04004471#else
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004472static int _sde_crtc_init_debugfs(struct drm_crtc *crtc)
Alan Kwong67a3f792016-11-01 23:16:53 -04004473{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004474 return 0;
Alan Kwong67a3f792016-11-01 23:16:53 -04004475}
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004476
4477static void _sde_crtc_destroy_debugfs(struct drm_crtc *crtc)
4478{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07004479}
4480#endif /* CONFIG_DEBUG_FS */
4481
4482static int sde_crtc_late_register(struct drm_crtc *crtc)
4483{
4484 return _sde_crtc_init_debugfs(crtc);
4485}
4486
4487static void sde_crtc_early_unregister(struct drm_crtc *crtc)
4488{
4489 _sde_crtc_destroy_debugfs(crtc);
4490}
4491
4492static const struct drm_crtc_funcs sde_crtc_funcs = {
4493 .set_config = drm_atomic_helper_set_config,
4494 .destroy = sde_crtc_destroy,
4495 .page_flip = drm_atomic_helper_page_flip,
4496 .set_property = sde_crtc_set_property,
4497 .atomic_set_property = sde_crtc_atomic_set_property,
4498 .atomic_get_property = sde_crtc_atomic_get_property,
4499 .reset = sde_crtc_reset,
4500 .atomic_duplicate_state = sde_crtc_duplicate_state,
4501 .atomic_destroy_state = sde_crtc_destroy_state,
4502 .late_register = sde_crtc_late_register,
4503 .early_unregister = sde_crtc_early_unregister,
4504};
4505
4506static const struct drm_crtc_helper_funcs sde_crtc_helper_funcs = {
4507 .mode_fixup = sde_crtc_mode_fixup,
4508 .disable = sde_crtc_disable,
4509 .enable = sde_crtc_enable,
4510 .atomic_check = sde_crtc_atomic_check,
4511 .atomic_begin = sde_crtc_atomic_begin,
4512 .atomic_flush = sde_crtc_atomic_flush,
4513};
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004514
Clarence Ipa18d4832017-03-13 12:35:44 -07004515static void _sde_crtc_event_cb(struct kthread_work *work)
4516{
4517 struct sde_crtc_event *event;
4518 struct sde_crtc *sde_crtc;
4519 unsigned long irq_flags;
4520
4521 if (!work) {
4522 SDE_ERROR("invalid work item\n");
4523 return;
4524 }
4525
4526 event = container_of(work, struct sde_crtc_event, kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004527
4528 /* set sde_crtc to NULL for static work structures */
4529 sde_crtc = event->sde_crtc;
4530 if (!sde_crtc)
4531 return;
4532
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004533 if (event->cb_func)
4534 event->cb_func(&sde_crtc->base, event->usr);
4535
Clarence Ipa18d4832017-03-13 12:35:44 -07004536 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4537 list_add_tail(&event->list, &sde_crtc->event_free_list);
4538 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4539}
4540
4541int sde_crtc_event_queue(struct drm_crtc *crtc,
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004542 void (*func)(struct drm_crtc *crtc, void *usr), void *usr)
Clarence Ipa18d4832017-03-13 12:35:44 -07004543{
4544 unsigned long irq_flags;
4545 struct sde_crtc *sde_crtc;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004546 struct msm_drm_private *priv;
Clarence Ipa18d4832017-03-13 12:35:44 -07004547 struct sde_crtc_event *event = NULL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004548 u32 crtc_id;
Clarence Ipa18d4832017-03-13 12:35:44 -07004549
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004550 if (!crtc || !crtc->dev || !crtc->dev->dev_private || !func) {
4551 SDE_ERROR("invalid parameters\n");
Clarence Ipa18d4832017-03-13 12:35:44 -07004552 return -EINVAL;
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004553 }
Clarence Ipa18d4832017-03-13 12:35:44 -07004554 sde_crtc = to_sde_crtc(crtc);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004555 priv = crtc->dev->dev_private;
4556 crtc_id = drm_crtc_index(crtc);
Clarence Ipa18d4832017-03-13 12:35:44 -07004557
4558 /*
4559 * Obtain an event struct from the private cache. This event
4560 * queue may be called from ISR contexts, so use a private
4561 * cache to avoid calling any memory allocation functions.
4562 */
4563 spin_lock_irqsave(&sde_crtc->event_lock, irq_flags);
4564 if (!list_empty(&sde_crtc->event_free_list)) {
4565 event = list_first_entry(&sde_crtc->event_free_list,
4566 struct sde_crtc_event, list);
4567 list_del_init(&event->list);
4568 }
4569 spin_unlock_irqrestore(&sde_crtc->event_lock, irq_flags);
4570
4571 if (!event)
4572 return -ENOMEM;
4573
4574 /* populate event node */
4575 event->sde_crtc = sde_crtc;
4576 event->cb_func = func;
4577 event->usr = usr;
4578
4579 /* queue new event request */
4580 kthread_init_work(&event->kt_work, _sde_crtc_event_cb);
Veera Sundaram Sankaran10ea2bd2017-06-14 14:10:57 -07004581 kthread_queue_work(&priv->event_thread[crtc_id].worker,
4582 &event->kt_work);
Clarence Ipa18d4832017-03-13 12:35:44 -07004583
4584 return 0;
4585}
4586
4587static int _sde_crtc_init_events(struct sde_crtc *sde_crtc)
4588{
4589 int i, rc = 0;
4590
4591 if (!sde_crtc) {
4592 SDE_ERROR("invalid crtc\n");
4593 return -EINVAL;
4594 }
4595
4596 spin_lock_init(&sde_crtc->event_lock);
4597
4598 INIT_LIST_HEAD(&sde_crtc->event_free_list);
4599 for (i = 0; i < SDE_CRTC_MAX_EVENT_COUNT; ++i)
4600 list_add_tail(&sde_crtc->event_cache[i].list,
4601 &sde_crtc->event_free_list);
4602
Dhaval Patel5023c3c2017-08-22 12:40:11 -07004603 INIT_LIST_HEAD(&sde_crtc->retire_event_list);
4604 for (i = 0; i < ARRAY_SIZE(sde_crtc->retire_events); i++)
4605 INIT_LIST_HEAD(&sde_crtc->retire_events[i].list);
4606
Clarence Ipa18d4832017-03-13 12:35:44 -07004607 return rc;
4608}
4609
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004610/* initialize crtc */
Lloyd Atkinsonac933642016-09-14 11:52:00 -04004611struct drm_crtc *sde_crtc_init(struct drm_device *dev, struct drm_plane *plane)
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004612{
4613 struct drm_crtc *crtc = NULL;
Clarence Ip8f7366c2016-07-05 12:15:26 -04004614 struct sde_crtc *sde_crtc = NULL;
4615 struct msm_drm_private *priv = NULL;
4616 struct sde_kms *kms = NULL;
Clarence Ipa18d4832017-03-13 12:35:44 -07004617 int i, rc;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004618
Clarence Ip8f7366c2016-07-05 12:15:26 -04004619 priv = dev->dev_private;
4620 kms = to_sde_kms(priv->kms);
4621
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004622 sde_crtc = kzalloc(sizeof(*sde_crtc), GFP_KERNEL);
4623 if (!sde_crtc)
4624 return ERR_PTR(-ENOMEM);
4625
4626 crtc = &sde_crtc->base;
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004627 crtc->dev = dev;
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004628
Clarence Ip7f70ce42017-03-20 06:53:46 -07004629 mutex_init(&sde_crtc->crtc_lock);
Alan Kwong628d19e2016-10-31 13:50:13 -04004630 spin_lock_init(&sde_crtc->spin_lock);
4631 atomic_set(&sde_crtc->frame_pending, 0);
4632
Alan Kwong310e9b02017-08-03 02:04:07 -04004633 mutex_init(&sde_crtc->rp_lock);
4634 INIT_LIST_HEAD(&sde_crtc->rp_head);
4635
Veera Sundaram Sankaran7ee99092017-06-13 11:19:36 -07004636 init_completion(&sde_crtc->frame_done_comp);
4637
Alan Kwong628d19e2016-10-31 13:50:13 -04004638 INIT_LIST_HEAD(&sde_crtc->frame_event_list);
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004639 INIT_LIST_HEAD(&sde_crtc->user_event_list);
Alan Kwong628d19e2016-10-31 13:50:13 -04004640 for (i = 0; i < ARRAY_SIZE(sde_crtc->frame_events); i++) {
4641 INIT_LIST_HEAD(&sde_crtc->frame_events[i].list);
4642 list_add(&sde_crtc->frame_events[i].list,
4643 &sde_crtc->frame_event_list);
4644 kthread_init_work(&sde_crtc->frame_events[i].work,
4645 sde_crtc_frame_event_work);
4646 }
4647
Dhaval Patel04c7e8e2016-09-26 20:14:31 -07004648 drm_crtc_init_with_planes(dev, crtc, plane, NULL, &sde_crtc_funcs,
4649 NULL);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004650
4651 drm_crtc_helper_add(crtc, &sde_crtc_helper_funcs);
Abhijit Kulkarni3e3e0d22016-06-24 17:56:13 -04004652 plane->crtc = crtc;
4653
Clarence Ip8f7366c2016-07-05 12:15:26 -04004654 /* save user friendly CRTC name for later */
4655 snprintf(sde_crtc->name, SDE_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
4656
Clarence Ipa18d4832017-03-13 12:35:44 -07004657 /* initialize event handling */
4658 rc = _sde_crtc_init_events(sde_crtc);
4659 if (rc) {
4660 drm_crtc_cleanup(crtc);
4661 kfree(sde_crtc);
4662 return ERR_PTR(rc);
4663 }
4664
Clarence Ip9a74a442016-08-25 18:29:03 -04004665 /* initialize output fence support */
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04004666 sde_fence_init(&sde_crtc->output_fence, sde_crtc->name, crtc->base.id);
Clarence Ip24f80662016-06-13 19:05:32 -04004667
Clarence Ip7a753bb2016-07-07 11:47:44 -04004668 /* create CRTC properties */
4669 msm_property_init(&sde_crtc->property_info, &crtc->base, dev,
4670 priv->crtc_property, sde_crtc->property_data,
4671 CRTC_PROP_COUNT, CRTC_PROP_BLOBCOUNT,
4672 sizeof(struct sde_crtc_state));
4673
Dhaval Patele4a5dda2016-10-13 19:29:30 -07004674 sde_crtc_install_properties(crtc, kms->catalog);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004675
4676 /* Install color processing properties */
Gopikrishnaiah Anandane0e5e0c2016-05-25 11:05:33 -07004677 sde_cp_crtc_init(crtc);
Gopikrishnaiah Anandan703eb902016-10-06 18:43:57 -07004678 sde_cp_crtc_install_properties(crtc);
Clarence Ip7a753bb2016-07-07 11:47:44 -04004679
Dhaval Patelec10fad2016-08-22 14:40:48 -07004680 SDE_DEBUG("%s: successfully initialized crtc\n", sde_crtc->name);
Narendra Muppalla1b0b3352015-09-29 10:16:51 -07004681 return crtc;
4682}
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004683
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004684static int _sde_crtc_event_enable(struct sde_kms *kms,
4685 struct drm_crtc *crtc_drm, u32 event)
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004686{
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004687 struct sde_crtc *crtc = NULL;
4688 struct sde_crtc_irq_info *node;
4689 struct msm_drm_private *priv;
4690 unsigned long flags;
4691 bool found = false;
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004692 int ret, i = 0;
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004693
4694 crtc = to_sde_crtc(crtc_drm);
4695 spin_lock_irqsave(&crtc->spin_lock, flags);
4696 list_for_each_entry(node, &crtc->user_event_list, list) {
4697 if (node->event == event) {
4698 found = true;
4699 break;
4700 }
4701 }
4702 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4703
4704 /* event already enabled */
4705 if (found)
4706 return 0;
4707
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004708 node = NULL;
4709 for (i = 0; i < ARRAY_SIZE(custom_events); i++) {
4710 if (custom_events[i].event == event &&
4711 custom_events[i].func) {
4712 node = kzalloc(sizeof(*node), GFP_KERNEL);
4713 if (!node)
4714 return -ENOMEM;
4715 node->event = event;
4716 INIT_LIST_HEAD(&node->list);
4717 node->func = custom_events[i].func;
4718 node->event = event;
4719 break;
4720 }
4721 }
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004722
Gopikrishnaiah Anandanb6b401f2017-03-14 16:39:49 -07004723 if (!node) {
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004724 SDE_ERROR("unsupported event %x\n", event);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004725 return -EINVAL;
4726 }
4727
4728 priv = kms->dev->dev_private;
4729 ret = 0;
4730 if (crtc_drm->enabled) {
4731 sde_power_resource_enable(&priv->phandle, kms->core_client,
4732 true);
Xu Yang37752282017-08-21 13:50:23 +08004733 INIT_LIST_HEAD(&node->irq.list);
Gopikrishnaiah Anandan5154c712017-02-27 17:48:24 -08004734 ret = node->func(crtc_drm, true, &node->irq);
4735 sde_power_resource_enable(&priv->phandle, kms->core_client,
4736 false);
4737 }
4738
4739 if (!ret) {
4740 spin_lock_irqsave(&crtc->spin_lock, flags);
4741 list_add_tail(&node->list, &crtc->user_event_list);
4742 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4743 } else {
4744 kfree(node);
4745 }
4746
4747 return ret;
4748}
4749
4750static int _sde_crtc_event_disable(struct sde_kms *kms,
4751 struct drm_crtc *crtc_drm, u32 event)
4752{
4753 struct sde_crtc *crtc = NULL;
4754 struct sde_crtc_irq_info *node = NULL;
4755 struct msm_drm_private *priv;
4756 unsigned long flags;
4757 bool found = false;
4758 int ret;
4759
4760 crtc = to_sde_crtc(crtc_drm);
4761 spin_lock_irqsave(&crtc->spin_lock, flags);
4762 list_for_each_entry(node, &crtc->user_event_list, list) {
4763 if (node->event == event) {
4764 list_del(&node->list);
4765 found = true;
4766 break;
4767 }
4768 }
4769 spin_unlock_irqrestore(&crtc->spin_lock, flags);
4770
4771 /* event already disabled */
4772 if (!found)
4773 return 0;
4774
4775 /**
4776 * crtc is disabled interrupts are cleared remove from the list,
4777 * no need to disable/de-register.
4778 */
4779 if (!crtc_drm->enabled) {
4780 kfree(node);
4781 return 0;
4782 }
4783 priv = kms->dev->dev_private;
4784 sde_power_resource_enable(&priv->phandle, kms->core_client, true);
4785 ret = node->func(crtc_drm, false, &node->irq);
4786 sde_power_resource_enable(&priv->phandle, kms->core_client, false);
4787 return ret;
4788}
4789
4790int sde_crtc_register_custom_event(struct sde_kms *kms,
4791 struct drm_crtc *crtc_drm, u32 event, bool en)
4792{
4793 struct sde_crtc *crtc = NULL;
4794 int ret;
4795
4796 crtc = to_sde_crtc(crtc_drm);
4797 if (!crtc || !kms || !kms->dev) {
4798 DRM_ERROR("invalid sde_crtc %pK kms %pK dev %pK\n", crtc,
4799 kms, ((kms) ? (kms->dev) : NULL));
4800 return -EINVAL;
4801 }
4802
4803 if (en)
4804 ret = _sde_crtc_event_enable(kms, crtc_drm, event);
4805 else
4806 ret = _sde_crtc_event_disable(kms, crtc_drm, event);
4807
4808 return ret;
Gopikrishnaiah Anandande2c81b2017-03-15 12:41:29 -07004809}
Gopikrishnaiah Anandan84b4f672017-04-26 10:28:51 -07004810
4811static int sde_crtc_power_interrupt_handler(struct drm_crtc *crtc_drm,
4812 bool en, struct sde_irq_callback *irq)
4813{
4814 return 0;
4815}
Benjamin Chan90139102017-06-21 16:00:39 -04004816
Sravanthi Kollukuduru59d431a2017-07-05 00:10:41 +05304817static int sde_crtc_idle_interrupt_handler(struct drm_crtc *crtc_drm,
4818 bool en, struct sde_irq_callback *irq)
4819{
4820 return 0;
4821}