• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "os/os_mman.h"
12 #include "util/os_time.h"
13 
14 #include <stdio.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 
18 #include <nouveau_drm.h>
19 #include <xf86drm.h>
20 
21 #include "nouveau_winsys.h"
22 #include "nouveau_screen.h"
23 #include "nouveau_context.h"
24 #include "nouveau_fence.h"
25 #include "nouveau_mm.h"
26 #include "nouveau_buffer.h"
27 
28 #include <compiler/glsl_types.h>
29 
30 /* XXX this should go away */
31 #include "frontend/drm_driver.h"
32 
33 /* Even though GPUs might allow addresses with more bits, some engines do not.
34  * Stick with 40 for compatibility.
35  */
36 #define NV_GENERIC_VM_LIMIT_SHIFT 39
37 
38 int nouveau_mesa_debug = 0;
39 
40 static const char *
nouveau_screen_get_name(struct pipe_screen * pscreen)41 nouveau_screen_get_name(struct pipe_screen *pscreen)
42 {
43    struct nouveau_screen *screen = nouveau_screen(pscreen);
44    return screen->chipset_name;
45 }
46 
47 static const char *
nouveau_screen_get_vendor(struct pipe_screen * pscreen)48 nouveau_screen_get_vendor(struct pipe_screen *pscreen)
49 {
50    return "nouveau";
51 }
52 
53 static const char *
nouveau_screen_get_device_vendor(struct pipe_screen * pscreen)54 nouveau_screen_get_device_vendor(struct pipe_screen *pscreen)
55 {
56    return "NVIDIA";
57 }
58 
59 static uint64_t
nouveau_screen_get_timestamp(struct pipe_screen * pscreen)60 nouveau_screen_get_timestamp(struct pipe_screen *pscreen)
61 {
62    int64_t cpu_time = os_time_get() * 1000;
63 
64    /* getparam of PTIMER_TIME takes about x10 as long (several usecs) */
65 
66    return cpu_time + nouveau_screen(pscreen)->cpu_gpu_time_delta;
67 }
68 
69 static struct disk_cache *
nouveau_screen_get_disk_shader_cache(struct pipe_screen * pscreen)70 nouveau_screen_get_disk_shader_cache(struct pipe_screen *pscreen)
71 {
72    return nouveau_screen(pscreen)->disk_shader_cache;
73 }
74 
75 static void
nouveau_screen_fence_ref(struct pipe_screen * pscreen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * pfence)76 nouveau_screen_fence_ref(struct pipe_screen *pscreen,
77                          struct pipe_fence_handle **ptr,
78                          struct pipe_fence_handle *pfence)
79 {
80    nouveau_fence_ref(nouveau_fence(pfence), (struct nouveau_fence **)ptr);
81 }
82 
83 static bool
nouveau_screen_fence_finish(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_fence_handle * pfence,uint64_t timeout)84 nouveau_screen_fence_finish(struct pipe_screen *screen,
85                             struct pipe_context *ctx,
86                             struct pipe_fence_handle *pfence,
87                             uint64_t timeout)
88 {
89    if (!timeout)
90       return nouveau_fence_signalled(nouveau_fence(pfence));
91 
92    return nouveau_fence_wait(nouveau_fence(pfence), NULL);
93 }
94 
95 
96 struct nouveau_bo *
nouveau_screen_bo_from_handle(struct pipe_screen * pscreen,struct winsys_handle * whandle,unsigned * out_stride)97 nouveau_screen_bo_from_handle(struct pipe_screen *pscreen,
98                               struct winsys_handle *whandle,
99                               unsigned *out_stride)
100 {
101    struct nouveau_device *dev = nouveau_screen(pscreen)->device;
102    struct nouveau_bo *bo = NULL;
103    int ret;
104 
105    if (whandle->offset != 0) {
106       debug_printf("%s: attempt to import unsupported winsys offset %d\n",
107                    __FUNCTION__, whandle->offset);
108       return NULL;
109    }
110 
111    if (whandle->type != WINSYS_HANDLE_TYPE_SHARED &&
112        whandle->type != WINSYS_HANDLE_TYPE_FD) {
113       debug_printf("%s: attempt to import unsupported handle type %d\n",
114                    __FUNCTION__, whandle->type);
115       return NULL;
116    }
117 
118    if (whandle->type == WINSYS_HANDLE_TYPE_SHARED)
119       ret = nouveau_bo_name_ref(dev, whandle->handle, &bo);
120    else
121       ret = nouveau_bo_prime_handle_ref(dev, whandle->handle, &bo);
122 
123    if (ret) {
124       debug_printf("%s: ref name 0x%08x failed with %d\n",
125                    __FUNCTION__, whandle->handle, ret);
126       return NULL;
127    }
128 
129    *out_stride = whandle->stride;
130    return bo;
131 }
132 
133 
134 bool
nouveau_screen_bo_get_handle(struct pipe_screen * pscreen,struct nouveau_bo * bo,unsigned stride,struct winsys_handle * whandle)135 nouveau_screen_bo_get_handle(struct pipe_screen *pscreen,
136                              struct nouveau_bo *bo,
137                              unsigned stride,
138                              struct winsys_handle *whandle)
139 {
140    whandle->stride = stride;
141 
142    if (whandle->type == WINSYS_HANDLE_TYPE_SHARED) {
143       return nouveau_bo_name_get(bo, &whandle->handle) == 0;
144    } else if (whandle->type == WINSYS_HANDLE_TYPE_KMS) {
145       whandle->handle = bo->handle;
146       return true;
147    } else if (whandle->type == WINSYS_HANDLE_TYPE_FD) {
148       return nouveau_bo_set_prime(bo, (int *)&whandle->handle) == 0;
149    } else {
150       return false;
151    }
152 }
153 
154 static void
nouveau_disk_cache_create(struct nouveau_screen * screen)155 nouveau_disk_cache_create(struct nouveau_screen *screen)
156 {
157    struct mesa_sha1 ctx;
158    unsigned char sha1[20];
159    char cache_id[20 * 2 + 1];
160    uint64_t driver_flags = 0;
161 
162    _mesa_sha1_init(&ctx);
163    if (!disk_cache_get_function_identifier(nouveau_disk_cache_create,
164                                            &ctx))
165       return;
166 
167    _mesa_sha1_final(&ctx, sha1);
168    disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
169 
170    if (screen->prefer_nir)
171       driver_flags |= NOUVEAU_SHADER_CACHE_FLAGS_IR_NIR;
172    else
173       driver_flags |= NOUVEAU_SHADER_CACHE_FLAGS_IR_TGSI;
174 
175    screen->disk_shader_cache =
176       disk_cache_create(nouveau_screen_get_name(&screen->base),
177                         cache_id, driver_flags);
178 }
179 
180 static void*
reserve_vma(uintptr_t start,uint64_t reserved_size)181 reserve_vma(uintptr_t start, uint64_t reserved_size)
182 {
183    void *reserved = os_mmap((void*)start, reserved_size, PROT_NONE,
184                             MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
185    if (reserved == MAP_FAILED)
186       return NULL;
187    return reserved;
188 }
189 
190 int
nouveau_screen_init(struct nouveau_screen * screen,struct nouveau_device * dev)191 nouveau_screen_init(struct nouveau_screen *screen, struct nouveau_device *dev)
192 {
193    struct pipe_screen *pscreen = &screen->base;
194    struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
195    struct nvc0_fifo nvc0_data = { };
196    uint64_t time;
197    int size, ret;
198    void *data;
199    union nouveau_bo_config mm_config;
200 
201    char *nv_dbg = getenv("NOUVEAU_MESA_DEBUG");
202    if (nv_dbg)
203       nouveau_mesa_debug = atoi(nv_dbg);
204 
205    screen->prefer_nir = !debug_get_bool_option("NV50_PROG_USE_TGSI", false);
206 
207    screen->force_enable_cl = debug_get_bool_option("NOUVEAU_ENABLE_CL", false);
208    if (screen->force_enable_cl)
209       glsl_type_singleton_init_or_ref();
210 
211    screen->disable_fences = debug_get_bool_option("NOUVEAU_DISABLE_FENCES", false);
212 
213    /* These must be set before any failure is possible, as the cleanup
214     * paths assume they're responsible for deleting them.
215     */
216    screen->drm = nouveau_drm(&dev->object);
217    screen->device = dev;
218 
219    /*
220     * this is initialized to 1 in nouveau_drm_screen_create after screen
221     * is fully constructed and added to the global screen list.
222     */
223    screen->refcount = -1;
224 
225    if (dev->chipset < 0xc0) {
226       data = &nv04_data;
227       size = sizeof(nv04_data);
228    } else {
229       data = &nvc0_data;
230       size = sizeof(nvc0_data);
231    }
232 
233    bool enable_svm = debug_get_bool_option("NOUVEAU_SVM", false);
234    screen->has_svm = false;
235    /* we only care about HMM with OpenCL enabled */
236    if (dev->chipset > 0x130 && screen->force_enable_cl && enable_svm) {
237       /* Before being able to enable SVM we need to carve out some memory for
238        * driver bo allocations. Let's just base the size on the available VRAM.
239        *
240        * 40 bit is the biggest we care about and for 32 bit systems we don't
241        * want to allocate all of the available memory either.
242        *
243        * Also we align the size we want to reserve to the next POT to make use
244        * of hugepages.
245        */
246       const int vram_shift = util_logbase2_ceil64(dev->vram_size);
247       const int limit_bit =
248          MIN2(sizeof(void*) * 8 - 1, NV_GENERIC_VM_LIMIT_SHIFT);
249       screen->svm_cutout_size =
250          BITFIELD64_BIT(MIN2(sizeof(void*) == 4 ? 26 : NV_GENERIC_VM_LIMIT_SHIFT, vram_shift));
251 
252       size_t start = screen->svm_cutout_size;
253       do {
254          screen->svm_cutout = reserve_vma(start, screen->svm_cutout_size);
255          if (!screen->svm_cutout) {
256             start += screen->svm_cutout_size;
257             continue;
258          }
259 
260          struct drm_nouveau_svm_init svm_args = {
261             .unmanaged_addr = (uintptr_t)screen->svm_cutout,
262             .unmanaged_size = screen->svm_cutout_size,
263          };
264 
265          ret = drmCommandWrite(screen->drm->fd, DRM_NOUVEAU_SVM_INIT,
266                                &svm_args, sizeof(svm_args));
267          screen->has_svm = !ret;
268          if (!screen->has_svm)
269             os_munmap(screen->svm_cutout, screen->svm_cutout_size);
270          break;
271       } while ((start + screen->svm_cutout_size) < BITFIELD64_MASK(limit_bit));
272    }
273 
274    switch (dev->chipset) {
275    case 0x0ea: /* TK1, GK20A */
276    case 0x12b: /* TX1, GM20B */
277    case 0x13b: /* TX2, GP10B */
278       screen->tegra_sector_layout = true;
279       break;
280    default:
281       /* Xavier's GPU and everything else */
282       screen->tegra_sector_layout = false;
283       break;
284    }
285 
286    /*
287     * Set default VRAM domain if not overridden
288     */
289    if (!screen->vram_domain) {
290       if (dev->vram_size > 0)
291          screen->vram_domain = NOUVEAU_BO_VRAM;
292       else
293          screen->vram_domain = NOUVEAU_BO_GART;
294    }
295 
296    ret = nouveau_object_new(&dev->object, 0, NOUVEAU_FIFO_CHANNEL_CLASS,
297                             data, size, &screen->channel);
298    if (ret)
299       goto err;
300 
301    ret = nouveau_client_new(screen->device, &screen->client);
302    if (ret)
303       goto err;
304    ret = nouveau_pushbuf_new(screen->client, screen->channel,
305                              4, 512 * 1024, 1,
306                              &screen->pushbuf);
307    if (ret)
308       goto err;
309 
310    /* getting CPU time first appears to be more accurate */
311    screen->cpu_gpu_time_delta = os_time_get();
312 
313    ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_PTIMER_TIME, &time);
314    if (!ret)
315       screen->cpu_gpu_time_delta = time - screen->cpu_gpu_time_delta * 1000;
316 
317    snprintf(screen->chipset_name, sizeof(screen->chipset_name), "NV%02X", dev->chipset);
318    pscreen->get_name = nouveau_screen_get_name;
319    pscreen->get_vendor = nouveau_screen_get_vendor;
320    pscreen->get_device_vendor = nouveau_screen_get_device_vendor;
321    pscreen->get_disk_shader_cache = nouveau_screen_get_disk_shader_cache;
322 
323    pscreen->get_timestamp = nouveau_screen_get_timestamp;
324 
325    pscreen->fence_reference = nouveau_screen_fence_ref;
326    pscreen->fence_finish = nouveau_screen_fence_finish;
327 
328    nouveau_disk_cache_create(screen);
329 
330    screen->transfer_pushbuf_threshold = 192;
331    screen->lowmem_bindings = PIPE_BIND_GLOBAL; /* gallium limit */
332    screen->vidmem_bindings =
333       PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL |
334       PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT |
335       PIPE_BIND_CURSOR |
336       PIPE_BIND_SAMPLER_VIEW |
337       PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE |
338       PIPE_BIND_COMPUTE_RESOURCE |
339       PIPE_BIND_GLOBAL;
340    screen->sysmem_bindings =
341       PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_STREAM_OUTPUT |
342       PIPE_BIND_COMMAND_ARGS_BUFFER;
343 
344    memset(&mm_config, 0, sizeof(mm_config));
345 
346    screen->mm_GART = nouveau_mm_create(dev,
347                                        NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
348                                        &mm_config);
349    screen->mm_VRAM = nouveau_mm_create(dev, NOUVEAU_BO_VRAM, &mm_config);
350    return 0;
351 
352 err:
353    if (screen->svm_cutout)
354       os_munmap(screen->svm_cutout, screen->svm_cutout_size);
355    return ret;
356 }
357 
358 void
nouveau_screen_fini(struct nouveau_screen * screen)359 nouveau_screen_fini(struct nouveau_screen *screen)
360 {
361    int fd = screen->drm->fd;
362 
363    if (screen->force_enable_cl)
364       glsl_type_singleton_decref();
365    if (screen->has_svm)
366       os_munmap(screen->svm_cutout, screen->svm_cutout_size);
367 
368    nouveau_mm_destroy(screen->mm_GART);
369    nouveau_mm_destroy(screen->mm_VRAM);
370 
371    nouveau_pushbuf_del(&screen->pushbuf);
372 
373    nouveau_client_del(&screen->client);
374    nouveau_object_del(&screen->channel);
375 
376    nouveau_device_del(&screen->device);
377    nouveau_drm_del(&screen->drm);
378    close(fd);
379 
380    disk_cache_destroy(screen->disk_shader_cache);
381 }
382 
383 static void
nouveau_set_debug_callback(struct pipe_context * pipe,const struct util_debug_callback * cb)384 nouveau_set_debug_callback(struct pipe_context *pipe,
385                            const struct util_debug_callback *cb)
386 {
387    struct nouveau_context *context = nouveau_context(pipe);
388 
389    if (cb)
390       context->debug = *cb;
391    else
392       memset(&context->debug, 0, sizeof(context->debug));
393 }
394 
395 void
nouveau_context_init(struct nouveau_context * context)396 nouveau_context_init(struct nouveau_context *context)
397 {
398    context->pipe.set_debug_callback = nouveau_set_debug_callback;
399 }
400