i965_drv_video: improved MV quality for VME
[platform/upstream/libva.git] / i965_drv_video / intel_driver.c
1 /*
2  * Copyright © 2009 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  *    Xiang Haihao <haihao.xiang@intel.com>
26  *    Zou Nan hai <nanhai.zou@intel.com>
27  *
28  */
29
30 #include <assert.h>
31
32 #include "va_dricommon.h"
33
34 #include "intel_batchbuffer.h"
35 #include "intel_memman.h"
36 #include "intel_driver.h"
37
38 static Bool
39 intel_driver_get_param(struct intel_driver_data *intel, int param, int *value)
40 {
41    struct drm_i915_getparam gp;
42
43    gp.param = param;
44    gp.value = value;
45
46    return drmCommandWriteRead(intel->fd, DRM_I915_GETPARAM, &gp, sizeof(gp)) == 0;
47 }
48
49 Bool 
50 intel_driver_init(VADriverContextP ctx)
51 {
52     struct intel_driver_data *intel = intel_driver_data(ctx);
53     struct dri_state *dri_state = (struct dri_state *)ctx->dri_state;
54     int has_exec2, has_bsd, has_blt;
55
56     assert(dri_state);
57     assert(dri_state->driConnectedFlag == VA_DRI2 || 
58            dri_state->driConnectedFlag == VA_DRI1);
59
60     intel->fd = dri_state->fd;
61     intel->dri2Enabled = (dri_state->driConnectedFlag == VA_DRI2);
62
63     if (!intel->dri2Enabled) {
64         drm_sarea_t *pSAREA;
65
66         pSAREA = (drm_sarea_t *)dri_state->pSAREA;
67         intel->hHWContext = dri_state->hwContext;
68         intel->driHwLock = (drmLock *)(&pSAREA->lock);
69         intel->pPrivSarea = (void *)pSAREA + sizeof(drm_sarea_t);
70     }
71
72     intel->locked = 0;
73     pthread_mutex_init(&intel->ctxmutex, NULL);
74
75     intel_driver_get_param(intel, I915_PARAM_CHIPSET_ID, &intel->device_id);
76     if (intel_driver_get_param(intel, I915_PARAM_HAS_EXECBUF2, &has_exec2))
77         intel->has_exec2 = has_exec2;
78     if (intel_driver_get_param(intel, I915_PARAM_HAS_BSD, &has_bsd))
79         intel->has_bsd = has_bsd;
80     if (intel_driver_get_param(intel, I915_PARAM_HAS_BLT, &has_blt))
81         intel->has_blt = has_blt;
82
83     intel_memman_init(intel);
84     return True;
85 }
86
87 Bool 
88 intel_driver_terminate(VADriverContextP ctx)
89 {
90     struct intel_driver_data *intel = intel_driver_data(ctx);
91
92     intel_memman_terminate(intel);
93     pthread_mutex_destroy(&intel->ctxmutex);
94
95     return True;
96 }
97
98 void 
99 intel_lock_hardware(VADriverContextP ctx)
100 {
101     struct intel_driver_data *intel = intel_driver_data(ctx);
102     char __ret = 0;
103
104     PPTHREAD_MUTEX_LOCK();
105
106     assert(!intel->locked);
107
108     if (!intel->dri2Enabled) {
109         DRM_CAS(intel->driHwLock, 
110                 intel->hHWContext,
111                 (DRM_LOCK_HELD|intel->hHWContext),
112                 __ret);
113
114         if (__ret) {
115             drmGetLock(intel->fd, intel->hHWContext, 0);
116         }       
117     }
118
119     intel->locked = 1;
120 }
121
122 void 
123 intel_unlock_hardware(VADriverContextP ctx)
124 {
125     struct intel_driver_data *intel = intel_driver_data(ctx);
126
127     if (!intel->dri2Enabled) {
128         DRM_UNLOCK(intel->fd, 
129                    intel->driHwLock,
130                    intel->hHWContext);
131     }
132
133     intel->locked = 0;
134     PPTHREAD_MUTEX_UNLOCK();
135 }