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