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