hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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
/*
 * Copyright 2010 Rockchip Electronics S.LSI Co. LTD
 *
 * Licensed under the Apache License, Versdrm 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITDRMS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissdrms and
 * limitatdrms under the License.
 */
 
#define MODULE_TAG "mpp_drm"
 
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
 
#include <linux/drm.h>
#include <linux/drm_mode.h>
 
#include "os_mem.h"
#include "allocator_drm.h"
 
#include "mpp_env.h"
#include "mpp_mem.h"
#include "mpp_debug.h"
#include "mpp_common.h"
#include "mpp_runtime.h"
 
static RK_U32 drm_debug = 0;
 
#define DRM_FUNCTION                (0x00000001)
#define DRM_DEVICE                  (0x00000002)
#define DRM_CLIENT                  (0x00000004)
#define DRM_IOCTL                   (0x00000008)
 
#define drm_dbg(flag, fmt, ...)     _mpp_dbg_f(drm_debug, flag, fmt, ## __VA_ARGS__)
#define drm_dbg_func(fmt, ...)      drm_dbg(DRM_FUNCTION, fmt, ## __VA_ARGS__)
 
typedef struct {
    RK_U32  alignment;
    RK_S32  drm_device;
    RK_U32  flags;
} allocator_ctx_drm;
 
static const char *dev_drm = "/dev/dri/card0";
 
static int drm_ioctl(int fd, int req, void *arg)
{
    int ret;
 
    do {
        ret = ioctl(fd, req, arg);
    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
 
    drm_dbg(DRM_IOCTL, "drm_ioctl %x with code %d: %s\n", req,
            ret, strerror(errno));
 
    return ret;
}
 
static int drm_handle_to_fd(int fd, RK_U32 handle, int *map_fd, RK_U32 flags)
{
    int ret;
    struct drm_prime_handle dph;
 
    memset(&dph, 0, sizeof(struct drm_prime_handle));
    dph.handle = handle;
    dph.fd = -1;
    dph.flags = flags;
 
    if (map_fd == NULL)
        return -EINVAL;
 
    ret = drm_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &dph);
    if (ret < 0)
        return ret;
 
    *map_fd = dph.fd;
 
    drm_dbg_func("dev %d handle %d get fd %d\n", fd, handle, *map_fd);
 
    if (*map_fd < 0) {
        mpp_err_f("map ioctl returned negative fd\n");
        return -EINVAL;
    }
 
    return ret;
}
 
static int drm_fd_to_handle(int fd, int map_fd, RK_U32 *handle, RK_U32 flags)
{
    int ret;
    struct drm_prime_handle dph;
 
    dph.fd = map_fd;
    dph.flags = flags;
 
    ret = drm_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &dph);
    if (ret < 0) {
        return ret;
    }
 
    *handle = dph.handle;
 
    drm_dbg_func("dev %d fd %d get handle %d\n", fd, map_fd, *handle);
 
    return ret;
}
 
static int drm_alloc(int fd, size_t len, size_t align, RK_U32 *handle, RK_U32 flags)
{
    int ret;
    struct drm_mode_create_dumb dmcb;
 
    memset(&dmcb, 0, sizeof(struct drm_mode_create_dumb));
    dmcb.bpp = 8;
    dmcb.width = (len + align - 1) & (~(align - 1));
    dmcb.height = 1;
    dmcb.flags = flags;
 
    if (handle == NULL)
        return -EINVAL;
 
    ret = drm_ioctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &dmcb);
    if (ret < 0)
        return ret;
 
    *handle = dmcb.handle;
    drm_dbg_func("dev %d alloc aligned %d size %lld handle %d\n", fd,
                 align, dmcb.size, dmcb.handle);
 
    return ret;
}
 
static int drm_free(int fd, RK_U32 handle)
{
    struct drm_mode_destroy_dumb data = {
        .handle = handle,
    };
    return drm_ioctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
}
 
