blob: c443f401ee524f090693d3cde17bd4922ed0ec27 [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;
Benjamin Chandbe13112016-09-26 12:10:06 -0400284 struct sde_mult_factor fudge_factor;
285 struct sde_mult_factor overhead;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700286
287 int (*ops_config_hw)(struct sde_rot_hw_resource *hw,
288 struct sde_rot_entry *entry);
289 int (*ops_kickoff_entry)(struct sde_rot_hw_resource *hw,
290 struct sde_rot_entry *entry);
291 int (*ops_wait_for_entry)(struct sde_rot_hw_resource *hw,
292 struct sde_rot_entry *entry);
293 struct sde_rot_hw_resource *(*ops_hw_alloc)(struct sde_rot_mgr *mgr,
294 u32 pipe_id, u32 wb_id);
295 void (*ops_hw_free)(struct sde_rot_mgr *mgr,
296 struct sde_rot_hw_resource *hw);
297 int (*ops_hw_init)(struct sde_rot_mgr *mgr);
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400298 void (*ops_hw_pre_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
299 void (*ops_hw_post_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700300 void (*ops_hw_destroy)(struct sde_rot_mgr *mgr);
301 ssize_t (*ops_hw_show_caps)(struct sde_rot_mgr *mgr,
302 struct device_attribute *attr, char *buf, ssize_t len);
303 ssize_t (*ops_hw_show_state)(struct sde_rot_mgr *mgr,
304 struct device_attribute *attr, char *buf, ssize_t len);
305 int (*ops_hw_create_debugfs)(struct sde_rot_mgr *mgr,
306 struct dentry *debugfs_root);
307 int (*ops_hw_validate_entry)(struct sde_rot_mgr *mgr,
308 struct sde_rot_entry *entry);
Alan Kwongda16e442016-08-14 20:47:18 -0400309 u32 (*ops_hw_get_pixfmt)(struct sde_rot_mgr *mgr, int index,
310 bool input);
311 int (*ops_hw_is_valid_pixfmt)(struct sde_rot_mgr *mgr, u32 pixfmt,
312 bool input);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700313
314 void *hw_data;
315};
316
Alan Kwongda16e442016-08-14 20:47:18 -0400317static inline int sde_rotator_is_valid_pixfmt(struct sde_rot_mgr *mgr,
318 u32 pixfmt, bool input)
319{
320 if (mgr && mgr->ops_hw_is_valid_pixfmt)
321 return mgr->ops_hw_is_valid_pixfmt(mgr, pixfmt, input);
322
323 return false;
324}
325
326static inline u32 sde_rotator_get_pixfmt(struct sde_rot_mgr *mgr,
327 int index, bool input)
328{
329 if (mgr && mgr->ops_hw_get_pixfmt)
330 return mgr->ops_hw_get_pixfmt(mgr, index, input);
331
332 return 0;
333}
334
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700335static inline int __compare_session_item_rect(
336 struct sde_rotation_buf_info *s_rect,
337 struct sde_rect *i_rect, uint32_t i_fmt, bool src)
338{
339 if ((s_rect->width != i_rect->w) || (s_rect->height != i_rect->h) ||
340 (s_rect->format != i_fmt)) {
341 SDEROT_DBG(
342 "%s: session{%u,%u}f:%u mismatch from item{%u,%u}f:%u\n",
343 (src ? "src":"dst"), s_rect->width, s_rect->height,
344 s_rect->format, i_rect->w, i_rect->h, i_fmt);
345 return -EINVAL;
346 }
347 return 0;
348}
349
350/*
351 * Compare all important flag bits associated with rotation between session
352 * config and item request. Format and roi validation is done during open
353 * session and is based certain defining bits. If these defining bits are
354 * different in item request, there is a possibility that rotation item
355 * is not a valid configuration.
356 */
357static inline int __compare_session_rotations(uint32_t cfg_flag,
358 uint32_t item_flag)
359{
360 cfg_flag &= SDE_ROT_DEFINING_FLAG_BITS;
361 item_flag &= SDE_ROT_DEFINING_FLAG_BITS;
362 if (cfg_flag != item_flag) {
363 SDEROT_DBG(
364 "Rotation degree request different from open session\n");
365 return -EINVAL;
366 }
367 return 0;
368}
369
370int sde_rotator_core_init(struct sde_rot_mgr **pmgr,
371 struct platform_device *pdev);
372
373void sde_rotator_core_destroy(struct sde_rot_mgr *mgr);
374
375int sde_rotator_session_open(struct sde_rot_mgr *mgr,
376 struct sde_rot_file_private **pprivate, int session_id,
377 struct sde_rot_queue *queue);
378
379void sde_rotator_session_close(struct sde_rot_mgr *mgr,
380 struct sde_rot_file_private *private, int session_id);
381
382int sde_rotator_session_config(struct sde_rot_mgr *mgr,
383 struct sde_rot_file_private *private,
384 struct sde_rotation_config *config);
385
386struct sde_rot_entry_container *sde_rotator_req_init(
387 struct sde_rot_mgr *rot_dev,
388 struct sde_rot_file_private *private,
389 struct sde_rotation_item *items,
390 u32 count, u32 flags);
391
392int sde_rotator_handle_request_common(struct sde_rot_mgr *rot_dev,
393 struct sde_rot_file_private *ctx,
394 struct sde_rot_entry_container *req,
395 struct sde_rotation_item *items);
396
397void sde_rotator_queue_request(struct sde_rot_mgr *rot_dev,
398 struct sde_rot_file_private *ctx,
399 struct sde_rot_entry_container *req);
400
401void sde_rotator_remove_request(struct sde_rot_mgr *mgr,
402 struct sde_rot_file_private *private,
403 struct sde_rot_entry_container *req);
404
405int sde_rotator_verify_config(struct sde_rot_mgr *rot_dev,
406 struct sde_rotation_config *config);
407
408int sde_rotator_validate_request(struct sde_rot_mgr *rot_dev,
409 struct sde_rot_file_private *ctx,
410 struct sde_rot_entry_container *req);
411
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400412int sde_rotator_clk_ctrl(struct sde_rot_mgr *mgr, int enable);
413
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700414static inline void sde_rot_mgr_lock(struct sde_rot_mgr *mgr)
415{
416 mutex_lock(&mgr->lock);
417}
418
419static inline void sde_rot_mgr_unlock(struct sde_rot_mgr *mgr)
420{
421 mutex_unlock(&mgr->lock);
422}
423
Alan Kwong3428f672016-04-18 12:32:06 -0400424#if defined(CONFIG_PM)
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700425int sde_rotator_runtime_resume(struct device *dev);
426int sde_rotator_runtime_suspend(struct device *dev);
427int sde_rotator_runtime_idle(struct device *dev);
428#endif
429
430#if defined(CONFIG_PM_SLEEP)
431int sde_rotator_pm_suspend(struct device *dev);
432int sde_rotator_pm_resume(struct device *dev);
433#endif
434
435#if defined(CONFIG_PM) && !defined(CONFIG_PM_SLEEP)
436int sde_rotator_suspend(struct platform_device *dev, pm_message_t state);
437int sde_rotator_resume(struct platform_device *dev);
438#else
439#define sde_rotator_suspend NULL
440#define sde_rotator_resume NULL
441#endif
442#endif /* __SDE_ROTATOR_CORE_H__ */