Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 1 | /* 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 Kwong | a94684f | 2016-01-16 22:06:36 -0500 | [diff] [blame] | 62 | /********************************************************************** |
| 63 | * configuration structures |
| 64 | **********************************************************************/ |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 65 | |
| 66 | struct 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 | |
| 73 | struct 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 | |
| 81 | enum 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 | |
| 95 | struct 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 Kwong | a94684f | 2016-01-16 22:06:36 -0500 | [diff] [blame] | 111 | /* |
| 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-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 122 | uint32_t pipe_idx; |
| 123 | |
Alan Kwong | a94684f | 2016-01-16 22:06:36 -0500 | [diff] [blame] | 124 | /* |
| 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-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 137 | uint32_t wb_idx; |
| 138 | |
Alan Kwong | a94684f | 2016-01-16 22:06:36 -0500 | [diff] [blame] | 139 | /* |
| 140 | * Sequence ID of this request within the session |
| 141 | */ |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 142 | 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 | |
| 157 | struct sde_rot_entry; |
| 158 | struct sde_rot_perf; |
| 159 | |
| 160 | struct sde_rot_clk { |
| 161 | struct clk *clk; |
| 162 | char clk_name[32]; |
| 163 | unsigned long rate; |
| 164 | }; |
| 165 | |
| 166 | struct 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-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | struct 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 | |
| 180 | struct 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 | |
| 191 | struct sde_rot_mgr; |
| 192 | struct sde_rot_file_private; |
| 193 | |
| 194 | struct 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 | |
| 219 | struct 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 Kwong | effb5ee | 2016-03-12 19:47:45 -0500 | [diff] [blame] | 227 | u32 rdot_limit; |
| 228 | u32 wrot_limit; |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | struct 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 | |
| 239 | struct 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 | |
| 246 | struct 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 Kwong | effb5ee | 2016-03-12 19:47:45 -0500 | [diff] [blame] | 279 | u32 rdot_limit; |
| 280 | u32 wrot_limit; |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 281 | |
| 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); |
Alan Kwong | da16e44 | 2016-08-14 20:47:18 -0400 | [diff] [blame^] | 305 | u32 (*ops_hw_get_pixfmt)(struct sde_rot_mgr *mgr, int index, |
| 306 | bool input); |
| 307 | int (*ops_hw_is_valid_pixfmt)(struct sde_rot_mgr *mgr, u32 pixfmt, |
| 308 | bool input); |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 309 | |
| 310 | void *hw_data; |
| 311 | }; |
| 312 | |
Alan Kwong | da16e44 | 2016-08-14 20:47:18 -0400 | [diff] [blame^] | 313 | static inline int sde_rotator_is_valid_pixfmt(struct sde_rot_mgr *mgr, |
| 314 | u32 pixfmt, bool input) |
| 315 | { |
| 316 | if (mgr && mgr->ops_hw_is_valid_pixfmt) |
| 317 | return mgr->ops_hw_is_valid_pixfmt(mgr, pixfmt, input); |
| 318 | |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | static inline u32 sde_rotator_get_pixfmt(struct sde_rot_mgr *mgr, |
| 323 | int index, bool input) |
| 324 | { |
| 325 | if (mgr && mgr->ops_hw_get_pixfmt) |
| 326 | return mgr->ops_hw_get_pixfmt(mgr, index, input); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 331 | static inline int __compare_session_item_rect( |
| 332 | struct sde_rotation_buf_info *s_rect, |
| 333 | struct sde_rect *i_rect, uint32_t i_fmt, bool src) |
| 334 | { |
| 335 | if ((s_rect->width != i_rect->w) || (s_rect->height != i_rect->h) || |
| 336 | (s_rect->format != i_fmt)) { |
| 337 | SDEROT_DBG( |
| 338 | "%s: session{%u,%u}f:%u mismatch from item{%u,%u}f:%u\n", |
| 339 | (src ? "src":"dst"), s_rect->width, s_rect->height, |
| 340 | s_rect->format, i_rect->w, i_rect->h, i_fmt); |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Compare all important flag bits associated with rotation between session |
| 348 | * config and item request. Format and roi validation is done during open |
| 349 | * session and is based certain defining bits. If these defining bits are |
| 350 | * different in item request, there is a possibility that rotation item |
| 351 | * is not a valid configuration. |
| 352 | */ |
| 353 | static inline int __compare_session_rotations(uint32_t cfg_flag, |
| 354 | uint32_t item_flag) |
| 355 | { |
| 356 | cfg_flag &= SDE_ROT_DEFINING_FLAG_BITS; |
| 357 | item_flag &= SDE_ROT_DEFINING_FLAG_BITS; |
| 358 | if (cfg_flag != item_flag) { |
| 359 | SDEROT_DBG( |
| 360 | "Rotation degree request different from open session\n"); |
| 361 | return -EINVAL; |
| 362 | } |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | int sde_rotator_core_init(struct sde_rot_mgr **pmgr, |
| 367 | struct platform_device *pdev); |
| 368 | |
| 369 | void sde_rotator_core_destroy(struct sde_rot_mgr *mgr); |
| 370 | |
| 371 | int sde_rotator_session_open(struct sde_rot_mgr *mgr, |
| 372 | struct sde_rot_file_private **pprivate, int session_id, |
| 373 | struct sde_rot_queue *queue); |
| 374 | |
| 375 | void sde_rotator_session_close(struct sde_rot_mgr *mgr, |
| 376 | struct sde_rot_file_private *private, int session_id); |
| 377 | |
| 378 | int sde_rotator_session_config(struct sde_rot_mgr *mgr, |
| 379 | struct sde_rot_file_private *private, |
| 380 | struct sde_rotation_config *config); |
| 381 | |
| 382 | struct sde_rot_entry_container *sde_rotator_req_init( |
| 383 | struct sde_rot_mgr *rot_dev, |
| 384 | struct sde_rot_file_private *private, |
| 385 | struct sde_rotation_item *items, |
| 386 | u32 count, u32 flags); |
| 387 | |
| 388 | int sde_rotator_handle_request_common(struct sde_rot_mgr *rot_dev, |
| 389 | struct sde_rot_file_private *ctx, |
| 390 | struct sde_rot_entry_container *req, |
| 391 | struct sde_rotation_item *items); |
| 392 | |
| 393 | void sde_rotator_queue_request(struct sde_rot_mgr *rot_dev, |
| 394 | struct sde_rot_file_private *ctx, |
| 395 | struct sde_rot_entry_container *req); |
| 396 | |
| 397 | void sde_rotator_remove_request(struct sde_rot_mgr *mgr, |
| 398 | struct sde_rot_file_private *private, |
| 399 | struct sde_rot_entry_container *req); |
| 400 | |
| 401 | int sde_rotator_verify_config(struct sde_rot_mgr *rot_dev, |
| 402 | struct sde_rotation_config *config); |
| 403 | |
| 404 | int sde_rotator_validate_request(struct sde_rot_mgr *rot_dev, |
| 405 | struct sde_rot_file_private *ctx, |
| 406 | struct sde_rot_entry_container *req); |
| 407 | |
| 408 | static inline void sde_rot_mgr_lock(struct sde_rot_mgr *mgr) |
| 409 | { |
| 410 | mutex_lock(&mgr->lock); |
| 411 | } |
| 412 | |
| 413 | static inline void sde_rot_mgr_unlock(struct sde_rot_mgr *mgr) |
| 414 | { |
| 415 | mutex_unlock(&mgr->lock); |
| 416 | } |
| 417 | |
Alan Kwong | 3428f67 | 2016-04-18 12:32:06 -0400 | [diff] [blame] | 418 | #if defined(CONFIG_PM) |
Adrian Salido-Moreno | 5c15038 | 2016-04-06 09:29:37 -0700 | [diff] [blame] | 419 | int sde_rotator_runtime_resume(struct device *dev); |
| 420 | int sde_rotator_runtime_suspend(struct device *dev); |
| 421 | int sde_rotator_runtime_idle(struct device *dev); |
| 422 | #endif |
| 423 | |
| 424 | #if defined(CONFIG_PM_SLEEP) |
| 425 | int sde_rotator_pm_suspend(struct device *dev); |
| 426 | int sde_rotator_pm_resume(struct device *dev); |
| 427 | #endif |
| 428 | |
| 429 | #if defined(CONFIG_PM) && !defined(CONFIG_PM_SLEEP) |
| 430 | int sde_rotator_suspend(struct platform_device *dev, pm_message_t state); |
| 431 | int sde_rotator_resume(struct platform_device *dev); |
| 432 | #else |
| 433 | #define sde_rotator_suspend NULL |
| 434 | #define sde_rotator_resume NULL |
| 435 | #endif |
| 436 | #endif /* __SDE_ROTATOR_CORE_H__ */ |