1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include "nouveau_drmif.h"
#include "nouveau_dma.h"
#include "nouveau_local.h"
static void
nouveau_mem_free(struct nouveau_device *dev, struct drm_nouveau_mem_alloc *ma,
void **map)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
struct drm_nouveau_mem_free mf;
if (map && *map) {
drmUnmap(*map, ma->size);
*map = NULL;
}
if (ma->size) {
mf.offset = ma->offset;
mf.flags = ma->flags;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_FREE,
&mf, sizeof(mf));
ma->size = 0;
}
}
static int
nouveau_mem_alloc(struct nouveau_device *dev, unsigned size, unsigned align,
uint32_t flags, struct drm_nouveau_mem_alloc *ma, void **map)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
int ret;
ma->alignment = align;
ma->size = size;
ma->flags = flags;
if (map)
ma->flags |= NOUVEAU_MEM_MAPPED;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_MEM_ALLOC, ma,
sizeof(struct drm_nouveau_mem_alloc));
if (ret)
return ret;
if (map) {
ret = drmMap(nvdev->fd, ma->map_handle, ma->size, map);
if (ret) {
*map = NULL;
nouveau_mem_free(dev, ma, map);
return ret;
}
}
return 0;
}
static void
nouveau_bo_tmp_del(void *priv)
{
struct nouveau_resource *r = priv;
nouveau_fence_ref(NULL, (struct nouveau_fence **)&r->priv);
nouveau_resource_free(&r);
}
static unsigned
nouveau_bo_tmp_max(struct nouveau_device_priv *nvdev)
{
struct nouveau_resource *r = nvdev->sa_heap;
unsigned max = 0;
while (r) {
if (r->in_use && !nouveau_fence(r->priv)->emitted) {
r = r->next;
continue;
}
if (max < r->size)
max = r->size;
r = r->next;
}
return max;
}
static struct nouveau_resource *
nouveau_bo_tmp(struct nouveau_channel *chan, unsigned size,
struct nouveau_fence *fence)
{
struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
struct nouveau_resource *r = NULL;
struct nouveau_fence *ref = NULL;
if (fence)
nouveau_fence_ref(fence, &ref);
else
nouveau_fence_new(chan, &ref);
assert(ref);
while (nouveau_resource_alloc(nvdev->sa_heap, size, ref, &r)) {
if (nouveau_bo_tmp_max(nvdev) < size) {
nouveau_fence_ref(NULL, &ref);
return NULL;
}
nouveau_fence_flush(chan);
}
nouveau_fence_signal_cb(ref, nouveau_bo_tmp_del, r);
return r;
}
int
nouveau_bo_init(struct nouveau_device *dev)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
int ret;
ret = nouveau_mem_alloc(dev, 128*1024, 0, NOUVEAU_MEM_AGP |
NOUVEAU_MEM_PCI, &nvdev->sa, &nvdev->sa_map);
if (ret)
return ret;
ret = nouveau_resource_init(&nvdev->sa_heap, 0, nvdev->sa.size);
if (ret) {
nouveau_mem_free(dev, &nvdev->sa, &nvdev->sa_map);
return ret;
}
return 0;
}
void
nouveau_bo_takedown(struct nouveau_device *dev)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
nouveau_mem_free(dev, &nvdev->sa, &nvdev->sa_map);
}
int
nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
int size, struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
int ret;
if (!dev || !bo || *bo)
return -EINVAL;
nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
if (!nvbo)
return -ENOMEM;
nvbo->base.device = dev;
nvbo->base.size = size;
nvbo->base.handle = bo_to_ptr(nvbo);
nvbo->drm.alignment = align;
nvbo->refcount = 1;
ret = nouveau_bo_set_status(&nvbo->base, flags);
if (ret) {
free(nvbo);
return ret;
}
*bo = &nvbo->base;
return 0;
}
int
nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
if (!dev || !bo || *bo)
return -EINVAL;
nvbo = calloc(1, sizeof(*nvbo));
if (!nvbo)
return -ENOMEM;
nvbo->base.device = dev;
nvbo->sysmem = ptr;
nvbo->user = 1;
nvbo->base.size = size;
nvbo->base.offset = nvbo->drm.offset;
nvbo->base.handle = bo_to_ptr(nvbo);
nvbo->refcount = 1;
*bo = &nvbo->base;
return 0;
}
int
nouveau_bo_ref(struct nouveau_device *dev, uint64_t handle,
struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo = ptr_to_bo(handle);
if (!dev || !bo || *bo)
return -EINVAL;
nvbo->refcount++;
*bo = &nvbo->base;
return 0;
}
static void
nouveau_bo_del_cb(void *priv)
{
struct nouveau_bo_priv *nvbo = priv;
nouveau_fence_ref(NULL, &nvbo->fence);
nouveau_mem_free(nvbo->base.device, &nvbo->drm, &nvbo->map);
if (nvbo->sysmem && !nvbo->user)
free(nvbo->sysmem);
free(nvbo);
}
void
nouveau_bo_del(struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
if (!bo || !*bo)
return;
nvbo = nouveau_bo(*bo);
*bo = NULL;
if (--nvbo->refcount)
return;
if (nvbo->fence)
nouveau_fence_signal_cb(nvbo->fence, nouveau_bo_del_cb, nvbo);
else
nouveau_bo_del_cb(nvbo);
}
int
nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
if (!nvbo)
return -EINVAL;
if (flags & NOUVEAU_BO_WR)
nouveau_fence_wait(&nvbo->fence);
else
nouveau_fence_wait(&nvbo->wr_fence);
if (nvbo->sysmem)
bo->map = nvbo->sysmem;
else
bo->map = nvbo->map;
return 0;
}
void
nouveau_bo_unmap(struct nouveau_bo *bo)
{
bo->map = NULL;
}
static int
nouveau_bo_upload(struct nouveau_bo_priv *nvbo)
{
if (nvbo->fence)
nouveau_fence_wait(&nvbo->fence);
memcpy(nvbo->map, nvbo->sysmem, nvbo->drm.size);
return 0;
}
int
nouveau_bo_set_status(struct nouveau_bo *bo, uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct drm_nouveau_mem_alloc new;
void *new_map = NULL, *new_sysmem = NULL;
unsigned new_flags = 0, ret;
assert(!bo->map);
/* Check current memtype vs requested, if they match do nothing */
if ((nvbo->drm.flags & NOUVEAU_MEM_FB) && (flags & NOUVEAU_BO_VRAM))
return 0;
if ((nvbo->drm.flags & NOUVEAU_MEM_AGP) && (flags & NOUVEAU_BO_GART))
return 0;
if (nvbo->drm.size == 0 && nvbo->sysmem && (flags & NOUVEAU_BO_LOCAL))
return 0;
memset(&new, 0x00, sizeof(new));
/* Allocate new memory */
if (flags & NOUVEAU_BO_VRAM)
new_flags |= NOUVEAU_MEM_FB;
else
if (flags & NOUVEAU_BO_GART)
new_flags |= (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI);
if (new_flags) {
ret = nouveau_mem_alloc(bo->device, bo->size,
nvbo->drm.alignment, new_flags,
&new, &new_map);
if (ret)
return ret;
} else
if (!nvbo->user) {
new_sysmem = malloc(bo->size);
}
/* Copy old -> new */
/*XXX: use M2MF */
if (nvbo->sysmem || nvbo->map) {
nouveau_bo_map(bo, NOUVEAU_BO_RD);
memcpy(new_map, bo->map, bo->size);
nouveau_bo_unmap(bo);
}
/* Free old memory */
if (nvbo->fence)
nouveau_fence_wait(&nvbo->fence);
nouveau_mem_free(bo->device, &nvbo->drm, &nvbo->map);
if (nvbo->sysmem && !nvbo->user)
free(nvbo->sysmem);
nvbo->drm = new;
nvbo->map = new_map;
if (!nvbo->user)
nvbo->sysmem = new_sysmem;
bo->flags = flags;
bo->offset = nvbo->drm.offset;
return 0;
}
static int
nouveau_bo_validate_user(struct nouveau_channel *chan, struct nouveau_bo *bo,
struct nouveau_fence *fence, uint32_t flags)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct nouveau_resource *r;
if (nvchan->user_charge + bo->size > nvdev->sa.size)
return 1;
if (!(flags & NOUVEAU_BO_GART))
return 1;
r = nouveau_bo_tmp(chan, bo->size, fence);
if (!r)
return 1;
nvchan->user_charge += bo->size;
memcpy(nvdev->sa_map + r->start, nvbo->sysmem, bo->size);
nvbo->offset = nvdev->sa.offset + r->start;
nvbo->flags = NOUVEAU_BO_GART;
return 0;
}
static int
nouveau_bo_validate_bo(struct nouveau_channel *chan, struct nouveau_bo *bo,
struct nouveau_fence *fence, uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
int ret;
ret = nouveau_bo_set_status(bo, flags);
if (ret) {
nouveau_fence_flush(chan);
ret = nouveau_bo_set_status(bo, flags);
if (ret)
return ret;
}
if (nvbo->user)
nouveau_bo_upload(nvbo);
nvbo->offset = nvbo->drm.offset;
if (nvbo->drm.flags & (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI))
nvbo->flags = NOUVEAU_BO_GART;
else
nvbo->flags = NOUVEAU_BO_VRAM;
return 0;
}
int
nouveau_bo_validate(struct nouveau_channel *chan, struct nouveau_bo *bo,
uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct nouveau_fence *fence = nouveau_pushbuf(chan->pushbuf)->fence;
int ret;
assert(bo->map == NULL);
if (nvbo->user) {
ret = nouveau_bo_validate_user(chan, bo, fence, flags);
if (ret) {
ret = nouveau_bo_validate_bo(chan, bo, fence, flags);
if (ret)
return ret;
}
} else {
ret = nouveau_bo_validate_bo(chan, bo, fence, flags);
if (ret)
return ret;
}
if (flags & NOUVEAU_BO_WR)
nouveau_fence_ref(fence, &nvbo->wr_fence);
nouveau_fence_ref(fence, &nvbo->fence);
return 0;
}
|