]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - nouveau/nouveau.c
nouveau: pull in major libdrm rewrite
[glsdk/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  */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
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>
38 #include <xf86drm.h>
39 #include <xf86atomic.h>
40 #include "libdrm_lists.h"
41 #include "nouveau_drm.h"
43 #include "nouveau.h"
44 #include "private.h"
46 #ifdef DEBUG
47 uint32_t nouveau_debug = 0;
49 static void
50 debug_init(char *args)
51 {
52         if (args) {
53                 int n = strtol(args, NULL, 0);
54                 if (n >= 0)
55                         nouveau_debug = n;
56         }
57 }
58 #endif
60 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
61  * is kept here to prevent AIGLX from crashing if the DDX is linked against
62  * the new libdrm, but the DRI driver against the old
63  */
64 int
65 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
66                              drm_context_t ctx)
67 {
68         return -EACCES;
69 }
71 int
72 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
73 {
74         struct nouveau_device_priv *nvdev = calloc(1, sizeof(*nvdev));
75         struct nouveau_device *dev = &nvdev->base;
76         uint64_t chipset, vram, gart, bousage;
77         drmVersionPtr ver;
78         int ret;
80 #ifdef DEBUG
81         debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
82 #endif
84         if (!nvdev)
85                 return -ENOMEM;
86         nvdev->base.fd = fd;
88         ver = drmGetVersion(fd);
89         if (ver) dev->drm_version = (ver->version_major << 24) |
90                                     (ver->version_minor << 8) |
91                                      ver->version_patchlevel;
92         drmFreeVersion(ver);
94         if ( dev->drm_version != 0x00000010 &&
95             (dev->drm_version <  0x01000000 ||
96              dev->drm_version >= 0x02000000)) {
97                 nouveau_device_del(&dev);
98                 return -EINVAL;
99         }
101         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
102         if (ret == 0)
103         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
104         if (ret == 0)
105         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
106         if (ret) {
107                 nouveau_device_del(&dev);
108                 return ret;
109         }
111         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
112         if (ret == 0)
113                 nvdev->have_bo_usage = (bousage != 0);
115         nvdev->close = close;
116         DRMINITLISTHEAD(&nvdev->bo_list);
117         nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
118         nvdev->base.lib_version = 0x01000000;
119         nvdev->base.chipset = chipset;
120         nvdev->base.vram_size = vram;
121         nvdev->base.gart_size = gart;
122         nvdev->base.vram_limit = (nvdev->base.vram_size * 80) / 100;
123         nvdev->base.gart_limit = (nvdev->base.gart_size * 80) / 100;
125         *pdev = &nvdev->base;
126         return 0;
129 int
130 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
132         int ret = -ENODEV, fd = drmOpen("nouveau", busid);
133         if (fd >= 0) {
134                 ret = nouveau_device_wrap(fd, 1, pdev);
135                 if (ret)
136                         drmClose(fd);
137         }
138         return ret;
141 void
142 nouveau_device_del(struct nouveau_device **pdev)
144         struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
145         if (nvdev) {
146                 if (nvdev->close)
147                         drmClose(nvdev->base.fd);
148                 free(nvdev->client);
149                 free(nvdev);
150                 *pdev = NULL;
151         }
154 int
155 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
157         struct drm_nouveau_getparam r = { param, 0 };
158         int fd = dev->fd, ret =
159                 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
160         *value = r.value;
161         return ret;
164 int
165 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
167         struct drm_nouveau_setparam r = { param, value };
168         return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
171 int
172 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
174         struct nouveau_device_priv *nvdev = nouveau_device(dev);
175         struct nouveau_client_priv *pcli;
176         int id = 0, i, ret = -ENOMEM;
177         uint32_t *clients;
179         for (i = 0; i < nvdev->nr_client; i++) {
180                 id = ffs(nvdev->client[i]) - 1;
181                 if (id >= 0)
182                         goto out;
183         }
185         clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
186         if (!clients)
187                 return ret;
188         nvdev->client = clients;
189         nvdev->client[i] = 0;
190         nvdev->nr_client++;
192 out:
193         pcli = calloc(1, sizeof(*pcli));
194         if (pcli) {
195                 nvdev->client[i] |= (1 << id);
196                 pcli->base.device = dev;
197                 pcli->base.id = (i * 32) + id;
198                 ret = 0;
199         }
201         *pclient = &pcli->base;
202         return ret;
205 void
206 nouveau_client_del(struct nouveau_client **pclient)
208         struct nouveau_client_priv *pcli = nouveau_client(*pclient);
209         struct nouveau_device_priv *nvdev;
210         if (pcli) {
211                 int id = pcli->base.id;
212                 nvdev = nouveau_device(pcli->base.device);
213                 nvdev->client[id / 32] &= ~(1 << (id % 32));
214                 free(pcli->kref);
215                 free(pcli);
216         }
219 int
220 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
221                    uint32_t oclass, void *data, uint32_t length,
222                    struct nouveau_object **pobj)
224         struct nouveau_device *dev;
225         struct nouveau_object *obj;
226         int ret = -EINVAL;
228         if (length == 0)
229                 length = sizeof(struct nouveau_object *);
230         obj = malloc(sizeof(*obj) + length);
231         obj->parent = parent;
232         obj->handle = handle;
233         obj->oclass = oclass;
234         obj->length = length;
235         obj->data = obj + 1;
236         if (data)
237                 memcpy(obj->data, data, length);
238         *(struct nouveau_object **)obj->data = obj;
240         dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
241         switch (parent->oclass) {
242         case NOUVEAU_DEVICE_CLASS:
243                 switch (obj->oclass) {
244                 case NOUVEAU_FIFO_CHANNEL_CLASS:
245                 {
246                         if (dev->chipset < 0xc0)
247                                 ret = abi16_chan_nv04(obj);
248                         else
249                                 ret = abi16_chan_nvc0(obj);
250                 }
251                         break;
252                 default:
253                         break;
254                 }
255                 break;
256         case NOUVEAU_FIFO_CHANNEL_CLASS:
257                 switch (obj->oclass) {
258                 case NOUVEAU_NOTIFIER_CLASS:
259                         ret = abi16_ntfy(obj);
260                         break;
261                 default:
262                         ret = abi16_engobj(obj);
263                         break;
264                 }
265         default:
266                 break;
267         }
269         if (ret) {
270                 free(obj);
271                 return ret;
272         }
274         *pobj = obj;
275         return 0;
278 void
279 nouveau_object_del(struct nouveau_object **pobj)
281         struct drm_nouveau_gpuobj_free req;
282         struct nouveau_object *obj = *pobj;
283         struct nouveau_device *dev;
284         if (obj) {
285                 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
286                 req.channel = obj->parent->handle;
287                 req.handle  = obj->handle;
288                 drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
289                                 &req, sizeof(req));
290         }
291         free(obj);
292         *pobj = NULL;
295 void *
296 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
298         while (obj && obj->oclass != pclass) {
299                 obj = obj->parent;
300                 if (pclass == NOUVEAU_PARENT_CLASS)
301                         break;
302         }
303         return obj;
306 static void
307 nouveau_bo_del(struct nouveau_bo *bo)
309         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
310         struct drm_gem_close req = { bo->handle };
311         DRMLISTDEL(&nvbo->head);
312         if (bo->map)
313                 munmap(bo->map, bo->size);
314         drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
315         free(nvbo);
318 int
319 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
320                uint64_t size, union nouveau_bo_config *config,
321                struct nouveau_bo **pbo)
323         struct nouveau_device_priv *nvdev = nouveau_device(dev);
324         struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
325         struct nouveau_bo *bo = &nvbo->base;
326         int ret;
328         if (!nvbo)
329                 return -ENOMEM;
330         atomic_set(&nvbo->refcnt, 1);
331         bo->device = dev;
332         bo->flags = flags;
333         bo->size = size;
335         ret = abi16_bo_init(bo, align, config);
336         if (ret) {
337                 free(nvbo);
338                 return ret;
339         }
341         DRMLISTADD(&nvbo->head, &nvdev->bo_list);
343         *pbo = bo;
344         return 0;
347 int
348 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
349                 struct nouveau_bo **pbo)
351         struct nouveau_device_priv *nvdev = nouveau_device(dev);
352         struct drm_nouveau_gem_info req = { .handle = handle };
353         struct nouveau_bo_priv *nvbo;
354         int ret;
356         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
357                 if (nvbo->base.handle == handle) {
358                         *pbo = NULL;
359                         nouveau_bo_ref(&nvbo->base, pbo);
360                         return 0;
361                 }
362         }
364         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
365                                   &req, sizeof(req));
366         if (ret)
367                 return ret;
369         nvbo = calloc(1, sizeof(*nvbo));
370         if (nvbo) {
371                 atomic_set(&nvbo->refcnt, 1);
372                 nvbo->base.device = dev;
373                 abi16_bo_info(&nvbo->base, &req);
374                 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
375                 *pbo = &nvbo->base;
376                 return 0;
377         }
379         return -ENOMEM;
382 int
383 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
384                     struct nouveau_bo **pbo)
386         struct nouveau_device_priv *nvdev = nouveau_device(dev);
387         struct nouveau_bo_priv *nvbo;
388         struct drm_gem_open req = { .name = name };
389         int ret;
391         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
392                 if (nvbo->name == name) {
393                         *pbo = NULL;
394                         nouveau_bo_ref(&nvbo->base, pbo);
395                         return 0;
396                 }
397         }
399         ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
400         if (ret == 0) {
401                 ret = nouveau_bo_wrap(dev, req.handle, pbo);
402                 nouveau_bo((*pbo))->name = name;
403         }
405         return ret;
408 int
409 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
411         struct drm_gem_flink req = { .handle = bo->handle };
412         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
413         if (!nvbo->name) {
414                 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
415                 if (ret)
416                         return ret;
417                 nvbo->name = req.name;
418         }
419         *name = nvbo->name;
420         return 0;
423 void
424 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
426         struct nouveau_bo *ref = *pref;
427         if (bo) {
428                 atomic_inc(&nouveau_bo(bo)->refcnt);
429         }
430         if (ref) {
431                 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
432                         nouveau_bo_del(ref);
433         }
434         *pref = bo;
437 int
438 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
439                 struct nouveau_client *client)
441         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
442         struct drm_nouveau_gem_cpu_prep req;
443         struct nouveau_pushbuf *push;
444         int ret = 0;
446         if (!(access & NOUVEAU_BO_RDWR))
447                 return 0;
449         push = cli_push_get(client, bo);
450         if (push && push->channel)
451                 nouveau_pushbuf_kick(push, push->channel);
453         if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
454                            !(      access & NOUVEAU_BO_WR))
455                 return 0;
457         req.handle = bo->handle;
458         req.flags = 0;
459         if (access & NOUVEAU_BO_WR)
460                 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
461         if (access & NOUVEAU_BO_NOBLOCK)
462                 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
464         do {
465                 ret = drmCommandWrite(bo->device->fd,
466                                       DRM_NOUVEAU_GEM_CPU_PREP,
467                                       &req, sizeof(req));
468         } while (ret == -EAGAIN);
470         if (ret == 0)
471                 nvbo->access = 0;
472         return ret;
475 int
476 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
477                struct nouveau_client *client)
479         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
480         if (bo->map == NULL) {
481                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
482                                MAP_SHARED, bo->device->fd, nvbo->map_handle);
483                 if (bo->map == MAP_FAILED) {
484                         bo->map = NULL;
485                         return -errno;
486                 }
487         }
488         return nouveau_bo_wait(bo, access, client);