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
33
#include "nouveau_private.h"
36
nouveau_bo_init(struct nouveau_device *dev)
42
nouveau_bo_takedown(struct nouveau_device *dev)
47
nouveau_bo_info(struct nouveau_bo_priv *nvbo, struct drm_nouveau_gem_info *arg)
49
nvbo->handle = nvbo->base.handle = arg->handle;
50
nvbo->domain = arg->domain;
51
nvbo->size = arg->size;
52
nvbo->offset = arg->offset;
53
nvbo->map_handle = arg->map_handle;
54
nvbo->base.tile_mode = arg->tile_mode;
55
nvbo->base.tile_flags = arg->tile_flags;
60
nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
62
if (nvbo->sysmem || nvbo->handle)
68
nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
70
if (nvbo->user || nvbo->sysmem) {
75
nvbo->sysmem = malloc(nvbo->size);
83
nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
93
nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
95
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
96
struct drm_gem_close req;
102
munmap(nvbo->map, nvbo->size);
106
req.handle = nvbo->handle;
108
drmIoctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
112
nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
114
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
115
struct drm_nouveau_gem_new req;
116
struct drm_nouveau_gem_info *info = &req.info;
122
req.channel_hint = chan ? chan->id : 0;
123
req.align = nvbo->align;
126
info->size = nvbo->size;
129
if (nvbo->flags & NOUVEAU_BO_VRAM)
130
info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
131
if (nvbo->flags & NOUVEAU_BO_GART)
132
info->domain |= NOUVEAU_GEM_DOMAIN_GART;
134
info->domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
135
NOUVEAU_GEM_DOMAIN_GART);
138
if (nvbo->flags & NOUVEAU_BO_MAP)
139
info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
141
info->tile_mode = nvbo->base.tile_mode;
142
info->tile_flags = nvbo->base.tile_flags;
144
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
149
nouveau_bo_info(nvbo, &req.info);
154
nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
156
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
161
if (!nvbo->map_handle)
164
nvbo->map = mmap(0, nvbo->size, PROT_READ | PROT_WRITE,
165
MAP_SHARED, nvdev->fd, nvbo->map_handle);
166
if (nvbo->map == MAP_FAILED) {
175
nouveau_bo_new_tile(struct nouveau_device *dev, uint32_t flags, int align,
176
int size, uint32_t tile_mode, uint32_t tile_flags,
177
struct nouveau_bo **bo)
179
struct nouveau_bo_priv *nvbo;
182
if (!dev || !bo || *bo)
185
nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
188
nvbo->base.device = dev;
189
nvbo->base.size = size;
190
nvbo->base.tile_mode = tile_mode;
191
nvbo->base.tile_flags = tile_flags;
198
if (flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
199
ret = nouveau_bo_kalloc(nvbo, NULL);
201
nouveau_bo_ref(NULL, (void *)nvbo);
205
if (flags & NOUVEAU_BO_PIN) {
206
ret = nouveau_bo_pin((void *)nvbo, nvbo->flags);
208
nouveau_bo_ref(NULL, (void *)nvbo);
219
nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
220
int size, struct nouveau_bo **bo)
222
uint32_t tile_flags = 0;
224
if (flags & NOUVEAU_BO_TILED) {
225
if (flags & NOUVEAU_BO_ZTILE)
231
return nouveau_bo_new_tile(dev, flags, align, size, 0, tile_flags, bo);
235
nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
236
struct nouveau_bo **bo)
238
struct nouveau_bo_priv *nvbo;
241
ret = nouveau_bo_new(dev, NOUVEAU_BO_MAP, 0, size, bo);
244
nvbo = nouveau_bo(*bo);
252
nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
253
struct nouveau_bo **bo)
255
struct nouveau_device_priv *nvdev = nouveau_device(dev);
256
struct drm_nouveau_gem_info req;
257
struct nouveau_bo_priv *nvbo;
260
ret = nouveau_bo_new(dev, 0, 0, 0, bo);
263
nvbo = nouveau_bo(*bo);
266
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_INFO,
269
nouveau_bo_ref(NULL, bo);
273
nouveau_bo_info(nvbo, &req);
274
nvbo->base.size = nvbo->size;
279
nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
281
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
282
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
288
if (!nvbo->global_handle) {
289
struct drm_gem_flink req;
291
ret = nouveau_bo_kalloc(nvbo, NULL);
295
req.handle = nvbo->handle;
296
ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
298
nouveau_bo_kfree(nvbo);
302
nvbo->global_handle = req.name;
305
*handle = nvbo->global_handle;
310
nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
311
struct nouveau_bo **bo)
313
struct nouveau_device_priv *nvdev = nouveau_device(dev);
314
struct nouveau_bo_priv *nvbo;
315
struct drm_gem_open req;
319
ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
321
nouveau_bo_ref(NULL, bo);
325
ret = nouveau_bo_wrap(dev, req.handle, bo);
327
nouveau_bo_ref(NULL, bo);
331
nvbo = nouveau_bo(*bo);
332
nvbo->base.handle = nvbo->handle;
337
nouveau_bo_del(struct nouveau_bo **bo)
339
struct nouveau_bo_priv *nvbo;
343
nvbo = nouveau_bo(*bo);
346
if (--nvbo->refcount)
350
nvbo->pending = NULL;
351
nouveau_pushbuf_flush(nvbo->pending_channel, 0);
354
nouveau_bo_ufree(nvbo);
355
nouveau_bo_kfree(nvbo);
360
nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
366
nouveau_bo(ref)->refcount++;
376
nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write, int no_wait, int no_block)
378
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
379
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
380
struct drm_nouveau_gem_cpu_prep req;
383
if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
387
(nvbo->pending->write_domains || cpu_write)) {
388
nvbo->pending = NULL;
389
nouveau_pushbuf_flush(nvbo->pending_channel, 0);
392
req.handle = nvbo->handle;
395
req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
397
req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
399
req.flags |= NOUVEAU_GEM_CPU_PREP_NOBLOCK;
402
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
404
} while (ret == -EAGAIN);
409
nvbo->write_marker = 0;
414
nouveau_bo_map_range(struct nouveau_bo *bo, uint32_t delta, uint32_t size,
417
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
420
if (!nvbo || bo->map)
423
if (!nouveau_bo_allocated(nvbo)) {
424
if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
425
ret = nouveau_bo_kalloc(nvbo, NULL);
430
if (!nouveau_bo_allocated(nvbo)) {
431
ret = nouveau_bo_ualloc(nvbo);
438
bo->map = (char *)nvbo->sysmem + delta;
440
ret = nouveau_bo_kmap(nvbo);
444
if (!(flags & NOUVEAU_BO_NOSYNC)) {
445
ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR),
446
(flags & NOUVEAU_BO_NOWAIT), 0);
451
bo->map = (char *)nvbo->map + delta;
458
nouveau_bo_map_flush(struct nouveau_bo *bo, uint32_t delta, uint32_t size)
463
nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
465
return nouveau_bo_map_range(bo, 0, bo->size, flags);
469
nouveau_bo_unmap(struct nouveau_bo *bo)
471
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
473
if (bo->map && !nvbo->sysmem) {
474
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
475
struct drm_nouveau_gem_cpu_fini req;
477
req.handle = nvbo->handle;
478
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
486
nouveau_bo_pin(struct nouveau_bo *bo, uint32_t flags)
488
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
489
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
490
struct drm_nouveau_gem_pin req;
499
/* Now force it to stay put :) */
500
req.handle = nvbo->handle;
502
if (flags & NOUVEAU_BO_VRAM)
503
req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;
504
if (flags & NOUVEAU_BO_GART)
505
req.domain |= NOUVEAU_GEM_DOMAIN_GART;
507
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_PIN, &req,
508
sizeof(struct drm_nouveau_gem_pin));
511
nvbo->offset = req.offset;
512
nvbo->domain = req.domain;
515
/* Fill in public nouveau_bo members */
516
if (nvbo->domain & NOUVEAU_GEM_DOMAIN_VRAM)
517
bo->flags = NOUVEAU_BO_VRAM;
518
if (nvbo->domain & NOUVEAU_GEM_DOMAIN_GART)
519
bo->flags = NOUVEAU_BO_GART;
520
bo->offset = nvbo->offset;
526
nouveau_bo_unpin(struct nouveau_bo *bo)
528
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
529
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
530
struct drm_nouveau_gem_unpin req;
535
req.handle = nvbo->handle;
536
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_UNPIN, &req, sizeof(req));
538
nvbo->pinned = bo->offset = bo->flags = 0;
542
nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
544
return nouveau_bo_wait(bo, (access & NOUVEAU_BO_WR), 1, 1);
548
nouveau_bo_pending(struct nouveau_bo *bo)
550
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
557
if (nvbo->pending->read_domains)
558
flags |= NOUVEAU_BO_RD;
559
if (nvbo->pending->write_domains)
560
flags |= NOUVEAU_BO_WR;
565
struct drm_nouveau_gem_pushbuf_bo *
566
nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
568
struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(chan->pushbuf);
569
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
570
struct drm_nouveau_gem_pushbuf_bo *pbbo;
571
struct nouveau_bo *ref = NULL;
575
return nvbo->pending;
578
ret = nouveau_bo_kalloc(nvbo, chan);
583
void *sysmem_tmp = nvbo->sysmem;
586
ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
589
nvbo->sysmem = sysmem_tmp;
591
memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
592
nouveau_bo_ufree(nvbo);
593
nouveau_bo_unmap(bo);
597
if (nvpb->nr_buffers >= NOUVEAU_GEM_MAX_BUFFERS)
599
pbbo = nvpb->buffers + nvpb->nr_buffers++;
600
nvbo->pending = pbbo;
601
nvbo->pending_channel = chan;
602
nvbo->pending_refcnt = 0;
604
nouveau_bo_ref(bo, &ref);
605
pbbo->user_priv = (uint64_t)(unsigned long)ref;
606
pbbo->handle = nvbo->handle;
607
pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
608
pbbo->read_domains = 0;
609
pbbo->write_domains = 0;
610
pbbo->presumed_domain = nvbo->domain;
611
pbbo->presumed_offset = nvbo->offset;
612
pbbo->presumed_ok = 1;