blob: 169a8920a03922eed0eb618a49f2f6d1c2a44c17 [file] [log] [blame]
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -07001/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#ifndef SDE_ROTATOR_CORE_H
15#define SDE_ROTATOR_CORE_H
16
17#include <linux/list.h>
18#include <linux/file.h>
19#include <linux/ktime.h>
20#include <linux/mutex.h>
21#include <linux/types.h>
22#include <linux/cdev.h>
23#include <linux/pm_runtime.h>
24
25#include "sde_rotator_base.h"
26#include "sde_rotator_util.h"
27#include "sde_rotator_sync.h"
28
29/**********************************************************************
30 * Rotation request flag
31 **********************************************************************/
32/* no rotation flag */
33#define SDE_ROTATION_NOP 0x01
34
35/* left/right flip */
36#define SDE_ROTATION_FLIP_LR 0x02
37
38/* up/down flip */
39#define SDE_ROTATION_FLIP_UD 0x04
40
41/* rotate 90 degree */
42#define SDE_ROTATION_90 0x08
43
44/* rotate 180 degre */
45#define SDE_ROTATION_180 (SDE_ROTATION_FLIP_LR | SDE_ROTATION_FLIP_UD)
46
47/* rotate 270 degree */
48#define SDE_ROTATION_270 (SDE_ROTATION_90 | SDE_ROTATION_180)
49
50/* format is interlaced */
51#define SDE_ROTATION_DEINTERLACE 0x10
52
53/* secure data */
54#define SDE_ROTATION_SECURE 0x80
55
56/* verify input configuration only */
57#define SDE_ROTATION_VERIFY_INPUT_ONLY 0x10000
58
59/* use client provided dma buf instead of ion fd */
60#define SDE_ROTATION_EXT_DMA_BUF 0x20000
61
Alan Kwonga94684f2016-01-16 22:06:36 -050062/**********************************************************************
63 * configuration structures
64 **********************************************************************/
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -070065
66struct sde_rotation_buf_info {
67 uint32_t width;
68 uint32_t height;
69 uint32_t format;
70 struct sde_mult_factor comp_ratio;
71};
72
73struct sde_rotation_config {
74 uint32_t session_id;
75 struct sde_rotation_buf_info input;
76 struct sde_rotation_buf_info output;
77 uint32_t frame_rate;
78 uint32_t flags;
79};
80
81enum sde_rotator_ts {
82 SDE_ROTATOR_TS_SRCQB, /* enqueue source buffer */
83 SDE_ROTATOR_TS_DSTQB, /* enqueue destination buffer */
84 SDE_ROTATOR_TS_FENCE, /* wait for source buffer fence */
85 SDE_ROTATOR_TS_QUEUE, /* wait for h/w resource */
86 SDE_ROTATOR_TS_COMMIT, /* prepare h/w command */
87 SDE_ROTATOR_TS_FLUSH, /* initiate h/w processing */
88 SDE_ROTATOR_TS_DONE, /* receive h/w completion */
89 SDE_ROTATOR_TS_RETIRE, /* signal destination buffer fence */
90 SDE_ROTATOR_TS_SRCDQB, /* dequeue source buffer */
91 SDE_ROTATOR_TS_DSTDQB, /* dequeue destination buffer */
92 SDE_ROTATOR_TS_MAX
93};
94
95struct sde_rotation_item {
96 /* rotation request flag */
97 uint32_t flags;
98
99 /* Source crop rectangle */
100 struct sde_rect src_rect;
101
102 /* Destination rectangle */
103 struct sde_rect dst_rect;
104
105 /* Input buffer for the request */
106 struct sde_layer_buffer input;
107
108 /* The output buffer for the request */
109 struct sde_layer_buffer output;
110
Alan Kwonga94684f2016-01-16 22:06:36 -0500111 /*
112 * DMA pipe selection for this request by client:
113 * 0: DMA pipe 0
114 * 1: DMA pipe 1
115 * or SDE_ROTATION_HW_ANY if client wants
116 * driver to allocate any that is available
117 *
118 * OR
119 *
120 * Reserved
121 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700122 uint32_t pipe_idx;
123
Alan Kwonga94684f2016-01-16 22:06:36 -0500124 /*
125 * Write-back block selection for this request by client:
126 * 0: Write-back block 0
127 * 1: Write-back block 1
128 * or SDE_ROTATION_HW_ANY if client wants
129 * driver to allocate any that is available
130 *
131 * OR
132 *
133 * Priority selection for this request by client:
134 * 0: Highest
135 * 1..n: Limited by the lowest available priority
136 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700137 uint32_t wb_idx;
138
Alan Kwonga94684f2016-01-16 22:06:36 -0500139 /*
140 * Sequence ID of this request within the session
141 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700142 uint32_t sequence_id;
143
144 /* Which session ID is this request scheduled on */
145 uint32_t session_id;
146
147 /* Time stamp for profiling purposes */
148 ktime_t *ts;
149};
150
151/*
152 * Defining characteristics about rotation work, that has corresponding
153 * fmt and roi checks in open session
154 */
155#define SDE_ROT_DEFINING_FLAG_BITS SDE_ROTATION_90
156
157struct sde_rot_entry;
158struct sde_rot_perf;
159
160struct sde_rot_clk {
161 struct clk *clk;
162 char clk_name[32];
163 unsigned long rate;
164};
165
166struct sde_rot_hw_resource {
167 u32 wb_id;
168 u32 pending_count;
169 atomic_t num_active;
170 int max_active;
171 wait_queue_head_t wait_queue;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700172};
173
174struct sde_rot_queue {
175 struct workqueue_struct *rot_work_queue;
176 struct sde_rot_timeline *timeline;
177 struct sde_rot_hw_resource *hw;
178};
179
180struct sde_rot_entry_container {
181 struct list_head list;
182 u32 flags;
183 u32 count;
184 atomic_t pending_count;
185 atomic_t failed_count;
186 struct workqueue_struct *retireq;
187 struct work_struct *retire_work;
188 struct sde_rot_entry *entries;
189};
190
191struct sde_rot_mgr;
192struct sde_rot_file_private;
193
194struct sde_rot_entry {
195 struct sde_rotation_item item;
196 struct work_struct commit_work;
197 struct work_struct done_work;
198 struct sde_rot_queue *commitq;
199 struct sde_rot_queue *fenceq;
200 struct sde_rot_queue *doneq;
201 struct sde_rot_entry_container *request;
202
203 struct sde_mdp_data src_buf;
204 struct sde_mdp_data dst_buf;
205
206 struct sde_rot_sync_fence *input_fence;
207
208 struct sde_rot_sync_fence *output_fence;
209 bool output_signaled;
210
211 u32 dnsc_factor_w;
212 u32 dnsc_factor_h;
213
214 struct sde_rot_perf *perf;
215 bool work_assigned; /* Used when cleaning up work_distribution */
216 struct sde_rot_file_private *private;
217};
218
219struct sde_rot_perf {
220 struct list_head list;
221 struct sde_rotation_config config;
222 unsigned long clk_rate;
223 u64 bw;
224 struct mutex work_dis_lock;
225 u32 *work_distribution;
226 int last_wb_idx; /* last known wb index, used when above count is 0 */
Alan Kwongeffb5ee2016-03-12 19:47:45 -0500227 u32 rdot_limit;
228 u32 wrot_limit;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700229};
230
231struct sde_rot_file_private {
232 struct list_head list;
233 struct list_head req_list;
234 struct list_head perf_list;
235 struct sde_rot_mgr *mgr;
236 struct sde_rot_queue *fenceq;
237};
238
239struct sde_rot_bus_data_type {
240 struct msm_bus_scale_pdata *bus_scale_pdata;
241 u32 bus_hdl;
242 u32 curr_bw_uc_idx;
243 u64 curr_quota_val;
244};
245
246struct sde_rot_mgr {
247 struct mutex lock;
248 atomic_t device_suspended;
249 struct platform_device *pdev;
250 struct device *device;
251
252 /*
253 * Managing rotation queues, depends on
254 * how many hw pipes available on the system
255 */
256 int queue_count;
257 struct sde_rot_queue *commitq;
258 struct sde_rot_queue *doneq;
259
260 /*
261 * managing all the open file sessions to bw calculations,
262 * and resource clean up during suspend
263 */
264 struct list_head file_list;
265
266 u64 pending_close_bw_vote;
267 struct sde_rot_bus_data_type data_bus;
268 struct sde_rot_bus_data_type reg_bus;
269
270 /* Module power is only used for regulator management */
271 struct sde_module_power module_power;
272 bool regulator_enable;
273
274 int res_ref_cnt;
275 int rot_enable_clk_cnt;
276 struct sde_rot_clk *rot_clk;
277 int num_rot_clk;
278 int core_clk_idx;
Alan Kwongeffb5ee2016-03-12 19:47:45 -0500279 u32 rdot_limit;
280 u32 wrot_limit;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700281
282 u32 hwacquire_timeout;
283 struct sde_mult_factor pixel_per_clk;
284
285 int (*ops_config_hw)(struct sde_rot_hw_resource *hw,
286 struct sde_rot_entry *entry);
287 int (*ops_kickoff_entry)(struct sde_rot_hw_resource *hw,
288 struct sde_rot_entry *entry);
289 int (*ops_wait_for_entry)(struct sde_rot_hw_resource *hw,
290 struct sde_rot_entry *entry);
291 struct sde_rot_hw_resource *(*ops_hw_alloc)(struct sde_rot_mgr *mgr,
292 u32 pipe_id, u32 wb_id);
293 void (*ops_hw_free)(struct sde_rot_mgr *mgr,
294 struct sde_rot_hw_resource *hw);
295 int (*ops_hw_init)(struct sde_rot_mgr *mgr);
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400296 void (*ops_hw_pre_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
297 void (*ops_hw_post_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700298 void (*ops_hw_destroy)(struct sde_rot_mgr *mgr);
299 ssize_t (*ops_hw_show_caps)(struct sde_rot_mgr *mgr,
300 struct device_attribute *attr, char *buf, ssize_t len);
301 ssize_t (*ops_hw_show_state)(struct sde_rot_mgr *mgr,
302 struct device_attribute *attr, char *buf, ssize_t len);
303 int (*ops_hw_create_debugfs)(struct sde_rot_mgr *mgr,
304 struct dentry *debugfs_root);
305 int (*ops_hw_validate_entry)(struct sde_rot_mgr *mgr,
306 struct sde_rot_entry *entry);
Alan Kwongda16e442016-08-14 20:47:18 -0400307 u32 (*ops_hw_get_pixfmt)(struct sde_rot_mgr *mgr, int index,
308 bool input);
309 int (*ops_hw_is_valid_pixfmt)(struct sde_rot_mgr *mgr, u32 pixfmt,
310 bool input);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700311
312 void *hw_data;
313};
314
Alan Kwongda16e442016-08-14 20:47:18 -0400315static inline int sde_rotator_is_valid_pixfmt(struct sde_rot_mgr *mgr,
316 u32 pixfmt, bool input)
317{
318 if (mgr && mgr->ops_hw_is_valid_pixfmt)
319 return mgr->ops_hw_is_valid_pixfmt(mgr, pixfmt, input);
320
321 return false;
322}
323
324static inline u32 sde_rotator_get_pixfmt(struct sde_rot_mgr *mgr,
325 int index, bool input)
326{
327 if (mgr && mgr->ops_hw_get_pixfmt)
328 return mgr->ops_hw_get_pixfmt(mgr, index, input);
329
330 return 0;
331}
332
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700333static inline int __compare_session_item_rect(
334 struct sde_rotation_buf_info *s_rect,
335 struct sde_rect *i_rect, uint32_t i_fmt, bool src)
336{
337 if ((s_rect->width != i_rect->w) || (s_rect->height != i_rect->h) ||
338 (s_rect->format != i_fmt)) {
339 SDEROT_DBG(
340 "%s: session{%u,%u}f:%u mismatch from item{%u,%u}f:%u\n",
341 (src ? "src":"dst"), s_rect->width, s_rect->height,
342 s_rect->format, i_rect->w, i_rect->h, i_fmt);
343 return -EINVAL;
344 }
345 return 0;
346}
347
348/*
349 * Compare all important flag bits associated with rotation between session
350 * config and item request. Format and roi validation is done during open
351 * session and is based certain defining bits. If these defining bits are
352 * different in item request, there is a possibility that rotation item
353 * is not a valid configuration.
354 */
355static inline int __compare_session_rotations(uint32_t cfg_flag,
356 uint32_t item_flag)
357{
358 cfg_flag &= SDE_ROT_DEFINING_FLAG_BITS;
359 item_flag &= SDE_ROT_DEFINING_FLAG_BITS;
360 if (cfg_flag != item_flag) {
361 SDEROT_DBG(
362 "Rotation degree request different from open session\n");
363 return -EINVAL;
364 }
365 return 0;
366}
367
368int sde_rotator_core_init(struct sde_rot_mgr **pmgr,
369 struct platform_device *pdev);
370
371void sde_rotator_core_destroy(struct sde_rot_mgr *mgr);
372
373int sde_rotator_session_open(struct sde_rot_mgr *mgr,
374 struct sde_rot_file_private **pprivate, int session_id,
375 struct sde_rot_queue *queue);
376
377void sde_rotator_session_close(struct sde_rot_mgr *mgr,
378 struct sde_rot_file_private *private, int session_id);
379
380int sde_rotator_session_config(struct sde_rot_mgr *mgr,
381 struct sde_rot_file_private *private,
382 struct sde_rotation_config *config);
383
384struct sde_rot_entry_container *sde_rotator_req_init(
385 struct sde_rot_mgr *rot_dev,
386 struct sde_rot_file_private *private,
387 struct sde_rotation_item *items,
388 u32 count, u32 flags);
389
390int sde_rotator_handle_request_common(struct sde_rot_mgr *rot_dev,
391 struct sde_rot_file_private *ctx,
392 struct sde_rot_entry_container *req,
393 struct sde_rotation_item *items);
394
395void sde_rotator_queue_request(struct sde_rot_mgr *rot_dev,
396 struct sde_rot_file_private *ctx,
397 struct sde_rot_entry_container *req);
398
399void sde_rotator_remove_request(struct sde_rot_mgr *mgr,
400 struct sde_rot_file_private *private,
401 struct sde_rot_entry_container *req);
402
403int sde_rotator_verify_config(struct sde_rot_mgr *rot_dev,
404 struct sde_rotation_config *config);
405
406int sde_rotator_validate_request(struct sde_rot_mgr *rot_dev,
407 struct sde_rot_file_private *ctx,
408 struct sde_rot_entry_container *req);
409
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400410int sde_rotator_clk_ctrl(struct sde_rot_mgr *mgr, int enable);
411
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700412static inline void sde_rot_mgr_lock(struct sde_rot_mgr *mgr)
413{
414 mutex_lock(&mgr->lock);
415}
416
417static inline void sde_rot_mgr_unlock(struct sde_rot_mgr *mgr)
418{
419 mutex_unlock(&mgr->lock);
420}
421
Alan Kwong3428f672016-04-18 12:32:06 -0400422#if defined(CONFIG_PM)
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700423int sde_rotator_runtime_resume(struct device *dev);
424int sde_rotator_runtime_suspend(struct device *dev);
425int sde_rotator_runtime_idle(struct device *dev);
426#endif
427
428#if defined(CONFIG_PM_SLEEP)
429int sde_rotator_pm_suspend(struct device *dev);
430int sde_rotator_pm_resume(struct device *dev);
431#endif
432
433#if defined(CONFIG_PM) && !defined(CONFIG_PM_SLEEP)
434int sde_rotator_suspend(struct platform_device *dev, pm_message_t state);
435int sde_rotator_resume(struct platform_device *dev);
436#else
437#define sde_rotator_suspend NULL
438#define sde_rotator_resume NULL
439#endif
440#endif /* __SDE_ROTATOR_CORE_H__ */