nouveau: gallium directory structure changed again..
[mesa.git] / src / gallium / winsys / drm / nouveau / 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 <stdlib.h>
25 #include <errno.h>
26 #include <assert.h>
27
28 #include "nouveau_drmif.h"
29 #include "nouveau_dma.h"
30 #include "nouveau_local.h"
31
32 static void
33 nouveau_mem_free(struct nouveau_device *dev, struct drm_nouveau_mem_alloc *ma,
34 void **map)
35 {
36 struct nouveau_device_priv *nvdev = nouveau_device(dev);
37 struct drm_nouveau_mem_free mf;
38
39 if (map && *map) {
40 drmUnmap(*map, ma->size);
41 *map = NULL;
42 }
43
44 if (ma->size) {
45 mf.offset = ma->offset;
46 mf.flags = ma->flags;
47 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_FREE,
48 &mf, sizeof(mf));
49 ma->size = 0;
50 }
51 }
52
53 static int
54 nouveau_mem_alloc(struct nouveau_device *dev, unsigned size, unsigned align,
55 uint32_t flags, struct drm_nouveau_mem_alloc *ma, void **map)
56 {
57 struct nouveau_device_priv *nvdev = nouveau_device(dev);
58 int ret;
59
60 ma->alignment = align;
61 ma->size = size;
62 ma->flags = flags;
63 if (map)
64 ma->flags |= NOUVEAU_MEM_MAPPED;
65 ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_MEM_ALLOC, ma,
66 sizeof(struct drm_nouveau_mem_alloc));
67 if (ret)
68 return ret;
69
70 if (map) {
71 ret = drmMap(nvdev->fd, ma->map_handle, ma->size, map);
72 if (ret) {
73 *map = NULL;
74 nouveau_mem_free(dev, ma, map);
75 return ret;
76 }
77 }
78
79 return 0;
80 }
81
82 static void
83 nouveau_bo_tmp_del(void *priv)
84 {
85 struct nouveau_resource *r = priv;
86
87 nouveau_fence_ref(NULL, (struct nouveau_fence **)&r->priv);
88 nouveau_resource_free(&r);
89 }
90
91 static unsigned
92 nouveau_bo_tmp_max(struct nouveau_device_priv *nvdev)
93 {
94 struct nouveau_resource *r = nvdev->sa_heap;
95 unsigned max = 0;
96
97 while (r) {
98 if (r->in_use && !nouveau_fence(r->priv)->emitted) {
99 r = r->next;
100 continue;
101 }
102
103 if (max < r->size)
104 max = r->size;
105 r = r->next;
106 }
107
108 return max;
109 }
110
111 static struct nouveau_resource *
112 nouveau_bo_tmp(struct nouveau_channel *chan, unsigned size,
113 struct nouveau_fence *fence)
114 {
115 struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
116 struct nouveau_resource *r = NULL;
117 struct nouveau_fence *ref = NULL;
118
119 if (fence)
120 nouveau_fence_ref(fence, &ref);
121 else
122 nouveau_fence_new(chan, &ref);
123 assert(ref);
124
125 while (nouveau_resource_alloc(nvdev->sa_heap, size, ref, &r)) {
126 if (nouveau_bo_tmp_max(nvdev) < size) {
127 nouveau_fence_ref(NULL, &ref);
128 return NULL;
129 }
130
131 nouveau_fence_flush(chan);
132 }
133 nouveau_fence_signal_cb(ref, nouveau_bo_tmp_del, r);
134
135 return r;
136 }
137
138 int
139 nouveau_bo_init(struct nouveau_device *dev)
140 {
141 struct nouveau_device_priv *nvdev = nouveau_device(dev);
142 int ret;
143
144 ret = nouveau_mem_alloc(dev, 128*1024, 0, NOUVEAU_MEM_AGP |
145 NOUVEAU_MEM_PCI, &nvdev->sa, &nvdev->sa_map);
146 if (ret)
147 return ret;
148
149 ret = nouveau_resource_init(&nvdev->sa_heap, 0, nvdev->sa.size);
150 if (ret) {
151 nouveau_mem_free(dev, &nvdev->sa, &nvdev->sa_map);
152 return ret;
153 }
154
155 return 0;
156 }
157
158 void
159 nouveau_bo_takedown(struct nouveau_device *dev)
160 {
161 struct nouveau_device_priv *nvdev = nouveau_device(dev);
162
163 nouveau_mem_free(dev, &nvdev->sa, &nvdev->sa_map);
164 }
165
166 int
167 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
168 int size, struct nouveau_bo **bo)
169 {
170 struct nouveau_bo_priv *nvbo;
171 int ret;
172
173 if (!dev || !bo || *bo)
174 return -EINVAL;
175
176 nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
177 if (!nvbo)
178 return -ENOMEM;
179 nvbo->base.device = dev;
180 nvbo->base.size = size;
181 nvbo->base.handle = bo_to_ptr(nvbo);
182 nvbo->drm.alignment = align;
183 nvbo->refcount = 1;
184
185 if (flags & NOUVEAU_BO_TILED) {
186 nvbo->tiled = 1;
187 if (flags & NOUVEAU_BO_ZTILE)
188 nvbo->tiled |= 2;
189 flags &= ~NOUVEAU_BO_TILED;
190 }
191
192 ret = nouveau_bo_set_status(&nvbo->base, flags);
193 if (ret) {
194 free(nvbo);
195 return ret;
196 }
197
198 *bo = &nvbo->base;
199 return 0;
200 }
201
202 int
203 nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
204 struct nouveau_bo **bo)
205 {
206 struct nouveau_bo_priv *nvbo;
207
208 if (!dev || !bo || *bo)
209 return -EINVAL;
210
211 nvbo = calloc(1, sizeof(*nvbo));
212 if (!nvbo)
213 return -ENOMEM;
214 nvbo->base.device = dev;
215
216 nvbo->sysmem = ptr;
217 nvbo->user = 1;
218
219 nvbo->base.size = size;
220 nvbo->base.offset = nvbo->drm.offset;
221 nvbo->base.handle = bo_to_ptr(nvbo);
222 nvbo->refcount = 1;
223 *bo = &nvbo->base;
224 return 0;
225 }
226
227 int
228 nouveau_bo_ref(struct nouveau_device *dev, uint64_t handle,
229 struct nouveau_bo **bo)
230 {
231 struct nouveau_bo_priv *nvbo = ptr_to_bo(handle);
232
233 if (!dev || !bo || *bo)
234 return -EINVAL;
235
236 nvbo->refcount++;
237 *bo = &nvbo->base;
238 return 0;
239 }
240
241 static void
242 nouveau_bo_del_cb(void *priv)
243 {
244 struct nouveau_bo_priv *nvbo = priv;
245
246 nouveau_fence_ref(NULL, &nvbo->fence);
247 nouveau_mem_free(nvbo->base.device, &nvbo->drm, &nvbo->map);
248 if (nvbo->sysmem && !nvbo->user)
249 free(nvbo->sysmem);
250 free(nvbo);
251 }
252
253 void
254 nouveau_bo_del(struct nouveau_bo **bo)
255 {
256 struct nouveau_bo_priv *nvbo;
257
258 if (!bo || !*bo)
259 return;
260 nvbo = nouveau_bo(*bo);
261 *bo = NULL;
262
263 if (--nvbo->refcount)
264 return;
265
266 if (nvbo->pending)
267 nouveau_pushbuf_flush(nvbo->pending->channel, 0);
268
269 if (nvbo->fence)
270 nouveau_fence_signal_cb(nvbo->fence, nouveau_bo_del_cb, nvbo);
271 else
272 nouveau_bo_del_cb(nvbo);
273 }
274
275 int
276 nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
277 {
278 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
279
280 if (!nvbo)
281 return -EINVAL;
282
283 if (nvbo->pending &&
284 (nvbo->pending->flags & NOUVEAU_BO_WR || flags & NOUVEAU_BO_WR)) {
285 nouveau_pushbuf_flush(nvbo->pending->channel, 0);
286 }
287
288 if (flags & NOUVEAU_BO_WR)
289 nouveau_fence_wait(&nvbo->fence);
290 else
291 nouveau_fence_wait(&nvbo->wr_fence);
292
293 if (nvbo->sysmem)
294 bo->map = nvbo->sysmem;
295 else
296 bo->map = nvbo->map;
297 return 0;
298 }
299
300 void
301 nouveau_bo_unmap(struct nouveau_bo *bo)
302 {
303 bo->map = NULL;
304 }
305
306 static int
307 nouveau_bo_upload(struct nouveau_bo_priv *nvbo)
308 {
309 if (nvbo->fence)
310 nouveau_fence_wait(&nvbo->fence);
311 memcpy(nvbo->map, nvbo->sysmem, nvbo->drm.size);
312 return 0;
313 }
314
315 int
316 nouveau_bo_set_status(struct nouveau_bo *bo, uint32_t flags)
317 {
318 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
319 struct drm_nouveau_mem_alloc new;
320 void *new_map = NULL, *new_sysmem = NULL;
321 unsigned new_flags = 0, ret;
322
323 assert(!bo->map);
324
325 /* Check current memtype vs requested, if they match do nothing */
326 if ((nvbo->drm.flags & NOUVEAU_MEM_FB) && (flags & NOUVEAU_BO_VRAM))
327 return 0;
328 if ((nvbo->drm.flags & (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI)) &&
329 (flags & NOUVEAU_BO_GART))
330 return 0;
331 if (nvbo->drm.size == 0 && nvbo->sysmem && (flags & NOUVEAU_BO_LOCAL))
332 return 0;
333
334 memset(&new, 0x00, sizeof(new));
335
336 /* Allocate new memory */
337 if (flags & NOUVEAU_BO_VRAM)
338 new_flags |= NOUVEAU_MEM_FB;
339 else
340 if (flags & NOUVEAU_BO_GART)
341 new_flags |= (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI);
342
343 if (nvbo->tiled && flags) {
344 new_flags |= NOUVEAU_MEM_TILE;
345 if (nvbo->tiled & 2)
346 new_flags |= NOUVEAU_MEM_TILE_ZETA;
347 }
348
349 if (new_flags) {
350 ret = nouveau_mem_alloc(bo->device, bo->size,
351 nvbo->drm.alignment, new_flags,
352 &new, &new_map);
353 if (ret)
354 return ret;
355 } else
356 if (!nvbo->user) {
357 new_sysmem = malloc(bo->size);
358 }
359
360 /* Copy old -> new */
361 /*XXX: use M2MF */
362 if (nvbo->sysmem || nvbo->map) {
363 struct nouveau_pushbuf_bo *pbo = nvbo->pending;
364 nvbo->pending = NULL;
365 nouveau_bo_map(bo, NOUVEAU_BO_RD);
366 memcpy(new_map, bo->map, bo->size);
367 nouveau_bo_unmap(bo);
368 nvbo->pending = pbo;
369 }
370
371 /* Free old memory */
372 if (nvbo->fence)
373 nouveau_fence_wait(&nvbo->fence);
374 nouveau_mem_free(bo->device, &nvbo->drm, &nvbo->map);
375 if (nvbo->sysmem && !nvbo->user)
376 free(nvbo->sysmem);
377
378 nvbo->drm = new;
379 nvbo->map = new_map;
380 if (!nvbo->user)
381 nvbo->sysmem = new_sysmem;
382 bo->flags = flags;
383 bo->offset = nvbo->drm.offset;
384 return 0;
385 }
386
387 static int
388 nouveau_bo_validate_user(struct nouveau_channel *chan, struct nouveau_bo *bo,
389 struct nouveau_fence *fence, uint32_t flags)
390 {
391 struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
392 struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
393 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
394 struct nouveau_resource *r;
395
396 if (nvchan->user_charge + bo->size > nvdev->sa.size)
397 return 1;
398
399 if (!(flags & NOUVEAU_BO_GART))
400 return 1;
401
402 r = nouveau_bo_tmp(chan, bo->size, fence);
403 if (!r)
404 return 1;
405 nvchan->user_charge += bo->size;
406
407 memcpy(nvdev->sa_map + r->start, nvbo->sysmem, bo->size);
408
409 nvbo->offset = nvdev->sa.offset + r->start;
410 nvbo->flags = NOUVEAU_BO_GART;
411 return 0;
412 }
413
414 static int
415 nouveau_bo_validate_bo(struct nouveau_channel *chan, struct nouveau_bo *bo,
416 struct nouveau_fence *fence, uint32_t flags)
417 {
418 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
419 int ret;
420
421 ret = nouveau_bo_set_status(bo, flags);
422 if (ret) {
423 nouveau_fence_flush(chan);
424
425 ret = nouveau_bo_set_status(bo, flags);
426 if (ret)
427 return ret;
428 }
429
430 if (nvbo->user)
431 nouveau_bo_upload(nvbo);
432
433 nvbo->offset = nvbo->drm.offset;
434 if (nvbo->drm.flags & (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI))
435 nvbo->flags = NOUVEAU_BO_GART;
436 else
437 nvbo->flags = NOUVEAU_BO_VRAM;
438
439 return 0;
440 }
441
442 int
443 nouveau_bo_validate(struct nouveau_channel *chan, struct nouveau_bo *bo,
444 uint32_t flags)
445 {
446 struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
447 struct nouveau_fence *fence = nouveau_pushbuf(chan->pushbuf)->fence;
448 int ret;
449
450 assert(bo->map == NULL);
451
452 if (nvbo->user) {
453 ret = nouveau_bo_validate_user(chan, bo, fence, flags);
454 if (ret) {
455 ret = nouveau_bo_validate_bo(chan, bo, fence, flags);
456 if (ret)
457 return ret;
458 }
459 } else {
460 ret = nouveau_bo_validate_bo(chan, bo, fence, flags);
461 if (ret)
462 return ret;
463 }
464
465 if (flags & NOUVEAU_BO_WR)
466 nouveau_fence_ref(fence, &nvbo->wr_fence);
467 nouveau_fence_ref(fence, &nvbo->fence);
468 return 0;
469 }
470