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"
49 drm_private uint32_t nouveau_debug = 0;
52 debug_init(char *args)
55 int n = strtol(args, NULL, 0);
63 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
64 uint32_t oclass, void *data, uint32_t length,
65 struct nouveau_object **pobj)
67 struct nouveau_object *obj;
68 int (*func)(struct nouveau_object *);
72 length = sizeof(struct nouveau_object *);
73 obj = malloc(sizeof(*obj) + length);
80 memcpy(obj->data, data, length);
81 *(struct nouveau_object **)obj->data = obj;
83 abi16_object(obj, &func);
97 nouveau_object_del(struct nouveau_object **pobj)
99 struct nouveau_object *obj = *pobj;
108 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
110 while (obj && obj->oclass != pclass) {
112 if (pclass == NOUVEAU_PARENT_CLASS)
118 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
119 * is kept here to prevent AIGLX from crashing if the DDX is linked against
120 * the new libdrm, but the DRI driver against the old
123 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
130 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
132 struct nouveau_device_priv *nvdev = calloc(1, sizeof(*nvdev));
133 struct nouveau_device *dev = &nvdev->base;
134 uint64_t chipset, vram, gart, bousage;
140 debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
145 ret = pthread_mutex_init(&nvdev->lock, NULL);
153 ver = drmGetVersion(fd);
154 if (ver) dev->drm_version = (ver->version_major << 24) |
155 (ver->version_minor << 8) |
156 ver->version_patchlevel;
159 if ( dev->drm_version != 0x00000010 &&
160 (dev->drm_version < 0x01000000 ||
161 dev->drm_version >= 0x02000000)) {
162 nouveau_device_del(&dev);
166 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
168 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
170 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
172 nouveau_device_del(&dev);
176 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
178 nvdev->have_bo_usage = (bousage != 0);
180 nvdev->close = close;
182 tmp = getenv("NOUVEAU_LIBDRM_VRAM_LIMIT_PERCENT");
184 nvdev->vram_limit_percent = atoi(tmp);
186 nvdev->vram_limit_percent = 80;
187 tmp = getenv("NOUVEAU_LIBDRM_GART_LIMIT_PERCENT");
189 nvdev->gart_limit_percent = atoi(tmp);
191 nvdev->gart_limit_percent = 80;
192 DRMINITLISTHEAD(&nvdev->bo_list);
193 nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
194 nvdev->base.object.length = ~0;
195 nvdev->base.lib_version = 0x01000000;
196 nvdev->base.chipset = chipset;
197 nvdev->base.vram_size = vram;
198 nvdev->base.gart_size = gart;
199 nvdev->base.vram_limit =
200 (nvdev->base.vram_size * nvdev->vram_limit_percent) / 100;
201 nvdev->base.gart_limit =
202 (nvdev->base.gart_size * nvdev->gart_limit_percent) / 100;
204 *pdev = &nvdev->base;
209 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
211 int ret = -ENODEV, fd = drmOpen("nouveau", busid);
213 ret = nouveau_device_wrap(fd, 1, pdev);
221 nouveau_device_del(struct nouveau_device **pdev)
223 struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
226 drmClose(nvdev->base.fd);
228 pthread_mutex_destroy(&nvdev->lock);
235 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
237 struct drm_nouveau_getparam r = { .param = param };
238 int fd = dev->fd, ret =
239 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
245 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
247 struct drm_nouveau_setparam r = { .param = param, .value = value };
248 return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
252 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
254 struct nouveau_device_priv *nvdev = nouveau_device(dev);
255 struct nouveau_client_priv *pcli;
256 int id = 0, i, ret = -ENOMEM;
259 pthread_mutex_lock(&nvdev->lock);
261 for (i = 0; i < nvdev->nr_client; i++) {
262 id = ffs(nvdev->client[i]) - 1;
267 clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
270 nvdev->client = clients;
271 nvdev->client[i] = 0;
275 pcli = calloc(1, sizeof(*pcli));
277 nvdev->client[i] |= (1 << id);
278 pcli->base.device = dev;
279 pcli->base.id = (i * 32) + id;
283 *pclient = &pcli->base;
286 pthread_mutex_unlock(&nvdev->lock);
291 nouveau_client_del(struct nouveau_client **pclient)
293 struct nouveau_client_priv *pcli = nouveau_client(*pclient);
294 struct nouveau_device_priv *nvdev;
296 int id = pcli->base.id;
297 nvdev = nouveau_device(pcli->base.device);
298 pthread_mutex_lock(&nvdev->lock);
299 nvdev->client[id / 32] &= ~(1 << (id % 32));
300 pthread_mutex_unlock(&nvdev->lock);
307 nouveau_bo_del(struct nouveau_bo *bo)
309 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
310 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
311 struct drm_gem_close req = { .handle = bo->handle };
313 if (nvbo->head.next) {
314 pthread_mutex_lock(&nvdev->lock);
315 if (atomic_read(&nvbo->refcnt) == 0) {
316 DRMLISTDEL(&nvbo->head);
318 * This bo has to be closed with the lock held because
319 * gem handles are not refcounted. If a shared bo is
320 * closed and re-opened in another thread a race
321 * against DRM_IOCTL_GEM_OPEN or drmPrimeFDToHandle
322 * might cause the bo to be closed accidentally while
325 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
327 pthread_mutex_unlock(&nvdev->lock);
329 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
332 drm_munmap(bo->map, bo->size);
337 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
338 uint64_t size, union nouveau_bo_config *config,
339 struct nouveau_bo **pbo)
341 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
342 struct nouveau_bo *bo = &nvbo->base;
347 atomic_set(&nvbo->refcnt, 1);
352 ret = abi16_bo_init(bo, align, config);
363 nouveau_bo_wrap_locked(struct nouveau_device *dev, uint32_t handle,
364 struct nouveau_bo **pbo, int name)
366 struct nouveau_device_priv *nvdev = nouveau_device(dev);
367 struct drm_nouveau_gem_info req = { .handle = handle };
368 struct nouveau_bo_priv *nvbo;
371 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
372 if (nvbo->base.handle == handle) {
373 if (atomic_inc_return(&nvbo->refcnt) == 1) {
375 * Uh oh, this bo is dead and someone else
376 * will free it, but because refcnt is
377 * now non-zero fortunately they won't
378 * call the ioctl to close the bo.
380 * Remove this bo from the list so other
381 * calls to nouveau_bo_wrap_locked will
382 * see our replacement nvbo.
384 DRMLISTDEL(&nvbo->head);
395 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
400 nvbo = calloc(1, sizeof(*nvbo));
402 atomic_set(&nvbo->refcnt, 1);
403 nvbo->base.device = dev;
404 abi16_bo_info(&nvbo->base, &req);
406 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
415 nouveau_bo_make_global(struct nouveau_bo_priv *nvbo)
417 if (!nvbo->head.next) {
418 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
419 pthread_mutex_lock(&nvdev->lock);
420 if (!nvbo->head.next)
421 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
422 pthread_mutex_unlock(&nvdev->lock);
427 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
428 struct nouveau_bo **pbo)
430 struct nouveau_device_priv *nvdev = nouveau_device(dev);
432 pthread_mutex_lock(&nvdev->lock);
433 ret = nouveau_bo_wrap_locked(dev, handle, pbo, 0);
434 pthread_mutex_unlock(&nvdev->lock);
439 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
440 struct nouveau_bo **pbo)
442 struct nouveau_device_priv *nvdev = nouveau_device(dev);
443 struct nouveau_bo_priv *nvbo;
444 struct drm_gem_open req = { .name = name };
447 pthread_mutex_lock(&nvdev->lock);
448 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
449 if (nvbo->name == name) {
450 ret = nouveau_bo_wrap_locked(dev, nvbo->base.handle,
452 pthread_mutex_unlock(&nvdev->lock);
457 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
459 ret = nouveau_bo_wrap_locked(dev, req.handle, pbo, name);
462 pthread_mutex_unlock(&nvdev->lock);
467 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
469 struct drm_gem_flink req = { .handle = bo->handle };
470 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
474 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
480 nvbo->name = *name = req.name;
482 nouveau_bo_make_global(nvbo);
488 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
490 struct nouveau_bo *ref = *pref;
492 atomic_inc(&nouveau_bo(bo)->refcnt);
495 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
502 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
503 struct nouveau_bo **bo)
505 struct nouveau_device_priv *nvdev = nouveau_device(dev);
509 nouveau_bo_ref(NULL, bo);
511 pthread_mutex_lock(&nvdev->lock);
512 ret = drmPrimeFDToHandle(dev->fd, prime_fd, &handle);
514 ret = nouveau_bo_wrap_locked(dev, handle, bo, 0);
516 pthread_mutex_unlock(&nvdev->lock);
521 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
523 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
526 ret = drmPrimeHandleToFD(bo->device->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
530 nouveau_bo_make_global(nvbo);
535 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
536 struct nouveau_client *client)
538 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
539 struct drm_nouveau_gem_cpu_prep req;
540 struct nouveau_pushbuf *push;
543 if (!(access & NOUVEAU_BO_RDWR))
546 push = cli_push_get(client, bo);
547 if (push && push->channel)
548 nouveau_pushbuf_kick(push, push->channel);
550 if (!nvbo->head.next && !(nvbo->access & NOUVEAU_BO_WR) &&
551 !(access & NOUVEAU_BO_WR))
554 req.handle = bo->handle;
556 if (access & NOUVEAU_BO_WR)
557 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
558 if (access & NOUVEAU_BO_NOBLOCK)
559 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
561 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
569 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
570 struct nouveau_client *client)
572 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
573 if (bo->map == NULL) {
574 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
575 MAP_SHARED, bo->device->fd, nvbo->map_handle);
576 if (bo->map == MAP_FAILED) {
581 return nouveau_bo_wait(bo, access, client);