blob: 0b6331c3f36f56b71ccf7d145821522117fc32f7 [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
Surajit Podder26e12f8a2017-10-13 14:11:42 +053048#define MIN_NUM_OUTPUT_BUFFERS_VP9 6
Praneeth Paladugudefea4e2017-02-09 23:44:08 -080049#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
Karthikeyan Periasamy4b5e7332017-07-28 12:07:42 -070053#define MAX_SUPPORTED_INSTANCES 16
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080054
55/* Maintains the number of FTB's between each FBD over a window */
Praneeth Paladugue5fd0872017-04-19 11:24:28 -070056#define DCVS_FTB_WINDOW 16
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080057
58#define V4L2_EVENT_VIDC_BASE 10
59
60#define SYS_MSG_START HAL_SYS_INIT_DONE
61#define SYS_MSG_END HAL_SYS_ERROR
62#define SESSION_MSG_START HAL_SESSION_EVENT_CHANGE
63#define SESSION_MSG_END HAL_SESSION_ERROR
64#define SYS_MSG_INDEX(__msg) (__msg - SYS_MSG_START)
65#define SESSION_MSG_INDEX(__msg) (__msg - SESSION_MSG_START)
66
67
68#define MAX_NAME_LENGTH 64
69
70#define EXTRADATA_IDX(__num_planes) ((__num_planes) ? (__num_planes) - 1 : 0)
71
72#define NUM_MBS_PER_SEC(__height, __width, __fps) \
73 (NUM_MBS_PER_FRAME(__height, __width) * __fps)
74
75#define NUM_MBS_PER_FRAME(__height, __width) \
76 ((ALIGN(__height, 16) / 16) * (ALIGN(__width, 16) / 16))
77
78enum vidc_ports {
79 OUTPUT_PORT,
80 CAPTURE_PORT,
81 MAX_PORT_NUM
82};
83
84enum vidc_core_state {
85 VIDC_CORE_UNINIT = 0,
86 VIDC_CORE_INIT,
87 VIDC_CORE_INIT_DONE,
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -080088};
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
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700142struct vidc_freq_data {
143 struct list_head list;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700144 u32 device_addr;
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700145 unsigned long freq;
146};
147
Praneeth Paladugu7722b4e2017-07-07 11:01:56 -0700148struct vidc_input_cr_data {
149 struct list_head list;
150 u32 index;
151 u32 input_cr;
152};
153
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700154struct recon_buf {
155 struct list_head list;
156 u32 buffer_index;
157 u32 CR;
158 u32 CF;
159};
160
Praneeth Paladuguf9ef8172017-07-18 22:55:59 -0700161struct eos_buf {
162 struct list_head list;
163 struct msm_smem smem;
164};
165
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800166struct internal_buf {
167 struct list_head list;
168 enum hal_buffer buffer_type;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700169 struct msm_smem smem;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800170 enum buffer_owner buffer_ownership;
171};
172
Chinmay Sawarkarddd1d972017-08-15 10:10:06 -0700173struct msm_vidc_csc_coeff {
174 u32 *vpe_csc_custom_matrix_coeff;
175 u32 *vpe_csc_custom_bias_coeff;
176 u32 *vpe_csc_custom_limit_coeff;
177};
178
Qiwei Liu551a22222017-08-23 15:28:29 +0800179struct msm_vidc_buf_data {
180 struct list_head list;
181 u32 index;
182 u32 mark_data;
183 u32 mark_target;
184};
185
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700186struct msm_vidc_common_data {
187 char key[128];
188 int value;
189};
190
191struct msm_vidc_codec_data {
192 u32 fourcc;
193 enum session_type session_type;
194 int vpp_cycles;
195 int vsp_cycles;
196 int low_power_cycles;
197};
198
Manikanta Kanamarlapudi0ff858d2017-08-28 17:35:11 +0530199enum efuse_purpose {
200 SKU_VERSION = 0,
201};
202
203enum sku_version {
204 SKU_VERSION_0 = 0,
205 SKU_VERSION_1,
206 SKU_VERSION_2,
207};
208
209struct msm_vidc_efuse_data {
210 u32 start_address;
211 u32 size;
212 u32 mask;
213 u32 shift;
214 enum efuse_purpose purpose;
215};
216
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700217struct msm_vidc_platform_data {
218 struct msm_vidc_common_data *common_data;
219 unsigned int common_data_length;
220 struct msm_vidc_codec_data *codec_data;
221 unsigned int codec_data_length;
Chinmay Sawarkarddd1d972017-08-15 10:10:06 -0700222 struct msm_vidc_csc_coeff csc_data;
Manikanta Kanamarlapudi0ff858d2017-08-28 17:35:11 +0530223 struct msm_vidc_efuse_data *efuse_data;
224 unsigned int efuse_data_length;
225 unsigned int sku_version;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700226};
227
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800228struct msm_vidc_format {
229 char name[MAX_NAME_LENGTH];
230 u8 description[32];
231 u32 fourcc;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800232 int type;
233 u32 (*get_frame_size)(int plane, u32 height, u32 width);
Vaibhav Deshu Venkatesh8c9b6db2017-02-08 11:22:36 -0800234 bool defer_outputs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800235};
236
237struct msm_vidc_drv {
238 struct mutex lock;
239 struct list_head cores;
240 int num_cores;
241 struct dentry *debugfs_root;
242 int thermal_level;
Manikanta Kanamarlapudi0ff858d2017-08-28 17:35:11 +0530243 u32 sku_version;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800244};
245
246struct msm_video_device {
247 int type;
248 struct video_device vdev;
249};
250
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700251struct session_crop {
252 u32 left;
253 u32 top;
254 u32 width;
255 u32 height;
256};
257
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800258struct session_prop {
259 u32 width[MAX_PORT_NUM];
260 u32 height[MAX_PORT_NUM];
Praneeth Paladugu520e9b22017-05-31 13:25:18 -0700261 struct session_crop crop_info;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800262 u32 fps;
263 u32 bitrate;
264};
265
266struct buf_queue {
267 struct vb2_queue vb2_bufq;
268 struct mutex lock;
Praneeth Paladugudefea4e2017-02-09 23:44:08 -0800269 unsigned int plane_sizes[VB2_MAX_PLANES];
270 int num_planes;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800271};
272
273enum profiling_points {
274 SYS_INIT = 0,
275 SESSION_INIT,
276 LOAD_RESOURCES,
277 FRAME_PROCESSING,
278 FW_IDLE,
279 MAX_PROFILING_POINTS,
280};
281
282struct buf_count {
283 int etb;
284 int ftb;
285 int fbd;
286 int ebd;
287};
288
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700289struct clock_data {
Praneeth Paladugu75cf18e2016-12-08 16:12:11 -0800290 int buffer_counter;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800291 int load;
292 int load_low;
Praneeth Paladugu04e77722017-06-21 11:38:31 -0700293 int load_norm;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800294 int load_high;
295 int min_threshold;
296 int max_threshold;
Praneeth Paladugu1f9d1d92017-04-11 10:36:16 -0700297 unsigned int extra_capture_buffer_count;
298 unsigned int extra_output_buffer_count;
Praneeth Paladugu75cf18e2016-12-08 16:12:11 -0800299 enum hal_buffer buffer_type;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700300 bool dcvs_mode;
301 unsigned long bitrate;
302 unsigned long min_freq;
303 unsigned long curr_freq;
304 u32 operating_rate;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700305 struct msm_vidc_codec_data *entry;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700306 u32 core_id;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700307 u32 dpb_fourcc;
308 u32 opb_fourcc;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700309 enum hal_work_mode work_mode;
310 bool low_latency_mode;
Saurabh Kothawade501be792017-08-29 11:41:38 -0700311 bool turbo_mode;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800312};
313
314struct profile_data {
315 int start;
316 int stop;
317 int cumulative;
318 char name[64];
319 int sampling;
320 int average;
321};
322
323struct msm_vidc_debug {
324 struct profile_data pdata[MAX_PROFILING_POINTS];
325 int profile;
326 int samples;
327};
328
329enum msm_vidc_modes {
330 VIDC_SECURE = BIT(0),
331 VIDC_TURBO = BIT(1),
332 VIDC_THUMBNAIL = BIT(2),
333 VIDC_LOW_POWER = BIT(3),
334 VIDC_REALTIME = BIT(4),
335};
336
337struct msm_vidc_core {
338 struct list_head list;
339 struct mutex lock;
340 int id;
341 struct hfi_device *device;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700342 struct msm_vidc_platform_data *platform_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800343 struct msm_video_device vdev[MSM_VIDC_MAX_DEVICES];
344 struct v4l2_device v4l2_dev;
345 struct list_head instances;
346 struct dentry *debugfs_root;
347 enum vidc_core_state state;
348 struct completion completions[SYS_MSG_END - SYS_MSG_START + 1];
349 enum msm_vidc_hfi_type hfi_type;
350 struct msm_vidc_platform_resources resources;
351 u32 enc_codec_supported;
352 u32 dec_codec_supported;
353 u32 codec_count;
354 struct msm_vidc_capability *capabilities;
355 struct delayed_work fw_unload_work;
356 bool smmu_fault_handled;
Maheshwar Ajja8382bec2017-08-11 13:41:35 -0700357 bool trigger_ssr;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700358 unsigned long min_freq;
359 unsigned long curr_freq;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700360 struct vidc_bus_vote_data *vote_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800361};
362
363struct msm_vidc_inst {
364 struct list_head list;
365 struct mutex sync_lock, lock;
366 struct msm_vidc_core *core;
367 enum session_type session_type;
368 void *session;
369 struct session_prop prop;
370 enum instance_state state;
371 struct msm_vidc_format fmts[MAX_PORT_NUM];
372 struct buf_queue bufq[MAX_PORT_NUM];
Praneeth Paladugub71968b2015-08-19 20:47:57 -0700373 struct msm_vidc_list freqs;
Praneeth Paladugu7722b4e2017-07-07 11:01:56 -0700374 struct msm_vidc_list input_crs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800375 struct msm_vidc_list scratchbufs;
376 struct msm_vidc_list persistbufs;
377 struct msm_vidc_list pending_getpropq;
378 struct msm_vidc_list outputbufs;
Praneeth Paladugu319e7922017-03-16 11:09:06 -0700379 struct msm_vidc_list reconbufs;
Praneeth Paladuguf9ef8172017-07-18 22:55:59 -0700380 struct msm_vidc_list eosbufs;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800381 struct msm_vidc_list registeredbufs;
Qiwei Liu551a22222017-08-23 15:28:29 +0800382 struct msm_vidc_list etb_data;
383 struct msm_vidc_list fbd_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800384 struct buffer_requirements buff_req;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700385 struct smem_client *mem_client;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800386 struct v4l2_ctrl_handler ctrl_handler;
387 struct completion completions[SESSION_MSG_END - SESSION_MSG_START + 1];
388 struct v4l2_ctrl **cluster;
389 struct v4l2_fh event_handler;
390 struct msm_smem *extradata_handle;
391 bool in_reconfig;
392 u32 reconfig_width;
393 u32 reconfig_height;
394 struct dentry *debugfs_root;
395 void *priv;
396 struct msm_vidc_debug debug;
397 struct buf_count count;
Praneeth Paladugue5fd0872017-04-19 11:24:28 -0700398 struct clock_data clk_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800399 enum msm_vidc_modes flags;
400 struct msm_vidc_capability capability;
401 u32 buffer_size_limit;
402 enum buffer_mode_type buffer_mode_set[MAX_PORT_NUM];
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800403 struct v4l2_ctrl **ctrls;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800404 enum msm_vidc_pixel_depth bit_depth;
405 struct kref kref;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700406 bool in_flush;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800407 u32 pic_struct;
408 u32 colour_space;
Chinmay Sawarkarb3c6ccb2017-02-23 18:01:32 -0800409 u32 profile;
410 u32 level;
411 u32 entropy_mode;
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700412 struct msm_vidc_codec_data *codec_data;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800413};
414
415extern struct msm_vidc_drv *vidc_driver;
416
417struct msm_vidc_ctrl_cluster {
418 struct v4l2_ctrl **cluster;
419 struct list_head list;
420};
421
422struct msm_vidc_ctrl {
423 u32 id;
424 char name[MAX_NAME_LENGTH];
425 enum v4l2_ctrl_type type;
Umesh Pandey3224e802017-10-12 20:18:58 -0700426 s64 minimum;
427 s64 maximum;
428 s64 default_value;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800429 u32 step;
430 u32 menu_skip_mask;
431 u32 flags;
432 const char * const *qmenu;
433};
434
435void handle_cmd_response(enum hal_command_response cmd, void *data);
436int msm_vidc_trigger_ssr(struct msm_vidc_core *core,
437 enum hal_ssr_trigger_type type);
Maheshwar Ajja9ff81a22017-08-05 13:25:55 -0700438int msm_vidc_noc_error_info(struct msm_vidc_core *core);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800439int msm_vidc_check_session_supported(struct msm_vidc_inst *inst);
440int msm_vidc_check_scaling_supported(struct msm_vidc_inst *inst);
441void msm_vidc_queue_v4l2_event(struct msm_vidc_inst *inst, int event_type);
442
Maheshwar Ajjae5765bd2017-08-08 11:59:38 -0700443enum msm_vidc_flags {
444 MSM_VIDC_FLAG_DEFERRED = BIT(0),
445 MSM_VIDC_FLAG_RBR_PENDING = BIT(1),
446};
447
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700448struct msm_vidc_buffer {
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800449 struct list_head list;
Maheshwar Ajjacca04052017-07-12 17:59:45 -0700450 struct kref kref;
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700451 struct msm_smem smem[VIDEO_MAX_PLANES];
452 struct vb2_v4l2_buffer vvb;
Maheshwar Ajjae5765bd2017-08-08 11:59:38 -0700453 enum msm_vidc_flags flags;
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800454};
455
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800456void msm_comm_handle_thermal_event(void);
457void *msm_smem_new_client(enum smem_type mtype,
458 void *platform_resources, enum session_type stype);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700459int msm_smem_alloc(struct smem_client *client,
460 size_t size, u32 align, u32 flags, enum hal_buffer buffer_type,
461 int map_kernel, struct msm_smem *smem);
462int msm_smem_free(void *clt, struct msm_smem *mem);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800463void msm_smem_delete_client(void *clt);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800464struct context_bank_info *msm_smem_get_context_bank(void *clt,
465 bool is_secure, enum hal_buffer buffer_type);
Maheshwar Ajjac6407c02017-06-09 18:53:20 -0700466int msm_smem_map_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
467int msm_smem_unmap_dma_buf(struct msm_vidc_inst *inst, struct msm_smem *smem);
468void *msm_smem_get_dma_buf(int fd);
469void msm_smem_put_dma_buf(void *dma_buf);
470void *msm_smem_get_handle(struct smem_client *client, void *dma_buf);
471void msm_smem_put_handle(struct smem_client *client, void *handle);
472int msm_smem_cache_operations(struct smem_client *client,
473 void *handle, unsigned long offset, unsigned long size,
474 enum smem_cache_ops cache_op);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800475void msm_vidc_fw_unload_handler(struct work_struct *work);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800476/*
477 * XXX: normally should be in msm_vidc.h, but that's meant for public APIs,
478 * whereas this is private
479 */
480int msm_vidc_destroy(struct msm_vidc_inst *inst);
Praneeth Paladugud5bf7bc2017-05-29 23:41:04 -0700481void *vidc_get_drv_data(struct device *dev);
Praneeth Paladugu6e6fbdb2017-01-16 15:43:01 -0800482#endif