static MPP_RET os_allocator_drm_open(void **ctx, MppAllocatorCfg *cfg)
{
    RK_S32 fd;
    allocator_ctx_drm *p;
 
    drm_dbg_func("enter\n");
 
    if (NULL == ctx) {
        mpp_err_f("does not accept NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    *ctx = NULL;
 
    mpp_env_get_u32("drm_debug", &drm_debug, 0);
 
    fd = open(dev_drm, O_RDWR | O_CLOEXEC);
    if (fd < 0) {
        mpp_err_f("open %s failed!\n", dev_drm);
        return MPP_ERR_UNKNOW;
    }
 
    drm_dbg(DRM_DEVICE, "open drm dev fd %d\n", fd);
 
    p = mpp_malloc(allocator_ctx_drm, 1);
    if (NULL == p) {
        close(fd);
        mpp_err_f("failed to allocate context\n");
        return MPP_ERR_MALLOC;
    } else {
        /*
         * default drm use cma, do nothing here
         */
        p->alignment    = cfg->alignment;
        p->flags        = cfg->flags;
        p->drm_device   = fd;
        *ctx = p;
    }
 
    drm_dbg_func("leave dev %d\n", fd);
 
    return MPP_OK;
}
 
static MPP_RET os_allocator_drm_alloc(void *ctx, MppBufferInfo *info)
{
    MPP_RET ret = MPP_OK;
    allocator_ctx_drm *p = NULL;
 
    if (NULL == ctx) {
        mpp_err_f("does not accept NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    p = (allocator_ctx_drm *)ctx;
 
    drm_dbg_func("dev %d alloc alignment %d size %d\n", p->drm_device,
                 p->alignment, info->size);
 
    ret = drm_alloc(p->drm_device, info->size, p->alignment,
                    (RK_U32 *)&info->hnd, p->flags);
    if (ret) {
        mpp_err_f("drm_alloc failed ret %d\n", ret);
        return ret;
    }
 
    drm_dbg_func("dev %d get handle %d", p->drm_device,
                 (RK_U32)((intptr_t)info->hnd));
 
    ret = drm_handle_to_fd(p->drm_device, (RK_U32)((intptr_t)info->hnd),
                           &info->fd, DRM_CLOEXEC | DRM_RDWR);
 
    if (ret) {
        mpp_err_f("handle_to_fd failed ret %d\n", ret);
        return ret;
    }
 
    info->ptr = NULL;
    return ret;
}
 
static MPP_RET os_allocator_drm_import(void *ctx, MppBufferInfo *data)
{
    MPP_RET ret = MPP_OK;
    allocator_ctx_drm *p = (allocator_ctx_drm *)ctx;
 
    drm_dbg_func("enter dev %d\n", p->drm_device);
 
    ret = drm_fd_to_handle(p->drm_device, data->fd, (RK_U32 *)&data->hnd, 0);
 
    ret = drm_handle_to_fd(p->drm_device, (RK_U32)((intptr_t)data->hnd),
                           &data->fd, DRM_CLOEXEC | DRM_RDWR);
 
    data->ptr = NULL;
    drm_dbg_func("leave dev %d\n", p->drm_device);
 
    return ret;
}
 
static MPP_RET os_allocator_drm_free(void *ctx, MppBufferInfo *data)
{
    allocator_ctx_drm *p = NULL;
 
    if (NULL == ctx) {
        mpp_err_f("does not accept NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    p = (allocator_ctx_drm *)ctx;
 
    /* NOTE: update hnd avoid freed hnd */
    drm_fd_to_handle(p->drm_device, data->fd, (RK_U32 *)&data->hnd, 0);
 
    drm_dbg_func("dev %d unmap %p size %d fd %d handle %p\n", p->drm_device,
                 data->ptr, data->size, data->fd, data->hnd);
 
    if (data->ptr) {
        munmap(data->ptr, data->size);
        data->ptr = NULL;
    }
    close(data->fd);
 
    return drm_free(p->drm_device, (RK_U32)((intptr_t)data->hnd));
}
 
static MPP_RET os_allocator_drm_close(void *ctx)
{
    int ret;
    allocator_ctx_drm *p;
 
    if (NULL == ctx) {
        mpp_err("os_allocator_close doesn't accept NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
 
    p = (allocator_ctx_drm *)ctx;
    drm_dbg_func("dev %d", p->drm_device);
 
    ret = close(p->drm_device);
    mpp_free(p);
    if (ret < 0)
        return (MPP_RET) - errno;
 
    return MPP_OK;
}
 
static MPP_RET os_allocator_drm_mmap(void *ctx, MppBufferInfo *data)
{
    allocator_ctx_drm *p;
    MPP_RET ret = MPP_OK;
    if (NULL == ctx) {
        mpp_err("os_allocator_close do not accept NULL input\n");
        return MPP_ERR_NULL_PTR;
    }
    p = (allocator_ctx_drm *)ctx;
 
    if (NULL == ctx)
        return MPP_ERR_NULL_PTR;
 
    if (NULL == data->ptr) {
        int flags = PROT_READ;
 
        if (fcntl(data->fd, F_GETFL) & O_RDWR)
            flags |= PROT_WRITE;
 
        data->ptr = mmap(NULL, data->size, flags, MAP_SHARED, data->fd, 0);
        if (data->ptr == MAP_FAILED) {
            mpp_err("mmap failed: %s\n", strerror(errno));
            data->ptr = NULL;
            return -errno;
        }
 
        drm_dbg_func("dev %d mmap fd %d to %p (%s)\n", p->drm_device,
                     data->fd, data->ptr,
                     flags & PROT_WRITE ? "RDWR" : "RDONLY");
    }
 
    return ret;
}
 
os_allocator allocator_drm = {
    .open = os_allocator_drm_open,
    .close = os_allocator_drm_close,
    .alloc = os_allocator_drm_alloc,
    .free = os_allocator_drm_free,
    .import = os_allocator_drm_import,
    .release = os_allocator_drm_free,
    .mmap = os_allocator_drm_mmap,
};