Fix my nv10 cursor.
[nouveau] / src / nouveau_bo.c
1 /*
2  * Copyright 2007 Nouveau Project
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * SOFTWARE.
21  */
22
23 #include <stdint.h>
24 #include <errno.h>
25
26 #include "nouveau_drmif.h"
27 #include "nouveau_dma.h"
28 #include "nouveau_local.h"
29
30 int
31 nouveau_bo_new(struct nouveau_device *userdev, uint32_t flags, int align,
32                int size, struct nouveau_bo **userbo)
33 {
34         struct nouveau_device_priv *nv = nouveau_device(userdev);
35         struct nouveau_bo_priv *bo;
36         int ret;
37
38         if (!nv || !userbo || *userbo)
39                 return -EINVAL;
40
41         bo = calloc(1, sizeof(*bo));
42         if (!bo)
43                 return -ENOMEM;
44         bo->base.device = userdev;
45
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;
51
52         bo->drm.size = size;
53         bo->drm.alignment = align;
54
55         ret = drmCommandWriteRead(nv->fd, DRM_NOUVEAU_MEM_ALLOC, &bo->drm,
56                                   sizeof(bo->drm));
57         if (ret) {
58                 free(bo);
59                 return ret;
60         }
61
62         ret = drmMap(nv->fd, bo->drm.map_handle, bo->drm.size, &bo->map);
63         if (ret) {
64                 bo->map = NULL;
65                 nouveau_bo_del((void *)&bo);
66                 return ret;
67         }
68
69         bo->base.size = bo->drm.size;
70         bo->base.offset = bo->drm.offset;
71         bo->base.handle = (unsigned long)bo;
72         bo->refcount = 1;
73         *userbo = &bo->base;
74         return 0;
75 }
76
77 int
78 nouveau_bo_ref(struct nouveau_device *userdev, uint64_t handle,
79                struct nouveau_bo **userbo)
80 {
81         struct nouveau_bo_priv *bo = (void *)(unsigned long)handle;
82
83         if (!bo || !userbo || *userbo)
84                 return -EINVAL;
85
86         bo->refcount++;
87         *userbo = &bo->base;
88         return 0;
89 }
90
91 void
92 nouveau_bo_del(struct nouveau_bo **userbo)
93 {
94         struct drm_nouveau_mem_free f;
95         struct nouveau_bo_priv *bo;
96
97         if (!userbo || !*userbo)
98                 return;
99         bo = nouveau_bo(*userbo);
100         *userbo = NULL;
101
102         if (--bo->refcount)
103                 return;
104
105         if (bo->map) {
106                 drmUnmap(bo->map, bo->drm.size);
107                 bo->map = NULL;
108         }
109
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));
114
115         free(bo);
116 }
117
118 int
119 nouveau_bo_map(struct nouveau_bo *userbo, uint32_t flags)
120 {
121         struct nouveau_bo_priv *bo = nouveau_bo(userbo);
122
123         if (!bo)
124                 return -EINVAL;
125         userbo->map = bo->map;
126         return 0;
127 }
128
129 void
130 nouveau_bo_unmap(struct nouveau_bo *userbo)
131 {
132         userbo->map = NULL;
133 }
134
135 void
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)
139 {
140         struct nouveau_channel_priv *chan = nouveau_channel(userchan);
141         struct nouveau_bo_priv *bo = nouveau_bo(userbo);
142         struct nouveau_bo_reloc *r;
143
144         if (chan->num_relocs >= chan->max_relocs)
145                 FIRE_RING_CH(userchan);
146         r = &chan->relocs[chan->num_relocs++];
147
148         r->ptr = ptr;
149         r->bo = bo;
150         r->data = data;
151         r->flags = flags;
152         r->vor = vor;
153         r->tor = tor;
154 }
155
156 void
157 nouveau_bo_validate(struct nouveau_channel *userchan)
158 {
159 }
160