nvc0: initial support for gv100
[mesa.git] / src / gallium / drivers / nouveau / nouveau_screen.c
1 #include "pipe/p_defines.h"
2 #include "pipe/p_screen.h"
3 #include "pipe/p_state.h"
4
5 #include "util/u_memory.h"
6 #include "util/u_inlines.h"
7 #include "util/format/u_format.h"
8 #include "util/format/u_format_s3tc.h"
9 #include "util/u_string.h"
10
11 #include "util/os_time.h"
12
13 #include <stdio.h>
14 #include <errno.h>
15 #include <stdlib.h>
16
17 #include <nouveau_drm.h>
18
19 #include "nouveau_winsys.h"
20 #include "nouveau_screen.h"
21 #include "nouveau_context.h"
22 #include "nouveau_fence.h"
23 #include "nouveau_mm.h"
24 #include "nouveau_buffer.h"
25
26 #include <compiler/glsl_types.h>
27
28 /* XXX this should go away */
29 #include "frontend/drm_driver.h"
30
31 int nouveau_mesa_debug = 0;
32
33 static const char *
34 nouveau_screen_get_name(struct pipe_screen *pscreen)
35 {
36 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
37 static char buffer[128];
38
39 snprintf(buffer, sizeof(buffer), "NV%02X", dev->chipset);
40 return buffer;
41 }
42
43 static const char *
44 nouveau_screen_get_vendor(struct pipe_screen *pscreen)
45 {
46 return "nouveau";
47 }
48
49 static const char *
50 nouveau_screen_get_device_vendor(struct pipe_screen *pscreen)
51 {
52 return "NVIDIA";
53 }
54
55 static uint64_t
56 nouveau_screen_get_timestamp(struct pipe_screen *pscreen)
57 {
58 int64_t cpu_time = os_time_get() * 1000;
59
60 /* getparam of PTIMER_TIME takes about x10 as long (several usecs) */
61
62 return cpu_time + nouveau_screen(pscreen)->cpu_gpu_time_delta;
63 }
64
65 static struct disk_cache *
66 nouveau_screen_get_disk_shader_cache(struct pipe_screen *pscreen)
67 {
68 return nouveau_screen(pscreen)->disk_shader_cache;
69 }
70
71 static void
72 nouveau_screen_fence_ref(struct pipe_screen *pscreen,
73 struct pipe_fence_handle **ptr,
74 struct pipe_fence_handle *pfence)
75 {
76 nouveau_fence_ref(nouveau_fence(pfence), (struct nouveau_fence **)ptr);
77 }
78
79 static bool
80 nouveau_screen_fence_finish(struct pipe_screen *screen,
81 struct pipe_context *ctx,
82 struct pipe_fence_handle *pfence,
83 uint64_t timeout)
84 {
85 if (!timeout)
86 return nouveau_fence_signalled(nouveau_fence(pfence));
87
88 return nouveau_fence_wait(nouveau_fence(pfence), NULL);
89 }
90
91
92 struct nouveau_bo *
93 nouveau_screen_bo_from_handle(struct pipe_screen *pscreen,
94 struct winsys_handle *whandle,
95 unsigned *out_stride)
96 {
97 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
98 struct nouveau_bo *bo = 0;
99 int ret;
100
101 if (whandle->offset != 0) {
102 debug_printf("%s: attempt to import unsupported winsys offset %d\n",
103 __FUNCTION__, whandle->offset);
104 return NULL;
105 }
106
107 if (whandle->type != WINSYS_HANDLE_TYPE_SHARED &&
108 whandle->type != WINSYS_HANDLE_TYPE_FD) {
109 debug_printf("%s: attempt to import unsupported handle type %d\n",
110 __FUNCTION__, whandle->type);
111 return NULL;
112 }
113
114 if (whandle->type == WINSYS_HANDLE_TYPE_SHARED)
115 ret = nouveau_bo_name_ref(dev, whandle->handle, &bo);
116 else
117 ret = nouveau_bo_prime_handle_ref(dev, whandle->handle, &bo);
118
119 if (ret) {
120 debug_printf("%s: ref name 0x%08x failed with %d\n",
121 __FUNCTION__, whandle->handle, ret);
122 return NULL;
123 }
124
125 *out_stride = whandle->stride;
126 return bo;
127 }
128
129
130 bool
131 nouveau_screen_bo_get_handle(struct pipe_screen *pscreen,
132 struct nouveau_bo *bo,
133 unsigned stride,
134 struct winsys_handle *whandle)
135 {
136 whandle->stride = stride;
137
138 if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
139 return nouveau_bo_name_get(bo, &whandle->handle) == 0;
140 } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
141 whandle->handle = bo->handle;
142 return true;
143 } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
144 return nouveau_bo_set_prime(bo, (int *)&whandle->handle) == 0;
145 } else {
146 return false;
147 }
148 }
149
150 static void
151 nouveau_disk_cache_create(struct nouveau_screen *screen)
152 {
153 struct mesa_sha1 ctx;
154 unsigned char sha1[20];
155 char cache_id[20 * 2 + 1];
156 uint64_t driver_flags = 0;
157
158 _mesa_sha1_init(&ctx);
159 if (!disk_cache_get_function_identifier(nouveau_disk_cache_create,
160 &ctx))
161 return;
162
163 _mesa_sha1_final(&ctx, sha1);
164 disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
165
166 if (screen->prefer_nir)
167 driver_flags |= NOUVEAU_SHADER_CACHE_FLAGS_IR_NIR;
168 else
169 driver_flags |= NOUVEAU_SHADER_CACHE_FLAGS_IR_TGSI;
170
171 screen->disk_shader_cache =
172 disk_cache_create(nouveau_screen_get_name(&screen->base),
173 cache_id, driver_flags);
174 }
175
176 int
177 nouveau_screen_init(struct nouveau_screen *screen, struct nouveau_device *dev)
178 {
179 struct pipe_screen *pscreen = &screen->base;
180 struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
181 struct nvc0_fifo nvc0_data = { };
182 uint64_t time;
183 int size, ret;
184 void *data;
185 union nouveau_bo_config mm_config;
186
187 char *nv_dbg = getenv("NOUVEAU_MESA_DEBUG");
188 if (nv_dbg)
189 nouveau_mesa_debug = atoi(nv_dbg);
190
191 if (dev->chipset < 0x140)
192 screen->prefer_nir = debug_get_bool_option("NV50_PROG_USE_NIR", false);
193 else
194 screen->prefer_nir = true;
195
196 screen->force_enable_cl = debug_get_bool_option("NOUVEAU_ENABLE_CL", false);
197 if (screen->force_enable_cl)
198 glsl_type_singleton_init_or_ref();
199
200 /* These must be set before any failure is possible, as the cleanup
201 * paths assume they're responsible for deleting them.
202 */
203 screen->drm = nouveau_drm(&dev->object);
204 screen->device = dev;
205
206 /*
207 * this is initialized to 1 in nouveau_drm_screen_create after screen
208 * is fully constructed and added to the global screen list.
209 */
210 screen->refcount = -1;
211
212 if (dev->chipset < 0xc0) {
213 data = &nv04_data;
214 size = sizeof(nv04_data);
215 } else {
216 data = &nvc0_data;
217 size = sizeof(nvc0_data);
218 }
219
220 /*
221 * Set default VRAM domain if not overridden
222 */
223 if (!screen->vram_domain) {
224 if (dev->vram_size > 0)
225 screen->vram_domain = NOUVEAU_BO_VRAM;
226 else
227 screen->vram_domain = NOUVEAU_BO_GART;
228 }
229
230 ret = nouveau_object_new(&dev->object, 0, NOUVEAU_FIFO_CHANNEL_CLASS,
231 data, size, &screen->channel);
232 if (ret)
233 return ret;
234
235 ret = nouveau_client_new(screen->device, &screen->client);
236 if (ret)
237 return ret;
238 ret = nouveau_pushbuf_new(screen->client, screen->channel,
239 4, 512 * 1024, 1,
240 &screen->pushbuf);
241 if (ret)
242 return ret;
243
244 /* getting CPU time first appears to be more accurate */
245 screen->cpu_gpu_time_delta = os_time_get();
246
247 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_PTIMER_TIME, &time);
248 if (!ret)
249 screen->cpu_gpu_time_delta = time - screen->cpu_gpu_time_delta * 1000;
250
251 pscreen->get_name = nouveau_screen_get_name;
252 pscreen->get_vendor = nouveau_screen_get_vendor;
253 pscreen->get_device_vendor = nouveau_screen_get_device_vendor;
254 pscreen->get_disk_shader_cache = nouveau_screen_get_disk_shader_cache;
255
256 pscreen->get_timestamp = nouveau_screen_get_timestamp;
257
258 pscreen->fence_reference = nouveau_screen_fence_ref;
259 pscreen->fence_finish = nouveau_screen_fence_finish;
260
261 nouveau_disk_cache_create(screen);
262
263 screen->transfer_pushbuf_threshold = 192;
264 screen->lowmem_bindings = PIPE_BIND_GLOBAL; /* gallium limit */
265 screen->vidmem_bindings =
266 PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
267 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
268 PIPE_BIND_CURSOR |
269 PIPE_BIND_SAMPLER_VIEW |
270 PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE |
271 PIPE_BIND_COMPUTE_RESOURCE |
272 PIPE_BIND_GLOBAL;
273 screen->sysmem_bindings =
274 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_STREAM_OUTPUT |
275 PIPE_BIND_COMMAND_ARGS_BUFFER;
276
277 memset(&mm_config, 0, sizeof(mm_config));
278
279 screen->mm_GART = nouveau_mm_create(dev,
280 NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
281 &mm_config);
282 screen->mm_VRAM = nouveau_mm_create(dev, NOUVEAU_BO_VRAM, &mm_config);
283 return 0;
284 }
285
286 void
287 nouveau_screen_fini(struct nouveau_screen *screen)
288 {
289 int fd = screen->drm->fd;
290
291 if (screen->force_enable_cl)
292 glsl_type_singleton_decref();
293
294 nouveau_mm_destroy(screen->mm_GART);
295 nouveau_mm_destroy(screen->mm_VRAM);
296
297 nouveau_pushbuf_del(&screen->pushbuf);
298
299 nouveau_client_del(&screen->client);
300 nouveau_object_del(&screen->channel);
301
302 nouveau_device_del(&screen->device);
303 nouveau_drm_del(&screen->drm);
304 close(fd);
305
306 disk_cache_destroy(screen->disk_shader_cache);
307 }
308
309 static void
310 nouveau_set_debug_callback(struct pipe_context *pipe,
311 const struct pipe_debug_callback *cb)
312 {
313 struct nouveau_context *context = nouveau_context(pipe);
314
315 if (cb)
316 context->debug = *cb;
317 else
318 memset(&context->debug, 0, sizeof(context->debug));
319 }
320
321 void
322 nouveau_context_init(struct nouveau_context *context)
323 {
324 context->pipe.set_debug_callback = nouveau_set_debug_callback;
325 }