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_macros.h"
42 #include "libdrm_lists.h"
43 #include "nouveau_drm.h"
48 #include "nvif/class.h"
49 #include "nvif/cl0080.h"
50 #include "nvif/unpack.h"
53 drm_private uint32_t nouveau_debug = 0;
56 debug_init(char *args)
59 int n = strtol(args, NULL, 0);
67 nouveau_object_mthd(struct nouveau_object *obj,
68 uint32_t mthd, void *data, uint32_t size)
74 nouveau_object_sclass_put(struct nouveau_sclass **psclass)
81 nouveau_object_sclass_get(struct nouveau_object *obj,
82 struct nouveau_sclass **psclass)
84 return abi16_sclass(obj, psclass);
88 nouveau_object_mclass(struct nouveau_object *obj,
89 const struct nouveau_mclass *mclass)
91 struct nouveau_sclass *sclass;
95 cnt = nouveau_object_sclass_get(obj, &sclass);
99 for (i = 0; ret < 0 && mclass[i].oclass; i++) {
100 for (j = 0; j < cnt; j++) {
101 if (mclass[i].oclass == sclass[j].oclass &&
102 mclass[i].version >= sclass[j].minver &&
103 mclass[i].version <= sclass[j].maxver) {
110 nouveau_object_sclass_put(&sclass);
115 nouveau_object_fini(struct nouveau_object *obj)
126 nouveau_object_init(struct nouveau_object *parent, uint32_t handle,
127 int32_t oclass, void *data, uint32_t size,
128 struct nouveau_object *obj)
130 int (*func)(struct nouveau_object *);
133 obj->parent = parent;
134 obj->handle = handle;
135 obj->oclass = oclass;
139 abi16_object(obj, &func);
141 obj->length = size ? size : sizeof(struct nouveau_object *);
142 if (!(obj->data = malloc(obj->length)))
145 memcpy(obj->data, data, obj->length);
146 *(struct nouveau_object **)obj->data = obj;
152 nouveau_object_fini(obj);
160 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
161 uint32_t oclass, void *data, uint32_t length,
162 struct nouveau_object **pobj)
164 struct nouveau_object *obj;
167 if (!(obj = malloc(sizeof(*obj))))
170 ret = nouveau_object_init(parent, handle, oclass, data, length, obj);
181 nouveau_object_del(struct nouveau_object **pobj)
183 struct nouveau_object *obj = *pobj;
185 nouveau_object_fini(obj);
192 nouveau_drm_del(struct nouveau_drm **pdrm)
199 nouveau_drm_new(int fd, struct nouveau_drm **pdrm)
201 struct nouveau_drm *drm;
205 debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
208 if (!(drm = calloc(1, sizeof(*drm))))
212 if (!(ver = drmGetVersion(fd))) {
213 nouveau_drm_del(&drm);
218 drm->version = (ver->version_major << 24) |
219 (ver->version_minor << 8) |
220 ver->version_patchlevel;
226 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
227 * is kept here to prevent AIGLX from crashing if the DDX is linked against
228 * the new libdrm, but the DRI driver against the old
231 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
238 nouveau_device_new(struct nouveau_object *parent, int32_t oclass,
239 void *data, uint32_t size, struct nouveau_device **pdev)
242 struct nv_device_v0 v0;
244 struct nouveau_drm *drm = nouveau_drm(parent);
245 struct nouveau_device_priv *nvdev;
246 struct nouveau_device *dev;
251 if (oclass != NV_DEVICE ||
252 nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))
255 if (!(nvdev = calloc(1, sizeof(*nvdev))))
257 dev = *pdev = &nvdev->base;
260 if (args->v0.device == ~0ULL) {
261 nvdev->base.object.parent = &drm->client;
262 nvdev->base.object.handle = ~0ULL;
263 nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
264 nvdev->base.object.length = ~0;
266 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &v);
269 nvdev->base.chipset = v;
271 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &v);
273 nvdev->have_bo_usage = (v != 0);
277 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &v);
280 nvdev->base.vram_size = v;
282 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &v);
285 nvdev->base.gart_size = v;
287 tmp = getenv("NOUVEAU_LIBDRM_VRAM_LIMIT_PERCENT");
289 nvdev->vram_limit_percent = atoi(tmp);
291 nvdev->vram_limit_percent = 80;
293 nvdev->base.vram_limit =
294 (nvdev->base.vram_size * nvdev->vram_limit_percent) / 100;
296 tmp = getenv("NOUVEAU_LIBDRM_GART_LIMIT_PERCENT");
298 nvdev->gart_limit_percent = atoi(tmp);
300 nvdev->gart_limit_percent = 80;
302 nvdev->base.gart_limit =
303 (nvdev->base.gart_size * nvdev->gart_limit_percent) / 100;
305 ret = pthread_mutex_init(&nvdev->lock, NULL);
306 DRMINITLISTHEAD(&nvdev->bo_list);
309 nouveau_device_del(pdev);
314 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
316 struct nouveau_drm *drm;
317 struct nouveau_device_priv *nvdev;
320 ret = nouveau_drm_new(fd, &drm);
325 ret = nouveau_device_new(&drm->client, NV_DEVICE,
326 &(struct nv_device_v0) {
328 }, sizeof(struct nv_device_v0), pdev);
330 nouveau_drm_del(&drm);
334 nvdev = nouveau_device(*pdev);
335 nvdev->base.fd = drm->fd;
336 nvdev->base.drm_version = drm->drm_version;
337 nvdev->base.lib_version = drm->lib_version;
338 nvdev->close = close;
343 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
345 int ret = -ENODEV, fd = drmOpen("nouveau", busid);
347 ret = nouveau_device_wrap(fd, 1, pdev);
355 nouveau_device_del(struct nouveau_device **pdev)
357 struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
360 pthread_mutex_destroy(&nvdev->lock);
361 if (nvdev->base.fd >= 0) {
362 struct nouveau_drm *drm =
363 nouveau_drm(&nvdev->base.object);
364 nouveau_drm_del(&drm);
366 drmClose(nvdev->base.fd);
374 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
376 struct nouveau_drm *drm = nouveau_drm(&dev->object);
377 struct drm_nouveau_getparam r = { .param = param };
378 int fd = drm->fd, ret =
379 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
385 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
387 struct nouveau_drm *drm = nouveau_drm(&dev->object);
388 struct drm_nouveau_setparam r = { .param = param, .value = value };
389 return drmCommandWrite(drm->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
393 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
395 struct nouveau_device_priv *nvdev = nouveau_device(dev);
396 struct nouveau_client_priv *pcli;
397 int id = 0, i, ret = -ENOMEM;
400 pthread_mutex_lock(&nvdev->lock);
402 for (i = 0; i < nvdev->nr_client; i++) {
403 id = ffs(nvdev->client[i]) - 1;
408 clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
411 nvdev->client = clients;
412 nvdev->client[i] = 0;
416 pcli = calloc(1, sizeof(*pcli));
418 nvdev->client[i] |= (1 << id);
419 pcli->base.device = dev;
420 pcli->base.id = (i * 32) + id;
424 *pclient = &pcli->base;
427 pthread_mutex_unlock(&nvdev->lock);
432 nouveau_client_del(struct nouveau_client **pclient)
434 struct nouveau_client_priv *pcli = nouveau_client(*pclient);
435 struct nouveau_device_priv *nvdev;
437 int id = pcli->base.id;
438 nvdev = nouveau_device(pcli->base.device);
439 pthread_mutex_lock(&nvdev->lock);
440 nvdev->client[id / 32] &= ~(1 << (id % 32));
441 pthread_mutex_unlock(&nvdev->lock);
448 nouveau_bo_del(struct nouveau_bo *bo)
450 struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
451 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
452 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
453 struct drm_gem_close req = { .handle = bo->handle };
455 if (nvbo->head.next) {
456 pthread_mutex_lock(&nvdev->lock);
457 if (atomic_read(&nvbo->refcnt) == 0) {
458 DRMLISTDEL(&nvbo->head);
460 * This bo has to be closed with the lock held because
461 * gem handles are not refcounted. If a shared bo is
462 * closed and re-opened in another thread a race
463 * against DRM_IOCTL_GEM_OPEN or drmPrimeFDToHandle
464 * might cause the bo to be closed accidentally while
467 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &req);
469 pthread_mutex_unlock(&nvdev->lock);
471 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &req);
474 drm_munmap(bo->map, bo->size);
479 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
480 uint64_t size, union nouveau_bo_config *config,
481 struct nouveau_bo **pbo)
483 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
484 struct nouveau_bo *bo = &nvbo->base;
489 atomic_set(&nvbo->refcnt, 1);
494 ret = abi16_bo_init(bo, align, config);
505 nouveau_bo_wrap_locked(struct nouveau_device *dev, uint32_t handle,
506 struct nouveau_bo **pbo, int name)
508 struct nouveau_drm *drm = nouveau_drm(&dev->object);
509 struct nouveau_device_priv *nvdev = nouveau_device(dev);
510 struct drm_nouveau_gem_info req = { .handle = handle };
511 struct nouveau_bo_priv *nvbo;
514 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
515 if (nvbo->base.handle == handle) {
516 if (atomic_inc_return(&nvbo->refcnt) == 1) {
518 * Uh oh, this bo is dead and someone else
519 * will free it, but because refcnt is
520 * now non-zero fortunately they won't
521 * call the ioctl to close the bo.
523 * Remove this bo from the list so other
524 * calls to nouveau_bo_wrap_locked will
525 * see our replacement nvbo.
527 DRMLISTDEL(&nvbo->head);
538 ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_GEM_INFO,
543 nvbo = calloc(1, sizeof(*nvbo));
545 atomic_set(&nvbo->refcnt, 1);
546 nvbo->base.device = dev;
547 abi16_bo_info(&nvbo->base, &req);
549 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
558 nouveau_bo_make_global(struct nouveau_bo_priv *nvbo)
560 if (!nvbo->head.next) {
561 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
562 pthread_mutex_lock(&nvdev->lock);
563 if (!nvbo->head.next)
564 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
565 pthread_mutex_unlock(&nvdev->lock);
570 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
571 struct nouveau_bo **pbo)
573 struct nouveau_device_priv *nvdev = nouveau_device(dev);
575 pthread_mutex_lock(&nvdev->lock);
576 ret = nouveau_bo_wrap_locked(dev, handle, pbo, 0);
577 pthread_mutex_unlock(&nvdev->lock);
582 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
583 struct nouveau_bo **pbo)
585 struct nouveau_drm *drm = nouveau_drm(&dev->object);
586 struct nouveau_device_priv *nvdev = nouveau_device(dev);
587 struct nouveau_bo_priv *nvbo;
588 struct drm_gem_open req = { .name = name };
591 pthread_mutex_lock(&nvdev->lock);
592 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
593 if (nvbo->name == name) {
594 ret = nouveau_bo_wrap_locked(dev, nvbo->base.handle,
596 pthread_mutex_unlock(&nvdev->lock);
601 ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_OPEN, &req);
603 ret = nouveau_bo_wrap_locked(dev, req.handle, pbo, name);
606 pthread_mutex_unlock(&nvdev->lock);
611 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
613 struct drm_gem_flink req = { .handle = bo->handle };
614 struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
615 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
619 int ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_FLINK, &req);
625 nvbo->name = *name = req.name;
627 nouveau_bo_make_global(nvbo);
633 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
635 struct nouveau_bo *ref = *pref;
637 atomic_inc(&nouveau_bo(bo)->refcnt);
640 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
647 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
648 struct nouveau_bo **bo)
650 struct nouveau_drm *drm = nouveau_drm(&dev->object);
651 struct nouveau_device_priv *nvdev = nouveau_device(dev);
655 nouveau_bo_ref(NULL, bo);
657 pthread_mutex_lock(&nvdev->lock);
658 ret = drmPrimeFDToHandle(drm->fd, prime_fd, &handle);
660 ret = nouveau_bo_wrap_locked(dev, handle, bo, 0);
662 pthread_mutex_unlock(&nvdev->lock);
667 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
669 struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
670 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
673 ret = drmPrimeHandleToFD(drm->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
677 nouveau_bo_make_global(nvbo);
682 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
683 struct nouveau_client *client)
685 struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
686 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
687 struct drm_nouveau_gem_cpu_prep req;
688 struct nouveau_pushbuf *push;
691 if (!(access & NOUVEAU_BO_RDWR))
694 push = cli_push_get(client, bo);
695 if (push && push->channel)
696 nouveau_pushbuf_kick(push, push->channel);
698 if (!nvbo->head.next && !(nvbo->access & NOUVEAU_BO_WR) &&
699 !(access & NOUVEAU_BO_WR))
702 req.handle = bo->handle;
704 if (access & NOUVEAU_BO_WR)
705 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
706 if (access & NOUVEAU_BO_NOBLOCK)
707 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
709 ret = drmCommandWrite(drm->fd, DRM_NOUVEAU_GEM_CPU_PREP,
717 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
718 struct nouveau_client *client)
720 struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
721 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
722 if (bo->map == NULL) {
723 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
724 MAP_SHARED, drm->fd, nvbo->map_handle);
725 if (bo->map == MAP_FAILED) {
730 return nouveau_bo_wait(bo, access, client);