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 if (dev->chipset < 0xe0)
251 ret = abi16_chan_nvc0(obj);
253 ret = abi16_chan_nve0(obj);
260 case NOUVEAU_FIFO_CHANNEL_CLASS:
261 switch (obj->oclass) {
262 case NOUVEAU_NOTIFIER_CLASS:
263 ret = abi16_ntfy(obj);
266 ret = abi16_engobj(obj);
283 nouveau_object_del(struct nouveau_object **pobj)
285 struct nouveau_object *obj = *pobj;
286 struct nouveau_device *dev;
288 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
289 if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
290 struct drm_nouveau_channel_free req;
291 req.channel = obj->handle;
292 drmCommandWrite(dev->fd, DRM_NOUVEAU_CHANNEL_FREE,
295 struct drm_nouveau_gpuobj_free req;
296 req.channel = obj->parent->handle;
297 req.handle = obj->handle;
298 drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
307 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
309 while (obj && obj->oclass != pclass) {
311 if (pclass == NOUVEAU_PARENT_CLASS)
318 nouveau_bo_del(struct nouveau_bo *bo)
320 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
321 struct drm_gem_close req = { bo->handle };
322 DRMLISTDEL(&nvbo->head);
324 munmap(bo->map, bo->size);
325 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
330 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
331 uint64_t size, union nouveau_bo_config *config,
332 struct nouveau_bo **pbo)
334 struct nouveau_device_priv *nvdev = nouveau_device(dev);
335 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
336 struct nouveau_bo *bo = &nvbo->base;
341 atomic_set(&nvbo->refcnt, 1);
346 ret = abi16_bo_init(bo, align, config);
352 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
359 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
360 struct nouveau_bo **pbo)
362 struct nouveau_device_priv *nvdev = nouveau_device(dev);
363 struct drm_nouveau_gem_info req = { .handle = handle };
364 struct nouveau_bo_priv *nvbo;
367 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
368 if (nvbo->base.handle == handle) {
370 nouveau_bo_ref(&nvbo->base, pbo);
375 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
380 nvbo = calloc(1, sizeof(*nvbo));
382 atomic_set(&nvbo->refcnt, 1);
383 nvbo->base.device = dev;
384 abi16_bo_info(&nvbo->base, &req);
385 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
394 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
395 struct nouveau_bo **pbo)
397 struct nouveau_device_priv *nvdev = nouveau_device(dev);
398 struct nouveau_bo_priv *nvbo;
399 struct drm_gem_open req = { .name = name };
402 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
403 if (nvbo->name == name) {
405 nouveau_bo_ref(&nvbo->base, pbo);
410 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
412 ret = nouveau_bo_wrap(dev, req.handle, pbo);
413 nouveau_bo((*pbo))->name = name;
420 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
422 struct drm_gem_flink req = { .handle = bo->handle };
423 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
425 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
428 nvbo->name = req.name;
435 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
437 struct nouveau_bo *ref = *pref;
439 atomic_inc(&nouveau_bo(bo)->refcnt);
442 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
449 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
450 struct nouveau_bo **bo)
455 ret = drmPrimeFDToHandle(dev->fd, prime_fd, &handle);
457 nouveau_bo_ref(NULL, bo);
461 ret = nouveau_bo_wrap(dev, handle, bo);
463 nouveau_bo_ref(NULL, bo);
471 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
473 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
476 ret = drmPrimeHandleToFD(bo->device->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
483 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
484 struct nouveau_client *client)
486 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
487 struct drm_nouveau_gem_cpu_prep req;
488 struct nouveau_pushbuf *push;
491 if (!(access & NOUVEAU_BO_RDWR))
494 push = cli_push_get(client, bo);
495 if (push && push->channel)
496 nouveau_pushbuf_kick(push, push->channel);
498 if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
499 !( access & NOUVEAU_BO_WR))
502 req.handle = bo->handle;
504 if (access & NOUVEAU_BO_WR)
505 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
506 if (access & NOUVEAU_BO_NOBLOCK)
507 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
509 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
517 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
518 struct nouveau_client *client)
520 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
521 if (bo->map == NULL) {
522 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
523 MAP_SHARED, bo->device->fd, nvbo->map_handle);
524 if (bo->map == MAP_FAILED) {
529 return nouveau_bo_wait(bo, access, client);