2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
40 #include <xf86atomic.h>
41 #include "libdrm_lists.h"
42 #include "nouveau_drm.h"
48 uint32_t nouveau_debug = 0;
51 debug_init(char *args)
54 int n = strtol(args, NULL, 0);
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
66 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
73 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
75 struct nouveau_device_priv *nvdev = calloc(1, sizeof(*nvdev));
76 struct nouveau_device *dev = &nvdev->base;
77 uint64_t chipset, vram, gart, bousage;
83 debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
88 ret = pthread_mutex_init(&nvdev->lock, NULL);
96 ver = drmGetVersion(fd);
97 if (ver) dev->drm_version = (ver->version_major << 24) |
98 (ver->version_minor << 8) |
99 ver->version_patchlevel;
102 if ( dev->drm_version != 0x00000010 &&
103 (dev->drm_version < 0x01000000 ||
104 dev->drm_version >= 0x02000000)) {
105 nouveau_device_del(&dev);
109 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
111 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
113 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
115 nouveau_device_del(&dev);
119 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
121 nvdev->have_bo_usage = (bousage != 0);
123 nvdev->close = close;
125 tmp = getenv("NOUVEAU_LIBDRM_VRAM_LIMIT_PERCENT");
127 nvdev->vram_limit_percent = atoi(tmp);
129 nvdev->vram_limit_percent = 80;
130 tmp = getenv("NOUVEAU_LIBDRM_GART_LIMIT_PERCENT");
132 nvdev->gart_limit_percent = atoi(tmp);
134 nvdev->gart_limit_percent = 80;
135 DRMINITLISTHEAD(&nvdev->bo_list);
136 nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
137 nvdev->base.lib_version = 0x01000000;
138 nvdev->base.chipset = chipset;
139 nvdev->base.vram_size = vram;
140 nvdev->base.gart_size = gart;
141 nvdev->base.vram_limit =
142 (nvdev->base.vram_size * nvdev->vram_limit_percent) / 100;
143 nvdev->base.gart_limit =
144 (nvdev->base.gart_size * nvdev->gart_limit_percent) / 100;
146 *pdev = &nvdev->base;
151 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
153 int ret = -ENODEV, fd = drmOpen("nouveau", busid);
155 ret = nouveau_device_wrap(fd, 1, pdev);
163 nouveau_device_del(struct nouveau_device **pdev)
165 struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
168 drmClose(nvdev->base.fd);
170 pthread_mutex_destroy(&nvdev->lock);
177 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
179 struct drm_nouveau_getparam r = { param, 0 };
180 int fd = dev->fd, ret =
181 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
187 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
189 struct drm_nouveau_setparam r = { param, value };
190 return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
194 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
196 struct nouveau_device_priv *nvdev = nouveau_device(dev);
197 struct nouveau_client_priv *pcli;
198 int id = 0, i, ret = -ENOMEM;
201 pthread_mutex_lock(&nvdev->lock);
203 for (i = 0; i < nvdev->nr_client; i++) {
204 id = ffs(nvdev->client[i]) - 1;
209 clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
212 nvdev->client = clients;
213 nvdev->client[i] = 0;
217 pcli = calloc(1, sizeof(*pcli));
219 nvdev->client[i] |= (1 << id);
220 pcli->base.device = dev;
221 pcli->base.id = (i * 32) + id;
225 *pclient = &pcli->base;
228 pthread_mutex_unlock(&nvdev->lock);
233 nouveau_client_del(struct nouveau_client **pclient)
235 struct nouveau_client_priv *pcli = nouveau_client(*pclient);
236 struct nouveau_device_priv *nvdev;
238 int id = pcli->base.id;
239 nvdev = nouveau_device(pcli->base.device);
240 pthread_mutex_lock(&nvdev->lock);
241 nvdev->client[id / 32] &= ~(1 << (id % 32));
242 pthread_mutex_unlock(&nvdev->lock);
249 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
250 uint32_t oclass, void *data, uint32_t length,
251 struct nouveau_object **pobj)
253 struct nouveau_device *dev;
254 struct nouveau_object *obj;
258 length = sizeof(struct nouveau_object *);
259 obj = malloc(sizeof(*obj) + length);
260 obj->parent = parent;
261 obj->handle = handle;
262 obj->oclass = oclass;
263 obj->length = length;
266 memcpy(obj->data, data, length);
267 *(struct nouveau_object **)obj->data = obj;
269 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
270 switch (parent->oclass) {
271 case NOUVEAU_DEVICE_CLASS:
272 switch (obj->oclass) {
273 case NOUVEAU_FIFO_CHANNEL_CLASS:
275 if (dev->chipset < 0xc0)
276 ret = abi16_chan_nv04(obj);
278 if (dev->chipset < 0xe0)
279 ret = abi16_chan_nvc0(obj);
281 ret = abi16_chan_nve0(obj);
288 case NOUVEAU_FIFO_CHANNEL_CLASS:
289 switch (obj->oclass) {
290 case NOUVEAU_NOTIFIER_CLASS:
291 ret = abi16_ntfy(obj);
294 ret = abi16_engobj(obj);
311 nouveau_object_del(struct nouveau_object **pobj)
313 struct nouveau_object *obj = *pobj;
314 struct nouveau_device *dev;
316 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
317 if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
318 struct drm_nouveau_channel_free req;
319 req.channel = obj->handle;
320 drmCommandWrite(dev->fd, DRM_NOUVEAU_CHANNEL_FREE,
323 struct drm_nouveau_gpuobj_free req;
324 req.channel = obj->parent->handle;
325 req.handle = obj->handle;
326 drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
335 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
337 while (obj && obj->oclass != pclass) {
339 if (pclass == NOUVEAU_PARENT_CLASS)
346 nouveau_bo_del(struct nouveau_bo *bo)
348 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
349 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
350 struct drm_gem_close req = { bo->handle };
352 pthread_mutex_lock(&nvdev->lock);
354 if (atomic_read(&nvbo->refcnt)) {
356 * bo has been revived by a race with
357 * nouveau_bo_prime_handle_ref, or nouveau_bo_name_ref.
359 * In theory there's still a race possible with
360 * nouveau_bo_wrap, but when using this function
361 * the lifetime of the handle is probably already
362 * handled in another way. If there are races
363 * you're probably using nouveau_bo_wrap wrong.
365 pthread_mutex_unlock(&nvdev->lock);
368 DRMLISTDEL(&nvbo->head);
370 * This bo has to be closed with the lock held because gem
371 * handles are not refcounted. If a shared bo is closed and
372 * re-opened in another thread a race against
373 * DRM_IOCTL_GEM_OPEN or drmPrimeFDToHandle might cause the
374 * bo to be closed accidentally while re-importing.
376 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
377 pthread_mutex_unlock(&nvdev->lock);
379 DRMLISTDEL(&nvbo->head);
380 pthread_mutex_unlock(&nvdev->lock);
381 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
384 munmap(bo->map, bo->size);
389 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
390 uint64_t size, union nouveau_bo_config *config,
391 struct nouveau_bo **pbo)
393 struct nouveau_device_priv *nvdev = nouveau_device(dev);
394 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
395 struct nouveau_bo *bo = &nvbo->base;
400 atomic_set(&nvbo->refcnt, 1);
405 ret = abi16_bo_init(bo, align, config);
411 pthread_mutex_lock(&nvdev->lock);
412 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
413 pthread_mutex_unlock(&nvdev->lock);
420 nouveau_bo_wrap_locked(struct nouveau_device *dev, uint32_t handle,
421 struct nouveau_bo **pbo)
423 struct nouveau_device_priv *nvdev = nouveau_device(dev);
424 struct drm_nouveau_gem_info req = { .handle = handle };
425 struct nouveau_bo_priv *nvbo;
428 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
429 if (nvbo->base.handle == handle) {
431 nouveau_bo_ref(&nvbo->base, pbo);
436 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
441 nvbo = calloc(1, sizeof(*nvbo));
443 atomic_set(&nvbo->refcnt, 1);
444 nvbo->base.device = dev;
445 abi16_bo_info(&nvbo->base, &req);
446 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
455 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
456 struct nouveau_bo **pbo)
458 struct nouveau_device_priv *nvdev = nouveau_device(dev);
460 pthread_mutex_lock(&nvdev->lock);
461 ret = nouveau_bo_wrap_locked(dev, handle, pbo);
462 pthread_mutex_unlock(&nvdev->lock);
467 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
468 struct nouveau_bo **pbo)
470 struct nouveau_device_priv *nvdev = nouveau_device(dev);
471 struct nouveau_bo_priv *nvbo;
472 struct drm_gem_open req = { .name = name };
475 pthread_mutex_lock(&nvdev->lock);
476 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
477 if (nvbo->name == name) {
479 nouveau_bo_ref(&nvbo->base, pbo);
480 pthread_mutex_unlock(&nvdev->lock);
485 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
487 ret = nouveau_bo_wrap_locked(dev, req.handle, pbo);
488 nouveau_bo((*pbo))->name = name;
489 pthread_mutex_unlock(&nvdev->lock);
496 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
498 struct drm_gem_flink req = { .handle = bo->handle };
499 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
502 if (!*name || *name == ~0U) {
503 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
508 nvbo->name = *name = req.name;
514 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
516 struct nouveau_bo *ref = *pref;
518 atomic_inc(&nouveau_bo(bo)->refcnt);
521 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
528 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
529 struct nouveau_bo **bo)
531 struct nouveau_device_priv *nvdev = nouveau_device(dev);
535 nouveau_bo_ref(NULL, bo);
537 pthread_mutex_lock(&nvdev->lock);
538 ret = drmPrimeFDToHandle(dev->fd, prime_fd, &handle);
540 ret = nouveau_bo_wrap_locked(dev, handle, bo);
542 struct nouveau_bo_priv *nvbo = nouveau_bo(*bo);
545 * XXX: Force locked DRM_IOCTL_GEM_CLOSE
546 * to rule out race conditions
552 pthread_mutex_unlock(&nvdev->lock);
557 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
559 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
562 ret = drmPrimeHandleToFD(bo->device->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
571 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
572 struct nouveau_client *client)
574 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
575 struct drm_nouveau_gem_cpu_prep req;
576 struct nouveau_pushbuf *push;
579 if (!(access & NOUVEAU_BO_RDWR))
582 push = cli_push_get(client, bo);
583 if (push && push->channel)
584 nouveau_pushbuf_kick(push, push->channel);
586 if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
587 !( access & NOUVEAU_BO_WR))
590 req.handle = bo->handle;
592 if (access & NOUVEAU_BO_WR)
593 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
594 if (access & NOUVEAU_BO_NOBLOCK)
595 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
597 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
605 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
606 struct nouveau_client *client)
608 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
609 if (bo->map == NULL) {
610 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
611 MAP_SHARED, bo->device->fd, nvbo->map_handle);
612 if (bo->map == MAP_FAILED) {
617 return nouveau_bo_wait(bo, access, client);