blob: 2476309dd454a579c3fc50884875d930ead9e14d [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);
296 void (*ops_hw_destroy)(struct sde_rot_mgr *mgr);
297 ssize_t (*ops_hw_show_caps)(struct sde_rot_mgr *mgr,
298 struct device_attribute *attr, char *buf, ssize_t len);
299 ssize_t (*ops_hw_show_state)(struct sde_rot_mgr *mgr,
300 struct device_attribute *attr, char *buf, ssize_t len);
301 int (*ops_hw_create_debugfs)(struct sde_rot_mgr *mgr,
302 struct dentry *debugfs_root);
303 int (*ops_hw_validate_entry)(struct sde_rot_mgr *mgr,
304 struct sde_rot_entry *entry);
305
306 void *hw_data;
307};
308
309static inline int __compare_session_item_rect(
310 struct sde_rotation_buf_info *s_rect,
311 struct sde_rect *i_rect, uint32_t i_fmt, bool src)
312{
313 if ((s_rect->width != i_rect->w) || (s_rect->height != i_rect->h) ||
314 (s_rect->format != i_fmt)) {
315 SDEROT_DBG(
316 "%s: session{%u,%u}f:%u mismatch from item{%u,%u}f:%u\n",
317 (src ? "src":"dst"), s_rect->width, s_rect->height,
318 s_rect->format, i_rect->w, i_rect->h, i_fmt);
319 return -EINVAL;
320 }
321 return 0;
322}
323
324/*
325 * Compare all important flag bits associated with rotation between session
326 * config and item request. Format and roi validation is done during open
327 * session and is based certain defining bits. If these defining bits are
328 * different in item request, there is a possibility that rotation item
329 * is not a valid configuration.
330 */
331static inline int __compare_session_rotations(uint32_t cfg_flag,
332 uint32_t item_flag)
333{
334 cfg_flag &= SDE_ROT_DEFINING_FLAG_BITS;
335 item_flag &= SDE_ROT_DEFINING_FLAG_BITS;
336 if (cfg_flag != item_flag) {
337 SDEROT_DBG(
338 "Rotation degree request different from open session\n");
339 return -EINVAL;
340 }
341 return 0;
342}
343
344int sde_rotator_core_init(struct sde_rot_mgr **pmgr,
345 struct platform_device *pdev);
346
347void sde_rotator_core_destroy(struct sde_rot_mgr *mgr);
348
349int sde_rotator_session_open(struct sde_rot_mgr *mgr,
350 struct sde_rot_file_private **pprivate, int session_id,
351 struct sde_rot_queue *queue);
352
353void sde_rotator_session_close(struct sde_rot_mgr *mgr,
354 struct sde_rot_file_private *private, int session_id);
355
356int sde_rotator_session_config(struct sde_rot_mgr *mgr,
357 struct sde_rot_file_private *private,
358 struct sde_rotation_config *config);
359
360struct sde_rot_entry_container *sde_rotator_req_init(
361 struct sde_rot_mgr *rot_dev,
362 struct sde_rot_file_private *private,
363 struct sde_rotation_item *items,
364 u32 count, u32 flags);
365
366int sde_rotator_handle_request_common(struct sde_rot_mgr *rot_dev,
367 struct sde_rot_file_private *ctx,
368 struct sde_rot_entry_container *req,
369 struct sde_rotation_item *items);
370
371void sde_rotator_queue_request(struct sde_rot_mgr *rot_dev,
372 struct sde_rot_file_private *ctx,
373 struct sde_rot_entry_container *req);
374
375void sde_rotator_remove_request(struct sde_rot_mgr *mgr,
376 struct sde_rot_file_private *private,
377 struct sde_rot_entry_container *req);
378
379int sde_rotator_verify_config(struct sde_rot_mgr *rot_dev,
380 struct sde_rotation_config *config);
381
382int sde_rotator_validate_request(struct sde_rot_mgr *rot_dev,
383 struct sde_rot_file_private *ctx,
384 struct sde_rot_entry_container *req);
385
386static inline void sde_rot_mgr_lock(struct sde_rot_mgr *mgr)
387{
388 mutex_lock(&mgr->lock);
389}
390
391static inline void sde_rot_mgr_unlock(struct sde_rot_mgr *mgr)
392{
393 mutex_unlock(&mgr->lock);
394}
395
Alan Kwong3428f672016-04-18 12:32:06 -0400396#if defined(CONFIG_PM)
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700397int sde_rotator_runtime_resume(struct device *dev);
398int sde_rotator_runtime_suspend(struct device *dev);
399int sde_rotator_runtime_idle(struct device *dev);
400#endif
401
402#if defined(CONFIG_PM_SLEEP)
403int sde_rotator_pm_suspend(struct device *dev);
404int sde_rotator_pm_resume(struct device *dev);
405#endif
406
407#if defined(CONFIG_PM) && !defined(CONFIG_PM_SLEEP)
408int sde_rotator_suspend(struct platform_device *dev, pm_message_t state);
409int sde_rotator_resume(struct platform_device *dev);
410#else
411#define sde_rotator_suspend NULL
412#define sde_rotator_resume NULL
413#endif
414#endif /* __SDE_ROTATOR_CORE_H__ */