blob: 49e6c3ec5b03dedc49079dbaa0b2b706c760a29f [file] [log] [blame]
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -08001/* Copyright (c) 2012-2017, 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 _MSM_VIDC_INTERNAL_H_
15#define _MSM_VIDC_INTERNAL_H_
16
17#include <linux/atomic.h>
18#include <linux/list.h>
19#include <linux/time.h>
20#include <linux/types.h>
21#include <linux/completion.h>
22#include <linux/wait.h>
23#include <linux/workqueue.h>
24#include <linux/msm-bus.h>
25#include <linux/msm-bus-board.h>
26#include <linux/kref.h>
27#include <media/v4l2-dev.h>
28#include <media/v4l2-device.h>
29#include <media/v4l2-ioctl.h>
30#include <media/v4l2-event.h>
31#include <media/v4l2-ctrls.h>
32#include <media/videobuf2-core.h>
33#include <media/videobuf2-v4l2.h>
34#include <media/msm_vidc.h>
35#include <media/msm_media_info.h>
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080036#include "vidc_hfi_api.h"
37
38#define MSM_VIDC_DRV_NAME "msm_vidc_driver"
39#define MSM_VIDC_VERSION KERNEL_VERSION(0, 0, 1)
40#define MAX_DEBUGFS_NAME 50
41#define DEFAULT_TIMEOUT 3
42#define DEFAULT_HEIGHT 1088
43#define DEFAULT_WIDTH 1920
44#define MIN_SUPPORTED_WIDTH 32
45#define MIN_SUPPORTED_HEIGHT 32
46#define DEFAULT_FPS 15
Praneeth Paladugudefea4e2017-02-09 23:44:08 -080047#define MIN_NUM_OUTPUT_BUFFERS 1
48#define MIN_NUM_CAPTURE_BUFFERS 1
49#define MAX_NUM_OUTPUT_BUFFERS VIDEO_MAX_FRAME // same as VB2_MAX_FRAME
50#define MAX_NUM_CAPTURE_BUFFERS VIDEO_MAX_FRAME // same as VB2_MAX_FRAME
51
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080052
53/* Maintains the number of FTB's between each FBD over a window */
Praneeth Paladugue5fd0872017-04-19 11:24:28 -070054#define DCVS_FTB_WINDOW 16
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080055
56#define V4L2_EVENT_VIDC_BASE 10
57
58#define SYS_MSG_START HAL_SYS_INIT_DONE
59#define SYS_MSG_END HAL_SYS_ERROR
60#define SESSION_MSG_START HAL_SESSION_EVENT_CHANGE
61#define SESSION_MSG_END HAL_SESSION_ERROR
62#define SYS_MSG_INDEX(__msg) (__msg - SYS_MSG_START)
63#define SESSION_MSG_INDEX(__msg) (__msg - SESSION_MSG_START)
64
65
66#define MAX_NAME_LENGTH 64
67
68#define EXTRADATA_IDX(__num_planes) ((__num_planes) ? (__num_planes) - 1 : 0)
69
70#define NUM_MBS_PER_SEC(__height, __width, __fps) \
71 (NUM_MBS_PER_FRAME(__height, __width) * __fps)
72
73#define NUM_MBS_PER_FRAME(__height, __width) \
74 ((ALIGN(__height, 16) / 16) * (ALIGN(__width, 16) / 16))
75
76enum vidc_ports {
77 OUTPUT_PORT,
78 CAPTURE_PORT,
79 MAX_PORT_NUM
80};
81
82enum vidc_core_state {
83 VIDC_CORE_UNINIT = 0,
84 VIDC_CORE_INIT,
85 VIDC_CORE_INIT_DONE,
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080086};
87
88/*
89 * Do not change the enum values unless
90 * you know what you are doing
91 */
92enum instance_state {
93 MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
94 MSM_VIDC_CORE_INIT,
95 MSM_VIDC_CORE_INIT_DONE,
96 MSM_VIDC_OPEN,
97 MSM_VIDC_OPEN_DONE,
98 MSM_VIDC_LOAD_RESOURCES,
99 MSM_VIDC_LOAD_RESOURCES_DONE,
100 MSM_VIDC_START,
101 MSM_VIDC_START_DONE,
102 MSM_VIDC_STOP,
103 MSM_VIDC_STOP_DONE,
104 MSM_VIDC_RELEASE_RESOURCES,
105 MSM_VIDC_RELEASE_RESOURCES_DONE,
106 MSM_VIDC_CLOSE,
107 MSM_VIDC_CLOSE_DONE,
108 MSM_VIDC_CORE_UNINIT,
109 MSM_VIDC_CORE_INVALID
110};
111
112struct buf_info {
113 struct list_head list;
114 struct vb2_buffer *buf;
115};
116
117struct msm_vidc_list {
118 struct list_head list;
119 struct mutex lock;
120};
121
122static inline void INIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
123{
124 mutex_init(&mlist->lock);
125 INIT_LIST_HEAD(&mlist->list);
126}
127
128static inline void DEINIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
129{
130 mutex_destroy(&mlist->lock);
131}
132
133enum buffer_owner {
134 DRIVER,
135 FIRMWARE,
136 CLIENT,
137 MAX_OWNER
138};
139
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700140struct vidc_freq_data {
141 struct list_head list;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700142 u32 device_addr;
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700143 unsigned long freq;
144};
145
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700146struct recon_buf {
147 struct list_head list;
148 u32 buffer_index;
149 u32 CR;
150 u32 CF;
151};
152
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800153struct internal_buf {
154 struct list_head list;
155 enum hal_buffer buffer_type;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700156 struct msm_smem smem;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800157 enum buffer_owner buffer_ownership;
158};
159
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700160struct msm_vidc_common_data {
161 char key[128];
162 int value;
163};
164
165struct msm_vidc_codec_data {
166 u32 fourcc;
167 enum session_type session_type;
168 int vpp_cycles;
169 int vsp_cycles;
170 int low_power_cycles;
171};
172
173struct msm_vidc_platform_data {
174 struct msm_vidc_common_data *common_data;
175 unsigned int common_data_length;
176 struct msm_vidc_codec_data *codec_data;
177 unsigned int codec_data_length;
178};
179
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800180struct msm_vidc_format {
181 char name[MAX_NAME_LENGTH];
182 u8 description[32];
183 u32 fourcc;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800184 int type;
185 u32 (*get_frame_size)(int plane, u32 height, u32 width);
Vaibhav Deshu Venkatesh8c9b6db2017-02-08 11:22:36 -0800186 bool defer_outputs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800187};
188
189struct msm_vidc_drv {
190 struct mutex lock;
191 struct list_head cores;
192 int num_cores;
193 struct dentry *debugfs_root;
194 int thermal_level;
195 u32 platform_version;
196};
197
198struct msm_video_device {
199 int type;
200 struct video_device vdev;
201};
202
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700203struct session_crop {
204 u32 left;
205 u32 top;
206 u32 width;
207 u32 height;
208};
209
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800210struct session_prop {
211 u32 width[MAX_PORT_NUM];
212 u32 height[MAX_PORT_NUM];
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700213 struct session_crop crop_info;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800214 u32 fps;
215 u32 bitrate;
216};
217
218struct buf_queue {
219 struct vb2_queue vb2_bufq;
220 struct mutex lock;
Praneeth Paladugudefea4e2017-02-09 23:44:08 -0800221 unsigned int plane_sizes[VB2_MAX_PLANES];
222 int num_planes;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800223};
224
225enum profiling_points {
226 SYS_INIT = 0,
227 SESSION_INIT,
228 LOAD_RESOURCES,
229 FRAME_PROCESSING,
230 FW_IDLE,
231 MAX_PROFILING_POINTS,
232};
233
234struct buf_count {
235 int etb;
236 int ftb;
237 int fbd;
238 int ebd;
239};
240
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700241struct clock_data {
Praneeth Paladugu75cf18e2016-12-08 16:12:11 -0800242 int buffer_counter;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800243 int load;
244 int load_low;
Praneeth Paladugu04e77722017-06-21 11:38:31 -0700245 int load_norm;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800246 int load_high;
247 int min_threshold;
248 int max_threshold;
Praneeth Paladugu1f9d1d92017-04-11 10:36:16 -0700249 unsigned int extra_capture_buffer_count;
250 unsigned int extra_output_buffer_count;
Praneeth Paladugu75cf18e2016-12-08 16:12:11 -0800251 enum hal_buffer buffer_type;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700252 bool dcvs_mode;
253 unsigned long bitrate;
254 unsigned long min_freq;
255 unsigned long curr_freq;
256 u32 operating_rate;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700257 struct msm_vidc_codec_data *entry;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700258 u32 core_id;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700259 u32 dpb_fourcc;
260 u32 opb_fourcc;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700261 enum hal_work_mode work_mode;
262 bool low_latency_mode;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800263};
264
265struct profile_data {
266 int start;
267 int stop;
268 int cumulative;
269 char name[64];
270 int sampling;
271 int average;
272};
273
274struct msm_vidc_debug {
275 struct profile_data pdata[MAX_PROFILING_POINTS];
276 int profile;
277 int samples;
278};
279
280enum msm_vidc_modes {
281 VIDC_SECURE = BIT(0),
282 VIDC_TURBO = BIT(1),
283 VIDC_THUMBNAIL = BIT(2),
284 VIDC_LOW_POWER = BIT(3),
285 VIDC_REALTIME = BIT(4),
286};
287
288struct msm_vidc_core {
289 struct list_head list;
290 struct mutex lock;
291 int id;
292 struct hfi_device *device;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700293 struct msm_vidc_platform_data *platform_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800294 struct msm_video_device vdev[MSM_VIDC_MAX_DEVICES];
295 struct v4l2_device v4l2_dev;
296 struct list_head instances;
297 struct dentry *debugfs_root;
298 enum vidc_core_state state;
299 struct completion completions[SYS_MSG_END - SYS_MSG_START + 1];
300 enum msm_vidc_hfi_type hfi_type;
301 struct msm_vidc_platform_resources resources;
302 u32 enc_codec_supported;
303 u32 dec_codec_supported;
304 u32 codec_count;
305 struct msm_vidc_capability *capabilities;
306 struct delayed_work fw_unload_work;
307 bool smmu_fault_handled;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700308 unsigned long min_freq;
309 unsigned long curr_freq;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700310 struct vidc_bus_vote_data *vote_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800311};
312
313struct msm_vidc_inst {
314 struct list_head list;
315 struct mutex sync_lock, lock;
316 struct msm_vidc_core *core;
317 enum session_type session_type;
318 void *session;
319 struct session_prop prop;
320 enum instance_state state;
321 struct msm_vidc_format fmts[MAX_PORT_NUM];
322 struct buf_queue bufq[MAX_PORT_NUM];
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700323 struct msm_vidc_list freqs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800324 struct msm_vidc_list scratchbufs;
325 struct msm_vidc_list persistbufs;
326 struct msm_vidc_list pending_getpropq;
327 struct msm_vidc_list outputbufs;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700328 struct msm_vidc_list reconbufs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800329 struct msm_vidc_list registeredbufs;
330 struct buffer_requirements buff_req;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700331 struct smem_client *mem_client;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800332 struct v4l2_ctrl_handler ctrl_handler;
333 struct completion completions[SESSION_MSG_END - SESSION_MSG_START + 1];
334 struct v4l2_ctrl **cluster;
335 struct v4l2_fh event_handler;
336 struct msm_smem *extradata_handle;
337 bool in_reconfig;
338 u32 reconfig_width;
339 u32 reconfig_height;
340 struct dentry *debugfs_root;
341 void *priv;
342 struct msm_vidc_debug debug;
343 struct buf_count count;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700344 struct clock_data clk_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800345 enum msm_vidc_modes flags;
346 struct msm_vidc_capability capability;
347 u32 buffer_size_limit;
348 enum buffer_mode_type buffer_mode_set[MAX_PORT_NUM];
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800349 struct v4l2_ctrl **ctrls;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800350 enum msm_vidc_pixel_depth bit_depth;
351 struct kref kref;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700352 bool in_flush;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800353 u32 pic_struct;
354 u32 colour_space;
Chinmay Sawarkarb3c6ccb2017-02-23 18:01:32 -0800355 u32 profile;
356 u32 level;
357 u32 entropy_mode;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700358 struct msm_vidc_codec_data *codec_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800359};
360
361extern struct msm_vidc_drv *vidc_driver;
362
363struct msm_vidc_ctrl_cluster {
364 struct v4l2_ctrl **cluster;
365 struct list_head list;
366};
367
368struct msm_vidc_ctrl {
369 u32 id;
370 char name[MAX_NAME_LENGTH];
371 enum v4l2_ctrl_type type;
372 s32 minimum;
373 s32 maximum;
374 s32 default_value;
375 u32 step;
376 u32 menu_skip_mask;
377 u32 flags;
378 const char * const *qmenu;
379};
380
381void handle_cmd_response(enum hal_command_response cmd, void *data);
382int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
383 enum hal_ssr_trigger_type type);
384int msm_vidc_check_session_supported(struct msm_vidc_inst *inst);
385int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst);
386void msm_vidc_queue_v4l2_event(struct msm_vidc_inst *inst, int event_type);
387
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700388struct msm_vidc_buffer {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800389 struct list_head list;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700390 struct msm_smem smem[VIDEO_MAX_PLANES];
391 struct vb2_v4l2_buffer vvb;
392 bool deferred;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800393};
394
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800395void msm_comm_handle_thermal_event(void);
396void *msm_smem_new_client(enum smem_type mtype,
397 void *platform_resources, enum session_type stype);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700398int msm_smem_alloc(struct smem_client *client,
399 size_t size, u32 align, u32 flags, enum hal_buffer buffer_type,
400 int map_kernel, struct msm_smem *smem);
401int msm_smem_free(void *clt, struct msm_smem *mem);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800402void msm_smem_delete_client(void *clt);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800403struct context_bank_info *msm_smem_get_context_bank(void *clt,
404 bool is_secure, enum hal_buffer buffer_type);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700405int msm_smem_map_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
406int msm_smem_unmap_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
407void *msm_smem_get_dma_buf(int fd);
408void msm_smem_put_dma_buf(void *dma_buf);
409void *msm_smem_get_handle(struct smem_client *client, void *dma_buf);
410void msm_smem_put_handle(struct smem_client *client, void *handle);
411int msm_smem_cache_operations(struct smem_client *client,
412 void *handle, unsigned long offset, unsigned long size,
413 enum smem_cache_ops cache_op);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800414void msm_vidc_fw_unload_handler(struct work_struct *work);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800415/*
416 * XXX: normally should be in msm_vidc.h, but that's meant for public APIs,
417 * whereas this is private
418 */
419int msm_vidc_destroy(struct msm_vidc_inst *inst);
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700420void *vidc_get_drv_data(struct device *dev);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800421#endif