blob: f8555eca979114c985041b48628cf410c1ef61aa [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
Abhijit Kulkarni298c8232016-09-26 22:32:10 -070062/* secure camera operation*/
63#define SDE_ROTATION_SECURE_CAMERA 0x40000
64
Alan Kwonga94684f2016-01-16 22:06:36 -050065/**********************************************************************
66 * configuration structures
67 **********************************************************************/
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -070068
69struct sde_rotation_buf_info {
70 uint32_t width;
71 uint32_t height;
72 uint32_t format;
73 struct sde_mult_factor comp_ratio;
74};
75
76struct sde_rotation_config {
77 uint32_t session_id;
78 struct sde_rotation_buf_info input;
79 struct sde_rotation_buf_info output;
80 uint32_t frame_rate;
81 uint32_t flags;
82};
83
84enum sde_rotator_ts {
85 SDE_ROTATOR_TS_SRCQB, /* enqueue source buffer */
86 SDE_ROTATOR_TS_DSTQB, /* enqueue destination buffer */
87 SDE_ROTATOR_TS_FENCE, /* wait for source buffer fence */
88 SDE_ROTATOR_TS_QUEUE, /* wait for h/w resource */
89 SDE_ROTATOR_TS_COMMIT, /* prepare h/w command */
90 SDE_ROTATOR_TS_FLUSH, /* initiate h/w processing */
91 SDE_ROTATOR_TS_DONE, /* receive h/w completion */
92 SDE_ROTATOR_TS_RETIRE, /* signal destination buffer fence */
93 SDE_ROTATOR_TS_SRCDQB, /* dequeue source buffer */
94 SDE_ROTATOR_TS_DSTDQB, /* dequeue destination buffer */
95 SDE_ROTATOR_TS_MAX
96};
97
Benjamin Chan77aed192016-10-17 17:49:41 -040098enum sde_rotator_clk_type {
99 SDE_ROTATOR_CLK_MDSS_AHB,
100 SDE_ROTATOR_CLK_MDSS_AXI,
101 SDE_ROTATOR_CLK_ROT_CORE,
102 SDE_ROTATOR_CLK_MDSS_ROT,
103 SDE_ROTATOR_CLK_MNOC_AHB,
104 SDE_ROTATOR_CLK_MAX
105};
106
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700107struct sde_rotation_item {
108 /* rotation request flag */
109 uint32_t flags;
110
111 /* Source crop rectangle */
112 struct sde_rect src_rect;
113
114 /* Destination rectangle */
115 struct sde_rect dst_rect;
116
117 /* Input buffer for the request */
118 struct sde_layer_buffer input;
119
120 /* The output buffer for the request */
121 struct sde_layer_buffer output;
122
Alan Kwonga94684f2016-01-16 22:06:36 -0500123 /*
124 * DMA pipe selection for this request by client:
125 * 0: DMA pipe 0
126 * 1: DMA pipe 1
127 * or SDE_ROTATION_HW_ANY if client wants
128 * driver to allocate any that is available
129 *
130 * OR
131 *
132 * Reserved
133 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700134 uint32_t pipe_idx;
135
Alan Kwonga94684f2016-01-16 22:06:36 -0500136 /*
137 * Write-back block selection for this request by client:
138 * 0: Write-back block 0
139 * 1: Write-back block 1
140 * or SDE_ROTATION_HW_ANY if client wants
141 * driver to allocate any that is available
142 *
143 * OR
144 *
145 * Priority selection for this request by client:
146 * 0: Highest
147 * 1..n: Limited by the lowest available priority
148 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700149 uint32_t wb_idx;
150
Alan Kwonga94684f2016-01-16 22:06:36 -0500151 /*
152 * Sequence ID of this request within the session
153 */
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700154 uint32_t sequence_id;
155
156 /* Which session ID is this request scheduled on */
157 uint32_t session_id;
158
159 /* Time stamp for profiling purposes */
160 ktime_t *ts;
161};
162
163/*
164 * Defining characteristics about rotation work, that has corresponding
165 * fmt and roi checks in open session
166 */
167#define SDE_ROT_DEFINING_FLAG_BITS SDE_ROTATION_90
168
169struct sde_rot_entry;
170struct sde_rot_perf;
171
172struct sde_rot_clk {
173 struct clk *clk;
174 char clk_name[32];
175 unsigned long rate;
176};
177
178struct sde_rot_hw_resource {
179 u32 wb_id;
180 u32 pending_count;
181 atomic_t num_active;
182 int max_active;
183 wait_queue_head_t wait_queue;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700184};
185
186struct sde_rot_queue {
187 struct workqueue_struct *rot_work_queue;
188 struct sde_rot_timeline *timeline;
189 struct sde_rot_hw_resource *hw;
190};
191
192struct sde_rot_entry_container {
193 struct list_head list;
194 u32 flags;
195 u32 count;
196 atomic_t pending_count;
197 atomic_t failed_count;
198 struct workqueue_struct *retireq;
199 struct work_struct *retire_work;
200 struct sde_rot_entry *entries;
201};
202
203struct sde_rot_mgr;
204struct sde_rot_file_private;
205
206struct sde_rot_entry {
207 struct sde_rotation_item item;
208 struct work_struct commit_work;
209 struct work_struct done_work;
210 struct sde_rot_queue *commitq;
211 struct sde_rot_queue *fenceq;
212 struct sde_rot_queue *doneq;
213 struct sde_rot_entry_container *request;
214
215 struct sde_mdp_data src_buf;
216 struct sde_mdp_data dst_buf;
217
218 struct sde_rot_sync_fence *input_fence;
219
220 struct sde_rot_sync_fence *output_fence;
221 bool output_signaled;
222
223 u32 dnsc_factor_w;
224 u32 dnsc_factor_h;
225
226 struct sde_rot_perf *perf;
227 bool work_assigned; /* Used when cleaning up work_distribution */
228 struct sde_rot_file_private *private;
229};
230
231struct sde_rot_perf {
232 struct list_head list;
233 struct sde_rotation_config config;
234 unsigned long clk_rate;
235 u64 bw;
236 struct mutex work_dis_lock;
237 u32 *work_distribution;
238 int last_wb_idx; /* last known wb index, used when above count is 0 */
Alan Kwongeffb5ee2016-03-12 19:47:45 -0500239 u32 rdot_limit;
240 u32 wrot_limit;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700241};
242
243struct sde_rot_file_private {
244 struct list_head list;
245 struct list_head req_list;
246 struct list_head perf_list;
247 struct sde_rot_mgr *mgr;
248 struct sde_rot_queue *fenceq;
249};
250
251struct sde_rot_bus_data_type {
252 struct msm_bus_scale_pdata *bus_scale_pdata;
253 u32 bus_hdl;
254 u32 curr_bw_uc_idx;
255 u64 curr_quota_val;
256};
257
258struct sde_rot_mgr {
259 struct mutex lock;
260 atomic_t device_suspended;
261 struct platform_device *pdev;
262 struct device *device;
263
264 /*
265 * Managing rotation queues, depends on
266 * how many hw pipes available on the system
267 */
268 int queue_count;
269 struct sde_rot_queue *commitq;
270 struct sde_rot_queue *doneq;
271
272 /*
273 * managing all the open file sessions to bw calculations,
274 * and resource clean up during suspend
275 */
276 struct list_head file_list;
277
278 u64 pending_close_bw_vote;
279 struct sde_rot_bus_data_type data_bus;
280 struct sde_rot_bus_data_type reg_bus;
281
282 /* Module power is only used for regulator management */
283 struct sde_module_power module_power;
284 bool regulator_enable;
285
286 int res_ref_cnt;
287 int rot_enable_clk_cnt;
288 struct sde_rot_clk *rot_clk;
289 int num_rot_clk;
Alan Kwongeffb5ee2016-03-12 19:47:45 -0500290 u32 rdot_limit;
291 u32 wrot_limit;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700292
293 u32 hwacquire_timeout;
294 struct sde_mult_factor pixel_per_clk;
Benjamin Chandbe13112016-09-26 12:10:06 -0400295 struct sde_mult_factor fudge_factor;
296 struct sde_mult_factor overhead;
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700297
298 int (*ops_config_hw)(struct sde_rot_hw_resource *hw,
299 struct sde_rot_entry *entry);
300 int (*ops_kickoff_entry)(struct sde_rot_hw_resource *hw,
301 struct sde_rot_entry *entry);
302 int (*ops_wait_for_entry)(struct sde_rot_hw_resource *hw,
303 struct sde_rot_entry *entry);
304 struct sde_rot_hw_resource *(*ops_hw_alloc)(struct sde_rot_mgr *mgr,
305 u32 pipe_id, u32 wb_id);
306 void (*ops_hw_free)(struct sde_rot_mgr *mgr,
307 struct sde_rot_hw_resource *hw);
308 int (*ops_hw_init)(struct sde_rot_mgr *mgr);
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400309 void (*ops_hw_pre_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
310 void (*ops_hw_post_pmevent)(struct sde_rot_mgr *mgr, bool pmon);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700311 void (*ops_hw_destroy)(struct sde_rot_mgr *mgr);
312 ssize_t (*ops_hw_show_caps)(struct sde_rot_mgr *mgr,
313 struct device_attribute *attr, char *buf, ssize_t len);
314 ssize_t (*ops_hw_show_state)(struct sde_rot_mgr *mgr,
315 struct device_attribute *attr, char *buf, ssize_t len);
316 int (*ops_hw_create_debugfs)(struct sde_rot_mgr *mgr,
317 struct dentry *debugfs_root);
318 int (*ops_hw_validate_entry)(struct sde_rot_mgr *mgr,
319 struct sde_rot_entry *entry);
Alan Kwongda16e442016-08-14 20:47:18 -0400320 u32 (*ops_hw_get_pixfmt)(struct sde_rot_mgr *mgr, int index,
321 bool input);
322 int (*ops_hw_is_valid_pixfmt)(struct sde_rot_mgr *mgr, u32 pixfmt,
323 bool input);
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700324
325 void *hw_data;
326};
327
Alan Kwongda16e442016-08-14 20:47:18 -0400328static inline int sde_rotator_is_valid_pixfmt(struct sde_rot_mgr *mgr,
329 u32 pixfmt, bool input)
330{
331 if (mgr && mgr->ops_hw_is_valid_pixfmt)
332 return mgr->ops_hw_is_valid_pixfmt(mgr, pixfmt, input);
333
334 return false;
335}
336
337static inline u32 sde_rotator_get_pixfmt(struct sde_rot_mgr *mgr,
338 int index, bool input)
339{
340 if (mgr && mgr->ops_hw_get_pixfmt)
341 return mgr->ops_hw_get_pixfmt(mgr, index, input);
342
343 return 0;
344}
345
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700346static inline int __compare_session_item_rect(
347 struct sde_rotation_buf_info *s_rect,
348 struct sde_rect *i_rect, uint32_t i_fmt, bool src)
349{
350 if ((s_rect->width != i_rect->w) || (s_rect->height != i_rect->h) ||
351 (s_rect->format != i_fmt)) {
352 SDEROT_DBG(
353 "%s: session{%u,%u}f:%u mismatch from item{%u,%u}f:%u\n",
354 (src ? "src":"dst"), s_rect->width, s_rect->height,
355 s_rect->format, i_rect->w, i_rect->h, i_fmt);
356 return -EINVAL;
357 }
358 return 0;
359}
360
361/*
362 * Compare all important flag bits associated with rotation between session
363 * config and item request. Format and roi validation is done during open
364 * session and is based certain defining bits. If these defining bits are
365 * different in item request, there is a possibility that rotation item
366 * is not a valid configuration.
367 */
368static inline int __compare_session_rotations(uint32_t cfg_flag,
369 uint32_t item_flag)
370{
371 cfg_flag &= SDE_ROT_DEFINING_FLAG_BITS;
372 item_flag &= SDE_ROT_DEFINING_FLAG_BITS;
373 if (cfg_flag != item_flag) {
374 SDEROT_DBG(
375 "Rotation degree request different from open session\n");
376 return -EINVAL;
377 }
378 return 0;
379}
380
381int sde_rotator_core_init(struct sde_rot_mgr **pmgr,
382 struct platform_device *pdev);
383
384void sde_rotator_core_destroy(struct sde_rot_mgr *mgr);
385
386int sde_rotator_session_open(struct sde_rot_mgr *mgr,
387 struct sde_rot_file_private **pprivate, int session_id,
388 struct sde_rot_queue *queue);
389
390void sde_rotator_session_close(struct sde_rot_mgr *mgr,
391 struct sde_rot_file_private *private, int session_id);
392
393int sde_rotator_session_config(struct sde_rot_mgr *mgr,
394 struct sde_rot_file_private *private,
395 struct sde_rotation_config *config);
396
397struct sde_rot_entry_container *sde_rotator_req_init(
398 struct sde_rot_mgr *rot_dev,
399 struct sde_rot_file_private *private,
400 struct sde_rotation_item *items,
401 u32 count, u32 flags);
402
403int sde_rotator_handle_request_common(struct sde_rot_mgr *rot_dev,
404 struct sde_rot_file_private *ctx,
405 struct sde_rot_entry_container *req,
406 struct sde_rotation_item *items);
407
408void sde_rotator_queue_request(struct sde_rot_mgr *rot_dev,
409 struct sde_rot_file_private *ctx,
410 struct sde_rot_entry_container *req);
411
412void sde_rotator_remove_request(struct sde_rot_mgr *mgr,
413 struct sde_rot_file_private *private,
414 struct sde_rot_entry_container *req);
415
416int sde_rotator_verify_config(struct sde_rot_mgr *rot_dev,
417 struct sde_rotation_config *config);
418
419int sde_rotator_validate_request(struct sde_rot_mgr *rot_dev,
420 struct sde_rot_file_private *ctx,
421 struct sde_rot_entry_container *req);
422
Benjamin Chan0f9e61d2016-09-16 16:01:09 -0400423int sde_rotator_clk_ctrl(struct sde_rot_mgr *mgr, int enable);
424
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700425static inline void sde_rot_mgr_lock(struct sde_rot_mgr *mgr)
426{
427 mutex_lock(&mgr->lock);
428}
429
430static inline void sde_rot_mgr_unlock(struct sde_rot_mgr *mgr)
431{
432 mutex_unlock(&mgr->lock);
433}
434
Alan Kwong3428f672016-04-18 12:32:06 -0400435#if defined(CONFIG_PM)
Adrian Salido-Moreno5c150382016-04-06 09:29:37 -0700436int sde_rotator_runtime_resume(struct device *dev);
437int sde_rotator_runtime_suspend(struct device *dev);
438int sde_rotator_runtime_idle(struct device *dev);
439#endif
440
441#if defined(CONFIG_PM_SLEEP)
442int sde_rotator_pm_suspend(struct device *dev);
443int sde_rotator_pm_resume(struct device *dev);
444#endif
445
446#if defined(CONFIG_PM) && !defined(CONFIG_PM_SLEEP)
447int sde_rotator_suspend(struct platform_device *dev, pm_message_t state);
448int sde_rotator_resume(struct platform_device *dev);
449#else
450#define sde_rotator_suspend NULL
451#define sde_rotator_resume NULL
452#endif
453#endif /* __SDE_ROTATOR_CORE_H__ */