libdrm/nouveau: add prime handle->bo and bo->handle support.
[profile/ivi/libdrm.git] / nouveau / nouveau.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <sys/mman.h>
37 #include <fcntl.h>
38
39 #include <xf86drm.h>
40 #include <xf86atomic.h>
41 #include "libdrm_lists.h"
42 #include "nouveau_drm.h"
43
44 #include "nouveau.h"
45 #include "private.h"
46
47 #ifdef DEBUG
48 uint32_t nouveau_debug = 0;
49
50 static void
51 debug_init(char *args)
52 {
53         if (args) {
54                 int n = strtol(args, NULL, 0);
55                 if (n >= 0)
56                         nouveau_debug = n;
57         }
58 }
59 #endif
60
61 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
62  * is kept here to prevent AIGLX from crashing if the DDX is linked against
63  * the new libdrm, but the DRI driver against the old
64  */
65 int
66 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
67                              drm_context_t ctx)
68 {
69         return -EACCES;
70 }
71
72 int
73 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
74 {
75         struct nouveau_device_priv *nvdev = calloc(1, sizeof(*nvdev));
76         struct nouveau_device *dev = &nvdev->base;
77         uint64_t chipset, vram, gart, bousage;
78         drmVersionPtr ver;
79         int ret;
80
81 #ifdef DEBUG
82         debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
83 #endif
84
85         if (!nvdev)
86                 return -ENOMEM;
87         nvdev->base.fd = fd;
88
89         ver = drmGetVersion(fd);
90         if (ver) dev->drm_version = (ver->version_major << 24) |
91                                     (ver->version_minor << 8) |
92                                      ver->version_patchlevel;
93         drmFreeVersion(ver);
94
95         if ( dev->drm_version != 0x00000010 &&
96             (dev->drm_version <  0x01000000 ||
97              dev->drm_version >= 0x02000000)) {
98                 nouveau_device_del(&dev);
99                 return -EINVAL;
100         }
101
102         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
103         if (ret == 0)
104         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
105         if (ret == 0)
106         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
107         if (ret) {
108                 nouveau_device_del(&dev);
109                 return ret;
110         }
111
112         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
113         if (ret == 0)
114                 nvdev->have_bo_usage = (bousage != 0);
115
116         nvdev->close = close;
117         DRMINITLISTHEAD(&nvdev->bo_list);
118         nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
119         nvdev->base.lib_version = 0x01000000;
120         nvdev->base.chipset = chipset;
121         nvdev->base.vram_size = vram;
122         nvdev->base.gart_size = gart;
123         nvdev->base.vram_limit = (nvdev->base.vram_size * 80) / 100;
124         nvdev->base.gart_limit = (nvdev->base.gart_size * 80) / 100;
125
126         *pdev = &nvdev->base;
127         return 0;
128 }
129
130 int
131 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
132 {
133         int ret = -ENODEV, fd = drmOpen("nouveau", busid);
134         if (fd >= 0) {
135                 ret = nouveau_device_wrap(fd, 1, pdev);
136                 if (ret)
137                         drmClose(fd);
138         }
139         return ret;
140 }
141
142 void
143 nouveau_device_del(struct nouveau_device **pdev)
144 {
145         struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
146         if (nvdev) {
147                 if (nvdev->close)
148                         drmClose(nvdev->base.fd);
149                 free(nvdev->client);
150                 free(nvdev);
151                 *pdev = NULL;
152         }
153 }
154
155 int
156 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
157 {
158         struct drm_nouveau_getparam r = { param, 0 };
159         int fd = dev->fd, ret =
160                 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
161         *value = r.value;
162         return ret;
163 }
164
165 int
166 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
167 {
168         struct drm_nouveau_setparam r = { param, value };
169         return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
170 }
171
172 int
173 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
174 {
175         struct nouveau_device_priv *nvdev = nouveau_device(dev);
176         struct nouveau_client_priv *pcli;
177         int id = 0, i, ret = -ENOMEM;
178         uint32_t *clients;
179
180         for (i = 0; i < nvdev->nr_client; i++) {
181                 id = ffs(nvdev->client[i]) - 1;
182                 if (id >= 0)
183                         goto out;
184         }
185
186         clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
187         if (!clients)
188                 return ret;
189         nvdev->client = clients;
190         nvdev->client[i] = 0;
191         nvdev->nr_client++;
192
193 out:
194         pcli = calloc(1, sizeof(*pcli));
195         if (pcli) {
196                 nvdev->client[i] |= (1 << id);
197                 pcli->base.device = dev;
198                 pcli->base.id = (i * 32) + id;
199                 ret = 0;
200         }
201
202         *pclient = &pcli->base;
203         return ret;
204 }
205
206 void
207 nouveau_client_del(struct nouveau_client **pclient)
208 {
209         struct nouveau_client_priv *pcli = nouveau_client(*pclient);
210         struct nouveau_device_priv *nvdev;
211         if (pcli) {
212                 int id = pcli->base.id;
213                 nvdev = nouveau_device(pcli->base.device);
214                 nvdev->client[id / 32] &= ~(1 << (id % 32));
215                 free(pcli->kref);
216                 free(pcli);
217         }
218 }
219
220 int
221 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
222                    uint32_t oclass, void *data, uint32_t length,
223                    struct nouveau_object **pobj)
224 {
225         struct nouveau_device *dev;
226         struct nouveau_object *obj;
227         int ret = -EINVAL;
228
229         if (length == 0)
230                 length = sizeof(struct nouveau_object *);
231         obj = malloc(sizeof(*obj) + length);
232         obj->parent = parent;
233         obj->handle = handle;
234         obj->oclass = oclass;
235         obj->length = length;
236         obj->data = obj + 1;
237         if (data)
238                 memcpy(obj->data, data, length);
239         *(struct nouveau_object **)obj->data = obj;
240
241         dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
242         switch (parent->oclass) {
243         case NOUVEAU_DEVICE_CLASS:
244                 switch (obj->oclass) {
245                 case NOUVEAU_FIFO_CHANNEL_CLASS:
246                 {
247                         if (dev->chipset < 0xc0)
248                                 ret = abi16_chan_nv04(obj);
249                         else
250                                 ret = abi16_chan_nvc0(obj);
251                 }
252                         break;
253                 default:
254                         break;
255                 }
256                 break;
257         case NOUVEAU_FIFO_CHANNEL_CLASS:
258                 switch (obj->oclass) {
259                 case NOUVEAU_NOTIFIER_CLASS:
260                         ret = abi16_ntfy(obj);
261                         break;
262                 default:
263                         ret = abi16_engobj(obj);
264                         break;
265                 }
266         default:
267                 break;
268         }
269
270         if (ret) {
271                 free(obj);
272                 return ret;
273         }
274
275         *pobj = obj;
276         return 0;
277 }
278
279 void
280 nouveau_object_del(struct nouveau_object **pobj)
281 {
282         struct nouveau_object *obj = *pobj;
283         struct nouveau_device *dev;
284         if (obj) {
285                 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
286                 if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
287                         struct drm_nouveau_channel_free req;
288                         req.channel = obj->handle;
289                         drmCommandWrite(dev->fd, DRM_NOUVEAU_CHANNEL_FREE,
290                                         &req, sizeof(req));
291                 } else {
292                         struct drm_nouveau_gpuobj_free req;
293                         req.channel = obj->parent->handle;
294                         req.handle  = obj->handle;
295                         drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
296                                         &req, sizeof(req));
297                 }
298         }
299         free(obj);
300         *pobj = NULL;
301 }
302
303 void *
304 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
305 {
306         while (obj && obj->oclass != pclass) {
307                 obj = obj->parent;
308                 if (pclass == NOUVEAU_PARENT_CLASS)
309                         break;
310         }
311         return obj;
312 }
313
314 static void
315 nouveau_bo_del(struct nouveau_bo *bo)
316 {
317         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
318         struct drm_gem_close req = { bo->handle };
319         DRMLISTDEL(&nvbo->head);
320         if (bo->map)
321                 munmap(bo->map, bo->size);
322         drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
323         free(nvbo);
324 }
325
326 int
327 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
328                uint64_t size, union nouveau_bo_config *config,
329                struct nouveau_bo **pbo)
330 {
331         struct nouveau_device_priv *nvdev = nouveau_device(dev);
332         struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
333         struct nouveau_bo *bo = &nvbo->base;
334         int ret;
335
336         if (!nvbo)
337                 return -ENOMEM;
338         atomic_set(&nvbo->refcnt, 1);
339         bo->device = dev;
340         bo->flags = flags;
341         bo->size = size;
342
343         ret = abi16_bo_init(bo, align, config);
344         if (ret) {
345                 free(nvbo);
346                 return ret;
347         }
348
349         DRMLISTADD(&nvbo->head, &nvdev->bo_list);
350
351         *pbo = bo;
352         return 0;
353 }
354
355 int
356 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
357                 struct nouveau_bo **pbo)
358 {
359         struct nouveau_device_priv *nvdev = nouveau_device(dev);
360         struct drm_nouveau_gem_info req = { .handle = handle };
361         struct nouveau_bo_priv *nvbo;
362         int ret;
363
364         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
365                 if (nvbo->base.handle == handle) {
366                         *pbo = NULL;
367                         nouveau_bo_ref(&nvbo->base, pbo);
368                         return 0;
369                 }
370         }
371
372         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
373                                   &req, sizeof(req));
374         if (ret)
375                 return ret;
376
377         nvbo = calloc(1, sizeof(*nvbo));
378         if (nvbo) {
379                 atomic_set(&nvbo->refcnt, 1);
380                 nvbo->base.device = dev;
381                 abi16_bo_info(&nvbo->base, &req);
382                 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
383                 *pbo = &nvbo->base;
384                 return 0;
385         }
386
387         return -ENOMEM;
388 }
389
390 int
391 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
392                     struct nouveau_bo **pbo)
393 {
394         struct nouveau_device_priv *nvdev = nouveau_device(dev);
395         struct nouveau_bo_priv *nvbo;
396         struct drm_gem_open req = { .name = name };
397         int ret;
398
399         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
400                 if (nvbo->name == name) {
401                         *pbo = NULL;
402                         nouveau_bo_ref(&nvbo->base, pbo);
403                         return 0;
404                 }
405         }
406
407         ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
408         if (ret == 0) {
409                 ret = nouveau_bo_wrap(dev, req.handle, pbo);
410                 nouveau_bo((*pbo))->name = name;
411         }
412
413         return ret;
414 }
415
416 int
417 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
418 {
419         struct drm_gem_flink req = { .handle = bo->handle };
420         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
421         if (!nvbo->name) {
422                 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
423                 if (ret)
424                         return ret;
425                 nvbo->name = req.name;
426         }
427         *name = nvbo->name;
428         return 0;
429 }
430
431 void
432 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
433 {
434         struct nouveau_bo *ref = *pref;
435         if (bo) {
436                 atomic_inc(&nouveau_bo(bo)->refcnt);
437         }
438         if (ref) {
439                 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
440                         nouveau_bo_del(ref);
441         }
442         *pref = bo;
443 }
444
445 int
446 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
447                             struct nouveau_bo **bo)
448 {
449         int ret;
450         unsigned int handle;
451
452         ret = drmPrimeFDToHandle(dev->fd, prime_fd, &handle);
453         if (ret) {
454                 nouveau_bo_ref(NULL, bo);
455                 return ret;
456         }
457
458         ret = nouveau_bo_wrap(dev, handle, bo);
459         if (ret) {
460                 nouveau_bo_ref(NULL, bo);
461                 return ret;
462         }
463
464         return 0;
465 }
466
467 int
468 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
469 {
470         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
471         int ret;
472
473         ret = drmPrimeHandleToFD(bo->device->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
474         if (ret)
475                 return ret;
476         return 0;
477 }
478
479 int
480 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
481                 struct nouveau_client *client)
482 {
483         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
484         struct drm_nouveau_gem_cpu_prep req;
485         struct nouveau_pushbuf *push;
486         int ret = 0;
487
488         if (!(access & NOUVEAU_BO_RDWR))
489                 return 0;
490
491         push = cli_push_get(client, bo);
492         if (push && push->channel)
493                 nouveau_pushbuf_kick(push, push->channel);
494
495         if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
496                            !(      access & NOUVEAU_BO_WR))
497                 return 0;
498
499         req.handle = bo->handle;
500         req.flags = 0;
501         if (access & NOUVEAU_BO_WR)
502                 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
503         if (access & NOUVEAU_BO_NOBLOCK)
504                 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
505
506         ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
507                               &req, sizeof(req));
508         if (ret == 0)
509                 nvbo->access = 0;
510         return ret;
511 }
512
513 int
514 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
515                struct nouveau_client *client)
516 {
517         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
518         if (bo->map == NULL) {
519                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
520                                MAP_SHARED, bo->device->fd, nvbo->map_handle);
521                 if (bo->map == MAP_FAILED) {
522                         bo->map = NULL;
523                         return -errno;
524                 }
525         }
526         return nouveau_bo_wait(bo, access, client);
527 }