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 drm_nouveau_gpuobj_free req;
282 struct nouveau_object *obj = *pobj;
283 struct nouveau_device *dev;
285 dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
286 req.channel = obj->parent->handle;
287 req.handle = obj->handle;
288 drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
296 nouveau_object_find(struct nouveau_object *obj, uint32_t pclass)
298 while (obj && obj->oclass != pclass) {
300 if (pclass == NOUVEAU_PARENT_CLASS)
307 nouveau_bo_del(struct nouveau_bo *bo)
309 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
310 struct drm_gem_close req = { bo->handle };
311 DRMLISTDEL(&nvbo->head);
313 munmap(bo->map, bo->size);
314 drmIoctl(bo->device->fd, DRM_IOCTL_GEM_CLOSE, &req);
319 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
320 uint64_t size, union nouveau_bo_config *config,
321 struct nouveau_bo **pbo)
323 struct nouveau_device_priv *nvdev = nouveau_device(dev);
324 struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
325 struct nouveau_bo *bo = &nvbo->base;
330 atomic_set(&nvbo->refcnt, 1);
335 ret = abi16_bo_init(bo, align, config);
341 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
348 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
349 struct nouveau_bo **pbo)
351 struct nouveau_device_priv *nvdev = nouveau_device(dev);
352 struct drm_nouveau_gem_info req = { .handle = handle };
353 struct nouveau_bo_priv *nvbo;
356 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
357 if (nvbo->base.handle == handle) {
359 nouveau_bo_ref(&nvbo->base, pbo);
364 ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_INFO,
369 nvbo = calloc(1, sizeof(*nvbo));
371 atomic_set(&nvbo->refcnt, 1);
372 nvbo->base.device = dev;
373 abi16_bo_info(&nvbo->base, &req);
374 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
383 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
384 struct nouveau_bo **pbo)
386 struct nouveau_device_priv *nvdev = nouveau_device(dev);
387 struct nouveau_bo_priv *nvbo;
388 struct drm_gem_open req = { .name = name };
391 DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
392 if (nvbo->name == name) {
394 nouveau_bo_ref(&nvbo->base, pbo);
399 ret = drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req);
401 ret = nouveau_bo_wrap(dev, req.handle, pbo);
402 nouveau_bo((*pbo))->name = name;
409 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
411 struct drm_gem_flink req = { .handle = bo->handle };
412 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
414 int ret = drmIoctl(bo->device->fd, DRM_IOCTL_GEM_FLINK, &req);
417 nvbo->name = req.name;
424 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
426 struct nouveau_bo *ref = *pref;
428 atomic_inc(&nouveau_bo(bo)->refcnt);
431 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
438 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
439 struct nouveau_client *client)
441 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
442 struct drm_nouveau_gem_cpu_prep req;
443 struct nouveau_pushbuf *push;
446 if (!(access & NOUVEAU_BO_RDWR))
449 push = cli_push_get(client, bo);
450 if (push && push->channel)
451 nouveau_pushbuf_kick(push, push->channel);
453 if (!nvbo->name && !(nvbo->access & NOUVEAU_BO_WR) &&
454 !( access & NOUVEAU_BO_WR))
457 req.handle = bo->handle;
459 if (access & NOUVEAU_BO_WR)
460 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
461 if (access & NOUVEAU_BO_NOBLOCK)
462 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
464 ret = drmCommandWrite(bo->device->fd, DRM_NOUVEAU_GEM_CPU_PREP,
472 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
473 struct nouveau_client *client)
475 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
476 if (bo->map == NULL) {
477 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
478 MAP_SHARED, bo->device->fd, nvbo->map_handle);
479 if (bo->map == MAP_FAILED) {
484 return nouveau_bo_wait(bo, access, client);