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