ilo: fix fence reference counting
[mesa.git] / src / gallium / drivers / nouveau / nouveau_vp3_video.h
1 /*
2 * Copyright 2011-2013 Maarten Lankhorst
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <nouveau.h>
24
25 #include "pipe/p_defines.h"
26 #include "vl/vl_video_buffer.h"
27 #include "util/u_video.h"
28
29 struct nouveau_vp3_video_buffer {
30 struct pipe_video_buffer base;
31 unsigned num_planes, valid_ref;
32 struct pipe_resource *resources[VL_NUM_COMPONENTS];
33 struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
34 struct pipe_sampler_view *sampler_view_components[VL_NUM_COMPONENTS];
35 struct pipe_surface *surfaces[VL_NUM_COMPONENTS * 2];
36 };
37
38 #define SLICE_SIZE 0x200
39 #define VP_OFFSET 0x200
40 #define COMM_OFFSET 0x500
41
42 #define NOUVEAU_VP3_DEBUG_FENCE 0
43
44 #if NOUVEAU_VP3_DEBUG_FENCE
45 # define NOUVEAU_VP3_VIDEO_QDEPTH 1
46 #else
47 # define NOUVEAU_VP3_VIDEO_QDEPTH 2
48 #endif
49
50 #define SUBC_BSP(m) dec->bsp_idx, (m)
51 #define SUBC_VP(m) dec->vp_idx, (m)
52 #define SUBC_PPP(m) dec->ppp_idx, (m)
53
54 union pipe_desc {
55 struct pipe_picture_desc *base;
56 struct pipe_mpeg12_picture_desc *mpeg12;
57 struct pipe_mpeg4_picture_desc *mpeg4;
58 struct pipe_vc1_picture_desc *vc1;
59 struct pipe_h264_picture_desc *h264;
60 };
61
62 struct nouveau_vp3_decoder {
63 struct pipe_video_codec base;
64 struct nouveau_client *client;
65 struct nouveau_object *channel[3], *bsp, *vp, *ppp;
66 struct nouveau_pushbuf *pushbuf[3];
67
68 #if NOUVEAU_VP3_DEBUG_FENCE
69 /* dump fence and comm, as needed.. */
70 unsigned *fence_map;
71 struct comm *comm;
72
73 struct nouveau_bo *fence_bo;
74 #endif
75
76 struct nouveau_bo *fw_bo, *bitplane_bo;
77
78 // array size max_references + 2, contains unpostprocessed images
79 // added at the end of ref_bo is a tmp array
80 // tmp is an array for h264, with each member being used for a ref frame or current
81 // target.. size = (((mb(w)*((mb(h)+1)&~1))+3)>>2)<<8 * (max_references+1)
82 // for other codecs, it simply seems that size = w*h is enough
83 // unsure what it's supposed to contain..
84 struct nouveau_bo *ref_bo;
85
86 struct nouveau_bo *inter_bo[2];
87
88 struct nouveau_bo *bsp_bo[NOUVEAU_VP3_VIDEO_QDEPTH];
89
90 // bo's used by each cycle:
91
92 // bsp_bo: contains raw bitstream data and parameters for BSP and VP.
93 // inter_bo: contains data shared between BSP and VP
94 // ref_bo: reference image data, used by PPP and VP
95 // bitplane_bo: contain bitplane data (similar to ref_bo), used by BSP only
96 // fw_bo: used by VP only.
97
98 // Needed amount of copies in optimal case:
99 // 2 copies of inter_bo, VP would process the last inter_bo, while BSP is
100 // writing out a new set.
101 // NOUVEAU_VP3_VIDEO_QDEPTH copies of bsp_bo. We don't want to block the
102 // pipeline ever, and give shaders a chance to run as well.
103
104 struct {
105 struct nouveau_vp3_video_buffer *vidbuf;
106 unsigned last_used;
107 unsigned field_pic_flag : 1;
108 unsigned decoded_top : 1;
109 unsigned decoded_bottom : 1;
110 unsigned decoded_first : 1;
111 } refs[17];
112 unsigned fence_seq, fw_sizes, last_frame_num, tmp_stride, ref_stride;
113
114 unsigned bsp_idx, vp_idx, ppp_idx;
115 };
116
117 struct comm {
118 uint32_t bsp_cur_index; // 000
119 uint32_t byte_ofs; // 004
120 uint32_t status[0x10]; // 008
121 uint32_t pos[0x10]; // 048
122 uint8_t pad[0x100 - 0x88]; // 0a0 bool comm_encrypted
123
124 uint32_t pvp_cur_index; // 100
125 uint32_t acked_byte_ofs; // 104
126 uint32_t status_vp[0x10]; // 108
127 uint16_t mb_y[0x10]; //148
128 uint32_t pvp_stage; // 168 0xeeXX
129 uint16_t parse_endpos_index; // 16c
130 uint16_t irq_index; // 16e
131 uint8_t irq_470[0x10]; // 170
132 uint32_t irq_pos[0x10]; // 180
133 uint32_t parse_endpos[0x10]; // 1c0
134 };
135
136 static INLINE uint32_t nouveau_vp3_video_align(uint32_t h)
137 {
138 return ((h+0x3f)&~0x3f);
139 };
140
141 static INLINE uint32_t mb(uint32_t coord)
142 {
143 return (coord + 0xf)>>4;
144 }
145
146 static INLINE uint32_t mb_half(uint32_t coord)
147 {
148 return (coord + 0x1f)>>5;
149 }
150
151 static INLINE uint64_t
152 nouveau_vp3_video_addr(struct nouveau_vp3_decoder *dec, struct nouveau_vp3_video_buffer *target)
153 {
154 uint64_t ret;
155 if (target)
156 ret = dec->ref_stride * target->valid_ref;
157 else
158 ret = dec->ref_stride * (dec->base.max_references+1);
159 return dec->ref_bo->offset + ret;
160 }
161
162 static INLINE void
163 nouveau_vp3_ycbcr_offsets(struct nouveau_vp3_decoder *dec, uint32_t *y2,
164 uint32_t *cbcr, uint32_t *cbcr2)
165 {
166 uint32_t w = mb(dec->base.width), size;
167 *y2 = mb_half(dec->base.height)*w;
168 *cbcr = *y2 * 2;
169 *cbcr2 = *cbcr + w * (nouveau_vp3_video_align(dec->base.height)>>6);
170
171 /* The check here should never fail because it means a bug
172 * in the code rather than a bug in hardware..
173 */
174 size = (2 * (*cbcr2 - *cbcr) + *cbcr) << 8;
175 if (size > dec->ref_stride) {
176 debug_printf("Overshot ref_stride (%u) with size %u and ofs (%u,%u,%u)\n",
177 dec->ref_stride, size, *y2<<8, *cbcr<<8, *cbcr2<<8);
178 *y2 = *cbcr = *cbcr2 = 0;
179 assert(size <= dec->ref_stride);
180 }
181 }
182
183 static INLINE void
184 nouveau_vp3_inter_sizes(struct nouveau_vp3_decoder *dec, uint32_t slice_count,
185 uint32_t *slice_size, uint32_t *bucket_size,
186 uint32_t *ring_size)
187 {
188 *slice_size = (SLICE_SIZE * slice_count)>>8;
189 if (u_reduce_video_profile(dec->base.profile) == PIPE_VIDEO_FORMAT_MPEG12)
190 *bucket_size = 0;
191 else
192 *bucket_size = mb(dec->base.width) * 3;
193 *ring_size = (dec->inter_bo[0]->size >> 8) - *bucket_size - *slice_size;
194 }
195
196 struct pipe_video_buffer *
197 nouveau_vp3_video_buffer_create(struct pipe_context *pipe,
198 const struct pipe_video_buffer *templat,
199 int flags);
200
201 void
202 nouveau_vp3_decoder_init_common(struct pipe_video_codec *decoder);
203
204 int
205 nouveau_vp3_load_firmware(struct nouveau_vp3_decoder *dec,
206 enum pipe_video_profile profile,
207 unsigned chipset);
208
209 uint32_t
210 nouveau_vp3_bsp(struct nouveau_vp3_decoder *dec, union pipe_desc desc,
211 struct nouveau_vp3_video_buffer *target,
212 unsigned comm_seq, unsigned num_buffers,
213 const void *const *data, const unsigned *num_bytes);
214
215 void
216 nouveau_vp3_vp_caps(struct nouveau_vp3_decoder *dec, union pipe_desc desc,
217 struct nouveau_vp3_video_buffer *target, unsigned comm_seq,
218 unsigned *caps, unsigned *is_ref,
219 struct nouveau_vp3_video_buffer *refs[16]);
220
221 int
222 nouveau_vp3_screen_get_video_param(struct pipe_screen *pscreen,
223 enum pipe_video_profile profile,
224 enum pipe_video_entrypoint entrypoint,
225 enum pipe_video_cap param);
226
227 boolean
228 nouveau_vp3_screen_video_supported(struct pipe_screen *screen,
229 enum pipe_format format,
230 enum pipe_video_profile profile,
231 enum pipe_video_entrypoint entrypoint);