Commit | Line | Data |
---|---|---|
2e3b3c42 LPC |
1 | /* |
2 | * drm kms/fb cma (contiguous memory allocator) helper functions | |
3 | * | |
4 | * Copyright (C) 2012 Analog Device Inc. | |
5 | * Author: Lars-Peter Clausen <lars@metafoo.de> | |
6 | * | |
7 | * Based on udl_fbdev.c | |
8 | * Copyright (C) 2012 Red Hat | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version 2 | |
13 | * of the License, or (at your option) any later version. | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | */ | |
19 | ||
20 | #include <drm/drmP.h> | |
14d7f96f | 21 | #include <drm/drm_atomic.h> |
2e3b3c42 LPC |
22 | #include <drm/drm_crtc.h> |
23 | #include <drm/drm_fb_helper.h> | |
24 | #include <drm/drm_crtc_helper.h> | |
25 | #include <drm/drm_gem_cma_helper.h> | |
26 | #include <drm/drm_fb_cma_helper.h> | |
14d7f96f | 27 | #include <linux/dma-buf.h> |
ce0c5757 | 28 | #include <linux/dma-mapping.h> |
2e3b3c42 | 29 | #include <linux/module.h> |
14d7f96f | 30 | #include <linux/reservation.h> |
2e3b3c42 | 31 | |
199c7717 NT |
32 | #define DEFAULT_FBDEFIO_DELAY_MS 50 |
33 | ||
2e3b3c42 LPC |
34 | struct drm_fb_cma { |
35 | struct drm_framebuffer fb; | |
36 | struct drm_gem_cma_object *obj[4]; | |
37 | }; | |
38 | ||
39 | struct drm_fbdev_cma { | |
40 | struct drm_fb_helper fb_helper; | |
41 | struct drm_fb_cma *fb; | |
b112481b | 42 | const struct drm_framebuffer_funcs *fb_funcs; |
2e3b3c42 LPC |
43 | }; |
44 | ||
199c7717 NT |
45 | /** |
46 | * DOC: framebuffer cma helper functions | |
47 | * | |
48 | * Provides helper functions for creating a cma (contiguous memory allocator) | |
49 | * backed framebuffer. | |
50 | * | |
421242aa | 51 | * drm_fb_cma_create() is used in the &drm_mode_config_funcs.fb_create |
02da16d0 | 52 | * callback function to create a cma backed framebuffer. |
199c7717 NT |
53 | * |
54 | * An fbdev framebuffer backed by cma is also available by calling | |
55 | * drm_fbdev_cma_init(). drm_fbdev_cma_fini() tears it down. | |
421242aa DV |
56 | * If the &drm_framebuffer_funcs.dirty callback is set, fb_deferred_io will be |
57 | * set up automatically. &drm_framebuffer_funcs.dirty is called by | |
58 | * drm_fb_helper_deferred_io() in process context (&struct delayed_work). | |
199c7717 | 59 | * |
da5335b8 | 60 | * Example fbdev deferred io code:: |
199c7717 | 61 | * |
b112481b DV |
62 | * static int driver_fb_dirty(struct drm_framebuffer *fb, |
63 | * struct drm_file *file_priv, | |
64 | * unsigned flags, unsigned color, | |
65 | * struct drm_clip_rect *clips, | |
66 | * unsigned num_clips) | |
199c7717 NT |
67 | * { |
68 | * struct drm_gem_cma_object *cma = drm_fb_cma_get_gem_obj(fb, 0); | |
69 | * ... push changes ... | |
70 | * return 0; | |
71 | * } | |
72 | * | |
b112481b | 73 | * static struct drm_framebuffer_funcs driver_fb_funcs = { |
199c7717 NT |
74 | * .destroy = drm_fb_cma_destroy, |
75 | * .create_handle = drm_fb_cma_create_handle, | |
b112481b | 76 | * .dirty = driver_fb_dirty, |
199c7717 NT |
77 | * }; |
78 | * | |
b112481b | 79 | * Initialize:: |
199c7717 | 80 | * |
199c7717 NT |
81 | * fbdev = drm_fbdev_cma_init_with_funcs(dev, 16, |
82 | * dev->mode_config.num_crtc, | |
83 | * dev->mode_config.num_connector, | |
b112481b | 84 | * &driver_fb_funcs); |
199c7717 NT |
85 | * |
86 | */ | |
87 | ||
2e3b3c42 LPC |
88 | static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper) |
89 | { | |
90 | return container_of(helper, struct drm_fbdev_cma, fb_helper); | |
91 | } | |
92 | ||
93 | static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb) | |
94 | { | |
95 | return container_of(fb, struct drm_fb_cma, fb); | |
96 | } | |
97 | ||
199c7717 | 98 | void drm_fb_cma_destroy(struct drm_framebuffer *fb) |
2e3b3c42 LPC |
99 | { |
100 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
101 | int i; | |
102 | ||
103 | for (i = 0; i < 4; i++) { | |
104 | if (fb_cma->obj[i]) | |
105 | drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base); | |
106 | } | |
107 | ||
108 | drm_framebuffer_cleanup(fb); | |
109 | kfree(fb_cma); | |
110 | } | |
199c7717 | 111 | EXPORT_SYMBOL(drm_fb_cma_destroy); |
2e3b3c42 | 112 | |
199c7717 | 113 | int drm_fb_cma_create_handle(struct drm_framebuffer *fb, |
2e3b3c42 LPC |
114 | struct drm_file *file_priv, unsigned int *handle) |
115 | { | |
116 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
117 | ||
118 | return drm_gem_handle_create(file_priv, | |
119 | &fb_cma->obj[0]->base, handle); | |
120 | } | |
199c7717 | 121 | EXPORT_SYMBOL(drm_fb_cma_create_handle); |
2e3b3c42 LPC |
122 | |
123 | static struct drm_framebuffer_funcs drm_fb_cma_funcs = { | |
124 | .destroy = drm_fb_cma_destroy, | |
125 | .create_handle = drm_fb_cma_create_handle, | |
126 | }; | |
127 | ||
128 | static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev, | |
70c0616d CIK |
129 | const struct drm_mode_fb_cmd2 *mode_cmd, |
130 | struct drm_gem_cma_object **obj, | |
fdce1846 | 131 | unsigned int num_planes, const struct drm_framebuffer_funcs *funcs) |
2e3b3c42 LPC |
132 | { |
133 | struct drm_fb_cma *fb_cma; | |
134 | int ret; | |
135 | int i; | |
136 | ||
137 | fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL); | |
138 | if (!fb_cma) | |
139 | return ERR_PTR(-ENOMEM); | |
140 | ||
a3f913ca | 141 | drm_helper_mode_fill_fb_struct(dev, &fb_cma->fb, mode_cmd); |
c7d73f6a DV |
142 | |
143 | for (i = 0; i < num_planes; i++) | |
144 | fb_cma->obj[i] = obj[i]; | |
145 | ||
199c7717 | 146 | ret = drm_framebuffer_init(dev, &fb_cma->fb, funcs); |
2e3b3c42 | 147 | if (ret) { |
8b513d0c | 148 | dev_err(dev->dev, "Failed to initialize framebuffer: %d\n", ret); |
2e3b3c42 LPC |
149 | kfree(fb_cma); |
150 | return ERR_PTR(ret); | |
151 | } | |
152 | ||
2e3b3c42 LPC |
153 | return fb_cma; |
154 | } | |
155 | ||
156 | /** | |
3995b395 | 157 | * drm_fb_cma_create_with_funcs() - helper function for the |
421242aa DV |
158 | * &drm_mode_config_funcs.fb_create |
159 | * callback | |
890358a6 DV |
160 | * @dev: DRM device |
161 | * @file_priv: drm file for the ioctl call | |
162 | * @mode_cmd: metadata from the userspace fb creation request | |
163 | * @funcs: vtable to be used for the new framebuffer object | |
2e3b3c42 | 164 | * |
3995b395 | 165 | * This can be used to set &drm_framebuffer_funcs for drivers that need the |
421242aa DV |
166 | * &drm_framebuffer_funcs.dirty callback. Use drm_fb_cma_create() if you don't |
167 | * need to change &drm_framebuffer_funcs. | |
2e3b3c42 | 168 | */ |
3995b395 NT |
169 | struct drm_framebuffer *drm_fb_cma_create_with_funcs(struct drm_device *dev, |
170 | struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd, | |
171 | const struct drm_framebuffer_funcs *funcs) | |
2e3b3c42 | 172 | { |
d5493490 | 173 | const struct drm_format_info *info; |
2e3b3c42 LPC |
174 | struct drm_fb_cma *fb_cma; |
175 | struct drm_gem_cma_object *objs[4]; | |
176 | struct drm_gem_object *obj; | |
2e3b3c42 LPC |
177 | int ret; |
178 | int i; | |
179 | ||
d5493490 LP |
180 | info = drm_format_info(mode_cmd->pixel_format); |
181 | if (!info) | |
182 | return ERR_PTR(-EINVAL); | |
2e3b3c42 | 183 | |
d5493490 LP |
184 | for (i = 0; i < info->num_planes; i++) { |
185 | unsigned int width = mode_cmd->width / (i ? info->hsub : 1); | |
186 | unsigned int height = mode_cmd->height / (i ? info->vsub : 1); | |
2e3b3c42 LPC |
187 | unsigned int min_size; |
188 | ||
a8ad0bd8 | 189 | obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[i]); |
2e3b3c42 LPC |
190 | if (!obj) { |
191 | dev_err(dev->dev, "Failed to lookup GEM object\n"); | |
192 | ret = -ENXIO; | |
193 | goto err_gem_object_unreference; | |
194 | } | |
195 | ||
196 | min_size = (height - 1) * mode_cmd->pitches[i] | |
d5493490 | 197 | + width * info->cpp[i] |
2e3b3c42 LPC |
198 | + mode_cmd->offsets[i]; |
199 | ||
200 | if (obj->size < min_size) { | |
201 | drm_gem_object_unreference_unlocked(obj); | |
202 | ret = -EINVAL; | |
203 | goto err_gem_object_unreference; | |
204 | } | |
205 | objs[i] = to_drm_gem_cma_obj(obj); | |
206 | } | |
207 | ||
3995b395 | 208 | fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i, funcs); |
2e3b3c42 LPC |
209 | if (IS_ERR(fb_cma)) { |
210 | ret = PTR_ERR(fb_cma); | |
211 | goto err_gem_object_unreference; | |
212 | } | |
213 | ||
214 | return &fb_cma->fb; | |
215 | ||
216 | err_gem_object_unreference: | |
217 | for (i--; i >= 0; i--) | |
218 | drm_gem_object_unreference_unlocked(&objs[i]->base); | |
219 | return ERR_PTR(ret); | |
220 | } | |
3995b395 NT |
221 | EXPORT_SYMBOL_GPL(drm_fb_cma_create_with_funcs); |
222 | ||
223 | /** | |
421242aa | 224 | * drm_fb_cma_create() - &drm_mode_config_funcs.fb_create callback function |
890358a6 DV |
225 | * @dev: DRM device |
226 | * @file_priv: drm file for the ioctl call | |
227 | * @mode_cmd: metadata from the userspace fb creation request | |
3995b395 NT |
228 | * |
229 | * If your hardware has special alignment or pitch requirements these should be | |
230 | * checked before calling this function. Use drm_fb_cma_create_with_funcs() if | |
421242aa | 231 | * you need to set &drm_framebuffer_funcs.dirty. |
3995b395 NT |
232 | */ |
233 | struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev, | |
234 | struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd) | |
235 | { | |
236 | return drm_fb_cma_create_with_funcs(dev, file_priv, mode_cmd, | |
237 | &drm_fb_cma_funcs); | |
238 | } | |
2e3b3c42 LPC |
239 | EXPORT_SYMBOL_GPL(drm_fb_cma_create); |
240 | ||
241 | /** | |
242 | * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer | |
243 | * @fb: The framebuffer | |
244 | * @plane: Which plane | |
245 | * | |
246 | * Return the CMA GEM object for given framebuffer. | |
247 | * | |
248 | * This function will usually be called from the CRTC callback functions. | |
249 | */ | |
250 | struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb, | |
890358a6 | 251 | unsigned int plane) |
2e3b3c42 LPC |
252 | { |
253 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
254 | ||
255 | if (plane >= 4) | |
256 | return NULL; | |
257 | ||
258 | return fb_cma->obj[plane]; | |
259 | } | |
260 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj); | |
261 | ||
14d7f96f MV |
262 | /** |
263 | * drm_fb_cma_prepare_fb() - Prepare CMA framebuffer | |
264 | * @plane: Which plane | |
265 | * @state: Plane state attach fence to | |
266 | * | |
421242aa | 267 | * This should be set as the &struct drm_plane_helper_funcs.prepare_fb hook. |
14d7f96f MV |
268 | * |
269 | * This function checks if the plane FB has an dma-buf attached, extracts | |
270 | * the exclusive fence and attaches it to plane state for the atomic helper | |
271 | * to wait on. | |
272 | * | |
273 | * There is no need for cleanup_fb for CMA based framebuffer drivers. | |
274 | */ | |
275 | int drm_fb_cma_prepare_fb(struct drm_plane *plane, | |
276 | struct drm_plane_state *state) | |
277 | { | |
278 | struct dma_buf *dma_buf; | |
279 | struct dma_fence *fence; | |
280 | ||
281 | if ((plane->state->fb == state->fb) || !state->fb) | |
282 | return 0; | |
283 | ||
284 | dma_buf = drm_fb_cma_get_gem_obj(state->fb, 0)->base.dma_buf; | |
285 | if (dma_buf) { | |
286 | fence = reservation_object_get_excl_rcu(dma_buf->resv); | |
287 | drm_atomic_set_fence_for_plane(state, fence); | |
288 | } | |
289 | ||
290 | return 0; | |
291 | } | |
292 | EXPORT_SYMBOL_GPL(drm_fb_cma_prepare_fb); | |
293 | ||
6f646095 | 294 | #ifdef CONFIG_DEBUG_FS |
2c9c52e8 | 295 | static void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m) |
6f646095 RC |
296 | { |
297 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
d5493490 | 298 | int i; |
6f646095 RC |
299 | |
300 | seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height, | |
438b74a5 | 301 | (char *)&fb->format->format); |
6f646095 | 302 | |
ffb9c369 | 303 | for (i = 0; i < fb->format->num_planes; i++) { |
6f646095 RC |
304 | seq_printf(m, " %d: offset=%d pitch=%d, obj: ", |
305 | i, fb->offsets[i], fb->pitches[i]); | |
306 | drm_gem_cma_describe(fb_cma->obj[i], m); | |
307 | } | |
308 | } | |
6f646095 RC |
309 | |
310 | /** | |
311 | * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects | |
890358a6 DV |
312 | * in debugfs. |
313 | * @m: output file | |
314 | * @arg: private data for the callback | |
6f646095 RC |
315 | */ |
316 | int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg) | |
317 | { | |
318 | struct drm_info_node *node = (struct drm_info_node *) m->private; | |
319 | struct drm_device *dev = node->minor->dev; | |
320 | struct drm_framebuffer *fb; | |
6f646095 | 321 | |
9e75c0ef | 322 | mutex_lock(&dev->mode_config.fb_lock); |
e4f62546 | 323 | drm_for_each_fb(fb, dev) |
6f646095 | 324 | drm_fb_cma_describe(fb, m); |
9e75c0ef | 325 | mutex_unlock(&dev->mode_config.fb_lock); |
6f646095 RC |
326 | |
327 | return 0; | |
328 | } | |
329 | EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show); | |
330 | #endif | |
331 | ||
ce0c5757 RM |
332 | static int drm_fb_cma_mmap(struct fb_info *info, struct vm_area_struct *vma) |
333 | { | |
334 | return dma_mmap_writecombine(info->device, vma, info->screen_base, | |
335 | info->fix.smem_start, info->fix.smem_len); | |
336 | } | |
337 | ||
2e3b3c42 LPC |
338 | static struct fb_ops drm_fbdev_cma_ops = { |
339 | .owner = THIS_MODULE, | |
659119de | 340 | DRM_FB_HELPER_DEFAULT_OPS, |
85f2edf2 AT |
341 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
342 | .fb_copyarea = drm_fb_helper_sys_copyarea, | |
343 | .fb_imageblit = drm_fb_helper_sys_imageblit, | |
ce0c5757 | 344 | .fb_mmap = drm_fb_cma_mmap, |
2e3b3c42 LPC |
345 | }; |
346 | ||
199c7717 NT |
347 | static int drm_fbdev_cma_deferred_io_mmap(struct fb_info *info, |
348 | struct vm_area_struct *vma) | |
349 | { | |
350 | fb_deferred_io_mmap(info, vma); | |
351 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); | |
352 | ||
353 | return 0; | |
354 | } | |
355 | ||
356 | static int drm_fbdev_cma_defio_init(struct fb_info *fbi, | |
357 | struct drm_gem_cma_object *cma_obj) | |
358 | { | |
359 | struct fb_deferred_io *fbdefio; | |
360 | struct fb_ops *fbops; | |
361 | ||
362 | /* | |
363 | * Per device structures are needed because: | |
364 | * fbops: fb_deferred_io_cleanup() clears fbops.fb_mmap | |
365 | * fbdefio: individual delays | |
366 | */ | |
367 | fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); | |
368 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); | |
369 | if (!fbdefio || !fbops) { | |
370 | kfree(fbdefio); | |
2a927623 | 371 | kfree(fbops); |
199c7717 NT |
372 | return -ENOMEM; |
373 | } | |
374 | ||
375 | /* can't be offset from vaddr since dirty() uses cma_obj */ | |
376 | fbi->screen_buffer = cma_obj->vaddr; | |
377 | /* fb_deferred_io_fault() needs a physical address */ | |
378 | fbi->fix.smem_start = page_to_phys(virt_to_page(fbi->screen_buffer)); | |
379 | ||
380 | *fbops = *fbi->fbops; | |
381 | fbi->fbops = fbops; | |
382 | ||
383 | fbdefio->delay = msecs_to_jiffies(DEFAULT_FBDEFIO_DELAY_MS); | |
384 | fbdefio->deferred_io = drm_fb_helper_deferred_io; | |
385 | fbi->fbdefio = fbdefio; | |
386 | fb_deferred_io_init(fbi); | |
387 | fbi->fbops->fb_mmap = drm_fbdev_cma_deferred_io_mmap; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static void drm_fbdev_cma_defio_fini(struct fb_info *fbi) | |
393 | { | |
394 | if (!fbi->fbdefio) | |
395 | return; | |
396 | ||
397 | fb_deferred_io_cleanup(fbi); | |
398 | kfree(fbi->fbdefio); | |
399 | kfree(fbi->fbops); | |
400 | } | |
401 | ||
b112481b DV |
402 | static int |
403 | drm_fbdev_cma_create(struct drm_fb_helper *helper, | |
404 | struct drm_fb_helper_surface_size *sizes) | |
2e3b3c42 LPC |
405 | { |
406 | struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper); | |
407 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; | |
408 | struct drm_device *dev = helper->dev; | |
409 | struct drm_gem_cma_object *obj; | |
410 | struct drm_framebuffer *fb; | |
411 | unsigned int bytes_per_pixel; | |
412 | unsigned long offset; | |
413 | struct fb_info *fbi; | |
414 | size_t size; | |
415 | int ret; | |
416 | ||
e0d78d08 | 417 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", |
2e3b3c42 LPC |
418 | sizes->surface_width, sizes->surface_height, |
419 | sizes->surface_bpp); | |
420 | ||
421 | bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); | |
422 | ||
423 | mode_cmd.width = sizes->surface_width; | |
424 | mode_cmd.height = sizes->surface_height; | |
425 | mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel; | |
426 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
427 | sizes->surface_depth); | |
428 | ||
429 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
430 | obj = drm_gem_cma_create(dev, size); | |
02813245 | 431 | if (IS_ERR(obj)) |
2e3b3c42 LPC |
432 | return -ENOMEM; |
433 | ||
85f2edf2 AT |
434 | fbi = drm_fb_helper_alloc_fbi(helper); |
435 | if (IS_ERR(fbi)) { | |
436 | ret = PTR_ERR(fbi); | |
50cbc132 | 437 | goto err_gem_free_object; |
2e3b3c42 LPC |
438 | } |
439 | ||
b112481b DV |
440 | fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1, |
441 | fbdev_cma->fb_funcs); | |
2e3b3c42 LPC |
442 | if (IS_ERR(fbdev_cma->fb)) { |
443 | dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n"); | |
444 | ret = PTR_ERR(fbdev_cma->fb); | |
85f2edf2 | 445 | goto err_fb_info_destroy; |
2e3b3c42 LPC |
446 | } |
447 | ||
448 | fb = &fbdev_cma->fb->fb; | |
449 | helper->fb = fb; | |
2e3b3c42 LPC |
450 | |
451 | fbi->par = helper; | |
452 | fbi->flags = FBINFO_FLAG_DEFAULT; | |
453 | fbi->fbops = &drm_fbdev_cma_ops; | |
454 | ||
b00c600e | 455 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); |
8d76612b | 456 | drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height); |
2e3b3c42 LPC |
457 | |
458 | offset = fbi->var.xoffset * bytes_per_pixel; | |
459 | offset += fbi->var.yoffset * fb->pitches[0]; | |
460 | ||
461 | dev->mode_config.fb_base = (resource_size_t)obj->paddr; | |
462 | fbi->screen_base = obj->vaddr + offset; | |
463 | fbi->fix.smem_start = (unsigned long)(obj->paddr + offset); | |
464 | fbi->screen_size = size; | |
465 | fbi->fix.smem_len = size; | |
466 | ||
b112481b | 467 | if (fbdev_cma->fb_funcs->dirty) { |
199c7717 NT |
468 | ret = drm_fbdev_cma_defio_init(fbi, obj); |
469 | if (ret) | |
470 | goto err_cma_destroy; | |
471 | } | |
472 | ||
2e3b3c42 LPC |
473 | return 0; |
474 | ||
199c7717 NT |
475 | err_cma_destroy: |
476 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); | |
477 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); | |
85f2edf2 AT |
478 | err_fb_info_destroy: |
479 | drm_fb_helper_release_fbi(helper); | |
50cbc132 | 480 | err_gem_free_object: |
afe705be | 481 | drm_gem_object_unreference_unlocked(&obj->base); |
2e3b3c42 LPC |
482 | return ret; |
483 | } | |
484 | ||
3a493879 | 485 | static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
cd5428a5 | 486 | .fb_probe = drm_fbdev_cma_create, |
2e3b3c42 LPC |
487 | }; |
488 | ||
489 | /** | |
199c7717 | 490 | * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct |
2e3b3c42 LPC |
491 | * @dev: DRM device |
492 | * @preferred_bpp: Preferred bits per pixel for the device | |
493 | * @num_crtc: Number of CRTCs | |
494 | * @max_conn_count: Maximum number of connectors | |
b112481b | 495 | * @funcs: fb helper functions, in particular a custom dirty() callback |
2e3b3c42 LPC |
496 | * |
497 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. | |
498 | */ | |
199c7717 | 499 | struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev, |
2e3b3c42 | 500 | unsigned int preferred_bpp, unsigned int num_crtc, |
b112481b | 501 | unsigned int max_conn_count, const struct drm_framebuffer_funcs *funcs) |
2e3b3c42 LPC |
502 | { |
503 | struct drm_fbdev_cma *fbdev_cma; | |
504 | struct drm_fb_helper *helper; | |
505 | int ret; | |
506 | ||
507 | fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL); | |
508 | if (!fbdev_cma) { | |
509 | dev_err(dev->dev, "Failed to allocate drm fbdev.\n"); | |
510 | return ERR_PTR(-ENOMEM); | |
511 | } | |
b112481b | 512 | fbdev_cma->fb_funcs = funcs; |
2e3b3c42 | 513 | |
2e3b3c42 LPC |
514 | helper = &fbdev_cma->fb_helper; |
515 | ||
b112481b | 516 | drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs); |
10a23102 | 517 | |
2e3b3c42 LPC |
518 | ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count); |
519 | if (ret < 0) { | |
520 | dev_err(dev->dev, "Failed to initialize drm fb helper.\n"); | |
521 | goto err_free; | |
522 | } | |
523 | ||
524 | ret = drm_fb_helper_single_add_all_connectors(helper); | |
525 | if (ret < 0) { | |
526 | dev_err(dev->dev, "Failed to add connectors.\n"); | |
527 | goto err_drm_fb_helper_fini; | |
528 | ||
529 | } | |
530 | ||
531 | ret = drm_fb_helper_initial_config(helper, preferred_bpp); | |
532 | if (ret < 0) { | |
8b513d0c | 533 | dev_err(dev->dev, "Failed to set initial hw configuration.\n"); |
2e3b3c42 LPC |
534 | goto err_drm_fb_helper_fini; |
535 | } | |
536 | ||
537 | return fbdev_cma; | |
538 | ||
539 | err_drm_fb_helper_fini: | |
540 | drm_fb_helper_fini(helper); | |
541 | err_free: | |
542 | kfree(fbdev_cma); | |
543 | ||
544 | return ERR_PTR(ret); | |
545 | } | |
199c7717 NT |
546 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs); |
547 | ||
548 | /** | |
549 | * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct | |
550 | * @dev: DRM device | |
551 | * @preferred_bpp: Preferred bits per pixel for the device | |
552 | * @num_crtc: Number of CRTCs | |
553 | * @max_conn_count: Maximum number of connectors | |
554 | * | |
555 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. | |
556 | */ | |
557 | struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, | |
558 | unsigned int preferred_bpp, unsigned int num_crtc, | |
559 | unsigned int max_conn_count) | |
560 | { | |
561 | return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp, num_crtc, | |
b112481b | 562 | max_conn_count, &drm_fb_cma_funcs); |
199c7717 | 563 | } |
2e3b3c42 LPC |
564 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init); |
565 | ||
566 | /** | |
567 | * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct | |
568 | * @fbdev_cma: The drm_fbdev_cma struct | |
569 | */ | |
570 | void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma) | |
571 | { | |
85f2edf2 | 572 | drm_fb_helper_unregister_fbi(&fbdev_cma->fb_helper); |
41b9bb1b SA |
573 | if (fbdev_cma->fb_helper.fbdev) |
574 | drm_fbdev_cma_defio_fini(fbdev_cma->fb_helper.fbdev); | |
85f2edf2 | 575 | drm_fb_helper_release_fbi(&fbdev_cma->fb_helper); |
2e3b3c42 | 576 | |
36206361 DV |
577 | if (fbdev_cma->fb) { |
578 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); | |
2e3b3c42 | 579 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
36206361 | 580 | } |
2e3b3c42 LPC |
581 | |
582 | drm_fb_helper_fini(&fbdev_cma->fb_helper); | |
583 | kfree(fbdev_cma); | |
584 | } | |
585 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini); | |
586 | ||
587 | /** | |
588 | * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode | |
589 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL | |
590 | * | |
421242aa | 591 | * This function is usually called from the &drm_driver.lastclose callback. |
2e3b3c42 LPC |
592 | */ |
593 | void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma) | |
594 | { | |
5ea1f752 RC |
595 | if (fbdev_cma) |
596 | drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev_cma->fb_helper); | |
2e3b3c42 LPC |
597 | } |
598 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode); | |
599 | ||
600 | /** | |
601 | * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events | |
602 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL | |
603 | * | |
421242aa | 604 | * This function is usually called from the &drm_mode_config.output_poll_changed |
2e3b3c42 LPC |
605 | * callback. |
606 | */ | |
607 | void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma) | |
608 | { | |
609 | if (fbdev_cma) | |
610 | drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper); | |
611 | } | |
612 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event); | |
917f4253 SA |
613 | |
614 | /** | |
615 | * drm_fbdev_cma_set_suspend - wrapper around drm_fb_helper_set_suspend | |
616 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL | |
617 | * @state: desired state, zero to resume, non-zero to suspend | |
618 | * | |
619 | * Calls drm_fb_helper_set_suspend, which is a wrapper around | |
620 | * fb_set_suspend implemented by fbdev core. | |
621 | */ | |
622 | void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, int state) | |
623 | { | |
624 | if (fbdev_cma) | |
625 | drm_fb_helper_set_suspend(&fbdev_cma->fb_helper, state); | |
626 | } | |
627 | EXPORT_SYMBOL(drm_fbdev_cma_set_suspend); |