-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathevdi_fb.c
More file actions
220 lines (181 loc) · 5.14 KB
/
evdi_fb.c
File metadata and controls
220 lines (181 loc) · 5.14 KB
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
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2012 Red Hat
* Copyright (c) 2015 - 2020 DisplayLink (UK) Ltd.
* Copyright (c) 2025 Lindroid Authors
*
* This file is subject to the terms and conditions of the GNU General Public
* License v2. See the file COPYING in the main directory of this archive for
* more details.
*/
#include "evdi_drv.h"
#include <drm/drm_file.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_gem.h>
#include <linux/overflow.h>
#include <linux/file.h>
static struct kmem_cache *evdi_fb_cache;
int evdi_fb_cache_init(void)
{
evdi_fb_cache = kmem_cache_create("evdi_framebuffer",
sizeof(struct evdi_framebuffer),
0, SLAB_HWCACHE_ALIGN, NULL);
if (!evdi_fb_cache)
return -ENOMEM;
return 0;
}
void evdi_fb_cache_cleanup(void)
{
if (evdi_fb_cache) {
kmem_cache_destroy(evdi_fb_cache);
evdi_fb_cache = NULL;
}
}
static struct evdi_framebuffer *evdi_fb_alloc(gfp_t gfp)
{
if (unlikely(!evdi_fb_cache))
return NULL;
return kmem_cache_zalloc(evdi_fb_cache, gfp);
}
static inline void evdi_gem_object_put_local(struct drm_gem_object *obj)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 0, 0)
drm_gem_object_put(obj);
#else
drm_gem_object_put_unlocked(obj);
#endif
}
static void evdi_fb_destroy(struct drm_framebuffer *fb)
{
struct evdi_framebuffer *efb = to_evdi_fb(fb);
if (efb->obj)
evdi_gem_object_put_local(&efb->obj->base);
drm_framebuffer_cleanup(fb);
// Closing fb isnt same as destroying buffer, so DONT enque destroy
kmem_cache_free(evdi_fb_cache, efb);
}
static int evdi_fb_create_handle(struct drm_framebuffer *fb,
struct drm_file *file,
unsigned int *handle)
{
struct evdi_framebuffer *efb = to_evdi_fb(fb);
if (!efb->obj)
return -EINVAL;
return drm_gem_handle_create(file, &efb->obj->base, handle);
}
const struct drm_framebuffer_funcs evdifb_funcs = {
.destroy = evdi_fb_destroy,
.create_handle = evdi_fb_create_handle,
};
static unsigned int evdi_fb_cpp(u32 format)
{
const struct drm_format_info *info = drm_format_info(format);
if (!info || info->num_planes != 1)
return 0;
return info->cpp[0];
}
static int evdi_fb_calc_size(const struct drm_mode_fb_cmd2 *mode_cmd,
u32 *out_pitch, size_t *out_size)
{
const struct drm_format_info *info = drm_format_info(mode_cmd->pixel_format);
u32 pitch, cpp;
size_t last_lines, total, tail;
if (!info || info->num_planes != 1)
return -EINVAL;
cpp = info->cpp[0];
if (!cpp)
return -EINVAL;
if (!mode_cmd->width || !mode_cmd->height)
return -EINVAL;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0))
if (mode_cmd->modifier[0] && mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR)
return -EINVAL;
#endif
if (mode_cmd->pitches[0]) {
pitch = mode_cmd->pitches[0];
if (pitch < mode_cmd->width * cpp)
return -EINVAL;
} else {
pitch = mode_cmd->width * cpp;
}
if (mode_cmd->height == 0)
return -EINVAL;
if (check_mul_overflow((size_t)(mode_cmd->height - 1), (size_t)pitch, &last_lines))
return -EOVERFLOW;
tail = (size_t)mode_cmd->width * cpp;
if (check_add_overflow((size_t)mode_cmd->offsets[0], last_lines, &total))
return -EOVERFLOW;
if (check_add_overflow(total, tail, &total))
return -EOVERFLOW;
*out_pitch = pitch;
*out_size = total;
return 0;
}
static struct evdi_gem_object *evdi_fb_acquire_bo(struct drm_device *dev,
struct drm_file *file,
const struct drm_mode_fb_cmd2 *mode_cmd)
{
size_t size;
u32 validated_pitch;
int ret;
ret = evdi_fb_calc_size(mode_cmd, &validated_pitch, &size);
if (ret)
return NULL;
return evdi_gem_alloc_object(dev, size);
}
static int evdi_fb_init_core(struct drm_device *dev,
struct evdi_framebuffer *efb,
const struct drm_mode_fb_cmd2 *mode_cmd)
{
struct drm_framebuffer *fb = &efb->base;
const struct drm_format_info *info = drm_format_info(mode_cmd->pixel_format);
int ret;
if (!info)
return -EINVAL;
fb->dev = dev;
fb->format = info;
fb->width = mode_cmd->width;
fb->height = mode_cmd->height;
fb->pitches[0] = mode_cmd->pitches[0] ?
mode_cmd->pitches[0] :
evdi_fb_cpp(mode_cmd->pixel_format) * mode_cmd->width;
fb->offsets[0] = mode_cmd->offsets[0];
#if defined(DRM_FORMAT_MOD_LINEAR) || (LINUX_VERSION_CODE >= KERNEL_VERSION(4,15,0))
fb->modifier = mode_cmd->modifier[0];
#endif
fb->flags = 0;
fb->funcs = &evdifb_funcs;
fb->obj[0] = &efb->obj->base;
ret = drm_framebuffer_init(dev, fb, &evdifb_funcs);
return ret;
}
struct drm_framebuffer *evdi_fb_user_fb_create(struct drm_device *dev,
struct drm_file *file,
const struct drm_mode_fb_cmd2 *mode_cmd)
{
struct evdi_framebuffer *efb;
struct evdi_gem_object *bo;
int ret;
bo = evdi_fb_acquire_bo(dev, file, mode_cmd);
if (!bo)
return ERR_PTR(-ENOENT);
efb = evdi_fb_alloc(GFP_KERNEL);
if (!efb) {
evdi_gem_object_put_local(&bo->base);
return ERR_PTR(-ENOMEM);
}
efb->obj = bo;
efb->owner = file;
efb->active = true;
efb->gralloc_buf_id = mode_cmd->handles[0];
efb->bound_display_id = -1;
efb->bound_generation = 0;
ret = evdi_fb_init_core(dev, efb, mode_cmd);
if (ret) {
evdi_gem_object_put_local(&bo->base);
kmem_cache_free(evdi_fb_cache, efb);
return ERR_PTR(ret);
}
return &efb->base;
}