xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/drivers/modesetting/dumb_bo.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2007 Red Hat, Inc.
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Dave Airlie <airlied@redhat.com>
25  *
26  */
27 
28 #ifdef HAVE_DIX_CONFIG_H
29 #include "dix-config.h"
30 #endif
31 
32 #include "dumb_bo.h"
33 
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <unistd.h>
40 #include <xf86drm.h>
41 
42 struct dumb_bo *
dumb_bo_create(int fd,const unsigned width,const unsigned height,const unsigned bpp)43 dumb_bo_create(int fd,
44                const unsigned width, const unsigned height, const unsigned bpp)
45 {
46     struct drm_mode_create_dumb arg;
47     struct dumb_bo *bo;
48     int ret;
49 
50     bo = calloc(1, sizeof(*bo));
51     if (!bo)
52         return NULL;
53 
54     memset(&arg, 0, sizeof(arg));
55     arg.width = width;
56     arg.height = height;
57     arg.bpp = bpp;
58 
59     ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
60     if (ret)
61         goto err_free;
62 
63     bo->handle = arg.handle;
64     bo->size = arg.size;
65     bo->pitch = arg.pitch;
66 
67     return bo;
68  err_free:
69     free(bo);
70     return NULL;
71 }
72 
73 int
dumb_bo_map(int fd,struct dumb_bo * bo)74 dumb_bo_map(int fd, struct dumb_bo *bo)
75 {
76     struct drm_mode_map_dumb arg;
77     int ret;
78     void *map;
79 
80     if (bo->ptr) {
81         return 0;
82     }
83 
84     memset(&arg, 0, sizeof(arg));
85     arg.handle = bo->handle;
86 
87     ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
88     if (ret)
89         return ret;
90 
91     map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, arg.offset);
92     if (map == MAP_FAILED)
93         return -errno;
94 
95     bo->ptr = map;
96     return 0;
97 }
98 
99 int
dumb_bo_get_fd(int fd,struct dumb_bo * bo,uint32_t flags)100 dumb_bo_get_fd(int fd, struct dumb_bo *bo, uint32_t flags)
101 {
102     struct drm_prime_handle args;
103     int ret;
104 
105     memset(&args, 0, sizeof(args));
106     args.fd = -1;
107     args.handle = bo->handle;
108     args.flags = flags;
109 
110     ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
111     if (ret)
112         return ret;
113 
114     return args.fd;
115 }
116 
117 /* From glamor_get_flink_name() */
118 int
dumb_bo_get_name(int fd,struct dumb_bo * bo)119 dumb_bo_get_name(int fd, struct dumb_bo *bo)
120 {
121     struct drm_gem_flink flink;
122 
123     flink.handle = bo->handle;
124     if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) < 0) {
125         /*
126          * Assume non-GEM kernels have names identical to the handle
127          */
128         if (errno == ENODEV)
129             return bo->handle;
130 
131         return -1;
132     }
133     return flink.name;
134 }
135 
136 int
dumb_bo_destroy(int fd,struct dumb_bo * bo)137 dumb_bo_destroy(int fd, struct dumb_bo *bo)
138 {
139     struct drm_mode_destroy_dumb arg;
140     int ret;
141 
142     if (bo->ptr) {
143         munmap(bo->ptr, bo->size);
144         bo->ptr = NULL;
145     }
146 
147     memset(&arg, 0, sizeof(arg));
148     arg.handle = bo->handle;
149     ret = drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
150     if (ret)
151         return -errno;
152 
153     free(bo);
154     return 0;
155 }
156 
157 struct dumb_bo *
dumb_get_bo_from_handle(int fd,int handle,int pitch,int size)158 dumb_get_bo_from_handle(int fd, int handle, int pitch, int size)
159 {
160     struct dumb_bo *bo;
161 
162     bo = calloc(1, sizeof(*bo));
163     if (!bo)
164         return NULL;
165 
166     bo->handle = handle;
167     bo->pitch = pitch;
168     bo->size = size;
169     return bo;
170 }
171 
172 struct dumb_bo *
dumb_get_bo_from_fd(int fd,int dmafd,int pitch,int size)173 dumb_get_bo_from_fd(int fd, int dmafd, int pitch, int size)
174 {
175     unsigned int handle;
176     int ret;
177 
178     ret = drmPrimeFDToHandle(fd, dmafd, &handle);
179     if (ret)
180         return NULL;
181 
182     return dumb_get_bo_from_handle(fd, handle, pitch, size);
183 }
184