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