Add 8200 detection.
[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         if (flags & NOUVEAU_BO_TILE)
51                 bo->drm.flags |= NOUVEAU_MEM_TILE;
52         bo->drm.flags |= NOUVEAU_MEM_MAPPED;
53
54         bo->drm.size = size;
55         bo->drm.alignment = align;
56
57         ret = drmCommandWriteRead(nv->fd, DRM_NOUVEAU_MEM_ALLOC, &bo->drm,
58                                   sizeof(bo->drm));
59         if (ret) {
60                 free(bo);
61                 return ret;
62         }
63
64         ret = drmMap(nv->fd, bo->drm.map_handle, bo->drm.size, &bo->map);
65         if (ret) {
66                 bo->map = NULL;
67                 nouveau_bo_del((void *)&bo);
68                 return ret;
69         }
70
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;
75         bo->refcount = 1;
76         *userbo = &bo->base;
77         return 0;
78 }
79
80 int
81 nouveau_bo_ref(struct nouveau_device *userdev, uint64_t handle,
82                struct nouveau_bo **userbo)
83 {
84         struct nouveau_bo_priv *bo = (void *)(unsigned long)handle;
85
86         if (!bo || !userbo || *userbo)
87                 return -EINVAL;
88
89         bo->refcount++;
90         *userbo = &bo->base;
91         return 0;
92 }
93
94 void
95 nouveau_bo_del(struct nouveau_bo **userbo)
96 {
97         struct drm_nouveau_mem_free f;
98         struct nouveau_bo_priv *bo;
99
100         if (!userbo || !*userbo)
101                 return;
102         bo = nouveau_bo(*userbo);
103         *userbo = NULL;
104
105         if (--bo->refcount)
106                 return;
107
108         if (bo->map) {
109                 drmUnmap(bo->map, bo->drm.size);
110                 bo->map = NULL;
111         }
112
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));
117
118         free(bo);
119 }
120
121 int
122 nouveau_bo_map(struct nouveau_bo *userbo, uint32_t flags)
123 {
124         struct nouveau_bo_priv *bo = nouveau_bo(userbo);
125
126         if (!bo)
127                 return -EINVAL;
128         userbo->map = bo->map;
129         return 0;
130 }
131
132 void
133 nouveau_bo_unmap(struct nouveau_bo *userbo)
134 {
135         userbo->map = NULL;
136 }
137
138 void
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)
142 {
143         struct nouveau_channel_priv *chan = nouveau_channel(userchan);
144         struct nouveau_bo_priv *bo = nouveau_bo(userbo);
145         struct nouveau_bo_reloc *r;
146
147         if (chan->num_relocs >= chan->max_relocs)
148                 FIRE_RING_CH(userchan);
149         r = &chan->relocs[chan->num_relocs++];
150
151         r->ptr = ptr;
152         r->bo = bo;
153         r->data = data;
154         r->flags = flags;
155         r->vor = vor;
156         r->tor = tor;
157 }
158
159 void
160 nouveau_bo_validate(struct nouveau_channel *userchan)
161 {
162 }
163