OSDN Git Service

Return false instead of assertion failure
[android-x86/hardware-intel-common-vaapi.git] / src / 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 "sysdeps.h"
31
32 #include <va/va_drmcommon.h>
33
34 #include "intel_batchbuffer.h"
35 #include "intel_memman.h"
36 #include "intel_driver.h"
37 uint32_t g_intel_debug_option_flags = 0;
38
39 #ifdef I915_PARAM_HAS_BSD2
40 #define LOCAL_I915_PARAM_HAS_BSD2 I915_PARAM_HAS_BSD2
41 #endif
42
43 #ifndef LOCAL_I915_PARAM_HAS_BSD2
44 #define LOCAL_I915_PARAM_HAS_BSD2   30
45 #endif
46
47 #ifdef I915_PARAM_HAS_HUC
48 #define LOCAL_I915_PARAM_HAS_HUC I915_PARAM_HAS_HUC
49 #else
50 #define LOCAL_I915_PARAM_HAS_HUC 42
51 #endif
52
53 #ifdef I915_PARAM_EU_TOTAL
54 #define LOCAL_I915_PARAM_EU_TOTAL I915_PARAM_EU_TOTAL
55 #else
56 #define LOCAL_I915_PARAM_EU_TOTAL 34
57 #endif
58
59 static Bool
60 intel_driver_get_param(struct intel_driver_data *intel, int param, int *value)
61 {
62     struct drm_i915_getparam gp;
63
64     gp.param = param;
65     gp.value = value;
66
67     return drmCommandWriteRead(intel->fd, DRM_I915_GETPARAM, &gp, sizeof(gp)) == 0;
68 }
69
70 static void intel_driver_get_revid(struct intel_driver_data *intel, int *value)
71 {
72 #define PCI_REVID   8
73     FILE *fp;
74     char config_data[16];
75
76     fp = fopen("/sys/devices/pci0000:00/0000:00:02.0/config", "r");
77
78     if (fp) {
79         if (fread(config_data, 1, 16, fp))
80             *value = config_data[PCI_REVID];
81         else
82             *value = 2; /* assume it is at least  B-steping */
83         fclose(fp);
84     } else {
85         *value = 2; /* assume it is at least  B-steping */
86     }
87
88     return;
89 }
90
91 extern const struct intel_device_info *i965_get_device_info(int devid);
92
93 bool
94 intel_driver_init(VADriverContextP ctx)
95 {
96     struct intel_driver_data *intel = intel_driver_data(ctx);
97     struct drm_state * const drm_state = (struct drm_state *)ctx->drm_state;
98     int has_exec2 = 0, has_bsd = 0, has_blt = 0, has_vebox = 0;
99     char *env_str = NULL;
100     int ret_value = 0;
101
102     g_intel_debug_option_flags = 0;
103     if ((env_str = getenv("VA_INTEL_DEBUG")))
104         g_intel_debug_option_flags = atoi(env_str);
105
106     if (g_intel_debug_option_flags)
107         fprintf(stderr, "g_intel_debug_option_flags:%x\n", g_intel_debug_option_flags);
108
109     ASSERT_RET(drm_state, false);
110     ASSERT_RET((VA_CHECK_DRM_AUTH_TYPE(ctx, VA_DRM_AUTH_DRI1) ||
111                 VA_CHECK_DRM_AUTH_TYPE(ctx, VA_DRM_AUTH_DRI2) ||
112                 VA_CHECK_DRM_AUTH_TYPE(ctx, VA_DRM_AUTH_CUSTOM)),
113                false);
114
115     intel->fd = drm_state->fd;
116     intel->dri2Enabled = (VA_CHECK_DRM_AUTH_TYPE(ctx, VA_DRM_AUTH_DRI2) ||
117                           VA_CHECK_DRM_AUTH_TYPE(ctx, VA_DRM_AUTH_CUSTOM));
118
119     if (!intel->dri2Enabled) {
120         return false;
121     }
122
123     intel->locked = 0;
124     pthread_mutex_init(&intel->ctxmutex, NULL);
125
126     if (!intel_memman_init(intel))
127         return false;
128
129     intel->device_id = drm_intel_bufmgr_gem_get_devid(intel->bufmgr);
130     intel->device_info = i965_get_device_info(intel->device_id);
131
132     if (!intel->device_info)
133         return false;
134
135     if (intel_driver_get_param(intel, I915_PARAM_HAS_EXECBUF2, &has_exec2))
136         intel->has_exec2 = has_exec2;
137     if (intel_driver_get_param(intel, I915_PARAM_HAS_BSD, &has_bsd))
138         intel->has_bsd = has_bsd;
139     if (intel_driver_get_param(intel, I915_PARAM_HAS_BLT, &has_blt))
140         intel->has_blt = has_blt;
141     if (intel_driver_get_param(intel, I915_PARAM_HAS_VEBOX, &has_vebox))
142         intel->has_vebox = !!has_vebox;
143
144     intel->has_bsd2 = 0;
145     if (intel_driver_get_param(intel, LOCAL_I915_PARAM_HAS_BSD2, &ret_value))
146         intel->has_bsd2 = !!ret_value;
147
148     intel->has_huc = 0;
149     ret_value = 0;
150
151     if (intel_driver_get_param(intel, LOCAL_I915_PARAM_HAS_HUC, &ret_value))
152         intel->has_huc = !!ret_value;
153
154     intel->eu_total = 0;
155     if (intel_driver_get_param(intel, LOCAL_I915_PARAM_EU_TOTAL, &ret_value)) {
156         intel->eu_total = ret_value;
157     }
158
159     intel->mocs_state = 0;
160
161 #define GEN9_PTE_CACHE    2
162
163     if (IS_GEN9(intel->device_info) ||
164         IS_GEN10(intel->device_info))
165         intel->mocs_state = GEN9_PTE_CACHE;
166
167     intel_driver_get_revid(intel, &intel->revision);
168     return true;
169 }
170
171 void
172 intel_driver_terminate(VADriverContextP ctx)
173 {
174     struct intel_driver_data *intel = intel_driver_data(ctx);
175
176     intel_memman_terminate(intel);
177     pthread_mutex_destroy(&intel->ctxmutex);
178 }