Commit | Line | Data |
---|---|---|
785b93ef DA |
1 | /* |
2 | * Copyright (c) 2006-2009 Red Hat Inc. | |
3 | * Copyright (c) 2006-2008 Intel Corporation | |
4 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> | |
5 | * | |
6 | * DRM framebuffer helper functions | |
7 | * | |
8 | * Permission to use, copy, modify, distribute, and sell this software and its | |
9 | * documentation for any purpose is hereby granted without fee, provided that | |
10 | * the above copyright notice appear in all copies and that both that copyright | |
11 | * notice and this permission notice appear in supporting documentation, and | |
12 | * that the name of the copyright holders not be used in advertising or | |
13 | * publicity pertaining to distribution of the software without specific, | |
14 | * written prior permission. The copyright holders make no representations | |
15 | * about the suitability of this software for any purpose. It is provided "as | |
16 | * is" without express or implied warranty. | |
17 | * | |
18 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
19 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
20 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
21 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
22 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
23 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
24 | * OF THIS SOFTWARE. | |
25 | * | |
26 | * Authors: | |
27 | * Dave Airlie <airlied@linux.ie> | |
28 | * Jesse Barnes <jesse.barnes@intel.com> | |
29 | */ | |
d56b1b9d SK |
30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
31 | ||
cfe63423 | 32 | #include <linux/console.h> |
d536540f | 33 | #include <linux/dma-buf.h> |
3b40a443 | 34 | #include <linux/kernel.h> |
e0cd3608 | 35 | #include <linux/module.h> |
0500c04e SR |
36 | #include <linux/slab.h> |
37 | #include <linux/sysrq.h> | |
38 | #include <linux/vmalloc.h> | |
39 | ||
bbb1e524 | 40 | #include <drm/drm_atomic.h> |
0500c04e SR |
41 | #include <drm/drm_crtc.h> |
42 | #include <drm/drm_crtc_helper.h> | |
43 | #include <drm/drm_drv.h> | |
44 | #include <drm/drm_fb_helper.h> | |
45 | #include <drm/drm_fourcc.h> | |
46 | #include <drm/drm_print.h> | |
47 | #include <drm/drm_vblank.h> | |
785b93ef | 48 | |
7baa77f1 | 49 | #include "drm_crtc_helper_internal.h" |
03a9606e | 50 | #include "drm_internal.h" |
699fbeea | 51 | |
f64c5573 DV |
52 | static bool drm_fbdev_emulation = true; |
53 | module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); | |
54 | MODULE_PARM_DESC(fbdev_emulation, | |
55 | "Enable legacy fbdev emulation [default=true]"); | |
56 | ||
5f152576 XL |
57 | static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; |
58 | module_param(drm_fbdev_overalloc, int, 0444); | |
59 | MODULE_PARM_DESC(drm_fbdev_overalloc, | |
60 | "Overallocation of the fbdev buffer (%) [default=" | |
61 | __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); | |
62 | ||
4be9bd10 NA |
63 | /* |
64 | * In order to keep user-space compatibility, we want in certain use-cases | |
65 | * to keep leaking the fbdev physical address to the user-space program | |
66 | * handling the fbdev buffer. | |
67 | * This is a bad habit essentially kept into closed source opengl driver | |
68 | * that should really be moved into open-source upstream projects instead | |
69 | * of using legacy physical addresses in user space to communicate with | |
70 | * other out-of-tree kernel modules. | |
71 | * | |
72 | * This module_param *should* be removed as soon as possible and be | |
73 | * considered as a broken and legacy behaviour from a modern fbdev device. | |
74 | */ | |
75 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
76 | static bool drm_leak_fbdev_smem = false; | |
77 | module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); | |
b31a3ca7 | 78 | MODULE_PARM_DESC(drm_leak_fbdev_smem, |
4be9bd10 NA |
79 | "Allow unsafe leaking fbdev physical smem address [default=false]"); |
80 | #endif | |
81 | ||
785b93ef | 82 | static LIST_HEAD(kernel_fb_helper_list); |
a53ca635 | 83 | static DEFINE_MUTEX(kernel_fb_helper_lock); |
785b93ef | 84 | |
d0ddc033 DV |
85 | /** |
86 | * DOC: fbdev helpers | |
87 | * | |
88 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel | |
83c617c5 | 89 | * mode setting driver. They can be used mostly independently from the crtc |
d0ddc033 DV |
90 | * helper functions used by many drivers to implement the kernel mode setting |
91 | * interfaces. | |
207fd329 | 92 | * |
9060d7f4 NT |
93 | * Drivers that support a dumb buffer with a virtual address and mmap support, |
94 | * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). | |
8e86dee0 TZ |
95 | * It will automatically set up deferred I/O if the driver requires a shadow |
96 | * buffer. | |
9060d7f4 | 97 | * |
95b0137f NT |
98 | * At runtime drivers should restore the fbdev console by using |
99 | * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. | |
100 | * They should also notify the fb helper code from updates to the output | |
101 | * configuration by using drm_fb_helper_output_poll_changed() as their | |
102 | * &drm_mode_config_funcs.output_poll_changed callback. | |
103 | * | |
104 | * For suspend/resume consider using drm_mode_config_helper_suspend() and | |
105 | * drm_mode_config_helper_resume() which takes care of fbdev as well. | |
207fd329 DV |
106 | * |
107 | * All other functions exported by the fb helper library can be used to | |
108 | * implement the fbdev driver interface by the driver. | |
10a23102 TR |
109 | * |
110 | * It is possible, though perhaps somewhat tricky, to implement race-free | |
111 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() | |
112 | * helper must be called first to initialize the minimum required to make | |
113 | * hotplug detection work. Drivers also need to make sure to properly set up | |
6806cdf9 | 114 | * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() |
10a23102 TR |
115 | * it is safe to enable interrupts and start processing hotplug events. At the |
116 | * same time, drivers should initialize all modeset objects such as CRTCs, | |
117 | * encoders and connectors. To finish up the fbdev helper initialization, the | |
118 | * drm_fb_helper_init() function is called. To probe for all attached displays | |
119 | * and set up an initial configuration using the detected hardware, drivers | |
e5852bee | 120 | * should call drm_fb_helper_initial_config(). |
eaa434de | 121 | * |
6806cdf9 | 122 | * If &drm_framebuffer_funcs.dirty is set, the |
2dad551c | 123 | * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will |
6806cdf9 | 124 | * accumulate changes and schedule &drm_fb_helper.dirty_work to run right |
2dad551c NT |
125 | * away. This worker then calls the dirty() function ensuring that it will |
126 | * always run in process context since the fb_*() function could be running in | |
127 | * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io | |
128 | * callback it will also schedule dirty_work with the damage collected from the | |
8e86dee0 TZ |
129 | * mmap page writes. |
130 | * | |
131 | * Deferred I/O is not compatible with SHMEM. Such drivers should request an | |
132 | * fbdev shadow buffer and call drm_fbdev_generic_setup() instead. | |
d0ddc033 DV |
133 | */ |
134 | ||
99231028 JW |
135 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
136 | { | |
137 | uint16_t *r_base, *g_base, *b_base; | |
138 | ||
ebe0f244 LP |
139 | if (crtc->funcs->gamma_set == NULL) |
140 | return; | |
141 | ||
99231028 JW |
142 | r_base = crtc->gamma_store; |
143 | g_base = r_base + crtc->gamma_size; | |
144 | b_base = g_base + crtc->gamma_size; | |
145 | ||
6d124ff8 DV |
146 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, |
147 | crtc->gamma_size, NULL); | |
99231028 JW |
148 | } |
149 | ||
207fd329 | 150 | /** |
6806cdf9 | 151 | * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter |
207fd329 DV |
152 | * @info: fbdev registered by the helper |
153 | */ | |
1a7aba7f JB |
154 | int drm_fb_helper_debug_enter(struct fb_info *info) |
155 | { | |
156 | struct drm_fb_helper *helper = info->par; | |
be26a66d | 157 | const struct drm_crtc_helper_funcs *funcs; |
d81294af | 158 | struct drm_mode_set *mode_set; |
1a7aba7f | 159 | |
1a7aba7f | 160 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
d81294af NT |
161 | mutex_lock(&helper->client.modeset_mutex); |
162 | drm_client_for_each_modeset(mode_set, &helper->client) { | |
1a7aba7f JB |
163 | if (!mode_set->crtc->enabled) |
164 | continue; | |
165 | ||
166 | funcs = mode_set->crtc->helper_private; | |
1b99b724 SC |
167 | if (funcs->mode_set_base_atomic == NULL) |
168 | continue; | |
169 | ||
9c79e0b1 DV |
170 | if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) |
171 | continue; | |
172 | ||
1a7aba7f JB |
173 | funcs->mode_set_base_atomic(mode_set->crtc, |
174 | mode_set->fb, | |
175 | mode_set->x, | |
413d45d3 | 176 | mode_set->y, |
21c74a8e | 177 | ENTER_ATOMIC_MODE_SET); |
1a7aba7f | 178 | } |
d81294af | 179 | mutex_unlock(&helper->client.modeset_mutex); |
1a7aba7f JB |
180 | } |
181 | ||
182 | return 0; | |
183 | } | |
184 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); | |
185 | ||
207fd329 | 186 | /** |
6806cdf9 | 187 | * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave |
207fd329 DV |
188 | * @info: fbdev registered by the helper |
189 | */ | |
1a7aba7f JB |
190 | int drm_fb_helper_debug_leave(struct fb_info *info) |
191 | { | |
192 | struct drm_fb_helper *helper = info->par; | |
d81294af | 193 | struct drm_client_dev *client = &helper->client; |
f33b9730 | 194 | struct drm_device *dev = helper->dev; |
1a7aba7f | 195 | struct drm_crtc *crtc; |
be26a66d | 196 | const struct drm_crtc_helper_funcs *funcs; |
d81294af | 197 | struct drm_mode_set *mode_set; |
1a7aba7f | 198 | struct drm_framebuffer *fb; |
4b4f99f5 | 199 | |
d81294af NT |
200 | mutex_lock(&client->modeset_mutex); |
201 | drm_client_for_each_modeset(mode_set, client) { | |
1a7aba7f | 202 | crtc = mode_set->crtc; |
7114d2e2 ML |
203 | if (drm_drv_uses_atomic_modeset(crtc->dev)) |
204 | continue; | |
205 | ||
1a7aba7f | 206 | funcs = crtc->helper_private; |
7114d2e2 | 207 | fb = crtc->primary->fb; |
1a7aba7f JB |
208 | |
209 | if (!crtc->enabled) | |
210 | continue; | |
211 | ||
212 | if (!fb) { | |
f33b9730 | 213 | drm_err(dev, "no fb to restore?\n"); |
1a7aba7f JB |
214 | continue; |
215 | } | |
216 | ||
1b99b724 SC |
217 | if (funcs->mode_set_base_atomic == NULL) |
218 | continue; | |
219 | ||
99231028 | 220 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
1a7aba7f | 221 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
21c74a8e | 222 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
1a7aba7f | 223 | } |
d81294af | 224 | mutex_unlock(&client->modeset_mutex); |
1a7aba7f JB |
225 | |
226 | return 0; | |
227 | } | |
228 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); | |
229 | ||
5ea1f752 RC |
230 | /** |
231 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration | |
c777990f | 232 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
5ea1f752 | 233 | * |
6806cdf9 | 234 | * This should be called from driver's drm &drm_driver.lastclose callback |
5ea1f752 RC |
235 | * when implementing an fbcon on top of kms using this helper. This ensures that |
236 | * the user isn't greeted with a black screen when e.g. X dies. | |
b7bdf0a8 DV |
237 | * |
238 | * RETURNS: | |
239 | * Zero if everything went ok, negative error code otherwise. | |
5ea1f752 | 240 | */ |
b7bdf0a8 | 241 | int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) |
5ea1f752 | 242 | { |
b7bdf0a8 DV |
243 | bool do_delayed; |
244 | int ret; | |
e2809c7d | 245 | |
c777990f | 246 | if (!drm_fbdev_emulation || !fb_helper) |
f64c5573 DV |
247 | return -ENODEV; |
248 | ||
ca91a275 DV |
249 | if (READ_ONCE(fb_helper->deferred_setup)) |
250 | return 0; | |
251 | ||
e9827d8e | 252 | mutex_lock(&fb_helper->lock); |
64914da2 | 253 | ret = drm_client_modeset_commit(&fb_helper->client); |
e2809c7d DA |
254 | |
255 | do_delayed = fb_helper->delayed_hotplug; | |
256 | if (do_delayed) | |
257 | fb_helper->delayed_hotplug = false; | |
e9827d8e | 258 | mutex_unlock(&fb_helper->lock); |
e2809c7d DA |
259 | |
260 | if (do_delayed) | |
261 | drm_fb_helper_hotplug_event(fb_helper); | |
e9827d8e | 262 | |
5ea1f752 RC |
263 | return ret; |
264 | } | |
265 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); | |
e8e7a2b8 | 266 | |
2c4124fd | 267 | #ifdef CONFIG_MAGIC_SYSRQ |
d21bf469 DV |
268 | /* |
269 | * restore fbcon display for all kms driver's using this helper, used for sysrq | |
270 | * and panic handling. | |
271 | */ | |
78b9c353 | 272 | static bool drm_fb_helper_force_kernel_mode(void) |
785b93ef | 273 | { |
785b93ef DA |
274 | bool ret, error = false; |
275 | struct drm_fb_helper *helper; | |
276 | ||
277 | if (list_empty(&kernel_fb_helper_list)) | |
278 | return false; | |
279 | ||
280 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { | |
b77f0765 TR |
281 | struct drm_device *dev = helper->dev; |
282 | ||
283 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) | |
284 | continue; | |
285 | ||
e9827d8e | 286 | mutex_lock(&helper->lock); |
eade2a17 | 287 | ret = drm_client_modeset_commit_force(&helper->client); |
e8e7a2b8 DA |
288 | if (ret) |
289 | error = true; | |
e9827d8e | 290 | mutex_unlock(&helper->lock); |
785b93ef DA |
291 | } |
292 | return error; | |
293 | } | |
294 | ||
785b93ef DA |
295 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
296 | { | |
d21bf469 | 297 | bool ret; |
4b4f99f5 | 298 | |
d21bf469 DV |
299 | ret = drm_fb_helper_force_kernel_mode(); |
300 | if (ret == true) | |
301 | DRM_ERROR("Failed to restore crtc configuration\n"); | |
785b93ef DA |
302 | } |
303 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); | |
304 | ||
1495cc9d | 305 | static void drm_fb_helper_sysrq(int dummy1) |
785b93ef DA |
306 | { |
307 | schedule_work(&drm_fb_helper_restore_work); | |
308 | } | |
309 | ||
310 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { | |
311 | .handler = drm_fb_helper_sysrq, | |
312 | .help_msg = "force-fb(V)", | |
313 | .action_msg = "Restore framebuffer console", | |
314 | }; | |
b8c40d62 RD |
315 | #else |
316 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; | |
bea1d35b | 317 | #endif |
785b93ef | 318 | |
eade2a17 NT |
319 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
320 | { | |
321 | struct drm_fb_helper *fb_helper = info->par; | |
322 | ||
323 | mutex_lock(&fb_helper->lock); | |
324 | drm_client_modeset_dpms(&fb_helper->client, dpms_mode); | |
e9827d8e | 325 | mutex_unlock(&fb_helper->lock); |
785b93ef DA |
326 | } |
327 | ||
207fd329 | 328 | /** |
6806cdf9 | 329 | * drm_fb_helper_blank - implementation for &fb_ops.fb_blank |
207fd329 DV |
330 | * @blank: desired blanking state |
331 | * @info: fbdev registered by the helper | |
332 | */ | |
785b93ef DA |
333 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
334 | { | |
c50bfd08 DV |
335 | if (oops_in_progress) |
336 | return -EBUSY; | |
337 | ||
785b93ef | 338 | switch (blank) { |
731b5a15 | 339 | /* Display: On; HSync: On, VSync: On */ |
785b93ef | 340 | case FB_BLANK_UNBLANK: |
3a8148c5 | 341 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
785b93ef | 342 | break; |
731b5a15 | 343 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 344 | case FB_BLANK_NORMAL: |
3a8148c5 | 345 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 346 | break; |
731b5a15 | 347 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef | 348 | case FB_BLANK_HSYNC_SUSPEND: |
3a8148c5 | 349 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 350 | break; |
731b5a15 | 351 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef | 352 | case FB_BLANK_VSYNC_SUSPEND: |
3a8148c5 | 353 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
785b93ef | 354 | break; |
731b5a15 | 355 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef | 356 | case FB_BLANK_POWERDOWN: |
3a8148c5 | 357 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
785b93ef DA |
358 | break; |
359 | } | |
360 | return 0; | |
361 | } | |
362 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
363 | ||
cfe63423 NT |
364 | static void drm_fb_helper_resume_worker(struct work_struct *work) |
365 | { | |
366 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, | |
367 | resume_work); | |
368 | ||
369 | console_lock(); | |
370 | fb_set_suspend(helper->fbdev, 0); | |
371 | console_unlock(); | |
372 | } | |
373 | ||
d536540f NT |
374 | static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, |
375 | struct drm_clip_rect *clip) | |
376 | { | |
377 | struct drm_framebuffer *fb = fb_helper->fb; | |
b0f986b4 | 378 | unsigned int cpp = fb->format->cpp[0]; |
d536540f NT |
379 | size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; |
380 | void *src = fb_helper->fbdev->screen_buffer + offset; | |
381 | void *dst = fb_helper->buffer->vaddr + offset; | |
382 | size_t len = (clip->x2 - clip->x1) * cpp; | |
383 | unsigned int y; | |
384 | ||
385 | for (y = clip->y1; y < clip->y2; y++) { | |
386 | memcpy(dst, src, len); | |
387 | src += fb->pitches[0]; | |
388 | dst += fb->pitches[0]; | |
389 | } | |
390 | } | |
391 | ||
eaa434de NT |
392 | static void drm_fb_helper_dirty_work(struct work_struct *work) |
393 | { | |
394 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, | |
395 | dirty_work); | |
396 | struct drm_clip_rect *clip = &helper->dirty_clip; | |
397 | struct drm_clip_rect clip_copy; | |
398 | unsigned long flags; | |
cf1ca9ae | 399 | void *vaddr; |
eaa434de NT |
400 | |
401 | spin_lock_irqsave(&helper->dirty_lock, flags); | |
402 | clip_copy = *clip; | |
403 | clip->x1 = clip->y1 = ~0; | |
404 | clip->x2 = clip->y2 = 0; | |
405 | spin_unlock_irqrestore(&helper->dirty_lock, flags); | |
406 | ||
87d3b658 | 407 | /* call dirty callback only when it has been really touched */ |
d536540f | 408 | if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { |
cf1ca9ae | 409 | |
d536540f | 410 | /* Generic fbdev uses a shadow buffer */ |
cf1ca9ae TZ |
411 | if (helper->buffer) { |
412 | vaddr = drm_client_buffer_vmap(helper->buffer); | |
413 | if (IS_ERR(vaddr)) | |
414 | return; | |
d536540f | 415 | drm_fb_helper_dirty_blit_real(helper, &clip_copy); |
cf1ca9ae | 416 | } |
f458579e TZ |
417 | if (helper->fb->funcs->dirty) |
418 | helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, | |
419 | &clip_copy, 1); | |
cf1ca9ae TZ |
420 | |
421 | if (helper->buffer) | |
422 | drm_client_buffer_vunmap(helper->buffer); | |
d536540f | 423 | } |
eaa434de NT |
424 | } |
425 | ||
10a23102 TR |
426 | /** |
427 | * drm_fb_helper_prepare - setup a drm_fb_helper structure | |
428 | * @dev: DRM device | |
429 | * @helper: driver-allocated fbdev helper structure to set up | |
430 | * @funcs: pointer to structure of functions associate with this helper | |
431 | * | |
432 | * Sets up the bare minimum to make the framebuffer helper usable. This is | |
433 | * useful to implement race-free initialization of the polling helpers. | |
434 | */ | |
435 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, | |
436 | const struct drm_fb_helper_funcs *funcs) | |
437 | { | |
438 | INIT_LIST_HEAD(&helper->kernel_fb_list); | |
eaa434de | 439 | spin_lock_init(&helper->dirty_lock); |
cfe63423 | 440 | INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); |
eaa434de NT |
441 | INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); |
442 | helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; | |
e9827d8e | 443 | mutex_init(&helper->lock); |
10a23102 TR |
444 | helper->funcs = funcs; |
445 | helper->dev = dev; | |
446 | } | |
447 | EXPORT_SYMBOL(drm_fb_helper_prepare); | |
448 | ||
207fd329 | 449 | /** |
ed84e254 | 450 | * drm_fb_helper_init - initialize a &struct drm_fb_helper |
207fd329 DV |
451 | * @dev: drm device |
452 | * @fb_helper: driver-allocated fbdev helper structure to initialize | |
d81294af | 453 | * @max_conn_count: max connector count (not used) |
207fd329 DV |
454 | * |
455 | * This allocates the structures for the fbdev helper with the given limits. | |
456 | * Note that this won't yet touch the hardware (through the driver interfaces) | |
457 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() | |
458 | * to allow driver writes more control over the exact init sequence. | |
459 | * | |
10a23102 | 460 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
207fd329 DV |
461 | * |
462 | * RETURNS: | |
463 | * Zero if everything went ok, nonzero otherwise. | |
464 | */ | |
4abe3520 DA |
465 | int drm_fb_helper_init(struct drm_device *dev, |
466 | struct drm_fb_helper *fb_helper, | |
e4563f6b | 467 | int max_conn_count) |
785b93ef | 468 | { |
d81294af | 469 | int ret; |
785b93ef | 470 | |
29ad20b2 NT |
471 | if (!drm_fbdev_emulation) { |
472 | dev->fb_helper = fb_helper; | |
f64c5573 | 473 | return 0; |
29ad20b2 | 474 | } |
f64c5573 | 475 | |
d81294af NT |
476 | /* |
477 | * If this is not the generic fbdev client, initialize a drm_client | |
478 | * without callbacks so we can use the modesets. | |
479 | */ | |
480 | if (!fb_helper->client.funcs) { | |
481 | ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL); | |
482 | if (ret) | |
483 | return ret; | |
484 | } | |
785b93ef | 485 | |
29ad20b2 NT |
486 | dev->fb_helper = fb_helper; |
487 | ||
785b93ef | 488 | return 0; |
785b93ef | 489 | } |
4abe3520 DA |
490 | EXPORT_SYMBOL(drm_fb_helper_init); |
491 | ||
b8017d6c AT |
492 | /** |
493 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members | |
494 | * @fb_helper: driver-allocated fbdev helper | |
495 | * | |
496 | * A helper to alloc fb_info and the members cmap and apertures. Called | |
da7bdda2 DV |
497 | * by the driver within the fb_probe fb_helper callback function. Drivers do not |
498 | * need to release the allocated fb_info structure themselves, this is | |
499 | * automatically done when calling drm_fb_helper_fini(). | |
b8017d6c AT |
500 | * |
501 | * RETURNS: | |
502 | * fb_info pointer if things went okay, pointer containing error code | |
503 | * otherwise | |
504 | */ | |
505 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) | |
506 | { | |
507 | struct device *dev = fb_helper->dev->dev; | |
508 | struct fb_info *info; | |
509 | int ret; | |
510 | ||
511 | info = framebuffer_alloc(0, dev); | |
512 | if (!info) | |
513 | return ERR_PTR(-ENOMEM); | |
514 | ||
515 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
516 | if (ret) | |
517 | goto err_release; | |
518 | ||
519 | info->apertures = alloc_apertures(1); | |
520 | if (!info->apertures) { | |
521 | ret = -ENOMEM; | |
522 | goto err_free_cmap; | |
523 | } | |
524 | ||
525 | fb_helper->fbdev = info; | |
8782c647 | 526 | info->skip_vt_switch = true; |
b8017d6c AT |
527 | |
528 | return info; | |
529 | ||
530 | err_free_cmap: | |
531 | fb_dealloc_cmap(&info->cmap); | |
532 | err_release: | |
533 | framebuffer_release(info); | |
534 | return ERR_PTR(ret); | |
535 | } | |
536 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); | |
537 | ||
538 | /** | |
539 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device | |
c777990f | 540 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
b8017d6c AT |
541 | * |
542 | * A wrapper around unregister_framebuffer, to release the fb_info | |
ed84e254 DV |
543 | * framebuffer device. This must be called before releasing all resources for |
544 | * @fb_helper by calling drm_fb_helper_fini(). | |
b8017d6c AT |
545 | */ |
546 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) | |
547 | { | |
548 | if (fb_helper && fb_helper->fbdev) | |
549 | unregister_framebuffer(fb_helper->fbdev); | |
550 | } | |
551 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); | |
552 | ||
ed84e254 DV |
553 | /** |
554 | * drm_fb_helper_fini - finialize a &struct drm_fb_helper | |
c777990f | 555 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
ed84e254 | 556 | * |
6821603a | 557 | * This cleans up all remaining resources associated with @fb_helper. |
ed84e254 | 558 | */ |
4abe3520 DA |
559 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
560 | { | |
da7bdda2 DV |
561 | struct fb_info *info; |
562 | ||
29ad20b2 NT |
563 | if (!fb_helper) |
564 | return; | |
565 | ||
566 | fb_helper->dev->fb_helper = NULL; | |
567 | ||
568 | if (!drm_fbdev_emulation) | |
f64c5573 DV |
569 | return; |
570 | ||
b52f09c4 NT |
571 | cancel_work_sync(&fb_helper->resume_work); |
572 | cancel_work_sync(&fb_helper->dirty_work); | |
573 | ||
da7bdda2 DV |
574 | info = fb_helper->fbdev; |
575 | if (info) { | |
576 | if (info->cmap.len) | |
577 | fb_dealloc_cmap(&info->cmap); | |
578 | framebuffer_release(info); | |
579 | } | |
580 | fb_helper->fbdev = NULL; | |
581 | ||
a53ca635 | 582 | mutex_lock(&kernel_fb_helper_lock); |
4abe3520 DA |
583 | if (!list_empty(&fb_helper->kernel_fb_list)) { |
584 | list_del(&fb_helper->kernel_fb_list); | |
4b4f99f5 | 585 | if (list_empty(&kernel_fb_helper_list)) |
4abe3520 | 586 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
4abe3520 | 587 | } |
a53ca635 | 588 | mutex_unlock(&kernel_fb_helper_lock); |
4abe3520 | 589 | |
e9827d8e | 590 | mutex_destroy(&fb_helper->lock); |
4abe3520 | 591 | |
d81294af NT |
592 | if (!fb_helper->client.funcs) |
593 | drm_client_release(&fb_helper->client); | |
4abe3520 DA |
594 | } |
595 | EXPORT_SYMBOL(drm_fb_helper_fini); | |
785b93ef | 596 | |
f458579e TZ |
597 | static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper) |
598 | { | |
599 | struct drm_device *dev = fb_helper->dev; | |
600 | struct drm_framebuffer *fb = fb_helper->fb; | |
601 | ||
602 | return dev->mode_config.prefer_shadow_fbdev || | |
603 | dev->mode_config.prefer_shadow || | |
604 | fb->funcs->dirty; | |
605 | } | |
606 | ||
eaa434de NT |
607 | static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, |
608 | u32 width, u32 height) | |
609 | { | |
610 | struct drm_fb_helper *helper = info->par; | |
611 | struct drm_clip_rect *clip = &helper->dirty_clip; | |
612 | unsigned long flags; | |
613 | ||
f458579e | 614 | if (!drm_fbdev_use_shadow_fb(helper)) |
eaa434de NT |
615 | return; |
616 | ||
617 | spin_lock_irqsave(&helper->dirty_lock, flags); | |
618 | clip->x1 = min_t(u32, clip->x1, x); | |
619 | clip->y1 = min_t(u32, clip->y1, y); | |
620 | clip->x2 = max_t(u32, clip->x2, x + width); | |
621 | clip->y2 = max_t(u32, clip->y2, y + height); | |
622 | spin_unlock_irqrestore(&helper->dirty_lock, flags); | |
623 | ||
624 | schedule_work(&helper->dirty_work); | |
625 | } | |
626 | ||
627 | /** | |
628 | * drm_fb_helper_deferred_io() - fbdev deferred_io callback function | |
629 | * @info: fb_info struct pointer | |
630 | * @pagelist: list of dirty mmap framebuffer pages | |
631 | * | |
6806cdf9 | 632 | * This function is used as the &fb_deferred_io.deferred_io |
eaa434de NT |
633 | * callback function for flushing the fbdev mmap writes. |
634 | */ | |
635 | void drm_fb_helper_deferred_io(struct fb_info *info, | |
636 | struct list_head *pagelist) | |
637 | { | |
638 | unsigned long start, end, min, max; | |
639 | struct page *page; | |
640 | u32 y1, y2; | |
641 | ||
642 | min = ULONG_MAX; | |
643 | max = 0; | |
644 | list_for_each_entry(page, pagelist, lru) { | |
645 | start = page->index << PAGE_SHIFT; | |
646 | end = start + PAGE_SIZE - 1; | |
647 | min = min(min, start); | |
648 | max = max(max, end); | |
649 | } | |
650 | ||
651 | if (min < max) { | |
652 | y1 = min / info->fix.line_length; | |
653 | y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), | |
654 | info->var.yres); | |
655 | drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); | |
656 | } | |
657 | } | |
658 | EXPORT_SYMBOL(drm_fb_helper_deferred_io); | |
659 | ||
cbb1a82e AT |
660 | /** |
661 | * drm_fb_helper_sys_read - wrapper around fb_sys_read | |
662 | * @info: fb_info struct pointer | |
663 | * @buf: userspace buffer to read from framebuffer memory | |
664 | * @count: number of bytes to read from framebuffer memory | |
665 | * @ppos: read offset within framebuffer memory | |
666 | * | |
667 | * A wrapper around fb_sys_read implemented by fbdev core | |
668 | */ | |
669 | ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, | |
670 | size_t count, loff_t *ppos) | |
671 | { | |
672 | return fb_sys_read(info, buf, count, ppos); | |
673 | } | |
674 | EXPORT_SYMBOL(drm_fb_helper_sys_read); | |
675 | ||
676 | /** | |
677 | * drm_fb_helper_sys_write - wrapper around fb_sys_write | |
678 | * @info: fb_info struct pointer | |
679 | * @buf: userspace buffer to write to framebuffer memory | |
680 | * @count: number of bytes to write to framebuffer memory | |
681 | * @ppos: write offset within framebuffer memory | |
682 | * | |
683 | * A wrapper around fb_sys_write implemented by fbdev core | |
684 | */ | |
685 | ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, | |
686 | size_t count, loff_t *ppos) | |
687 | { | |
eaa434de NT |
688 | ssize_t ret; |
689 | ||
690 | ret = fb_sys_write(info, buf, count, ppos); | |
691 | if (ret > 0) | |
692 | drm_fb_helper_dirty(info, 0, 0, info->var.xres, | |
693 | info->var.yres); | |
694 | ||
695 | return ret; | |
cbb1a82e AT |
696 | } |
697 | EXPORT_SYMBOL(drm_fb_helper_sys_write); | |
698 | ||
742547b7 AT |
699 | /** |
700 | * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect | |
701 | * @info: fbdev registered by the helper | |
702 | * @rect: info about rectangle to fill | |
703 | * | |
704 | * A wrapper around sys_fillrect implemented by fbdev core | |
705 | */ | |
706 | void drm_fb_helper_sys_fillrect(struct fb_info *info, | |
707 | const struct fb_fillrect *rect) | |
708 | { | |
709 | sys_fillrect(info, rect); | |
eaa434de NT |
710 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
711 | rect->width, rect->height); | |
742547b7 AT |
712 | } |
713 | EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); | |
714 | ||
715 | /** | |
716 | * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea | |
717 | * @info: fbdev registered by the helper | |
718 | * @area: info about area to copy | |
719 | * | |
720 | * A wrapper around sys_copyarea implemented by fbdev core | |
721 | */ | |
722 | void drm_fb_helper_sys_copyarea(struct fb_info *info, | |
723 | const struct fb_copyarea *area) | |
724 | { | |
725 | sys_copyarea(info, area); | |
eaa434de NT |
726 | drm_fb_helper_dirty(info, area->dx, area->dy, |
727 | area->width, area->height); | |
742547b7 AT |
728 | } |
729 | EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); | |
730 | ||
731 | /** | |
732 | * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit | |
733 | * @info: fbdev registered by the helper | |
734 | * @image: info about image to blit | |
735 | * | |
736 | * A wrapper around sys_imageblit implemented by fbdev core | |
737 | */ | |
738 | void drm_fb_helper_sys_imageblit(struct fb_info *info, | |
739 | const struct fb_image *image) | |
740 | { | |
741 | sys_imageblit(info, image); | |
eaa434de NT |
742 | drm_fb_helper_dirty(info, image->dx, image->dy, |
743 | image->width, image->height); | |
742547b7 AT |
744 | } |
745 | EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); | |
746 | ||
747 | /** | |
748 | * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect | |
749 | * @info: fbdev registered by the helper | |
750 | * @rect: info about rectangle to fill | |
751 | * | |
d104328c | 752 | * A wrapper around cfb_fillrect implemented by fbdev core |
742547b7 AT |
753 | */ |
754 | void drm_fb_helper_cfb_fillrect(struct fb_info *info, | |
755 | const struct fb_fillrect *rect) | |
756 | { | |
757 | cfb_fillrect(info, rect); | |
eaa434de NT |
758 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
759 | rect->width, rect->height); | |
742547b7 AT |
760 | } |
761 | EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); | |
762 | ||
763 | /** | |
764 | * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea | |
765 | * @info: fbdev registered by the helper | |
766 | * @area: info about area to copy | |
767 | * | |
768 | * A wrapper around cfb_copyarea implemented by fbdev core | |
769 | */ | |
770 | void drm_fb_helper_cfb_copyarea(struct fb_info *info, | |
771 | const struct fb_copyarea *area) | |
772 | { | |
773 | cfb_copyarea(info, area); | |
eaa434de NT |
774 | drm_fb_helper_dirty(info, area->dx, area->dy, |
775 | area->width, area->height); | |
742547b7 AT |
776 | } |
777 | EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); | |
778 | ||
779 | /** | |
780 | * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit | |
781 | * @info: fbdev registered by the helper | |
782 | * @image: info about image to blit | |
783 | * | |
784 | * A wrapper around cfb_imageblit implemented by fbdev core | |
785 | */ | |
786 | void drm_fb_helper_cfb_imageblit(struct fb_info *info, | |
787 | const struct fb_image *image) | |
788 | { | |
789 | cfb_imageblit(info, image); | |
eaa434de NT |
790 | drm_fb_helper_dirty(info, image->dx, image->dy, |
791 | image->width, image->height); | |
742547b7 AT |
792 | } |
793 | EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); | |
794 | ||
fdefa58a AT |
795 | /** |
796 | * drm_fb_helper_set_suspend - wrapper around fb_set_suspend | |
c777990f | 797 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
28579f37 | 798 | * @suspend: whether to suspend or resume |
fdefa58a | 799 | * |
cfe63423 NT |
800 | * A wrapper around fb_set_suspend implemented by fbdev core. |
801 | * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take | |
802 | * the lock yourself | |
fdefa58a | 803 | */ |
28579f37 | 804 | void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) |
fdefa58a AT |
805 | { |
806 | if (fb_helper && fb_helper->fbdev) | |
28579f37 | 807 | fb_set_suspend(fb_helper->fbdev, suspend); |
fdefa58a AT |
808 | } |
809 | EXPORT_SYMBOL(drm_fb_helper_set_suspend); | |
810 | ||
cfe63423 NT |
811 | /** |
812 | * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also | |
813 | * takes the console lock | |
c777990f | 814 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
28579f37 | 815 | * @suspend: whether to suspend or resume |
cfe63423 NT |
816 | * |
817 | * A wrapper around fb_set_suspend() that takes the console lock. If the lock | |
818 | * isn't available on resume, a worker is tasked with waiting for the lock | |
819 | * to become available. The console lock can be pretty contented on resume | |
820 | * due to all the printk activity. | |
821 | * | |
822 | * This function can be called multiple times with the same state since | |
6806cdf9 | 823 | * &fb_info.state is checked to see if fbdev is running or not before locking. |
cfe63423 NT |
824 | * |
825 | * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. | |
826 | */ | |
827 | void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, | |
28579f37 | 828 | bool suspend) |
cfe63423 NT |
829 | { |
830 | if (!fb_helper || !fb_helper->fbdev) | |
831 | return; | |
832 | ||
833 | /* make sure there's no pending/ongoing resume */ | |
834 | flush_work(&fb_helper->resume_work); | |
835 | ||
836 | if (suspend) { | |
837 | if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) | |
838 | return; | |
839 | ||
840 | console_lock(); | |
841 | ||
842 | } else { | |
843 | if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) | |
844 | return; | |
845 | ||
846 | if (!console_trylock()) { | |
847 | schedule_work(&fb_helper->resume_work); | |
848 | return; | |
849 | } | |
850 | } | |
851 | ||
852 | fb_set_suspend(fb_helper->fbdev, suspend); | |
853 | console_unlock(); | |
854 | } | |
855 | EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); | |
856 | ||
b8e2b019 PR |
857 | static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) |
858 | { | |
859 | u32 *palette = (u32 *)info->pseudo_palette; | |
860 | int i; | |
861 | ||
862 | if (cmap->start + cmap->len > 16) | |
863 | return -EINVAL; | |
864 | ||
865 | for (i = 0; i < cmap->len; ++i) { | |
866 | u16 red = cmap->red[i]; | |
867 | u16 green = cmap->green[i]; | |
868 | u16 blue = cmap->blue[i]; | |
869 | u32 value; | |
870 | ||
871 | red >>= 16 - info->var.red.length; | |
872 | green >>= 16 - info->var.green.length; | |
873 | blue >>= 16 - info->var.blue.length; | |
874 | value = (red << info->var.red.offset) | | |
875 | (green << info->var.green.offset) | | |
876 | (blue << info->var.blue.offset); | |
877 | if (info->var.transp.length > 0) { | |
878 | u32 mask = (1 << info->var.transp.length) - 1; | |
879 | ||
880 | mask <<= info->var.transp.offset; | |
881 | value |= mask; | |
882 | } | |
883 | palette[cmap->start + i] = value; | |
884 | } | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
964c6006 | 889 | static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) |
068143d3 DA |
890 | { |
891 | struct drm_fb_helper *fb_helper = info->par; | |
d81294af | 892 | struct drm_mode_set *modeset; |
068143d3 | 893 | struct drm_crtc *crtc; |
a3562a0e | 894 | u16 *r, *g, *b; |
d81294af | 895 | int ret = 0; |
068143d3 | 896 | |
964c6006 | 897 | drm_modeset_lock_all(fb_helper->dev); |
d81294af NT |
898 | drm_client_for_each_modeset(modeset, &fb_helper->client) { |
899 | crtc = modeset->crtc; | |
964c6006 PR |
900 | if (!crtc->funcs->gamma_set || !crtc->gamma_size) |
901 | return -EINVAL; | |
c50bfd08 | 902 | |
964c6006 PR |
903 | if (cmap->start + cmap->len > crtc->gamma_size) |
904 | return -EINVAL; | |
905 | ||
906 | r = crtc->gamma_store; | |
907 | g = r + crtc->gamma_size; | |
908 | b = g + crtc->gamma_size; | |
909 | ||
910 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); | |
911 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); | |
912 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); | |
913 | ||
914 | ret = crtc->funcs->gamma_set(crtc, r, g, b, | |
915 | crtc->gamma_size, NULL); | |
916 | if (ret) | |
917 | return ret; | |
8391a3d5 | 918 | } |
964c6006 | 919 | drm_modeset_unlock_all(fb_helper->dev); |
8391a3d5 | 920 | |
964c6006 PR |
921 | return ret; |
922 | } | |
923 | ||
924 | static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, | |
925 | struct fb_cmap *cmap) | |
926 | { | |
927 | struct drm_device *dev = crtc->dev; | |
928 | struct drm_property_blob *gamma_lut; | |
929 | struct drm_color_lut *lut; | |
930 | int size = crtc->gamma_size; | |
931 | int i; | |
932 | ||
933 | if (!size || cmap->start + cmap->len > size) | |
934 | return ERR_PTR(-EINVAL); | |
935 | ||
936 | gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); | |
937 | if (IS_ERR(gamma_lut)) | |
938 | return gamma_lut; | |
939 | ||
11b83e3f | 940 | lut = gamma_lut->data; |
964c6006 PR |
941 | if (cmap->start || cmap->len != size) { |
942 | u16 *r = crtc->gamma_store; | |
943 | u16 *g = r + crtc->gamma_size; | |
944 | u16 *b = g + crtc->gamma_size; | |
945 | ||
946 | for (i = 0; i < cmap->start; i++) { | |
947 | lut[i].red = r[i]; | |
948 | lut[i].green = g[i]; | |
949 | lut[i].blue = b[i]; | |
950 | } | |
951 | for (i = cmap->start + cmap->len; i < size; i++) { | |
952 | lut[i].red = r[i]; | |
953 | lut[i].green = g[i]; | |
954 | lut[i].blue = b[i]; | |
955 | } | |
956 | } | |
957 | ||
958 | for (i = 0; i < cmap->len; i++) { | |
959 | lut[cmap->start + i].red = cmap->red[i]; | |
960 | lut[cmap->start + i].green = cmap->green[i]; | |
961 | lut[cmap->start + i].blue = cmap->blue[i]; | |
962 | } | |
963 | ||
964 | return gamma_lut; | |
965 | } | |
966 | ||
967 | static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) | |
968 | { | |
969 | struct drm_fb_helper *fb_helper = info->par; | |
970 | struct drm_device *dev = fb_helper->dev; | |
971 | struct drm_property_blob *gamma_lut = NULL; | |
972 | struct drm_modeset_acquire_ctx ctx; | |
973 | struct drm_crtc_state *crtc_state; | |
974 | struct drm_atomic_state *state; | |
d81294af | 975 | struct drm_mode_set *modeset; |
964c6006 PR |
976 | struct drm_crtc *crtc; |
977 | u16 *r, *g, *b; | |
964c6006 | 978 | bool replaced; |
d81294af | 979 | int ret = 0; |
964c6006 PR |
980 | |
981 | drm_modeset_acquire_init(&ctx, 0); | |
982 | ||
983 | state = drm_atomic_state_alloc(dev); | |
984 | if (!state) { | |
985 | ret = -ENOMEM; | |
986 | goto out_ctx; | |
b8e2b019 PR |
987 | } |
988 | ||
964c6006 PR |
989 | state->acquire_ctx = &ctx; |
990 | retry: | |
d81294af NT |
991 | drm_client_for_each_modeset(modeset, &fb_helper->client) { |
992 | crtc = modeset->crtc; | |
068143d3 | 993 | |
964c6006 PR |
994 | if (!gamma_lut) |
995 | gamma_lut = setcmap_new_gamma_lut(crtc, cmap); | |
996 | if (IS_ERR(gamma_lut)) { | |
997 | ret = PTR_ERR(gamma_lut); | |
998 | gamma_lut = NULL; | |
999 | goto out_state; | |
a3562a0e PR |
1000 | } |
1001 | ||
964c6006 PR |
1002 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
1003 | if (IS_ERR(crtc_state)) { | |
1004 | ret = PTR_ERR(crtc_state); | |
1005 | goto out_state; | |
a3562a0e PR |
1006 | } |
1007 | ||
964c6006 PR |
1008 | replaced = drm_property_replace_blob(&crtc_state->degamma_lut, |
1009 | NULL); | |
1010 | replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); | |
1011 | replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, | |
1012 | gamma_lut); | |
1013 | crtc_state->color_mgmt_changed |= replaced; | |
1014 | } | |
1015 | ||
1016 | ret = drm_atomic_commit(state); | |
1017 | if (ret) | |
1018 | goto out_state; | |
1019 | ||
d81294af NT |
1020 | drm_client_for_each_modeset(modeset, &fb_helper->client) { |
1021 | crtc = modeset->crtc; | |
964c6006 | 1022 | |
a3562a0e PR |
1023 | r = crtc->gamma_store; |
1024 | g = r + crtc->gamma_size; | |
1025 | b = g + crtc->gamma_size; | |
1026 | ||
1027 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); | |
1028 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); | |
1029 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); | |
964c6006 | 1030 | } |
a3562a0e | 1031 | |
964c6006 PR |
1032 | out_state: |
1033 | if (ret == -EDEADLK) | |
1034 | goto backoff; | |
068143d3 | 1035 | |
964c6006 PR |
1036 | drm_property_blob_put(gamma_lut); |
1037 | drm_atomic_state_put(state); | |
1038 | out_ctx: | |
1039 | drm_modeset_drop_locks(&ctx); | |
1040 | drm_modeset_acquire_fini(&ctx); | |
068143d3 | 1041 | |
964c6006 | 1042 | return ret; |
068143d3 | 1043 | |
964c6006 PR |
1044 | backoff: |
1045 | drm_atomic_state_clear(state); | |
1046 | drm_modeset_backoff(&ctx); | |
1047 | goto retry; | |
1048 | } | |
1049 | ||
1050 | /** | |
1051 | * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap | |
1052 | * @cmap: cmap to set | |
1053 | * @info: fbdev registered by the helper | |
1054 | */ | |
1055 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) | |
1056 | { | |
1057 | struct drm_fb_helper *fb_helper = info->par; | |
03a9606e | 1058 | struct drm_device *dev = fb_helper->dev; |
964c6006 PR |
1059 | int ret; |
1060 | ||
1061 | if (oops_in_progress) | |
1062 | return -EBUSY; | |
1063 | ||
1064 | mutex_lock(&fb_helper->lock); | |
1065 | ||
03a9606e | 1066 | if (!drm_master_internal_acquire(dev)) { |
964c6006 | 1067 | ret = -EBUSY; |
03a9606e | 1068 | goto unlock; |
068143d3 | 1069 | } |
964c6006 | 1070 | |
d81294af | 1071 | mutex_lock(&fb_helper->client.modeset_mutex); |
964c6006 PR |
1072 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) |
1073 | ret = setcmap_pseudo_palette(cmap, info); | |
1074 | else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) | |
1075 | ret = setcmap_atomic(cmap, info); | |
1076 | else | |
1077 | ret = setcmap_legacy(cmap, info); | |
d81294af | 1078 | mutex_unlock(&fb_helper->client.modeset_mutex); |
964c6006 | 1079 | |
03a9606e NT |
1080 | drm_master_internal_release(dev); |
1081 | unlock: | |
e9827d8e | 1082 | mutex_unlock(&fb_helper->lock); |
964c6006 PR |
1083 | |
1084 | return ret; | |
068143d3 DA |
1085 | } |
1086 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
1087 | ||
0f3bbe07 MR |
1088 | /** |
1089 | * drm_fb_helper_ioctl - legacy ioctl implementation | |
1090 | * @info: fbdev registered by the helper | |
1091 | * @cmd: ioctl command | |
1092 | * @arg: ioctl argument | |
1093 | * | |
1094 | * A helper to implement the standard fbdev ioctl. Only | |
1095 | * FBIO_WAITFORVSYNC is implemented for now. | |
1096 | */ | |
1097 | int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, | |
1098 | unsigned long arg) | |
1099 | { | |
1100 | struct drm_fb_helper *fb_helper = info->par; | |
03a9606e | 1101 | struct drm_device *dev = fb_helper->dev; |
0f3bbe07 MR |
1102 | struct drm_crtc *crtc; |
1103 | int ret = 0; | |
1104 | ||
e9827d8e | 1105 | mutex_lock(&fb_helper->lock); |
03a9606e | 1106 | if (!drm_master_internal_acquire(dev)) { |
0f3bbe07 MR |
1107 | ret = -EBUSY; |
1108 | goto unlock; | |
1109 | } | |
1110 | ||
1111 | switch (cmd) { | |
1112 | case FBIO_WAITFORVSYNC: | |
1113 | /* | |
1114 | * Only consider the first CRTC. | |
1115 | * | |
1116 | * This ioctl is supposed to take the CRTC number as | |
1117 | * an argument, but in fbdev times, what that number | |
1118 | * was supposed to be was quite unclear, different | |
1119 | * drivers were passing that argument differently | |
1120 | * (some by reference, some by value), and most of the | |
1121 | * userspace applications were just hardcoding 0 as an | |
1122 | * argument. | |
1123 | * | |
1124 | * The first CRTC should be the integrated panel on | |
1125 | * most drivers, so this is the best choice we can | |
1126 | * make. If we're not smart enough here, one should | |
1127 | * just consider switch the userspace to KMS. | |
1128 | */ | |
d81294af | 1129 | crtc = fb_helper->client.modesets[0].crtc; |
0f3bbe07 MR |
1130 | |
1131 | /* | |
1132 | * Only wait for a vblank event if the CRTC is | |
1133 | * enabled, otherwise just don't do anythintg, | |
1134 | * not even report an error. | |
1135 | */ | |
1136 | ret = drm_crtc_vblank_get(crtc); | |
1137 | if (!ret) { | |
1138 | drm_crtc_wait_one_vblank(crtc); | |
1139 | drm_crtc_vblank_put(crtc); | |
1140 | } | |
1141 | ||
1142 | ret = 0; | |
03a9606e | 1143 | break; |
0f3bbe07 MR |
1144 | default: |
1145 | ret = -ENOTTY; | |
1146 | } | |
1147 | ||
03a9606e | 1148 | drm_master_internal_release(dev); |
0f3bbe07 | 1149 | unlock: |
e9827d8e | 1150 | mutex_unlock(&fb_helper->lock); |
0f3bbe07 MR |
1151 | return ret; |
1152 | } | |
1153 | EXPORT_SYMBOL(drm_fb_helper_ioctl); | |
1154 | ||
db05c481 EP |
1155 | static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, |
1156 | const struct fb_var_screeninfo *var_2) | |
1157 | { | |
1158 | return var_1->bits_per_pixel == var_2->bits_per_pixel && | |
1159 | var_1->grayscale == var_2->grayscale && | |
1160 | var_1->red.offset == var_2->red.offset && | |
1161 | var_1->red.length == var_2->red.length && | |
1162 | var_1->red.msb_right == var_2->red.msb_right && | |
1163 | var_1->green.offset == var_2->green.offset && | |
1164 | var_1->green.length == var_2->green.length && | |
1165 | var_1->green.msb_right == var_2->green.msb_right && | |
1166 | var_1->blue.offset == var_2->blue.offset && | |
1167 | var_1->blue.length == var_2->blue.length && | |
1168 | var_1->blue.msb_right == var_2->blue.msb_right && | |
1169 | var_1->transp.offset == var_2->transp.offset && | |
1170 | var_1->transp.length == var_2->transp.length && | |
1171 | var_1->transp.msb_right == var_2->transp.msb_right; | |
1172 | } | |
1173 | ||
62d85b3b IM |
1174 | static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var, |
1175 | u8 depth) | |
1176 | { | |
1177 | switch (depth) { | |
1178 | case 8: | |
1179 | var->red.offset = 0; | |
1180 | var->green.offset = 0; | |
1181 | var->blue.offset = 0; | |
1182 | var->red.length = 8; /* 8bit DAC */ | |
1183 | var->green.length = 8; | |
1184 | var->blue.length = 8; | |
1185 | var->transp.offset = 0; | |
1186 | var->transp.length = 0; | |
1187 | break; | |
1188 | case 15: | |
1189 | var->red.offset = 10; | |
1190 | var->green.offset = 5; | |
1191 | var->blue.offset = 0; | |
1192 | var->red.length = 5; | |
1193 | var->green.length = 5; | |
1194 | var->blue.length = 5; | |
1195 | var->transp.offset = 15; | |
1196 | var->transp.length = 1; | |
1197 | break; | |
1198 | case 16: | |
1199 | var->red.offset = 11; | |
1200 | var->green.offset = 5; | |
1201 | var->blue.offset = 0; | |
1202 | var->red.length = 5; | |
1203 | var->green.length = 6; | |
1204 | var->blue.length = 5; | |
1205 | var->transp.offset = 0; | |
1206 | break; | |
1207 | case 24: | |
1208 | var->red.offset = 16; | |
1209 | var->green.offset = 8; | |
1210 | var->blue.offset = 0; | |
1211 | var->red.length = 8; | |
1212 | var->green.length = 8; | |
1213 | var->blue.length = 8; | |
1214 | var->transp.offset = 0; | |
1215 | var->transp.length = 0; | |
1216 | break; | |
1217 | case 32: | |
1218 | var->red.offset = 16; | |
1219 | var->green.offset = 8; | |
1220 | var->blue.offset = 0; | |
1221 | var->red.length = 8; | |
1222 | var->green.length = 8; | |
1223 | var->blue.length = 8; | |
1224 | var->transp.offset = 24; | |
1225 | var->transp.length = 8; | |
1226 | break; | |
1227 | default: | |
1228 | break; | |
1229 | } | |
1230 | } | |
1231 | ||
207fd329 | 1232 | /** |
6806cdf9 | 1233 | * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var |
207fd329 DV |
1234 | * @var: screeninfo to check |
1235 | * @info: fbdev registered by the helper | |
1236 | */ | |
785b93ef DA |
1237 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
1238 | struct fb_info *info) | |
1239 | { | |
1240 | struct drm_fb_helper *fb_helper = info->par; | |
1241 | struct drm_framebuffer *fb = fb_helper->fb; | |
f33b9730 | 1242 | struct drm_device *dev = fb_helper->dev; |
785b93ef | 1243 | |
66a8d5bf | 1244 | if (in_dbg_master()) |
785b93ef DA |
1245 | return -EINVAL; |
1246 | ||
66a8d5bf | 1247 | if (var->pixclock != 0) { |
f33b9730 | 1248 | drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n"); |
66a8d5bf IM |
1249 | var->pixclock = 0; |
1250 | } | |
1251 | ||
042bf753 AG |
1252 | if ((drm_format_info_block_width(fb->format, 0) > 1) || |
1253 | (drm_format_info_block_height(fb->format, 0) > 1)) | |
1254 | return -EINVAL; | |
1255 | ||
865afb11 SA |
1256 | /* |
1257 | * Changes struct fb_var_screeninfo are currently not pushed back | |
1258 | * to KMS, hence fail if different settings are requested. | |
1259 | */ | |
272725c7 | 1260 | if (var->bits_per_pixel != fb->format->cpp[0] * 8 || |
12ffed96 MD |
1261 | var->xres > fb->width || var->yres > fb->height || |
1262 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { | |
f33b9730 | 1263 | drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb " |
62fb376e CW |
1264 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
1265 | var->xres, var->yres, var->bits_per_pixel, | |
1266 | var->xres_virtual, var->yres_virtual, | |
272725c7 | 1267 | fb->width, fb->height, fb->format->cpp[0] * 8); |
785b93ef DA |
1268 | return -EINVAL; |
1269 | } | |
1270 | ||
62d85b3b IM |
1271 | /* |
1272 | * Workaround for SDL 1.2, which is known to be setting all pixel format | |
1273 | * fields values to zero in some cases. We treat this situation as a | |
1274 | * kind of "use some reasonable autodetected values". | |
1275 | */ | |
1276 | if (!var->red.offset && !var->green.offset && | |
1277 | !var->blue.offset && !var->transp.offset && | |
1278 | !var->red.length && !var->green.length && | |
1279 | !var->blue.length && !var->transp.length && | |
1280 | !var->red.msb_right && !var->green.msb_right && | |
1281 | !var->blue.msb_right && !var->transp.msb_right) { | |
1282 | drm_fb_helper_fill_pixel_fmt(var, fb->format->depth); | |
1283 | } | |
1284 | ||
db05c481 EP |
1285 | /* |
1286 | * drm fbdev emulation doesn't support changing the pixel format at all, | |
1287 | * so reject all pixel format changing requests. | |
1288 | */ | |
1289 | if (!drm_fb_pixel_format_equal(var, &info->var)) { | |
f33b9730 | 1290 | drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n"); |
785b93ef DA |
1291 | return -EINVAL; |
1292 | } | |
db05c481 | 1293 | |
785b93ef DA |
1294 | return 0; |
1295 | } | |
1296 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
1297 | ||
207fd329 | 1298 | /** |
6806cdf9 | 1299 | * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par |
207fd329 DV |
1300 | * @info: fbdev registered by the helper |
1301 | * | |
1302 | * This will let fbcon do the mode init and is called at initialization time by | |
1303 | * the fbdev core when registering the driver, and later on through the hotplug | |
1304 | * callback. | |
1305 | */ | |
785b93ef DA |
1306 | int drm_fb_helper_set_par(struct fb_info *info) |
1307 | { | |
1308 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 1309 | struct fb_var_screeninfo *var = &info->var; |
785b93ef | 1310 | |
c50bfd08 DV |
1311 | if (oops_in_progress) |
1312 | return -EBUSY; | |
1313 | ||
5349ef31 | 1314 | if (var->pixclock != 0) { |
f33b9730 | 1315 | drm_err(fb_helper->dev, "PIXEL CLOCK SET\n"); |
785b93ef DA |
1316 | return -EINVAL; |
1317 | } | |
1318 | ||
5ea1f752 | 1319 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
4abe3520 | 1320 | |
785b93ef DA |
1321 | return 0; |
1322 | } | |
1323 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
1324 | ||
6b7dc6e9 | 1325 | static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) |
1edf0269 | 1326 | { |
d81294af | 1327 | struct drm_mode_set *mode_set; |
1edf0269 | 1328 | |
d81294af NT |
1329 | mutex_lock(&fb_helper->client.modeset_mutex); |
1330 | drm_client_for_each_modeset(mode_set, &fb_helper->client) { | |
6b7dc6e9 DV |
1331 | mode_set->x = x; |
1332 | mode_set->y = y; | |
1edf0269 | 1333 | } |
d81294af | 1334 | mutex_unlock(&fb_helper->client.modeset_mutex); |
6b7dc6e9 | 1335 | } |
1edf0269 | 1336 | |
6b7dc6e9 DV |
1337 | static int pan_display_atomic(struct fb_var_screeninfo *var, |
1338 | struct fb_info *info) | |
1339 | { | |
1340 | struct drm_fb_helper *fb_helper = info->par; | |
1341 | int ret; | |
a0fb6ad7 | 1342 | |
6b7dc6e9 | 1343 | pan_set(fb_helper, var->xoffset, var->yoffset); |
1edf0269 | 1344 | |
eade2a17 | 1345 | ret = drm_client_modeset_commit_force(&fb_helper->client); |
6b7dc6e9 DV |
1346 | if (!ret) { |
1347 | info->var.xoffset = var->xoffset; | |
1348 | info->var.yoffset = var->yoffset; | |
1349 | } else | |
1350 | pan_set(fb_helper, info->var.xoffset, info->var.yoffset); | |
5c2e3448 | 1351 | |
1edf0269 | 1352 | return ret; |
1edf0269 RC |
1353 | } |
1354 | ||
7128645d | 1355 | static int pan_display_legacy(struct fb_var_screeninfo *var, |
785b93ef DA |
1356 | struct fb_info *info) |
1357 | { | |
1358 | struct drm_fb_helper *fb_helper = info->par; | |
d81294af | 1359 | struct drm_client_dev *client = &fb_helper->client; |
785b93ef | 1360 | struct drm_mode_set *modeset; |
785b93ef | 1361 | int ret = 0; |
785b93ef | 1362 | |
d81294af | 1363 | mutex_lock(&client->modeset_mutex); |
1ff30dd8 | 1364 | drm_modeset_lock_all(fb_helper->dev); |
d81294af | 1365 | drm_client_for_each_modeset(modeset, client) { |
785b93ef DA |
1366 | modeset->x = var->xoffset; |
1367 | modeset->y = var->yoffset; | |
1368 | ||
1369 | if (modeset->num_connectors) { | |
2d13b679 | 1370 | ret = drm_mode_set_config_internal(modeset); |
785b93ef DA |
1371 | if (!ret) { |
1372 | info->var.xoffset = var->xoffset; | |
1373 | info->var.yoffset = var->yoffset; | |
1374 | } | |
1375 | } | |
1376 | } | |
5c2e3448 | 1377 | drm_modeset_unlock_all(fb_helper->dev); |
1ff30dd8 | 1378 | mutex_unlock(&client->modeset_mutex); |
7128645d DV |
1379 | |
1380 | return ret; | |
1381 | } | |
1382 | ||
1383 | /** | |
1384 | * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display | |
1385 | * @var: updated screen information | |
1386 | * @info: fbdev registered by the helper | |
1387 | */ | |
1388 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, | |
1389 | struct fb_info *info) | |
1390 | { | |
1391 | struct drm_fb_helper *fb_helper = info->par; | |
1392 | struct drm_device *dev = fb_helper->dev; | |
1393 | int ret; | |
1394 | ||
1395 | if (oops_in_progress) | |
1396 | return -EBUSY; | |
1397 | ||
e9827d8e | 1398 | mutex_lock(&fb_helper->lock); |
03a9606e NT |
1399 | if (!drm_master_internal_acquire(dev)) { |
1400 | ret = -EBUSY; | |
1401 | goto unlock; | |
7128645d DV |
1402 | } |
1403 | ||
1404 | if (drm_drv_uses_atomic_modeset(dev)) | |
1405 | ret = pan_display_atomic(var, info); | |
1406 | else | |
1407 | ret = pan_display_legacy(var, info); | |
03a9606e NT |
1408 | |
1409 | drm_master_internal_release(dev); | |
1410 | unlock: | |
e9827d8e | 1411 | mutex_unlock(&fb_helper->lock); |
7128645d | 1412 | |
785b93ef DA |
1413 | return ret; |
1414 | } | |
1415 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
1416 | ||
8acf658a | 1417 | /* |
207fd329 | 1418 | * Allocates the backing storage and sets up the fbdev info structure through |
ca91a275 | 1419 | * the ->fb_probe callback. |
8acf658a | 1420 | */ |
de1ace5b DV |
1421 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
1422 | int preferred_bpp) | |
785b93ef | 1423 | { |
d81294af | 1424 | struct drm_client_dev *client = &fb_helper->client; |
f33b9730 | 1425 | struct drm_device *dev = fb_helper->dev; |
8acf658a | 1426 | int ret = 0; |
785b93ef | 1427 | int crtc_count = 0; |
e5852bee | 1428 | struct drm_connector_list_iter conn_iter; |
38651674 | 1429 | struct drm_fb_helper_surface_size sizes; |
e5852bee | 1430 | struct drm_connector *connector; |
d81294af | 1431 | struct drm_mode_set *mode_set; |
f4bd542b | 1432 | int best_depth = 0; |
38651674 DA |
1433 | |
1434 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
1435 | sizes.surface_depth = 24; | |
1436 | sizes.surface_bpp = 32; | |
4b4f99f5 TR |
1437 | sizes.fb_width = (u32)-1; |
1438 | sizes.fb_height = (u32)-1; | |
785b93ef | 1439 | |
f4bd542b LW |
1440 | /* |
1441 | * If driver picks 8 or 16 by default use that for both depth/bpp | |
1442 | * to begin with | |
1443 | */ | |
96081cdf | 1444 | if (preferred_bpp != sizes.surface_bpp) |
38651674 | 1445 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
96081cdf | 1446 | |
e5852bee NT |
1447 | drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); |
1448 | drm_client_for_each_connector_iter(connector, &conn_iter) { | |
1794d257 | 1449 | struct drm_cmdline_mode *cmdline_mode; |
8ef8678c | 1450 | |
e5852bee | 1451 | cmdline_mode = &connector->cmdline_mode; |
d50ba256 DA |
1452 | |
1453 | if (cmdline_mode->bpp_specified) { | |
1454 | switch (cmdline_mode->bpp) { | |
1455 | case 8: | |
38651674 | 1456 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
1457 | break; |
1458 | case 15: | |
38651674 DA |
1459 | sizes.surface_depth = 15; |
1460 | sizes.surface_bpp = 16; | |
d50ba256 DA |
1461 | break; |
1462 | case 16: | |
38651674 | 1463 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
1464 | break; |
1465 | case 24: | |
38651674 | 1466 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
1467 | break; |
1468 | case 32: | |
38651674 DA |
1469 | sizes.surface_depth = 24; |
1470 | sizes.surface_bpp = 32; | |
d50ba256 DA |
1471 | break; |
1472 | } | |
1473 | break; | |
1474 | } | |
1475 | } | |
e5852bee | 1476 | drm_connector_list_iter_end(&conn_iter); |
d50ba256 | 1477 | |
f4bd542b LW |
1478 | /* |
1479 | * If we run into a situation where, for example, the primary plane | |
1480 | * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth | |
1481 | * 16) we need to scale down the depth of the sizes we request. | |
1482 | */ | |
d81294af NT |
1483 | mutex_lock(&client->modeset_mutex); |
1484 | drm_client_for_each_modeset(mode_set, client) { | |
f4bd542b LW |
1485 | struct drm_crtc *crtc = mode_set->crtc; |
1486 | struct drm_plane *plane = crtc->primary; | |
1487 | int j; | |
1488 | ||
f33b9730 | 1489 | drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc)); |
f4bd542b LW |
1490 | |
1491 | for (j = 0; j < plane->format_count; j++) { | |
1492 | const struct drm_format_info *fmt; | |
1493 | ||
1494 | fmt = drm_format_info(plane->format_types[j]); | |
1495 | ||
1496 | /* | |
1497 | * Do not consider YUV or other complicated formats | |
1498 | * for framebuffers. This means only legacy formats | |
1499 | * are supported (fmt->depth is a legacy field) but | |
1500 | * the framebuffer emulation can only deal with such | |
1501 | * formats, specifically RGB/BGA formats. | |
1502 | */ | |
1503 | if (fmt->depth == 0) | |
1504 | continue; | |
1505 | ||
1506 | /* We found a perfect fit, great */ | |
1507 | if (fmt->depth == sizes.surface_depth) { | |
1508 | best_depth = fmt->depth; | |
1509 | break; | |
1510 | } | |
1511 | ||
1512 | /* Skip depths above what we're looking for */ | |
1513 | if (fmt->depth > sizes.surface_depth) | |
1514 | continue; | |
1515 | ||
1516 | /* Best depth found so far */ | |
1517 | if (fmt->depth > best_depth) | |
1518 | best_depth = fmt->depth; | |
1519 | } | |
1520 | } | |
b6a36e5d | 1521 | if (sizes.surface_depth != best_depth && best_depth) { |
f33b9730 | 1522 | drm_info(dev, "requested bpp %d, scaled depth down to %d", |
f4bd542b LW |
1523 | sizes.surface_bpp, best_depth); |
1524 | sizes.surface_depth = best_depth; | |
1525 | } | |
1526 | ||
b9ed8cff | 1527 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
8be48d92 | 1528 | crtc_count = 0; |
d81294af | 1529 | drm_client_for_each_modeset(mode_set, client) { |
8be48d92 | 1530 | struct drm_display_mode *desired_mode; |
0e3704c9 RC |
1531 | int x, y, j; |
1532 | /* in case of tile group, are we the last tile vert or horiz? | |
1533 | * If no tile group you are always the last one both vertically | |
1534 | * and horizontally | |
1535 | */ | |
1536 | bool lastv = true, lasth = true; | |
675c8328 | 1537 | |
c8de0d5f | 1538 | desired_mode = mode_set->mode; |
675c8328 RC |
1539 | |
1540 | if (!desired_mode) | |
1541 | continue; | |
1542 | ||
1543 | crtc_count++; | |
1544 | ||
c8de0d5f NT |
1545 | x = mode_set->x; |
1546 | y = mode_set->y; | |
675c8328 | 1547 | |
675c8328 RC |
1548 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); |
1549 | sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); | |
0e3704c9 RC |
1550 | |
1551 | for (j = 0; j < mode_set->num_connectors; j++) { | |
1552 | struct drm_connector *connector = mode_set->connectors[j]; | |
4b4f99f5 | 1553 | |
0e3704c9 RC |
1554 | if (connector->has_tile) { |
1555 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); | |
1556 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); | |
1557 | /* cloning to multiple tiles is just crazy-talk, so: */ | |
1558 | break; | |
1559 | } | |
1560 | } | |
1561 | ||
1562 | if (lasth) | |
1563 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); | |
1564 | if (lastv) | |
1565 | sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); | |
785b93ef | 1566 | } |
d81294af | 1567 | mutex_unlock(&client->modeset_mutex); |
785b93ef | 1568 | |
38651674 | 1569 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
f33b9730 | 1570 | drm_info(dev, "Cannot find any crtc or sizes\n"); |
52dd0650 ML |
1571 | |
1572 | /* First time: disable all crtc's.. */ | |
03a9606e | 1573 | if (!fb_helper->deferred_setup) |
eade2a17 | 1574 | drm_client_modeset_commit(client); |
ca91a275 | 1575 | return -EAGAIN; |
785b93ef DA |
1576 | } |
1577 | ||
5f152576 XL |
1578 | /* Handle our overallocation */ |
1579 | sizes.surface_height *= drm_fbdev_overalloc; | |
1580 | sizes.surface_height /= 100; | |
1581 | ||
38651674 | 1582 | /* push down into drivers */ |
8acf658a DV |
1583 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
1584 | if (ret < 0) | |
1585 | return ret; | |
785b93ef | 1586 | |
8d44e9e6 | 1587 | strcpy(fb_helper->fb->comm, "[fbcon]"); |
785b93ef DA |
1588 | return 0; |
1589 | } | |
785b93ef | 1590 | |
168b85e9 DV |
1591 | static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
1592 | uint32_t depth) | |
3632ef89 DA |
1593 | { |
1594 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
1595 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : | |
1596 | FB_VISUAL_TRUECOLOR; | |
1597 | info->fix.mmio_start = 0; | |
1598 | info->fix.mmio_len = 0; | |
1599 | info->fix.type_aux = 0; | |
1600 | info->fix.xpanstep = 1; /* doing it in hw */ | |
1601 | info->fix.ypanstep = 1; /* doing it in hw */ | |
1602 | info->fix.ywrapstep = 0; | |
1603 | info->fix.accel = FB_ACCEL_NONE; | |
3632ef89 DA |
1604 | |
1605 | info->fix.line_length = pitch; | |
3632ef89 | 1606 | } |
3632ef89 | 1607 | |
168b85e9 DV |
1608 | static void drm_fb_helper_fill_var(struct fb_info *info, |
1609 | struct drm_fb_helper *fb_helper, | |
1610 | uint32_t fb_width, uint32_t fb_height) | |
785b93ef | 1611 | { |
38651674 | 1612 | struct drm_framebuffer *fb = fb_helper->fb; |
4b4f99f5 | 1613 | |
042bf753 AG |
1614 | WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) || |
1615 | (drm_format_info_block_height(fb->format, 0) > 1)); | |
38651674 | 1616 | info->pseudo_palette = fb_helper->pseudo_palette; |
785b93ef DA |
1617 | info->var.xres_virtual = fb->width; |
1618 | info->var.yres_virtual = fb->height; | |
272725c7 | 1619 | info->var.bits_per_pixel = fb->format->cpp[0] * 8; |
57084d05 | 1620 | info->var.accel_flags = FB_ACCELF_TEXT; |
785b93ef DA |
1621 | info->var.xoffset = 0; |
1622 | info->var.yoffset = 0; | |
1623 | info->var.activate = FB_ACTIVATE_NOW; | |
785b93ef | 1624 | |
62d85b3b | 1625 | drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth); |
785b93ef DA |
1626 | |
1627 | info->var.xres = fb_width; | |
1628 | info->var.yres = fb_height; | |
1629 | } | |
38651674 | 1630 | |
3df3116a DV |
1631 | /** |
1632 | * drm_fb_helper_fill_info - initializes fbdev information | |
1633 | * @info: fbdev instance to set up | |
1634 | * @fb_helper: fb helper instance to use as template | |
1635 | * @sizes: describes fbdev size and scanout surface size | |
1636 | * | |
1637 | * Sets up the variable and fixed fbdev metainformation from the given fb helper | |
1638 | * instance and the drm framebuffer allocated in &drm_fb_helper.fb. | |
1639 | * | |
1640 | * Drivers should call this (or their equivalent setup code) from their | |
1641 | * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev | |
1642 | * backing storage framebuffer. | |
1643 | */ | |
1644 | void drm_fb_helper_fill_info(struct fb_info *info, | |
1645 | struct drm_fb_helper *fb_helper, | |
1646 | struct drm_fb_helper_surface_size *sizes) | |
1647 | { | |
1648 | struct drm_framebuffer *fb = fb_helper->fb; | |
1649 | ||
1650 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth); | |
1651 | drm_fb_helper_fill_var(info, fb_helper, | |
1652 | sizes->fb_width, sizes->fb_height); | |
1653 | ||
ecbfc474 | 1654 | info->par = fb_helper; |
f1eca684 DV |
1655 | snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb", |
1656 | fb_helper->dev->driver->name); | |
1657 | ||
3df3116a DV |
1658 | } |
1659 | EXPORT_SYMBOL(drm_fb_helper_fill_info); | |
1660 | ||
f461bd2b DL |
1661 | /* |
1662 | * This is a continuation of drm_setup_crtcs() that sets up anything related | |
1663 | * to the framebuffer. During initialization, drm_setup_crtcs() is called before | |
1664 | * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). | |
1665 | * So, any setup that touches those fields needs to be done here instead of in | |
1666 | * drm_setup_crtcs(). | |
1667 | */ | |
1668 | static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) | |
1669 | { | |
d81294af | 1670 | struct drm_client_dev *client = &fb_helper->client; |
e5852bee | 1671 | struct drm_connector_list_iter conn_iter; |
991a3999 | 1672 | struct fb_info *info = fb_helper->fbdev; |
bc934755 | 1673 | unsigned int rotation, sw_rotations = 0; |
e5852bee | 1674 | struct drm_connector *connector; |
d81294af | 1675 | struct drm_mode_set *modeset; |
f461bd2b | 1676 | |
d81294af NT |
1677 | mutex_lock(&client->modeset_mutex); |
1678 | drm_client_for_each_modeset(modeset, client) { | |
bc934755 NT |
1679 | if (!modeset->num_connectors) |
1680 | continue; | |
1681 | ||
1682 | modeset->fb = fb_helper->fb; | |
1683 | ||
a99076e8 | 1684 | if (drm_client_rotation(modeset, &rotation)) |
bc934755 NT |
1685 | /* Rotating in hardware, fbcon should not rotate */ |
1686 | sw_rotations |= DRM_MODE_ROTATE_0; | |
1687 | else | |
1688 | sw_rotations |= rotation; | |
1689 | } | |
d81294af | 1690 | mutex_unlock(&client->modeset_mutex); |
991a3999 | 1691 | |
e5852bee NT |
1692 | drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); |
1693 | drm_client_for_each_connector_iter(connector, &conn_iter) { | |
991a3999 DL |
1694 | |
1695 | /* use first connected connector for the physical dimensions */ | |
1696 | if (connector->status == connector_status_connected) { | |
1697 | info->var.width = connector->display_info.width_mm; | |
1698 | info->var.height = connector->display_info.height_mm; | |
1699 | break; | |
1700 | } | |
1701 | } | |
e5852bee | 1702 | drm_connector_list_iter_end(&conn_iter); |
8f0cb418 | 1703 | |
bc934755 | 1704 | switch (sw_rotations) { |
8f0cb418 HG |
1705 | case DRM_MODE_ROTATE_0: |
1706 | info->fbcon_rotate_hint = FB_ROTATE_UR; | |
1707 | break; | |
1708 | case DRM_MODE_ROTATE_90: | |
1709 | info->fbcon_rotate_hint = FB_ROTATE_CCW; | |
1710 | break; | |
1711 | case DRM_MODE_ROTATE_180: | |
1712 | info->fbcon_rotate_hint = FB_ROTATE_UD; | |
1713 | break; | |
1714 | case DRM_MODE_ROTATE_270: | |
1715 | info->fbcon_rotate_hint = FB_ROTATE_CW; | |
1716 | break; | |
1717 | default: | |
1718 | /* | |
1719 | * Multiple bits are set / multiple rotations requested | |
1720 | * fbcon cannot handle separate rotation settings per | |
1721 | * output, so fallback to unrotated. | |
1722 | */ | |
1723 | info->fbcon_rotate_hint = FB_ROTATE_UR; | |
1724 | } | |
f461bd2b DL |
1725 | } |
1726 | ||
ca91a275 DV |
1727 | /* Note: Drops fb_helper->lock before returning. */ |
1728 | static int | |
1729 | __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, | |
1730 | int bpp_sel) | |
1731 | { | |
1732 | struct drm_device *dev = fb_helper->dev; | |
1733 | struct fb_info *info; | |
1734 | unsigned int width, height; | |
1735 | int ret; | |
1736 | ||
1737 | width = dev->mode_config.max_width; | |
1738 | height = dev->mode_config.max_height; | |
1739 | ||
aafa9e06 | 1740 | drm_client_modeset_probe(&fb_helper->client, width, height); |
ca91a275 DV |
1741 | ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
1742 | if (ret < 0) { | |
1743 | if (ret == -EAGAIN) { | |
1744 | fb_helper->preferred_bpp = bpp_sel; | |
1745 | fb_helper->deferred_setup = true; | |
1746 | ret = 0; | |
1747 | } | |
1748 | mutex_unlock(&fb_helper->lock); | |
1749 | ||
1750 | return ret; | |
1751 | } | |
f461bd2b | 1752 | drm_setup_crtcs_fb(fb_helper); |
ca91a275 DV |
1753 | |
1754 | fb_helper->deferred_setup = false; | |
1755 | ||
1756 | info = fb_helper->fbdev; | |
1757 | info->var.pixclock = 0; | |
4be9bd10 NA |
1758 | /* Shamelessly allow physical address leaking to userspace */ |
1759 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
1760 | if (!drm_leak_fbdev_smem) | |
1761 | #endif | |
1762 | /* don't leak any physical addresses to userspace */ | |
1763 | info->flags |= FBINFO_HIDE_SMEM_START; | |
ca91a275 DV |
1764 | |
1765 | /* Need to drop locks to avoid recursive deadlock in | |
1766 | * register_framebuffer. This is ok because the only thing left to do is | |
1767 | * register the fbdev emulation instance in kernel_fb_helper_list. */ | |
1768 | mutex_unlock(&fb_helper->lock); | |
1769 | ||
1770 | ret = register_framebuffer(info); | |
1771 | if (ret < 0) | |
1772 | return ret; | |
1773 | ||
1774 | dev_info(dev->dev, "fb%d: %s frame buffer device\n", | |
1775 | info->node, info->fix.id); | |
1776 | ||
1777 | mutex_lock(&kernel_fb_helper_lock); | |
1778 | if (list_empty(&kernel_fb_helper_list)) | |
1779 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
1780 | ||
1781 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
1782 | mutex_unlock(&kernel_fb_helper_lock); | |
1783 | ||
1784 | return 0; | |
1785 | } | |
1786 | ||
38651674 | 1787 | /** |
207fd329 | 1788 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
d0ddc033 DV |
1789 | * @fb_helper: fb_helper device struct |
1790 | * @bpp_sel: bpp value to use for the framebuffer configuration | |
38651674 | 1791 | * |
d0ddc033 | 1792 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
38651674 DA |
1793 | * At the moment, this is a cloned configuration across all heads with |
1794 | * a new framebuffer object as the backing store. | |
1795 | * | |
207fd329 DV |
1796 | * Note that this also registers the fbdev and so allows userspace to call into |
1797 | * the driver through the fbdev interfaces. | |
1798 | * | |
6806cdf9 DV |
1799 | * This function will call down into the &drm_fb_helper_funcs.fb_probe callback |
1800 | * to let the driver allocate and initialize the fbdev info structure and the | |
ec8bf194 DV |
1801 | * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided |
1802 | * as a helper to setup simple default values for the fbdev info structure. | |
207fd329 | 1803 | * |
40f8cf4b DV |
1804 | * HANG DEBUGGING: |
1805 | * | |
1806 | * When you have fbcon support built-in or already loaded, this function will do | |
1807 | * a full modeset to setup the fbdev console. Due to locking misdesign in the | |
1808 | * VT/fbdev subsystem that entire modeset sequence has to be done while holding | |
1809 | * console_lock. Until console_unlock is called no dmesg lines will be sent out | |
1810 | * to consoles, not even serial console. This means when your driver crashes, | |
1811 | * you will see absolutely nothing else but a system stuck in this function, | |
1812 | * with no further output. Any kind of printk() you place within your own driver | |
1813 | * or in the drm core modeset code will also never show up. | |
1814 | * | |
1815 | * Standard debug practice is to run the fbcon setup without taking the | |
1816 | * console_lock as a hack, to be able to see backtraces and crashes on the | |
1817 | * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel | |
1818 | * cmdline option. | |
1819 | * | |
1820 | * The other option is to just disable fbdev emulation since very likely the | |
af509d38 L |
1821 | * first modeset from userspace will crash in the same way, and is even easier |
1822 | * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 | |
40f8cf4b DV |
1823 | * kernel cmdline option. |
1824 | * | |
38651674 DA |
1825 | * RETURNS: |
1826 | * Zero if everything went ok, nonzero otherwise. | |
1827 | */ | |
01934c2a | 1828 | int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
38651674 | 1829 | { |
966a6a13 | 1830 | int ret; |
38651674 | 1831 | |
f64c5573 DV |
1832 | if (!drm_fbdev_emulation) |
1833 | return 0; | |
1834 | ||
e9827d8e | 1835 | mutex_lock(&fb_helper->lock); |
ca91a275 | 1836 | ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); |
38651674 | 1837 | |
ca91a275 | 1838 | return ret; |
38651674 | 1839 | } |
8be48d92 | 1840 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 1841 | |
7394371d CW |
1842 | /** |
1843 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by | |
d0ddc033 | 1844 | * probing all the outputs attached to the fb |
c777990f | 1845 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
7394371d | 1846 | * |
7394371d | 1847 | * Scan the connectors attached to the fb_helper and try to put together a |
62cacc79 | 1848 | * setup after notification of a change in output configuration. |
7394371d | 1849 | * |
207fd329 DV |
1850 | * Called at runtime, takes the mode config locks to be able to check/change the |
1851 | * modeset configuration. Must be run from process context (which usually means | |
1852 | * either the output polling work or a work item launched from the driver's | |
1853 | * hotplug interrupt). | |
1854 | * | |
50c3dc97 | 1855 | * Note that drivers may call this even before calling |
af509d38 | 1856 | * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows |
50c3dc97 DV |
1857 | * for a race-free fbcon setup and will make sure that the fbdev emulation will |
1858 | * not miss any hotplug events. | |
207fd329 | 1859 | * |
7394371d CW |
1860 | * RETURNS: |
1861 | * 0 on success and a non-zero error code otherwise. | |
1862 | */ | |
1863 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) | |
38651674 | 1864 | { |
e9827d8e | 1865 | int err = 0; |
5c4426a7 | 1866 | |
c777990f | 1867 | if (!drm_fbdev_emulation || !fb_helper) |
f64c5573 DV |
1868 | return 0; |
1869 | ||
e9827d8e | 1870 | mutex_lock(&fb_helper->lock); |
ca91a275 DV |
1871 | if (fb_helper->deferred_setup) { |
1872 | err = __drm_fb_helper_initial_config_and_unlock(fb_helper, | |
1873 | fb_helper->preferred_bpp); | |
1874 | return err; | |
1875 | } | |
1876 | ||
05865769 | 1877 | if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) { |
4abe3520 | 1878 | fb_helper->delayed_hotplug = true; |
bdac4a05 DV |
1879 | mutex_unlock(&fb_helper->lock); |
1880 | return err; | |
4abe3520 | 1881 | } |
e9827d8e | 1882 | |
05865769 | 1883 | drm_master_internal_release(fb_helper->dev); |
03a9606e | 1884 | |
f33b9730 | 1885 | drm_dbg_kms(fb_helper->dev, "\n"); |
4abe3520 | 1886 | |
aafa9e06 | 1887 | drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height); |
f461bd2b | 1888 | drm_setup_crtcs_fb(fb_helper); |
e9827d8e | 1889 | mutex_unlock(&fb_helper->lock); |
89ced125 | 1890 | |
2180c3c7 DV |
1891 | drm_fb_helper_set_par(fb_helper->fbdev); |
1892 | ||
1893 | return 0; | |
5c4426a7 | 1894 | } |
eb1f8e4f | 1895 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
5c4426a7 | 1896 | |
304a4f6a NT |
1897 | /** |
1898 | * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation | |
1899 | * @dev: DRM device | |
1900 | * | |
1901 | * This function can be used as the &drm_driver->lastclose callback for drivers | |
1902 | * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). | |
1903 | */ | |
1904 | void drm_fb_helper_lastclose(struct drm_device *dev) | |
1905 | { | |
1906 | drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); | |
1907 | } | |
1908 | EXPORT_SYMBOL(drm_fb_helper_lastclose); | |
1909 | ||
1910 | /** | |
1911 | * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed | |
1912 | * helper for fbdev emulation | |
1913 | * @dev: DRM device | |
1914 | * | |
1915 | * This function can be used as the | |
1916 | * &drm_mode_config_funcs.output_poll_changed callback for drivers that only | |
1917 | * need to call drm_fb_helper_hotplug_event(). | |
1918 | */ | |
1919 | void drm_fb_helper_output_poll_changed(struct drm_device *dev) | |
1920 | { | |
1921 | drm_fb_helper_hotplug_event(dev->fb_helper); | |
1922 | } | |
1923 | EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); | |
1924 | ||
d536540f NT |
1925 | /* @user: 1=userspace, 0=fbcon */ |
1926 | static int drm_fbdev_fb_open(struct fb_info *info, int user) | |
1927 | { | |
1928 | struct drm_fb_helper *fb_helper = info->par; | |
1929 | ||
6ab20a05 NT |
1930 | /* No need to take a ref for fbcon because it unbinds on unregister */ |
1931 | if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) | |
d536540f NT |
1932 | return -ENODEV; |
1933 | ||
1934 | return 0; | |
1935 | } | |
1936 | ||
1937 | static int drm_fbdev_fb_release(struct fb_info *info, int user) | |
1938 | { | |
1939 | struct drm_fb_helper *fb_helper = info->par; | |
1940 | ||
6ab20a05 NT |
1941 | if (user) |
1942 | module_put(fb_helper->dev->driver->fops->owner); | |
d536540f NT |
1943 | |
1944 | return 0; | |
1945 | } | |
1946 | ||
6e1490cf | 1947 | static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper) |
d536540f | 1948 | { |
d536540f | 1949 | struct fb_info *fbi = fb_helper->fbdev; |
d536540f NT |
1950 | void *shadow = NULL; |
1951 | ||
6e1490cf NT |
1952 | if (!fb_helper->dev) |
1953 | return; | |
1954 | ||
1955 | if (fbi && fbi->fbdefio) { | |
d536540f NT |
1956 | fb_deferred_io_cleanup(fbi); |
1957 | shadow = fbi->screen_buffer; | |
d536540f NT |
1958 | } |
1959 | ||
1960 | drm_fb_helper_fini(fb_helper); | |
1961 | ||
57778075 | 1962 | vfree(shadow); |
d536540f NT |
1963 | |
1964 | drm_client_framebuffer_delete(fb_helper->buffer); | |
6e1490cf NT |
1965 | } |
1966 | ||
1967 | static void drm_fbdev_release(struct drm_fb_helper *fb_helper) | |
1968 | { | |
1969 | drm_fbdev_cleanup(fb_helper); | |
2de304b4 NT |
1970 | drm_client_release(&fb_helper->client); |
1971 | kfree(fb_helper); | |
d536540f NT |
1972 | } |
1973 | ||
6e1490cf NT |
1974 | /* |
1975 | * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of | |
1976 | * unregister_framebuffer() or fb_release(). | |
1977 | */ | |
1978 | static void drm_fbdev_fb_destroy(struct fb_info *info) | |
1979 | { | |
1980 | drm_fbdev_release(info->par); | |
1981 | } | |
1982 | ||
d536540f NT |
1983 | static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) |
1984 | { | |
1985 | struct drm_fb_helper *fb_helper = info->par; | |
1986 | ||
1987 | if (fb_helper->dev->driver->gem_prime_mmap) | |
1988 | return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); | |
1989 | else | |
1990 | return -ENODEV; | |
1991 | } | |
1992 | ||
b6ff753a | 1993 | static const struct fb_ops drm_fbdev_fb_ops = { |
d536540f NT |
1994 | .owner = THIS_MODULE, |
1995 | DRM_FB_HELPER_DEFAULT_OPS, | |
1996 | .fb_open = drm_fbdev_fb_open, | |
1997 | .fb_release = drm_fbdev_fb_release, | |
1998 | .fb_destroy = drm_fbdev_fb_destroy, | |
1999 | .fb_mmap = drm_fbdev_fb_mmap, | |
2000 | .fb_read = drm_fb_helper_sys_read, | |
2001 | .fb_write = drm_fb_helper_sys_write, | |
2002 | .fb_fillrect = drm_fb_helper_sys_fillrect, | |
2003 | .fb_copyarea = drm_fb_helper_sys_copyarea, | |
2004 | .fb_imageblit = drm_fb_helper_sys_imageblit, | |
2005 | }; | |
2006 | ||
2007 | static struct fb_deferred_io drm_fbdev_defio = { | |
2008 | .delay = HZ / 20, | |
2009 | .deferred_io = drm_fb_helper_deferred_io, | |
2010 | }; | |
2011 | ||
47734835 | 2012 | /* |
c10802b6 | 2013 | * This function uses the client API to create a framebuffer backed by a dumb buffer. |
d536540f NT |
2014 | * |
2015 | * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, | |
2016 | * fb_copyarea, fb_imageblit. | |
d536540f | 2017 | */ |
47734835 DV |
2018 | static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, |
2019 | struct drm_fb_helper_surface_size *sizes) | |
d536540f NT |
2020 | { |
2021 | struct drm_client_dev *client = &fb_helper->client; | |
f33b9730 | 2022 | struct drm_device *dev = fb_helper->dev; |
d536540f NT |
2023 | struct drm_client_buffer *buffer; |
2024 | struct drm_framebuffer *fb; | |
2025 | struct fb_info *fbi; | |
2026 | u32 format; | |
cf1ca9ae | 2027 | void *vaddr; |
d536540f | 2028 | |
f33b9730 JN |
2029 | drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", |
2030 | sizes->surface_width, sizes->surface_height, | |
2031 | sizes->surface_bpp); | |
d536540f NT |
2032 | |
2033 | format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); | |
2034 | buffer = drm_client_framebuffer_create(client, sizes->surface_width, | |
2035 | sizes->surface_height, format); | |
2036 | if (IS_ERR(buffer)) | |
2037 | return PTR_ERR(buffer); | |
2038 | ||
2039 | fb_helper->buffer = buffer; | |
2040 | fb_helper->fb = buffer->fb; | |
2041 | fb = buffer->fb; | |
2042 | ||
2043 | fbi = drm_fb_helper_alloc_fbi(fb_helper); | |
6e1490cf NT |
2044 | if (IS_ERR(fbi)) |
2045 | return PTR_ERR(fbi); | |
d536540f | 2046 | |
d536540f NT |
2047 | fbi->fbops = &drm_fbdev_fb_ops; |
2048 | fbi->screen_size = fb->height * fb->pitches[0]; | |
2049 | fbi->fix.smem_len = fbi->screen_size; | |
cf1ca9ae | 2050 | |
3df3116a | 2051 | drm_fb_helper_fill_info(fbi, fb_helper, sizes); |
d536540f | 2052 | |
f458579e | 2053 | if (drm_fbdev_use_shadow_fb(fb_helper)) { |
57778075 JN |
2054 | fbi->screen_buffer = vzalloc(fbi->screen_size); |
2055 | if (!fbi->screen_buffer) | |
6e1490cf | 2056 | return -ENOMEM; |
d536540f | 2057 | |
d536540f NT |
2058 | fbi->fbdefio = &drm_fbdev_defio; |
2059 | ||
2060 | fb_deferred_io_init(fbi); | |
cf1ca9ae TZ |
2061 | } else { |
2062 | /* buffer is mapped for HW framebuffer */ | |
2063 | vaddr = drm_client_buffer_vmap(fb_helper->buffer); | |
2064 | if (IS_ERR(vaddr)) | |
2065 | return PTR_ERR(vaddr); | |
2066 | ||
2067 | fbi->screen_buffer = vaddr; | |
2068 | /* Shamelessly leak the physical address to user-space */ | |
2069 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
2070 | if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) | |
2071 | fbi->fix.smem_start = | |
2072 | page_to_phys(virt_to_page(fbi->screen_buffer)); | |
2073 | #endif | |
d536540f NT |
2074 | } |
2075 | ||
2076 | return 0; | |
d536540f | 2077 | } |
d536540f | 2078 | |
9060d7f4 NT |
2079 | static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { |
2080 | .fb_probe = drm_fb_helper_generic_probe, | |
2081 | }; | |
2082 | ||
2083 | static void drm_fbdev_client_unregister(struct drm_client_dev *client) | |
2084 | { | |
2085 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); | |
2086 | ||
6e1490cf | 2087 | if (fb_helper->fbdev) |
9060d7f4 | 2088 | /* drm_fbdev_fb_destroy() takes care of cleanup */ |
6e1490cf NT |
2089 | drm_fb_helper_unregister_fbi(fb_helper); |
2090 | else | |
2091 | drm_fbdev_release(fb_helper); | |
9060d7f4 NT |
2092 | } |
2093 | ||
2094 | static int drm_fbdev_client_restore(struct drm_client_dev *client) | |
2095 | { | |
78de14c2 | 2096 | drm_fb_helper_lastclose(client->dev); |
9060d7f4 NT |
2097 | |
2098 | return 0; | |
2099 | } | |
2100 | ||
2101 | static int drm_fbdev_client_hotplug(struct drm_client_dev *client) | |
2102 | { | |
2103 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); | |
2104 | struct drm_device *dev = client->dev; | |
2105 | int ret; | |
2106 | ||
6e1490cf | 2107 | /* Setup is not retried if it has failed */ |
9060d7f4 NT |
2108 | if (!fb_helper->dev && fb_helper->funcs) |
2109 | return 0; | |
2110 | ||
2111 | if (dev->fb_helper) | |
2112 | return drm_fb_helper_hotplug_event(dev->fb_helper); | |
2113 | ||
c10802b6 | 2114 | if (!dev->mode_config.num_connector) { |
f33b9730 | 2115 | drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n"); |
9060d7f4 | 2116 | return 0; |
c10802b6 | 2117 | } |
9060d7f4 | 2118 | |
6e1490cf NT |
2119 | drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs); |
2120 | ||
d81294af | 2121 | ret = drm_fb_helper_init(dev, fb_helper, 0); |
6e1490cf NT |
2122 | if (ret) |
2123 | goto err; | |
2124 | ||
6e1490cf NT |
2125 | if (!drm_drv_uses_atomic_modeset(dev)) |
2126 | drm_helper_disable_unused_functions(dev); | |
2127 | ||
2128 | ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp); | |
2129 | if (ret) | |
2130 | goto err_cleanup; | |
9060d7f4 NT |
2131 | |
2132 | return 0; | |
6e1490cf NT |
2133 | |
2134 | err_cleanup: | |
2135 | drm_fbdev_cleanup(fb_helper); | |
2136 | err: | |
2137 | fb_helper->dev = NULL; | |
2138 | fb_helper->fbdev = NULL; | |
2139 | ||
f33b9730 | 2140 | drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); |
6e1490cf NT |
2141 | |
2142 | return ret; | |
9060d7f4 NT |
2143 | } |
2144 | ||
2145 | static const struct drm_client_funcs drm_fbdev_client_funcs = { | |
2146 | .owner = THIS_MODULE, | |
2147 | .unregister = drm_fbdev_client_unregister, | |
2148 | .restore = drm_fbdev_client_restore, | |
2149 | .hotplug = drm_fbdev_client_hotplug, | |
2150 | }; | |
2151 | ||
2152 | /** | |
c10802b6 | 2153 | * drm_fbdev_generic_setup() - Setup generic fbdev emulation |
9060d7f4 NT |
2154 | * @dev: DRM device |
2155 | * @preferred_bpp: Preferred bits per pixel for the device. | |
2156 | * @dev->mode_config.preferred_depth is used if this is zero. | |
2157 | * | |
2158 | * This function sets up generic fbdev emulation for drivers that supports | |
8204f235 | 2159 | * dumb buffers with a virtual address and that can be mmap'ed. |
9060d7f4 NT |
2160 | * |
2161 | * Restore, hotplug events and teardown are all taken care of. Drivers that do | |
2162 | * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. | |
2163 | * Simple drivers might use drm_mode_config_helper_suspend(). | |
2164 | * | |
2165 | * Drivers that set the dirty callback on their framebuffer will get a shadow | |
2166 | * fbdev buffer that is blitted onto the real buffer. This is done in order to | |
8e86dee0 TZ |
2167 | * make deferred I/O work with all kinds of buffers. A shadow buffer can be |
2168 | * requested explicitly by setting struct drm_mode_config.prefer_shadow or | |
2169 | * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is | |
2170 | * required to use generic fbdev emulation with SHMEM helpers. | |
9060d7f4 NT |
2171 | * |
2172 | * This function is safe to call even when there are no connectors present. | |
2173 | * Setup will be retried on the next hotplug event. | |
2174 | * | |
c10802b6 PW |
2175 | * The fbdev is destroyed by drm_dev_unregister(). |
2176 | * | |
9060d7f4 NT |
2177 | * Returns: |
2178 | * Zero on success or negative error code on failure. | |
2179 | */ | |
2180 | int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) | |
2181 | { | |
2182 | struct drm_fb_helper *fb_helper; | |
2183 | int ret; | |
2184 | ||
c10802b6 PW |
2185 | WARN(dev->fb_helper, "fb_helper is already set!\n"); |
2186 | ||
9060d7f4 NT |
2187 | if (!drm_fbdev_emulation) |
2188 | return 0; | |
2189 | ||
2190 | fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); | |
2191 | if (!fb_helper) | |
2192 | return -ENOMEM; | |
2193 | ||
4d4c2d89 | 2194 | ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); |
9060d7f4 NT |
2195 | if (ret) { |
2196 | kfree(fb_helper); | |
f33b9730 | 2197 | drm_err(dev, "Failed to register client: %d\n", ret); |
9060d7f4 NT |
2198 | return ret; |
2199 | } | |
2200 | ||
6e1490cf NT |
2201 | if (!preferred_bpp) |
2202 | preferred_bpp = dev->mode_config.preferred_depth; | |
2203 | if (!preferred_bpp) | |
2204 | preferred_bpp = 32; | |
9060d7f4 NT |
2205 | fb_helper->preferred_bpp = preferred_bpp; |
2206 | ||
c10802b6 PW |
2207 | ret = drm_fbdev_client_hotplug(&fb_helper->client); |
2208 | if (ret) | |
f33b9730 | 2209 | drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); |
9060d7f4 | 2210 | |
e33898a2 | 2211 | drm_client_register(&fb_helper->client); |
6e3f17ee | 2212 | |
9060d7f4 NT |
2213 | return 0; |
2214 | } | |
2215 | EXPORT_SYMBOL(drm_fbdev_generic_setup); | |
2216 | ||
6a108a14 | 2217 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
3ce05168 DF |
2218 | * but the module doesn't depend on any fb console symbols. At least |
2219 | * attempt to load fbcon to avoid leaving the system without a usable console. | |
2220 | */ | |
70412cfa | 2221 | int __init drm_fb_helper_modinit(void) |
3ce05168 | 2222 | { |
70412cfa | 2223 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
06324664 | 2224 | const char name[] = "fbcon"; |
3ce05168 DF |
2225 | struct module *fbcon; |
2226 | ||
2227 | mutex_lock(&module_mutex); | |
2228 | fbcon = find_module(name); | |
2229 | mutex_unlock(&module_mutex); | |
2230 | ||
2231 | if (!fbcon) | |
2232 | request_module_nowait(name); | |
70412cfa | 2233 | #endif |
3ce05168 DF |
2234 | return 0; |
2235 | } | |
70412cfa | 2236 | EXPORT_SYMBOL(drm_fb_helper_modinit); |