blob: bebb5de79474f267b2f30c6f9fae5b7fbb29c0b8 [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>
36
37#include "vidc_hfi_api.h"
38
39#define MSM_VIDC_DRV_NAME "msm_vidc_driver"
40#define MSM_VIDC_VERSION KERNEL_VERSION(0, 0, 1)
41#define MAX_DEBUGFS_NAME 50
42#define DEFAULT_TIMEOUT 3
43#define DEFAULT_HEIGHT 1088
44#define DEFAULT_WIDTH 1920
45#define MIN_SUPPORTED_WIDTH 32
46#define MIN_SUPPORTED_HEIGHT 32
47#define DEFAULT_FPS 15
Praneeth Paladugudefea4e2017-02-09 23:44:08 -080048#define MIN_NUM_OUTPUT_BUFFERS 1
49#define MIN_NUM_CAPTURE_BUFFERS 1
50#define MAX_NUM_OUTPUT_BUFFERS VIDEO_MAX_FRAME // same as VB2_MAX_FRAME
51#define MAX_NUM_CAPTURE_BUFFERS VIDEO_MAX_FRAME // same as VB2_MAX_FRAME
52
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080053
54/* Maintains the number of FTB's between each FBD over a window */
55#define DCVS_FTB_WINDOW 32
56
57#define V4L2_EVENT_VIDC_BASE 10
58
59#define SYS_MSG_START HAL_SYS_INIT_DONE
60#define SYS_MSG_END HAL_SYS_ERROR
61#define SESSION_MSG_START HAL_SESSION_EVENT_CHANGE
62#define SESSION_MSG_END HAL_SESSION_ERROR
63#define SYS_MSG_INDEX(__msg) (__msg - SYS_MSG_START)
64#define SESSION_MSG_INDEX(__msg) (__msg - SESSION_MSG_START)
65
66
67#define MAX_NAME_LENGTH 64
68
69#define EXTRADATA_IDX(__num_planes) ((__num_planes) ? (__num_planes) - 1 : 0)
70
71#define NUM_MBS_PER_SEC(__height, __width, __fps) \
72 (NUM_MBS_PER_FRAME(__height, __width) * __fps)
73
74#define NUM_MBS_PER_FRAME(__height, __width) \
75 ((ALIGN(__height, 16) / 16) * (ALIGN(__width, 16) / 16))
76
77enum vidc_ports {
78 OUTPUT_PORT,
79 CAPTURE_PORT,
80 MAX_PORT_NUM
81};
82
83enum vidc_core_state {
84 VIDC_CORE_UNINIT = 0,
85 VIDC_CORE_INIT,
86 VIDC_CORE_INIT_DONE,
87 VIDC_CORE_INVALID
88};
89
90/*
91 * Do not change the enum values unless
92 * you know what you are doing
93 */
94enum instance_state {
95 MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
96 MSM_VIDC_CORE_INIT,
97 MSM_VIDC_CORE_INIT_DONE,
98 MSM_VIDC_OPEN,
99 MSM_VIDC_OPEN_DONE,
100 MSM_VIDC_LOAD_RESOURCES,
101 MSM_VIDC_LOAD_RESOURCES_DONE,
102 MSM_VIDC_START,
103 MSM_VIDC_START_DONE,
104 MSM_VIDC_STOP,
105 MSM_VIDC_STOP_DONE,
106 MSM_VIDC_RELEASE_RESOURCES,
107 MSM_VIDC_RELEASE_RESOURCES_DONE,
108 MSM_VIDC_CLOSE,
109 MSM_VIDC_CLOSE_DONE,
110 MSM_VIDC_CORE_UNINIT,
111 MSM_VIDC_CORE_INVALID
112};
113
114struct buf_info {
115 struct list_head list;
116 struct vb2_buffer *buf;
117};
118
119struct msm_vidc_list {
120 struct list_head list;
121 struct mutex lock;
122};
123
124static inline void INIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
125{
126 mutex_init(&mlist->lock);
127 INIT_LIST_HEAD(&mlist->list);
128}
129
130static inline void DEINIT_MSM_VIDC_LIST(struct msm_vidc_list *mlist)
131{
132 mutex_destroy(&mlist->lock);
133}
134
135enum buffer_owner {
136 DRIVER,
137 FIRMWARE,
138 CLIENT,
139 MAX_OWNER
140};
141
142struct internal_buf {
143 struct list_head list;
144 enum hal_buffer buffer_type;
145 struct msm_smem *handle;
146 enum buffer_owner buffer_ownership;
147};
148
149struct msm_vidc_format {
150 char name[MAX_NAME_LENGTH];
151 u8 description[32];
152 u32 fourcc;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800153 int type;
154 u32 (*get_frame_size)(int plane, u32 height, u32 width);
Vaibhav Deshu Venkatesh8c9b6db2017-02-08 11:22:36 -0800155 bool defer_outputs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800156};
157
158struct msm_vidc_drv {
159 struct mutex lock;
160 struct list_head cores;
161 int num_cores;
162 struct dentry *debugfs_root;
163 int thermal_level;
164 u32 platform_version;
165};
166
167struct msm_video_device {
168 int type;
169 struct video_device vdev;
170};
171
172struct session_prop {
173 u32 width[MAX_PORT_NUM];
174 u32 height[MAX_PORT_NUM];
175 u32 fps;
176 u32 bitrate;
177};
178
179struct buf_queue {
180 struct vb2_queue vb2_bufq;
181 struct mutex lock;
Praneeth Paladugudefea4e2017-02-09 23:44:08 -0800182 unsigned int plane_sizes[VB2_MAX_PLANES];
183 int num_planes;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800184};
185
186enum profiling_points {
187 SYS_INIT = 0,
188 SESSION_INIT,
189 LOAD_RESOURCES,
190 FRAME_PROCESSING,
191 FW_IDLE,
192 MAX_PROFILING_POINTS,
193};
194
195struct buf_count {
196 int etb;
197 int ftb;
198 int fbd;
199 int ebd;
200};
201
202struct dcvs_stats {
203 int num_ftb[DCVS_FTB_WINDOW];
204 bool transition_turbo;
205 int ftb_index;
206 int ftb_counter;
207 bool prev_freq_lowered;
208 bool prev_freq_increased;
209 int threshold_disp_buf_high;
210 int threshold_disp_buf_low;
211 int load;
212 int load_low;
213 int load_high;
214 int min_threshold;
215 int max_threshold;
216 int etb_counter;
217 bool is_power_save_mode;
218 unsigned int extra_buffer_count;
219 u32 supported_codecs;
220};
221
222struct profile_data {
223 int start;
224 int stop;
225 int cumulative;
226 char name[64];
227 int sampling;
228 int average;
229};
230
231struct msm_vidc_debug {
232 struct profile_data pdata[MAX_PROFILING_POINTS];
233 int profile;
234 int samples;
235};
236
237enum msm_vidc_modes {
238 VIDC_SECURE = BIT(0),
239 VIDC_TURBO = BIT(1),
240 VIDC_THUMBNAIL = BIT(2),
241 VIDC_LOW_POWER = BIT(3),
242 VIDC_REALTIME = BIT(4),
243};
244
245struct msm_vidc_core {
246 struct list_head list;
247 struct mutex lock;
248 int id;
249 struct hfi_device *device;
250 struct msm_video_device vdev[MSM_VIDC_MAX_DEVICES];
251 struct v4l2_device v4l2_dev;
252 struct list_head instances;
253 struct dentry *debugfs_root;
254 enum vidc_core_state state;
255 struct completion completions[SYS_MSG_END - SYS_MSG_START + 1];
256 enum msm_vidc_hfi_type hfi_type;
257 struct msm_vidc_platform_resources resources;
258 u32 enc_codec_supported;
259 u32 dec_codec_supported;
260 u32 codec_count;
261 struct msm_vidc_capability *capabilities;
262 struct delayed_work fw_unload_work;
263 bool smmu_fault_handled;
264};
265
266struct msm_vidc_inst {
267 struct list_head list;
268 struct mutex sync_lock, lock;
269 struct msm_vidc_core *core;
270 enum session_type session_type;
271 void *session;
272 struct session_prop prop;
273 enum instance_state state;
274 struct msm_vidc_format fmts[MAX_PORT_NUM];
275 struct buf_queue bufq[MAX_PORT_NUM];
276 struct msm_vidc_list pendingq;
277 struct msm_vidc_list scratchbufs;
278 struct msm_vidc_list persistbufs;
279 struct msm_vidc_list pending_getpropq;
280 struct msm_vidc_list outputbufs;
281 struct msm_vidc_list registeredbufs;
282 struct buffer_requirements buff_req;
283 void *mem_client;
284 struct v4l2_ctrl_handler ctrl_handler;
285 struct completion completions[SESSION_MSG_END - SESSION_MSG_START + 1];
286 struct v4l2_ctrl **cluster;
287 struct v4l2_fh event_handler;
288 struct msm_smem *extradata_handle;
289 bool in_reconfig;
290 u32 reconfig_width;
291 u32 reconfig_height;
292 struct dentry *debugfs_root;
293 void *priv;
294 struct msm_vidc_debug debug;
295 struct buf_count count;
296 struct dcvs_stats dcvs;
297 enum msm_vidc_modes flags;
298 struct msm_vidc_capability capability;
299 u32 buffer_size_limit;
300 enum buffer_mode_type buffer_mode_set[MAX_PORT_NUM];
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800301 struct v4l2_ctrl **ctrls;
302 bool dcvs_mode;
303 enum msm_vidc_pixel_depth bit_depth;
304 struct kref kref;
305 unsigned long instant_bitrate;
306 u32 buffers_held_in_driver;
307 atomic_t in_flush;
308 u32 pic_struct;
309 u32 colour_space;
310 u32 operating_rate;
Chinmay Sawarkarb3c6ccb2017-02-23 18:01:32 -0800311 u32 profile;
312 u32 level;
313 u32 entropy_mode;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800314};
315
316extern struct msm_vidc_drv *vidc_driver;
317
318struct msm_vidc_ctrl_cluster {
319 struct v4l2_ctrl **cluster;
320 struct list_head list;
321};
322
323struct msm_vidc_ctrl {
324 u32 id;
325 char name[MAX_NAME_LENGTH];
326 enum v4l2_ctrl_type type;
327 s32 minimum;
328 s32 maximum;
329 s32 default_value;
330 u32 step;
331 u32 menu_skip_mask;
332 u32 flags;
333 const char * const *qmenu;
334};
335
336void handle_cmd_response(enum hal_command_response cmd, void *data);
337int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
338 enum hal_ssr_trigger_type type);
339int msm_vidc_check_session_supported(struct msm_vidc_inst *inst);
340int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst);
341void msm_vidc_queue_v4l2_event(struct msm_vidc_inst *inst, int event_type);
342
343struct buffer_info {
344 struct list_head list;
345 int type;
346 int num_planes;
347 int fd[VIDEO_MAX_PLANES];
348 int buff_off[VIDEO_MAX_PLANES];
349 int size[VIDEO_MAX_PLANES];
350 unsigned long uvaddr[VIDEO_MAX_PLANES];
351 ion_phys_addr_t device_addr[VIDEO_MAX_PLANES];
352 struct msm_smem *handle[VIDEO_MAX_PLANES];
353 enum v4l2_memory memory;
354 u32 v4l2_index;
355 bool pending_deletion;
356 atomic_t ref_count;
357 bool dequeued;
358 bool inactive;
359 bool mapped[VIDEO_MAX_PLANES];
360 int same_fd_ref[VIDEO_MAX_PLANES];
361 struct timeval timestamp;
362};
363
364struct buffer_info *device_to_uvaddr(struct msm_vidc_list *buf_list,
365 ion_phys_addr_t device_addr);
366int buf_ref_get(struct msm_vidc_inst *inst, struct buffer_info *binfo);
367int buf_ref_put(struct msm_vidc_inst *inst, struct buffer_info *binfo);
368int output_buffer_cache_invalidate(struct msm_vidc_inst *inst,
369 struct buffer_info *binfo);
370int qbuf_dynamic_buf(struct msm_vidc_inst *inst,
371 struct buffer_info *binfo);
372int unmap_and_deregister_buf(struct msm_vidc_inst *inst,
373 struct buffer_info *binfo);
374
375void msm_comm_handle_thermal_event(void);
376void *msm_smem_new_client(enum smem_type mtype,
377 void *platform_resources, enum session_type stype);
378struct msm_smem *msm_smem_alloc(void *clt, size_t size, u32 align, u32 flags,
379 enum hal_buffer buffer_type, int map_kernel);
380void msm_smem_free(void *clt, struct msm_smem *mem);
381void msm_smem_delete_client(void *clt);
382int msm_smem_cache_operations(void *clt, struct msm_smem *mem,
383 enum smem_cache_ops);
384struct msm_smem *msm_smem_user_to_kernel(void *clt, int fd, u32 offset,
385 enum hal_buffer buffer_type);
386struct context_bank_info *msm_smem_get_context_bank(void *clt,
387 bool is_secure, enum hal_buffer buffer_type);
388void msm_vidc_fw_unload_handler(struct work_struct *work);
389bool msm_smem_compare_buffers(void *clt, int fd, void *priv);
390/*
391 * XXX: normally should be in msm_vidc.h, but that's meant for public APIs,
392 * whereas this is private
393 */
394int msm_vidc_destroy(struct msm_vidc_inst *inst);
395#endif