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.
39 #include <xf86atomic.h>
40 #include "libdrm_lists.h"
41 #include "nouveau_drm.h"
47 uint32_t nouveau_debug = 0;
50 debug_init(char *args)
53 int n = strtol(args, NULL, 0);
60 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
61 * is kept here to prevent AIGLX from crashing if the DDX is linked against
62 * the new libdrm, but the DRI driver against the old
65 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
72 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
74 struct nouveau_device_priv *nvdev = calloc(1, sizeof(*nvdev));
75 struct nouveau_device *dev = &nvdev->base;
76 uint64_t chipset, vram, gart, bousage;
81 debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
88 ver = drmGetVersion(fd);
89 if (ver) dev->drm_version = (ver->version_major << 24) |
90 (ver->version_minor << 8) |
91 ver->version_patchlevel;
94 if ( dev->drm_version != 0x00000010 &&
95 (dev->drm_version < 0x01000000 ||
96 dev->drm_version >= 0x02000000)) {
97 nouveau_device_del(&dev);
101 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &chipset);
103 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &vram);
105 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &gart);
107 nouveau_device_del(&dev);
111 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &bousage);
113 nvdev->have_bo_usage = (bousage != 0);
115 nvdev->close = close;
116 DRMINITLISTHEAD(&nvdev->bo_list);
117 nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
118 nvdev->base.lib_version = 0x01000000;
119 nvdev->base.chipset = chipset;
120 nvdev->base.vram_size = vram;
121 nvdev->base.gart_size = gart;
122 nvdev->base.vram_limit = (nvdev->base.vram_size * 80) / 100;
123 nvdev->base.gart_limit = (nvdev->base.gart_size * 80) / 100;
125 *pdev = &nvdev->base;
130 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
132 int ret = -ENODEV, fd = drmOpen("nouveau", busid);
134 ret = nouveau_device_wrap(fd, 1, pdev);
142 nouveau_device_del(struct nouveau_device **pdev)
144 struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
147 drmClose(nvdev->base.fd);
155 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
157 struct drm_nouveau_getparam r = { param, 0 };
158 int fd = dev->fd, ret =
159 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
165 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
167 struct drm_nouveau_setparam r = { param, value };
168 return drmCommandWrite(dev->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
172 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
174 struct nouveau_device_priv *nvdev = nouveau_device(dev);
175 struct nouveau_client_priv *pcli;
176 int id = 0, i, ret = -ENOMEM;
179 for (i = 0; i < nvdev->nr_client; i++) {
180 id = ffs(nvdev->client[i]) - 1;
185 clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
188 nvdev->client = clients;
189 nvdev->client[i] = 0;
193 pcli = calloc(1, sizeof(*pcli));
195 nvdev->client[i] |= (1 << id);
196 pcli->base.device = dev;
197 pcli->base.id = (i * 32) + id;
201 *pclient = &pcli->base;
206 nouveau_client_del(struct nouveau_client **pclient)
208 struct nouveau_client_priv *pcli = nouveau_client(*pclient);
209 struct nouveau_device_priv *nvdev;
211 int id = pcli->base.id;
212 nvdev = nouveau_device(pcli->base.device);
213 nvdev->client[id / 32] &= ~(1 << (id % 32));
220 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
221 uint32_t oclass, void *data, uint32_t length,
222 struct nouveau_object **pobj)
224 struct nouveau_device *dev;
225 struct nouveau_object *obj;
229 length = sizeof(struct nouveau_object *);
230 obj = malloc(sizeof(*obj) + length);
231 obj->parent = parent;
232 obj->handle = handle;
233 obj->oclass = oclass;
234 obj->length = length;
237 memcpy(obj->data, data, length);
238 *(struct nouveau_object **)obj->data = obj;
240 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
241 switch (parent->oclass) {
242 case NOUVEAU_DEVICE_CLASS:
243 switch (obj->oclass) {
244 case NOUVEAU_FIFO_CHANNEL_CLASS:
246 if (dev->chipset < 0xc0)
247 ret = abi16_chan_nv04(obj);
249 ret = abi16_chan_nvc0(obj);
256 case NOUVEAU_FIFO_CHANNEL_CLASS:
257 switch (obj->oclass) {
258 case NOUVEAU_NOTIFIER_CLASS:
259 ret = abi16_ntfy(obj);
262 ret = abi16_engobj(obj);
279 nouveau_object_del(struct nouveau_object **pobj)
281 struct nouveau_object *obj = *pobj;
282 struct nouveau_device *dev;
284 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
285 if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) {
286 struct drm_nouveau_channel_free req;
287 req.channel = obj->handle;
288 drmCommandWrite(dev->fd, DRM_NOUVEAU_CHANNEL_FREE,
291 struct drm_nouveau_gpuobj_free req;
292 req.channel = obj->parent->handle;
293 req.handle = obj->handle;
294 drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
303 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
305 while (obj && obj->oclass != pclass) {
307 if (pclass == NOUVEAU_PARENT_CLASS)
314 nouveau_bo_del(struct nouveau_bo *bo)
316 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
317 struct drm_gem_close req = { bo->handle };
318 DRMLISTDEL(&nvbo->head);
320 munmap(bo->map, bo->size);
321 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
326 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
327 uint64_t size, union nouveau_bo_config *config,
328 struct nouveau_bo **pbo)
330 struct nouveau_device_priv *nvdev = nouveau_device(dev);
331 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
332 struct nouveau_bo *bo = &nvbo->base;
337 atomic_set(&nvbo->refcnt, 1);
342 ret = abi16_bo_init(bo, align, config);
348 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
355 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
356 struct nouveau_bo **pbo)
358 struct nouveau_device_priv *nvdev = nouveau_device(dev);
359 struct drm_nouveau_gem_info req = { .handle = handle };
360 struct nouveau_bo_priv *nvbo;
363 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
364 if (nvbo->base.handle == handle) {
366 nouveau_bo_ref(&nvbo->base, pbo);
371 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
376 nvbo = calloc(1, sizeof(*nvbo));
378 atomic_set(&nvbo->refcnt, 1);
379 nvbo->base.device = dev;
380 abi16_bo_info(&nvbo->base, &req);
381 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
390 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
391 struct nouveau_bo **pbo)
393 struct nouveau_device_priv *nvdev = nouveau_device(dev);
394 struct nouveau_bo_priv *nvbo;
395 struct drm_gem_open req = { .name = name };
398 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
399 if (nvbo->name == name) {
401 nouveau_bo_ref(&nvbo->base, pbo);
406 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
408 ret = nouveau_bo_wrap(dev, req.handle, pbo);
409 nouveau_bo((*pbo))->name = name;
416 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
418 struct drm_gem_flink req = { .handle = bo->handle };
419 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
421 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
424 nvbo->name = req.name;
431 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
433 struct nouveau_bo *ref = *pref;
435 atomic_inc(&nouveau_bo(bo)->refcnt);
438 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
445 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
446 struct nouveau_client *client)
448 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
449 struct drm_nouveau_gem_cpu_prep req;
450 struct nouveau_pushbuf *push;
453 if (!(access & NOUVEAU_BO_RDWR))
456 push = cli_push_get(client, bo);
457 if (push && push->channel)
458 nouveau_pushbuf_kick(push, push->channel);
460 if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
461 !( access & NOUVEAU_BO_WR))
464 req.handle = bo->handle;
466 if (access & NOUVEAU_BO_WR)
467 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
468 if (access & NOUVEAU_BO_NOBLOCK)
469 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
471 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
479 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
480 struct nouveau_client *client)
482 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
483 if (bo->map == NULL) {
484 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
485 MAP_SHARED, bo->device->fd, nvbo->map_handle);
486 if (bo->map == MAP_FAILED) {
491 return nouveau_bo_wait(bo, access, client);