1 /*
2  * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3  * Copyright (c) Imagination Technologies Limited, UK
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Zeng Li <zeng.li@intel.com>
27  *    Shengquan Yuan  <shengquan.yuan@intel.com>
28  *    Binglin Chen <binglin.chen@intel.com>
29  *
30  */
31 
32 #ifndef _LNC_CMDBUF_H_
33 #define _LNC_CMDBUF_H_
34 
35 #include "psb_drv_video.h"
36 #include "psb_surface.h"
37 #include "psb_buffer.h"
38 #ifdef ANDROID
39 #include <linux/psb_drm.h>
40 #else
41 #include "psb_drm.h"
42 #endif
43 
44 #include <stdint.h>
45 
46 
47 struct lnc_cmdbuf_s {
48     struct psb_buffer_s buf;
49     unsigned int size;
50 
51     /* Relocation records */
52     unsigned char *reloc_base;
53     struct drm_psb_reloc *reloc_idx;
54 
55     /* CMD stream data */
56     int cmd_count;
57     unsigned char *cmd_base;
58     unsigned char *cmd_start;
59     uint32_t *cmd_idx;
60     uint32_t *cmd_idx_saved_frameskip; /* idx saved for frameskip redo */
61 
62     /* all frames share one topaz param buffer which contains InParamBase
63      * AboveParam/BellowParam, and the buffer allocated when the context is created
64      */
65     struct psb_buffer_s *topaz_in_params_I;
66     struct psb_buffer_s *topaz_in_params_P;
67     struct psb_buffer_s *topaz_above_bellow_params;
68     unsigned char *topaz_in_params_I_p;
69     unsigned char *topaz_in_params_P_p;
70     unsigned char *topaz_above_bellow_params_p;
71 
72     /* Every frame has its own PIC_PARAMS, SLICE_PARAMS and HEADER mem
73      */
74 
75     /* PicParams: */
76     struct psb_buffer_s pic_params;
77     unsigned char *pic_params_p;
78 
79     /* SeqHeaderMem PicHeaderMem EOSeqHeaderMem  EOStreamHeaderMem SliceHeaderMem[MAX_SLICES_PER_PICTURE]*/
80     struct psb_buffer_s header_mem;
81     unsigned char *header_mem_p;
82 
83     /*SliceParams[MAX_SLICES_PER_PICTURE] */
84     struct psb_buffer_s slice_params;
85     unsigned char *slice_params_p;
86 
87     /* Referenced buffers */
88     psb_buffer_p *buffer_refs;
89     int buffer_refs_count;
90     int buffer_refs_allocated;
91 
92 };
93 
94 typedef struct lnc_cmdbuf_s *lnc_cmdbuf_p;
95 
96 /*
97  * Create command buffer
98  */
99 VAStatus lnc_cmdbuf_create(object_context_p obj_context,
100                            psb_driver_data_p driver_data,
101                            lnc_cmdbuf_p cmdbuf
102                           );
103 
104 /*
105  * Destroy buffer
106  */
107 void lnc_cmdbuf_destroy(lnc_cmdbuf_p cmdbuf);
108 
109 /*
110  * Reset buffer & map
111  *
112  * Returns 0 on success
113  */
114 int lnc_cmdbuf_reset(lnc_cmdbuf_p cmdbuf);
115 
116 /*
117  * Unmap buffer
118  *
119  * Returns 0 on success
120  */
121 int lnc_cmdbuf_unmap(lnc_cmdbuf_p cmdbuf);
122 
123 /*
124  * Reference an addtional buffer "buf" in the command stream
125  * Returns a reference index that can be used to refer to "buf" in
126  * relocation records, on error -1 is returned.
127  */
128 int lnc_cmdbuf_buffer_ref(lnc_cmdbuf_p cmdbuf, psb_buffer_p buf);
129 
130 /* Creates a relocation record for a DWORD in the mapped "cmdbuf" at address
131  * "addr_in_cmdbuf"
132  * The relocation is based on the device virtual address of "ref_buffer"
133  * "buf_offset" is be added to the device virtual address, and the sum is then
134  * right shifted with "align_shift".
135  * "mask" determines which bits of the target DWORD will be updated with the so
136  * constructed address. The remaining bits will be filled with bits from "background".
137  */
138 void lnc_cmdbuf_add_relocation(lnc_cmdbuf_p cmdbuf,
139                                uint32_t *addr_in_dst_buffer,/*addr of dst_buffer for the DWORD*/
140                                psb_buffer_p ref_buffer,
141                                uint32_t buf_offset,
142                                uint32_t mask,
143                                uint32_t background,
144                                uint32_t align_shift,
145                                uint32_t dst_buffer, /*Index of the list refered by cmdbuf->buffer_refs */
146                                uint32_t *start_of_dst_buffer);
147 
148 #define RELOC_CMDBUF(dest, offset, buf) lnc_cmdbuf_add_relocation(cmdbuf, (uint32_t*)(dest), buf, offset, 0XFFFFFFFF, 0, 0, 0, (uint32_t *)cmdbuf->cmd_start)
149 
150 /* do relocation in PIC_PARAMS: src/dst Y/UV base, InParamsBase, CodeBase, BellowParamsBase, AboveParamsBase */
151 #define RELOC_PIC_PARAMS(dest, offset, buf)     lnc_cmdbuf_add_relocation(cmdbuf, (uint32_t*)(dest), buf, offset, 0XFFFFFFFF, 0, 0, 1, (uint32_t *)cmdbuf->pic_params_p)
152 
153 /* do relocation in SLICE_PARAMS: reference Y/UV base,CodedData */
154 #define RELOC_SLICE_PARAMS(dest, offset, buf)   lnc_cmdbuf_add_relocation(cmdbuf, (uint32_t*)(dest), buf, offset, 0XFFFFFFFF, 0, 0, 2,(uint32_t *)cmdbuf->slice_params_p)
155 
156 /* operation number is inserted by DRM */
157 #define lnc_cmdbuf_insert_command(cmdbuf,cmdhdr,size,hint)              \
158     do { *cmdbuf->cmd_idx++ = ((cmdhdr) << 1) | ((size)<<8) | ((hint)<<16); } while(0)
159 
160 #define lnc_cmdbuf_insert_command_param(cmdbuf,param)   \
161     do { *cmdbuf->cmd_idx++ = param; } while(0)
162 
163 
164 /*
165  * Advances "obj_context" to the next cmdbuf
166  *
167  * Returns 0 on success
168  */
169 int lnc_context_get_next_cmdbuf(object_context_p obj_context);
170 
171 /*
172  * Submits the current cmdbuf
173  *
174  * Returns 0 on success
175  */
176 int lnc_context_submit_cmdbuf(object_context_p obj_context);
177 
178 /*
179  * Get a encode surface FRAMESKIP flag, and store it into frame_skip argument
180  *
181  * Returns 0 on success
182  */
183 int lnc_surface_get_frameskip(psb_driver_data_p driver_data, psb_surface_p psb_surface, int *frame_skip);
184 
185 /*
186  * Flushes the pending cmdbuf
187  *
188  * Return 0 on success
189  */
190 int lnc_context_flush_cmdbuf(object_context_p obj_context);
191 
192 #endif /* _LNC_CMDBUF_H_ */
193 
194