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
26 #include "nouveau_drmif.h"
27 #include "nouveau_dma.h"
28 #include "nouveau_local.h"
31 nouveau_bo_new(struct nouveau_device *userdev, uint32_t flags, int align,
32 int size, struct nouveau_bo **userbo)
34 struct nouveau_device_priv *nv = nouveau_device(userdev);
35 struct nouveau_bo_priv *bo;
38 if (!nv || !userbo || *userbo)
41 bo = calloc(1, sizeof(*bo));
44 bo->base.device = userdev;
46 if (flags & NOUVEAU_BO_VRAM)
47 bo->drm.flags |= NOUVEAU_MEM_FB;
48 if (flags & NOUVEAU_BO_GART)
49 bo->drm.flags |= (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI);
50 if (flags & NOUVEAU_BO_TILE)
51 bo->drm.flags |= NOUVEAU_MEM_TILE;
52 bo->drm.flags |= NOUVEAU_MEM_MAPPED;
55 bo->drm.alignment = align;
57 ret = drmCommandWriteRead(nv->fd, DRM_NOUVEAU_MEM_ALLOC, &bo->drm,
64 ret = drmMap(nv->fd, bo->drm.map_handle, bo->drm.size, &bo->map);
67 nouveau_bo_del((void *)&bo);
71 bo->base.size = bo->drm.size;
72 bo->base.offset = bo->drm.offset;
73 bo->base.handle = (unsigned long)bo;
74 bo->base.map_handle = bo->drm.map_handle;
81 nouveau_bo_ref(struct nouveau_device *userdev, uint64_t handle,
82 struct nouveau_bo **userbo)
84 struct nouveau_bo_priv *bo = (void *)(unsigned long)handle;
86 if (!bo || !userbo || *userbo)
95 nouveau_bo_del(struct nouveau_bo **userbo)
97 struct drm_nouveau_mem_free f;
98 struct nouveau_bo_priv *bo;
100 if (!userbo || !*userbo)
102 bo = nouveau_bo(*userbo);
109 drmUnmap(bo->map, bo->drm.size);
113 f.flags = bo->drm.flags;
114 f.offset = bo->drm.offset;
115 drmCommandWrite(nouveau_device(bo->base.device)->fd,
116 DRM_NOUVEAU_MEM_FREE, &f, sizeof(f));
122 nouveau_bo_map(struct nouveau_bo *userbo, uint32_t flags)
124 struct nouveau_bo_priv *bo = nouveau_bo(userbo);
128 userbo->map = bo->map;
133 nouveau_bo_unmap(struct nouveau_bo *userbo)
139 nouveau_bo_emit_reloc(struct nouveau_channel *userchan, void *ptr,
140 struct nouveau_bo *userbo, uint32_t data, uint32_t flags,
141 uint32_t vor, uint32_t tor)
143 struct nouveau_channel_priv *chan = nouveau_channel(userchan);
144 struct nouveau_bo_priv *bo = nouveau_bo(userbo);
145 struct nouveau_bo_reloc *r;
147 if (chan->num_relocs >= chan->max_relocs)
148 FIRE_RING_CH(userchan);
149 r = &chan->relocs[chan->num_relocs++];
160 nouveau_bo_validate(struct nouveau_channel *userchan)