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 | ||
3b40a443 | 32 | #include <linux/kernel.h> |
785b93ef | 33 | #include <linux/sysrq.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
785b93ef | 35 | #include <linux/fb.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> | |
785b93ef DA |
41 | |
42 | static LIST_HEAD(kernel_fb_helper_list); | |
43 | ||
d0ddc033 DV |
44 | /** |
45 | * DOC: fbdev helpers | |
46 | * | |
47 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel | |
83c617c5 | 48 | * mode setting driver. They can be used mostly independently from the crtc |
d0ddc033 DV |
49 | * helper functions used by many drivers to implement the kernel mode setting |
50 | * interfaces. | |
207fd329 | 51 | * |
10a23102 TR |
52 | * Initialization is done as a four-step process with drm_fb_helper_prepare(), |
53 | * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and | |
54 | * drm_fb_helper_initial_config(). Drivers with fancier requirements than the | |
55 | * default behaviour can override the third step with their own code. | |
56 | * Teardown is done with drm_fb_helper_fini(). | |
207fd329 DV |
57 | * |
58 | * At runtime drivers should restore the fbdev console by calling | |
59 | * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They | |
60 | * should also notify the fb helper code from updates to the output | |
61 | * configuration by calling drm_fb_helper_hotplug_event(). For easier | |
62 | * integration with the output polling code in drm_crtc_helper.c the modeset | |
83c617c5 | 63 | * code provides a ->output_poll_changed callback. |
207fd329 DV |
64 | * |
65 | * All other functions exported by the fb helper library can be used to | |
66 | * implement the fbdev driver interface by the driver. | |
10a23102 TR |
67 | * |
68 | * It is possible, though perhaps somewhat tricky, to implement race-free | |
69 | * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() | |
70 | * helper must be called first to initialize the minimum required to make | |
71 | * hotplug detection work. Drivers also need to make sure to properly set up | |
72 | * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init() | |
73 | * it is safe to enable interrupts and start processing hotplug events. At the | |
74 | * same time, drivers should initialize all modeset objects such as CRTCs, | |
75 | * encoders and connectors. To finish up the fbdev helper initialization, the | |
76 | * drm_fb_helper_init() function is called. To probe for all attached displays | |
77 | * and set up an initial configuration using the detected hardware, drivers | |
78 | * should call drm_fb_helper_single_add_all_connectors() followed by | |
79 | * drm_fb_helper_initial_config(). | |
d0ddc033 DV |
80 | */ |
81 | ||
207fd329 DV |
82 | /** |
83 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev | |
84 | * emulation helper | |
85 | * @fb_helper: fbdev initialized with drm_fb_helper_init | |
86 | * | |
87 | * This functions adds all the available connectors for use with the given | |
88 | * fb_helper. This is a separate step to allow drivers to freely assign | |
89 | * connectors to the fbdev, e.g. if some are reserved for special purposes or | |
90 | * not adequate to be used for the fbcon. | |
91 | * | |
169faeca DV |
92 | * This function is protected against concurrent connector hotadds/removals |
93 | * using drm_fb_helper_add_one_connector() and | |
94 | * drm_fb_helper_remove_one_connector(). | |
207fd329 | 95 | */ |
0b4c0f3f | 96 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) |
d50ba256 | 97 | { |
0b4c0f3f DA |
98 | struct drm_device *dev = fb_helper->dev; |
99 | struct drm_connector *connector; | |
100 | int i; | |
d50ba256 | 101 | |
169faeca | 102 | mutex_lock(&dev->mode_config.mutex); |
6295d607 | 103 | drm_for_each_connector(connector, dev) { |
0b4c0f3f DA |
104 | struct drm_fb_helper_connector *fb_helper_connector; |
105 | ||
106 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); | |
107 | if (!fb_helper_connector) | |
108 | goto fail; | |
d50ba256 | 109 | |
0b4c0f3f DA |
110 | fb_helper_connector->connector = connector; |
111 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; | |
112 | } | |
169faeca | 113 | mutex_unlock(&dev->mode_config.mutex); |
d50ba256 | 114 | return 0; |
0b4c0f3f DA |
115 | fail: |
116 | for (i = 0; i < fb_helper->connector_count; i++) { | |
117 | kfree(fb_helper->connector_info[i]); | |
118 | fb_helper->connector_info[i] = NULL; | |
119 | } | |
120 | fb_helper->connector_count = 0; | |
169faeca DV |
121 | mutex_unlock(&dev->mode_config.mutex); |
122 | ||
0b4c0f3f | 123 | return -ENOMEM; |
d50ba256 | 124 | } |
0b4c0f3f | 125 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
d50ba256 | 126 | |
65c2a89c DA |
127 | int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector) |
128 | { | |
129 | struct drm_fb_helper_connector **temp; | |
130 | struct drm_fb_helper_connector *fb_helper_connector; | |
131 | ||
132 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); | |
133 | if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) { | |
14f476fa | 134 | temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), GFP_KERNEL); |
65c2a89c DA |
135 | if (!temp) |
136 | return -ENOMEM; | |
137 | ||
138 | fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1; | |
139 | fb_helper->connector_info = temp; | |
140 | } | |
141 | ||
142 | ||
143 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); | |
144 | if (!fb_helper_connector) | |
145 | return -ENOMEM; | |
146 | ||
147 | fb_helper_connector->connector = connector; | |
148 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; | |
149 | return 0; | |
150 | } | |
151 | EXPORT_SYMBOL(drm_fb_helper_add_one_connector); | |
152 | ||
2148f18f RC |
153 | static void remove_from_modeset(struct drm_mode_set *set, |
154 | struct drm_connector *connector) | |
155 | { | |
156 | int i, j; | |
157 | ||
158 | for (i = 0; i < set->num_connectors; i++) { | |
159 | if (set->connectors[i] == connector) | |
160 | break; | |
161 | } | |
162 | ||
163 | if (i == set->num_connectors) | |
164 | return; | |
165 | ||
166 | for (j = i + 1; j < set->num_connectors; j++) { | |
167 | set->connectors[j - 1] = set->connectors[j]; | |
168 | } | |
169 | set->num_connectors--; | |
170 | ||
171 | /* because i915 is pissy about this.. | |
172 | * TODO maybe need to makes sure we set it back to !=NULL somewhere? | |
173 | */ | |
174 | if (set->num_connectors == 0) | |
175 | set->fb = NULL; | |
176 | } | |
177 | ||
65c2a89c DA |
178 | int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, |
179 | struct drm_connector *connector) | |
180 | { | |
181 | struct drm_fb_helper_connector *fb_helper_connector; | |
182 | int i, j; | |
183 | ||
184 | WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); | |
185 | ||
186 | for (i = 0; i < fb_helper->connector_count; i++) { | |
187 | if (fb_helper->connector_info[i]->connector == connector) | |
188 | break; | |
189 | } | |
190 | ||
191 | if (i == fb_helper->connector_count) | |
192 | return -EINVAL; | |
193 | fb_helper_connector = fb_helper->connector_info[i]; | |
194 | ||
195 | for (j = i + 1; j < fb_helper->connector_count; j++) { | |
196 | fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; | |
197 | } | |
198 | fb_helper->connector_count--; | |
199 | kfree(fb_helper_connector); | |
2148f18f RC |
200 | |
201 | /* also cleanup dangling references to the connector: */ | |
202 | for (i = 0; i < fb_helper->crtc_count; i++) | |
203 | remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector); | |
204 | ||
65c2a89c DA |
205 | return 0; |
206 | } | |
207 | EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); | |
208 | ||
99231028 JW |
209 | static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) |
210 | { | |
211 | uint16_t *r_base, *g_base, *b_base; | |
212 | int i; | |
213 | ||
04c0c569 VS |
214 | if (helper->funcs->gamma_get == NULL) |
215 | return; | |
216 | ||
99231028 JW |
217 | r_base = crtc->gamma_store; |
218 | g_base = r_base + crtc->gamma_size; | |
219 | b_base = g_base + crtc->gamma_size; | |
220 | ||
221 | for (i = 0; i < crtc->gamma_size; i++) | |
222 | helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); | |
223 | } | |
224 | ||
225 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) | |
226 | { | |
227 | uint16_t *r_base, *g_base, *b_base; | |
228 | ||
ebe0f244 LP |
229 | if (crtc->funcs->gamma_set == NULL) |
230 | return; | |
231 | ||
99231028 JW |
232 | r_base = crtc->gamma_store; |
233 | g_base = r_base + crtc->gamma_size; | |
234 | b_base = g_base + crtc->gamma_size; | |
235 | ||
236 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); | |
237 | } | |
238 | ||
207fd329 DV |
239 | /** |
240 | * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter | |
241 | * @info: fbdev registered by the helper | |
242 | */ | |
1a7aba7f JB |
243 | int drm_fb_helper_debug_enter(struct fb_info *info) |
244 | { | |
245 | struct drm_fb_helper *helper = info->par; | |
be26a66d | 246 | const struct drm_crtc_helper_funcs *funcs; |
1a7aba7f JB |
247 | int i; |
248 | ||
1a7aba7f JB |
249 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { |
250 | for (i = 0; i < helper->crtc_count; i++) { | |
251 | struct drm_mode_set *mode_set = | |
252 | &helper->crtc_info[i].mode_set; | |
253 | ||
254 | if (!mode_set->crtc->enabled) | |
255 | continue; | |
256 | ||
257 | funcs = mode_set->crtc->helper_private; | |
99231028 | 258 | drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); |
1a7aba7f JB |
259 | funcs->mode_set_base_atomic(mode_set->crtc, |
260 | mode_set->fb, | |
261 | mode_set->x, | |
413d45d3 | 262 | mode_set->y, |
21c74a8e | 263 | ENTER_ATOMIC_MODE_SET); |
1a7aba7f JB |
264 | } |
265 | } | |
266 | ||
267 | return 0; | |
268 | } | |
269 | EXPORT_SYMBOL(drm_fb_helper_debug_enter); | |
270 | ||
271 | /* Find the real fb for a given fb helper CRTC */ | |
272 | static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) | |
273 | { | |
274 | struct drm_device *dev = crtc->dev; | |
275 | struct drm_crtc *c; | |
276 | ||
6295d607 | 277 | drm_for_each_crtc(c, dev) { |
1a7aba7f | 278 | if (crtc->base.id == c->base.id) |
f4510a27 | 279 | return c->primary->fb; |
1a7aba7f JB |
280 | } |
281 | ||
282 | return NULL; | |
283 | } | |
284 | ||
207fd329 DV |
285 | /** |
286 | * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave | |
287 | * @info: fbdev registered by the helper | |
288 | */ | |
1a7aba7f JB |
289 | int drm_fb_helper_debug_leave(struct fb_info *info) |
290 | { | |
291 | struct drm_fb_helper *helper = info->par; | |
292 | struct drm_crtc *crtc; | |
be26a66d | 293 | const struct drm_crtc_helper_funcs *funcs; |
1a7aba7f JB |
294 | struct drm_framebuffer *fb; |
295 | int i; | |
296 | ||
297 | for (i = 0; i < helper->crtc_count; i++) { | |
298 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; | |
299 | crtc = mode_set->crtc; | |
300 | funcs = crtc->helper_private; | |
301 | fb = drm_mode_config_fb(crtc); | |
302 | ||
303 | if (!crtc->enabled) | |
304 | continue; | |
305 | ||
306 | if (!fb) { | |
307 | DRM_ERROR("no fb to restore??\n"); | |
308 | continue; | |
309 | } | |
310 | ||
99231028 | 311 | drm_fb_helper_restore_lut_atomic(mode_set->crtc); |
1a7aba7f | 312 | funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, |
21c74a8e | 313 | crtc->y, LEAVE_ATOMIC_MODE_SET); |
1a7aba7f JB |
314 | } |
315 | ||
316 | return 0; | |
317 | } | |
318 | EXPORT_SYMBOL(drm_fb_helper_debug_leave); | |
319 | ||
5ea1f752 | 320 | static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper) |
e8e7a2b8 | 321 | { |
3858bc5d VS |
322 | struct drm_device *dev = fb_helper->dev; |
323 | struct drm_plane *plane; | |
e8e7a2b8 | 324 | bool error = false; |
3858bc5d VS |
325 | int i; |
326 | ||
327 | drm_warn_on_modeset_not_all_locked(dev); | |
6aed8ec3 | 328 | |
6295d607 | 329 | drm_for_each_plane(plane, dev) { |
e27dde3e MR |
330 | if (plane->type != DRM_PLANE_TYPE_PRIMARY) |
331 | drm_plane_force_disable(plane); | |
6aed8ec3 | 332 | |
9783de20 | 333 | if (dev->mode_config.rotation_property) { |
3a5f87c2 TW |
334 | drm_mode_plane_set_obj_prop(plane, |
335 | dev->mode_config.rotation_property, | |
336 | BIT(DRM_ROTATE_0)); | |
9783de20 SJ |
337 | } |
338 | } | |
339 | ||
e8e7a2b8 DA |
340 | for (i = 0; i < fb_helper->crtc_count; i++) { |
341 | struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; | |
3858bc5d VS |
342 | struct drm_crtc *crtc = mode_set->crtc; |
343 | int ret; | |
344 | ||
345 | if (crtc->funcs->cursor_set) { | |
346 | ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); | |
347 | if (ret) | |
348 | error = true; | |
349 | } | |
350 | ||
2d13b679 | 351 | ret = drm_mode_set_config_internal(mode_set); |
e8e7a2b8 DA |
352 | if (ret) |
353 | error = true; | |
354 | } | |
355 | return error; | |
356 | } | |
5ea1f752 RC |
357 | /** |
358 | * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration | |
359 | * @fb_helper: fbcon to restore | |
360 | * | |
361 | * This should be called from driver's drm ->lastclose callback | |
362 | * when implementing an fbcon on top of kms using this helper. This ensures that | |
363 | * the user isn't greeted with a black screen when e.g. X dies. | |
364 | * | |
365 | * Use this variant if you need to bypass locking (panic), or already | |
366 | * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked() | |
367 | */ | |
368 | static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper) | |
369 | { | |
370 | return restore_fbdev_mode(fb_helper); | |
371 | } | |
372 | ||
373 | /** | |
374 | * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration | |
375 | * @fb_helper: fbcon to restore | |
376 | * | |
377 | * This should be called from driver's drm ->lastclose callback | |
378 | * when implementing an fbcon on top of kms using this helper. This ensures that | |
379 | * the user isn't greeted with a black screen when e.g. X dies. | |
380 | */ | |
381 | bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) | |
382 | { | |
383 | struct drm_device *dev = fb_helper->dev; | |
384 | bool ret; | |
e2809c7d DA |
385 | bool do_delayed = false; |
386 | ||
5ea1f752 RC |
387 | drm_modeset_lock_all(dev); |
388 | ret = restore_fbdev_mode(fb_helper); | |
e2809c7d DA |
389 | |
390 | do_delayed = fb_helper->delayed_hotplug; | |
391 | if (do_delayed) | |
392 | fb_helper->delayed_hotplug = false; | |
5ea1f752 | 393 | drm_modeset_unlock_all(dev); |
e2809c7d DA |
394 | |
395 | if (do_delayed) | |
396 | drm_fb_helper_hotplug_event(fb_helper); | |
5ea1f752 RC |
397 | return ret; |
398 | } | |
399 | EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); | |
e8e7a2b8 | 400 | |
d21bf469 DV |
401 | /* |
402 | * restore fbcon display for all kms driver's using this helper, used for sysrq | |
403 | * and panic handling. | |
404 | */ | |
78b9c353 | 405 | static bool drm_fb_helper_force_kernel_mode(void) |
785b93ef | 406 | { |
785b93ef DA |
407 | bool ret, error = false; |
408 | struct drm_fb_helper *helper; | |
409 | ||
410 | if (list_empty(&kernel_fb_helper_list)) | |
411 | return false; | |
412 | ||
413 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { | |
b77f0765 TR |
414 | struct drm_device *dev = helper->dev; |
415 | ||
416 | if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) | |
417 | continue; | |
418 | ||
cb597bb3 DV |
419 | /* |
420 | * NOTE: Use trylock mode to avoid deadlocks and sleeping in | |
421 | * panic context. | |
51fd371b | 422 | */ |
cb597bb3 | 423 | if (__drm_modeset_lock_all(dev, true) != 0) { |
b77f0765 | 424 | error = true; |
e8e7a2b8 | 425 | continue; |
b77f0765 | 426 | } |
e8e7a2b8 DA |
427 | |
428 | ret = drm_fb_helper_restore_fbdev_mode(helper); | |
429 | if (ret) | |
430 | error = true; | |
b77f0765 | 431 | |
cb597bb3 | 432 | drm_modeset_unlock_all(dev); |
785b93ef DA |
433 | } |
434 | return error; | |
435 | } | |
436 | ||
20c60c35 DV |
437 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
438 | { | |
439 | struct drm_device *dev = fb_helper->dev; | |
440 | struct drm_crtc *crtc; | |
441 | int bound = 0, crtcs_bound = 0; | |
442 | ||
520edd13 PZ |
443 | /* Sometimes user space wants everything disabled, so don't steal the |
444 | * display if there's a master. */ | |
445 | if (dev->primary->master) | |
446 | return false; | |
447 | ||
6295d607 | 448 | drm_for_each_crtc(crtc, dev) { |
f4510a27 | 449 | if (crtc->primary->fb) |
20c60c35 | 450 | crtcs_bound++; |
f4510a27 | 451 | if (crtc->primary->fb == fb_helper->fb) |
20c60c35 DV |
452 | bound++; |
453 | } | |
454 | ||
455 | if (bound < crtcs_bound) | |
456 | return false; | |
520edd13 | 457 | |
20c60c35 DV |
458 | return true; |
459 | } | |
460 | ||
bea1d35b | 461 | #ifdef CONFIG_MAGIC_SYSRQ |
785b93ef DA |
462 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
463 | { | |
d21bf469 DV |
464 | bool ret; |
465 | ret = drm_fb_helper_force_kernel_mode(); | |
466 | if (ret == true) | |
467 | DRM_ERROR("Failed to restore crtc configuration\n"); | |
785b93ef DA |
468 | } |
469 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); | |
470 | ||
1495cc9d | 471 | static void drm_fb_helper_sysrq(int dummy1) |
785b93ef DA |
472 | { |
473 | schedule_work(&drm_fb_helper_restore_work); | |
474 | } | |
475 | ||
476 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { | |
477 | .handler = drm_fb_helper_sysrq, | |
478 | .help_msg = "force-fb(V)", | |
479 | .action_msg = "Restore framebuffer console", | |
480 | }; | |
b8c40d62 RD |
481 | #else |
482 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; | |
bea1d35b | 483 | #endif |
785b93ef | 484 | |
3a8148c5 | 485 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
785b93ef DA |
486 | { |
487 | struct drm_fb_helper *fb_helper = info->par; | |
488 | struct drm_device *dev = fb_helper->dev; | |
489 | struct drm_crtc *crtc; | |
023eb571 | 490 | struct drm_connector *connector; |
023eb571 | 491 | int i, j; |
785b93ef | 492 | |
928c2f0c DV |
493 | /* |
494 | * fbdev->blank can be called from irq context in case of a panic. | |
1b1d5397 DV |
495 | * Since we already have our own special panic handler which will |
496 | * restore the fbdev console mode completely, just bail out early. | |
497 | */ | |
498 | if (oops_in_progress) | |
499 | return; | |
500 | ||
785b93ef | 501 | /* |
3a8148c5 | 502 | * For each CRTC in this fb, turn the connectors on/off. |
785b93ef | 503 | */ |
84849903 | 504 | drm_modeset_lock_all(dev); |
20c60c35 DV |
505 | if (!drm_fb_helper_is_bound(fb_helper)) { |
506 | drm_modeset_unlock_all(dev); | |
507 | return; | |
508 | } | |
509 | ||
e87b2c42 | 510 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 | 511 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
785b93ef | 512 | |
8be48d92 DA |
513 | if (!crtc->enabled) |
514 | continue; | |
515 | ||
3a8148c5 | 516 | /* Walk the connectors & encoders on this fb turning them on/off */ |
023eb571 JB |
517 | for (j = 0; j < fb_helper->connector_count; j++) { |
518 | connector = fb_helper->connector_info[j]->connector; | |
e04190e0 | 519 | connector->funcs->dpms(connector, dpms_mode); |
58495563 | 520 | drm_object_property_set_value(&connector->base, |
3a8148c5 | 521 | dev->mode_config.dpms_property, dpms_mode); |
785b93ef | 522 | } |
785b93ef | 523 | } |
84849903 | 524 | drm_modeset_unlock_all(dev); |
785b93ef DA |
525 | } |
526 | ||
207fd329 DV |
527 | /** |
528 | * drm_fb_helper_blank - implementation for ->fb_blank | |
529 | * @blank: desired blanking state | |
530 | * @info: fbdev registered by the helper | |
531 | */ | |
785b93ef DA |
532 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
533 | { | |
534 | switch (blank) { | |
731b5a15 | 535 | /* Display: On; HSync: On, VSync: On */ |
785b93ef | 536 | case FB_BLANK_UNBLANK: |
3a8148c5 | 537 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
785b93ef | 538 | break; |
731b5a15 | 539 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 540 | case FB_BLANK_NORMAL: |
3a8148c5 | 541 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 542 | break; |
731b5a15 | 543 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef | 544 | case FB_BLANK_HSYNC_SUSPEND: |
3a8148c5 | 545 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 546 | break; |
731b5a15 | 547 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef | 548 | case FB_BLANK_VSYNC_SUSPEND: |
3a8148c5 | 549 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
785b93ef | 550 | break; |
731b5a15 | 551 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef | 552 | case FB_BLANK_POWERDOWN: |
3a8148c5 | 553 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
785b93ef DA |
554 | break; |
555 | } | |
556 | return 0; | |
557 | } | |
558 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
559 | ||
560 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) | |
561 | { | |
562 | int i; | |
563 | ||
0b4c0f3f DA |
564 | for (i = 0; i < helper->connector_count; i++) |
565 | kfree(helper->connector_info[i]); | |
566 | kfree(helper->connector_info); | |
a1b7736d | 567 | for (i = 0; i < helper->crtc_count; i++) { |
785b93ef | 568 | kfree(helper->crtc_info[i].mode_set.connectors); |
a1b7736d SH |
569 | if (helper->crtc_info[i].mode_set.mode) |
570 | drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); | |
571 | } | |
785b93ef DA |
572 | kfree(helper->crtc_info); |
573 | } | |
574 | ||
10a23102 TR |
575 | /** |
576 | * drm_fb_helper_prepare - setup a drm_fb_helper structure | |
577 | * @dev: DRM device | |
578 | * @helper: driver-allocated fbdev helper structure to set up | |
579 | * @funcs: pointer to structure of functions associate with this helper | |
580 | * | |
581 | * Sets up the bare minimum to make the framebuffer helper usable. This is | |
582 | * useful to implement race-free initialization of the polling helpers. | |
583 | */ | |
584 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, | |
585 | const struct drm_fb_helper_funcs *funcs) | |
586 | { | |
587 | INIT_LIST_HEAD(&helper->kernel_fb_list); | |
588 | helper->funcs = funcs; | |
589 | helper->dev = dev; | |
590 | } | |
591 | EXPORT_SYMBOL(drm_fb_helper_prepare); | |
592 | ||
207fd329 DV |
593 | /** |
594 | * drm_fb_helper_init - initialize a drm_fb_helper structure | |
595 | * @dev: drm device | |
596 | * @fb_helper: driver-allocated fbdev helper structure to initialize | |
597 | * @crtc_count: maximum number of crtcs to support in this fbdev emulation | |
598 | * @max_conn_count: max connector count | |
599 | * | |
600 | * This allocates the structures for the fbdev helper with the given limits. | |
601 | * Note that this won't yet touch the hardware (through the driver interfaces) | |
602 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() | |
603 | * to allow driver writes more control over the exact init sequence. | |
604 | * | |
10a23102 | 605 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
207fd329 DV |
606 | * |
607 | * RETURNS: | |
608 | * Zero if everything went ok, nonzero otherwise. | |
609 | */ | |
4abe3520 DA |
610 | int drm_fb_helper_init(struct drm_device *dev, |
611 | struct drm_fb_helper *fb_helper, | |
eb1f8e4f | 612 | int crtc_count, int max_conn_count) |
785b93ef | 613 | { |
785b93ef | 614 | struct drm_crtc *crtc; |
785b93ef DA |
615 | int i; |
616 | ||
04cfe97e XL |
617 | if (!max_conn_count) |
618 | return -EINVAL; | |
619 | ||
4abe3520 DA |
620 | fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
621 | if (!fb_helper->crtc_info) | |
785b93ef DA |
622 | return -ENOMEM; |
623 | ||
4abe3520 DA |
624 | fb_helper->crtc_count = crtc_count; |
625 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); | |
626 | if (!fb_helper->connector_info) { | |
627 | kfree(fb_helper->crtc_info); | |
0b4c0f3f DA |
628 | return -ENOMEM; |
629 | } | |
65c2a89c | 630 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
4abe3520 | 631 | fb_helper->connector_count = 0; |
785b93ef DA |
632 | |
633 | for (i = 0; i < crtc_count; i++) { | |
4abe3520 | 634 | fb_helper->crtc_info[i].mode_set.connectors = |
785b93ef DA |
635 | kcalloc(max_conn_count, |
636 | sizeof(struct drm_connector *), | |
637 | GFP_KERNEL); | |
638 | ||
4a1b0714 | 639 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
785b93ef | 640 | goto out_free; |
4abe3520 | 641 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
785b93ef DA |
642 | } |
643 | ||
644 | i = 0; | |
6295d607 | 645 | drm_for_each_crtc(crtc, dev) { |
4abe3520 | 646 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
785b93ef DA |
647 | i++; |
648 | } | |
e9ad3181 | 649 | |
785b93ef DA |
650 | return 0; |
651 | out_free: | |
4abe3520 | 652 | drm_fb_helper_crtc_free(fb_helper); |
785b93ef DA |
653 | return -ENOMEM; |
654 | } | |
4abe3520 DA |
655 | EXPORT_SYMBOL(drm_fb_helper_init); |
656 | ||
b8017d6c AT |
657 | /** |
658 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members | |
659 | * @fb_helper: driver-allocated fbdev helper | |
660 | * | |
661 | * A helper to alloc fb_info and the members cmap and apertures. Called | |
662 | * by the driver within the fb_probe fb_helper callback function. | |
663 | * | |
664 | * RETURNS: | |
665 | * fb_info pointer if things went okay, pointer containing error code | |
666 | * otherwise | |
667 | */ | |
668 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) | |
669 | { | |
670 | struct device *dev = fb_helper->dev->dev; | |
671 | struct fb_info *info; | |
672 | int ret; | |
673 | ||
674 | info = framebuffer_alloc(0, dev); | |
675 | if (!info) | |
676 | return ERR_PTR(-ENOMEM); | |
677 | ||
678 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
679 | if (ret) | |
680 | goto err_release; | |
681 | ||
682 | info->apertures = alloc_apertures(1); | |
683 | if (!info->apertures) { | |
684 | ret = -ENOMEM; | |
685 | goto err_free_cmap; | |
686 | } | |
687 | ||
688 | fb_helper->fbdev = info; | |
689 | ||
690 | return info; | |
691 | ||
692 | err_free_cmap: | |
693 | fb_dealloc_cmap(&info->cmap); | |
694 | err_release: | |
695 | framebuffer_release(info); | |
696 | return ERR_PTR(ret); | |
697 | } | |
698 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); | |
699 | ||
700 | /** | |
701 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device | |
702 | * @fb_helper: driver-allocated fbdev helper | |
703 | * | |
704 | * A wrapper around unregister_framebuffer, to release the fb_info | |
705 | * framebuffer device | |
706 | */ | |
707 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) | |
708 | { | |
709 | if (fb_helper && fb_helper->fbdev) | |
710 | unregister_framebuffer(fb_helper->fbdev); | |
711 | } | |
712 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); | |
713 | ||
714 | /** | |
715 | * drm_fb_helper_release_fbi - dealloc fb_info and its members | |
716 | * @fb_helper: driver-allocated fbdev helper | |
717 | * | |
718 | * A helper to free memory taken by fb_info and the members cmap and | |
719 | * apertures | |
720 | */ | |
721 | void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper) | |
722 | { | |
723 | if (fb_helper) { | |
724 | struct fb_info *info = fb_helper->fbdev; | |
725 | ||
726 | if (info) { | |
727 | if (info->cmap.len) | |
728 | fb_dealloc_cmap(&info->cmap); | |
729 | framebuffer_release(info); | |
730 | } | |
731 | ||
732 | fb_helper->fbdev = NULL; | |
733 | } | |
734 | } | |
735 | EXPORT_SYMBOL(drm_fb_helper_release_fbi); | |
736 | ||
4abe3520 DA |
737 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
738 | { | |
739 | if (!list_empty(&fb_helper->kernel_fb_list)) { | |
740 | list_del(&fb_helper->kernel_fb_list); | |
741 | if (list_empty(&kernel_fb_helper_list)) { | |
4abe3520 DA |
742 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
743 | } | |
744 | } | |
745 | ||
746 | drm_fb_helper_crtc_free(fb_helper); | |
747 | ||
4abe3520 DA |
748 | } |
749 | EXPORT_SYMBOL(drm_fb_helper_fini); | |
785b93ef | 750 | |
c850cb78 | 751 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
b8c00ac5 DA |
752 | u16 blue, u16 regno, struct fb_info *info) |
753 | { | |
754 | struct drm_fb_helper *fb_helper = info->par; | |
755 | struct drm_framebuffer *fb = fb_helper->fb; | |
756 | int pindex; | |
757 | ||
c850cb78 DA |
758 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
759 | u32 *palette; | |
760 | u32 value; | |
761 | /* place color in psuedopalette */ | |
762 | if (regno > 16) | |
763 | return -EINVAL; | |
764 | palette = (u32 *)info->pseudo_palette; | |
765 | red >>= (16 - info->var.red.length); | |
766 | green >>= (16 - info->var.green.length); | |
767 | blue >>= (16 - info->var.blue.length); | |
768 | value = (red << info->var.red.offset) | | |
769 | (green << info->var.green.offset) | | |
770 | (blue << info->var.blue.offset); | |
9da12b6a RC |
771 | if (info->var.transp.length > 0) { |
772 | u32 mask = (1 << info->var.transp.length) - 1; | |
773 | mask <<= info->var.transp.offset; | |
774 | value |= mask; | |
775 | } | |
c850cb78 DA |
776 | palette[regno] = value; |
777 | return 0; | |
778 | } | |
779 | ||
04c0c569 VS |
780 | /* |
781 | * The driver really shouldn't advertise pseudo/directcolor | |
782 | * visuals if it can't deal with the palette. | |
783 | */ | |
784 | if (WARN_ON(!fb_helper->funcs->gamma_set || | |
785 | !fb_helper->funcs->gamma_get)) | |
786 | return -EINVAL; | |
787 | ||
b8c00ac5 DA |
788 | pindex = regno; |
789 | ||
790 | if (fb->bits_per_pixel == 16) { | |
791 | pindex = regno << 3; | |
792 | ||
793 | if (fb->depth == 16 && regno > 63) | |
c850cb78 | 794 | return -EINVAL; |
b8c00ac5 | 795 | if (fb->depth == 15 && regno > 31) |
c850cb78 | 796 | return -EINVAL; |
b8c00ac5 DA |
797 | |
798 | if (fb->depth == 16) { | |
799 | u16 r, g, b; | |
800 | int i; | |
801 | if (regno < 32) { | |
802 | for (i = 0; i < 8; i++) | |
803 | fb_helper->funcs->gamma_set(crtc, red, | |
804 | green, blue, pindex + i); | |
805 | } | |
806 | ||
807 | fb_helper->funcs->gamma_get(crtc, &r, | |
808 | &g, &b, | |
809 | pindex >> 1); | |
810 | ||
811 | for (i = 0; i < 4; i++) | |
812 | fb_helper->funcs->gamma_set(crtc, r, | |
813 | green, b, | |
814 | (pindex >> 1) + i); | |
815 | } | |
816 | } | |
817 | ||
818 | if (fb->depth != 16) | |
819 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); | |
c850cb78 | 820 | return 0; |
b8c00ac5 DA |
821 | } |
822 | ||
207fd329 DV |
823 | /** |
824 | * drm_fb_helper_setcmap - implementation for ->fb_setcmap | |
825 | * @cmap: cmap to set | |
826 | * @info: fbdev registered by the helper | |
827 | */ | |
068143d3 DA |
828 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
829 | { | |
830 | struct drm_fb_helper *fb_helper = info->par; | |
8391a3d5 | 831 | struct drm_device *dev = fb_helper->dev; |
be26a66d | 832 | const struct drm_crtc_helper_funcs *crtc_funcs; |
068143d3 DA |
833 | u16 *red, *green, *blue, *transp; |
834 | struct drm_crtc *crtc; | |
062ac622 | 835 | int i, j, rc = 0; |
068143d3 DA |
836 | int start; |
837 | ||
9aa609e1 RW |
838 | if (__drm_modeset_lock_all(dev, !!oops_in_progress)) { |
839 | return -EBUSY; | |
840 | } | |
8391a3d5 VS |
841 | if (!drm_fb_helper_is_bound(fb_helper)) { |
842 | drm_modeset_unlock_all(dev); | |
843 | return -EBUSY; | |
844 | } | |
845 | ||
8be48d92 DA |
846 | for (i = 0; i < fb_helper->crtc_count; i++) { |
847 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
848 | crtc_funcs = crtc->helper_private; | |
068143d3 DA |
849 | |
850 | red = cmap->red; | |
851 | green = cmap->green; | |
852 | blue = cmap->blue; | |
853 | transp = cmap->transp; | |
854 | start = cmap->start; | |
855 | ||
062ac622 | 856 | for (j = 0; j < cmap->len; j++) { |
068143d3 DA |
857 | u16 hred, hgreen, hblue, htransp = 0xffff; |
858 | ||
859 | hred = *red++; | |
860 | hgreen = *green++; | |
861 | hblue = *blue++; | |
862 | ||
863 | if (transp) | |
864 | htransp = *transp++; | |
865 | ||
c850cb78 DA |
866 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
867 | if (rc) | |
8391a3d5 | 868 | goto out; |
068143d3 | 869 | } |
04c0c569 VS |
870 | if (crtc_funcs->load_lut) |
871 | crtc_funcs->load_lut(crtc); | |
068143d3 | 872 | } |
8391a3d5 VS |
873 | out: |
874 | drm_modeset_unlock_all(dev); | |
068143d3 DA |
875 | return rc; |
876 | } | |
877 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
878 | ||
207fd329 DV |
879 | /** |
880 | * drm_fb_helper_check_var - implementation for ->fb_check_var | |
881 | * @var: screeninfo to check | |
882 | * @info: fbdev registered by the helper | |
883 | */ | |
785b93ef DA |
884 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
885 | struct fb_info *info) | |
886 | { | |
887 | struct drm_fb_helper *fb_helper = info->par; | |
888 | struct drm_framebuffer *fb = fb_helper->fb; | |
889 | int depth; | |
890 | ||
f90ebd9e | 891 | if (var->pixclock != 0 || in_dbg_master()) |
785b93ef DA |
892 | return -EINVAL; |
893 | ||
894 | /* Need to resize the fb object !!! */ | |
62fb376e CW |
895 | if (var->bits_per_pixel > fb->bits_per_pixel || |
896 | var->xres > fb->width || var->yres > fb->height || | |
897 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { | |
509c7d83 | 898 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " |
62fb376e CW |
899 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
900 | var->xres, var->yres, var->bits_per_pixel, | |
901 | var->xres_virtual, var->yres_virtual, | |
509c7d83 | 902 | fb->width, fb->height, fb->bits_per_pixel); |
785b93ef DA |
903 | return -EINVAL; |
904 | } | |
905 | ||
906 | switch (var->bits_per_pixel) { | |
907 | case 16: | |
908 | depth = (var->green.length == 6) ? 16 : 15; | |
909 | break; | |
910 | case 32: | |
911 | depth = (var->transp.length > 0) ? 32 : 24; | |
912 | break; | |
913 | default: | |
914 | depth = var->bits_per_pixel; | |
915 | break; | |
916 | } | |
917 | ||
918 | switch (depth) { | |
919 | case 8: | |
920 | var->red.offset = 0; | |
921 | var->green.offset = 0; | |
922 | var->blue.offset = 0; | |
923 | var->red.length = 8; | |
924 | var->green.length = 8; | |
925 | var->blue.length = 8; | |
926 | var->transp.length = 0; | |
927 | var->transp.offset = 0; | |
928 | break; | |
929 | case 15: | |
930 | var->red.offset = 10; | |
931 | var->green.offset = 5; | |
932 | var->blue.offset = 0; | |
933 | var->red.length = 5; | |
934 | var->green.length = 5; | |
935 | var->blue.length = 5; | |
936 | var->transp.length = 1; | |
937 | var->transp.offset = 15; | |
938 | break; | |
939 | case 16: | |
940 | var->red.offset = 11; | |
941 | var->green.offset = 5; | |
942 | var->blue.offset = 0; | |
943 | var->red.length = 5; | |
944 | var->green.length = 6; | |
945 | var->blue.length = 5; | |
946 | var->transp.length = 0; | |
947 | var->transp.offset = 0; | |
948 | break; | |
949 | case 24: | |
950 | var->red.offset = 16; | |
951 | var->green.offset = 8; | |
952 | var->blue.offset = 0; | |
953 | var->red.length = 8; | |
954 | var->green.length = 8; | |
955 | var->blue.length = 8; | |
956 | var->transp.length = 0; | |
957 | var->transp.offset = 0; | |
958 | break; | |
959 | case 32: | |
960 | var->red.offset = 16; | |
961 | var->green.offset = 8; | |
962 | var->blue.offset = 0; | |
963 | var->red.length = 8; | |
964 | var->green.length = 8; | |
965 | var->blue.length = 8; | |
966 | var->transp.length = 8; | |
967 | var->transp.offset = 24; | |
968 | break; | |
969 | default: | |
970 | return -EINVAL; | |
971 | } | |
972 | return 0; | |
973 | } | |
974 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
975 | ||
207fd329 DV |
976 | /** |
977 | * drm_fb_helper_set_par - implementation for ->fb_set_par | |
978 | * @info: fbdev registered by the helper | |
979 | * | |
980 | * This will let fbcon do the mode init and is called at initialization time by | |
981 | * the fbdev core when registering the driver, and later on through the hotplug | |
982 | * callback. | |
983 | */ | |
785b93ef DA |
984 | int drm_fb_helper_set_par(struct fb_info *info) |
985 | { | |
986 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 987 | struct fb_var_screeninfo *var = &info->var; |
785b93ef | 988 | |
5349ef31 | 989 | if (var->pixclock != 0) { |
172e91f5 | 990 | DRM_ERROR("PIXEL CLOCK SET\n"); |
785b93ef DA |
991 | return -EINVAL; |
992 | } | |
993 | ||
5ea1f752 | 994 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
4abe3520 | 995 | |
785b93ef DA |
996 | return 0; |
997 | } | |
998 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
999 | ||
207fd329 DV |
1000 | /** |
1001 | * drm_fb_helper_pan_display - implementation for ->fb_pan_display | |
1002 | * @var: updated screen information | |
1003 | * @info: fbdev registered by the helper | |
1004 | */ | |
785b93ef DA |
1005 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
1006 | struct fb_info *info) | |
1007 | { | |
1008 | struct drm_fb_helper *fb_helper = info->par; | |
1009 | struct drm_device *dev = fb_helper->dev; | |
1010 | struct drm_mode_set *modeset; | |
785b93ef DA |
1011 | int ret = 0; |
1012 | int i; | |
1013 | ||
9aa609e1 RW |
1014 | if (__drm_modeset_lock_all(dev, !!oops_in_progress)) { |
1015 | return -EBUSY; | |
1016 | } | |
20c60c35 DV |
1017 | if (!drm_fb_helper_is_bound(fb_helper)) { |
1018 | drm_modeset_unlock_all(dev); | |
1019 | return -EBUSY; | |
1020 | } | |
1021 | ||
8be48d92 | 1022 | for (i = 0; i < fb_helper->crtc_count; i++) { |
785b93ef DA |
1023 | modeset = &fb_helper->crtc_info[i].mode_set; |
1024 | ||
1025 | modeset->x = var->xoffset; | |
1026 | modeset->y = var->yoffset; | |
1027 | ||
1028 | if (modeset->num_connectors) { | |
2d13b679 | 1029 | ret = drm_mode_set_config_internal(modeset); |
785b93ef DA |
1030 | if (!ret) { |
1031 | info->var.xoffset = var->xoffset; | |
1032 | info->var.yoffset = var->yoffset; | |
1033 | } | |
1034 | } | |
1035 | } | |
84849903 | 1036 | drm_modeset_unlock_all(dev); |
785b93ef DA |
1037 | return ret; |
1038 | } | |
1039 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
1040 | ||
8acf658a | 1041 | /* |
207fd329 DV |
1042 | * Allocates the backing storage and sets up the fbdev info structure through |
1043 | * the ->fb_probe callback and then registers the fbdev and sets up the panic | |
1044 | * notifier. | |
8acf658a | 1045 | */ |
de1ace5b DV |
1046 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
1047 | int preferred_bpp) | |
785b93ef | 1048 | { |
8acf658a | 1049 | int ret = 0; |
785b93ef | 1050 | int crtc_count = 0; |
4abe3520 | 1051 | int i; |
785b93ef | 1052 | struct fb_info *info; |
38651674 | 1053 | struct drm_fb_helper_surface_size sizes; |
8be48d92 | 1054 | int gamma_size = 0; |
38651674 DA |
1055 | |
1056 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
1057 | sizes.surface_depth = 24; | |
1058 | sizes.surface_bpp = 32; | |
1059 | sizes.fb_width = (unsigned)-1; | |
1060 | sizes.fb_height = (unsigned)-1; | |
785b93ef | 1061 | |
b8c00ac5 DA |
1062 | /* if driver picks 8 or 16 by default use that |
1063 | for both depth/bpp */ | |
96081cdf | 1064 | if (preferred_bpp != sizes.surface_bpp) |
38651674 | 1065 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
96081cdf | 1066 | |
785b93ef | 1067 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
0b4c0f3f DA |
1068 | for (i = 0; i < fb_helper->connector_count; i++) { |
1069 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; | |
1794d257 | 1070 | struct drm_cmdline_mode *cmdline_mode; |
8ef8678c | 1071 | |
eaf99c74 | 1072 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
d50ba256 DA |
1073 | |
1074 | if (cmdline_mode->bpp_specified) { | |
1075 | switch (cmdline_mode->bpp) { | |
1076 | case 8: | |
38651674 | 1077 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
1078 | break; |
1079 | case 15: | |
38651674 DA |
1080 | sizes.surface_depth = 15; |
1081 | sizes.surface_bpp = 16; | |
d50ba256 DA |
1082 | break; |
1083 | case 16: | |
38651674 | 1084 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
1085 | break; |
1086 | case 24: | |
38651674 | 1087 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
1088 | break; |
1089 | case 32: | |
38651674 DA |
1090 | sizes.surface_depth = 24; |
1091 | sizes.surface_bpp = 32; | |
d50ba256 DA |
1092 | break; |
1093 | } | |
1094 | break; | |
1095 | } | |
1096 | } | |
1097 | ||
8be48d92 DA |
1098 | crtc_count = 0; |
1099 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1100 | struct drm_display_mode *desired_mode; | |
0e3704c9 RC |
1101 | struct drm_mode_set *mode_set; |
1102 | int x, y, j; | |
1103 | /* in case of tile group, are we the last tile vert or horiz? | |
1104 | * If no tile group you are always the last one both vertically | |
1105 | * and horizontally | |
1106 | */ | |
1107 | bool lastv = true, lasth = true; | |
675c8328 | 1108 | |
8be48d92 | 1109 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
0e3704c9 | 1110 | mode_set = &fb_helper->crtc_info[i].mode_set; |
675c8328 RC |
1111 | |
1112 | if (!desired_mode) | |
1113 | continue; | |
1114 | ||
1115 | crtc_count++; | |
1116 | ||
b0ee9e7f DA |
1117 | x = fb_helper->crtc_info[i].x; |
1118 | y = fb_helper->crtc_info[i].y; | |
675c8328 RC |
1119 | |
1120 | if (gamma_size == 0) | |
1121 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; | |
1122 | ||
1123 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); | |
1124 | sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); | |
0e3704c9 RC |
1125 | |
1126 | for (j = 0; j < mode_set->num_connectors; j++) { | |
1127 | struct drm_connector *connector = mode_set->connectors[j]; | |
1128 | if (connector->has_tile) { | |
1129 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); | |
1130 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); | |
1131 | /* cloning to multiple tiles is just crazy-talk, so: */ | |
1132 | break; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | if (lasth) | |
1137 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); | |
1138 | if (lastv) | |
1139 | sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); | |
785b93ef DA |
1140 | } |
1141 | ||
38651674 | 1142 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
785b93ef DA |
1143 | /* hmm everyone went away - assume VGA cable just fell out |
1144 | and will come back later. */ | |
eb1f8e4f | 1145 | DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); |
19b4b445 DA |
1146 | sizes.fb_width = sizes.surface_width = 1024; |
1147 | sizes.fb_height = sizes.surface_height = 768; | |
785b93ef DA |
1148 | } |
1149 | ||
38651674 | 1150 | /* push down into drivers */ |
8acf658a DV |
1151 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
1152 | if (ret < 0) | |
1153 | return ret; | |
785b93ef | 1154 | |
38651674 | 1155 | info = fb_helper->fbdev; |
785b93ef | 1156 | |
7e53f3a4 DV |
1157 | /* |
1158 | * Set the fb pointer - usually drm_setup_crtcs does this for hotplug | |
1159 | * events, but at init time drm_setup_crtcs needs to be called before | |
1160 | * the fb is allocated (since we need to figure out the desired size of | |
1161 | * the fb before we can allocate it ...). Hence we need to fix things up | |
1162 | * here again. | |
1163 | */ | |
96081cdf | 1164 | for (i = 0; i < fb_helper->crtc_count; i++) |
7e53f3a4 DV |
1165 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
1166 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; | |
1167 | ||
785b93ef | 1168 | |
8acf658a DV |
1169 | info->var.pixclock = 0; |
1170 | if (register_framebuffer(info) < 0) | |
1171 | return -EINVAL; | |
38651674 | 1172 | |
8acf658a DV |
1173 | dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", |
1174 | info->node, info->fix.id); | |
785b93ef | 1175 | |
785b93ef | 1176 | if (list_empty(&kernel_fb_helper_list)) { |
785b93ef DA |
1177 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
1178 | } | |
8acf658a DV |
1179 | |
1180 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
38651674 | 1181 | |
785b93ef DA |
1182 | return 0; |
1183 | } | |
785b93ef | 1184 | |
207fd329 DV |
1185 | /** |
1186 | * drm_fb_helper_fill_fix - initializes fixed fbdev information | |
1187 | * @info: fbdev registered by the helper | |
1188 | * @pitch: desired pitch | |
1189 | * @depth: desired depth | |
1190 | * | |
1191 | * Helper to fill in the fixed fbdev information useful for a non-accelerated | |
1192 | * fbdev emulations. Drivers which support acceleration methods which impose | |
1193 | * additional constraints need to set up their own limits. | |
1194 | * | |
1195 | * Drivers should call this (or their equivalent setup code) from their | |
1196 | * ->fb_probe callback. | |
1197 | */ | |
3632ef89 DA |
1198 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
1199 | uint32_t depth) | |
1200 | { | |
1201 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
1202 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : | |
1203 | FB_VISUAL_TRUECOLOR; | |
1204 | info->fix.mmio_start = 0; | |
1205 | info->fix.mmio_len = 0; | |
1206 | info->fix.type_aux = 0; | |
1207 | info->fix.xpanstep = 1; /* doing it in hw */ | |
1208 | info->fix.ypanstep = 1; /* doing it in hw */ | |
1209 | info->fix.ywrapstep = 0; | |
1210 | info->fix.accel = FB_ACCEL_NONE; | |
3632ef89 DA |
1211 | |
1212 | info->fix.line_length = pitch; | |
1213 | return; | |
1214 | } | |
1215 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); | |
1216 | ||
207fd329 DV |
1217 | /** |
1218 | * drm_fb_helper_fill_var - initalizes variable fbdev information | |
1219 | * @info: fbdev instance to set up | |
1220 | * @fb_helper: fb helper instance to use as template | |
1221 | * @fb_width: desired fb width | |
1222 | * @fb_height: desired fb height | |
1223 | * | |
1224 | * Sets up the variable fbdev metainformation from the given fb helper instance | |
1225 | * and the drm framebuffer allocated in fb_helper->fb. | |
1226 | * | |
1227 | * Drivers should call this (or their equivalent setup code) from their | |
1228 | * ->fb_probe callback after having allocated the fbdev backing | |
1229 | * storage framebuffer. | |
1230 | */ | |
38651674 | 1231 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
785b93ef DA |
1232 | uint32_t fb_width, uint32_t fb_height) |
1233 | { | |
38651674 DA |
1234 | struct drm_framebuffer *fb = fb_helper->fb; |
1235 | info->pseudo_palette = fb_helper->pseudo_palette; | |
785b93ef DA |
1236 | info->var.xres_virtual = fb->width; |
1237 | info->var.yres_virtual = fb->height; | |
1238 | info->var.bits_per_pixel = fb->bits_per_pixel; | |
57084d05 | 1239 | info->var.accel_flags = FB_ACCELF_TEXT; |
785b93ef DA |
1240 | info->var.xoffset = 0; |
1241 | info->var.yoffset = 0; | |
1242 | info->var.activate = FB_ACTIVATE_NOW; | |
1243 | info->var.height = -1; | |
1244 | info->var.width = -1; | |
1245 | ||
1246 | switch (fb->depth) { | |
1247 | case 8: | |
1248 | info->var.red.offset = 0; | |
1249 | info->var.green.offset = 0; | |
1250 | info->var.blue.offset = 0; | |
1251 | info->var.red.length = 8; /* 8bit DAC */ | |
1252 | info->var.green.length = 8; | |
1253 | info->var.blue.length = 8; | |
1254 | info->var.transp.offset = 0; | |
1255 | info->var.transp.length = 0; | |
1256 | break; | |
1257 | case 15: | |
1258 | info->var.red.offset = 10; | |
1259 | info->var.green.offset = 5; | |
1260 | info->var.blue.offset = 0; | |
1261 | info->var.red.length = 5; | |
1262 | info->var.green.length = 5; | |
1263 | info->var.blue.length = 5; | |
1264 | info->var.transp.offset = 15; | |
1265 | info->var.transp.length = 1; | |
1266 | break; | |
1267 | case 16: | |
1268 | info->var.red.offset = 11; | |
1269 | info->var.green.offset = 5; | |
1270 | info->var.blue.offset = 0; | |
1271 | info->var.red.length = 5; | |
1272 | info->var.green.length = 6; | |
1273 | info->var.blue.length = 5; | |
1274 | info->var.transp.offset = 0; | |
1275 | break; | |
1276 | case 24: | |
1277 | info->var.red.offset = 16; | |
1278 | info->var.green.offset = 8; | |
1279 | info->var.blue.offset = 0; | |
1280 | info->var.red.length = 8; | |
1281 | info->var.green.length = 8; | |
1282 | info->var.blue.length = 8; | |
1283 | info->var.transp.offset = 0; | |
1284 | info->var.transp.length = 0; | |
1285 | break; | |
1286 | case 32: | |
1287 | info->var.red.offset = 16; | |
1288 | info->var.green.offset = 8; | |
1289 | info->var.blue.offset = 0; | |
1290 | info->var.red.length = 8; | |
1291 | info->var.green.length = 8; | |
1292 | info->var.blue.length = 8; | |
1293 | info->var.transp.offset = 24; | |
1294 | info->var.transp.length = 8; | |
1295 | break; | |
1296 | default: | |
1297 | break; | |
1298 | } | |
1299 | ||
1300 | info->var.xres = fb_width; | |
1301 | info->var.yres = fb_height; | |
1302 | } | |
1303 | EXPORT_SYMBOL(drm_fb_helper_fill_var); | |
38651674 | 1304 | |
0b4c0f3f DA |
1305 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
1306 | uint32_t maxX, | |
1307 | uint32_t maxY) | |
38651674 DA |
1308 | { |
1309 | struct drm_connector *connector; | |
1310 | int count = 0; | |
0b4c0f3f | 1311 | int i; |
38651674 | 1312 | |
0b4c0f3f DA |
1313 | for (i = 0; i < fb_helper->connector_count; i++) { |
1314 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1315 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
1316 | } | |
1317 | ||
1318 | return count; | |
1319 | } | |
1320 | ||
2f1046f3 | 1321 | struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
38651674 DA |
1322 | { |
1323 | struct drm_display_mode *mode; | |
1324 | ||
0b4c0f3f | 1325 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
9d3de138 DV |
1326 | if (mode->hdisplay > width || |
1327 | mode->vdisplay > height) | |
38651674 DA |
1328 | continue; |
1329 | if (mode->type & DRM_MODE_TYPE_PREFERRED) | |
1330 | return mode; | |
1331 | } | |
1332 | return NULL; | |
1333 | } | |
2f1046f3 | 1334 | EXPORT_SYMBOL(drm_has_preferred_mode); |
38651674 | 1335 | |
0b4c0f3f | 1336 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
38651674 | 1337 | { |
eaf99c74 | 1338 | return fb_connector->connector->cmdline_mode.specified; |
38651674 DA |
1339 | } |
1340 | ||
2f1046f3 | 1341 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
0b4c0f3f | 1342 | int width, int height) |
38651674 | 1343 | { |
1794d257 | 1344 | struct drm_cmdline_mode *cmdline_mode; |
f3af5c7d | 1345 | struct drm_display_mode *mode; |
c683f427 | 1346 | bool prefer_non_interlace; |
38651674 | 1347 | |
eaf99c74 | 1348 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
38651674 | 1349 | if (cmdline_mode->specified == false) |
f3af5c7d | 1350 | return NULL; |
38651674 DA |
1351 | |
1352 | /* attempt to find a matching mode in the list of modes | |
1353 | * we have gotten so far, if not add a CVT mode that conforms | |
1354 | */ | |
1355 | if (cmdline_mode->rb || cmdline_mode->margins) | |
1356 | goto create_mode; | |
1357 | ||
c683f427 | 1358 | prefer_non_interlace = !cmdline_mode->interlace; |
f3af5c7d | 1359 | again: |
0b4c0f3f | 1360 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
38651674 DA |
1361 | /* check width/height */ |
1362 | if (mode->hdisplay != cmdline_mode->xres || | |
1363 | mode->vdisplay != cmdline_mode->yres) | |
1364 | continue; | |
1365 | ||
1366 | if (cmdline_mode->refresh_specified) { | |
1367 | if (mode->vrefresh != cmdline_mode->refresh) | |
1368 | continue; | |
1369 | } | |
1370 | ||
1371 | if (cmdline_mode->interlace) { | |
1372 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) | |
1373 | continue; | |
c683f427 TI |
1374 | } else if (prefer_non_interlace) { |
1375 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) | |
1376 | continue; | |
38651674 DA |
1377 | } |
1378 | return mode; | |
1379 | } | |
1380 | ||
c683f427 TI |
1381 | if (prefer_non_interlace) { |
1382 | prefer_non_interlace = false; | |
1383 | goto again; | |
1384 | } | |
1385 | ||
38651674 | 1386 | create_mode: |
1794d257 CW |
1387 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
1388 | cmdline_mode); | |
0b4c0f3f | 1389 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
38651674 DA |
1390 | return mode; |
1391 | } | |
2f1046f3 | 1392 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
38651674 DA |
1393 | |
1394 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) | |
1395 | { | |
1396 | bool enable; | |
1397 | ||
96081cdf | 1398 | if (strict) |
38651674 | 1399 | enable = connector->status == connector_status_connected; |
96081cdf | 1400 | else |
38651674 | 1401 | enable = connector->status != connector_status_disconnected; |
96081cdf | 1402 | |
38651674 DA |
1403 | return enable; |
1404 | } | |
1405 | ||
0b4c0f3f DA |
1406 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
1407 | bool *enabled) | |
38651674 DA |
1408 | { |
1409 | bool any_enabled = false; | |
1410 | struct drm_connector *connector; | |
1411 | int i = 0; | |
1412 | ||
0b4c0f3f DA |
1413 | for (i = 0; i < fb_helper->connector_count; i++) { |
1414 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1415 | enabled[i] = drm_connector_enabled(connector, true); |
1416 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, | |
1417 | enabled[i] ? "yes" : "no"); | |
1418 | any_enabled |= enabled[i]; | |
38651674 DA |
1419 | } |
1420 | ||
1421 | if (any_enabled) | |
1422 | return; | |
1423 | ||
0b4c0f3f DA |
1424 | for (i = 0; i < fb_helper->connector_count; i++) { |
1425 | connector = fb_helper->connector_info[i]->connector; | |
38651674 | 1426 | enabled[i] = drm_connector_enabled(connector, false); |
38651674 DA |
1427 | } |
1428 | } | |
1429 | ||
1d42bbc8 DA |
1430 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
1431 | struct drm_display_mode **modes, | |
b0ee9e7f | 1432 | struct drm_fb_offset *offsets, |
1d42bbc8 DA |
1433 | bool *enabled, int width, int height) |
1434 | { | |
1435 | int count, i, j; | |
1436 | bool can_clone = false; | |
1437 | struct drm_fb_helper_connector *fb_helper_conn; | |
1438 | struct drm_display_mode *dmt_mode, *mode; | |
1439 | ||
1440 | /* only contemplate cloning in the single crtc case */ | |
1441 | if (fb_helper->crtc_count > 1) | |
1442 | return false; | |
1443 | ||
1444 | count = 0; | |
1445 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1446 | if (enabled[i]) | |
1447 | count++; | |
1448 | } | |
1449 | ||
1450 | /* only contemplate cloning if more than one connector is enabled */ | |
1451 | if (count <= 1) | |
1452 | return false; | |
1453 | ||
1454 | /* check the command line or if nothing common pick 1024x768 */ | |
1455 | can_clone = true; | |
1456 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1457 | if (!enabled[i]) | |
1458 | continue; | |
1459 | fb_helper_conn = fb_helper->connector_info[i]; | |
1460 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); | |
1461 | if (!modes[i]) { | |
1462 | can_clone = false; | |
1463 | break; | |
1464 | } | |
1465 | for (j = 0; j < i; j++) { | |
1466 | if (!enabled[j]) | |
1467 | continue; | |
1468 | if (!drm_mode_equal(modes[j], modes[i])) | |
1469 | can_clone = false; | |
1470 | } | |
1471 | } | |
1472 | ||
1473 | if (can_clone) { | |
1474 | DRM_DEBUG_KMS("can clone using command line\n"); | |
1475 | return true; | |
1476 | } | |
1477 | ||
1478 | /* try and find a 1024x768 mode on each connector */ | |
1479 | can_clone = true; | |
f6e252ba | 1480 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
1d42bbc8 DA |
1481 | |
1482 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1483 | ||
1484 | if (!enabled[i]) | |
1485 | continue; | |
1486 | ||
1487 | fb_helper_conn = fb_helper->connector_info[i]; | |
1488 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { | |
1489 | if (drm_mode_equal(mode, dmt_mode)) | |
1490 | modes[i] = mode; | |
1491 | } | |
1492 | if (!modes[i]) | |
1493 | can_clone = false; | |
1494 | } | |
1495 | ||
1496 | if (can_clone) { | |
1497 | DRM_DEBUG_KMS("can clone using 1024x768\n"); | |
1498 | return true; | |
1499 | } | |
1500 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); | |
1501 | return false; | |
1502 | } | |
1503 | ||
b0ee9e7f DA |
1504 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
1505 | struct drm_display_mode **modes, | |
1506 | struct drm_fb_offset *offsets, | |
1507 | int idx, | |
1508 | int h_idx, int v_idx) | |
1509 | { | |
1510 | struct drm_fb_helper_connector *fb_helper_conn; | |
1511 | int i; | |
1512 | int hoffset = 0, voffset = 0; | |
1513 | ||
1514 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1515 | fb_helper_conn = fb_helper->connector_info[i]; | |
1516 | if (!fb_helper_conn->connector->has_tile) | |
1517 | continue; | |
1518 | ||
1519 | if (!modes[i] && (h_idx || v_idx)) { | |
1520 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, | |
1521 | fb_helper_conn->connector->base.id); | |
1522 | continue; | |
1523 | } | |
1524 | if (fb_helper_conn->connector->tile_h_loc < h_idx) | |
1525 | hoffset += modes[i]->hdisplay; | |
1526 | ||
1527 | if (fb_helper_conn->connector->tile_v_loc < v_idx) | |
1528 | voffset += modes[i]->vdisplay; | |
1529 | } | |
1530 | offsets[idx].x = hoffset; | |
1531 | offsets[idx].y = voffset; | |
1532 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); | |
1533 | return 0; | |
1534 | } | |
1535 | ||
0b4c0f3f | 1536 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
38651674 | 1537 | struct drm_display_mode **modes, |
b0ee9e7f | 1538 | struct drm_fb_offset *offsets, |
38651674 DA |
1539 | bool *enabled, int width, int height) |
1540 | { | |
0b4c0f3f DA |
1541 | struct drm_fb_helper_connector *fb_helper_conn; |
1542 | int i; | |
b0ee9e7f DA |
1543 | uint64_t conn_configured = 0, mask; |
1544 | int tile_pass = 0; | |
1545 | mask = (1 << fb_helper->connector_count) - 1; | |
1546 | retry: | |
0b4c0f3f DA |
1547 | for (i = 0; i < fb_helper->connector_count; i++) { |
1548 | fb_helper_conn = fb_helper->connector_info[i]; | |
38651674 | 1549 | |
b0ee9e7f DA |
1550 | if (conn_configured & (1 << i)) |
1551 | continue; | |
1552 | ||
1553 | if (enabled[i] == false) { | |
1554 | conn_configured |= (1 << i); | |
1555 | continue; | |
1556 | } | |
1557 | ||
1558 | /* first pass over all the untiled connectors */ | |
1559 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) | |
38651674 | 1560 | continue; |
38651674 | 1561 | |
b0ee9e7f DA |
1562 | if (tile_pass == 1) { |
1563 | if (fb_helper_conn->connector->tile_h_loc != 0 || | |
1564 | fb_helper_conn->connector->tile_v_loc != 0) | |
1565 | continue; | |
1566 | ||
1567 | } else { | |
1568 | if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && | |
1569 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) | |
1570 | /* if this tile_pass doesn't cover any of the tiles - keep going */ | |
1571 | continue; | |
1572 | ||
1573 | /* find the tile offsets for this pass - need | |
1574 | to find all tiles left and above */ | |
1575 | drm_get_tile_offsets(fb_helper, modes, offsets, | |
1576 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); | |
1577 | } | |
38651674 | 1578 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
0b4c0f3f | 1579 | fb_helper_conn->connector->base.id); |
38651674 DA |
1580 | |
1581 | /* got for command line mode first */ | |
0b4c0f3f | 1582 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
38651674 | 1583 | if (!modes[i]) { |
b0ee9e7f DA |
1584 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
1585 | fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); | |
0b4c0f3f | 1586 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
38651674 DA |
1587 | } |
1588 | /* No preferred modes, pick one off the list */ | |
0b4c0f3f DA |
1589 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
1590 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) | |
38651674 DA |
1591 | break; |
1592 | } | |
1593 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : | |
1594 | "none"); | |
b0ee9e7f DA |
1595 | conn_configured |= (1 << i); |
1596 | } | |
1597 | ||
1598 | if ((conn_configured & mask) != mask) { | |
1599 | tile_pass++; | |
1600 | goto retry; | |
38651674 DA |
1601 | } |
1602 | return true; | |
1603 | } | |
1604 | ||
8be48d92 DA |
1605 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
1606 | struct drm_fb_helper_crtc **best_crtcs, | |
38651674 DA |
1607 | struct drm_display_mode **modes, |
1608 | int n, int width, int height) | |
1609 | { | |
1610 | int c, o; | |
8be48d92 | 1611 | struct drm_device *dev = fb_helper->dev; |
38651674 | 1612 | struct drm_connector *connector; |
be26a66d | 1613 | const struct drm_connector_helper_funcs *connector_funcs; |
38651674 | 1614 | struct drm_encoder *encoder; |
38651674 | 1615 | int my_score, best_score, score; |
8be48d92 | 1616 | struct drm_fb_helper_crtc **crtcs, *crtc; |
0b4c0f3f | 1617 | struct drm_fb_helper_connector *fb_helper_conn; |
38651674 | 1618 | |
0b4c0f3f | 1619 | if (n == fb_helper->connector_count) |
38651674 | 1620 | return 0; |
0b4c0f3f DA |
1621 | |
1622 | fb_helper_conn = fb_helper->connector_info[n]; | |
1623 | connector = fb_helper_conn->connector; | |
38651674 DA |
1624 | |
1625 | best_crtcs[n] = NULL; | |
8be48d92 | 1626 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
38651674 DA |
1627 | if (modes[n] == NULL) |
1628 | return best_score; | |
1629 | ||
8be48d92 DA |
1630 | crtcs = kzalloc(dev->mode_config.num_connector * |
1631 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); | |
38651674 DA |
1632 | if (!crtcs) |
1633 | return best_score; | |
1634 | ||
1635 | my_score = 1; | |
1636 | if (connector->status == connector_status_connected) | |
1637 | my_score++; | |
0b4c0f3f | 1638 | if (drm_has_cmdline_mode(fb_helper_conn)) |
38651674 | 1639 | my_score++; |
0b4c0f3f | 1640 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
38651674 DA |
1641 | my_score++; |
1642 | ||
1643 | connector_funcs = connector->helper_private; | |
1644 | encoder = connector_funcs->best_encoder(connector); | |
1645 | if (!encoder) | |
1646 | goto out; | |
1647 | ||
38651674 DA |
1648 | /* select a crtc for this connector and then attempt to configure |
1649 | remaining connectors */ | |
8be48d92 DA |
1650 | for (c = 0; c < fb_helper->crtc_count; c++) { |
1651 | crtc = &fb_helper->crtc_info[c]; | |
38651674 | 1652 | |
96081cdf | 1653 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
38651674 | 1654 | continue; |
38651674 DA |
1655 | |
1656 | for (o = 0; o < n; o++) | |
1657 | if (best_crtcs[o] == crtc) | |
1658 | break; | |
1659 | ||
1660 | if (o < n) { | |
1d42bbc8 DA |
1661 | /* ignore cloning unless only a single crtc */ |
1662 | if (fb_helper->crtc_count > 1) | |
1663 | continue; | |
1664 | ||
1665 | if (!drm_mode_equal(modes[o], modes[n])) | |
1666 | continue; | |
38651674 DA |
1667 | } |
1668 | ||
1669 | crtcs[n] = crtc; | |
8be48d92 DA |
1670 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
1671 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, | |
38651674 DA |
1672 | width, height); |
1673 | if (score > best_score) { | |
38651674 DA |
1674 | best_score = score; |
1675 | memcpy(best_crtcs, crtcs, | |
1676 | dev->mode_config.num_connector * | |
8be48d92 | 1677 | sizeof(struct drm_fb_helper_crtc *)); |
38651674 | 1678 | } |
38651674 DA |
1679 | } |
1680 | out: | |
1681 | kfree(crtcs); | |
1682 | return best_score; | |
1683 | } | |
1684 | ||
8be48d92 | 1685 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
38651674 | 1686 | { |
8be48d92 DA |
1687 | struct drm_device *dev = fb_helper->dev; |
1688 | struct drm_fb_helper_crtc **crtcs; | |
38651674 | 1689 | struct drm_display_mode **modes; |
b0ee9e7f | 1690 | struct drm_fb_offset *offsets; |
8be48d92 | 1691 | struct drm_mode_set *modeset; |
38651674 DA |
1692 | bool *enabled; |
1693 | int width, height; | |
11e17a08 | 1694 | int i; |
38651674 DA |
1695 | |
1696 | DRM_DEBUG_KMS("\n"); | |
1697 | ||
1698 | width = dev->mode_config.max_width; | |
1699 | height = dev->mode_config.max_height; | |
1700 | ||
38651674 | 1701 | crtcs = kcalloc(dev->mode_config.num_connector, |
8be48d92 | 1702 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
38651674 DA |
1703 | modes = kcalloc(dev->mode_config.num_connector, |
1704 | sizeof(struct drm_display_mode *), GFP_KERNEL); | |
b0ee9e7f DA |
1705 | offsets = kcalloc(dev->mode_config.num_connector, |
1706 | sizeof(struct drm_fb_offset), GFP_KERNEL); | |
38651674 DA |
1707 | enabled = kcalloc(dev->mode_config.num_connector, |
1708 | sizeof(bool), GFP_KERNEL); | |
b0ee9e7f | 1709 | if (!crtcs || !modes || !enabled || !offsets) { |
8c5eaca0 SK |
1710 | DRM_ERROR("Memory allocation failed\n"); |
1711 | goto out; | |
1712 | } | |
1713 | ||
38651674 | 1714 | |
0b4c0f3f | 1715 | drm_enable_connectors(fb_helper, enabled); |
38651674 | 1716 | |
11e17a08 JB |
1717 | if (!(fb_helper->funcs->initial_config && |
1718 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, | |
b0ee9e7f | 1719 | offsets, |
11e17a08 JB |
1720 | enabled, width, height))) { |
1721 | memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); | |
1722 | memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); | |
b0ee9e7f | 1723 | memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0])); |
11e17a08 | 1724 | |
b0ee9e7f DA |
1725 | if (!drm_target_cloned(fb_helper, modes, offsets, |
1726 | enabled, width, height) && | |
1727 | !drm_target_preferred(fb_helper, modes, offsets, | |
1728 | enabled, width, height)) | |
1d42bbc8 | 1729 | DRM_ERROR("Unable to find initial modes\n"); |
38651674 | 1730 | |
11e17a08 JB |
1731 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
1732 | width, height); | |
38651674 | 1733 | |
11e17a08 JB |
1734 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
1735 | } | |
8be48d92 DA |
1736 | |
1737 | /* need to set the modesets up here for use later */ | |
1738 | /* fill out the connector<->crtc mappings into the modesets */ | |
1739 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1740 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1741 | modeset->num_connectors = 0; | |
7e53f3a4 | 1742 | modeset->fb = NULL; |
8be48d92 | 1743 | } |
38651674 | 1744 | |
0b4c0f3f | 1745 | for (i = 0; i < fb_helper->connector_count; i++) { |
38651674 | 1746 | struct drm_display_mode *mode = modes[i]; |
8be48d92 | 1747 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
b0ee9e7f | 1748 | struct drm_fb_offset *offset = &offsets[i]; |
8be48d92 | 1749 | modeset = &fb_crtc->mode_set; |
38651674 | 1750 | |
8be48d92 | 1751 | if (mode && fb_crtc) { |
b0ee9e7f DA |
1752 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
1753 | mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); | |
8be48d92 | 1754 | fb_crtc->desired_mode = mode; |
b0ee9e7f DA |
1755 | fb_crtc->x = offset->x; |
1756 | fb_crtc->y = offset->y; | |
8be48d92 DA |
1757 | if (modeset->mode) |
1758 | drm_mode_destroy(dev, modeset->mode); | |
1759 | modeset->mode = drm_mode_duplicate(dev, | |
1760 | fb_crtc->desired_mode); | |
0b4c0f3f | 1761 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
7e53f3a4 | 1762 | modeset->fb = fb_helper->fb; |
b0ee9e7f DA |
1763 | modeset->x = offset->x; |
1764 | modeset->y = offset->y; | |
38651674 | 1765 | } |
38651674 DA |
1766 | } |
1767 | ||
7e53f3a4 DV |
1768 | /* Clear out any old modes if there are no more connected outputs. */ |
1769 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1770 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1771 | if (modeset->num_connectors == 0) { | |
1772 | BUG_ON(modeset->fb); | |
7e53f3a4 DV |
1773 | if (modeset->mode) |
1774 | drm_mode_destroy(dev, modeset->mode); | |
1775 | modeset->mode = NULL; | |
1776 | } | |
1777 | } | |
8c5eaca0 | 1778 | out: |
38651674 DA |
1779 | kfree(crtcs); |
1780 | kfree(modes); | |
b0ee9e7f | 1781 | kfree(offsets); |
38651674 DA |
1782 | kfree(enabled); |
1783 | } | |
1784 | ||
1785 | /** | |
207fd329 | 1786 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
d0ddc033 DV |
1787 | * @fb_helper: fb_helper device struct |
1788 | * @bpp_sel: bpp value to use for the framebuffer configuration | |
38651674 | 1789 | * |
d0ddc033 | 1790 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
38651674 DA |
1791 | * At the moment, this is a cloned configuration across all heads with |
1792 | * a new framebuffer object as the backing store. | |
1793 | * | |
207fd329 DV |
1794 | * Note that this also registers the fbdev and so allows userspace to call into |
1795 | * the driver through the fbdev interfaces. | |
1796 | * | |
1797 | * This function will call down into the ->fb_probe callback to let | |
1798 | * the driver allocate and initialize the fbdev info structure and the drm | |
1799 | * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and | |
1800 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default | |
1801 | * values for the fbdev info structure. | |
1802 | * | |
38651674 DA |
1803 | * RETURNS: |
1804 | * Zero if everything went ok, nonzero otherwise. | |
1805 | */ | |
01934c2a | 1806 | int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
38651674 | 1807 | { |
8be48d92 | 1808 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1809 | int count = 0; |
1810 | ||
53f1904b | 1811 | mutex_lock(&dev->mode_config.mutex); |
0b4c0f3f DA |
1812 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
1813 | dev->mode_config.max_width, | |
1814 | dev->mode_config.max_height); | |
53f1904b | 1815 | mutex_unlock(&dev->mode_config.mutex); |
38651674 DA |
1816 | /* |
1817 | * we shouldn't end up with no modes here. | |
1818 | */ | |
96081cdf | 1819 | if (count == 0) |
d56b1b9d | 1820 | dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); |
96081cdf | 1821 | |
8be48d92 | 1822 | drm_setup_crtcs(fb_helper); |
38651674 | 1823 | |
4abe3520 | 1824 | return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
38651674 | 1825 | } |
8be48d92 | 1826 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 1827 | |
7394371d CW |
1828 | /** |
1829 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by | |
d0ddc033 | 1830 | * probing all the outputs attached to the fb |
7394371d CW |
1831 | * @fb_helper: the drm_fb_helper |
1832 | * | |
7394371d CW |
1833 | * Scan the connectors attached to the fb_helper and try to put together a |
1834 | * setup after *notification of a change in output configuration. | |
1835 | * | |
207fd329 DV |
1836 | * Called at runtime, takes the mode config locks to be able to check/change the |
1837 | * modeset configuration. Must be run from process context (which usually means | |
1838 | * either the output polling work or a work item launched from the driver's | |
1839 | * hotplug interrupt). | |
1840 | * | |
50c3dc97 DV |
1841 | * Note that drivers may call this even before calling |
1842 | * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows | |
1843 | * for a race-free fbcon setup and will make sure that the fbdev emulation will | |
1844 | * not miss any hotplug events. | |
207fd329 | 1845 | * |
7394371d CW |
1846 | * RETURNS: |
1847 | * 0 on success and a non-zero error code otherwise. | |
1848 | */ | |
1849 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) | |
38651674 | 1850 | { |
7394371d | 1851 | struct drm_device *dev = fb_helper->dev; |
51bbd276 | 1852 | u32 max_width, max_height; |
5c4426a7 | 1853 | |
89ced125 | 1854 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
50c3dc97 | 1855 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
4abe3520 | 1856 | fb_helper->delayed_hotplug = true; |
89ced125 | 1857 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
7394371d | 1858 | return 0; |
4abe3520 | 1859 | } |
eb1f8e4f | 1860 | DRM_DEBUG_KMS("\n"); |
4abe3520 | 1861 | |
eb1f8e4f DA |
1862 | max_width = fb_helper->fb->width; |
1863 | max_height = fb_helper->fb->height; | |
5c4426a7 | 1864 | |
51bbd276 | 1865 | drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); |
89ced125 DV |
1866 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
1867 | ||
1868 | drm_modeset_lock_all(dev); | |
eb1f8e4f | 1869 | drm_setup_crtcs(fb_helper); |
84849903 | 1870 | drm_modeset_unlock_all(dev); |
2180c3c7 DV |
1871 | drm_fb_helper_set_par(fb_helper->fbdev); |
1872 | ||
1873 | return 0; | |
5c4426a7 | 1874 | } |
eb1f8e4f | 1875 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
5c4426a7 | 1876 | |
6a108a14 | 1877 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
3ce05168 DF |
1878 | * but the module doesn't depend on any fb console symbols. At least |
1879 | * attempt to load fbcon to avoid leaving the system without a usable console. | |
1880 | */ | |
6a108a14 | 1881 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
3ce05168 DF |
1882 | static int __init drm_fb_helper_modinit(void) |
1883 | { | |
1884 | const char *name = "fbcon"; | |
1885 | struct module *fbcon; | |
1886 | ||
1887 | mutex_lock(&module_mutex); | |
1888 | fbcon = find_module(name); | |
1889 | mutex_unlock(&module_mutex); | |
1890 | ||
1891 | if (!fbcon) | |
1892 | request_module_nowait(name); | |
1893 | return 0; | |
1894 | } | |
1895 | ||
1896 | module_init(drm_fb_helper_modinit); | |
1897 | #endif |