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;
82 debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
89 ver = drmGetVersion(fd);
90 if (ver) dev->drm_version = (ver->version_major << 24) |
91 (ver->version_minor << 8) |
92 ver->version_patchlevel;
95 if ( dev->drm_version != 0x00000010 &&
96 (dev->drm_version < 0x01000000 ||
97 dev->drm_version >= 0x02000000)) {
98 nouveau_device_del(&dev);
102 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
104 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
106 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
108 nouveau_device_del(&dev);
112 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
114 nvdev->have_bo_usage = (bousage != 0);
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;
126 *pdev = &nvdev->base;
131 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
133 int ret = -ENODEV, fd = drmOpen("nouveau", busid);
135 ret = nouveau_device_wrap(fd, 1, pdev);
143 nouveau_device_del(struct nouveau_device **pdev)
145 struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
148 drmClose(nvdev->base.fd);
156 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
158 struct drm_nouveau_getparam r = { param, 0 };
159 int fd = dev->fd, ret =
160 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
166 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
168 struct drm_nouveau_setparam r = { param, value };
169 return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
173 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
175 struct nouveau_device_priv *nvdev = nouveau_device(dev);
176 struct nouveau_client_priv *pcli;
177 int id = 0, i, ret = -ENOMEM;
180 for (i = 0; i < nvdev->nr_client; i++) {
181 id = ffs(nvdev->client[i]) - 1;
186 clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
189 nvdev->client = clients;
190 nvdev->client[i] = 0;
194 pcli = calloc(1, sizeof(*pcli));
196 nvdev->client[i] |= (1 << id);
197 pcli->base.device = dev;
198 pcli->base.id = (i * 32) + id;
202 *pclient = &pcli->base;
207 nouveau_client_del(struct nouveau_client **pclient)
209 struct nouveau_client_priv *pcli = nouveau_client(*pclient);
210 struct nouveau_device_priv *nvdev;
212 int id = pcli->base.id;
213 nvdev = nouveau_device(pcli->base.device);
214 nvdev->client[id / 32] &= ~(1 << (id % 32));
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)
225 struct nouveau_device *dev;
226 struct nouveau_object *obj;
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;
238 memcpy(obj->data, data, length);
239 *(struct nouveau_object **)obj->data = obj;
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:
247 if (dev->chipset < 0xc0)
248 ret = abi16_chan_nv04(obj);
250 ret = abi16_chan_nvc0(obj);
257 case NOUVEAU_FIFO_CHANNEL_CLASS:
258 switch (obj->oclass) {
259 case NOUVEAU_NOTIFIER_CLASS:
260 ret = abi16_ntfy(obj);
263 ret = abi16_engobj(obj);
280 nouveau_object_del(struct nouveau_object **pobj)
282 struct nouveau_object *obj = *pobj;
283 struct nouveau_device *dev;
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,
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,
304 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
306 while (obj && obj->oclass != pclass) {
308 if (pclass == NOUVEAU_PARENT_CLASS)
315 nouveau_bo_del(struct nouveau_bo *bo)
317 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
318 struct drm_gem_close req = { bo->handle };
319 DRMLISTDEL(&nvbo->head);
321 munmap(bo->map, bo->size);
322 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
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)
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;
338 atomic_set(&nvbo->refcnt, 1);
343 ret = abi16_bo_init(bo, align, config);
349 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
356 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
357 struct nouveau_bo **pbo)
359 struct nouveau_device_priv *nvdev = nouveau_device(dev);
360 struct drm_nouveau_gem_info req = { .handle = handle };
361 struct nouveau_bo_priv *nvbo;
364 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
365 if (nvbo->base.handle == handle) {
367 nouveau_bo_ref(&nvbo->base, pbo);
372 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
377 nvbo = calloc(1, sizeof(*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);
391 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
392 struct nouveau_bo **pbo)
394 struct nouveau_device_priv *nvdev = nouveau_device(dev);
395 struct nouveau_bo_priv *nvbo;
396 struct drm_gem_open req = { .name = name };
399 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
400 if (nvbo->name == name) {
402 nouveau_bo_ref(&nvbo->base, pbo);
407 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
409 ret = nouveau_bo_wrap(dev, req.handle, pbo);
410 nouveau_bo((*pbo))->name = name;
417 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
419 struct drm_gem_flink req = { .handle = bo->handle };
420 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
422 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
425 nvbo->name = req.name;
432 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
434 struct nouveau_bo *ref = *pref;
436 atomic_inc(&nouveau_bo(bo)->refcnt);
439 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
446 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
447 struct nouveau_bo **bo)
452 ret = drmPrimeFDToHandle(dev->fd, prime_fd, &handle);
454 nouveau_bo_ref(NULL, bo);
458 ret = nouveau_bo_wrap(dev, handle, bo);
460 nouveau_bo_ref(NULL, bo);
468 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
470 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
473 ret = drmPrimeHandleToFD(bo->device->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
480 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
481 struct nouveau_client *client)
483 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
484 struct drm_nouveau_gem_cpu_prep req;
485 struct nouveau_pushbuf *push;
488 if (!(access & NOUVEAU_BO_RDWR))
491 push = cli_push_get(client, bo);
492 if (push && push->channel)
493 nouveau_pushbuf_kick(push, push->channel);
495 if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
496 !( access & NOUVEAU_BO_WR))
499 req.handle = bo->handle;
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;
506 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
514 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
515 struct nouveau_client *client)
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) {
526 return nouveau_bo_wait(bo, access, client);