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 bo->drm.flags |= NOUVEAU_MEM_MAPPED;
53 bo->drm.alignment = align;
55 ret = drmCommandWriteRead(nv->fd, DRM_NOUVEAU_MEM_ALLOC, &bo->drm,
62 ret = drmMap(nv->fd, bo->drm.map_handle, bo->drm.size, &bo->map);
65 nouveau_bo_del((void *)&bo);
69 bo->base.size = bo->drm.size;
70 bo->base.offset = bo->drm.offset;
71 bo->base.handle = (unsigned long)bo;
78 nouveau_bo_ref(struct nouveau_device *userdev, uint64_t handle,
79 struct nouveau_bo **userbo)
81 struct nouveau_bo_priv *bo = (void *)(unsigned long)handle;
83 if (!bo || !userbo || *userbo)
92 nouveau_bo_del(struct nouveau_bo **userbo)
94 struct drm_nouveau_mem_free f;
95 struct nouveau_bo_priv *bo;
97 if (!userbo || !*userbo)
99 bo = nouveau_bo(*userbo);
106 drmUnmap(bo->map, bo->drm.size);
110 f.flags = bo->drm.flags;
111 f.offset = bo->drm.offset;
112 drmCommandWrite(nouveau_device(bo->base.device)->fd,
113 DRM_NOUVEAU_MEM_FREE, &f, sizeof(f));
119 nouveau_bo_map(struct nouveau_bo *userbo, uint32_t flags)
121 struct nouveau_bo_priv *bo = nouveau_bo(userbo);
125 userbo->map = bo->map;
130 nouveau_bo_unmap(struct nouveau_bo *userbo)
136 nouveau_bo_emit_reloc(struct nouveau_channel *userchan, void *ptr,
137 struct nouveau_bo *userbo, uint32_t data, uint32_t flags,
138 uint32_t vor, uint32_t tor)
140 struct nouveau_channel_priv *chan = nouveau_channel(userchan);
141 struct nouveau_bo_priv *bo = nouveau_bo(userbo);
142 struct nouveau_bo_reloc *r;
144 if (chan->num_relocs >= chan->max_relocs)
145 FIRE_RING_CH(userchan);
146 r = &chan->relocs[chan->num_relocs++];
157 nouveau_bo_validate(struct nouveau_channel *userchan)