]>
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 DA |
492 | |
493 | /* | |
3a8148c5 | 494 | * For each CRTC in this fb, turn the connectors on/off. |
785b93ef | 495 | */ |
84849903 | 496 | drm_modeset_lock_all(dev); |
20c60c35 DV |
497 | if (!drm_fb_helper_is_bound(fb_helper)) { |
498 | drm_modeset_unlock_all(dev); | |
499 | return; | |
500 | } | |
501 | ||
e87b2c42 | 502 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 | 503 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
785b93ef | 504 | |
8be48d92 DA |
505 | if (!crtc->enabled) |
506 | continue; | |
507 | ||
3a8148c5 | 508 | /* Walk the connectors & encoders on this fb turning them on/off */ |
023eb571 JB |
509 | for (j = 0; j < fb_helper->connector_count; j++) { |
510 | connector = fb_helper->connector_info[j]->connector; | |
e04190e0 | 511 | connector->funcs->dpms(connector, dpms_mode); |
58495563 | 512 | drm_object_property_set_value(&connector->base, |
3a8148c5 | 513 | dev->mode_config.dpms_property, dpms_mode); |
785b93ef | 514 | } |
785b93ef | 515 | } |
84849903 | 516 | drm_modeset_unlock_all(dev); |
785b93ef DA |
517 | } |
518 | ||
207fd329 DV |
519 | /** |
520 | * drm_fb_helper_blank - implementation for ->fb_blank | |
521 | * @blank: desired blanking state | |
522 | * @info: fbdev registered by the helper | |
523 | */ | |
785b93ef DA |
524 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
525 | { | |
c50bfd08 DV |
526 | if (oops_in_progress) |
527 | return -EBUSY; | |
528 | ||
785b93ef | 529 | switch (blank) { |
731b5a15 | 530 | /* Display: On; HSync: On, VSync: On */ |
785b93ef | 531 | case FB_BLANK_UNBLANK: |
3a8148c5 | 532 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
785b93ef | 533 | break; |
731b5a15 | 534 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 535 | case FB_BLANK_NORMAL: |
3a8148c5 | 536 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 537 | break; |
731b5a15 | 538 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef | 539 | case FB_BLANK_HSYNC_SUSPEND: |
3a8148c5 | 540 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 541 | break; |
731b5a15 | 542 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef | 543 | case FB_BLANK_VSYNC_SUSPEND: |
3a8148c5 | 544 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
785b93ef | 545 | break; |
731b5a15 | 546 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef | 547 | case FB_BLANK_POWERDOWN: |
3a8148c5 | 548 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
785b93ef DA |
549 | break; |
550 | } | |
551 | return 0; | |
552 | } | |
553 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
554 | ||
555 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) | |
556 | { | |
557 | int i; | |
558 | ||
0b4c0f3f DA |
559 | for (i = 0; i < helper->connector_count; i++) |
560 | kfree(helper->connector_info[i]); | |
561 | kfree(helper->connector_info); | |
a1b7736d | 562 | for (i = 0; i < helper->crtc_count; i++) { |
785b93ef | 563 | kfree(helper->crtc_info[i].mode_set.connectors); |
a1b7736d SH |
564 | if (helper->crtc_info[i].mode_set.mode) |
565 | drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); | |
566 | } | |
785b93ef DA |
567 | kfree(helper->crtc_info); |
568 | } | |
569 | ||
10a23102 TR |
570 | /** |
571 | * drm_fb_helper_prepare - setup a drm_fb_helper structure | |
572 | * @dev: DRM device | |
573 | * @helper: driver-allocated fbdev helper structure to set up | |
574 | * @funcs: pointer to structure of functions associate with this helper | |
575 | * | |
576 | * Sets up the bare minimum to make the framebuffer helper usable. This is | |
577 | * useful to implement race-free initialization of the polling helpers. | |
578 | */ | |
579 | void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, | |
580 | const struct drm_fb_helper_funcs *funcs) | |
581 | { | |
582 | INIT_LIST_HEAD(&helper->kernel_fb_list); | |
583 | helper->funcs = funcs; | |
584 | helper->dev = dev; | |
585 | } | |
586 | EXPORT_SYMBOL(drm_fb_helper_prepare); | |
587 | ||
207fd329 DV |
588 | /** |
589 | * drm_fb_helper_init - initialize a drm_fb_helper structure | |
590 | * @dev: drm device | |
591 | * @fb_helper: driver-allocated fbdev helper structure to initialize | |
592 | * @crtc_count: maximum number of crtcs to support in this fbdev emulation | |
593 | * @max_conn_count: max connector count | |
594 | * | |
595 | * This allocates the structures for the fbdev helper with the given limits. | |
596 | * Note that this won't yet touch the hardware (through the driver interfaces) | |
597 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() | |
598 | * to allow driver writes more control over the exact init sequence. | |
599 | * | |
10a23102 | 600 | * Drivers must call drm_fb_helper_prepare() before calling this function. |
207fd329 DV |
601 | * |
602 | * RETURNS: | |
603 | * Zero if everything went ok, nonzero otherwise. | |
604 | */ | |
4abe3520 DA |
605 | int drm_fb_helper_init(struct drm_device *dev, |
606 | struct drm_fb_helper *fb_helper, | |
eb1f8e4f | 607 | int crtc_count, int max_conn_count) |
785b93ef | 608 | { |
785b93ef | 609 | struct drm_crtc *crtc; |
785b93ef DA |
610 | int i; |
611 | ||
04cfe97e XL |
612 | if (!max_conn_count) |
613 | return -EINVAL; | |
614 | ||
4abe3520 DA |
615 | fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
616 | if (!fb_helper->crtc_info) | |
785b93ef DA |
617 | return -ENOMEM; |
618 | ||
4abe3520 DA |
619 | fb_helper->crtc_count = crtc_count; |
620 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); | |
621 | if (!fb_helper->connector_info) { | |
622 | kfree(fb_helper->crtc_info); | |
0b4c0f3f DA |
623 | return -ENOMEM; |
624 | } | |
65c2a89c | 625 | fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; |
4abe3520 | 626 | fb_helper->connector_count = 0; |
785b93ef DA |
627 | |
628 | for (i = 0; i < crtc_count; i++) { | |
4abe3520 | 629 | fb_helper->crtc_info[i].mode_set.connectors = |
785b93ef DA |
630 | kcalloc(max_conn_count, |
631 | sizeof(struct drm_connector *), | |
632 | GFP_KERNEL); | |
633 | ||
4a1b0714 | 634 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
785b93ef | 635 | goto out_free; |
4abe3520 | 636 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
785b93ef DA |
637 | } |
638 | ||
639 | i = 0; | |
6295d607 | 640 | drm_for_each_crtc(crtc, dev) { |
4abe3520 | 641 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
785b93ef DA |
642 | i++; |
643 | } | |
e9ad3181 | 644 | |
785b93ef DA |
645 | return 0; |
646 | out_free: | |
4abe3520 | 647 | drm_fb_helper_crtc_free(fb_helper); |
785b93ef DA |
648 | return -ENOMEM; |
649 | } | |
4abe3520 DA |
650 | EXPORT_SYMBOL(drm_fb_helper_init); |
651 | ||
b8017d6c AT |
652 | /** |
653 | * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members | |
654 | * @fb_helper: driver-allocated fbdev helper | |
655 | * | |
656 | * A helper to alloc fb_info and the members cmap and apertures. Called | |
657 | * by the driver within the fb_probe fb_helper callback function. | |
658 | * | |
659 | * RETURNS: | |
660 | * fb_info pointer if things went okay, pointer containing error code | |
661 | * otherwise | |
662 | */ | |
663 | struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) | |
664 | { | |
665 | struct device *dev = fb_helper->dev->dev; | |
666 | struct fb_info *info; | |
667 | int ret; | |
668 | ||
669 | info = framebuffer_alloc(0, dev); | |
670 | if (!info) | |
671 | return ERR_PTR(-ENOMEM); | |
672 | ||
673 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
674 | if (ret) | |
675 | goto err_release; | |
676 | ||
677 | info->apertures = alloc_apertures(1); | |
678 | if (!info->apertures) { | |
679 | ret = -ENOMEM; | |
680 | goto err_free_cmap; | |
681 | } | |
682 | ||
683 | fb_helper->fbdev = info; | |
684 | ||
685 | return info; | |
686 | ||
687 | err_free_cmap: | |
688 | fb_dealloc_cmap(&info->cmap); | |
689 | err_release: | |
690 | framebuffer_release(info); | |
691 | return ERR_PTR(ret); | |
692 | } | |
693 | EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); | |
694 | ||
695 | /** | |
696 | * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device | |
697 | * @fb_helper: driver-allocated fbdev helper | |
698 | * | |
699 | * A wrapper around unregister_framebuffer, to release the fb_info | |
700 | * framebuffer device | |
701 | */ | |
702 | void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) | |
703 | { | |
704 | if (fb_helper && fb_helper->fbdev) | |
705 | unregister_framebuffer(fb_helper->fbdev); | |
706 | } | |
707 | EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); | |
708 | ||
709 | /** | |
710 | * drm_fb_helper_release_fbi - dealloc fb_info and its members | |
711 | * @fb_helper: driver-allocated fbdev helper | |
712 | * | |
713 | * A helper to free memory taken by fb_info and the members cmap and | |
714 | * apertures | |
715 | */ | |
716 | void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper) | |
717 | { | |
718 | if (fb_helper) { | |
719 | struct fb_info *info = fb_helper->fbdev; | |
720 | ||
721 | if (info) { | |
722 | if (info->cmap.len) | |
723 | fb_dealloc_cmap(&info->cmap); | |
724 | framebuffer_release(info); | |
725 | } | |
726 | ||
727 | fb_helper->fbdev = NULL; | |
728 | } | |
729 | } | |
730 | EXPORT_SYMBOL(drm_fb_helper_release_fbi); | |
731 | ||
4abe3520 DA |
732 | void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) |
733 | { | |
734 | if (!list_empty(&fb_helper->kernel_fb_list)) { | |
735 | list_del(&fb_helper->kernel_fb_list); | |
736 | if (list_empty(&kernel_fb_helper_list)) { | |
4abe3520 DA |
737 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
738 | } | |
739 | } | |
740 | ||
741 | drm_fb_helper_crtc_free(fb_helper); | |
742 | ||
4abe3520 DA |
743 | } |
744 | EXPORT_SYMBOL(drm_fb_helper_fini); | |
785b93ef | 745 | |
47074ab7 AT |
746 | /** |
747 | * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer | |
748 | * @fb_helper: driver-allocated fbdev helper | |
749 | * | |
750 | * A wrapper around unlink_framebuffer implemented by fbdev core | |
751 | */ | |
752 | void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) | |
753 | { | |
754 | if (fb_helper && fb_helper->fbdev) | |
755 | unlink_framebuffer(fb_helper->fbdev); | |
756 | } | |
757 | EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); | |
758 | ||
cbb1a82e AT |
759 | /** |
760 | * drm_fb_helper_sys_read - wrapper around fb_sys_read | |
761 | * @info: fb_info struct pointer | |
762 | * @buf: userspace buffer to read from framebuffer memory | |
763 | * @count: number of bytes to read from framebuffer memory | |
764 | * @ppos: read offset within framebuffer memory | |
765 | * | |
766 | * A wrapper around fb_sys_read implemented by fbdev core | |
767 | */ | |
768 | ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, | |
769 | size_t count, loff_t *ppos) | |
770 | { | |
771 | return fb_sys_read(info, buf, count, ppos); | |
772 | } | |
773 | EXPORT_SYMBOL(drm_fb_helper_sys_read); | |
774 | ||
775 | /** | |
776 | * drm_fb_helper_sys_write - wrapper around fb_sys_write | |
777 | * @info: fb_info struct pointer | |
778 | * @buf: userspace buffer to write to framebuffer memory | |
779 | * @count: number of bytes to write to framebuffer memory | |
780 | * @ppos: write offset within framebuffer memory | |
781 | * | |
782 | * A wrapper around fb_sys_write implemented by fbdev core | |
783 | */ | |
784 | ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, | |
785 | size_t count, loff_t *ppos) | |
786 | { | |
787 | return fb_sys_write(info, buf, count, ppos); | |
788 | } | |
789 | EXPORT_SYMBOL(drm_fb_helper_sys_write); | |
790 | ||
742547b7 AT |
791 | /** |
792 | * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect | |
793 | * @info: fbdev registered by the helper | |
794 | * @rect: info about rectangle to fill | |
795 | * | |
796 | * A wrapper around sys_fillrect implemented by fbdev core | |
797 | */ | |
798 | void drm_fb_helper_sys_fillrect(struct fb_info *info, | |
799 | const struct fb_fillrect *rect) | |
800 | { | |
801 | sys_fillrect(info, rect); | |
802 | } | |
803 | EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); | |
804 | ||
805 | /** | |
806 | * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea | |
807 | * @info: fbdev registered by the helper | |
808 | * @area: info about area to copy | |
809 | * | |
810 | * A wrapper around sys_copyarea implemented by fbdev core | |
811 | */ | |
812 | void drm_fb_helper_sys_copyarea(struct fb_info *info, | |
813 | const struct fb_copyarea *area) | |
814 | { | |
815 | sys_copyarea(info, area); | |
816 | } | |
817 | EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); | |
818 | ||
819 | /** | |
820 | * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit | |
821 | * @info: fbdev registered by the helper | |
822 | * @image: info about image to blit | |
823 | * | |
824 | * A wrapper around sys_imageblit implemented by fbdev core | |
825 | */ | |
826 | void drm_fb_helper_sys_imageblit(struct fb_info *info, | |
827 | const struct fb_image *image) | |
828 | { | |
829 | sys_imageblit(info, image); | |
830 | } | |
831 | EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); | |
832 | ||
833 | /** | |
834 | * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect | |
835 | * @info: fbdev registered by the helper | |
836 | * @rect: info about rectangle to fill | |
837 | * | |
838 | * A wrapper around cfb_imageblit implemented by fbdev core | |
839 | */ | |
840 | void drm_fb_helper_cfb_fillrect(struct fb_info *info, | |
841 | const struct fb_fillrect *rect) | |
842 | { | |
843 | cfb_fillrect(info, rect); | |
844 | } | |
845 | EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); | |
846 | ||
847 | /** | |
848 | * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea | |
849 | * @info: fbdev registered by the helper | |
850 | * @area: info about area to copy | |
851 | * | |
852 | * A wrapper around cfb_copyarea implemented by fbdev core | |
853 | */ | |
854 | void drm_fb_helper_cfb_copyarea(struct fb_info *info, | |
855 | const struct fb_copyarea *area) | |
856 | { | |
857 | cfb_copyarea(info, area); | |
858 | } | |
859 | EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); | |
860 | ||
861 | /** | |
862 | * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit | |
863 | * @info: fbdev registered by the helper | |
864 | * @image: info about image to blit | |
865 | * | |
866 | * A wrapper around cfb_imageblit implemented by fbdev core | |
867 | */ | |
868 | void drm_fb_helper_cfb_imageblit(struct fb_info *info, | |
869 | const struct fb_image *image) | |
870 | { | |
871 | cfb_imageblit(info, image); | |
872 | } | |
873 | EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); | |
874 | ||
fdefa58a AT |
875 | /** |
876 | * drm_fb_helper_set_suspend - wrapper around fb_set_suspend | |
877 | * @fb_helper: driver-allocated fbdev helper | |
878 | * @state: desired state, zero to resume, non-zero to suspend | |
879 | * | |
880 | * A wrapper around fb_set_suspend implemented by fbdev core | |
881 | */ | |
882 | void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state) | |
883 | { | |
884 | if (fb_helper && fb_helper->fbdev) | |
885 | fb_set_suspend(fb_helper->fbdev, state); | |
886 | } | |
887 | EXPORT_SYMBOL(drm_fb_helper_set_suspend); | |
888 | ||
c850cb78 | 889 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
b8c00ac5 DA |
890 | u16 blue, u16 regno, struct fb_info *info) |
891 | { | |
892 | struct drm_fb_helper *fb_helper = info->par; | |
893 | struct drm_framebuffer *fb = fb_helper->fb; | |
894 | int pindex; | |
895 | ||
c850cb78 DA |
896 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
897 | u32 *palette; | |
898 | u32 value; | |
899 | /* place color in psuedopalette */ | |
900 | if (regno > 16) | |
901 | return -EINVAL; | |
902 | palette = (u32 *)info->pseudo_palette; | |
903 | red >>= (16 - info->var.red.length); | |
904 | green >>= (16 - info->var.green.length); | |
905 | blue >>= (16 - info->var.blue.length); | |
906 | value = (red << info->var.red.offset) | | |
907 | (green << info->var.green.offset) | | |
908 | (blue << info->var.blue.offset); | |
9da12b6a RC |
909 | if (info->var.transp.length > 0) { |
910 | u32 mask = (1 << info->var.transp.length) - 1; | |
911 | mask <<= info->var.transp.offset; | |
912 | value |= mask; | |
913 | } | |
c850cb78 DA |
914 | palette[regno] = value; |
915 | return 0; | |
916 | } | |
917 | ||
04c0c569 VS |
918 | /* |
919 | * The driver really shouldn't advertise pseudo/directcolor | |
920 | * visuals if it can't deal with the palette. | |
921 | */ | |
922 | if (WARN_ON(!fb_helper->funcs->gamma_set || | |
923 | !fb_helper->funcs->gamma_get)) | |
924 | return -EINVAL; | |
925 | ||
b8c00ac5 DA |
926 | pindex = regno; |
927 | ||
928 | if (fb->bits_per_pixel == 16) { | |
929 | pindex = regno << 3; | |
930 | ||
931 | if (fb->depth == 16 && regno > 63) | |
c850cb78 | 932 | return -EINVAL; |
b8c00ac5 | 933 | if (fb->depth == 15 && regno > 31) |
c850cb78 | 934 | return -EINVAL; |
b8c00ac5 DA |
935 | |
936 | if (fb->depth == 16) { | |
937 | u16 r, g, b; | |
938 | int i; | |
939 | if (regno < 32) { | |
940 | for (i = 0; i < 8; i++) | |
941 | fb_helper->funcs->gamma_set(crtc, red, | |
942 | green, blue, pindex + i); | |
943 | } | |
944 | ||
945 | fb_helper->funcs->gamma_get(crtc, &r, | |
946 | &g, &b, | |
947 | pindex >> 1); | |
948 | ||
949 | for (i = 0; i < 4; i++) | |
950 | fb_helper->funcs->gamma_set(crtc, r, | |
951 | green, b, | |
952 | (pindex >> 1) + i); | |
953 | } | |
954 | } | |
955 | ||
956 | if (fb->depth != 16) | |
957 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); | |
c850cb78 | 958 | return 0; |
b8c00ac5 DA |
959 | } |
960 | ||
207fd329 DV |
961 | /** |
962 | * drm_fb_helper_setcmap - implementation for ->fb_setcmap | |
963 | * @cmap: cmap to set | |
964 | * @info: fbdev registered by the helper | |
965 | */ | |
068143d3 DA |
966 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
967 | { | |
968 | struct drm_fb_helper *fb_helper = info->par; | |
8391a3d5 | 969 | struct drm_device *dev = fb_helper->dev; |
be26a66d | 970 | const struct drm_crtc_helper_funcs *crtc_funcs; |
068143d3 DA |
971 | u16 *red, *green, *blue, *transp; |
972 | struct drm_crtc *crtc; | |
062ac622 | 973 | int i, j, rc = 0; |
068143d3 DA |
974 | int start; |
975 | ||
c50bfd08 | 976 | if (oops_in_progress) |
9aa609e1 | 977 | return -EBUSY; |
c50bfd08 DV |
978 | |
979 | drm_modeset_lock_all(dev); | |
8391a3d5 VS |
980 | if (!drm_fb_helper_is_bound(fb_helper)) { |
981 | drm_modeset_unlock_all(dev); | |
982 | return -EBUSY; | |
983 | } | |
984 | ||
8be48d92 DA |
985 | for (i = 0; i < fb_helper->crtc_count; i++) { |
986 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
987 | crtc_funcs = crtc->helper_private; | |
068143d3 DA |
988 | |
989 | red = cmap->red; | |
990 | green = cmap->green; | |
991 | blue = cmap->blue; | |
992 | transp = cmap->transp; | |
993 | start = cmap->start; | |
994 | ||
062ac622 | 995 | for (j = 0; j < cmap->len; j++) { |
068143d3 DA |
996 | u16 hred, hgreen, hblue, htransp = 0xffff; |
997 | ||
998 | hred = *red++; | |
999 | hgreen = *green++; | |
1000 | hblue = *blue++; | |
1001 | ||
1002 | if (transp) | |
1003 | htransp = *transp++; | |
1004 | ||
c850cb78 DA |
1005 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
1006 | if (rc) | |
8391a3d5 | 1007 | goto out; |
068143d3 | 1008 | } |
04c0c569 VS |
1009 | if (crtc_funcs->load_lut) |
1010 | crtc_funcs->load_lut(crtc); | |
068143d3 | 1011 | } |
8391a3d5 VS |
1012 | out: |
1013 | drm_modeset_unlock_all(dev); | |
068143d3 DA |
1014 | return rc; |
1015 | } | |
1016 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
1017 | ||
207fd329 DV |
1018 | /** |
1019 | * drm_fb_helper_check_var - implementation for ->fb_check_var | |
1020 | * @var: screeninfo to check | |
1021 | * @info: fbdev registered by the helper | |
1022 | */ | |
785b93ef DA |
1023 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
1024 | struct fb_info *info) | |
1025 | { | |
1026 | struct drm_fb_helper *fb_helper = info->par; | |
1027 | struct drm_framebuffer *fb = fb_helper->fb; | |
1028 | int depth; | |
1029 | ||
f90ebd9e | 1030 | if (var->pixclock != 0 || in_dbg_master()) |
785b93ef DA |
1031 | return -EINVAL; |
1032 | ||
1033 | /* Need to resize the fb object !!! */ | |
62fb376e CW |
1034 | if (var->bits_per_pixel > fb->bits_per_pixel || |
1035 | var->xres > fb->width || var->yres > fb->height || | |
1036 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { | |
509c7d83 | 1037 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " |
62fb376e CW |
1038 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
1039 | var->xres, var->yres, var->bits_per_pixel, | |
1040 | var->xres_virtual, var->yres_virtual, | |
509c7d83 | 1041 | fb->width, fb->height, fb->bits_per_pixel); |
785b93ef DA |
1042 | return -EINVAL; |
1043 | } | |
1044 | ||
1045 | switch (var->bits_per_pixel) { | |
1046 | case 16: | |
1047 | depth = (var->green.length == 6) ? 16 : 15; | |
1048 | break; | |
1049 | case 32: | |
1050 | depth = (var->transp.length > 0) ? 32 : 24; | |
1051 | break; | |
1052 | default: | |
1053 | depth = var->bits_per_pixel; | |
1054 | break; | |
1055 | } | |
1056 | ||
1057 | switch (depth) { | |
1058 | case 8: | |
1059 | var->red.offset = 0; | |
1060 | var->green.offset = 0; | |
1061 | var->blue.offset = 0; | |
1062 | var->red.length = 8; | |
1063 | var->green.length = 8; | |
1064 | var->blue.length = 8; | |
1065 | var->transp.length = 0; | |
1066 | var->transp.offset = 0; | |
1067 | break; | |
1068 | case 15: | |
1069 | var->red.offset = 10; | |
1070 | var->green.offset = 5; | |
1071 | var->blue.offset = 0; | |
1072 | var->red.length = 5; | |
1073 | var->green.length = 5; | |
1074 | var->blue.length = 5; | |
1075 | var->transp.length = 1; | |
1076 | var->transp.offset = 15; | |
1077 | break; | |
1078 | case 16: | |
1079 | var->red.offset = 11; | |
1080 | var->green.offset = 5; | |
1081 | var->blue.offset = 0; | |
1082 | var->red.length = 5; | |
1083 | var->green.length = 6; | |
1084 | var->blue.length = 5; | |
1085 | var->transp.length = 0; | |
1086 | var->transp.offset = 0; | |
1087 | break; | |
1088 | case 24: | |
1089 | var->red.offset = 16; | |
1090 | var->green.offset = 8; | |
1091 | var->blue.offset = 0; | |
1092 | var->red.length = 8; | |
1093 | var->green.length = 8; | |
1094 | var->blue.length = 8; | |
1095 | var->transp.length = 0; | |
1096 | var->transp.offset = 0; | |
1097 | break; | |
1098 | case 32: | |
1099 | var->red.offset = 16; | |
1100 | var->green.offset = 8; | |
1101 | var->blue.offset = 0; | |
1102 | var->red.length = 8; | |
1103 | var->green.length = 8; | |
1104 | var->blue.length = 8; | |
1105 | var->transp.length = 8; | |
1106 | var->transp.offset = 24; | |
1107 | break; | |
1108 | default: | |
1109 | return -EINVAL; | |
1110 | } | |
1111 | return 0; | |
1112 | } | |
1113 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
1114 | ||
207fd329 DV |
1115 | /** |
1116 | * drm_fb_helper_set_par - implementation for ->fb_set_par | |
1117 | * @info: fbdev registered by the helper | |
1118 | * | |
1119 | * This will let fbcon do the mode init and is called at initialization time by | |
1120 | * the fbdev core when registering the driver, and later on through the hotplug | |
1121 | * callback. | |
1122 | */ | |
785b93ef DA |
1123 | int drm_fb_helper_set_par(struct fb_info *info) |
1124 | { | |
1125 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 1126 | struct fb_var_screeninfo *var = &info->var; |
785b93ef | 1127 | |
c50bfd08 DV |
1128 | if (oops_in_progress) |
1129 | return -EBUSY; | |
1130 | ||
5349ef31 | 1131 | if (var->pixclock != 0) { |
172e91f5 | 1132 | DRM_ERROR("PIXEL CLOCK SET\n"); |
785b93ef DA |
1133 | return -EINVAL; |
1134 | } | |
1135 | ||
5ea1f752 | 1136 | drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); |
4abe3520 | 1137 | |
785b93ef DA |
1138 | return 0; |
1139 | } | |
1140 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
1141 | ||
207fd329 DV |
1142 | /** |
1143 | * drm_fb_helper_pan_display - implementation for ->fb_pan_display | |
1144 | * @var: updated screen information | |
1145 | * @info: fbdev registered by the helper | |
1146 | */ | |
785b93ef DA |
1147 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
1148 | struct fb_info *info) | |
1149 | { | |
1150 | struct drm_fb_helper *fb_helper = info->par; | |
1151 | struct drm_device *dev = fb_helper->dev; | |
1152 | struct drm_mode_set *modeset; | |
785b93ef DA |
1153 | int ret = 0; |
1154 | int i; | |
1155 | ||
c50bfd08 | 1156 | if (oops_in_progress) |
9aa609e1 | 1157 | return -EBUSY; |
c50bfd08 DV |
1158 | |
1159 | drm_modeset_lock_all(dev); | |
20c60c35 DV |
1160 | if (!drm_fb_helper_is_bound(fb_helper)) { |
1161 | drm_modeset_unlock_all(dev); | |
1162 | return -EBUSY; | |
1163 | } | |
1164 | ||
8be48d92 | 1165 | for (i = 0; i < fb_helper->crtc_count; i++) { |
785b93ef DA |
1166 | modeset = &fb_helper->crtc_info[i].mode_set; |
1167 | ||
1168 | modeset->x = var->xoffset; | |
1169 | modeset->y = var->yoffset; | |
1170 | ||
1171 | if (modeset->num_connectors) { | |
2d13b679 | 1172 | ret = drm_mode_set_config_internal(modeset); |
785b93ef DA |
1173 | if (!ret) { |
1174 | info->var.xoffset = var->xoffset; | |
1175 | info->var.yoffset = var->yoffset; | |
1176 | } | |
1177 | } | |
1178 | } | |
84849903 | 1179 | drm_modeset_unlock_all(dev); |
785b93ef DA |
1180 | return ret; |
1181 | } | |
1182 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
1183 | ||
8acf658a | 1184 | /* |
207fd329 DV |
1185 | * Allocates the backing storage and sets up the fbdev info structure through |
1186 | * the ->fb_probe callback and then registers the fbdev and sets up the panic | |
1187 | * notifier. | |
8acf658a | 1188 | */ |
de1ace5b DV |
1189 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
1190 | int preferred_bpp) | |
785b93ef | 1191 | { |
8acf658a | 1192 | int ret = 0; |
785b93ef | 1193 | int crtc_count = 0; |
4abe3520 | 1194 | int i; |
785b93ef | 1195 | struct fb_info *info; |
38651674 | 1196 | struct drm_fb_helper_surface_size sizes; |
8be48d92 | 1197 | int gamma_size = 0; |
38651674 DA |
1198 | |
1199 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
1200 | sizes.surface_depth = 24; | |
1201 | sizes.surface_bpp = 32; | |
1202 | sizes.fb_width = (unsigned)-1; | |
1203 | sizes.fb_height = (unsigned)-1; | |
785b93ef | 1204 | |
b8c00ac5 DA |
1205 | /* if driver picks 8 or 16 by default use that |
1206 | for both depth/bpp */ | |
96081cdf | 1207 | if (preferred_bpp != sizes.surface_bpp) |
38651674 | 1208 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
96081cdf | 1209 | |
785b93ef | 1210 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
0b4c0f3f DA |
1211 | for (i = 0; i < fb_helper->connector_count; i++) { |
1212 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; | |
1794d257 | 1213 | struct drm_cmdline_mode *cmdline_mode; |
8ef8678c | 1214 | |
eaf99c74 | 1215 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
d50ba256 DA |
1216 | |
1217 | if (cmdline_mode->bpp_specified) { | |
1218 | switch (cmdline_mode->bpp) { | |
1219 | case 8: | |
38651674 | 1220 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
1221 | break; |
1222 | case 15: | |
38651674 DA |
1223 | sizes.surface_depth = 15; |
1224 | sizes.surface_bpp = 16; | |
d50ba256 DA |
1225 | break; |
1226 | case 16: | |
38651674 | 1227 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
1228 | break; |
1229 | case 24: | |
38651674 | 1230 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
1231 | break; |
1232 | case 32: | |
38651674 DA |
1233 | sizes.surface_depth = 24; |
1234 | sizes.surface_bpp = 32; | |
d50ba256 DA |
1235 | break; |
1236 | } | |
1237 | break; | |
1238 | } | |
1239 | } | |
1240 | ||
8be48d92 DA |
1241 | crtc_count = 0; |
1242 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1243 | struct drm_display_mode *desired_mode; | |
0e3704c9 RC |
1244 | struct drm_mode_set *mode_set; |
1245 | int x, y, j; | |
1246 | /* in case of tile group, are we the last tile vert or horiz? | |
1247 | * If no tile group you are always the last one both vertically | |
1248 | * and horizontally | |
1249 | */ | |
1250 | bool lastv = true, lasth = true; | |
675c8328 | 1251 | |
8be48d92 | 1252 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
0e3704c9 | 1253 | mode_set = &fb_helper->crtc_info[i].mode_set; |
675c8328 RC |
1254 | |
1255 | if (!desired_mode) | |
1256 | continue; | |
1257 | ||
1258 | crtc_count++; | |
1259 | ||
b0ee9e7f DA |
1260 | x = fb_helper->crtc_info[i].x; |
1261 | y = fb_helper->crtc_info[i].y; | |
675c8328 RC |
1262 | |
1263 | if (gamma_size == 0) | |
1264 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; | |
1265 | ||
1266 | sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); | |
1267 | sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); | |
0e3704c9 RC |
1268 | |
1269 | for (j = 0; j < mode_set->num_connectors; j++) { | |
1270 | struct drm_connector *connector = mode_set->connectors[j]; | |
1271 | if (connector->has_tile) { | |
1272 | lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); | |
1273 | lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); | |
1274 | /* cloning to multiple tiles is just crazy-talk, so: */ | |
1275 | break; | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | if (lasth) | |
1280 | sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); | |
1281 | if (lastv) | |
1282 | sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); | |
785b93ef DA |
1283 | } |
1284 | ||
38651674 | 1285 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
785b93ef DA |
1286 | /* hmm everyone went away - assume VGA cable just fell out |
1287 | and will come back later. */ | |
eb1f8e4f | 1288 | DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); |
19b4b445 DA |
1289 | sizes.fb_width = sizes.surface_width = 1024; |
1290 | sizes.fb_height = sizes.surface_height = 768; | |
785b93ef DA |
1291 | } |
1292 | ||
38651674 | 1293 | /* push down into drivers */ |
8acf658a DV |
1294 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
1295 | if (ret < 0) | |
1296 | return ret; | |
785b93ef | 1297 | |
38651674 | 1298 | info = fb_helper->fbdev; |
785b93ef | 1299 | |
7e53f3a4 DV |
1300 | /* |
1301 | * Set the fb pointer - usually drm_setup_crtcs does this for hotplug | |
1302 | * events, but at init time drm_setup_crtcs needs to be called before | |
1303 | * the fb is allocated (since we need to figure out the desired size of | |
1304 | * the fb before we can allocate it ...). Hence we need to fix things up | |
1305 | * here again. | |
1306 | */ | |
96081cdf | 1307 | for (i = 0; i < fb_helper->crtc_count; i++) |
7e53f3a4 DV |
1308 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
1309 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; | |
1310 | ||
785b93ef | 1311 | |
8acf658a DV |
1312 | info->var.pixclock = 0; |
1313 | if (register_framebuffer(info) < 0) | |
1314 | return -EINVAL; | |
38651674 | 1315 | |
8acf658a DV |
1316 | dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", |
1317 | info->node, info->fix.id); | |
785b93ef | 1318 | |
785b93ef | 1319 | if (list_empty(&kernel_fb_helper_list)) { |
785b93ef DA |
1320 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); |
1321 | } | |
8acf658a DV |
1322 | |
1323 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
38651674 | 1324 | |
785b93ef DA |
1325 | return 0; |
1326 | } | |
785b93ef | 1327 | |
207fd329 DV |
1328 | /** |
1329 | * drm_fb_helper_fill_fix - initializes fixed fbdev information | |
1330 | * @info: fbdev registered by the helper | |
1331 | * @pitch: desired pitch | |
1332 | * @depth: desired depth | |
1333 | * | |
1334 | * Helper to fill in the fixed fbdev information useful for a non-accelerated | |
1335 | * fbdev emulations. Drivers which support acceleration methods which impose | |
1336 | * additional constraints need to set up their own limits. | |
1337 | * | |
1338 | * Drivers should call this (or their equivalent setup code) from their | |
1339 | * ->fb_probe callback. | |
1340 | */ | |
3632ef89 DA |
1341 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
1342 | uint32_t depth) | |
1343 | { | |
1344 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
1345 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : | |
1346 | FB_VISUAL_TRUECOLOR; | |
1347 | info->fix.mmio_start = 0; | |
1348 | info->fix.mmio_len = 0; | |
1349 | info->fix.type_aux = 0; | |
1350 | info->fix.xpanstep = 1; /* doing it in hw */ | |
1351 | info->fix.ypanstep = 1; /* doing it in hw */ | |
1352 | info->fix.ywrapstep = 0; | |
1353 | info->fix.accel = FB_ACCEL_NONE; | |
3632ef89 DA |
1354 | |
1355 | info->fix.line_length = pitch; | |
1356 | return; | |
1357 | } | |
1358 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); | |
1359 | ||
207fd329 DV |
1360 | /** |
1361 | * drm_fb_helper_fill_var - initalizes variable fbdev information | |
1362 | * @info: fbdev instance to set up | |
1363 | * @fb_helper: fb helper instance to use as template | |
1364 | * @fb_width: desired fb width | |
1365 | * @fb_height: desired fb height | |
1366 | * | |
1367 | * Sets up the variable fbdev metainformation from the given fb helper instance | |
1368 | * and the drm framebuffer allocated in fb_helper->fb. | |
1369 | * | |
1370 | * Drivers should call this (or their equivalent setup code) from their | |
1371 | * ->fb_probe callback after having allocated the fbdev backing | |
1372 | * storage framebuffer. | |
1373 | */ | |
38651674 | 1374 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
785b93ef DA |
1375 | uint32_t fb_width, uint32_t fb_height) |
1376 | { | |
38651674 DA |
1377 | struct drm_framebuffer *fb = fb_helper->fb; |
1378 | info->pseudo_palette = fb_helper->pseudo_palette; | |
785b93ef DA |
1379 | info->var.xres_virtual = fb->width; |
1380 | info->var.yres_virtual = fb->height; | |
1381 | info->var.bits_per_pixel = fb->bits_per_pixel; | |
57084d05 | 1382 | info->var.accel_flags = FB_ACCELF_TEXT; |
785b93ef DA |
1383 | info->var.xoffset = 0; |
1384 | info->var.yoffset = 0; | |
1385 | info->var.activate = FB_ACTIVATE_NOW; | |
1386 | info->var.height = -1; | |
1387 | info->var.width = -1; | |
1388 | ||
1389 | switch (fb->depth) { | |
1390 | case 8: | |
1391 | info->var.red.offset = 0; | |
1392 | info->var.green.offset = 0; | |
1393 | info->var.blue.offset = 0; | |
1394 | info->var.red.length = 8; /* 8bit DAC */ | |
1395 | info->var.green.length = 8; | |
1396 | info->var.blue.length = 8; | |
1397 | info->var.transp.offset = 0; | |
1398 | info->var.transp.length = 0; | |
1399 | break; | |
1400 | case 15: | |
1401 | info->var.red.offset = 10; | |
1402 | info->var.green.offset = 5; | |
1403 | info->var.blue.offset = 0; | |
1404 | info->var.red.length = 5; | |
1405 | info->var.green.length = 5; | |
1406 | info->var.blue.length = 5; | |
1407 | info->var.transp.offset = 15; | |
1408 | info->var.transp.length = 1; | |
1409 | break; | |
1410 | case 16: | |
1411 | info->var.red.offset = 11; | |
1412 | info->var.green.offset = 5; | |
1413 | info->var.blue.offset = 0; | |
1414 | info->var.red.length = 5; | |
1415 | info->var.green.length = 6; | |
1416 | info->var.blue.length = 5; | |
1417 | info->var.transp.offset = 0; | |
1418 | break; | |
1419 | case 24: | |
1420 | info->var.red.offset = 16; | |
1421 | info->var.green.offset = 8; | |
1422 | info->var.blue.offset = 0; | |
1423 | info->var.red.length = 8; | |
1424 | info->var.green.length = 8; | |
1425 | info->var.blue.length = 8; | |
1426 | info->var.transp.offset = 0; | |
1427 | info->var.transp.length = 0; | |
1428 | break; | |
1429 | case 32: | |
1430 | info->var.red.offset = 16; | |
1431 | info->var.green.offset = 8; | |
1432 | info->var.blue.offset = 0; | |
1433 | info->var.red.length = 8; | |
1434 | info->var.green.length = 8; | |
1435 | info->var.blue.length = 8; | |
1436 | info->var.transp.offset = 24; | |
1437 | info->var.transp.length = 8; | |
1438 | break; | |
1439 | default: | |
1440 | break; | |
1441 | } | |
1442 | ||
1443 | info->var.xres = fb_width; | |
1444 | info->var.yres = fb_height; | |
1445 | } | |
1446 | EXPORT_SYMBOL(drm_fb_helper_fill_var); | |
38651674 | 1447 | |
0b4c0f3f DA |
1448 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
1449 | uint32_t maxX, | |
1450 | uint32_t maxY) | |
38651674 DA |
1451 | { |
1452 | struct drm_connector *connector; | |
1453 | int count = 0; | |
0b4c0f3f | 1454 | int i; |
38651674 | 1455 | |
0b4c0f3f DA |
1456 | for (i = 0; i < fb_helper->connector_count; i++) { |
1457 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1458 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
1459 | } | |
1460 | ||
1461 | return count; | |
1462 | } | |
1463 | ||
2f1046f3 | 1464 | struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
38651674 DA |
1465 | { |
1466 | struct drm_display_mode *mode; | |
1467 | ||
0b4c0f3f | 1468 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
9d3de138 DV |
1469 | if (mode->hdisplay > width || |
1470 | mode->vdisplay > height) | |
38651674 DA |
1471 | continue; |
1472 | if (mode->type & DRM_MODE_TYPE_PREFERRED) | |
1473 | return mode; | |
1474 | } | |
1475 | return NULL; | |
1476 | } | |
2f1046f3 | 1477 | EXPORT_SYMBOL(drm_has_preferred_mode); |
38651674 | 1478 | |
0b4c0f3f | 1479 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
38651674 | 1480 | { |
eaf99c74 | 1481 | return fb_connector->connector->cmdline_mode.specified; |
38651674 DA |
1482 | } |
1483 | ||
2f1046f3 | 1484 | struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
0b4c0f3f | 1485 | int width, int height) |
38651674 | 1486 | { |
1794d257 | 1487 | struct drm_cmdline_mode *cmdline_mode; |
f3af5c7d | 1488 | struct drm_display_mode *mode; |
c683f427 | 1489 | bool prefer_non_interlace; |
38651674 | 1490 | |
eaf99c74 | 1491 | cmdline_mode = &fb_helper_conn->connector->cmdline_mode; |
38651674 | 1492 | if (cmdline_mode->specified == false) |
f3af5c7d | 1493 | return NULL; |
38651674 DA |
1494 | |
1495 | /* attempt to find a matching mode in the list of modes | |
1496 | * we have gotten so far, if not add a CVT mode that conforms | |
1497 | */ | |
1498 | if (cmdline_mode->rb || cmdline_mode->margins) | |
1499 | goto create_mode; | |
1500 | ||
c683f427 | 1501 | prefer_non_interlace = !cmdline_mode->interlace; |
f3af5c7d | 1502 | again: |
0b4c0f3f | 1503 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
38651674 DA |
1504 | /* check width/height */ |
1505 | if (mode->hdisplay != cmdline_mode->xres || | |
1506 | mode->vdisplay != cmdline_mode->yres) | |
1507 | continue; | |
1508 | ||
1509 | if (cmdline_mode->refresh_specified) { | |
1510 | if (mode->vrefresh != cmdline_mode->refresh) | |
1511 | continue; | |
1512 | } | |
1513 | ||
1514 | if (cmdline_mode->interlace) { | |
1515 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) | |
1516 | continue; | |
c683f427 TI |
1517 | } else if (prefer_non_interlace) { |
1518 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) | |
1519 | continue; | |
38651674 DA |
1520 | } |
1521 | return mode; | |
1522 | } | |
1523 | ||
c683f427 TI |
1524 | if (prefer_non_interlace) { |
1525 | prefer_non_interlace = false; | |
1526 | goto again; | |
1527 | } | |
1528 | ||
38651674 | 1529 | create_mode: |
1794d257 CW |
1530 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
1531 | cmdline_mode); | |
0b4c0f3f | 1532 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
38651674 DA |
1533 | return mode; |
1534 | } | |
2f1046f3 | 1535 | EXPORT_SYMBOL(drm_pick_cmdline_mode); |
38651674 DA |
1536 | |
1537 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) | |
1538 | { | |
1539 | bool enable; | |
1540 | ||
96081cdf | 1541 | if (strict) |
38651674 | 1542 | enable = connector->status == connector_status_connected; |
96081cdf | 1543 | else |
38651674 | 1544 | enable = connector->status != connector_status_disconnected; |
96081cdf | 1545 | |
38651674 DA |
1546 | return enable; |
1547 | } | |
1548 | ||
0b4c0f3f DA |
1549 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
1550 | bool *enabled) | |
38651674 DA |
1551 | { |
1552 | bool any_enabled = false; | |
1553 | struct drm_connector *connector; | |
1554 | int i = 0; | |
1555 | ||
0b4c0f3f DA |
1556 | for (i = 0; i < fb_helper->connector_count; i++) { |
1557 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1558 | enabled[i] = drm_connector_enabled(connector, true); |
1559 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, | |
1560 | enabled[i] ? "yes" : "no"); | |
1561 | any_enabled |= enabled[i]; | |
38651674 DA |
1562 | } |
1563 | ||
1564 | if (any_enabled) | |
1565 | return; | |
1566 | ||
0b4c0f3f DA |
1567 | for (i = 0; i < fb_helper->connector_count; i++) { |
1568 | connector = fb_helper->connector_info[i]->connector; | |
38651674 | 1569 | enabled[i] = drm_connector_enabled(connector, false); |
38651674 DA |
1570 | } |
1571 | } | |
1572 | ||
1d42bbc8 DA |
1573 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
1574 | struct drm_display_mode **modes, | |
b0ee9e7f | 1575 | struct drm_fb_offset *offsets, |
1d42bbc8 DA |
1576 | bool *enabled, int width, int height) |
1577 | { | |
1578 | int count, i, j; | |
1579 | bool can_clone = false; | |
1580 | struct drm_fb_helper_connector *fb_helper_conn; | |
1581 | struct drm_display_mode *dmt_mode, *mode; | |
1582 | ||
1583 | /* only contemplate cloning in the single crtc case */ | |
1584 | if (fb_helper->crtc_count > 1) | |
1585 | return false; | |
1586 | ||
1587 | count = 0; | |
1588 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1589 | if (enabled[i]) | |
1590 | count++; | |
1591 | } | |
1592 | ||
1593 | /* only contemplate cloning if more than one connector is enabled */ | |
1594 | if (count <= 1) | |
1595 | return false; | |
1596 | ||
1597 | /* check the command line or if nothing common pick 1024x768 */ | |
1598 | can_clone = true; | |
1599 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1600 | if (!enabled[i]) | |
1601 | continue; | |
1602 | fb_helper_conn = fb_helper->connector_info[i]; | |
1603 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); | |
1604 | if (!modes[i]) { | |
1605 | can_clone = false; | |
1606 | break; | |
1607 | } | |
1608 | for (j = 0; j < i; j++) { | |
1609 | if (!enabled[j]) | |
1610 | continue; | |
1611 | if (!drm_mode_equal(modes[j], modes[i])) | |
1612 | can_clone = false; | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | if (can_clone) { | |
1617 | DRM_DEBUG_KMS("can clone using command line\n"); | |
1618 | return true; | |
1619 | } | |
1620 | ||
1621 | /* try and find a 1024x768 mode on each connector */ | |
1622 | can_clone = true; | |
f6e252ba | 1623 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
1d42bbc8 DA |
1624 | |
1625 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1626 | ||
1627 | if (!enabled[i]) | |
1628 | continue; | |
1629 | ||
1630 | fb_helper_conn = fb_helper->connector_info[i]; | |
1631 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { | |
1632 | if (drm_mode_equal(mode, dmt_mode)) | |
1633 | modes[i] = mode; | |
1634 | } | |
1635 | if (!modes[i]) | |
1636 | can_clone = false; | |
1637 | } | |
1638 | ||
1639 | if (can_clone) { | |
1640 | DRM_DEBUG_KMS("can clone using 1024x768\n"); | |
1641 | return true; | |
1642 | } | |
1643 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); | |
1644 | return false; | |
1645 | } | |
1646 | ||
b0ee9e7f DA |
1647 | static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, |
1648 | struct drm_display_mode **modes, | |
1649 | struct drm_fb_offset *offsets, | |
1650 | int idx, | |
1651 | int h_idx, int v_idx) | |
1652 | { | |
1653 | struct drm_fb_helper_connector *fb_helper_conn; | |
1654 | int i; | |
1655 | int hoffset = 0, voffset = 0; | |
1656 | ||
1657 | for (i = 0; i < fb_helper->connector_count; i++) { | |
1658 | fb_helper_conn = fb_helper->connector_info[i]; | |
1659 | if (!fb_helper_conn->connector->has_tile) | |
1660 | continue; | |
1661 | ||
1662 | if (!modes[i] && (h_idx || v_idx)) { | |
1663 | DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, | |
1664 | fb_helper_conn->connector->base.id); | |
1665 | continue; | |
1666 | } | |
1667 | if (fb_helper_conn->connector->tile_h_loc < h_idx) | |
1668 | hoffset += modes[i]->hdisplay; | |
1669 | ||
1670 | if (fb_helper_conn->connector->tile_v_loc < v_idx) | |
1671 | voffset += modes[i]->vdisplay; | |
1672 | } | |
1673 | offsets[idx].x = hoffset; | |
1674 | offsets[idx].y = voffset; | |
1675 | DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); | |
1676 | return 0; | |
1677 | } | |
1678 | ||
0b4c0f3f | 1679 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
38651674 | 1680 | struct drm_display_mode **modes, |
b0ee9e7f | 1681 | struct drm_fb_offset *offsets, |
38651674 DA |
1682 | bool *enabled, int width, int height) |
1683 | { | |
0b4c0f3f DA |
1684 | struct drm_fb_helper_connector *fb_helper_conn; |
1685 | int i; | |
b0ee9e7f DA |
1686 | uint64_t conn_configured = 0, mask; |
1687 | int tile_pass = 0; | |
1688 | mask = (1 << fb_helper->connector_count) - 1; | |
1689 | retry: | |
0b4c0f3f DA |
1690 | for (i = 0; i < fb_helper->connector_count; i++) { |
1691 | fb_helper_conn = fb_helper->connector_info[i]; | |
38651674 | 1692 | |
b0ee9e7f DA |
1693 | if (conn_configured & (1 << i)) |
1694 | continue; | |
1695 | ||
1696 | if (enabled[i] == false) { | |
1697 | conn_configured |= (1 << i); | |
1698 | continue; | |
1699 | } | |
1700 | ||
1701 | /* first pass over all the untiled connectors */ | |
1702 | if (tile_pass == 0 && fb_helper_conn->connector->has_tile) | |
38651674 | 1703 | continue; |
38651674 | 1704 | |
b0ee9e7f DA |
1705 | if (tile_pass == 1) { |
1706 | if (fb_helper_conn->connector->tile_h_loc != 0 || | |
1707 | fb_helper_conn->connector->tile_v_loc != 0) | |
1708 | continue; | |
1709 | ||
1710 | } else { | |
1711 | if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && | |
1712 | fb_helper_conn->connector->tile_v_loc != tile_pass - 1) | |
1713 | /* if this tile_pass doesn't cover any of the tiles - keep going */ | |
1714 | continue; | |
1715 | ||
1716 | /* find the tile offsets for this pass - need | |
1717 | to find all tiles left and above */ | |
1718 | drm_get_tile_offsets(fb_helper, modes, offsets, | |
1719 | i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); | |
1720 | } | |
38651674 | 1721 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
0b4c0f3f | 1722 | fb_helper_conn->connector->base.id); |
38651674 DA |
1723 | |
1724 | /* got for command line mode first */ | |
0b4c0f3f | 1725 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
38651674 | 1726 | if (!modes[i]) { |
b0ee9e7f DA |
1727 | DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", |
1728 | fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); | |
0b4c0f3f | 1729 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
38651674 DA |
1730 | } |
1731 | /* No preferred modes, pick one off the list */ | |
0b4c0f3f DA |
1732 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
1733 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) | |
38651674 DA |
1734 | break; |
1735 | } | |
1736 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : | |
1737 | "none"); | |
b0ee9e7f DA |
1738 | conn_configured |= (1 << i); |
1739 | } | |
1740 | ||
1741 | if ((conn_configured & mask) != mask) { | |
1742 | tile_pass++; | |
1743 | goto retry; | |
38651674 DA |
1744 | } |
1745 | return true; | |
1746 | } | |
1747 | ||
8be48d92 DA |
1748 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
1749 | struct drm_fb_helper_crtc **best_crtcs, | |
38651674 DA |
1750 | struct drm_display_mode **modes, |
1751 | int n, int width, int height) | |
1752 | { | |
1753 | int c, o; | |
8be48d92 | 1754 | struct drm_device *dev = fb_helper->dev; |
38651674 | 1755 | struct drm_connector *connector; |
be26a66d | 1756 | const struct drm_connector_helper_funcs *connector_funcs; |
38651674 | 1757 | struct drm_encoder *encoder; |
38651674 | 1758 | int my_score, best_score, score; |
8be48d92 | 1759 | struct drm_fb_helper_crtc **crtcs, *crtc; |
0b4c0f3f | 1760 | struct drm_fb_helper_connector *fb_helper_conn; |
38651674 | 1761 | |
0b4c0f3f | 1762 | if (n == fb_helper->connector_count) |
38651674 | 1763 | return 0; |
0b4c0f3f DA |
1764 | |
1765 | fb_helper_conn = fb_helper->connector_info[n]; | |
1766 | connector = fb_helper_conn->connector; | |
38651674 DA |
1767 | |
1768 | best_crtcs[n] = NULL; | |
8be48d92 | 1769 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
38651674 DA |
1770 | if (modes[n] == NULL) |
1771 | return best_score; | |
1772 | ||
8be48d92 DA |
1773 | crtcs = kzalloc(dev->mode_config.num_connector * |
1774 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); | |
38651674 DA |
1775 | if (!crtcs) |
1776 | return best_score; | |
1777 | ||
1778 | my_score = 1; | |
1779 | if (connector->status == connector_status_connected) | |
1780 | my_score++; | |
0b4c0f3f | 1781 | if (drm_has_cmdline_mode(fb_helper_conn)) |
38651674 | 1782 | my_score++; |
0b4c0f3f | 1783 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
38651674 DA |
1784 | my_score++; |
1785 | ||
1786 | connector_funcs = connector->helper_private; | |
1787 | encoder = connector_funcs->best_encoder(connector); | |
1788 | if (!encoder) | |
1789 | goto out; | |
1790 | ||
38651674 DA |
1791 | /* select a crtc for this connector and then attempt to configure |
1792 | remaining connectors */ | |
8be48d92 DA |
1793 | for (c = 0; c < fb_helper->crtc_count; c++) { |
1794 | crtc = &fb_helper->crtc_info[c]; | |
38651674 | 1795 | |
96081cdf | 1796 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
38651674 | 1797 | continue; |
38651674 DA |
1798 | |
1799 | for (o = 0; o < n; o++) | |
1800 | if (best_crtcs[o] == crtc) | |
1801 | break; | |
1802 | ||
1803 | if (o < n) { | |
1d42bbc8 DA |
1804 | /* ignore cloning unless only a single crtc */ |
1805 | if (fb_helper->crtc_count > 1) | |
1806 | continue; | |
1807 | ||
1808 | if (!drm_mode_equal(modes[o], modes[n])) | |
1809 | continue; | |
38651674 DA |
1810 | } |
1811 | ||
1812 | crtcs[n] = crtc; | |
8be48d92 DA |
1813 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
1814 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, | |
38651674 DA |
1815 | width, height); |
1816 | if (score > best_score) { | |
38651674 DA |
1817 | best_score = score; |
1818 | memcpy(best_crtcs, crtcs, | |
1819 | dev->mode_config.num_connector * | |
8be48d92 | 1820 | sizeof(struct drm_fb_helper_crtc *)); |
38651674 | 1821 | } |
38651674 DA |
1822 | } |
1823 | out: | |
1824 | kfree(crtcs); | |
1825 | return best_score; | |
1826 | } | |
1827 | ||
8be48d92 | 1828 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
38651674 | 1829 | { |
8be48d92 DA |
1830 | struct drm_device *dev = fb_helper->dev; |
1831 | struct drm_fb_helper_crtc **crtcs; | |
38651674 | 1832 | struct drm_display_mode **modes; |
b0ee9e7f | 1833 | struct drm_fb_offset *offsets; |
8be48d92 | 1834 | struct drm_mode_set *modeset; |
38651674 DA |
1835 | bool *enabled; |
1836 | int width, height; | |
11e17a08 | 1837 | int i; |
38651674 DA |
1838 | |
1839 | DRM_DEBUG_KMS("\n"); | |
1840 | ||
1841 | width = dev->mode_config.max_width; | |
1842 | height = dev->mode_config.max_height; | |
1843 | ||
38651674 | 1844 | crtcs = kcalloc(dev->mode_config.num_connector, |
8be48d92 | 1845 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
38651674 DA |
1846 | modes = kcalloc(dev->mode_config.num_connector, |
1847 | sizeof(struct drm_display_mode *), GFP_KERNEL); | |
b0ee9e7f DA |
1848 | offsets = kcalloc(dev->mode_config.num_connector, |
1849 | sizeof(struct drm_fb_offset), GFP_KERNEL); | |
38651674 DA |
1850 | enabled = kcalloc(dev->mode_config.num_connector, |
1851 | sizeof(bool), GFP_KERNEL); | |
b0ee9e7f | 1852 | if (!crtcs || !modes || !enabled || !offsets) { |
8c5eaca0 SK |
1853 | DRM_ERROR("Memory allocation failed\n"); |
1854 | goto out; | |
1855 | } | |
1856 | ||
38651674 | 1857 | |
0b4c0f3f | 1858 | drm_enable_connectors(fb_helper, enabled); |
38651674 | 1859 | |
11e17a08 JB |
1860 | if (!(fb_helper->funcs->initial_config && |
1861 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, | |
b0ee9e7f | 1862 | offsets, |
11e17a08 JB |
1863 | enabled, width, height))) { |
1864 | memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); | |
1865 | memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); | |
b0ee9e7f | 1866 | memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0])); |
11e17a08 | 1867 | |
b0ee9e7f DA |
1868 | if (!drm_target_cloned(fb_helper, modes, offsets, |
1869 | enabled, width, height) && | |
1870 | !drm_target_preferred(fb_helper, modes, offsets, | |
1871 | enabled, width, height)) | |
1d42bbc8 | 1872 | DRM_ERROR("Unable to find initial modes\n"); |
38651674 | 1873 | |
11e17a08 JB |
1874 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
1875 | width, height); | |
38651674 | 1876 | |
11e17a08 JB |
1877 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
1878 | } | |
8be48d92 DA |
1879 | |
1880 | /* need to set the modesets up here for use later */ | |
1881 | /* fill out the connector<->crtc mappings into the modesets */ | |
1882 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1883 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1884 | modeset->num_connectors = 0; | |
7e53f3a4 | 1885 | modeset->fb = NULL; |
8be48d92 | 1886 | } |
38651674 | 1887 | |
0b4c0f3f | 1888 | for (i = 0; i < fb_helper->connector_count; i++) { |
38651674 | 1889 | struct drm_display_mode *mode = modes[i]; |
8be48d92 | 1890 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
b0ee9e7f | 1891 | struct drm_fb_offset *offset = &offsets[i]; |
8be48d92 | 1892 | modeset = &fb_crtc->mode_set; |
38651674 | 1893 | |
8be48d92 | 1894 | if (mode && fb_crtc) { |
b0ee9e7f DA |
1895 | DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", |
1896 | mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); | |
8be48d92 | 1897 | fb_crtc->desired_mode = mode; |
b0ee9e7f DA |
1898 | fb_crtc->x = offset->x; |
1899 | fb_crtc->y = offset->y; | |
8be48d92 DA |
1900 | if (modeset->mode) |
1901 | drm_mode_destroy(dev, modeset->mode); | |
1902 | modeset->mode = drm_mode_duplicate(dev, | |
1903 | fb_crtc->desired_mode); | |
0b4c0f3f | 1904 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
7e53f3a4 | 1905 | modeset->fb = fb_helper->fb; |
b0ee9e7f DA |
1906 | modeset->x = offset->x; |
1907 | modeset->y = offset->y; | |
38651674 | 1908 | } |
38651674 DA |
1909 | } |
1910 | ||
7e53f3a4 DV |
1911 | /* Clear out any old modes if there are no more connected outputs. */ |
1912 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1913 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1914 | if (modeset->num_connectors == 0) { | |
1915 | BUG_ON(modeset->fb); | |
7e53f3a4 DV |
1916 | if (modeset->mode) |
1917 | drm_mode_destroy(dev, modeset->mode); | |
1918 | modeset->mode = NULL; | |
1919 | } | |
1920 | } | |
8c5eaca0 | 1921 | out: |
38651674 DA |
1922 | kfree(crtcs); |
1923 | kfree(modes); | |
b0ee9e7f | 1924 | kfree(offsets); |
38651674 DA |
1925 | kfree(enabled); |
1926 | } | |
1927 | ||
1928 | /** | |
207fd329 | 1929 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
d0ddc033 DV |
1930 | * @fb_helper: fb_helper device struct |
1931 | * @bpp_sel: bpp value to use for the framebuffer configuration | |
38651674 | 1932 | * |
d0ddc033 | 1933 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
38651674 DA |
1934 | * At the moment, this is a cloned configuration across all heads with |
1935 | * a new framebuffer object as the backing store. | |
1936 | * | |
207fd329 DV |
1937 | * Note that this also registers the fbdev and so allows userspace to call into |
1938 | * the driver through the fbdev interfaces. | |
1939 | * | |
1940 | * This function will call down into the ->fb_probe callback to let | |
1941 | * the driver allocate and initialize the fbdev info structure and the drm | |
1942 | * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and | |
1943 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default | |
1944 | * values for the fbdev info structure. | |
1945 | * | |
38651674 DA |
1946 | * RETURNS: |
1947 | * Zero if everything went ok, nonzero otherwise. | |
1948 | */ | |
01934c2a | 1949 | int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
38651674 | 1950 | { |
8be48d92 | 1951 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1952 | int count = 0; |
1953 | ||
53f1904b | 1954 | mutex_lock(&dev->mode_config.mutex); |
0b4c0f3f DA |
1955 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
1956 | dev->mode_config.max_width, | |
1957 | dev->mode_config.max_height); | |
53f1904b | 1958 | mutex_unlock(&dev->mode_config.mutex); |
38651674 DA |
1959 | /* |
1960 | * we shouldn't end up with no modes here. | |
1961 | */ | |
96081cdf | 1962 | if (count == 0) |
d56b1b9d | 1963 | dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); |
96081cdf | 1964 | |
8be48d92 | 1965 | drm_setup_crtcs(fb_helper); |
38651674 | 1966 | |
4abe3520 | 1967 | return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
38651674 | 1968 | } |
8be48d92 | 1969 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 1970 | |
7394371d CW |
1971 | /** |
1972 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by | |
d0ddc033 | 1973 | * probing all the outputs attached to the fb |
7394371d CW |
1974 | * @fb_helper: the drm_fb_helper |
1975 | * | |
7394371d CW |
1976 | * Scan the connectors attached to the fb_helper and try to put together a |
1977 | * setup after *notification of a change in output configuration. | |
1978 | * | |
207fd329 DV |
1979 | * Called at runtime, takes the mode config locks to be able to check/change the |
1980 | * modeset configuration. Must be run from process context (which usually means | |
1981 | * either the output polling work or a work item launched from the driver's | |
1982 | * hotplug interrupt). | |
1983 | * | |
50c3dc97 DV |
1984 | * Note that drivers may call this even before calling |
1985 | * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows | |
1986 | * for a race-free fbcon setup and will make sure that the fbdev emulation will | |
1987 | * not miss any hotplug events. | |
207fd329 | 1988 | * |
7394371d CW |
1989 | * RETURNS: |
1990 | * 0 on success and a non-zero error code otherwise. | |
1991 | */ | |
1992 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) | |
38651674 | 1993 | { |
7394371d | 1994 | struct drm_device *dev = fb_helper->dev; |
51bbd276 | 1995 | u32 max_width, max_height; |
5c4426a7 | 1996 | |
89ced125 | 1997 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
50c3dc97 | 1998 | if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { |
4abe3520 | 1999 | fb_helper->delayed_hotplug = true; |
89ced125 | 2000 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
7394371d | 2001 | return 0; |
4abe3520 | 2002 | } |
eb1f8e4f | 2003 | DRM_DEBUG_KMS("\n"); |
4abe3520 | 2004 | |
eb1f8e4f DA |
2005 | max_width = fb_helper->fb->width; |
2006 | max_height = fb_helper->fb->height; | |
5c4426a7 | 2007 | |
51bbd276 | 2008 | drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); |
89ced125 DV |
2009 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
2010 | ||
2011 | drm_modeset_lock_all(dev); | |
eb1f8e4f | 2012 | drm_setup_crtcs(fb_helper); |
84849903 | 2013 | drm_modeset_unlock_all(dev); |
2180c3c7 DV |
2014 | drm_fb_helper_set_par(fb_helper->fbdev); |
2015 | ||
2016 | return 0; | |
5c4426a7 | 2017 | } |
eb1f8e4f | 2018 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
5c4426a7 | 2019 | |
6a108a14 | 2020 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
3ce05168 DF |
2021 | * but the module doesn't depend on any fb console symbols. At least |
2022 | * attempt to load fbcon to avoid leaving the system without a usable console. | |
2023 | */ | |
6a108a14 | 2024 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
3ce05168 DF |
2025 | static int __init drm_fb_helper_modinit(void) |
2026 | { | |
2027 | const char *name = "fbcon"; | |
2028 | struct module *fbcon; | |
2029 | ||
2030 | mutex_lock(&module_mutex); | |
2031 | fbcon = find_module(name); | |
2032 | mutex_unlock(&module_mutex); | |
2033 | ||
2034 | if (!fbcon) | |
2035 | request_module_nowait(name); | |
2036 | return 0; | |
2037 | } | |
2038 | ||
2039 | module_init(drm_fb_helper_modinit); | |
2040 | #endif |