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> | |
21 | #include <drm/drm_crtc.h> | |
22 | #include <drm/drm_fb_helper.h> | |
23 | #include <drm/drm_crtc_helper.h> | |
24 | #include <drm/drm_gem_cma_helper.h> | |
25 | #include <drm/drm_fb_cma_helper.h> | |
26 | #include <linux/module.h> | |
27 | ||
28 | struct drm_fb_cma { | |
29 | struct drm_framebuffer fb; | |
30 | struct drm_gem_cma_object *obj[4]; | |
31 | }; | |
32 | ||
33 | struct drm_fbdev_cma { | |
34 | struct drm_fb_helper fb_helper; | |
35 | struct drm_fb_cma *fb; | |
36 | }; | |
37 | ||
38 | static inline struct drm_fbdev_cma *to_fbdev_cma(struct drm_fb_helper *helper) | |
39 | { | |
40 | return container_of(helper, struct drm_fbdev_cma, fb_helper); | |
41 | } | |
42 | ||
43 | static inline struct drm_fb_cma *to_fb_cma(struct drm_framebuffer *fb) | |
44 | { | |
45 | return container_of(fb, struct drm_fb_cma, fb); | |
46 | } | |
47 | ||
48 | static void drm_fb_cma_destroy(struct drm_framebuffer *fb) | |
49 | { | |
50 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
51 | int i; | |
52 | ||
53 | for (i = 0; i < 4; i++) { | |
54 | if (fb_cma->obj[i]) | |
55 | drm_gem_object_unreference_unlocked(&fb_cma->obj[i]->base); | |
56 | } | |
57 | ||
58 | drm_framebuffer_cleanup(fb); | |
59 | kfree(fb_cma); | |
60 | } | |
61 | ||
62 | static int drm_fb_cma_create_handle(struct drm_framebuffer *fb, | |
63 | struct drm_file *file_priv, unsigned int *handle) | |
64 | { | |
65 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
66 | ||
67 | return drm_gem_handle_create(file_priv, | |
68 | &fb_cma->obj[0]->base, handle); | |
69 | } | |
70 | ||
71 | static struct drm_framebuffer_funcs drm_fb_cma_funcs = { | |
72 | .destroy = drm_fb_cma_destroy, | |
73 | .create_handle = drm_fb_cma_create_handle, | |
74 | }; | |
75 | ||
76 | static struct drm_fb_cma *drm_fb_cma_alloc(struct drm_device *dev, | |
77 | struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_cma_object **obj, | |
78 | unsigned int num_planes) | |
79 | { | |
80 | struct drm_fb_cma *fb_cma; | |
81 | int ret; | |
82 | int i; | |
83 | ||
84 | fb_cma = kzalloc(sizeof(*fb_cma), GFP_KERNEL); | |
85 | if (!fb_cma) | |
86 | return ERR_PTR(-ENOMEM); | |
87 | ||
c7d73f6a DV |
88 | drm_helper_mode_fill_fb_struct(&fb_cma->fb, mode_cmd); |
89 | ||
90 | for (i = 0; i < num_planes; i++) | |
91 | fb_cma->obj[i] = obj[i]; | |
92 | ||
2e3b3c42 LPC |
93 | ret = drm_framebuffer_init(dev, &fb_cma->fb, &drm_fb_cma_funcs); |
94 | if (ret) { | |
95 | dev_err(dev->dev, "Failed to initalize framebuffer: %d\n", ret); | |
96 | kfree(fb_cma); | |
97 | return ERR_PTR(ret); | |
98 | } | |
99 | ||
2e3b3c42 LPC |
100 | return fb_cma; |
101 | } | |
102 | ||
103 | /** | |
104 | * drm_fb_cma_create() - (struct drm_mode_config_funcs *)->fb_create callback function | |
105 | * | |
106 | * If your hardware has special alignment or pitch requirements these should be | |
107 | * checked before calling this function. | |
108 | */ | |
109 | struct drm_framebuffer *drm_fb_cma_create(struct drm_device *dev, | |
110 | struct drm_file *file_priv, struct drm_mode_fb_cmd2 *mode_cmd) | |
111 | { | |
112 | struct drm_fb_cma *fb_cma; | |
113 | struct drm_gem_cma_object *objs[4]; | |
114 | struct drm_gem_object *obj; | |
115 | unsigned int hsub; | |
116 | unsigned int vsub; | |
117 | int ret; | |
118 | int i; | |
119 | ||
120 | hsub = drm_format_horz_chroma_subsampling(mode_cmd->pixel_format); | |
121 | vsub = drm_format_vert_chroma_subsampling(mode_cmd->pixel_format); | |
122 | ||
123 | for (i = 0; i < drm_format_num_planes(mode_cmd->pixel_format); i++) { | |
124 | unsigned int width = mode_cmd->width / (i ? hsub : 1); | |
125 | unsigned int height = mode_cmd->height / (i ? vsub : 1); | |
126 | unsigned int min_size; | |
127 | ||
128 | obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[i]); | |
129 | if (!obj) { | |
130 | dev_err(dev->dev, "Failed to lookup GEM object\n"); | |
131 | ret = -ENXIO; | |
132 | goto err_gem_object_unreference; | |
133 | } | |
134 | ||
135 | min_size = (height - 1) * mode_cmd->pitches[i] | |
136 | + width * drm_format_plane_cpp(mode_cmd->pixel_format, i) | |
137 | + mode_cmd->offsets[i]; | |
138 | ||
139 | if (obj->size < min_size) { | |
140 | drm_gem_object_unreference_unlocked(obj); | |
141 | ret = -EINVAL; | |
142 | goto err_gem_object_unreference; | |
143 | } | |
144 | objs[i] = to_drm_gem_cma_obj(obj); | |
145 | } | |
146 | ||
147 | fb_cma = drm_fb_cma_alloc(dev, mode_cmd, objs, i); | |
148 | if (IS_ERR(fb_cma)) { | |
149 | ret = PTR_ERR(fb_cma); | |
150 | goto err_gem_object_unreference; | |
151 | } | |
152 | ||
153 | return &fb_cma->fb; | |
154 | ||
155 | err_gem_object_unreference: | |
156 | for (i--; i >= 0; i--) | |
157 | drm_gem_object_unreference_unlocked(&objs[i]->base); | |
158 | return ERR_PTR(ret); | |
159 | } | |
160 | EXPORT_SYMBOL_GPL(drm_fb_cma_create); | |
161 | ||
162 | /** | |
163 | * drm_fb_cma_get_gem_obj() - Get CMA GEM object for framebuffer | |
164 | * @fb: The framebuffer | |
165 | * @plane: Which plane | |
166 | * | |
167 | * Return the CMA GEM object for given framebuffer. | |
168 | * | |
169 | * This function will usually be called from the CRTC callback functions. | |
170 | */ | |
171 | struct drm_gem_cma_object *drm_fb_cma_get_gem_obj(struct drm_framebuffer *fb, | |
172 | unsigned int plane) | |
173 | { | |
174 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
175 | ||
176 | if (plane >= 4) | |
177 | return NULL; | |
178 | ||
179 | return fb_cma->obj[plane]; | |
180 | } | |
181 | EXPORT_SYMBOL_GPL(drm_fb_cma_get_gem_obj); | |
182 | ||
6f646095 RC |
183 | #ifdef CONFIG_DEBUG_FS |
184 | /** | |
185 | * drm_fb_cma_describe() - Helper to dump information about a single | |
186 | * CMA framebuffer object | |
187 | */ | |
188 | void drm_fb_cma_describe(struct drm_framebuffer *fb, struct seq_file *m) | |
189 | { | |
190 | struct drm_fb_cma *fb_cma = to_fb_cma(fb); | |
191 | int i, n = drm_format_num_planes(fb->pixel_format); | |
192 | ||
193 | seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height, | |
194 | (char *)&fb->pixel_format); | |
195 | ||
196 | for (i = 0; i < n; i++) { | |
197 | seq_printf(m, " %d: offset=%d pitch=%d, obj: ", | |
198 | i, fb->offsets[i], fb->pitches[i]); | |
199 | drm_gem_cma_describe(fb_cma->obj[i], m); | |
200 | } | |
201 | } | |
202 | EXPORT_SYMBOL_GPL(drm_fb_cma_describe); | |
203 | ||
204 | /** | |
205 | * drm_fb_cma_debugfs_show() - Helper to list CMA framebuffer objects | |
206 | * in debugfs. | |
207 | */ | |
208 | int drm_fb_cma_debugfs_show(struct seq_file *m, void *arg) | |
209 | { | |
210 | struct drm_info_node *node = (struct drm_info_node *) m->private; | |
211 | struct drm_device *dev = node->minor->dev; | |
212 | struct drm_framebuffer *fb; | |
213 | int ret; | |
214 | ||
215 | ret = mutex_lock_interruptible(&dev->mode_config.mutex); | |
216 | if (ret) | |
217 | return ret; | |
218 | ||
219 | ret = mutex_lock_interruptible(&dev->struct_mutex); | |
220 | if (ret) { | |
221 | mutex_unlock(&dev->mode_config.mutex); | |
222 | return ret; | |
223 | } | |
224 | ||
225 | list_for_each_entry(fb, &dev->mode_config.fb_list, head) | |
226 | drm_fb_cma_describe(fb, m); | |
227 | ||
228 | mutex_unlock(&dev->struct_mutex); | |
229 | mutex_unlock(&dev->mode_config.mutex); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(drm_fb_cma_debugfs_show); | |
234 | #endif | |
235 | ||
2e3b3c42 LPC |
236 | static struct fb_ops drm_fbdev_cma_ops = { |
237 | .owner = THIS_MODULE, | |
238 | .fb_fillrect = sys_fillrect, | |
239 | .fb_copyarea = sys_copyarea, | |
240 | .fb_imageblit = sys_imageblit, | |
241 | .fb_check_var = drm_fb_helper_check_var, | |
242 | .fb_set_par = drm_fb_helper_set_par, | |
243 | .fb_blank = drm_fb_helper_blank, | |
244 | .fb_pan_display = drm_fb_helper_pan_display, | |
245 | .fb_setcmap = drm_fb_helper_setcmap, | |
246 | }; | |
247 | ||
248 | static int drm_fbdev_cma_create(struct drm_fb_helper *helper, | |
249 | struct drm_fb_helper_surface_size *sizes) | |
250 | { | |
251 | struct drm_fbdev_cma *fbdev_cma = to_fbdev_cma(helper); | |
252 | struct drm_mode_fb_cmd2 mode_cmd = { 0 }; | |
253 | struct drm_device *dev = helper->dev; | |
254 | struct drm_gem_cma_object *obj; | |
255 | struct drm_framebuffer *fb; | |
256 | unsigned int bytes_per_pixel; | |
257 | unsigned long offset; | |
258 | struct fb_info *fbi; | |
259 | size_t size; | |
260 | int ret; | |
261 | ||
e0d78d08 | 262 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", |
2e3b3c42 LPC |
263 | sizes->surface_width, sizes->surface_height, |
264 | sizes->surface_bpp); | |
265 | ||
266 | bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8); | |
267 | ||
268 | mode_cmd.width = sizes->surface_width; | |
269 | mode_cmd.height = sizes->surface_height; | |
270 | mode_cmd.pitches[0] = sizes->surface_width * bytes_per_pixel; | |
271 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, | |
272 | sizes->surface_depth); | |
273 | ||
274 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
275 | obj = drm_gem_cma_create(dev, size); | |
02813245 | 276 | if (IS_ERR(obj)) |
2e3b3c42 LPC |
277 | return -ENOMEM; |
278 | ||
279 | fbi = framebuffer_alloc(0, dev->dev); | |
280 | if (!fbi) { | |
281 | dev_err(dev->dev, "Failed to allocate framebuffer info.\n"); | |
282 | ret = -ENOMEM; | |
283 | goto err_drm_gem_cma_free_object; | |
284 | } | |
285 | ||
286 | fbdev_cma->fb = drm_fb_cma_alloc(dev, &mode_cmd, &obj, 1); | |
287 | if (IS_ERR(fbdev_cma->fb)) { | |
288 | dev_err(dev->dev, "Failed to allocate DRM framebuffer.\n"); | |
289 | ret = PTR_ERR(fbdev_cma->fb); | |
290 | goto err_framebuffer_release; | |
291 | } | |
292 | ||
293 | fb = &fbdev_cma->fb->fb; | |
294 | helper->fb = fb; | |
295 | helper->fbdev = fbi; | |
296 | ||
297 | fbi->par = helper; | |
298 | fbi->flags = FBINFO_FLAG_DEFAULT; | |
299 | fbi->fbops = &drm_fbdev_cma_ops; | |
300 | ||
301 | ret = fb_alloc_cmap(&fbi->cmap, 256, 0); | |
302 | if (ret) { | |
303 | dev_err(dev->dev, "Failed to allocate color map.\n"); | |
304 | goto err_drm_fb_cma_destroy; | |
305 | } | |
306 | ||
307 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth); | |
308 | drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height); | |
309 | ||
310 | offset = fbi->var.xoffset * bytes_per_pixel; | |
311 | offset += fbi->var.yoffset * fb->pitches[0]; | |
312 | ||
313 | dev->mode_config.fb_base = (resource_size_t)obj->paddr; | |
314 | fbi->screen_base = obj->vaddr + offset; | |
315 | fbi->fix.smem_start = (unsigned long)(obj->paddr + offset); | |
316 | fbi->screen_size = size; | |
317 | fbi->fix.smem_len = size; | |
318 | ||
319 | return 0; | |
320 | ||
321 | err_drm_fb_cma_destroy: | |
36206361 | 322 | drm_framebuffer_unregister_private(fb); |
2e3b3c42 LPC |
323 | drm_fb_cma_destroy(fb); |
324 | err_framebuffer_release: | |
325 | framebuffer_release(fbi); | |
326 | err_drm_gem_cma_free_object: | |
327 | drm_gem_cma_free_object(&obj->base); | |
328 | return ret; | |
329 | } | |
330 | ||
2e3b3c42 | 331 | static struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = { |
cd5428a5 | 332 | .fb_probe = drm_fbdev_cma_create, |
2e3b3c42 LPC |
333 | }; |
334 | ||
335 | /** | |
336 | * drm_fbdev_cma_init() - Allocate and initializes a drm_fbdev_cma struct | |
337 | * @dev: DRM device | |
338 | * @preferred_bpp: Preferred bits per pixel for the device | |
339 | * @num_crtc: Number of CRTCs | |
340 | * @max_conn_count: Maximum number of connectors | |
341 | * | |
342 | * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR. | |
343 | */ | |
344 | struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev, | |
345 | unsigned int preferred_bpp, unsigned int num_crtc, | |
346 | unsigned int max_conn_count) | |
347 | { | |
348 | struct drm_fbdev_cma *fbdev_cma; | |
349 | struct drm_fb_helper *helper; | |
350 | int ret; | |
351 | ||
352 | fbdev_cma = kzalloc(sizeof(*fbdev_cma), GFP_KERNEL); | |
353 | if (!fbdev_cma) { | |
354 | dev_err(dev->dev, "Failed to allocate drm fbdev.\n"); | |
355 | return ERR_PTR(-ENOMEM); | |
356 | } | |
357 | ||
358 | fbdev_cma->fb_helper.funcs = &drm_fb_cma_helper_funcs; | |
359 | helper = &fbdev_cma->fb_helper; | |
360 | ||
361 | ret = drm_fb_helper_init(dev, helper, num_crtc, max_conn_count); | |
362 | if (ret < 0) { | |
363 | dev_err(dev->dev, "Failed to initialize drm fb helper.\n"); | |
364 | goto err_free; | |
365 | } | |
366 | ||
367 | ret = drm_fb_helper_single_add_all_connectors(helper); | |
368 | if (ret < 0) { | |
369 | dev_err(dev->dev, "Failed to add connectors.\n"); | |
370 | goto err_drm_fb_helper_fini; | |
371 | ||
372 | } | |
373 | ||
76a39dbf DV |
374 | /* disable all the possible outputs/crtcs before entering KMS mode */ |
375 | drm_helper_disable_unused_functions(dev); | |
376 | ||
2e3b3c42 LPC |
377 | ret = drm_fb_helper_initial_config(helper, preferred_bpp); |
378 | if (ret < 0) { | |
379 | dev_err(dev->dev, "Failed to set inital hw configuration.\n"); | |
380 | goto err_drm_fb_helper_fini; | |
381 | } | |
382 | ||
383 | return fbdev_cma; | |
384 | ||
385 | err_drm_fb_helper_fini: | |
386 | drm_fb_helper_fini(helper); | |
387 | err_free: | |
388 | kfree(fbdev_cma); | |
389 | ||
390 | return ERR_PTR(ret); | |
391 | } | |
392 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_init); | |
393 | ||
394 | /** | |
395 | * drm_fbdev_cma_fini() - Free drm_fbdev_cma struct | |
396 | * @fbdev_cma: The drm_fbdev_cma struct | |
397 | */ | |
398 | void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma) | |
399 | { | |
400 | if (fbdev_cma->fb_helper.fbdev) { | |
401 | struct fb_info *info; | |
402 | int ret; | |
403 | ||
404 | info = fbdev_cma->fb_helper.fbdev; | |
405 | ret = unregister_framebuffer(info); | |
406 | if (ret < 0) | |
407 | DRM_DEBUG_KMS("failed unregister_framebuffer()\n"); | |
408 | ||
409 | if (info->cmap.len) | |
410 | fb_dealloc_cmap(&info->cmap); | |
411 | ||
412 | framebuffer_release(info); | |
413 | } | |
414 | ||
36206361 DV |
415 | if (fbdev_cma->fb) { |
416 | drm_framebuffer_unregister_private(&fbdev_cma->fb->fb); | |
2e3b3c42 | 417 | drm_fb_cma_destroy(&fbdev_cma->fb->fb); |
36206361 | 418 | } |
2e3b3c42 LPC |
419 | |
420 | drm_fb_helper_fini(&fbdev_cma->fb_helper); | |
421 | kfree(fbdev_cma); | |
422 | } | |
423 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_fini); | |
424 | ||
425 | /** | |
426 | * drm_fbdev_cma_restore_mode() - Restores initial framebuffer mode | |
427 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL | |
428 | * | |
429 | * This function is usually called from the DRM drivers lastclose callback. | |
430 | */ | |
431 | void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma) | |
432 | { | |
8fcb6c47 DV |
433 | if (fbdev_cma) { |
434 | struct drm_device *dev = fbdev_cma->fb_helper.dev; | |
435 | ||
436 | drm_modeset_lock_all(dev); | |
2e3b3c42 | 437 | drm_fb_helper_restore_fbdev_mode(&fbdev_cma->fb_helper); |
8fcb6c47 DV |
438 | drm_modeset_unlock_all(dev); |
439 | } | |
2e3b3c42 LPC |
440 | } |
441 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_restore_mode); | |
442 | ||
443 | /** | |
444 | * drm_fbdev_cma_hotplug_event() - Poll for hotpulug events | |
445 | * @fbdev_cma: The drm_fbdev_cma struct, may be NULL | |
446 | * | |
447 | * This function is usually called from the DRM drivers output_poll_changed | |
448 | * callback. | |
449 | */ | |
450 | void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma) | |
451 | { | |
452 | if (fbdev_cma) | |
453 | drm_fb_helper_hotplug_event(&fbdev_cma->fb_helper); | |
454 | } | |
455 | EXPORT_SYMBOL_GPL(drm_fbdev_cma_hotplug_event); |