OSDN Git Service

libkms/radeon: Add backend
[android-x86/external-libdrm.git] / 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <assert.h>
30
31 #include <sys/mman.h>
32
33 #include "nouveau_private.h"
34
35 int
36 nouveau_bo_init(struct nouveau_device *dev)
37 {
38         return 0;
39 }
40
41 void
42 nouveau_bo_takedown(struct nouveau_device *dev)
43 {
44 }
45
46 static int
47 nouveau_bo_info(struct nouveau_bo_priv *nvbo, struct drm_nouveau_gem_info *arg)
48 {
49         nvbo->handle = nvbo->base.handle = arg->handle;
50         nvbo->domain = arg->domain;
51         nvbo->size = arg->size;
52         nvbo->offset = arg->offset;
53         nvbo->map_handle = arg->map_handle;
54         nvbo->base.tile_mode = arg->tile_mode;
55         /* XXX - flag inverted for backwards compatibility */
56         nvbo->base.tile_flags = arg->tile_flags ^ NOUVEAU_GEM_TILE_NONCONTIG;
57         return 0;
58 }
59
60 static int
61 nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
62 {
63         if (nvbo->sysmem || nvbo->handle)
64                 return 1;
65         return 0;
66 }
67
68 static int
69 nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
70 {
71         if (nvbo->user || nvbo->sysmem) {
72                 assert(nvbo->sysmem);
73                 return 0;
74         }
75
76         nvbo->sysmem = malloc(nvbo->size);
77         if (!nvbo->sysmem)
78                 return -ENOMEM;
79
80         return 0;
81 }
82
83 static void
84 nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
85 {
86         if (nvbo->sysmem) {
87                 if (!nvbo->user)
88                         free(nvbo->sysmem);
89                 nvbo->sysmem = NULL;
90         }
91 }
92
93 static void
94 nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
95 {
96         struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
97         struct drm_gem_close req;
98
99         if (!nvbo->handle)
100                 return;
101
102         if (nvbo->map) {
103                 munmap(nvbo->map, nvbo->size);
104                 nvbo->map = NULL;
105         }
106
107         req.handle = nvbo->handle;
108         nvbo->handle = 0;
109         drmIoctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
110 }
111
112 static int
113 nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
114 {
115         struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
116         struct drm_nouveau_gem_new req;
117         struct drm_nouveau_gem_info *info = &req.info;
118         int ret;
119
120         if (nvbo->handle)
121                 return 0;
122
123         req.channel_hint = chan ? chan->id : 0;
124         req.align = nvbo->align;
125
126
127         info->size = nvbo->size;
128         info->domain = 0;
129
130         if (nvbo->flags & NOUVEAU_BO_VRAM)
131                 info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
132         if (nvbo->flags & NOUVEAU_BO_GART)
133                 info->domain |= NOUVEAU_GEM_DOMAIN_GART;
134         if (!info->domain) {
135                 info->domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
136                                  NOUVEAU_GEM_DOMAIN_GART);
137         }
138
139         if (nvbo->flags & NOUVEAU_BO_MAP)
140                 info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
141
142         info->tile_mode = nvbo->base.tile_mode;
143         info->tile_flags = nvbo->base.tile_flags;
144         /* XXX - flag inverted for backwards compatibility */
145         info->tile_flags ^= NOUVEAU_GEM_TILE_NONCONTIG;
146         if (!nvdev->has_bo_usage)
147                 info->tile_flags &= NOUVEAU_GEM_TILE_LAYOUT_MASK;
148
149         ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
150                                   &req, sizeof(req));
151         if (ret)
152                 return ret;
153
154         nouveau_bo_info(nvbo, &req.info);
155         return 0;
156 }
157
158 static int
159 nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
160 {
161         struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
162
163         if (nvbo->map)
164                 return 0;
165
166         if (!nvbo->map_handle)
167                 return -EINVAL;
168
169         nvbo->map = mmap(0, nvbo->size, PROT_READ | PROT_WRITE,
170                          MAP_SHARED, nvdev->fd, nvbo->map_handle);
171         if (nvbo->map == MAP_FAILED) {
172                 nvbo->map = NULL;
173                 return -errno;
174         }
175
176         return 0;
177 }
178
179 int
180 nouveau_bo_new_tile(struct nouveau_device *dev, uint32_t flags, int align,
181                     int size, uint32_t tile_mode, uint32_t tile_flags,
182                     struct nouveau_bo **bo)
183 {
184         struct nouveau_bo_priv *nvbo;
185         int ret;
186
187         if (!dev || !bo || *bo)
188                 return -EINVAL;
189
190         nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
191         if (!nvbo)
192                 return -ENOMEM;
193         nvbo->base.device = dev;
194         nvbo->base.size = size;
195         nvbo->base.tile_mode = tile_mode;
196         nvbo->base.tile_flags = tile_flags;
197
198         nvbo->refcount = 1;
199         nvbo->flags = flags;
200         nvbo->size = size;
201         nvbo->align = align;
202
203         if (flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
204                 ret = nouveau_bo_kalloc(nvbo, NULL);
205                 if (ret) {
206                         nouveau_bo_ref(NULL, (void *)&nvbo);
207                         return ret;
208                 }
209         }
210
211         *bo = &nvbo->base;
212         return 0;
213 }
214
215 int
216 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
217                int size, struct nouveau_bo **bo)
218 {
219         return nouveau_bo_new_tile(dev, flags, align, size, 0, 0, bo);
220 }
221
222 int
223 nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
224                 struct nouveau_bo **bo)
225 {
226         struct nouveau_bo_priv *nvbo;
227         int ret;
228
229         ret = nouveau_bo_new(dev, NOUVEAU_BO_MAP, 0, size, bo);
230         if (ret)
231                 return ret;
232         nvbo = nouveau_bo(*bo);
233
234         nvbo->sysmem = ptr;
235         nvbo->user = 1;
236         return 0;
237 }
238
239 int
240 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
241                 struct nouveau_bo **bo)
242 {
243         struct nouveau_device_priv *nvdev = nouveau_device(dev);
244         struct drm_nouveau_gem_info req;
245         struct nouveau_bo_priv *nvbo;
246         int ret;
247
248         ret = nouveau_bo_new(dev, 0, 0, 0, bo);
249         if (ret)
250                 return ret;
251         nvbo = nouveau_bo(*bo);
252
253         req.handle = handle;
254         ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_INFO,
255                                   &req, sizeof(req));
256         if (ret) {
257                 nouveau_bo_ref(NULL, bo);
258                 return ret;
259         }
260
261         nouveau_bo_info(nvbo, &req);
262         nvbo->base.size = nvbo->size;
263         return 0;
264 }
265
266 int
267 nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
268 {
269         struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
270         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
271         int ret;
272  
273         if (!bo || !handle)
274                 return -EINVAL;
275
276         if (!nvbo->global_handle) {
277                 struct drm_gem_flink req;
278  
279                 ret = nouveau_bo_kalloc(nvbo, NULL);
280                 if (ret)
281                         return ret;
282
283                 req.handle = nvbo->handle;
284                 ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
285                 if (ret) {
286                         nouveau_bo_kfree(nvbo);
287                         return ret;
288                 }
289
290                 nvbo->global_handle = req.name;
291         }
292  
293         *handle = nvbo->global_handle;
294         return 0;
295 }
296  
297 int
298 nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
299                       struct nouveau_bo **bo)
300 {
301         struct nouveau_device_priv *nvdev = nouveau_device(dev);
302         struct nouveau_bo_priv *nvbo;
303         struct drm_gem_open req;
304         int ret;
305
306         req.name = handle;
307         ret = drmIoctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
308         if (ret) {
309                 nouveau_bo_ref(NULL, bo);
310                 return ret;
311         }
312
313         ret = nouveau_bo_wrap(dev, req.handle, bo);
314         if (ret) {
315                 nouveau_bo_ref(NULL, bo);
316                 return ret;
317         }
318
319         nvbo = nouveau_bo(*bo);
320         nvbo->base.handle = nvbo->handle;
321         return 0;
322
323
324 static void
325 nouveau_bo_del(struct nouveau_bo **bo)
326 {
327         struct nouveau_bo_priv *nvbo;
328
329         if (!bo || !*bo)
330                 return;
331         nvbo = nouveau_bo(*bo);
332         *bo = NULL;
333
334         if (--nvbo->refcount)
335                 return;
336
337         if (nvbo->pending) {
338                 nvbo->pending = NULL;
339                 nouveau_pushbuf_flush(nvbo->pending_channel, 0);
340         }
341
342         nouveau_bo_ufree(nvbo);
343         nouveau_bo_kfree(nvbo);
344         free(nvbo);
345 }
346
347 int
348 nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
349 {
350         if (!pbo)
351                 return -EINVAL;
352
353         if (ref)
354                 nouveau_bo(ref)->refcount++;
355
356         if (*pbo)
357                 nouveau_bo_del(pbo);
358
359         *pbo = ref;
360         return 0;
361 }
362
363 static int
364 nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write, int no_wait, int no_block)
365 {
366         struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
367         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
368         struct drm_nouveau_gem_cpu_prep req;
369         int ret;
370
371         if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
372                 return 0;
373
374         if (nvbo->pending &&
375             (nvbo->pending->write_domains || cpu_write)) {
376                 nvbo->pending = NULL;
377                 nouveau_pushbuf_flush(nvbo->pending_channel, 0);
378         }
379
380         req.handle = nvbo->handle;
381         req.flags = 0;
382         if (cpu_write)
383                 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
384         if (no_wait)
385                 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
386         if (no_block)
387                 req.flags |= NOUVEAU_GEM_CPU_PREP_NOBLOCK;
388
389         do {
390                 ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
391                                       &req, sizeof(req));
392         } while (ret == -EAGAIN);
393         if (ret)
394                 return ret;
395
396         if (ret == 0)
397                 nvbo->write_marker = 0;
398         return 0;
399 }
400
401 int
402 nouveau_bo_map_range(struct nouveau_bo *bo, uint32_t delta, uint32_t size,
403                      uint32_t flags)
404 {
405         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
406         int ret;
407
408         if (!nvbo || bo->map)
409                 return -EINVAL;
410
411         if (!nouveau_bo_allocated(nvbo)) {
412                 if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
413                         ret = nouveau_bo_kalloc(nvbo, NULL);
414                         if (ret)
415                                 return ret;
416                 }
417
418                 if (!nouveau_bo_allocated(nvbo)) {
419                         ret = nouveau_bo_ualloc(nvbo);
420                         if (ret)
421                                 return ret;
422                 }
423         }
424
425         if (nvbo->sysmem) {
426                 bo->map = (char *)nvbo->sysmem + delta;
427         } else {
428                 ret = nouveau_bo_kmap(nvbo);
429                 if (ret)
430                         return ret;
431
432                 if (!(flags & NOUVEAU_BO_NOSYNC)) {
433                         ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR),
434                                               (flags & NOUVEAU_BO_NOWAIT), 0);
435                         if (ret)
436                                 return ret;
437
438                         nvbo->map_refcnt++;
439                 }
440
441                 bo->map = (char *)nvbo->map + delta;
442         }
443
444         return 0;
445 }
446
447 void
448 nouveau_bo_map_flush(struct nouveau_bo *bo, uint32_t delta, uint32_t size)
449 {
450 }
451
452 int
453 nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
454 {
455         return nouveau_bo_map_range(bo, 0, bo->size, flags);
456 }
457
458 void
459 nouveau_bo_unmap(struct nouveau_bo *bo)
460 {
461         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
462
463         if (bo->map && !nvbo->sysmem && nvbo->map_refcnt) {
464                 struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
465                 struct drm_nouveau_gem_cpu_fini req;
466
467                 req.handle = nvbo->handle;
468                 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
469                                 &req, sizeof(req));
470                 nvbo->map_refcnt--;
471         }
472
473         bo->map = NULL;
474 }
475
476 int
477 nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
478 {
479         return nouveau_bo_wait(bo, (access & NOUVEAU_BO_WR), 1, 1);
480 }
481
482 uint32_t
483 nouveau_bo_pending(struct nouveau_bo *bo)
484 {
485         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
486         uint32_t flags;
487
488         if (!nvbo->pending)
489                 return 0;
490
491         flags = 0;
492         if (nvbo->pending->read_domains)
493                 flags |= NOUVEAU_BO_RD;
494         if (nvbo->pending->write_domains)
495                 flags |= NOUVEAU_BO_WR;
496
497         return flags;
498 }
499
500 struct drm_nouveau_gem_pushbuf_bo *
501 nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
502 {
503         struct nouveau_pushbuf_priv *nvpb = &nouveau_channel(chan)->pb;
504         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
505         struct drm_nouveau_gem_pushbuf_bo *pbbo;
506         struct nouveau_bo *ref = NULL;
507         int ret;
508
509         if (nvbo->pending)
510                 return nvbo->pending;
511
512         if (!nvbo->handle) {
513                 ret = nouveau_bo_kalloc(nvbo, chan);
514                 if (ret)
515                         return NULL;
516
517                 if (nvbo->sysmem) {
518                         void *sysmem_tmp = nvbo->sysmem;
519
520                         nvbo->sysmem = NULL;
521                         ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
522                         if (ret)
523                                 return NULL;
524                         nvbo->sysmem = sysmem_tmp;
525
526                         memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
527                         nouveau_bo_ufree(nvbo);
528                         nouveau_bo_unmap(bo);
529                 }
530         }
531
532         if (nvpb->nr_buffers >= NOUVEAU_GEM_MAX_BUFFERS)
533                 return NULL;
534         pbbo = nvpb->buffers + nvpb->nr_buffers++;
535         nvbo->pending = pbbo;
536         nvbo->pending_channel = chan;
537         nvbo->pending_refcnt = 0;
538
539         nouveau_bo_ref(bo, &ref);
540         pbbo->user_priv = (uint64_t)(unsigned long)ref;
541         pbbo->handle = nvbo->handle;
542         pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
543         pbbo->read_domains = 0;
544         pbbo->write_domains = 0;
545         pbbo->presumed.domain = nvbo->domain;
546         pbbo->presumed.offset = nvbo->offset;
547         pbbo->presumed.valid = 1;
548         return pbbo;
549 }