2 * Copyright 2007 Nouveau Project
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 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <sys/ioctl.h>
34 #include "nouveau_private.h"
37 nouveau_bo_init(struct nouveau_device *dev)
43 nouveau_bo_takedown(struct nouveau_device *dev)
48 nouveau_bo_info(struct nouveau_bo_priv *nvbo, struct drm_nouveau_gem_info *arg)
50 nvbo->handle = nvbo->base.handle = arg->handle;
51 nvbo->domain = arg->domain;
52 nvbo->size = arg->size;
53 nvbo->offset = arg->offset;
54 nvbo->map_handle = arg->map_handle;
55 nvbo->base.tile_mode = arg->tile_mode;
56 nvbo->base.tile_flags = arg->tile_flags;
61 nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
63 if (nvbo->sysmem || nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
69 nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
71 if (nvbo->user || nvbo->sysmem) {
76 nvbo->sysmem = malloc(nvbo->size);
84 nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
94 nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
96 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
97 struct drm_gem_close req;
103 munmap(nvbo->map, nvbo->size);
107 req.handle = nvbo->handle;
109 ioctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
113 nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
115 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
116 struct drm_nouveau_gem_new req;
117 struct drm_nouveau_gem_info *info = &req.info;
120 if (nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
123 req.channel_hint = chan ? chan->id : 0;
124 req.align = nvbo->align;
127 info->size = nvbo->size;
130 if (nvbo->flags & NOUVEAU_BO_VRAM)
131 info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
132 if (nvbo->flags & NOUVEAU_BO_GART)
133 info->domain |= NOUVEAU_GEM_DOMAIN_GART;
135 info->domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
136 NOUVEAU_GEM_DOMAIN_GART);
139 if (nvbo->flags & NOUVEAU_BO_MAP)
140 info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
142 info->tile_mode = nvbo->base.tile_mode;
143 info->tile_flags = nvbo->base.tile_flags;
145 ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
150 nouveau_bo_info(nvbo, &req.info);
155 nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
157 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
162 if (!nvbo->map_handle)
165 nvbo->map = mmap(0, nvbo->size, PROT_READ | PROT_WRITE,
166 MAP_SHARED, nvdev->fd, nvbo->map_handle);
167 if (nvbo->map == MAP_FAILED) {
176 nouveau_bo_new_tile(struct nouveau_device *dev, uint32_t flags, int align,
177 int size, uint32_t tile_mode, uint32_t tile_flags,
178 struct nouveau_bo **bo)
180 struct nouveau_bo_priv *nvbo;
183 if (!dev || !bo || *bo)
186 nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
189 nvbo->base.device = dev;
190 nvbo->base.size = size;
191 nvbo->base.tile_mode = tile_mode;
192 nvbo->base.tile_flags = tile_flags;
195 /* Don't set NOUVEAU_BO_PIN here, or nouveau_bo_allocated() will
196 * decided the buffer's already allocated when it's not. The
197 * call to nouveau_bo_pin() later will set this flag.
199 nvbo->flags = (flags & ~NOUVEAU_BO_PIN);
203 if (flags & NOUVEAU_BO_PIN) {
204 ret = nouveau_bo_pin((void *)nvbo, nvbo->flags);
206 nouveau_bo_ref(NULL, (void *)nvbo);
216 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
217 int size, struct nouveau_bo **bo)
219 uint32_t tile_flags = 0;
221 if (flags & NOUVEAU_BO_TILED) {
222 if (flags & NOUVEAU_BO_ZTILE)
228 return nouveau_bo_new_tile(dev, flags, align, size, 0, tile_flags, bo);
232 nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
233 struct nouveau_bo **bo)
235 struct nouveau_bo_priv *nvbo;
238 ret = nouveau_bo_new(dev, NOUVEAU_BO_MAP, 0, size, bo);
241 nvbo = nouveau_bo(*bo);
249 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
250 struct nouveau_bo **bo)
252 struct nouveau_device_priv *nvdev = nouveau_device(dev);
253 struct drm_nouveau_gem_info req;
254 struct nouveau_bo_priv *nvbo;
257 ret = nouveau_bo_new(dev, 0, 0, 0, bo);
260 nvbo = nouveau_bo(*bo);
263 ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_INFO,
266 nouveau_bo_ref(NULL, bo);
270 nouveau_bo_info(nvbo, &req);
271 nvbo->base.size = nvbo->size;
276 nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
278 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
279 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
285 if (!nvbo->global_handle) {
286 struct drm_gem_flink req;
288 ret = nouveau_bo_kalloc(nvbo, NULL);
292 req.handle = nvbo->handle;
293 ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
295 nouveau_bo_kfree(nvbo);
299 nvbo->global_handle = req.name;
302 *handle = nvbo->global_handle;
307 nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
308 struct nouveau_bo **bo)
310 struct nouveau_device_priv *nvdev = nouveau_device(dev);
311 struct nouveau_bo_priv *nvbo;
312 struct drm_gem_open req;
316 ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
318 nouveau_bo_ref(NULL, bo);
322 ret = nouveau_bo_wrap(dev, req.handle, bo);
324 nouveau_bo_ref(NULL, bo);
328 nvbo = nouveau_bo(*bo);
329 nvbo->base.handle = nvbo->handle;
334 nouveau_bo_del(struct nouveau_bo **bo)
336 struct nouveau_bo_priv *nvbo;
340 nvbo = nouveau_bo(*bo);
343 if (--nvbo->refcount)
347 nvbo->pending = NULL;
348 nouveau_pushbuf_flush(nvbo->pending_channel, 0);
351 nouveau_bo_ufree(nvbo);
352 nouveau_bo_kfree(nvbo);
357 nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
363 nouveau_bo(ref)->refcount++;
373 nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write, int no_wait, int no_block)
375 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
376 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
377 struct drm_nouveau_gem_cpu_prep req;
380 if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
384 (nvbo->pending->write_domains || cpu_write)) {
385 nvbo->pending = NULL;
386 nouveau_pushbuf_flush(nvbo->pending_channel, 0);
389 req.handle = nvbo->handle;
392 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
394 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
396 req.flags |= NOUVEAU_GEM_CPU_PREP_NOBLOCK;
399 ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
401 } while (ret == -EAGAIN);
406 nvbo->write_marker = 0;
411 nouveau_bo_map_range(struct nouveau_bo *bo, uint32_t delta, uint32_t size,
414 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
417 if (!nvbo || bo->map)
420 if (!nouveau_bo_allocated(nvbo)) {
421 if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
422 ret = nouveau_bo_kalloc(nvbo, NULL);
427 if (!nouveau_bo_allocated(nvbo)) {
428 ret = nouveau_bo_ualloc(nvbo);
435 bo->map = (char *)nvbo->sysmem + delta;
437 ret = nouveau_bo_kmap(nvbo);
441 if (!(flags & NOUVEAU_BO_NOSYNC)) {
442 ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR),
443 (flags & NOUVEAU_BO_NOWAIT), 0);
448 bo->map = (char *)nvbo->map + delta;
455 nouveau_bo_map_flush(struct nouveau_bo *bo, uint32_t delta, uint32_t size)
460 nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
462 return nouveau_bo_map_range(bo, 0, bo->size, flags);
466 nouveau_bo_unmap(struct nouveau_bo *bo)
468 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
470 if (bo->map && !nvbo->sysmem) {
471 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
472 struct drm_nouveau_gem_cpu_fini req;
474 req.handle = nvbo->handle;
475 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
483 nouveau_bo_pin(struct nouveau_bo *bo, uint32_t flags)
485 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
486 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
487 struct drm_nouveau_gem_pin req;
493 /* Ensure we have a kernel object... */
495 if (!(flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)))
501 ret = nouveau_bo_kalloc(nvbo, NULL);
506 /* Now force it to stay put :) */
507 req.handle = nvbo->handle;
509 if (nvbo->flags & NOUVEAU_BO_VRAM)
510 req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;
511 if (nvbo->flags & NOUVEAU_BO_GART)
512 req.domain |= NOUVEAU_GEM_DOMAIN_GART;
514 ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_PIN, &req,
515 sizeof(struct drm_nouveau_gem_pin));
518 nvbo->offset = req.offset;
519 nvbo->domain = req.domain;
521 nvbo->flags |= NOUVEAU_BO_PIN;
523 /* Fill in public nouveau_bo members */
524 if (nvbo->domain & NOUVEAU_GEM_DOMAIN_VRAM)
525 bo->flags = NOUVEAU_BO_VRAM;
526 if (nvbo->domain & NOUVEAU_GEM_DOMAIN_GART)
527 bo->flags = NOUVEAU_BO_GART;
528 bo->offset = nvbo->offset;
534 nouveau_bo_unpin(struct nouveau_bo *bo)
536 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
537 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
538 struct drm_nouveau_gem_unpin req;
543 req.handle = nvbo->handle;
544 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_UNPIN, &req, sizeof(req));
546 nvbo->pinned = bo->offset = bo->flags = 0;
550 nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
552 return nouveau_bo_wait(bo, (access & NOUVEAU_BO_WR), 1, 1);
556 nouveau_bo_pending(struct nouveau_bo *bo)
558 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
565 if (nvbo->pending->read_domains)
566 flags |= NOUVEAU_BO_RD;
567 if (nvbo->pending->write_domains)
568 flags |= NOUVEAU_BO_WR;
573 struct drm_nouveau_gem_pushbuf_bo *
574 nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
576 struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(chan->pushbuf);
577 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
578 struct drm_nouveau_gem_pushbuf_bo *pbbo;
579 struct nouveau_bo *ref = NULL;
583 return nvbo->pending;
586 ret = nouveau_bo_kalloc(nvbo, chan);
591 void *sysmem_tmp = nvbo->sysmem;
594 ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
597 nvbo->sysmem = sysmem_tmp;
599 memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
600 nouveau_bo_ufree(nvbo);
601 nouveau_bo_unmap(bo);
605 if (nvpb->nr_buffers >= NOUVEAU_GEM_MAX_BUFFERS)
607 pbbo = nvpb->buffers + nvpb->nr_buffers++;
608 nvbo->pending = pbbo;
609 nvbo->pending_channel = chan;
610 nvbo->pending_refcnt = 0;
612 nouveau_bo_ref(bo, &ref);
613 pbbo->user_priv = (uint64_t)(unsigned long)ref;
614 pbbo->handle = nvbo->handle;
615 pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
616 pbbo->read_domains = 0;
617 pbbo->write_domains = 0;
618 pbbo->presumed_domain = nvbo->domain;
619 pbbo->presumed_offset = nvbo->offset;
620 pbbo->presumed_ok = 1;