1 /*
2  * Copyright ©  2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Midhunchandra Kodiyath <midhunchandra.kodiyath@intel.com>
26  *
27  */
28 
29 #include <stdio.h>
30 #include <va/va_drmcommon.h>
31 #include "media_drv_surface.h"
32 #include "media_drv_driver.h"
33 #include "media_drv_gpe_utils.h"
34 #include "media_drv_util.h"
35 #include "media_drv_hw.h"
36 #include "media_drv_hw_g9.h"
37 #include "media_drv_hw_g8.h"
38 #include "media_drv_hw_g75.h"
39 #include "media_drv_hw_g7.h"
40 #include "object_heap.h"
41 
42 uint32_t g_intel_debug_option_flags = 0;
43 
44 BOOL
media_drv_intel_bufmgr_init(MEDIA_DRV_CONTEXT * drv_ctx)45 media_drv_intel_bufmgr_init (MEDIA_DRV_CONTEXT * drv_ctx)
46 {
47   BOOL status = SUCCESS;
48   drv_ctx->drv_data.bufmgr =
49     intel_bufmgr_gem_init (drv_ctx->drv_data.fd, BATCH_BUF_SIZE);
50   if (drv_ctx->drv_data.bufmgr == NULL)
51     {
52       //MEDIA_DRV_ASSERT (drv_ctx->bufmgr);
53       return FAILED;
54     }
55   intel_bufmgr_gem_enable_reuse (drv_ctx->drv_data.bufmgr);
56   return status;
57 }
58 
59 VOID
media_drv_bufmgr_destroy(MEDIA_DRV_CONTEXT * drv_ctx)60 media_drv_bufmgr_destroy (MEDIA_DRV_CONTEXT * drv_ctx)
61 {
62   drm_intel_bufmgr_destroy (drv_ctx->drv_data.bufmgr);
63 }
64 
65 static VOID
media_driver_get_revid(INT * value)66 media_driver_get_revid (INT * value)
67 {
68 #define PCI_REVID       8
69   FILE *fp;
70   CHAR config_data[16];
71 
72   fp = fopen ("/sys/devices/pci0000:00/0000:00:02.0/config", "r");
73 
74   if (fp)
75     {
76       if (fread (config_data, 1, 16, fp))
77 	*value = config_data[PCI_REVID];
78       else
79 	*value = 2;		/* assume it is at least  B-steping */
80       fclose (fp);
81     }
82   else
83     {
84       *value = 2;		/* assume it is at least  B-steping */
85     }
86 
87   return;
88 }
89 
90 BOOL
media_drv_get_param(MEDIA_DRV_CONTEXT * drv_ctx,INT param,INT * value)91 media_drv_get_param (MEDIA_DRV_CONTEXT * drv_ctx, INT param, INT * value)
92 {
93   struct drm_i915_getparam gp;
94 
95   gp.param = param;
96   gp.value = value;
97   return drmCommandWriteRead (drv_ctx->drv_data.fd, DRM_I915_GETPARAM, &gp,
98 			      sizeof (gp)) == 0;
99 
100 }
101 
102 BOOL
media_driver_init(VADriverContextP ctx)103 media_driver_init (VADriverContextP ctx)
104 {
105   MEDIA_DRV_CONTEXT *drv_ctx = NULL;
106   MEDIA_DRV_ASSERT (ctx);
107 
108   struct drm_state *const drm_state = (struct drm_state *) ctx->drm_state;
109   INT has_exec2 = 0, has_bsd = 0, has_blt = 0, has_vebox = 0;
110   drv_ctx = ctx->pDriverData;
111   MEDIA_DRV_ASSERT (drm_state);
112   MEDIA_DRV_ASSERT (VA_CHECK_DRM_AUTH_TYPE (ctx, VA_DRM_AUTH_DRI1) ||
113 		    VA_CHECK_DRM_AUTH_TYPE (ctx, VA_DRM_AUTH_DRI2) ||
114 		    VA_CHECK_DRM_AUTH_TYPE (ctx, VA_DRM_AUTH_CUSTOM));
115 
116   {
117     char *env_str;
118     g_intel_debug_option_flags = 0;
119     if ((env_str = getenv("VA_INTEL_DEBUG"))) {
120       char *debug_ptr = NULL;
121       debug_ptr = strstr(env_str, "0x");
122       if (debug_ptr) {
123         g_intel_debug_option_flags = strtoul(debug_ptr, NULL, 16);
124       } else {
125         g_intel_debug_option_flags = atoi(env_str);
126       }
127     }
128   }
129 
130   drv_ctx->drv_data.fd = drm_state->fd;
131   drv_ctx->drv_data.dri2_enabled =
132     (VA_CHECK_DRM_AUTH_TYPE (ctx, VA_DRM_AUTH_DRI2)
133      || VA_CHECK_DRM_AUTH_TYPE (ctx, VA_DRM_AUTH_CUSTOM));
134 
135   if (!drv_ctx->drv_data.dri2_enabled)
136     {
137       return FALSE;
138     }
139 
140   drv_ctx->locked = 0;
141   media_drv_mutex_init (&drv_ctx->ctxmutex);
142 
143   media_drv_get_param (drv_ctx, I915_PARAM_CHIPSET_ID,
144 		       &drv_ctx->drv_data.device_id);
145   if (media_drv_get_param (drv_ctx, I915_PARAM_HAS_EXECBUF2, &has_exec2))
146     drv_ctx->drv_data.exec2_flag = has_exec2;
147   if (media_drv_get_param (drv_ctx, I915_PARAM_HAS_BSD, &has_bsd))
148     drv_ctx->drv_data.bsd_flag = has_bsd;
149   if (media_drv_get_param (drv_ctx, I915_PARAM_HAS_BLT, &has_blt))
150     drv_ctx->drv_data.blt_flag = has_blt;
151   if (media_drv_get_param (drv_ctx, I915_PARAM_HAS_VEBOX, &has_vebox))
152     drv_ctx->drv_data.vebox_flag = ! !has_vebox;
153 
154   media_driver_get_revid (&drv_ctx->drv_data.revision);
155   media_drv_intel_bufmgr_init (drv_ctx);
156   return TRUE;
157 }
158 
159 VOID
media_driver_terminate(VADriverContextP ctx)160 media_driver_terminate (VADriverContextP ctx)
161 {
162   MEDIA_DRV_CONTEXT *drv_ctx = NULL;
163   MEDIA_DRV_ASSERT (ctx);
164   drv_ctx = ctx->pDriverData;
165   media_drv_bufmgr_destroy (drv_ctx);
166   media_drv_mutex_destroy (&drv_ctx->ctxmutex);
167 }
168 
169 
170 BOOL
media_driver_data_init(VADriverContextP ctx)171 media_driver_data_init (VADriverContextP ctx)
172 {
173   MEDIA_DRV_CONTEXT *drv_ctx = NULL;
174   MEDIA_DRV_ASSERT (ctx);
175   drv_ctx = ctx->pDriverData;
176   if (IS_GEN75 (drv_ctx->drv_data.device_id))
177     drv_ctx->codec_info = &gen75_hw_codec_info;
178   else if (IS_GEN7 (drv_ctx->drv_data.device_id))
179     drv_ctx->codec_info = &gen7_hw_codec_info;
180   else if (IS_GEN8(drv_ctx->drv_data.device_id))
181     drv_ctx->codec_info = &gen8_hw_codec_info;
182   else if (IS_CHERRYVIEW(drv_ctx->drv_data.device_id))
183     drv_ctx->codec_info = &chv_hw_codec_info;
184   else if (IS_GEN9(drv_ctx->drv_data.device_id))
185     drv_ctx->codec_info = &gen9_hw_codec_info;
186   else
187     return false;
188 
189   if (object_heap_init (&drv_ctx->config_heap,
190 			sizeof (struct object_config), CONFIG_ID_OFFSET))
191     goto err_config_heap;
192   if (object_heap_init (&drv_ctx->context_heap,
193 			sizeof (struct object_context), CONTEXT_ID_OFFSET))
194     goto err_context_heap;
195 
196   if (object_heap_init (&drv_ctx->surface_heap,
197 			sizeof (struct object_surface), SURFACE_ID_OFFSET))
198     goto err_surface_heap;
199   if (object_heap_init (&drv_ctx->buffer_heap,
200 			sizeof (struct object_buffer), BUFFER_ID_OFFSET))
201     goto err_buffer_heap;
202   if (object_heap_init (&drv_ctx->image_heap,
203 			sizeof (struct object_image), IMAGE_ID_OFFSET))
204     goto err_image_heap;
205 
206   if (object_heap_init (&drv_ctx->subpic_heap,
207                         sizeof (struct object_subpic), IMAGE_ID_OFFSET))
208     goto err_subpic_heap;
209 
210   drv_ctx->batch =
211     media_batchbuffer_new (&drv_ctx->drv_data, I915_EXEC_RENDER, 0);
212   drv_ctx->pp_batch =
213     media_batchbuffer_new (&drv_ctx->drv_data, I915_EXEC_RENDER, 0);
214   drv_ctx->render_batch =
215     media_batchbuffer_new (&drv_ctx->drv_data, I915_EXEC_RENDER, 0);
216   media_drv_mutex_init (&drv_ctx->render_mutex);
217   media_drv_mutex_init (&drv_ctx->pp_mutex);
218 
219   return true;
220 
221 err_subpic_heap:
222   object_heap_destroy(&drv_ctx->subpic_heap);
223 
224 err_image_heap:
225   object_heap_destroy (&drv_ctx->buffer_heap);
226 err_buffer_heap:
227   object_heap_destroy (&drv_ctx->surface_heap);
228 err_surface_heap:
229   object_heap_destroy (&drv_ctx->context_heap);
230 err_context_heap:
231   object_heap_destroy (&drv_ctx->config_heap);
232 err_config_heap:
233   return false;
234 }
235 
236 VOID
media_release_buffer_store(struct buffer_store ** ptr)237 media_release_buffer_store (struct buffer_store ** ptr)
238 {
239   struct buffer_store *buffer_store = *ptr;
240 
241   if (buffer_store == NULL)
242     return;
243 
244   MEDIA_DRV_ASSERT (buffer_store->bo || buffer_store->buffer);
245   MEDIA_DRV_ASSERT (!(buffer_store->bo && buffer_store->buffer));
246   buffer_store->ref_count--;
247 
248   if (buffer_store->ref_count == 0)
249     {
250       dri_bo_unreference (buffer_store->bo);
251       media_drv_free_memory (buffer_store->buffer);
252       buffer_store->bo = NULL;
253       buffer_store->buffer = NULL;
254       media_drv_free_memory (buffer_store);
255     }
256 
257   *ptr = NULL;
258 }
259 
260 VOID
media_destroy_context(struct object_heap * heap,struct object_base * obj)261 media_destroy_context (struct object_heap *heap, struct object_base *obj)
262 {
263   struct object_context *obj_context = (struct object_context *) obj;
264   INT i;
265 
266   if (obj_context->hw_context)
267     {
268       obj_context->hw_context->destroy (obj_context->hw_context);
269       obj_context->hw_context = NULL;
270     }
271 
272   if (obj_context->codec_type == CODEC_ENC)
273     {
274       MEDIA_DRV_ASSERT (obj_context->codec_state.encode.num_slice_params <=
275 			obj_context->codec_state.encode.max_slice_params);
276       media_release_buffer_store (&obj_context->codec_state.encode.pic_param);
277       media_release_buffer_store (&obj_context->codec_state.encode.seq_param);
278       media_release_buffer_store (&obj_context->codec_state.encode.q_matrix);
279       for (i = 0; i < obj_context->codec_state.encode.num_slice_params; i++)
280 	media_release_buffer_store (&obj_context->codec_state.encode.
281 				    slice_params[i]);
282 
283       media_drv_free_memory (obj_context->codec_state.encode.slice_params);
284 
285       MEDIA_DRV_ASSERT (obj_context->codec_state.encode.
286 			num_slice_params_ext <=
287 			obj_context->codec_state.encode.max_slice_params_ext);
288       media_release_buffer_store (&obj_context->codec_state.encode.
289 				  pic_param_ext);
290       media_release_buffer_store (&obj_context->codec_state.encode.
291 				  seq_param_ext);
292       media_release_buffer_store (&obj_context->codec_state.
293 				  encode.frame_update_param);
294 
295       for (i = 0;
296 	   i <
297 	   ARRAY_ELEMS (obj_context->codec_state.encode.packed_header_param);
298 	   i++)
299 	media_release_buffer_store (&obj_context->codec_state.encode.
300 				    packed_header_param[i]);
301 
302       for (i = 0;
303 	   i <
304 	   ARRAY_ELEMS (obj_context->codec_state.encode.packed_header_data);
305 	   i++)
306 	media_release_buffer_store (&obj_context->codec_state.encode.
307 				    packed_header_data[i]);
308 
309       for (i = 0;
310 	   i < ARRAY_ELEMS (obj_context->codec_state.encode.misc_param); i++)
311 	media_release_buffer_store (&obj_context->codec_state.encode.
312 				    misc_param[i]);
313 
314       for (i = 0; i < obj_context->codec_state.encode.num_slice_params_ext;
315 	   i++)
316 	media_release_buffer_store (&obj_context->codec_state.encode.
317 				    slice_params_ext[i]);
318 
319       media_drv_free_memory (obj_context->codec_state.encode.
320 			     slice_params_ext);
321     }
322     else if (obj_context->codec_type == CODEC_DEC)
323     {
324       media_release_buffer_store(&obj_context->codec_state.decode.pic_param);
325       media_release_buffer_store(&obj_context->codec_state.decode.iq_matrix);
326       media_release_buffer_store(&obj_context->codec_state.decode.bit_plane);
327       media_release_buffer_store(&obj_context->codec_state.decode.huffman_table);
328 
329       for (i = 0; i < obj_context->codec_state.decode.num_slice_params; i++) {
330         media_release_buffer_store(&obj_context->codec_state.decode.slice_params[i]);
331         media_release_buffer_store(&obj_context->codec_state.decode.slice_datas[i]);
332       }
333 
334       media_drv_free_memory (obj_context->codec_state.decode.slice_params);
335       media_drv_free_memory (obj_context->codec_state.decode.slice_datas);
336     }
337 
338   media_drv_free_memory (obj_context->render_targets);
339   object_heap_free (heap, obj);
340 }
341 
342 VOID
media_destroy_config(struct object_heap * heap,struct object_base * obj)343 media_destroy_config (struct object_heap *heap, struct object_base *obj)
344 {
345   object_heap_free (heap, obj);
346 }
347 
348 static VOID
media_destroy_heap(struct object_heap * heap,VOID (* func)(struct object_heap * heap,struct object_base * object))349 media_destroy_heap (struct object_heap *heap,
350 		    VOID (*func) (struct object_heap * heap,
351 				  struct object_base * object))
352 {
353   struct object_base *object;
354   object_heap_iterator iter;
355 
356   object = object_heap_first (heap, &iter);
357 
358   while (object)
359     {
360       if (func)
361 	func (heap, object);
362 
363       object = object_heap_next (heap, &iter);
364     }
365 
366   object_heap_destroy (heap);
367 }
368 
369 VOID
media_destroy_buffer(struct object_heap * heap,struct object_base * obj)370 media_destroy_buffer (struct object_heap *heap, struct object_base *obj)
371 {
372   struct object_buffer *obj_buffer = (struct object_buffer *) obj;
373   MEDIA_DRV_ASSERT (obj_buffer->buffer_store);
374   media_release_buffer_store (&obj_buffer->buffer_store);
375   object_heap_free (heap, obj);
376 }
377 
378 
379 void
media_destroy_subpic(struct object_heap * heap,struct object_base * obj)380 media_destroy_subpic(struct object_heap *heap, struct object_base *obj)
381 {
382   object_heap_free(heap, obj);
383 }
384 
385 
386 VOID
media_driver_data_terminate(VADriverContextP ctx)387 media_driver_data_terminate (VADriverContextP ctx)
388 {
389   MEDIA_DRV_CONTEXT *drv_ctx = NULL;
390   MEDIA_DRV_ASSERT (ctx);
391   drv_ctx = ctx->pDriverData;
392 
393   media_drv_mutex_destroy (&drv_ctx->pp_mutex);
394   media_drv_mutex_destroy (&drv_ctx->render_mutex);
395 
396   if (drv_ctx->batch)
397     media_batchbuffer_free (drv_ctx->batch);
398 
399   if (drv_ctx->pp_batch)
400     media_batchbuffer_free (drv_ctx->pp_batch);
401 
402   if (drv_ctx->render_batch)
403     media_batchbuffer_free (drv_ctx->render_batch);
404 
405   media_destroy_heap (&drv_ctx->image_heap, media_destroy_image);
406   media_destroy_heap (&drv_ctx->buffer_heap, media_destroy_buffer);
407   media_destroy_heap (&drv_ctx->surface_heap, media_destroy_surface);
408   media_destroy_heap (&drv_ctx->context_heap, media_destroy_context);
409   media_destroy_heap (&drv_ctx->config_heap, media_destroy_config);
410   media_destroy_heap (&drv_ctx->subpic_heap, media_destroy_subpic);
411 }
412