blob: 22772ef021bc887bdbc8a1d720df2751a8af12a1 [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,
86 VIDC_CORE_INVALID
87};
88
89/*
90 * Do not change the enum values unless
91 * you know what you are doing
92 */
93enum instance_state {
94 MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
95 MSM_VIDC_CORE_INIT,
96 MSM_VIDC_CORE_INIT_DONE,
97 MSM_VIDC_OPEN,
98 MSM_VIDC_OPEN_DONE,
99 MSM_VIDC_LOAD_RESOURCES,
100 MSM_VIDC_LOAD_RESOURCES_DONE,
101 MSM_VIDC_START,
102 MSM_VIDC_START_DONE,
103 MSM_VIDC_STOP,
104 MSM_VIDC_STOP_DONE,
105 MSM_VIDC_RELEASE_RESOURCES,
106 MSM_VIDC_RELEASE_RESOURCES_DONE,
107 MSM_VIDC_CLOSE,
108 MSM_VIDC_CLOSE_DONE,
109 MSM_VIDC_CORE_UNINIT,
110 MSM_VIDC_CORE_INVALID
111};
112
113struct buf_info {
114 struct list_head list;
115 struct vb2_buffer *buf;
116};
117
118struct msm_vidc_list {
119 struct list_head list;
120 struct mutex lock;
121};
122
123static inline void INIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
124{
125 mutex_init(&mlist->lock);
126 INIT_LIST_HEAD(&mlist->list);
127}
128
129static inline void DEINIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
130{
131 mutex_destroy(&mlist->lock);
132}
133
134enum buffer_owner {
135 DRIVER,
136 FIRMWARE,
137 CLIENT,
138 MAX_OWNER
139};
140
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700141struct vidc_freq_data {
142 struct list_head list;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700143 u32 device_addr;
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700144 unsigned long freq;
145};
146
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700147struct recon_buf {
148 struct list_head list;
149 u32 buffer_index;
150 u32 CR;
151 u32 CF;
152};
153
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800154struct internal_buf {
155 struct list_head list;
156 enum hal_buffer buffer_type;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700157 struct msm_smem smem;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800158 enum buffer_owner buffer_ownership;
159};
160
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700161struct msm_vidc_common_data {
162 char key[128];
163 int value;
164};
165
166struct msm_vidc_codec_data {
167 u32 fourcc;
168 enum session_type session_type;
169 int vpp_cycles;
170 int vsp_cycles;
171 int low_power_cycles;
172};
173
174struct msm_vidc_platform_data {
175 struct msm_vidc_common_data *common_data;
176 unsigned int common_data_length;
177 struct msm_vidc_codec_data *codec_data;
178 unsigned int codec_data_length;
179};
180
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800181struct msm_vidc_format {
182 char name[MAX_NAME_LENGTH];
183 u8 description[32];
184 u32 fourcc;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800185 int type;
186 u32 (*get_frame_size)(int plane, u32 height, u32 width);
Vaibhav Deshu Venkatesh8c9b6db2017-02-08 11:22:36 -0800187 bool defer_outputs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800188};
189
190struct msm_vidc_drv {
191 struct mutex lock;
192 struct list_head cores;
193 int num_cores;
194 struct dentry *debugfs_root;
195 int thermal_level;
196 u32 platform_version;
197};
198
199struct msm_video_device {
200 int type;
201 struct video_device vdev;
202};
203
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700204struct session_crop {
205 u32 left;
206 u32 top;
207 u32 width;
208 u32 height;
209};
210
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800211struct session_prop {
212 u32 width[MAX_PORT_NUM];
213 u32 height[MAX_PORT_NUM];
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700214 struct session_crop crop_info;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800215 u32 fps;
216 u32 bitrate;
217};
218
219struct buf_queue {
220 struct vb2_queue vb2_bufq;
221 struct mutex lock;
Praneeth Paladugudefea4e2017-02-09 23:44:08 -0800222 unsigned int plane_sizes[VB2_MAX_PLANES];
223 int num_planes;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800224};
225
226enum profiling_points {
227 SYS_INIT = 0,
228 SESSION_INIT,
229 LOAD_RESOURCES,
230 FRAME_PROCESSING,
231 FW_IDLE,
232 MAX_PROFILING_POINTS,
233};
234
235struct buf_count {
236 int etb;
237 int ftb;
238 int fbd;
239 int ebd;
240};
241
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700242struct clock_data {
Praneeth Paladugu75cf18e2016-12-08 16:12:11 -0800243 int buffer_counter;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800244 int load;
245 int load_low;
246 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 Paladugud5bf7bc2017-05-29 23:41:04 -0700263 bool use_sys_cache;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800264};
265
266struct profile_data {
267 int start;
268 int stop;
269 int cumulative;
270 char name[64];
271 int sampling;
272 int average;
273};
274
275struct msm_vidc_debug {
276 struct profile_data pdata[MAX_PROFILING_POINTS];
277 int profile;
278 int samples;
279};
280
281enum msm_vidc_modes {
282 VIDC_SECURE = BIT(0),
283 VIDC_TURBO = BIT(1),
284 VIDC_THUMBNAIL = BIT(2),
285 VIDC_LOW_POWER = BIT(3),
286 VIDC_REALTIME = BIT(4),
287};
288
289struct msm_vidc_core {
290 struct list_head list;
291 struct mutex lock;
292 int id;
293 struct hfi_device *device;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700294 struct msm_vidc_platform_data *platform_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800295 struct msm_video_device vdev[MSM_VIDC_MAX_DEVICES];
296 struct v4l2_device v4l2_dev;
297 struct list_head instances;
298 struct dentry *debugfs_root;
299 enum vidc_core_state state;
300 struct completion completions[SYS_MSG_END - SYS_MSG_START + 1];
301 enum msm_vidc_hfi_type hfi_type;
302 struct msm_vidc_platform_resources resources;
303 u32 enc_codec_supported;
304 u32 dec_codec_supported;
305 u32 codec_count;
306 struct msm_vidc_capability *capabilities;
307 struct delayed_work fw_unload_work;
308 bool smmu_fault_handled;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700309 unsigned long min_freq;
310 unsigned long curr_freq;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700311 struct vidc_bus_vote_data *vote_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800312};
313
314struct msm_vidc_inst {
315 struct list_head list;
316 struct mutex sync_lock, lock;
317 struct msm_vidc_core *core;
318 enum session_type session_type;
319 void *session;
320 struct session_prop prop;
321 enum instance_state state;
322 struct msm_vidc_format fmts[MAX_PORT_NUM];
323 struct buf_queue bufq[MAX_PORT_NUM];
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700324 struct msm_vidc_list freqs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800325 struct msm_vidc_list scratchbufs;
326 struct msm_vidc_list persistbufs;
327 struct msm_vidc_list pending_getpropq;
328 struct msm_vidc_list outputbufs;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700329 struct msm_vidc_list reconbufs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800330 struct msm_vidc_list registeredbufs;
331 struct buffer_requirements buff_req;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700332 struct smem_client *mem_client;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800333 struct v4l2_ctrl_handler ctrl_handler;
334 struct completion completions[SESSION_MSG_END - SESSION_MSG_START + 1];
335 struct v4l2_ctrl **cluster;
336 struct v4l2_fh event_handler;
337 struct msm_smem *extradata_handle;
338 bool in_reconfig;
339 u32 reconfig_width;
340 u32 reconfig_height;
341 struct dentry *debugfs_root;
342 void *priv;
343 struct msm_vidc_debug debug;
344 struct buf_count count;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700345 struct clock_data clk_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800346 enum msm_vidc_modes flags;
347 struct msm_vidc_capability capability;
348 u32 buffer_size_limit;
349 enum buffer_mode_type buffer_mode_set[MAX_PORT_NUM];
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800350 struct v4l2_ctrl **ctrls;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800351 enum msm_vidc_pixel_depth bit_depth;
352 struct kref kref;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700353 bool in_flush;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800354 u32 pic_struct;
355 u32 colour_space;
Chinmay Sawarkarb3c6ccb2017-02-23 18:01:32 -0800356 u32 profile;
357 u32 level;
358 u32 entropy_mode;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700359 struct msm_vidc_codec_data *codec_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800360};
361
362extern struct msm_vidc_drv *vidc_driver;
363
364struct msm_vidc_ctrl_cluster {
365 struct v4l2_ctrl **cluster;
366 struct list_head list;
367};
368
369struct msm_vidc_ctrl {
370 u32 id;
371 char name[MAX_NAME_LENGTH];
372 enum v4l2_ctrl_type type;
373 s32 minimum;
374 s32 maximum;
375 s32 default_value;
376 u32 step;
377 u32 menu_skip_mask;
378 u32 flags;
379 const char * const *qmenu;
380};
381
382void handle_cmd_response(enum hal_command_response cmd, void *data);
383int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
384 enum hal_ssr_trigger_type type);
385int msm_vidc_check_session_supported(struct msm_vidc_inst *inst);
386int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst);
387void msm_vidc_queue_v4l2_event(struct msm_vidc_inst *inst, int event_type);
388
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700389struct msm_vidc_buffer {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800390 struct list_head list;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700391 struct msm_smem smem[VIDEO_MAX_PLANES];
392 struct vb2_v4l2_buffer vvb;
393 bool deferred;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800394};
395
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800396void msm_comm_handle_thermal_event(void);
397void *msm_smem_new_client(enum smem_type mtype,
398 void *platform_resources, enum session_type stype);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700399int msm_smem_alloc(struct smem_client *client,
400 size_t size, u32 align, u32 flags, enum hal_buffer buffer_type,
401 int map_kernel, struct msm_smem *smem);
402int msm_smem_free(void *clt, struct msm_smem *mem);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800403void msm_smem_delete_client(void *clt);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800404struct context_bank_info *msm_smem_get_context_bank(void *clt,
405 bool is_secure, enum hal_buffer buffer_type);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700406int msm_smem_map_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
407int msm_smem_unmap_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
408void *msm_smem_get_dma_buf(int fd);
409void msm_smem_put_dma_buf(void *dma_buf);
410void *msm_smem_get_handle(struct smem_client *client, void *dma_buf);
411void msm_smem_put_handle(struct smem_client *client, void *handle);
412int msm_smem_cache_operations(struct smem_client *client,
413 void *handle, unsigned long offset, unsigned long size,
414 enum smem_cache_ops cache_op);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800415void msm_vidc_fw_unload_handler(struct work_struct *work);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800416/*
417 * XXX: normally should be in msm_vidc.h, but that's meant for public APIs,
418 * whereas this is private
419 */
420int msm_vidc_destroy(struct msm_vidc_inst *inst);
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700421void *vidc_get_drv_data(struct device *dev);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800422#endif