OSDN Git Service

vc4: Don't forget to unmap the GEM BO when freeing.
[android-x86/external-mesa.git] / src / gallium / drivers / vc4 / vc4_bufmgr.c
1 /*
2  * Copyright © 2014 Broadcom
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23
24 #include <errno.h>
25 #include <err.h>
26 #include <stdio.h>
27 #include <sys/mman.h>
28 #include <xf86drm.h>
29 #include <xf86drmMode.h>
30
31 #include "util/u_memory.h"
32
33 #include "vc4_context.h"
34 #include "vc4_screen.h"
35
36 struct vc4_bo *
37 vc4_bo_alloc(struct vc4_screen *screen, uint32_t size, const char *name)
38 {
39         struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);
40         if (!bo)
41                 return NULL;
42
43         pipe_reference_init(&bo->reference, 1);
44         bo->screen = screen;
45         bo->size = size;
46         bo->name = name;
47
48 #ifndef USE_VC4_SIMULATOR
49         struct drm_mode_create_dumb create;
50         memset(&create, 0, sizeof(create));
51
52         create.width = 128;
53         create.bpp = 8;
54         create.height = (size + 127) / 128;
55
56         int ret = drmIoctl(screen->fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
57         if (ret != 0)
58                 errx(1, "create ioctl");
59
60         bo->handle = create.handle;
61         assert(create.size >= size);
62 #else /* USE_VC4_SIMULATOR */
63         static int next_handle = 0;
64         bo->handle = next_handle++;
65
66         bo->map = malloc(size);
67 #endif /* USE_VC4_SIMULATOR */
68
69         return bo;
70 }
71
72 void
73 vc4_bo_free(struct vc4_bo *bo)
74 {
75 #ifndef USE_VC4_SIMULATOR
76         struct vc4_screen *screen = bo->screen;
77
78         if (bo->map)
79                 munmap(bo->map, bo->size);
80
81         struct drm_gem_close c;
82         c.handle = bo->handle;
83         int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_CLOSE, &c);
84         if (ret != 0)
85                 fprintf(stderr, "close object %d: %s\n", bo->handle, strerror(errno));
86 #else
87         free(bo->map);
88 #endif
89
90         free(bo);
91 }
92
93 struct vc4_bo *
94 vc4_bo_open_name(struct vc4_screen *screen, uint32_t name,
95                  uint32_t winsys_stride)
96 {
97         struct vc4_bo *bo = CALLOC_STRUCT(vc4_bo);
98
99         struct drm_gem_open o;
100         o.name = name;
101         int ret = drmIoctl(screen->fd, DRM_IOCTL_GEM_OPEN, &o);
102         if (ret) {
103                 fprintf(stderr, "Failed to open bo %d: %s\n",
104                         name, strerror(errno));
105                 free(bo);
106                 return NULL;
107         }
108
109         pipe_reference_init(&bo->reference, 1);
110         bo->screen = screen;
111         bo->handle = o.handle;
112         bo->size = o.size;
113
114 #ifdef USE_VC4_SIMULATOR
115         vc4_bo_map(bo);
116         bo->simulator_winsys_map = bo->map;
117         bo->simulator_winsys_stride = winsys_stride;
118         bo->map = malloc(bo->size);
119 #endif
120
121         return bo;
122 }
123
124 struct vc4_bo *
125 vc4_bo_alloc_mem(struct vc4_screen *screen, const void *data, uint32_t size,
126                  const char *name)
127 {
128         void *map;
129         struct vc4_bo *bo;
130
131         bo = vc4_bo_alloc(screen, size, name);
132         map = vc4_bo_map(bo);
133         memcpy(map, data, size);
134         return bo;
135 }
136
137 bool
138 vc4_bo_flink(struct vc4_bo *bo, uint32_t *name)
139 {
140 #ifndef USE_VC4_SIMULATOR
141         struct drm_gem_flink flink = {
142                 .handle = bo->handle,
143         };
144         int ret = drmIoctl(bo->screen->fd, DRM_IOCTL_GEM_FLINK, &flink);
145         if (ret) {
146                 fprintf(stderr, "Failed to flink bo %d: %s\n",
147                         bo->handle, strerror(errno));
148                 free(bo);
149                 return false;
150         }
151
152         *name = flink.name;
153 #endif /* USE_VC4_SIMULATOR */
154
155         return true;
156 }
157
158 void *
159 vc4_bo_map(struct vc4_bo *bo)
160 {
161         int ret;
162
163         if (bo->map)
164                 return bo->map;
165
166         struct drm_mode_map_dumb map;
167         memset(&map, 0, sizeof(map));
168         map.handle = bo->handle;
169         ret = drmIoctl(bo->screen->fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
170         if (ret != 0)
171                 errx(1, "map ioctl");
172
173         bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
174                        bo->screen->fd, map.offset);
175         if (bo->map == MAP_FAILED) {
176                 errx(1, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
177                      bo->handle, (long long)map.offset, bo->size);
178         }
179
180         return bo->map;
181 }