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> |
785b93ef | 35 | #include <linux/sysrq.h> |
5a0e3ad6 | 36 | #include <linux/slab.h> |
e0cd3608 | 37 | #include <linux/module.h> |
760285e7 DH |
38 | #include <drm/drmP.h> |
39 | #include <drm/drm_crtc.h> | |
40 | #include <drm/drm_fb_helper.h> | |
41 | #include <drm/drm_crtc_helper.h> | |
bbb1e524 RC |
42 | #include <drm/drm_atomic.h> |
43 | #include <drm/drm_atomic_helper.h> | |
785b93ef | 44 | |
8f0cb418 | 45 | #include "drm_crtc_internal.h" |
699fbeea VS |
46 | #include "drm_crtc_helper_internal.h" |
47 | ||
f64c5573 DV |
48 | static bool drm_fbdev_emulation = true; |
49 | module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); | |
50 | MODULE_PARM_DESC(fbdev_emulation, | |
51 | "Enable legacy fbdev emulation [default=true]"); | |
52 | ||
5f152576 XL |
53 | static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; |
54 | module_param(drm_fbdev_overalloc, int, 0444); | |
55 | MODULE_PARM_DESC(drm_fbdev_overalloc, | |
56 | "Overallocation of the fbdev buffer (%) [default=" | |
57 | __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); | |
58 | ||
4be9bd10 NA |
59 | /* |
60 | * In order to keep user-space compatibility, we want in certain use-cases | |
61 | * to keep leaking the fbdev physical address to the user-space program | |
62 | * handling the fbdev buffer. | |
63 | * This is a bad habit essentially kept into closed source opengl driver | |
64 | * that should really be moved into open-source upstream projects instead | |
65 | * of using legacy physical addresses in user space to communicate with | |
66 | * other out-of-tree kernel modules. | |
67 | * | |
68 | * This module_param *should* be removed as soon as possible and be | |
69 | * considered as a broken and legacy behaviour from a modern fbdev device. | |
70 | */ | |
71 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
72 | static bool drm_leak_fbdev_smem = false; | |
73 | module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); | |
74 | MODULE_PARM_DESC(fbdev_emulation, | |
75 | "Allow unsafe leaking fbdev physical smem address [default=false]"); | |
76 | #endif | |
77 | ||
785b93ef | 78 | static LIST_HEAD(kernel_fb_helper_list); |
a53ca635 | 79 | static DEFINE_MUTEX(kernel_fb_helper_lock); |
785b93ef | 80 | |
d0ddc033 DV |
81 | /** |
82 | * DOC: fbdev helpers | |
83 | * | |
84 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel | |
83c617c5 | 85 | * mode setting driver. They can be used mostly independently from the crtc |
d0ddc033 DV |
86 | * helper functions used by many drivers to implement the kernel mode setting |
87 | * interfaces. | |
207fd329 | 88 | * |
9060d7f4 NT |
89 | * Drivers that support a dumb buffer with a virtual address and mmap support, |
90 | * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). | |
91 | * | |
95b0137f NT |
92 | * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it |
93 | * down by calling drm_fb_helper_fbdev_teardown(). | |
207fd329 | 94 | * |
95b0137f NT |
95 | * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use |
96 | * the setup helper and will need to do the whole four-step setup process with | |
97 | * drm_fb_helper_prepare(), drm_fb_helper_init(), | |
98 | * drm_fb_helper_single_add_all_connectors(), enable hotplugging and | |
99 | * drm_fb_helper_initial_config() to avoid a possible race window. | |
100 | * | |
101 | * At runtime drivers should restore the fbdev console by using | |
102 | * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. | |
103 | * They should also notify the fb helper code from updates to the output | |
104 | * configuration by using drm_fb_helper_output_poll_changed() as their | |
105 | * &drm_mode_config_funcs.output_poll_changed callback. | |
106 | * | |
107 | * For suspend/resume consider using drm_mode_config_helper_suspend() and | |
108 | * drm_mode_config_helper_resume() which takes care of fbdev as well. | |
207fd329 DV |
109 | * |
110 | * All other functions exported by the fb helper library can be used to | |
111 | * implement the fbdev driver interface by the driver. | |
10a23102 TR |
112 | * |
113 | * It is possible, though perhaps somewhat tricky, to implement race-free | |
114 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() | |
115 | * helper must be called first to initialize the minimum required to make | |
116 | * hotplug detection work. Drivers also need to make sure to properly set up | |
6806cdf9 | 117 | * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() |
10a23102 TR |
118 | * it is safe to enable interrupts and start processing hotplug events. At the |
119 | * same time, drivers should initialize all modeset objects such as CRTCs, | |
120 | * encoders and connectors. To finish up the fbdev helper initialization, the | |
121 | * drm_fb_helper_init() function is called. To probe for all attached displays | |
122 | * and set up an initial configuration using the detected hardware, drivers | |
123 | * should call drm_fb_helper_single_add_all_connectors() followed by | |
124 | * drm_fb_helper_initial_config(). | |
eaa434de | 125 | * |
6806cdf9 | 126 | * If &drm_framebuffer_funcs.dirty is set, the |
2dad551c | 127 | * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will |
6806cdf9 | 128 | * accumulate changes and schedule &drm_fb_helper.dirty_work to run right |
2dad551c NT |
129 | * away. This worker then calls the dirty() function ensuring that it will |
130 | * always run in process context since the fb_*() function could be running in | |
131 | * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io | |
132 | * callback it will also schedule dirty_work with the damage collected from the | |
48c9571c NT |
133 | * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup |
134 | * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()). | |
d0ddc033 DV |
135 | */ |
136 | ||
966a6a13 | 137 | #define drm_fb_helper_for_each_connector(fbh, i__) \ |
e13a0583 | 138 | for (({ lockdep_assert_held(&(fbh)->lock); }), \ |
966a6a13 CW |
139 | i__ = 0; i__ < (fbh)->connector_count; i__++) |
140 | ||
af2405af TR |
141 | static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, |
142 | struct drm_connector *connector) | |
39b8b2ed | 143 | { |
50021ff1 | 144 | struct drm_fb_helper_connector *fb_conn; |
39b8b2ed | 145 | struct drm_fb_helper_connector **temp; |
50021ff1 | 146 | unsigned int count; |
39b8b2ed TR |
147 | |
148 | if (!drm_fbdev_emulation) | |
149 | return 0; | |
150 | ||
e9827d8e | 151 | lockdep_assert_held(&fb_helper->lock); |
50021ff1 TR |
152 | |
153 | count = fb_helper->connector_count + 1; | |
154 | ||
155 | if (count > fb_helper->connector_info_alloc_count) { | |
156 | size_t size = count * sizeof(fb_conn); | |
157 | ||
158 | temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL); | |
39b8b2ed TR |
159 | if (!temp) |
160 | return -ENOMEM; | |
161 | ||
50021ff1 | 162 | fb_helper->connector_info_alloc_count = count; |
39b8b2ed TR |
163 | fb_helper->connector_info = temp; |
164 | } | |
165 | ||
50021ff1 TR |
166 | fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); |
167 | if (!fb_conn) | |
39b8b2ed TR |
168 | return -ENOMEM; |
169 | ||
170 | drm_connector_get(connector); | |
50021ff1 TR |
171 | fb_conn->connector = connector; |
172 | fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; | |
af2405af | 173 | |
39b8b2ed TR |
174 | return 0; |
175 | } | |
af2405af TR |
176 | |
177 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, | |
178 | struct drm_connector *connector) | |
179 | { | |
180 | int err; | |
181 | ||
c777990f NT |
182 | if (!fb_helper) |
183 | return 0; | |
184 | ||
e9827d8e | 185 | mutex_lock(&fb_helper->lock); |
af2405af | 186 | err = __drm_fb_helper_add_one_connector(fb_helper, connector); |
e9827d8e | 187 | mutex_unlock(&fb_helper->lock); |
af2405af TR |
188 | |
189 | return err; | |
190 | } | |
39b8b2ed TR |
191 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); |
192 | ||
207fd329 DV |
193 | /** |
194 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev | |
195 | * emulation helper | |
c777990f | 196 | * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL |
207fd329 DV |
197 | * |
198 | * This functions adds all the available connectors for use with the given | |
199 | * fb_helper. This is a separate step to allow drivers to freely assign | |
200 | * connectors to the fbdev, e.g. if some are reserved for special purposes or | |
201 | * not adequate to be used for the fbcon. | |
202 | * | |
169faeca DV |
203 | * This function is protected against concurrent connector hotadds/removals |
204 | * using drm_fb_helper_add_one_connector() and | |
205 | * drm_fb_helper_remove_one_connector(). | |
207fd329 | 206 | */ |
0b4c0f3f | 207 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) |
d50ba256 | 208 | { |
89f3f356 | 209 | struct drm_device *dev; |
0b4c0f3f | 210 | struct drm_connector *connector; |
c36a3254 DV |
211 | struct drm_connector_list_iter conn_iter; |
212 | int i, ret = 0; | |
d50ba256 | 213 | |
c777990f | 214 | if (!drm_fbdev_emulation || !fb_helper) |
f64c5573 DV |
215 | return 0; |
216 | ||
89f3f356 GS |
217 | dev = fb_helper->dev; |
218 | ||
e9827d8e | 219 | mutex_lock(&fb_helper->lock); |
b982dab1 | 220 | drm_connector_list_iter_begin(dev, &conn_iter); |
c36a3254 | 221 | drm_for_each_connector_iter(connector, &conn_iter) { |
af2405af | 222 | ret = __drm_fb_helper_add_one_connector(fb_helper, connector); |
15fce29d | 223 | if (ret) |
0b4c0f3f | 224 | goto fail; |
0b4c0f3f | 225 | } |
c36a3254 DV |
226 | goto out; |
227 | ||
0b4c0f3f | 228 | fail: |
966a6a13 | 229 | drm_fb_helper_for_each_connector(fb_helper, i) { |
7dfcb36a VS |
230 | struct drm_fb_helper_connector *fb_helper_connector = |
231 | fb_helper->connector_info[i]; | |
232 | ||
ad093607 | 233 | drm_connector_put(fb_helper_connector->connector); |
7dfcb36a VS |
234 | |
235 | kfree(fb_helper_connector); | |
0b4c0f3f DA |
236 | fb_helper->connector_info[i] = NULL; |
237 | } | |
238 | fb_helper->connector_count = 0; | |
c36a3254 | 239 | out: |
b982dab1 | 240 | drm_connector_list_iter_end(&conn_iter); |
e9827d8e | 241 | mutex_unlock(&fb_helper->lock); |
169faeca | 242 | |
15fce29d | 243 | return ret; |
d50ba256 | 244 | } |
0b4c0f3f | 245 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
d50ba256 | 246 | |
af2405af TR |
247 | static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
248 | struct drm_connector *connector) | |
65c2a89c DA |
249 | { |
250 | struct drm_fb_helper_connector *fb_helper_connector; | |
251 | int i, j; | |
252 | ||
f64c5573 DV |
253 | if (!drm_fbdev_emulation) |
254 | return 0; | |
255 | ||
e9827d8e | 256 | lockdep_assert_held(&fb_helper->lock); |
65c2a89c | 257 | |
e9827d8e | 258 | drm_fb_helper_for_each_connector(fb_helper, i) { |
65c2a89c DA |
259 | if (fb_helper->connector_info[i]->connector == connector) |
260 | break; | |
261 | } | |
262 | ||
263 | if (i == fb_helper->connector_count) | |
264 | return -EINVAL; | |
265 | fb_helper_connector = fb_helper->connector_info[i]; | |
ad093607 | 266 | drm_connector_put(fb_helper_connector->connector); |
65c2a89c | 267 | |
4b4f99f5 | 268 | for (j = i + 1; j < fb_helper->connector_count; j++) |
65c2a89c | 269 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; |
4b4f99f5 | 270 | |
65c2a89c DA |
271 | fb_helper->connector_count--; |
272 | kfree(fb_helper_connector); | |
2148f18f | 273 | |
65c2a89c DA |
274 | return 0; |
275 | } | |
af2405af TR |
276 | |
277 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, | |
278 | struct drm_connector *connector) | |
279 | { | |
280 | int err; | |
281 | ||
c777990f NT |
282 | if (!fb_helper) |
283 | return 0; | |
284 | ||
e9827d8e | 285 | mutex_lock(&fb_helper->lock); |
af2405af | 286 | err = __drm_fb_helper_remove_one_connector(fb_helper, connector); |
e9827d8e | 287 | mutex_unlock(&fb_helper->lock); |
af2405af TR |
288 | |
289 | return err; | |
290 | } | |
65c2a89c DA |
291 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); |
292 | ||
99231028 JW |
293 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
294 | { | |
295 | uint16_t *r_base, *g_base, *b_base; | |
296 | ||
ebe0f244 LP |
297 | if (crtc->funcs->gamma_set == NULL) |
298 | return; | |
299 | ||
99231028 JW |
300 | r_base = crtc->gamma_store; |
301 | g_base = r_base + crtc->gamma_size; | |
302 | b_base = g_base + crtc->gamma_size; | |
303 | ||
6d124ff8 DV |
304 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, |
305 | crtc->gamma_size, NULL); | |
99231028 JW |
306 | } |
307 | ||
207fd329 | 308 | /** |
6806cdf9 | 309 | * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter |
207fd329 DV |
310 | * @info: fbdev registered by the helper |
311 | */ | |
1a7aba7f JB |
312 | int drm_fb_helper_debug_enter(struct fb_info *info) |
313 | { | |
314 | struct drm_fb_helper *helper = info->par; | |
be26a66d | 315 | const struct drm_crtc_helper_funcs *funcs; |
1a7aba7f JB |
316 | int i; |
317 | ||
1a7aba7f JB |
318 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
319 | for (i = 0; i < helper->crtc_count; i++) { | |
320 | struct drm_mode_set *mode_set = | |
321 | &helper->crtc_info[i].mode_set; | |
322 | ||
323 | if (!mode_set->crtc->enabled) | |
324 | continue; | |
325 | ||
326 | funcs = mode_set->crtc->helper_private; | |
1b99b724 SC |
327 | if (funcs->mode_set_base_atomic == NULL) |
328 | continue; | |
329 | ||
9c79e0b1 DV |
330 | if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) |
331 | continue; | |
332 | ||
1a7aba7f JB |
333 | funcs->mode_set_base_atomic(mode_set->crtc, |
334 | mode_set->fb, | |
335 | mode_set->x, | |
413d45d3 | 336 | mode_set->y, |
21c74a8e | 337 | ENTER_ATOMIC_MODE_SET); |
1a7aba7f JB |
338 | } |
339 | } | |
340 | ||
341 | return 0; | |
342 | } | |
343 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); | |
344 | ||
207fd329 | 345 | /** |
6806cdf9 | 346 | * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave |
207fd329 DV |
347 | * @info: fbdev registered by the helper |
348 | */ | |
1a7aba7f JB |
349 | int drm_fb_helper_debug_leave(struct fb_info *info) |
350 | { | |
351 | struct drm_fb_helper *helper = info->par; | |
352 | struct drm_crtc *crtc; | |
be26a66d | 353 | const struct drm_crtc_helper_funcs *funcs; |
1a7aba7f JB |
354 | struct drm_framebuffer *fb; |
355 | int i; | |
356 | ||
357 | for (i = 0; i < helper->crtc_count; i++) { | |
358 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; | |
4b4f99f5 | 359 | |
1a7aba7f | 360 | crtc = mode_set->crtc; |
7114d2e2 ML |
361 | if (drm_drv_uses_atomic_modeset(crtc->dev)) |
362 | continue; | |
363 | ||
1a7aba7f | 364 | funcs = crtc->helper_private; |
7114d2e2 | 365 | fb = crtc->primary->fb; |
1a7aba7f JB |
366 | |
367 | if (!crtc->enabled) | |
368 | continue; | |
369 | ||
370 | if (!fb) { | |
371 | DRM_ERROR("no fb to restore??\n"); | |
372 | continue; | |
373 | } | |
374 | ||
1b99b724 SC |
375 | if (funcs->mode_set_base_atomic == NULL) |
376 | continue; | |
377 | ||
99231028 | 378 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
1a7aba7f | 379 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
21c74a8e | 380 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
1a7aba7f JB |
381 | } |
382 | ||
383 | return 0; | |
384 | } | |
385 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); | |
386 | ||
6b7dc6e9 | 387 | static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active) |
bbb1e524 RC |
388 | { |
389 | struct drm_device *dev = fb_helper->dev; | |
8f0cb418 | 390 | struct drm_plane_state *plane_state; |
bbb1e524 RC |
391 | struct drm_plane *plane; |
392 | struct drm_atomic_state *state; | |
393 | int i, ret; | |
1d0c6410 DV |
394 | struct drm_modeset_acquire_ctx ctx; |
395 | ||
396 | drm_modeset_acquire_init(&ctx, 0); | |
bbb1e524 RC |
397 | |
398 | state = drm_atomic_state_alloc(dev); | |
1d0c6410 DV |
399 | if (!state) { |
400 | ret = -ENOMEM; | |
401 | goto out_ctx; | |
402 | } | |
bbb1e524 | 403 | |
1d0c6410 | 404 | state->acquire_ctx = &ctx; |
bbb1e524 RC |
405 | retry: |
406 | drm_for_each_plane(plane, dev) { | |
bbb1e524 RC |
407 | plane_state = drm_atomic_get_plane_state(state, plane); |
408 | if (IS_ERR(plane_state)) { | |
409 | ret = PTR_ERR(plane_state); | |
1d0c6410 | 410 | goto out_state; |
bbb1e524 RC |
411 | } |
412 | ||
c2c446ad | 413 | plane_state->rotation = DRM_MODE_ROTATE_0; |
bbb1e524 RC |
414 | |
415 | /* disable non-primary: */ | |
416 | if (plane->type == DRM_PLANE_TYPE_PRIMARY) | |
417 | continue; | |
418 | ||
419 | ret = __drm_atomic_helper_disable_plane(plane, plane_state); | |
420 | if (ret != 0) | |
1d0c6410 | 421 | goto out_state; |
bbb1e524 RC |
422 | } |
423 | ||
4b4f99f5 | 424 | for (i = 0; i < fb_helper->crtc_count; i++) { |
bbb1e524 | 425 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; |
8f0cb418 HG |
426 | struct drm_plane *primary = mode_set->crtc->primary; |
427 | ||
428 | /* Cannot fail as we've already gotten the plane state above */ | |
429 | plane_state = drm_atomic_get_new_plane_state(state, primary); | |
430 | plane_state->rotation = fb_helper->crtc_info[i].rotation; | |
bbb1e524 RC |
431 | |
432 | ret = __drm_atomic_helper_set_config(mode_set, state); | |
433 | if (ret != 0) | |
1d0c6410 | 434 | goto out_state; |
6b7dc6e9 DV |
435 | |
436 | /* | |
437 | * __drm_atomic_helper_set_config() sets active when a | |
438 | * mode is set, unconditionally clear it if we force DPMS off | |
439 | */ | |
440 | if (!active) { | |
441 | struct drm_crtc *crtc = mode_set->crtc; | |
442 | struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); | |
443 | ||
444 | crtc_state->active = false; | |
445 | } | |
bbb1e524 RC |
446 | } |
447 | ||
448 | ret = drm_atomic_commit(state); | |
bbb1e524 | 449 | |
1d0c6410 | 450 | out_state: |
bbb1e524 RC |
451 | if (ret == -EDEADLK) |
452 | goto backoff; | |
453 | ||
0853695c | 454 | drm_atomic_state_put(state); |
1d0c6410 DV |
455 | out_ctx: |
456 | drm_modeset_drop_locks(&ctx); | |
457 | drm_modeset_acquire_fini(&ctx); | |
458 | ||
bbb1e524 RC |
459 | return ret; |
460 | ||
461 | backoff: | |
462 | drm_atomic_state_clear(state); | |
1d0c6410 | 463 | drm_modeset_backoff(&ctx); |
bbb1e524 RC |
464 | |
465 | goto retry; | |
466 | } | |
467 | ||
7128645d | 468 | static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper) |
e8e7a2b8 | 469 | { |
3858bc5d VS |
470 | struct drm_device *dev = fb_helper->dev; |
471 | struct drm_plane *plane; | |
1d0c6410 | 472 | int i, ret = 0; |
3858bc5d | 473 | |
1d0c6410 | 474 | drm_modeset_lock_all(fb_helper->dev); |
6295d607 | 475 | drm_for_each_plane(plane, dev) { |
e27dde3e MR |
476 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
477 | drm_plane_force_disable(plane); | |
6aed8ec3 | 478 | |
6686df8c | 479 | if (plane->rotation_property) |
d138dd3c VS |
480 | drm_mode_plane_set_obj_prop(plane, |
481 | plane->rotation_property, | |
c2c446ad | 482 | DRM_MODE_ROTATE_0); |
9783de20 SJ |
483 | } |
484 | ||
e8e7a2b8 DA |
485 | for (i = 0; i < fb_helper->crtc_count; i++) { |
486 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; | |
3858bc5d | 487 | struct drm_crtc *crtc = mode_set->crtc; |
3858bc5d | 488 | |
03f9abb2 AD |
489 | if (crtc->funcs->cursor_set2) { |
490 | ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0); | |
491 | if (ret) | |
1d0c6410 | 492 | goto out; |
03f9abb2 | 493 | } else if (crtc->funcs->cursor_set) { |
3858bc5d VS |
494 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); |
495 | if (ret) | |
1d0c6410 | 496 | goto out; |
3858bc5d VS |
497 | } |
498 | ||
2d13b679 | 499 | ret = drm_mode_set_config_internal(mode_set); |
e8e7a2b8 | 500 | if (ret) |
1d0c6410 | 501 | goto out; |
e8e7a2b8 | 502 | } |
1d0c6410 DV |
503 | out: |
504 | drm_modeset_unlock_all(fb_helper->dev); | |
b7bdf0a8 | 505 | |
1d0c6410 | 506 | return ret; |
e8e7a2b8 | 507 | } |
5ea1f752 | 508 | |
7128645d DV |
509 | static int restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
510 | { | |
511 | struct drm_device *dev = fb_helper->dev; | |
512 | ||
7128645d | 513 | if (drm_drv_uses_atomic_modeset(dev)) |
6b7dc6e9 | 514 | return restore_fbdev_mode_atomic(fb_helper, true); |
7128645d DV |
515 | else |
516 | return restore_fbdev_mode_legacy(fb_helper); | |
517 | } | |
518 | ||
5ea1f752 RC |
519 | /** |
520 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration | |
c777990f | 521 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
5ea1f752 | 522 | * |
6806cdf9 | 523 | * This should be called from driver's drm &drm_driver.lastclose callback |
5ea1f752 RC |
524 | * when implementing an fbcon on top of kms using this helper. This ensures that |
525 | * the user isn't greeted with a black screen when e.g. X dies. | |
b7bdf0a8 DV |
526 | * |
527 | * RETURNS: | |
528 | * Zero if everything went ok, negative error code otherwise. | |
5ea1f752 | 529 | */ |
b7bdf0a8 | 530 | int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) |
5ea1f752 | 531 | { |
b7bdf0a8 DV |
532 | bool do_delayed; |
533 | int ret; | |
e2809c7d | 534 | |
c777990f | 535 | if (!drm_fbdev_emulation || !fb_helper) |
f64c5573 DV |
536 | return -ENODEV; |
537 | ||
ca91a275 DV |
538 | if (READ_ONCE(fb_helper->deferred_setup)) |
539 | return 0; | |
540 | ||
e9827d8e | 541 | mutex_lock(&fb_helper->lock); |
5ea1f752 | 542 | ret = restore_fbdev_mode(fb_helper); |
e2809c7d DA |
543 | |
544 | do_delayed = fb_helper->delayed_hotplug; | |
545 | if (do_delayed) | |
546 | fb_helper->delayed_hotplug = false; | |
e9827d8e | 547 | mutex_unlock(&fb_helper->lock); |
e2809c7d DA |
548 | |
549 | if (do_delayed) | |
550 | drm_fb_helper_hotplug_event(fb_helper); | |
e9827d8e | 551 | |
5ea1f752 RC |
552 | return ret; |
553 | } | |
554 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); | |
e8e7a2b8 | 555 | |
2c4124fd GU |
556 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
557 | { | |
558 | struct drm_device *dev = fb_helper->dev; | |
559 | struct drm_crtc *crtc; | |
560 | int bound = 0, crtcs_bound = 0; | |
561 | ||
4b4f99f5 TR |
562 | /* |
563 | * Sometimes user space wants everything disabled, so don't steal the | |
564 | * display if there's a master. | |
565 | */ | |
f17b3ea3 | 566 | if (READ_ONCE(dev->master)) |
2c4124fd GU |
567 | return false; |
568 | ||
569 | drm_for_each_crtc(crtc, dev) { | |
bdac4a05 | 570 | drm_modeset_lock(&crtc->mutex, NULL); |
2c4124fd GU |
571 | if (crtc->primary->fb) |
572 | crtcs_bound++; | |
573 | if (crtc->primary->fb == fb_helper->fb) | |
574 | bound++; | |
bdac4a05 | 575 | drm_modeset_unlock(&crtc->mutex); |
2c4124fd GU |
576 | } |
577 | ||
578 | if (bound < crtcs_bound) | |
579 | return false; | |
580 | ||
581 | return true; | |
582 | } | |
583 | ||
584 | #ifdef CONFIG_MAGIC_SYSRQ | |
d21bf469 DV |
585 | /* |
586 | * restore fbcon display for all kms driver's using this helper, used for sysrq | |
587 | * and panic handling. | |
588 | */ | |
78b9c353 | 589 | static bool drm_fb_helper_force_kernel_mode(void) |
785b93ef | 590 | { |
785b93ef DA |
591 | bool ret, error = false; |
592 | struct drm_fb_helper *helper; | |
593 | ||
594 | if (list_empty(&kernel_fb_helper_list)) | |
595 | return false; | |
596 | ||
597 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { | |
b77f0765 TR |
598 | struct drm_device *dev = helper->dev; |
599 | ||
600 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) | |
601 | continue; | |
602 | ||
e9827d8e | 603 | mutex_lock(&helper->lock); |
3d9e35a9 | 604 | ret = restore_fbdev_mode(helper); |
e8e7a2b8 DA |
605 | if (ret) |
606 | error = true; | |
e9827d8e | 607 | mutex_unlock(&helper->lock); |
785b93ef DA |
608 | } |
609 | return error; | |
610 | } | |
611 | ||
785b93ef DA |
612 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
613 | { | |
d21bf469 | 614 | bool ret; |
4b4f99f5 | 615 | |
d21bf469 DV |
616 | ret = drm_fb_helper_force_kernel_mode(); |
617 | if (ret == true) | |
618 | DRM_ERROR("Failed to restore crtc configuration\n"); | |
785b93ef DA |
619 | } |
620 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); | |
621 | ||
1495cc9d | 622 | static void drm_fb_helper_sysrq(int dummy1) |
785b93ef DA |
623 | { |
624 | schedule_work(&drm_fb_helper_restore_work); | |
625 | } | |
626 | ||
627 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { | |
628 | .handler = drm_fb_helper_sysrq, | |
629 | .help_msg = "force-fb(V)", | |
630 | .action_msg = "Restore framebuffer console", | |
631 | }; | |
b8c40d62 RD |
632 | #else |
633 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; | |
bea1d35b | 634 | #endif |
785b93ef | 635 | |
6b7dc6e9 | 636 | static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode) |
785b93ef | 637 | { |
785b93ef DA |
638 | struct drm_device *dev = fb_helper->dev; |
639 | struct drm_crtc *crtc; | |
023eb571 | 640 | struct drm_connector *connector; |
023eb571 | 641 | int i, j; |
785b93ef | 642 | |
bdac4a05 | 643 | drm_modeset_lock_all(dev); |
e87b2c42 | 644 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 | 645 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
785b93ef | 646 | |
8be48d92 DA |
647 | if (!crtc->enabled) |
648 | continue; | |
649 | ||
3a8148c5 | 650 | /* Walk the connectors & encoders on this fb turning them on/off */ |
966a6a13 | 651 | drm_fb_helper_for_each_connector(fb_helper, j) { |
023eb571 | 652 | connector = fb_helper->connector_info[j]->connector; |
e04190e0 | 653 | connector->funcs->dpms(connector, dpms_mode); |
58495563 | 654 | drm_object_property_set_value(&connector->base, |
3a8148c5 | 655 | dev->mode_config.dpms_property, dpms_mode); |
785b93ef | 656 | } |
785b93ef | 657 | } |
84849903 | 658 | drm_modeset_unlock_all(dev); |
6b7dc6e9 DV |
659 | } |
660 | ||
661 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) | |
662 | { | |
663 | struct drm_fb_helper *fb_helper = info->par; | |
664 | ||
665 | /* | |
666 | * For each CRTC in this fb, turn the connectors on/off. | |
667 | */ | |
668 | mutex_lock(&fb_helper->lock); | |
669 | if (!drm_fb_helper_is_bound(fb_helper)) { | |
670 | mutex_unlock(&fb_helper->lock); | |
671 | return; | |
672 | } | |
673 | ||
674 | if (drm_drv_uses_atomic_modeset(fb_helper->dev)) | |
675 | restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON); | |
676 | else | |
677 | dpms_legacy(fb_helper, dpms_mode); | |
e9827d8e | 678 | mutex_unlock(&fb_helper->lock); |
785b93ef DA |
679 | } |
680 | ||
207fd329 | 681 | /** |
6806cdf9 | 682 | * drm_fb_helper_blank - implementation for &fb_ops.fb_blank |
207fd329 DV |
683 | * @blank: desired blanking state |
684 | * @info: fbdev registered by the helper | |
685 | */ | |
785b93ef DA |
686 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
687 | { | |
c50bfd08 DV |
688 | if (oops_in_progress) |
689 | return -EBUSY; | |
690 | ||
785b93ef | 691 | switch (blank) { |
731b5a15 | 692 | /* Display: On; HSync: On, VSync: On */ |
785b93ef | 693 | case FB_BLANK_UNBLANK: |
3a8148c5 | 694 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
785b93ef | 695 | break; |
731b5a15 | 696 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 697 | case FB_BLANK_NORMAL: |
3a8148c5 | 698 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 699 | break; |
731b5a15 | 700 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef | 701 | case FB_BLANK_HSYNC_SUSPEND: |
3a8148c5 | 702 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 703 | break; |
731b5a15 | 704 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef | 705 | case FB_BLANK_VSYNC_SUSPEND: |
3a8148c5 | 706 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
785b93ef | 707 | break; |
731b5a15 | 708 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef | 709 | case FB_BLANK_POWERDOWN: |
3a8148c5 | 710 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
785b93ef DA |
711 | break; |
712 | } | |
713 | return 0; | |
714 | } | |
715 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
716 | ||
a2889606 VS |
717 | static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, |
718 | struct drm_mode_set *modeset) | |
719 | { | |
720 | int i; | |
721 | ||
722 | for (i = 0; i < modeset->num_connectors; i++) { | |
ad093607 | 723 | drm_connector_put(modeset->connectors[i]); |
a2889606 VS |
724 | modeset->connectors[i] = NULL; |
725 | } | |
726 | modeset->num_connectors = 0; | |
727 | ||
728 | drm_mode_destroy(helper->dev, modeset->mode); | |
729 | modeset->mode = NULL; | |
730 | ||
731 | /* FIXME should hold a ref? */ | |
732 | modeset->fb = NULL; | |
733 | } | |
734 | ||
785b93ef DA |
735 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) |
736 | { | |
737 | int i; | |
738 | ||
6e86d58b | 739 | for (i = 0; i < helper->connector_count; i++) { |
ad093607 | 740 | drm_connector_put(helper->connector_info[i]->connector); |
0b4c0f3f | 741 | kfree(helper->connector_info[i]); |
6e86d58b | 742 | } |
0b4c0f3f | 743 | kfree(helper->connector_info); |
a2889606 | 744 | |
a1b7736d | 745 | for (i = 0; i < helper->crtc_count; i++) { |
a2889606 VS |
746 | struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; |
747 | ||
748 | drm_fb_helper_modeset_release(helper, modeset); | |
749 | kfree(modeset->connectors); | |
a1b7736d | 750 | } |
785b93ef DA |
751 | kfree(helper->crtc_info); |
752 | } | |
753 | ||
cfe63423 NT |
754 | static void drm_fb_helper_resume_worker(struct work_struct *work) |
755 | { | |
756 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, | |
757 | resume_work); | |
758 | ||
759 | console_lock(); | |
760 | fb_set_suspend(helper->fbdev, 0); | |
761 | console_unlock(); | |
762 | } | |
763 | ||
d536540f NT |
764 | static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, |
765 | struct drm_clip_rect *clip) | |
766 | { | |
767 | struct drm_framebuffer *fb = fb_helper->fb; | |
768 | unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0); | |
769 | size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; | |
770 | void *src = fb_helper->fbdev->screen_buffer + offset; | |
771 | void *dst = fb_helper->buffer->vaddr + offset; | |
772 | size_t len = (clip->x2 - clip->x1) * cpp; | |
773 | unsigned int y; | |
774 | ||
775 | for (y = clip->y1; y < clip->y2; y++) { | |
776 | memcpy(dst, src, len); | |
777 | src += fb->pitches[0]; | |
778 | dst += fb->pitches[0]; | |
779 | } | |
780 | } | |
781 | ||
eaa434de NT |
782 | static void drm_fb_helper_dirty_work(struct work_struct *work) |
783 | { | |
784 | struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, | |
785 | dirty_work); | |
786 | struct drm_clip_rect *clip = &helper->dirty_clip; | |
787 | struct drm_clip_rect clip_copy; | |
788 | unsigned long flags; | |
789 | ||
790 | spin_lock_irqsave(&helper->dirty_lock, flags); | |
791 | clip_copy = *clip; | |
792 | clip->x1 = clip->y1 = ~0; | |
793 | clip->x2 = clip->y2 = 0; | |
794 | spin_unlock_irqrestore(&helper->dirty_lock, flags); | |
795 | ||
87d3b658 | 796 | /* call dirty callback only when it has been really touched */ |
d536540f NT |
797 | if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { |
798 | /* Generic fbdev uses a shadow buffer */ | |
799 | if (helper->buffer) | |
800 | drm_fb_helper_dirty_blit_real(helper, &clip_copy); | |
87d3b658 | 801 | helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); |
d536540f | 802 | } |
eaa434de NT |
803 | } |
804 | ||
10a23102 TR |
805 | /** |
806 | * drm_fb_helper_prepare - setup a drm_fb_helper structure | |
807 | * @dev: DRM device | |
808 | * @helper: driver-allocated fbdev helper structure to set up | |
809 | * @funcs: pointer to structure of functions associate with this helper | |
810 | * | |
811 | * Sets up the bare minimum to make the framebuffer helper usable. This is | |
812 | * useful to implement race-free initialization of the polling helpers. | |
813 | */ | |
814 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, | |
815 | const struct drm_fb_helper_funcs *funcs) | |
816 | { | |
817 | INIT_LIST_HEAD(&helper->kernel_fb_list); | |
eaa434de | 818 | spin_lock_init(&helper->dirty_lock); |
cfe63423 | 819 | INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); |
eaa434de NT |
820 | INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); |
821 | helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; | |
e9827d8e | 822 | mutex_init(&helper->lock); |
10a23102 TR |
823 | helper->funcs = funcs; |
824 | helper->dev = dev; | |
825 | } | |
826 | EXPORT_SYMBOL(drm_fb_helper_prepare); | |
827 | ||
207fd329 | 828 | /** |
ed84e254 | 829 | * drm_fb_helper_init - initialize a &struct drm_fb_helper |
207fd329 DV |
830 | * @dev: drm device |
831 | * @fb_helper: driver-allocated fbdev helper structure to initialize | |
207fd329 DV |
832 | * @max_conn_count: max connector count |
833 | * | |
834 | * This allocates the structures for the fbdev helper with the given limits. | |
835 | * Note that this won't yet touch the hardware (through the driver interfaces) | |
836 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() | |
837 | * to allow driver writes more control over the exact init sequence. | |
838 | * | |
10a23102 | 839 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
207fd329 DV |
840 | * |
841 | * RETURNS: | |
842 | * Zero if everything went ok, nonzero otherwise. | |
843 | */ | |
4abe3520 DA |
844 | int drm_fb_helper_init(struct drm_device *dev, |
845 | struct drm_fb_helper *fb_helper, | |
e4563f6b | 846 | int max_conn_count) |
785b93ef | 847 | { |
785b93ef | 848 | struct drm_crtc *crtc; |
e4563f6b | 849 | struct drm_mode_config *config = &dev->mode_config; |
785b93ef DA |
850 | int i; |
851 | ||
29ad20b2 NT |
852 | if (!drm_fbdev_emulation) { |
853 | dev->fb_helper = fb_helper; | |
f64c5573 | 854 | return 0; |
29ad20b2 | 855 | } |
f64c5573 | 856 | |
04cfe97e XL |
857 | if (!max_conn_count) |
858 | return -EINVAL; | |
859 | ||
e4563f6b | 860 | fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
4abe3520 | 861 | if (!fb_helper->crtc_info) |
785b93ef DA |
862 | return -ENOMEM; |
863 | ||
e4563f6b | 864 | fb_helper->crtc_count = config->num_crtc; |
4abe3520 DA |
865 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
866 | if (!fb_helper->connector_info) { | |
867 | kfree(fb_helper->crtc_info); | |
0b4c0f3f DA |
868 | return -ENOMEM; |
869 | } | |
65c2a89c | 870 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
4abe3520 | 871 | fb_helper->connector_count = 0; |
785b93ef | 872 | |
e4563f6b | 873 | for (i = 0; i < fb_helper->crtc_count; i++) { |
4abe3520 | 874 | fb_helper->crtc_info[i].mode_set.connectors = |
785b93ef DA |
875 | kcalloc(max_conn_count, |
876 | sizeof(struct drm_connector *), | |
877 | GFP_KERNEL); | |
878 | ||
4a1b0714 | 879 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
785b93ef | 880 | goto out_free; |
4abe3520 | 881 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
8f0cb418 | 882 | fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0; |
785b93ef DA |
883 | } |
884 | ||
885 | i = 0; | |
6295d607 | 886 | drm_for_each_crtc(crtc, dev) { |
4abe3520 | 887 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
785b93ef DA |
888 | i++; |
889 | } | |
e9ad3181 | 890 | |
29ad20b2 NT |
891 | dev->fb_helper = fb_helper; |
892 | ||
785b93ef DA |
893 | return 0; |
894 | out_free: | |
4abe3520 | 895 | drm_fb_helper_crtc_free(fb_helper); |
785b93ef DA |
896 | return -ENOMEM; |
897 | } | |
4abe3520 DA |
898 | EXPORT_SYMBOL(drm_fb_helper_init); |
899 | ||
b8017d6c AT |
900 | /** |
901 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members | |
902 | * @fb_helper: driver-allocated fbdev helper | |
903 | * | |
904 | * A helper to alloc fb_info and the members cmap and apertures. Called | |
da7bdda2 DV |
905 | * by the driver within the fb_probe fb_helper callback function. Drivers do not |
906 | * need to release the allocated fb_info structure themselves, this is | |
907 | * automatically done when calling drm_fb_helper_fini(). | |
b8017d6c AT |
908 | * |
909 | * RETURNS: | |
910 | * fb_info pointer if things went okay, pointer containing error code | |
911 | * otherwise | |
912 | */ | |
913 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) | |
914 | { | |
915 | struct device *dev = fb_helper->dev->dev; | |
916 | struct fb_info *info; | |
917 | int ret; | |
918 | ||
919 | info = framebuffer_alloc(0, dev); | |
920 | if (!info) | |
921 | return ERR_PTR(-ENOMEM); | |
922 | ||
923 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
924 | if (ret) | |
925 | goto err_release; | |
926 | ||
927 | info->apertures = alloc_apertures(1); | |
928 | if (!info->apertures) { | |
929 | ret = -ENOMEM; | |
930 | goto err_free_cmap; | |
931 | } | |
932 | ||
933 | fb_helper->fbdev = info; | |
934 | ||
935 | return info; | |
936 | ||
937 | err_free_cmap: | |
938 | fb_dealloc_cmap(&info->cmap); | |
939 | err_release: | |
940 | framebuffer_release(info); | |
941 | return ERR_PTR(ret); | |
942 | } | |
943 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); | |
944 | ||
945 | /** | |
946 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device | |
c777990f | 947 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
b8017d6c AT |
948 | * |
949 | * A wrapper around unregister_framebuffer, to release the fb_info | |
ed84e254 DV |
950 | * framebuffer device. This must be called before releasing all resources for |
951 | * @fb_helper by calling drm_fb_helper_fini(). | |
b8017d6c AT |
952 | */ |
953 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) | |
954 | { | |
955 | if (fb_helper && fb_helper->fbdev) | |
956 | unregister_framebuffer(fb_helper->fbdev); | |
957 | } | |
958 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); | |
959 | ||
ed84e254 DV |
960 | /** |
961 | * drm_fb_helper_fini - finialize a &struct drm_fb_helper | |
c777990f | 962 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
ed84e254 DV |
963 | * |
964 | * This cleans up all remaining resources associated with @fb_helper. Must be | |
965 | * called after drm_fb_helper_unlink_fbi() was called. | |
966 | */ | |
4abe3520 DA |
967 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
968 | { | |
da7bdda2 DV |
969 | struct fb_info *info; |
970 | ||
29ad20b2 NT |
971 | if (!fb_helper) |
972 | return; | |
973 | ||
974 | fb_helper->dev->fb_helper = NULL; | |
975 | ||
976 | if (!drm_fbdev_emulation) | |
f64c5573 DV |
977 | return; |
978 | ||
b52f09c4 NT |
979 | cancel_work_sync(&fb_helper->resume_work); |
980 | cancel_work_sync(&fb_helper->dirty_work); | |
981 | ||
da7bdda2 DV |
982 | info = fb_helper->fbdev; |
983 | if (info) { | |
984 | if (info->cmap.len) | |
985 | fb_dealloc_cmap(&info->cmap); | |
986 | framebuffer_release(info); | |
987 | } | |
988 | fb_helper->fbdev = NULL; | |
989 | ||
a53ca635 | 990 | mutex_lock(&kernel_fb_helper_lock); |
4abe3520 DA |
991 | if (!list_empty(&fb_helper->kernel_fb_list)) { |
992 | list_del(&fb_helper->kernel_fb_list); | |
4b4f99f5 | 993 | if (list_empty(&kernel_fb_helper_list)) |
4abe3520 | 994 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
4abe3520 | 995 | } |
a53ca635 | 996 | mutex_unlock(&kernel_fb_helper_lock); |
4abe3520 | 997 | |
e9827d8e | 998 | mutex_destroy(&fb_helper->lock); |
4abe3520 DA |
999 | drm_fb_helper_crtc_free(fb_helper); |
1000 | ||
4abe3520 DA |
1001 | } |
1002 | EXPORT_SYMBOL(drm_fb_helper_fini); | |
785b93ef | 1003 | |
47074ab7 AT |
1004 | /** |
1005 | * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer | |
c777990f | 1006 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
47074ab7 AT |
1007 | * |
1008 | * A wrapper around unlink_framebuffer implemented by fbdev core | |
1009 | */ | |
1010 | void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) | |
1011 | { | |
1012 | if (fb_helper && fb_helper->fbdev) | |
1013 | unlink_framebuffer(fb_helper->fbdev); | |
1014 | } | |
1015 | EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); | |
1016 | ||
eaa434de NT |
1017 | static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, |
1018 | u32 width, u32 height) | |
1019 | { | |
1020 | struct drm_fb_helper *helper = info->par; | |
1021 | struct drm_clip_rect *clip = &helper->dirty_clip; | |
1022 | unsigned long flags; | |
1023 | ||
1024 | if (!helper->fb->funcs->dirty) | |
1025 | return; | |
1026 | ||
1027 | spin_lock_irqsave(&helper->dirty_lock, flags); | |
1028 | clip->x1 = min_t(u32, clip->x1, x); | |
1029 | clip->y1 = min_t(u32, clip->y1, y); | |
1030 | clip->x2 = max_t(u32, clip->x2, x + width); | |
1031 | clip->y2 = max_t(u32, clip->y2, y + height); | |
1032 | spin_unlock_irqrestore(&helper->dirty_lock, flags); | |
1033 | ||
1034 | schedule_work(&helper->dirty_work); | |
1035 | } | |
1036 | ||
1037 | /** | |
1038 | * drm_fb_helper_deferred_io() - fbdev deferred_io callback function | |
1039 | * @info: fb_info struct pointer | |
1040 | * @pagelist: list of dirty mmap framebuffer pages | |
1041 | * | |
6806cdf9 | 1042 | * This function is used as the &fb_deferred_io.deferred_io |
eaa434de NT |
1043 | * callback function for flushing the fbdev mmap writes. |
1044 | */ | |
1045 | void drm_fb_helper_deferred_io(struct fb_info *info, | |
1046 | struct list_head *pagelist) | |
1047 | { | |
1048 | unsigned long start, end, min, max; | |
1049 | struct page *page; | |
1050 | u32 y1, y2; | |
1051 | ||
1052 | min = ULONG_MAX; | |
1053 | max = 0; | |
1054 | list_for_each_entry(page, pagelist, lru) { | |
1055 | start = page->index << PAGE_SHIFT; | |
1056 | end = start + PAGE_SIZE - 1; | |
1057 | min = min(min, start); | |
1058 | max = max(max, end); | |
1059 | } | |
1060 | ||
1061 | if (min < max) { | |
1062 | y1 = min / info->fix.line_length; | |
1063 | y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), | |
1064 | info->var.yres); | |
1065 | drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); | |
1066 | } | |
1067 | } | |
1068 | EXPORT_SYMBOL(drm_fb_helper_deferred_io); | |
1069 | ||
48c9571c NT |
1070 | /** |
1071 | * drm_fb_helper_defio_init - fbdev deferred I/O initialization | |
1072 | * @fb_helper: driver-allocated fbdev helper | |
1073 | * | |
1074 | * This function allocates &fb_deferred_io, sets callback to | |
1075 | * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init(). | |
1076 | * It should be called from the &drm_fb_helper_funcs->fb_probe callback. | |
1077 | * drm_fb_helper_fbdev_teardown() cleans up deferred I/O. | |
1078 | * | |
1079 | * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done | |
1080 | * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby | |
1081 | * affect other instances of that &fb_ops. | |
1082 | * | |
1083 | * Returns: | |
1084 | * 0 on success or a negative error code on failure. | |
1085 | */ | |
1086 | int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) | |
1087 | { | |
1088 | struct fb_info *info = fb_helper->fbdev; | |
1089 | struct fb_deferred_io *fbdefio; | |
1090 | struct fb_ops *fbops; | |
1091 | ||
1092 | fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); | |
1093 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); | |
1094 | if (!fbdefio || !fbops) { | |
1095 | kfree(fbdefio); | |
1096 | kfree(fbops); | |
1097 | return -ENOMEM; | |
1098 | } | |
1099 | ||
1100 | info->fbdefio = fbdefio; | |
1101 | fbdefio->delay = msecs_to_jiffies(50); | |
1102 | fbdefio->deferred_io = drm_fb_helper_deferred_io; | |
1103 | ||
1104 | *fbops = *info->fbops; | |
1105 | info->fbops = fbops; | |
1106 | ||
1107 | fb_deferred_io_init(info); | |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | EXPORT_SYMBOL(drm_fb_helper_defio_init); | |
1112 | ||
cbb1a82e AT |
1113 | /** |
1114 | * drm_fb_helper_sys_read - wrapper around fb_sys_read | |
1115 | * @info: fb_info struct pointer | |
1116 | * @buf: userspace buffer to read from framebuffer memory | |
1117 | * @count: number of bytes to read from framebuffer memory | |
1118 | * @ppos: read offset within framebuffer memory | |
1119 | * | |
1120 | * A wrapper around fb_sys_read implemented by fbdev core | |
1121 | */ | |
1122 | ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, | |
1123 | size_t count, loff_t *ppos) | |
1124 | { | |
1125 | return fb_sys_read(info, buf, count, ppos); | |
1126 | } | |
1127 | EXPORT_SYMBOL(drm_fb_helper_sys_read); | |
1128 | ||
1129 | /** | |
1130 | * drm_fb_helper_sys_write - wrapper around fb_sys_write | |
1131 | * @info: fb_info struct pointer | |
1132 | * @buf: userspace buffer to write to framebuffer memory | |
1133 | * @count: number of bytes to write to framebuffer memory | |
1134 | * @ppos: write offset within framebuffer memory | |
1135 | * | |
1136 | * A wrapper around fb_sys_write implemented by fbdev core | |
1137 | */ | |
1138 | ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, | |
1139 | size_t count, loff_t *ppos) | |
1140 | { | |
eaa434de NT |
1141 | ssize_t ret; |
1142 | ||
1143 | ret = fb_sys_write(info, buf, count, ppos); | |
1144 | if (ret > 0) | |
1145 | drm_fb_helper_dirty(info, 0, 0, info->var.xres, | |
1146 | info->var.yres); | |
1147 | ||
1148 | return ret; | |
cbb1a82e AT |
1149 | } |
1150 | EXPORT_SYMBOL(drm_fb_helper_sys_write); | |
1151 | ||
742547b7 AT |
1152 | /** |
1153 | * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect | |
1154 | * @info: fbdev registered by the helper | |
1155 | * @rect: info about rectangle to fill | |
1156 | * | |
1157 | * A wrapper around sys_fillrect implemented by fbdev core | |
1158 | */ | |
1159 | void drm_fb_helper_sys_fillrect(struct fb_info *info, | |
1160 | const struct fb_fillrect *rect) | |
1161 | { | |
1162 | sys_fillrect(info, rect); | |
eaa434de NT |
1163 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
1164 | rect->width, rect->height); | |
742547b7 AT |
1165 | } |
1166 | EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); | |
1167 | ||
1168 | /** | |
1169 | * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea | |
1170 | * @info: fbdev registered by the helper | |
1171 | * @area: info about area to copy | |
1172 | * | |
1173 | * A wrapper around sys_copyarea implemented by fbdev core | |
1174 | */ | |
1175 | void drm_fb_helper_sys_copyarea(struct fb_info *info, | |
1176 | const struct fb_copyarea *area) | |
1177 | { | |
1178 | sys_copyarea(info, area); | |
eaa434de NT |
1179 | drm_fb_helper_dirty(info, area->dx, area->dy, |
1180 | area->width, area->height); | |
742547b7 AT |
1181 | } |
1182 | EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); | |
1183 | ||
1184 | /** | |
1185 | * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit | |
1186 | * @info: fbdev registered by the helper | |
1187 | * @image: info about image to blit | |
1188 | * | |
1189 | * A wrapper around sys_imageblit implemented by fbdev core | |
1190 | */ | |
1191 | void drm_fb_helper_sys_imageblit(struct fb_info *info, | |
1192 | const struct fb_image *image) | |
1193 | { | |
1194 | sys_imageblit(info, image); | |
eaa434de NT |
1195 | drm_fb_helper_dirty(info, image->dx, image->dy, |
1196 | image->width, image->height); | |
742547b7 AT |
1197 | } |
1198 | EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); | |
1199 | ||
1200 | /** | |
1201 | * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect | |
1202 | * @info: fbdev registered by the helper | |
1203 | * @rect: info about rectangle to fill | |
1204 | * | |
d104328c | 1205 | * A wrapper around cfb_fillrect implemented by fbdev core |
742547b7 AT |
1206 | */ |
1207 | void drm_fb_helper_cfb_fillrect(struct fb_info *info, | |
1208 | const struct fb_fillrect *rect) | |
1209 | { | |
1210 | cfb_fillrect(info, rect); | |
eaa434de NT |
1211 | drm_fb_helper_dirty(info, rect->dx, rect->dy, |
1212 | rect->width, rect->height); | |
742547b7 AT |
1213 | } |
1214 | EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); | |
1215 | ||
1216 | /** | |
1217 | * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea | |
1218 | * @info: fbdev registered by the helper | |
1219 | * @area: info about area to copy | |
1220 | * | |
1221 | * A wrapper around cfb_copyarea implemented by fbdev core | |
1222 | */ | |
1223 | void drm_fb_helper_cfb_copyarea(struct fb_info *info, | |
1224 | const struct fb_copyarea *area) | |
1225 | { | |
1226 | cfb_copyarea(info, area); | |
eaa434de NT |
1227 | drm_fb_helper_dirty(info, area->dx, area->dy, |
1228 | area->width, area->height); | |
742547b7 AT |
1229 | } |
1230 | EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); | |
1231 | ||
1232 | /** | |
1233 | * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit | |
1234 | * @info: fbdev registered by the helper | |
1235 | * @image: info about image to blit | |
1236 | * | |
1237 | * A wrapper around cfb_imageblit implemented by fbdev core | |
1238 | */ | |
1239 | void drm_fb_helper_cfb_imageblit(struct fb_info *info, | |
1240 | const struct fb_image *image) | |
1241 | { | |
1242 | cfb_imageblit(info, image); | |
eaa434de NT |
1243 | drm_fb_helper_dirty(info, image->dx, image->dy, |
1244 | image->width, image->height); | |
742547b7 AT |
1245 | } |
1246 | EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); | |
1247 | ||
fdefa58a AT |
1248 | /** |
1249 | * drm_fb_helper_set_suspend - wrapper around fb_set_suspend | |
c777990f | 1250 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
28579f37 | 1251 | * @suspend: whether to suspend or resume |
fdefa58a | 1252 | * |
cfe63423 NT |
1253 | * A wrapper around fb_set_suspend implemented by fbdev core. |
1254 | * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take | |
1255 | * the lock yourself | |
fdefa58a | 1256 | */ |
28579f37 | 1257 | void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) |
fdefa58a AT |
1258 | { |
1259 | if (fb_helper && fb_helper->fbdev) | |
28579f37 | 1260 | fb_set_suspend(fb_helper->fbdev, suspend); |
fdefa58a AT |
1261 | } |
1262 | EXPORT_SYMBOL(drm_fb_helper_set_suspend); | |
1263 | ||
cfe63423 NT |
1264 | /** |
1265 | * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also | |
1266 | * takes the console lock | |
c777990f | 1267 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
28579f37 | 1268 | * @suspend: whether to suspend or resume |
cfe63423 NT |
1269 | * |
1270 | * A wrapper around fb_set_suspend() that takes the console lock. If the lock | |
1271 | * isn't available on resume, a worker is tasked with waiting for the lock | |
1272 | * to become available. The console lock can be pretty contented on resume | |
1273 | * due to all the printk activity. | |
1274 | * | |
1275 | * This function can be called multiple times with the same state since | |
6806cdf9 | 1276 | * &fb_info.state is checked to see if fbdev is running or not before locking. |
cfe63423 NT |
1277 | * |
1278 | * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. | |
1279 | */ | |
1280 | void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, | |
28579f37 | 1281 | bool suspend) |
cfe63423 NT |
1282 | { |
1283 | if (!fb_helper || !fb_helper->fbdev) | |
1284 | return; | |
1285 | ||
1286 | /* make sure there's no pending/ongoing resume */ | |
1287 | flush_work(&fb_helper->resume_work); | |
1288 | ||
1289 | if (suspend) { | |
1290 | if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) | |
1291 | return; | |
1292 | ||
1293 | console_lock(); | |
1294 | ||
1295 | } else { | |
1296 | if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) | |
1297 | return; | |
1298 | ||
1299 | if (!console_trylock()) { | |
1300 | schedule_work(&fb_helper->resume_work); | |
1301 | return; | |
1302 | } | |
1303 | } | |
1304 | ||
1305 | fb_set_suspend(fb_helper->fbdev, suspend); | |
1306 | console_unlock(); | |
1307 | } | |
1308 | EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); | |
1309 | ||
b8e2b019 PR |
1310 | static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) |
1311 | { | |
1312 | u32 *palette = (u32 *)info->pseudo_palette; | |
1313 | int i; | |
1314 | ||
1315 | if (cmap->start + cmap->len > 16) | |
1316 | return -EINVAL; | |
1317 | ||
1318 | for (i = 0; i < cmap->len; ++i) { | |
1319 | u16 red = cmap->red[i]; | |
1320 | u16 green = cmap->green[i]; | |
1321 | u16 blue = cmap->blue[i]; | |
1322 | u32 value; | |
1323 | ||
1324 | red >>= 16 - info->var.red.length; | |
1325 | green >>= 16 - info->var.green.length; | |
1326 | blue >>= 16 - info->var.blue.length; | |
1327 | value = (red << info->var.red.offset) | | |
1328 | (green << info->var.green.offset) | | |
1329 | (blue << info->var.blue.offset); | |
1330 | if (info->var.transp.length > 0) { | |
1331 | u32 mask = (1 << info->var.transp.length) - 1; | |
1332 | ||
1333 | mask <<= info->var.transp.offset; | |
1334 | value |= mask; | |
1335 | } | |
1336 | palette[cmap->start + i] = value; | |
1337 | } | |
1338 | ||
1339 | return 0; | |
1340 | } | |
1341 | ||
964c6006 | 1342 | static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) |
068143d3 DA |
1343 | { |
1344 | struct drm_fb_helper *fb_helper = info->par; | |
068143d3 | 1345 | struct drm_crtc *crtc; |
a3562a0e | 1346 | u16 *r, *g, *b; |
964c6006 | 1347 | int i, ret = 0; |
068143d3 | 1348 | |
964c6006 PR |
1349 | drm_modeset_lock_all(fb_helper->dev); |
1350 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1351 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
1352 | if (!crtc->funcs->gamma_set || !crtc->gamma_size) | |
1353 | return -EINVAL; | |
c50bfd08 | 1354 | |
964c6006 PR |
1355 | if (cmap->start + cmap->len > crtc->gamma_size) |
1356 | return -EINVAL; | |
1357 | ||
1358 | r = crtc->gamma_store; | |
1359 | g = r + crtc->gamma_size; | |
1360 | b = g + crtc->gamma_size; | |
1361 | ||
1362 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); | |
1363 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); | |
1364 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); | |
1365 | ||
1366 | ret = crtc->funcs->gamma_set(crtc, r, g, b, | |
1367 | crtc->gamma_size, NULL); | |
1368 | if (ret) | |
1369 | return ret; | |
8391a3d5 | 1370 | } |
964c6006 | 1371 | drm_modeset_unlock_all(fb_helper->dev); |
8391a3d5 | 1372 | |
964c6006 PR |
1373 | return ret; |
1374 | } | |
1375 | ||
1376 | static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, | |
1377 | struct fb_cmap *cmap) | |
1378 | { | |
1379 | struct drm_device *dev = crtc->dev; | |
1380 | struct drm_property_blob *gamma_lut; | |
1381 | struct drm_color_lut *lut; | |
1382 | int size = crtc->gamma_size; | |
1383 | int i; | |
1384 | ||
1385 | if (!size || cmap->start + cmap->len > size) | |
1386 | return ERR_PTR(-EINVAL); | |
1387 | ||
1388 | gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); | |
1389 | if (IS_ERR(gamma_lut)) | |
1390 | return gamma_lut; | |
1391 | ||
11b83e3f | 1392 | lut = gamma_lut->data; |
964c6006 PR |
1393 | if (cmap->start || cmap->len != size) { |
1394 | u16 *r = crtc->gamma_store; | |
1395 | u16 *g = r + crtc->gamma_size; | |
1396 | u16 *b = g + crtc->gamma_size; | |
1397 | ||
1398 | for (i = 0; i < cmap->start; i++) { | |
1399 | lut[i].red = r[i]; | |
1400 | lut[i].green = g[i]; | |
1401 | lut[i].blue = b[i]; | |
1402 | } | |
1403 | for (i = cmap->start + cmap->len; i < size; i++) { | |
1404 | lut[i].red = r[i]; | |
1405 | lut[i].green = g[i]; | |
1406 | lut[i].blue = b[i]; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | for (i = 0; i < cmap->len; i++) { | |
1411 | lut[cmap->start + i].red = cmap->red[i]; | |
1412 | lut[cmap->start + i].green = cmap->green[i]; | |
1413 | lut[cmap->start + i].blue = cmap->blue[i]; | |
1414 | } | |
1415 | ||
1416 | return gamma_lut; | |
1417 | } | |
1418 | ||
1419 | static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) | |
1420 | { | |
1421 | struct drm_fb_helper *fb_helper = info->par; | |
1422 | struct drm_device *dev = fb_helper->dev; | |
1423 | struct drm_property_blob *gamma_lut = NULL; | |
1424 | struct drm_modeset_acquire_ctx ctx; | |
1425 | struct drm_crtc_state *crtc_state; | |
1426 | struct drm_atomic_state *state; | |
1427 | struct drm_crtc *crtc; | |
1428 | u16 *r, *g, *b; | |
1429 | int i, ret = 0; | |
1430 | bool replaced; | |
1431 | ||
1432 | drm_modeset_acquire_init(&ctx, 0); | |
1433 | ||
1434 | state = drm_atomic_state_alloc(dev); | |
1435 | if (!state) { | |
1436 | ret = -ENOMEM; | |
1437 | goto out_ctx; | |
b8e2b019 PR |
1438 | } |
1439 | ||
964c6006 PR |
1440 | state->acquire_ctx = &ctx; |
1441 | retry: | |
8be48d92 DA |
1442 | for (i = 0; i < fb_helper->crtc_count; i++) { |
1443 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
068143d3 | 1444 | |
964c6006 PR |
1445 | if (!gamma_lut) |
1446 | gamma_lut = setcmap_new_gamma_lut(crtc, cmap); | |
1447 | if (IS_ERR(gamma_lut)) { | |
1448 | ret = PTR_ERR(gamma_lut); | |
1449 | gamma_lut = NULL; | |
1450 | goto out_state; | |
a3562a0e PR |
1451 | } |
1452 | ||
964c6006 PR |
1453 | crtc_state = drm_atomic_get_crtc_state(state, crtc); |
1454 | if (IS_ERR(crtc_state)) { | |
1455 | ret = PTR_ERR(crtc_state); | |
1456 | goto out_state; | |
a3562a0e PR |
1457 | } |
1458 | ||
964c6006 PR |
1459 | replaced = drm_property_replace_blob(&crtc_state->degamma_lut, |
1460 | NULL); | |
1461 | replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); | |
1462 | replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, | |
1463 | gamma_lut); | |
1464 | crtc_state->color_mgmt_changed |= replaced; | |
1465 | } | |
1466 | ||
1467 | ret = drm_atomic_commit(state); | |
1468 | if (ret) | |
1469 | goto out_state; | |
1470 | ||
1471 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1472 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
1473 | ||
a3562a0e PR |
1474 | r = crtc->gamma_store; |
1475 | g = r + crtc->gamma_size; | |
1476 | b = g + crtc->gamma_size; | |
1477 | ||
1478 | memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); | |
1479 | memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); | |
1480 | memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); | |
964c6006 | 1481 | } |
a3562a0e | 1482 | |
964c6006 PR |
1483 | out_state: |
1484 | if (ret == -EDEADLK) | |
1485 | goto backoff; | |
068143d3 | 1486 | |
964c6006 PR |
1487 | drm_property_blob_put(gamma_lut); |
1488 | drm_atomic_state_put(state); | |
1489 | out_ctx: | |
1490 | drm_modeset_drop_locks(&ctx); | |
1491 | drm_modeset_acquire_fini(&ctx); | |
068143d3 | 1492 | |
964c6006 | 1493 | return ret; |
068143d3 | 1494 | |
964c6006 PR |
1495 | backoff: |
1496 | drm_atomic_state_clear(state); | |
1497 | drm_modeset_backoff(&ctx); | |
1498 | goto retry; | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap | |
1503 | * @cmap: cmap to set | |
1504 | * @info: fbdev registered by the helper | |
1505 | */ | |
1506 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) | |
1507 | { | |
1508 | struct drm_fb_helper *fb_helper = info->par; | |
1509 | int ret; | |
1510 | ||
1511 | if (oops_in_progress) | |
1512 | return -EBUSY; | |
1513 | ||
1514 | mutex_lock(&fb_helper->lock); | |
1515 | ||
1516 | if (!drm_fb_helper_is_bound(fb_helper)) { | |
1517 | ret = -EBUSY; | |
1518 | goto out; | |
068143d3 | 1519 | } |
964c6006 PR |
1520 | |
1521 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) | |
1522 | ret = setcmap_pseudo_palette(cmap, info); | |
1523 | else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) | |
1524 | ret = setcmap_atomic(cmap, info); | |
1525 | else | |
1526 | ret = setcmap_legacy(cmap, info); | |
1527 | ||
1528 | out: | |
e9827d8e | 1529 | mutex_unlock(&fb_helper->lock); |
964c6006 PR |
1530 | |
1531 | return ret; | |
068143d3 DA |
1532 | } |
1533 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
1534 | ||
0f3bbe07 MR |
1535 | /** |
1536 | * drm_fb_helper_ioctl - legacy ioctl implementation | |
1537 | * @info: fbdev registered by the helper | |
1538 | * @cmd: ioctl command | |
1539 | * @arg: ioctl argument | |
1540 | * | |
1541 | * A helper to implement the standard fbdev ioctl. Only | |
1542 | * FBIO_WAITFORVSYNC is implemented for now. | |
1543 | */ | |
1544 | int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, | |
1545 | unsigned long arg) | |
1546 | { | |
1547 | struct drm_fb_helper *fb_helper = info->par; | |
0f3bbe07 MR |
1548 | struct drm_mode_set *mode_set; |
1549 | struct drm_crtc *crtc; | |
1550 | int ret = 0; | |
1551 | ||
e9827d8e | 1552 | mutex_lock(&fb_helper->lock); |
0f3bbe07 MR |
1553 | if (!drm_fb_helper_is_bound(fb_helper)) { |
1554 | ret = -EBUSY; | |
1555 | goto unlock; | |
1556 | } | |
1557 | ||
1558 | switch (cmd) { | |
1559 | case FBIO_WAITFORVSYNC: | |
1560 | /* | |
1561 | * Only consider the first CRTC. | |
1562 | * | |
1563 | * This ioctl is supposed to take the CRTC number as | |
1564 | * an argument, but in fbdev times, what that number | |
1565 | * was supposed to be was quite unclear, different | |
1566 | * drivers were passing that argument differently | |
1567 | * (some by reference, some by value), and most of the | |
1568 | * userspace applications were just hardcoding 0 as an | |
1569 | * argument. | |
1570 | * | |
1571 | * The first CRTC should be the integrated panel on | |
1572 | * most drivers, so this is the best choice we can | |
1573 | * make. If we're not smart enough here, one should | |
1574 | * just consider switch the userspace to KMS. | |
1575 | */ | |
1576 | mode_set = &fb_helper->crtc_info[0].mode_set; | |
1577 | crtc = mode_set->crtc; | |
1578 | ||
1579 | /* | |
1580 | * Only wait for a vblank event if the CRTC is | |
1581 | * enabled, otherwise just don't do anythintg, | |
1582 | * not even report an error. | |
1583 | */ | |
1584 | ret = drm_crtc_vblank_get(crtc); | |
1585 | if (!ret) { | |
1586 | drm_crtc_wait_one_vblank(crtc); | |
1587 | drm_crtc_vblank_put(crtc); | |
1588 | } | |
1589 | ||
1590 | ret = 0; | |
1591 | goto unlock; | |
1592 | default: | |
1593 | ret = -ENOTTY; | |
1594 | } | |
1595 | ||
1596 | unlock: | |
e9827d8e | 1597 | mutex_unlock(&fb_helper->lock); |
0f3bbe07 MR |
1598 | return ret; |
1599 | } | |
1600 | EXPORT_SYMBOL(drm_fb_helper_ioctl); | |
1601 | ||
207fd329 | 1602 | /** |
6806cdf9 | 1603 | * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var |
207fd329 DV |
1604 | * @var: screeninfo to check |
1605 | * @info: fbdev registered by the helper | |
1606 | */ | |
785b93ef DA |
1607 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
1608 | struct fb_info *info) | |
1609 | { | |
1610 | struct drm_fb_helper *fb_helper = info->par; | |
1611 | struct drm_framebuffer *fb = fb_helper->fb; | |
1612 | int depth; | |
1613 | ||
f90ebd9e | 1614 | if (var->pixclock != 0 || in_dbg_master()) |
785b93ef DA |
1615 | return -EINVAL; |
1616 | ||
865afb11 SA |
1617 | /* |
1618 | * Changes struct fb_var_screeninfo are currently not pushed back | |
1619 | * to KMS, hence fail if different settings are requested. | |
1620 | */ | |
272725c7 | 1621 | if (var->bits_per_pixel != fb->format->cpp[0] * 8 || |
12ffed96 MD |
1622 | var->xres > fb->width || var->yres > fb->height || |
1623 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { | |
1624 | DRM_DEBUG("fb requested width/height/bpp can't fit in current fb " | |
62fb376e CW |
1625 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
1626 | var->xres, var->yres, var->bits_per_pixel, | |
1627 | var->xres_virtual, var->yres_virtual, | |
272725c7 | 1628 | fb->width, fb->height, fb->format->cpp[0] * 8); |
785b93ef DA |
1629 | return -EINVAL; |
1630 | } | |
1631 | ||
1632 | switch (var->bits_per_pixel) { | |
1633 | case 16: | |
1634 | depth = (var->green.length == 6) ? 16 : 15; | |
1635 | break; | |
1636 | case 32: | |
1637 | depth = (var->transp.length > 0) ? 32 : 24; | |
1638 | break; | |
1639 | default: | |
1640 | depth = var->bits_per_pixel; | |
1641 | break; | |
1642 | } | |
1643 | ||
1644 | switch (depth) { | |
1645 | case 8: | |
1646 | var->red.offset = 0; | |
1647 | var->green.offset = 0; | |
1648 | var->blue.offset = 0; | |
1649 | var->red.length = 8; | |
1650 | var->green.length = 8; | |
1651 | var->blue.length = 8; | |
1652 | var->transp.length = 0; | |
1653 | var->transp.offset = 0; | |
1654 | break; | |
1655 | case 15: | |
1656 | var->red.offset = 10; | |
1657 | var->green.offset = 5; | |
1658 | var->blue.offset = 0; | |
1659 | var->red.length = 5; | |
1660 | var->green.length = 5; | |
1661 | var->blue.length = 5; | |
1662 | var->transp.length = 1; | |
1663 | var->transp.offset = 15; | |
1664 | break; | |
1665 | case 16: | |
1666 | var->red.offset = 11; | |
1667 | var->green.offset = 5; | |
1668 | var->blue.offset = 0; | |
1669 | var->red.length = 5; | |
1670 | var->green.length = 6; | |
1671 | var->blue.length = 5; | |
1672 | var->transp.length = 0; | |
1673 | var->transp.offset = 0; | |
1674 | break; | |
1675 | case 24: | |
1676 | var->red.offset = 16; | |
1677 | var->green.offset = 8; | |
1678 | var->blue.offset = 0; | |
1679 | var->red.length = 8; | |
1680 | var->green.length = 8; | |
1681 | var->blue.length = 8; | |
1682 | var->transp.length = 0; | |
1683 | var->transp.offset = 0; | |
1684 | break; | |
1685 | case 32: | |
1686 | var->red.offset = 16; | |
1687 | var->green.offset = 8; | |
1688 | var->blue.offset = 0; | |
1689 | var->red.length = 8; | |
1690 | var->green.length = 8; | |
1691 | var->blue.length = 8; | |
1692 | var->transp.length = 8; | |
1693 | var->transp.offset = 24; | |
1694 | break; | |
1695 | default: | |
1696 | return -EINVAL; | |
1697 | } | |
1698 | return 0; | |
1699 | } | |
1700 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
1701 | ||
207fd329 | 1702 | /** |
6806cdf9 | 1703 | * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par |
207fd329 DV |
1704 | * @info: fbdev registered by the helper |
1705 | * | |
1706 | * This will let fbcon do the mode init and is called at initialization time by | |
1707 | * the fbdev core when registering the driver, and later on through the hotplug | |
1708 | * callback. | |
1709 | */ | |
785b93ef DA |
1710 | int drm_fb_helper_set_par(struct fb_info *info) |
1711 | { | |
1712 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 1713 | struct fb_var_screeninfo *var = &info->var; |
785b93ef | 1714 | |
c50bfd08 DV |
1715 | if (oops_in_progress) |
1716 | return -EBUSY; | |
1717 | ||
5349ef31 | 1718 | if (var->pixclock != 0) { |
172e91f5 | 1719 | DRM_ERROR("PIXEL CLOCK SET\n"); |
785b93ef DA |
1720 | return -EINVAL; |
1721 | } | |
1722 | ||
5ea1f752 | 1723 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
4abe3520 | 1724 | |
785b93ef DA |
1725 | return 0; |
1726 | } | |
1727 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
1728 | ||
6b7dc6e9 | 1729 | static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) |
1edf0269 | 1730 | { |
6b7dc6e9 | 1731 | int i; |
1edf0269 | 1732 | |
4b4f99f5 | 1733 | for (i = 0; i < fb_helper->crtc_count; i++) { |
1edf0269 RC |
1734 | struct drm_mode_set *mode_set; |
1735 | ||
1736 | mode_set = &fb_helper->crtc_info[i].mode_set; | |
1737 | ||
6b7dc6e9 DV |
1738 | mode_set->x = x; |
1739 | mode_set->y = y; | |
1edf0269 | 1740 | } |
6b7dc6e9 | 1741 | } |
1edf0269 | 1742 | |
6b7dc6e9 DV |
1743 | static int pan_display_atomic(struct fb_var_screeninfo *var, |
1744 | struct fb_info *info) | |
1745 | { | |
1746 | struct drm_fb_helper *fb_helper = info->par; | |
1747 | int ret; | |
a0fb6ad7 | 1748 | |
6b7dc6e9 | 1749 | pan_set(fb_helper, var->xoffset, var->yoffset); |
1edf0269 | 1750 | |
6b7dc6e9 DV |
1751 | ret = restore_fbdev_mode_atomic(fb_helper, true); |
1752 | if (!ret) { | |
1753 | info->var.xoffset = var->xoffset; | |
1754 | info->var.yoffset = var->yoffset; | |
1755 | } else | |
1756 | pan_set(fb_helper, info->var.xoffset, info->var.yoffset); | |
5c2e3448 | 1757 | |
1edf0269 | 1758 | return ret; |
1edf0269 RC |
1759 | } |
1760 | ||
7128645d | 1761 | static int pan_display_legacy(struct fb_var_screeninfo *var, |
785b93ef DA |
1762 | struct fb_info *info) |
1763 | { | |
1764 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 1765 | struct drm_mode_set *modeset; |
785b93ef DA |
1766 | int ret = 0; |
1767 | int i; | |
1768 | ||
5c2e3448 | 1769 | drm_modeset_lock_all(fb_helper->dev); |
8be48d92 | 1770 | for (i = 0; i < fb_helper->crtc_count; i++) { |
785b93ef DA |
1771 | modeset = &fb_helper->crtc_info[i].mode_set; |
1772 | ||
1773 | modeset->x = var->xoffset; | |
1774 | modeset->y = var->yoffset; | |
1775 | ||
1776 | if (modeset->num_connectors) { | |
2d13b679 | 1777 | ret = drm_mode_set_config_internal(modeset); |
785b93ef DA |
1778 | if (!ret) { |
1779 | info->var.xoffset = var->xoffset; | |
1780 | info->var.yoffset = var->yoffset; | |
1781 | } | |
1782 | } | |
1783 | } | |
5c2e3448 | 1784 | drm_modeset_unlock_all(fb_helper->dev); |
7128645d DV |
1785 | |
1786 | return ret; | |
1787 | } | |
1788 | ||
1789 | /** | |
1790 | * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display | |
1791 | * @var: updated screen information | |
1792 | * @info: fbdev registered by the helper | |
1793 | */ | |
1794 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, | |
1795 | struct fb_info *info) | |
1796 | { | |
1797 | struct drm_fb_helper *fb_helper = info->par; | |
1798 | struct drm_device *dev = fb_helper->dev; | |
1799 | int ret; | |
1800 | ||
1801 | if (oops_in_progress) | |
1802 | return -EBUSY; | |
1803 | ||
e9827d8e | 1804 | mutex_lock(&fb_helper->lock); |
7128645d | 1805 | if (!drm_fb_helper_is_bound(fb_helper)) { |
e9827d8e | 1806 | mutex_unlock(&fb_helper->lock); |
7128645d DV |
1807 | return -EBUSY; |
1808 | } | |
1809 | ||
1810 | if (drm_drv_uses_atomic_modeset(dev)) | |
1811 | ret = pan_display_atomic(var, info); | |
1812 | else | |
1813 | ret = pan_display_legacy(var, info); | |
e9827d8e | 1814 | mutex_unlock(&fb_helper->lock); |
7128645d | 1815 | |
785b93ef DA |
1816 | return ret; |
1817 | } | |
1818 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
1819 | ||
8acf658a | 1820 | /* |
207fd329 | 1821 | * Allocates the backing storage and sets up the fbdev info structure through |
ca91a275 | 1822 | * the ->fb_probe callback. |
8acf658a | 1823 | */ |
de1ace5b DV |
1824 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
1825 | int preferred_bpp) | |
785b93ef | 1826 | { |
8acf658a | 1827 | int ret = 0; |
785b93ef | 1828 | int crtc_count = 0; |
4abe3520 | 1829 | int i; |
38651674 | 1830 | struct drm_fb_helper_surface_size sizes; |
8be48d92 | 1831 | int gamma_size = 0; |
38651674 DA |
1832 | |
1833 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
1834 | sizes.surface_depth = 24; | |
1835 | sizes.surface_bpp = 32; | |
4b4f99f5 TR |
1836 | sizes.fb_width = (u32)-1; |
1837 | sizes.fb_height = (u32)-1; | |
785b93ef | 1838 | |
4b4f99f5 | 1839 | /* if driver picks 8 or 16 by default use that for both depth/bpp */ |
96081cdf | 1840 | if (preferred_bpp != sizes.surface_bpp) |
38651674 | 1841 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
96081cdf | 1842 | |
785b93ef | 1843 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
966a6a13 | 1844 | drm_fb_helper_for_each_connector(fb_helper, i) { |
0b4c0f3f | 1845 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; |
1794d257 | 1846 | struct drm_cmdline_mode *cmdline_mode; |
8ef8678c | 1847 | |
eaf99c74 | 1848 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
d50ba256 DA |
1849 | |
1850 | if (cmdline_mode->bpp_specified) { | |
1851 | switch (cmdline_mode->bpp) { | |
1852 | case 8: | |
38651674 | 1853 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
1854 | break; |
1855 | case 15: | |
38651674 DA |
1856 | sizes.surface_depth = 15; |
1857 | sizes.surface_bpp = 16; | |
d50ba256 DA |
1858 | break; |
1859 | case 16: | |
38651674 | 1860 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
1861 | break; |
1862 | case 24: | |
38651674 | 1863 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
1864 | break; |
1865 | case 32: | |
38651674 DA |
1866 | sizes.surface_depth = 24; |
1867 | sizes.surface_bpp = 32; | |
d50ba256 DA |
1868 | break; |
1869 | } | |
1870 | break; | |
1871 | } | |
1872 | } | |
1873 | ||
8be48d92 DA |
1874 | crtc_count = 0; |
1875 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1876 | struct drm_display_mode *desired_mode; | |
0e3704c9 RC |
1877 | struct drm_mode_set *mode_set; |
1878 | int x, y, j; | |
1879 | /* in case of tile group, are we the last tile vert or horiz? | |
1880 | * If no tile group you are always the last one both vertically | |
1881 | * and horizontally | |
1882 | */ | |
1883 | bool lastv = true, lasth = true; | |
675c8328 | 1884 | |
8be48d92 | 1885 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
0e3704c9 | 1886 | mode_set = &fb_helper->crtc_info[i].mode_set; |
675c8328 RC |
1887 | |
1888 | if (!desired_mode) | |
1889 | continue; | |
1890 | ||
1891 | crtc_count++; | |
1892 | ||
b0ee9e7f DA |
1893 | x = fb_helper->crtc_info[i].x; |
1894 | y = fb_helper->crtc_info[i].y; | |
675c8328 RC |
1895 | |
1896 | if (gamma_size == 0) | |
1897 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; | |
1898 | ||
1899 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); | |
1900 | sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); | |
0e3704c9 RC |
1901 | |
1902 | for (j = 0; j < mode_set->num_connectors; j++) { | |
1903 | struct drm_connector *connector = mode_set->connectors[j]; | |
4b4f99f5 | 1904 | |
0e3704c9 RC |
1905 | if (connector->has_tile) { |
1906 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); | |
1907 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); | |
1908 | /* cloning to multiple tiles is just crazy-talk, so: */ | |
1909 | break; | |
1910 | } | |
1911 | } | |
1912 | ||
1913 | if (lasth) | |
1914 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); | |
1915 | if (lastv) | |
1916 | sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); | |
785b93ef DA |
1917 | } |
1918 | ||
38651674 | 1919 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
ca91a275 | 1920 | DRM_INFO("Cannot find any crtc or sizes\n"); |
52dd0650 ML |
1921 | |
1922 | /* First time: disable all crtc's.. */ | |
1923 | if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master)) | |
1924 | restore_fbdev_mode(fb_helper); | |
ca91a275 | 1925 | return -EAGAIN; |
785b93ef DA |
1926 | } |
1927 | ||
5f152576 XL |
1928 | /* Handle our overallocation */ |
1929 | sizes.surface_height *= drm_fbdev_overalloc; | |
1930 | sizes.surface_height /= 100; | |
1931 | ||
38651674 | 1932 | /* push down into drivers */ |
8acf658a DV |
1933 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
1934 | if (ret < 0) | |
1935 | return ret; | |
785b93ef | 1936 | |
8d44e9e6 | 1937 | strcpy(fb_helper->fb->comm, "[fbcon]"); |
785b93ef DA |
1938 | return 0; |
1939 | } | |
785b93ef | 1940 | |
207fd329 DV |
1941 | /** |
1942 | * drm_fb_helper_fill_fix - initializes fixed fbdev information | |
1943 | * @info: fbdev registered by the helper | |
1944 | * @pitch: desired pitch | |
1945 | * @depth: desired depth | |
1946 | * | |
1947 | * Helper to fill in the fixed fbdev information useful for a non-accelerated | |
1948 | * fbdev emulations. Drivers which support acceleration methods which impose | |
1949 | * additional constraints need to set up their own limits. | |
1950 | * | |
1951 | * Drivers should call this (or their equivalent setup code) from their | |
6806cdf9 | 1952 | * &drm_fb_helper_funcs.fb_probe callback. |
207fd329 | 1953 | */ |
3632ef89 DA |
1954 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
1955 | uint32_t depth) | |
1956 | { | |
1957 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
1958 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : | |
1959 | FB_VISUAL_TRUECOLOR; | |
1960 | info->fix.mmio_start = 0; | |
1961 | info->fix.mmio_len = 0; | |
1962 | info->fix.type_aux = 0; | |
1963 | info->fix.xpanstep = 1; /* doing it in hw */ | |
1964 | info->fix.ypanstep = 1; /* doing it in hw */ | |
1965 | info->fix.ywrapstep = 0; | |
1966 | info->fix.accel = FB_ACCEL_NONE; | |
3632ef89 DA |
1967 | |
1968 | info->fix.line_length = pitch; | |
3632ef89 DA |
1969 | } |
1970 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); | |
1971 | ||
207fd329 DV |
1972 | /** |
1973 | * drm_fb_helper_fill_var - initalizes variable fbdev information | |
1974 | * @info: fbdev instance to set up | |
1975 | * @fb_helper: fb helper instance to use as template | |
1976 | * @fb_width: desired fb width | |
1977 | * @fb_height: desired fb height | |
1978 | * | |
1979 | * Sets up the variable fbdev metainformation from the given fb helper instance | |
6806cdf9 | 1980 | * and the drm framebuffer allocated in &drm_fb_helper.fb. |
207fd329 DV |
1981 | * |
1982 | * Drivers should call this (or their equivalent setup code) from their | |
6806cdf9 DV |
1983 | * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev |
1984 | * backing storage framebuffer. | |
207fd329 | 1985 | */ |
38651674 | 1986 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
785b93ef DA |
1987 | uint32_t fb_width, uint32_t fb_height) |
1988 | { | |
38651674 | 1989 | struct drm_framebuffer *fb = fb_helper->fb; |
4b4f99f5 | 1990 | |
38651674 | 1991 | info->pseudo_palette = fb_helper->pseudo_palette; |
785b93ef DA |
1992 | info->var.xres_virtual = fb->width; |
1993 | info->var.yres_virtual = fb->height; | |
272725c7 | 1994 | info->var.bits_per_pixel = fb->format->cpp[0] * 8; |
57084d05 | 1995 | info->var.accel_flags = FB_ACCELF_TEXT; |
785b93ef DA |
1996 | info->var.xoffset = 0; |
1997 | info->var.yoffset = 0; | |
1998 | info->var.activate = FB_ACTIVATE_NOW; | |
785b93ef | 1999 | |
b00c600e | 2000 | switch (fb->format->depth) { |
785b93ef DA |
2001 | case 8: |
2002 | info->var.red.offset = 0; | |
2003 | info->var.green.offset = 0; | |
2004 | info->var.blue.offset = 0; | |
2005 | info->var.red.length = 8; /* 8bit DAC */ | |
2006 | info->var.green.length = 8; | |
2007 | info->var.blue.length = 8; | |
2008 | info->var.transp.offset = 0; | |
2009 | info->var.transp.length = 0; | |
2010 | break; | |
2011 | case 15: | |
2012 | info->var.red.offset = 10; | |
2013 | info->var.green.offset = 5; | |
2014 | info->var.blue.offset = 0; | |
2015 | info->var.red.length = 5; | |
2016 | info->var.green.length = 5; | |
2017 | info->var.blue.length = 5; | |
2018 | info->var.transp.offset = 15; | |
2019 | info->var.transp.length = 1; | |
2020 | break; | |
2021 | case 16: | |
2022 | info->var.red.offset = 11; | |
2023 | info->var.green.offset = 5; | |
2024 | info->var.blue.offset = 0; | |
2025 | info->var.red.length = 5; | |
2026 | info->var.green.length = 6; | |
2027 | info->var.blue.length = 5; | |
2028 | info->var.transp.offset = 0; | |
2029 | break; | |
2030 | case 24: | |
2031 | info->var.red.offset = 16; | |
2032 | info->var.green.offset = 8; | |
2033 | info->var.blue.offset = 0; | |
2034 | info->var.red.length = 8; | |
2035 | info->var.green.length = 8; | |
2036 | info->var.blue.length = 8; | |
2037 | info->var.transp.offset = 0; | |
2038 | info->var.transp.length = 0; | |
2039 | break; | |
2040 | case 32: | |
2041 | info->var.red.offset = 16; | |
2042 | info->var.green.offset = 8; | |
2043 | info->var.blue.offset = 0; | |
2044 | info->var.red.length = 8; | |
2045 | info->var.green.length = 8; | |
2046 | info->var.blue.length = 8; | |
2047 | info->var.transp.offset = 24; | |
2048 | info->var.transp.length = 8; | |
2049 | break; | |
2050 | default: | |
2051 | break; | |
2052 | } | |
2053 | ||
2054 | info->var.xres = fb_width; | |
2055 | info->var.yres = fb_height; | |
2056 | } | |
2057 | EXPORT_SYMBOL(drm_fb_helper_fill_var); | |
38651674 | 2058 | |
0b4c0f3f | 2059 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
e13a0583 DV |
2060 | uint32_t maxX, |
2061 | uint32_t maxY) | |
38651674 DA |
2062 | { |
2063 | struct drm_connector *connector; | |
e13a0583 | 2064 | int i, count = 0; |
38651674 | 2065 | |
966a6a13 | 2066 | drm_fb_helper_for_each_connector(fb_helper, i) { |
0b4c0f3f | 2067 | connector = fb_helper->connector_info[i]->connector; |
38651674 DA |
2068 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
2069 | } | |
2070 | ||
2071 | return count; | |
2072 | } | |
2073 | ||
2f1046f3 | 2074 | struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
38651674 DA |
2075 | { |
2076 | struct drm_display_mode *mode; | |
2077 | ||
0b4c0f3f | 2078 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
9d3de138 DV |
2079 | if (mode->hdisplay > width || |
2080 | mode->vdisplay > height) | |
38651674 DA |
2081 | continue; |
2082 | if (mode->type & DRM_MODE_TYPE_PREFERRED) | |
2083 | return mode; | |
2084 | } | |
2085 | return NULL; | |
2086 | } | |
2f1046f3 | 2087 | EXPORT_SYMBOL(drm_has_preferred_mode); |
38651674 | 2088 | |
0b4c0f3f | 2089 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
38651674 | 2090 | { |
eaf99c74 | 2091 | return fb_connector->connector->cmdline_mode.specified; |
38651674 DA |
2092 | } |
2093 | ||
a09759e8 | 2094 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn) |
38651674 | 2095 | { |
1794d257 | 2096 | struct drm_cmdline_mode *cmdline_mode; |
f3af5c7d | 2097 | struct drm_display_mode *mode; |
c683f427 | 2098 | bool prefer_non_interlace; |
38651674 | 2099 | |
eaf99c74 | 2100 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
38651674 | 2101 | if (cmdline_mode->specified == false) |
f3af5c7d | 2102 | return NULL; |
38651674 DA |
2103 | |
2104 | /* attempt to find a matching mode in the list of modes | |
2105 | * we have gotten so far, if not add a CVT mode that conforms | |
2106 | */ | |
2107 | if (cmdline_mode->rb || cmdline_mode->margins) | |
2108 | goto create_mode; | |
2109 | ||
c683f427 | 2110 | prefer_non_interlace = !cmdline_mode->interlace; |
f3af5c7d | 2111 | again: |
0b4c0f3f | 2112 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
38651674 DA |
2113 | /* check width/height */ |
2114 | if (mode->hdisplay != cmdline_mode->xres || | |
2115 | mode->vdisplay != cmdline_mode->yres) | |
2116 | continue; | |
2117 | ||
2118 | if (cmdline_mode->refresh_specified) { | |
2119 | if (mode->vrefresh != cmdline_mode->refresh) | |
2120 | continue; | |
2121 | } | |
2122 | ||
2123 | if (cmdline_mode->interlace) { | |
2124 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) | |
2125 | continue; | |
c683f427 TI |
2126 | } else if (prefer_non_interlace) { |
2127 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) | |
2128 | continue; | |
38651674 DA |
2129 | } |
2130 | return mode; | |
2131 | } | |
2132 | ||
c683f427 TI |
2133 | if (prefer_non_interlace) { |
2134 | prefer_non_interlace = false; | |
2135 | goto again; | |
2136 | } | |
2137 | ||
38651674 | 2138 | create_mode: |
1794d257 CW |
2139 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
2140 | cmdline_mode); | |
0b4c0f3f | 2141 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
38651674 DA |
2142 | return mode; |
2143 | } | |
2f1046f3 | 2144 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
38651674 DA |
2145 | |
2146 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) | |
2147 | { | |
2148 | bool enable; | |
2149 | ||
b5f05388 DA |
2150 | if (connector->display_info.non_desktop) |
2151 | return false; | |
2152 | ||
96081cdf | 2153 | if (strict) |
38651674 | 2154 | enable = connector->status == connector_status_connected; |
96081cdf | 2155 | else |
38651674 | 2156 | enable = connector->status != connector_status_disconnected; |
96081cdf | 2157 | |
38651674 DA |
2158 | return enable; |
2159 | } | |
2160 | ||
0b4c0f3f DA |
2161 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
2162 | bool *enabled) | |
38651674 DA |
2163 | { |
2164 | bool any_enabled = false; | |
2165 | struct drm_connector *connector; | |
2166 | int i = 0; | |
2167 | ||
966a6a13 | 2168 | drm_fb_helper_for_each_connector(fb_helper, i) { |
0b4c0f3f | 2169 | connector = fb_helper->connector_info[i]->connector; |
38651674 DA |
2170 | enabled[i] = drm_connector_enabled(connector, true); |
2171 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, | |
b5f05388 DA |
2172 | connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no"); |
2173 | ||
38651674 | 2174 | any_enabled |= enabled[i]; |
38651674 DA |
2175 | } |
2176 | ||
2177 | if (any_enabled) | |
2178 | return; | |
2179 | ||
966a6a13 | 2180 | drm_fb_helper_for_each_connector(fb_helper, i) { |
0b4c0f3f | 2181 | connector = fb_helper->connector_info[i]->connector; |
38651674 | 2182 | enabled[i] = drm_connector_enabled(connector, false); |
38651674 DA |
2183 | } |
2184 | } | |
2185 | ||
1d42bbc8 DA |
2186 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
2187 | struct drm_display_mode **modes, | |
b0ee9e7f | 2188 | struct drm_fb_offset *offsets, |
1d42bbc8 DA |
2189 | bool *enabled, int width, int height) |
2190 | { | |
2191 | int count, i, j; | |
2192 | bool can_clone = false; | |
2193 | struct drm_fb_helper_connector *fb_helper_conn; | |
2194 | struct drm_display_mode *dmt_mode, *mode; | |
2195 | ||
2196 | /* only contemplate cloning in the single crtc case */ | |
2197 | if (fb_helper->crtc_count > 1) | |
2198 | return false; | |
2199 | ||
2200 | count = 0; | |
966a6a13 | 2201 | drm_fb_helper_for_each_connector(fb_helper, i) { |
1d42bbc8 DA |
2202 | if (enabled[i]) |
2203 | count++; | |
2204 | } | |
2205 | ||
2206 | /* only contemplate cloning if more than one connector is enabled */ | |
2207 | if (count <= 1) | |
2208 | return false; | |
2209 | ||
2210 | /* check the command line or if nothing common pick 1024x768 */ | |
2211 | can_clone = true; | |
966a6a13 | 2212 | drm_fb_helper_for_each_connector(fb_helper, i) { |
1d42bbc8 DA |
2213 | if (!enabled[i]) |
2214 | continue; | |
2215 | fb_helper_conn = fb_helper->connector_info[i]; | |
a09759e8 | 2216 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
1d42bbc8 DA |
2217 | if (!modes[i]) { |
2218 | can_clone = false; | |
2219 | break; | |
2220 | } | |
2221 | for (j = 0; j < i; j++) { | |
2222 | if (!enabled[j]) | |
2223 | continue; | |
222ec161 SS |
2224 | if (!drm_mode_match(modes[j], modes[i], |
2225 | DRM_MODE_MATCH_TIMINGS | | |
2226 | DRM_MODE_MATCH_CLOCK | | |
2227 | DRM_MODE_MATCH_FLAGS | | |
2228 | DRM_MODE_MATCH_3D_FLAGS)) | |
1d42bbc8 DA |
2229 | can_clone = false; |
2230 | } | |
2231 | } | |
2232 | ||
2233 | if (can_clone) { | |
2234 | DRM_DEBUG_KMS("can clone using command line\n"); | |
2235 | return true; | |
2236 | } | |
2237 | ||
2238 | /* try and find a 1024x768 mode on each connector */ | |
2239 | can_clone = true; | |
f6e252ba | 2240 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
1d42bbc8 | 2241 | |
966a6a13 | 2242 | drm_fb_helper_for_each_connector(fb_helper, i) { |
1d42bbc8 DA |
2243 | if (!enabled[i]) |
2244 | continue; | |
2245 | ||
2246 | fb_helper_conn = fb_helper->connector_info[i]; | |
2247 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { | |
222ec161 SS |
2248 | if (drm_mode_match(mode, dmt_mode, |
2249 | DRM_MODE_MATCH_TIMINGS | | |
2250 | DRM_MODE_MATCH_CLOCK | | |
2251 | DRM_MODE_MATCH_FLAGS | | |
2252 | DRM_MODE_MATCH_3D_FLAGS)) | |
1d42bbc8 DA |
2253 | modes[i] = mode; |
2254 | } | |
2255 | if (!modes[i]) | |
2256 | can_clone = false; | |
2257 | } | |
2258 | ||
2259 | if (can_clone) { | |
2260 | DRM_DEBUG_KMS("can clone using 1024x768\n"); | |
2261 | return true; | |
2262 | } | |
2263 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); | |
2264 | return false; | |
2265 | } | |
2266 | ||
b0ee9e7f DA |
2267 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
2268 | struct drm_display_mode **modes, | |
2269 | struct drm_fb_offset *offsets, | |
2270 | int idx, | |
2271 | int h_idx, int v_idx) | |
2272 | { | |
2273 | struct drm_fb_helper_connector *fb_helper_conn; | |
2274 | int i; | |
2275 | int hoffset = 0, voffset = 0; | |
2276 | ||
966a6a13 | 2277 | drm_fb_helper_for_each_connector(fb_helper, i) { |
b0ee9e7f DA |
2278 | fb_helper_conn = fb_helper->connector_info[i]; |
2279 | if (!fb_helper_conn->connector->has_tile) | |
2280 | continue; | |
2281 | ||
2282 | if (!modes[i] && (h_idx || v_idx)) { | |
2283 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, | |
2284 | fb_helper_conn->connector->base.id); | |
2285 | continue; | |
2286 | } | |
2287 | if (fb_helper_conn->connector->tile_h_loc < h_idx) | |
2288 | hoffset += modes[i]->hdisplay; | |
2289 | ||
2290 | if (fb_helper_conn->connector->tile_v_loc < v_idx) | |
2291 | voffset += modes[i]->vdisplay; | |
2292 | } | |
2293 | offsets[idx].x = hoffset; | |
2294 | offsets[idx].y = voffset; | |
2295 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); | |
2296 | return 0; | |
2297 | } | |
2298 | ||
0b4c0f3f | 2299 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
38651674 | 2300 | struct drm_display_mode **modes, |
b0ee9e7f | 2301 | struct drm_fb_offset *offsets, |
38651674 DA |
2302 | bool *enabled, int width, int height) |
2303 | { | |
0b4c0f3f | 2304 | struct drm_fb_helper_connector *fb_helper_conn; |
c96521ee CW |
2305 | const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; |
2306 | u64 conn_configured = 0; | |
b0ee9e7f | 2307 | int tile_pass = 0; |
c96521ee CW |
2308 | int i; |
2309 | ||
b0ee9e7f | 2310 | retry: |
966a6a13 | 2311 | drm_fb_helper_for_each_connector(fb_helper, i) { |
0b4c0f3f | 2312 | fb_helper_conn = fb_helper->connector_info[i]; |
38651674 | 2313 | |
c96521ee | 2314 | if (conn_configured & BIT_ULL(i)) |
b0ee9e7f DA |
2315 | continue; |
2316 | ||
2317 | if (enabled[i] == false) { | |
c96521ee | 2318 | conn_configured |= BIT_ULL(i); |
b0ee9e7f DA |
2319 | continue; |
2320 | } | |
2321 | ||
2322 | /* first pass over all the untiled connectors */ | |
2323 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) | |
38651674 | 2324 | continue; |
38651674 | 2325 | |
b0ee9e7f DA |
2326 | if (tile_pass == 1) { |
2327 | if (fb_helper_conn->connector->tile_h_loc != 0 || | |
2328 | fb_helper_conn->connector->tile_v_loc != 0) | |
2329 | continue; | |
2330 | ||
2331 | } else { | |
4b4f99f5 | 2332 | if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && |
b0ee9e7f DA |
2333 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) |
2334 | /* if this tile_pass doesn't cover any of the tiles - keep going */ | |
2335 | continue; | |
2336 | ||
4b4f99f5 TR |
2337 | /* |
2338 | * find the tile offsets for this pass - need to find | |
2339 | * all tiles left and above | |
2340 | */ | |
b0ee9e7f DA |
2341 | drm_get_tile_offsets(fb_helper, modes, offsets, |
2342 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); | |
2343 | } | |
38651674 | 2344 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
0b4c0f3f | 2345 | fb_helper_conn->connector->base.id); |
38651674 DA |
2346 | |
2347 | /* got for command line mode first */ | |
a09759e8 | 2348 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn); |
38651674 | 2349 | if (!modes[i]) { |
b0ee9e7f DA |
2350 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
2351 | fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); | |
0b4c0f3f | 2352 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
38651674 DA |
2353 | } |
2354 | /* No preferred modes, pick one off the list */ | |
0b4c0f3f DA |
2355 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
2356 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) | |
38651674 DA |
2357 | break; |
2358 | } | |
2359 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : | |
2360 | "none"); | |
c96521ee | 2361 | conn_configured |= BIT_ULL(i); |
b0ee9e7f DA |
2362 | } |
2363 | ||
2364 | if ((conn_configured & mask) != mask) { | |
2365 | tile_pass++; | |
2366 | goto retry; | |
38651674 DA |
2367 | } |
2368 | return true; | |
2369 | } | |
2370 | ||
0d998891 VS |
2371 | static bool connector_has_possible_crtc(struct drm_connector *connector, |
2372 | struct drm_crtc *crtc) | |
2373 | { | |
83aefbb8 | 2374 | struct drm_encoder *encoder; |
0d998891 VS |
2375 | int i; |
2376 | ||
83aefbb8 | 2377 | drm_connector_for_each_possible_encoder(connector, encoder, i) { |
0d998891 VS |
2378 | if (encoder->possible_crtcs & drm_crtc_mask(crtc)) |
2379 | return true; | |
2380 | } | |
2381 | ||
2382 | return false; | |
2383 | } | |
2384 | ||
8be48d92 DA |
2385 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
2386 | struct drm_fb_helper_crtc **best_crtcs, | |
38651674 DA |
2387 | struct drm_display_mode **modes, |
2388 | int n, int width, int height) | |
2389 | { | |
2390 | int c, o; | |
2391 | struct drm_connector *connector; | |
38651674 | 2392 | int my_score, best_score, score; |
8be48d92 | 2393 | struct drm_fb_helper_crtc **crtcs, *crtc; |
0b4c0f3f | 2394 | struct drm_fb_helper_connector *fb_helper_conn; |
38651674 | 2395 | |
0b4c0f3f | 2396 | if (n == fb_helper->connector_count) |
38651674 | 2397 | return 0; |
0b4c0f3f DA |
2398 | |
2399 | fb_helper_conn = fb_helper->connector_info[n]; | |
2400 | connector = fb_helper_conn->connector; | |
38651674 DA |
2401 | |
2402 | best_crtcs[n] = NULL; | |
8be48d92 | 2403 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
38651674 DA |
2404 | if (modes[n] == NULL) |
2405 | return best_score; | |
2406 | ||
4b947b1c | 2407 | crtcs = kcalloc(fb_helper->connector_count, |
8be48d92 | 2408 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
38651674 DA |
2409 | if (!crtcs) |
2410 | return best_score; | |
2411 | ||
2412 | my_score = 1; | |
2413 | if (connector->status == connector_status_connected) | |
2414 | my_score++; | |
0b4c0f3f | 2415 | if (drm_has_cmdline_mode(fb_helper_conn)) |
38651674 | 2416 | my_score++; |
0b4c0f3f | 2417 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
38651674 DA |
2418 | my_score++; |
2419 | ||
4b4f99f5 TR |
2420 | /* |
2421 | * select a crtc for this connector and then attempt to configure | |
2422 | * remaining connectors | |
2423 | */ | |
8be48d92 DA |
2424 | for (c = 0; c < fb_helper->crtc_count; c++) { |
2425 | crtc = &fb_helper->crtc_info[c]; | |
38651674 | 2426 | |
0d998891 VS |
2427 | if (!connector_has_possible_crtc(connector, |
2428 | crtc->mode_set.crtc)) | |
38651674 | 2429 | continue; |
38651674 DA |
2430 | |
2431 | for (o = 0; o < n; o++) | |
2432 | if (best_crtcs[o] == crtc) | |
2433 | break; | |
2434 | ||
2435 | if (o < n) { | |
1d42bbc8 DA |
2436 | /* ignore cloning unless only a single crtc */ |
2437 | if (fb_helper->crtc_count > 1) | |
2438 | continue; | |
2439 | ||
2440 | if (!drm_mode_equal(modes[o], modes[n])) | |
2441 | continue; | |
38651674 DA |
2442 | } |
2443 | ||
2444 | crtcs[n] = crtc; | |
8be48d92 DA |
2445 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
2446 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, | |
38651674 DA |
2447 | width, height); |
2448 | if (score > best_score) { | |
38651674 DA |
2449 | best_score = score; |
2450 | memcpy(best_crtcs, crtcs, | |
255f0e7c | 2451 | fb_helper->connector_count * |
8be48d92 | 2452 | sizeof(struct drm_fb_helper_crtc *)); |
38651674 | 2453 | } |
38651674 | 2454 | } |
0d998891 | 2455 | |
38651674 DA |
2456 | kfree(crtcs); |
2457 | return best_score; | |
2458 | } | |
2459 | ||
8f0cb418 HG |
2460 | /* |
2461 | * This function checks if rotation is necessary because of panel orientation | |
2462 | * and if it is, if it is supported. | |
2463 | * If rotation is necessary and supported, its gets set in fb_crtc.rotation. | |
2464 | * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets | |
2465 | * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only | |
2466 | * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do | |
2467 | * the unsupported rotation. | |
2468 | */ | |
2469 | static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper, | |
2470 | struct drm_fb_helper_crtc *fb_crtc, | |
2471 | struct drm_connector *connector) | |
2472 | { | |
2473 | struct drm_plane *plane = fb_crtc->mode_set.crtc->primary; | |
2474 | uint64_t valid_mask = 0; | |
2475 | int i, rotation; | |
2476 | ||
2477 | fb_crtc->rotation = DRM_MODE_ROTATE_0; | |
2478 | ||
2479 | switch (connector->display_info.panel_orientation) { | |
2480 | case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP: | |
2481 | rotation = DRM_MODE_ROTATE_180; | |
2482 | break; | |
2483 | case DRM_MODE_PANEL_ORIENTATION_LEFT_UP: | |
2484 | rotation = DRM_MODE_ROTATE_90; | |
2485 | break; | |
2486 | case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP: | |
2487 | rotation = DRM_MODE_ROTATE_270; | |
2488 | break; | |
2489 | default: | |
2490 | rotation = DRM_MODE_ROTATE_0; | |
2491 | } | |
2492 | ||
2493 | /* | |
2494 | * TODO: support 90 / 270 degree hardware rotation, | |
2495 | * depending on the hardware this may require the framebuffer | |
2496 | * to be in a specific tiling format. | |
2497 | */ | |
2498 | if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) { | |
2499 | fb_helper->sw_rotations |= rotation; | |
2500 | return; | |
2501 | } | |
2502 | ||
2503 | for (i = 0; i < plane->rotation_property->num_values; i++) | |
2504 | valid_mask |= (1ULL << plane->rotation_property->values[i]); | |
2505 | ||
2506 | if (!(rotation & valid_mask)) { | |
2507 | fb_helper->sw_rotations |= rotation; | |
2508 | return; | |
2509 | } | |
2510 | ||
2511 | fb_crtc->rotation = rotation; | |
2512 | /* Rotating in hardware, fbcon should not rotate */ | |
2513 | fb_helper->sw_rotations |= DRM_MODE_ROTATE_0; | |
2514 | } | |
2515 | ||
64e94407 CW |
2516 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, |
2517 | u32 width, u32 height) | |
38651674 | 2518 | { |
8be48d92 DA |
2519 | struct drm_device *dev = fb_helper->dev; |
2520 | struct drm_fb_helper_crtc **crtcs; | |
38651674 | 2521 | struct drm_display_mode **modes; |
b0ee9e7f | 2522 | struct drm_fb_offset *offsets; |
38651674 | 2523 | bool *enabled; |
11e17a08 | 2524 | int i; |
38651674 DA |
2525 | |
2526 | DRM_DEBUG_KMS("\n"); | |
966a6a13 | 2527 | /* prevent concurrent modification of connector_count by hotplug */ |
e9827d8e | 2528 | lockdep_assert_held(&fb_helper->lock); |
966a6a13 | 2529 | |
383b2e57 | 2530 | crtcs = kcalloc(fb_helper->connector_count, |
8be48d92 | 2531 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
383b2e57 | 2532 | modes = kcalloc(fb_helper->connector_count, |
38651674 | 2533 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
383b2e57 | 2534 | offsets = kcalloc(fb_helper->connector_count, |
b0ee9e7f | 2535 | sizeof(struct drm_fb_offset), GFP_KERNEL); |
383b2e57 | 2536 | enabled = kcalloc(fb_helper->connector_count, |
38651674 | 2537 | sizeof(bool), GFP_KERNEL); |
b0ee9e7f | 2538 | if (!crtcs || !modes || !enabled || !offsets) { |
8c5eaca0 SK |
2539 | DRM_ERROR("Memory allocation failed\n"); |
2540 | goto out; | |
2541 | } | |
2542 | ||
e13a0583 DV |
2543 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
2544 | if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) | |
2545 | DRM_DEBUG_KMS("No connectors reported connected with modes\n"); | |
0b4c0f3f | 2546 | drm_enable_connectors(fb_helper, enabled); |
38651674 | 2547 | |
11e17a08 JB |
2548 | if (!(fb_helper->funcs->initial_config && |
2549 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, | |
b0ee9e7f | 2550 | offsets, |
11e17a08 | 2551 | enabled, width, height))) { |
383b2e57 ML |
2552 | memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); |
2553 | memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); | |
2554 | memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); | |
11e17a08 | 2555 | |
b0ee9e7f DA |
2556 | if (!drm_target_cloned(fb_helper, modes, offsets, |
2557 | enabled, width, height) && | |
2558 | !drm_target_preferred(fb_helper, modes, offsets, | |
2559 | enabled, width, height)) | |
1d42bbc8 | 2560 | DRM_ERROR("Unable to find initial modes\n"); |
38651674 | 2561 | |
11e17a08 JB |
2562 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
2563 | width, height); | |
38651674 | 2564 | |
11e17a08 JB |
2565 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
2566 | } | |
e13a0583 | 2567 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
8be48d92 DA |
2568 | |
2569 | /* need to set the modesets up here for use later */ | |
2570 | /* fill out the connector<->crtc mappings into the modesets */ | |
a2889606 VS |
2571 | for (i = 0; i < fb_helper->crtc_count; i++) |
2572 | drm_fb_helper_modeset_release(fb_helper, | |
2573 | &fb_helper->crtc_info[i].mode_set); | |
38651674 | 2574 | |
8f0cb418 | 2575 | fb_helper->sw_rotations = 0; |
966a6a13 | 2576 | drm_fb_helper_for_each_connector(fb_helper, i) { |
38651674 | 2577 | struct drm_display_mode *mode = modes[i]; |
8be48d92 | 2578 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
b0ee9e7f | 2579 | struct drm_fb_offset *offset = &offsets[i]; |
38651674 | 2580 | |
8be48d92 | 2581 | if (mode && fb_crtc) { |
27a061fb | 2582 | struct drm_mode_set *modeset = &fb_crtc->mode_set; |
a2889606 VS |
2583 | struct drm_connector *connector = |
2584 | fb_helper->connector_info[i]->connector; | |
2585 | ||
b0ee9e7f DA |
2586 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
2587 | mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); | |
a2889606 | 2588 | |
8be48d92 | 2589 | fb_crtc->desired_mode = mode; |
b0ee9e7f DA |
2590 | fb_crtc->x = offset->x; |
2591 | fb_crtc->y = offset->y; | |
8be48d92 DA |
2592 | modeset->mode = drm_mode_duplicate(dev, |
2593 | fb_crtc->desired_mode); | |
ad093607 | 2594 | drm_connector_get(connector); |
8f0cb418 | 2595 | drm_setup_crtc_rotation(fb_helper, fb_crtc, connector); |
a2889606 | 2596 | modeset->connectors[modeset->num_connectors++] = connector; |
b0ee9e7f DA |
2597 | modeset->x = offset->x; |
2598 | modeset->y = offset->y; | |
7e53f3a4 DV |
2599 | } |
2600 | } | |
8c5eaca0 | 2601 | out: |
38651674 DA |
2602 | kfree(crtcs); |
2603 | kfree(modes); | |
b0ee9e7f | 2604 | kfree(offsets); |
38651674 DA |
2605 | kfree(enabled); |
2606 | } | |
2607 | ||
f461bd2b DL |
2608 | /* |
2609 | * This is a continuation of drm_setup_crtcs() that sets up anything related | |
2610 | * to the framebuffer. During initialization, drm_setup_crtcs() is called before | |
2611 | * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). | |
2612 | * So, any setup that touches those fields needs to be done here instead of in | |
2613 | * drm_setup_crtcs(). | |
2614 | */ | |
2615 | static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) | |
2616 | { | |
991a3999 | 2617 | struct fb_info *info = fb_helper->fbdev; |
f461bd2b DL |
2618 | int i; |
2619 | ||
2620 | for (i = 0; i < fb_helper->crtc_count; i++) | |
2621 | if (fb_helper->crtc_info[i].mode_set.num_connectors) | |
2622 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; | |
991a3999 DL |
2623 | |
2624 | mutex_lock(&fb_helper->dev->mode_config.mutex); | |
2625 | drm_fb_helper_for_each_connector(fb_helper, i) { | |
2626 | struct drm_connector *connector = | |
2627 | fb_helper->connector_info[i]->connector; | |
2628 | ||
2629 | /* use first connected connector for the physical dimensions */ | |
2630 | if (connector->status == connector_status_connected) { | |
2631 | info->var.width = connector->display_info.width_mm; | |
2632 | info->var.height = connector->display_info.height_mm; | |
2633 | break; | |
2634 | } | |
2635 | } | |
2636 | mutex_unlock(&fb_helper->dev->mode_config.mutex); | |
8f0cb418 HG |
2637 | |
2638 | switch (fb_helper->sw_rotations) { | |
2639 | case DRM_MODE_ROTATE_0: | |
2640 | info->fbcon_rotate_hint = FB_ROTATE_UR; | |
2641 | break; | |
2642 | case DRM_MODE_ROTATE_90: | |
2643 | info->fbcon_rotate_hint = FB_ROTATE_CCW; | |
2644 | break; | |
2645 | case DRM_MODE_ROTATE_180: | |
2646 | info->fbcon_rotate_hint = FB_ROTATE_UD; | |
2647 | break; | |
2648 | case DRM_MODE_ROTATE_270: | |
2649 | info->fbcon_rotate_hint = FB_ROTATE_CW; | |
2650 | break; | |
2651 | default: | |
2652 | /* | |
2653 | * Multiple bits are set / multiple rotations requested | |
2654 | * fbcon cannot handle separate rotation settings per | |
2655 | * output, so fallback to unrotated. | |
2656 | */ | |
2657 | info->fbcon_rotate_hint = FB_ROTATE_UR; | |
2658 | } | |
f461bd2b DL |
2659 | } |
2660 | ||
ca91a275 DV |
2661 | /* Note: Drops fb_helper->lock before returning. */ |
2662 | static int | |
2663 | __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, | |
2664 | int bpp_sel) | |
2665 | { | |
2666 | struct drm_device *dev = fb_helper->dev; | |
2667 | struct fb_info *info; | |
2668 | unsigned int width, height; | |
2669 | int ret; | |
2670 | ||
2671 | width = dev->mode_config.max_width; | |
2672 | height = dev->mode_config.max_height; | |
2673 | ||
2674 | drm_setup_crtcs(fb_helper, width, height); | |
2675 | ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); | |
2676 | if (ret < 0) { | |
2677 | if (ret == -EAGAIN) { | |
2678 | fb_helper->preferred_bpp = bpp_sel; | |
2679 | fb_helper->deferred_setup = true; | |
2680 | ret = 0; | |
2681 | } | |
2682 | mutex_unlock(&fb_helper->lock); | |
2683 | ||
2684 | return ret; | |
2685 | } | |
f461bd2b | 2686 | drm_setup_crtcs_fb(fb_helper); |
ca91a275 DV |
2687 | |
2688 | fb_helper->deferred_setup = false; | |
2689 | ||
2690 | info = fb_helper->fbdev; | |
2691 | info->var.pixclock = 0; | |
4be9bd10 NA |
2692 | /* Shamelessly allow physical address leaking to userspace */ |
2693 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
2694 | if (!drm_leak_fbdev_smem) | |
2695 | #endif | |
2696 | /* don't leak any physical addresses to userspace */ | |
2697 | info->flags |= FBINFO_HIDE_SMEM_START; | |
ca91a275 DV |
2698 | |
2699 | /* Need to drop locks to avoid recursive deadlock in | |
2700 | * register_framebuffer. This is ok because the only thing left to do is | |
2701 | * register the fbdev emulation instance in kernel_fb_helper_list. */ | |
2702 | mutex_unlock(&fb_helper->lock); | |
2703 | ||
2704 | ret = register_framebuffer(info); | |
2705 | if (ret < 0) | |
2706 | return ret; | |
2707 | ||
2708 | dev_info(dev->dev, "fb%d: %s frame buffer device\n", | |
2709 | info->node, info->fix.id); | |
2710 | ||
2711 | mutex_lock(&kernel_fb_helper_lock); | |
2712 | if (list_empty(&kernel_fb_helper_list)) | |
2713 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
2714 | ||
2715 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
2716 | mutex_unlock(&kernel_fb_helper_lock); | |
2717 | ||
2718 | return 0; | |
2719 | } | |
2720 | ||
38651674 | 2721 | /** |
207fd329 | 2722 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
d0ddc033 DV |
2723 | * @fb_helper: fb_helper device struct |
2724 | * @bpp_sel: bpp value to use for the framebuffer configuration | |
38651674 | 2725 | * |
d0ddc033 | 2726 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
38651674 DA |
2727 | * At the moment, this is a cloned configuration across all heads with |
2728 | * a new framebuffer object as the backing store. | |
2729 | * | |
207fd329 DV |
2730 | * Note that this also registers the fbdev and so allows userspace to call into |
2731 | * the driver through the fbdev interfaces. | |
2732 | * | |
6806cdf9 DV |
2733 | * This function will call down into the &drm_fb_helper_funcs.fb_probe callback |
2734 | * to let the driver allocate and initialize the fbdev info structure and the | |
2735 | * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and | |
207fd329 DV |
2736 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default |
2737 | * values for the fbdev info structure. | |
2738 | * | |
40f8cf4b DV |
2739 | * HANG DEBUGGING: |
2740 | * | |
2741 | * When you have fbcon support built-in or already loaded, this function will do | |
2742 | * a full modeset to setup the fbdev console. Due to locking misdesign in the | |
2743 | * VT/fbdev subsystem that entire modeset sequence has to be done while holding | |
2744 | * console_lock. Until console_unlock is called no dmesg lines will be sent out | |
2745 | * to consoles, not even serial console. This means when your driver crashes, | |
2746 | * you will see absolutely nothing else but a system stuck in this function, | |
2747 | * with no further output. Any kind of printk() you place within your own driver | |
2748 | * or in the drm core modeset code will also never show up. | |
2749 | * | |
2750 | * Standard debug practice is to run the fbcon setup without taking the | |
2751 | * console_lock as a hack, to be able to see backtraces and crashes on the | |
2752 | * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel | |
2753 | * cmdline option. | |
2754 | * | |
2755 | * The other option is to just disable fbdev emulation since very likely the | |
af509d38 L |
2756 | * first modeset from userspace will crash in the same way, and is even easier |
2757 | * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 | |
40f8cf4b DV |
2758 | * kernel cmdline option. |
2759 | * | |
38651674 DA |
2760 | * RETURNS: |
2761 | * Zero if everything went ok, nonzero otherwise. | |
2762 | */ | |
01934c2a | 2763 | int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
38651674 | 2764 | { |
966a6a13 | 2765 | int ret; |
38651674 | 2766 | |
f64c5573 DV |
2767 | if (!drm_fbdev_emulation) |
2768 | return 0; | |
2769 | ||
e9827d8e | 2770 | mutex_lock(&fb_helper->lock); |
ca91a275 | 2771 | ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); |
38651674 | 2772 | |
ca91a275 | 2773 | return ret; |
38651674 | 2774 | } |
8be48d92 | 2775 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 2776 | |
7394371d CW |
2777 | /** |
2778 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by | |
d0ddc033 | 2779 | * probing all the outputs attached to the fb |
c777990f | 2780 | * @fb_helper: driver-allocated fbdev helper, can be NULL |
7394371d | 2781 | * |
7394371d | 2782 | * Scan the connectors attached to the fb_helper and try to put together a |
62cacc79 | 2783 | * setup after notification of a change in output configuration. |
7394371d | 2784 | * |
207fd329 DV |
2785 | * Called at runtime, takes the mode config locks to be able to check/change the |
2786 | * modeset configuration. Must be run from process context (which usually means | |
2787 | * either the output polling work or a work item launched from the driver's | |
2788 | * hotplug interrupt). | |
2789 | * | |
50c3dc97 | 2790 | * Note that drivers may call this even before calling |
af509d38 | 2791 | * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows |
50c3dc97 DV |
2792 | * for a race-free fbcon setup and will make sure that the fbdev emulation will |
2793 | * not miss any hotplug events. | |
207fd329 | 2794 | * |
7394371d CW |
2795 | * RETURNS: |
2796 | * 0 on success and a non-zero error code otherwise. | |
2797 | */ | |
2798 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) | |
38651674 | 2799 | { |
e9827d8e | 2800 | int err = 0; |
5c4426a7 | 2801 | |
c777990f | 2802 | if (!drm_fbdev_emulation || !fb_helper) |
f64c5573 DV |
2803 | return 0; |
2804 | ||
e9827d8e | 2805 | mutex_lock(&fb_helper->lock); |
ca91a275 DV |
2806 | if (fb_helper->deferred_setup) { |
2807 | err = __drm_fb_helper_initial_config_and_unlock(fb_helper, | |
2808 | fb_helper->preferred_bpp); | |
2809 | return err; | |
2810 | } | |
2811 | ||
50c3dc97 | 2812 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
4abe3520 | 2813 | fb_helper->delayed_hotplug = true; |
bdac4a05 DV |
2814 | mutex_unlock(&fb_helper->lock); |
2815 | return err; | |
4abe3520 | 2816 | } |
e9827d8e | 2817 | |
eb1f8e4f | 2818 | DRM_DEBUG_KMS("\n"); |
4abe3520 | 2819 | |
64e94407 | 2820 | drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height); |
f461bd2b | 2821 | drm_setup_crtcs_fb(fb_helper); |
e9827d8e | 2822 | mutex_unlock(&fb_helper->lock); |
89ced125 | 2823 | |
2180c3c7 DV |
2824 | drm_fb_helper_set_par(fb_helper->fbdev); |
2825 | ||
2826 | return 0; | |
5c4426a7 | 2827 | } |
eb1f8e4f | 2828 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
5c4426a7 | 2829 | |
87412163 NT |
2830 | /** |
2831 | * drm_fb_helper_fbdev_setup() - Setup fbdev emulation | |
2832 | * @dev: DRM device | |
2833 | * @fb_helper: fbdev helper structure to set up | |
2834 | * @funcs: fbdev helper functions | |
2835 | * @preferred_bpp: Preferred bits per pixel for the device. | |
2836 | * @dev->mode_config.preferred_depth is used if this is zero. | |
2837 | * @max_conn_count: Maximum number of connectors. | |
2838 | * @dev->mode_config.num_connector is used if this is zero. | |
2839 | * | |
2840 | * This function sets up fbdev emulation and registers fbdev for access by | |
2841 | * userspace. If all connectors are disconnected, setup is deferred to the next | |
2842 | * time drm_fb_helper_hotplug_event() is called. | |
2843 | * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback | |
2844 | * function. | |
2845 | * | |
c10802b6 PW |
2846 | * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev. |
2847 | * | |
2848 | * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup(). | |
87412163 NT |
2849 | * |
2850 | * Returns: | |
2851 | * Zero on success or negative error code on failure. | |
2852 | */ | |
2853 | int drm_fb_helper_fbdev_setup(struct drm_device *dev, | |
2854 | struct drm_fb_helper *fb_helper, | |
2855 | const struct drm_fb_helper_funcs *funcs, | |
2856 | unsigned int preferred_bpp, | |
2857 | unsigned int max_conn_count) | |
2858 | { | |
2859 | int ret; | |
2860 | ||
2861 | if (!preferred_bpp) | |
2862 | preferred_bpp = dev->mode_config.preferred_depth; | |
2863 | if (!preferred_bpp) | |
2864 | preferred_bpp = 32; | |
2865 | ||
2866 | if (!max_conn_count) | |
2867 | max_conn_count = dev->mode_config.num_connector; | |
2868 | if (!max_conn_count) { | |
6129369a | 2869 | DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n"); |
87412163 NT |
2870 | return -EINVAL; |
2871 | } | |
2872 | ||
2873 | drm_fb_helper_prepare(dev, fb_helper, funcs); | |
2874 | ||
2875 | ret = drm_fb_helper_init(dev, fb_helper, max_conn_count); | |
2876 | if (ret < 0) { | |
6129369a | 2877 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret); |
87412163 NT |
2878 | return ret; |
2879 | } | |
2880 | ||
2881 | ret = drm_fb_helper_single_add_all_connectors(fb_helper); | |
2882 | if (ret < 0) { | |
6129369a | 2883 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret); |
87412163 NT |
2884 | goto err_drm_fb_helper_fini; |
2885 | } | |
2886 | ||
2887 | if (!drm_drv_uses_atomic_modeset(dev)) | |
2888 | drm_helper_disable_unused_functions(dev); | |
2889 | ||
2890 | ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp); | |
2891 | if (ret < 0) { | |
6129369a | 2892 | DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret); |
87412163 NT |
2893 | goto err_drm_fb_helper_fini; |
2894 | } | |
2895 | ||
2896 | return 0; | |
2897 | ||
2898 | err_drm_fb_helper_fini: | |
2899 | drm_fb_helper_fini(fb_helper); | |
2900 | ||
2901 | return ret; | |
2902 | } | |
2903 | EXPORT_SYMBOL(drm_fb_helper_fbdev_setup); | |
2904 | ||
2905 | /** | |
2906 | * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation | |
2907 | * @dev: DRM device | |
2908 | * | |
2909 | * This function unregisters fbdev if not already done and cleans up the | |
2910 | * associated resources including the &drm_framebuffer. | |
2911 | * The driver is responsible for freeing the &drm_fb_helper structure which is | |
2912 | * stored in &drm_device->fb_helper. Do note that this pointer has been cleared | |
2913 | * when this function returns. | |
2914 | * | |
2915 | * In order to support device removal/unplug while file handles are still open, | |
2916 | * drm_fb_helper_unregister_fbi() should be called on device removal and | |
2917 | * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when | |
2918 | * file handles are closed. | |
2919 | */ | |
2920 | void drm_fb_helper_fbdev_teardown(struct drm_device *dev) | |
2921 | { | |
2922 | struct drm_fb_helper *fb_helper = dev->fb_helper; | |
48c9571c | 2923 | struct fb_ops *fbops = NULL; |
87412163 NT |
2924 | |
2925 | if (!fb_helper) | |
2926 | return; | |
2927 | ||
2928 | /* Unregister if it hasn't been done already */ | |
2929 | if (fb_helper->fbdev && fb_helper->fbdev->dev) | |
2930 | drm_fb_helper_unregister_fbi(fb_helper); | |
2931 | ||
48c9571c NT |
2932 | if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) { |
2933 | fb_deferred_io_cleanup(fb_helper->fbdev); | |
2934 | kfree(fb_helper->fbdev->fbdefio); | |
2935 | fbops = fb_helper->fbdev->fbops; | |
2936 | } | |
2937 | ||
87412163 | 2938 | drm_fb_helper_fini(fb_helper); |
48c9571c | 2939 | kfree(fbops); |
87412163 NT |
2940 | |
2941 | if (fb_helper->fb) | |
2942 | drm_framebuffer_remove(fb_helper->fb); | |
2943 | } | |
2944 | EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown); | |
2945 | ||
304a4f6a NT |
2946 | /** |
2947 | * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation | |
2948 | * @dev: DRM device | |
2949 | * | |
2950 | * This function can be used as the &drm_driver->lastclose callback for drivers | |
2951 | * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). | |
2952 | */ | |
2953 | void drm_fb_helper_lastclose(struct drm_device *dev) | |
2954 | { | |
2955 | drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); | |
2956 | } | |
2957 | EXPORT_SYMBOL(drm_fb_helper_lastclose); | |
2958 | ||
2959 | /** | |
2960 | * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed | |
2961 | * helper for fbdev emulation | |
2962 | * @dev: DRM device | |
2963 | * | |
2964 | * This function can be used as the | |
2965 | * &drm_mode_config_funcs.output_poll_changed callback for drivers that only | |
2966 | * need to call drm_fb_helper_hotplug_event(). | |
2967 | */ | |
2968 | void drm_fb_helper_output_poll_changed(struct drm_device *dev) | |
2969 | { | |
2970 | drm_fb_helper_hotplug_event(dev->fb_helper); | |
2971 | } | |
2972 | EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); | |
2973 | ||
d536540f NT |
2974 | /* @user: 1=userspace, 0=fbcon */ |
2975 | static int drm_fbdev_fb_open(struct fb_info *info, int user) | |
2976 | { | |
2977 | struct drm_fb_helper *fb_helper = info->par; | |
2978 | ||
2979 | if (!try_module_get(fb_helper->dev->driver->fops->owner)) | |
2980 | return -ENODEV; | |
2981 | ||
2982 | return 0; | |
2983 | } | |
2984 | ||
2985 | static int drm_fbdev_fb_release(struct fb_info *info, int user) | |
2986 | { | |
2987 | struct drm_fb_helper *fb_helper = info->par; | |
2988 | ||
2989 | module_put(fb_helper->dev->driver->fops->owner); | |
2990 | ||
2991 | return 0; | |
2992 | } | |
2993 | ||
2994 | /* | |
2995 | * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of | |
2996 | * unregister_framebuffer() or fb_release(). | |
2997 | */ | |
2998 | static void drm_fbdev_fb_destroy(struct fb_info *info) | |
2999 | { | |
3000 | struct drm_fb_helper *fb_helper = info->par; | |
3001 | struct fb_info *fbi = fb_helper->fbdev; | |
3002 | struct fb_ops *fbops = NULL; | |
3003 | void *shadow = NULL; | |
3004 | ||
3005 | if (fbi->fbdefio) { | |
3006 | fb_deferred_io_cleanup(fbi); | |
3007 | shadow = fbi->screen_buffer; | |
3008 | fbops = fbi->fbops; | |
3009 | } | |
3010 | ||
3011 | drm_fb_helper_fini(fb_helper); | |
3012 | ||
3013 | if (shadow) { | |
3014 | vfree(shadow); | |
3015 | kfree(fbops); | |
3016 | } | |
3017 | ||
3018 | drm_client_framebuffer_delete(fb_helper->buffer); | |
3019 | /* | |
3020 | * FIXME: | |
3021 | * Remove conditional when all CMA drivers have been moved over to using | |
3022 | * drm_fbdev_generic_setup(). | |
3023 | */ | |
3024 | if (fb_helper->client.funcs) { | |
3025 | drm_client_release(&fb_helper->client); | |
3026 | kfree(fb_helper); | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) | |
3031 | { | |
3032 | struct drm_fb_helper *fb_helper = info->par; | |
3033 | ||
3034 | if (fb_helper->dev->driver->gem_prime_mmap) | |
3035 | return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); | |
3036 | else | |
3037 | return -ENODEV; | |
3038 | } | |
3039 | ||
3040 | static struct fb_ops drm_fbdev_fb_ops = { | |
3041 | .owner = THIS_MODULE, | |
3042 | DRM_FB_HELPER_DEFAULT_OPS, | |
3043 | .fb_open = drm_fbdev_fb_open, | |
3044 | .fb_release = drm_fbdev_fb_release, | |
3045 | .fb_destroy = drm_fbdev_fb_destroy, | |
3046 | .fb_mmap = drm_fbdev_fb_mmap, | |
3047 | .fb_read = drm_fb_helper_sys_read, | |
3048 | .fb_write = drm_fb_helper_sys_write, | |
3049 | .fb_fillrect = drm_fb_helper_sys_fillrect, | |
3050 | .fb_copyarea = drm_fb_helper_sys_copyarea, | |
3051 | .fb_imageblit = drm_fb_helper_sys_imageblit, | |
3052 | }; | |
3053 | ||
3054 | static struct fb_deferred_io drm_fbdev_defio = { | |
3055 | .delay = HZ / 20, | |
3056 | .deferred_io = drm_fb_helper_deferred_io, | |
3057 | }; | |
3058 | ||
3059 | /** | |
3060 | * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper | |
3061 | * @fb_helper: fbdev helper structure | |
3062 | * @sizes: describes fbdev size and scanout surface size | |
3063 | * | |
c10802b6 | 3064 | * This function uses the client API to create a framebuffer backed by a dumb buffer. |
d536540f NT |
3065 | * |
3066 | * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, | |
3067 | * fb_copyarea, fb_imageblit. | |
3068 | * | |
3069 | * Returns: | |
3070 | * Zero on success or negative error code on failure. | |
3071 | */ | |
3072 | int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, | |
3073 | struct drm_fb_helper_surface_size *sizes) | |
3074 | { | |
3075 | struct drm_client_dev *client = &fb_helper->client; | |
3076 | struct drm_client_buffer *buffer; | |
3077 | struct drm_framebuffer *fb; | |
3078 | struct fb_info *fbi; | |
3079 | u32 format; | |
3080 | int ret; | |
3081 | ||
3082 | DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", | |
3083 | sizes->surface_width, sizes->surface_height, | |
3084 | sizes->surface_bpp); | |
3085 | ||
3086 | format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); | |
3087 | buffer = drm_client_framebuffer_create(client, sizes->surface_width, | |
3088 | sizes->surface_height, format); | |
3089 | if (IS_ERR(buffer)) | |
3090 | return PTR_ERR(buffer); | |
3091 | ||
3092 | fb_helper->buffer = buffer; | |
3093 | fb_helper->fb = buffer->fb; | |
3094 | fb = buffer->fb; | |
3095 | ||
3096 | fbi = drm_fb_helper_alloc_fbi(fb_helper); | |
3097 | if (IS_ERR(fbi)) { | |
3098 | ret = PTR_ERR(fbi); | |
3099 | goto err_free_buffer; | |
3100 | } | |
3101 | ||
3102 | fbi->par = fb_helper; | |
3103 | fbi->fbops = &drm_fbdev_fb_ops; | |
3104 | fbi->screen_size = fb->height * fb->pitches[0]; | |
3105 | fbi->fix.smem_len = fbi->screen_size; | |
3106 | fbi->screen_buffer = buffer->vaddr; | |
4be9bd10 NA |
3107 | /* Shamelessly leak the physical address to user-space */ |
3108 | #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) | |
3109 | if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) | |
3110 | fbi->fix.smem_start = | |
3111 | page_to_phys(virt_to_page(fbi->screen_buffer)); | |
3112 | #endif | |
d536540f NT |
3113 | strcpy(fbi->fix.id, "DRM emulated"); |
3114 | ||
3115 | drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); | |
3116 | drm_fb_helper_fill_var(fbi, fb_helper, sizes->fb_width, sizes->fb_height); | |
3117 | ||
3118 | if (fb->funcs->dirty) { | |
3119 | struct fb_ops *fbops; | |
3120 | void *shadow; | |
3121 | ||
3122 | /* | |
3123 | * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per | |
3124 | * instance version is necessary. | |
3125 | */ | |
3126 | fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); | |
3127 | shadow = vzalloc(fbi->screen_size); | |
3128 | if (!fbops || !shadow) { | |
3129 | kfree(fbops); | |
3130 | vfree(shadow); | |
3131 | ret = -ENOMEM; | |
3132 | goto err_fb_info_destroy; | |
3133 | } | |
3134 | ||
3135 | *fbops = *fbi->fbops; | |
3136 | fbi->fbops = fbops; | |
3137 | fbi->screen_buffer = shadow; | |
3138 | fbi->fbdefio = &drm_fbdev_defio; | |
3139 | ||
3140 | fb_deferred_io_init(fbi); | |
3141 | } | |
3142 | ||
3143 | return 0; | |
3144 | ||
3145 | err_fb_info_destroy: | |
3146 | drm_fb_helper_fini(fb_helper); | |
3147 | err_free_buffer: | |
3148 | drm_client_framebuffer_delete(buffer); | |
3149 | ||
3150 | return ret; | |
3151 | } | |
3152 | EXPORT_SYMBOL(drm_fb_helper_generic_probe); | |
3153 | ||
9060d7f4 NT |
3154 | static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { |
3155 | .fb_probe = drm_fb_helper_generic_probe, | |
3156 | }; | |
3157 | ||
3158 | static void drm_fbdev_client_unregister(struct drm_client_dev *client) | |
3159 | { | |
3160 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); | |
3161 | ||
3162 | if (fb_helper->fbdev) { | |
3163 | drm_fb_helper_unregister_fbi(fb_helper); | |
3164 | /* drm_fbdev_fb_destroy() takes care of cleanup */ | |
3165 | return; | |
3166 | } | |
3167 | ||
3168 | /* Did drm_fb_helper_fbdev_setup() run? */ | |
3169 | if (fb_helper->dev) | |
3170 | drm_fb_helper_fini(fb_helper); | |
3171 | ||
3172 | drm_client_release(client); | |
3173 | kfree(fb_helper); | |
3174 | } | |
3175 | ||
3176 | static int drm_fbdev_client_restore(struct drm_client_dev *client) | |
3177 | { | |
3178 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); | |
3179 | ||
3180 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); | |
3181 | ||
3182 | return 0; | |
3183 | } | |
3184 | ||
3185 | static int drm_fbdev_client_hotplug(struct drm_client_dev *client) | |
3186 | { | |
3187 | struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); | |
3188 | struct drm_device *dev = client->dev; | |
3189 | int ret; | |
3190 | ||
3191 | /* If drm_fb_helper_fbdev_setup() failed, we only try once */ | |
3192 | if (!fb_helper->dev && fb_helper->funcs) | |
3193 | return 0; | |
3194 | ||
3195 | if (dev->fb_helper) | |
3196 | return drm_fb_helper_hotplug_event(dev->fb_helper); | |
3197 | ||
c10802b6 PW |
3198 | if (!dev->mode_config.num_connector) { |
3199 | DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n"); | |
9060d7f4 | 3200 | return 0; |
c10802b6 | 3201 | } |
9060d7f4 NT |
3202 | |
3203 | ret = drm_fb_helper_fbdev_setup(dev, fb_helper, &drm_fb_helper_generic_funcs, | |
3204 | fb_helper->preferred_bpp, 0); | |
3205 | if (ret) { | |
3206 | fb_helper->dev = NULL; | |
3207 | fb_helper->fbdev = NULL; | |
3208 | return ret; | |
3209 | } | |
3210 | ||
3211 | return 0; | |
3212 | } | |
3213 | ||
3214 | static const struct drm_client_funcs drm_fbdev_client_funcs = { | |
3215 | .owner = THIS_MODULE, | |
3216 | .unregister = drm_fbdev_client_unregister, | |
3217 | .restore = drm_fbdev_client_restore, | |
3218 | .hotplug = drm_fbdev_client_hotplug, | |
3219 | }; | |
3220 | ||
3221 | /** | |
c10802b6 | 3222 | * drm_fbdev_generic_setup() - Setup generic fbdev emulation |
9060d7f4 NT |
3223 | * @dev: DRM device |
3224 | * @preferred_bpp: Preferred bits per pixel for the device. | |
3225 | * @dev->mode_config.preferred_depth is used if this is zero. | |
3226 | * | |
3227 | * This function sets up generic fbdev emulation for drivers that supports | |
c10802b6 PW |
3228 | * dumb buffers with a virtual address and that can be mmap'ed. If the driver |
3229 | * does not support these functions, it could use drm_fb_helper_fbdev_setup(). | |
9060d7f4 NT |
3230 | * |
3231 | * Restore, hotplug events and teardown are all taken care of. Drivers that do | |
3232 | * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. | |
3233 | * Simple drivers might use drm_mode_config_helper_suspend(). | |
3234 | * | |
3235 | * Drivers that set the dirty callback on their framebuffer will get a shadow | |
3236 | * fbdev buffer that is blitted onto the real buffer. This is done in order to | |
3237 | * make deferred I/O work with all kinds of buffers. | |
3238 | * | |
3239 | * This function is safe to call even when there are no connectors present. | |
3240 | * Setup will be retried on the next hotplug event. | |
3241 | * | |
c10802b6 PW |
3242 | * The fbdev is destroyed by drm_dev_unregister(). |
3243 | * | |
9060d7f4 NT |
3244 | * Returns: |
3245 | * Zero on success or negative error code on failure. | |
3246 | */ | |
3247 | int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) | |
3248 | { | |
3249 | struct drm_fb_helper *fb_helper; | |
3250 | int ret; | |
3251 | ||
c10802b6 PW |
3252 | WARN(dev->fb_helper, "fb_helper is already set!\n"); |
3253 | ||
9060d7f4 NT |
3254 | if (!drm_fbdev_emulation) |
3255 | return 0; | |
3256 | ||
3257 | fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); | |
3258 | if (!fb_helper) | |
3259 | return -ENOMEM; | |
3260 | ||
3261 | ret = drm_client_new(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); | |
3262 | if (ret) { | |
3263 | kfree(fb_helper); | |
c10802b6 | 3264 | DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret); |
9060d7f4 NT |
3265 | return ret; |
3266 | } | |
3267 | ||
3268 | fb_helper->preferred_bpp = preferred_bpp; | |
3269 | ||
c10802b6 PW |
3270 | ret = drm_fbdev_client_hotplug(&fb_helper->client); |
3271 | if (ret) | |
3272 | DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret); | |
9060d7f4 NT |
3273 | |
3274 | return 0; | |
3275 | } | |
3276 | EXPORT_SYMBOL(drm_fbdev_generic_setup); | |
3277 | ||
6a108a14 | 3278 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
3ce05168 DF |
3279 | * but the module doesn't depend on any fb console symbols. At least |
3280 | * attempt to load fbcon to avoid leaving the system without a usable console. | |
3281 | */ | |
70412cfa | 3282 | int __init drm_fb_helper_modinit(void) |
3ce05168 | 3283 | { |
70412cfa | 3284 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
06324664 | 3285 | const char name[] = "fbcon"; |
3ce05168 DF |
3286 | struct module *fbcon; |
3287 | ||
3288 | mutex_lock(&module_mutex); | |
3289 | fbcon = find_module(name); | |
3290 | mutex_unlock(&module_mutex); | |
3291 | ||
3292 | if (!fbcon) | |
3293 | request_module_nowait(name); | |
70412cfa | 3294 | #endif |
3ce05168 DF |
3295 | return 0; |
3296 | } | |
70412cfa | 3297 | EXPORT_SYMBOL(drm_fb_helper_modinit); |