Rev 4539 | Rev 5060 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1179 | serge | 1 | /* |
2 | * Copyright (c) 2006-2009 Red Hat Inc. |
||
3 | * Copyright (c) 2006-2008 Intel Corporation |
||
4 | * Copyright (c) 2007 Dave Airlie |
||
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 |
||
28 | * Jesse Barnes |
||
29 | */ |
||
3192 | Serge | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
31 | |||
1430 | serge | 32 | #include |
1963 | serge | 33 | #include |
34 | #include |
||
1179 | serge | 35 | #include |
3031 | serge | 36 | #include |
37 | #include |
||
38 | #include |
||
39 | #include |
||
40 | #include |
||
1179 | serge | 41 | |
42 | static LIST_HEAD(kernel_fb_helper_list); |
||
43 | |||
3192 | Serge | 44 | /** |
45 | * DOC: fbdev helpers |
||
46 | * |
||
47 | * The fb helper functions are useful to provide an fbdev on top of a drm kernel |
||
48 | * mode setting driver. They can be used mostly independantely from the crtc |
||
49 | * helper functions used by many drivers to implement the kernel mode setting |
||
50 | * interfaces. |
||
3480 | Serge | 51 | * |
52 | * Initialization is done as a three-step process with drm_fb_helper_init(), |
||
53 | * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config(). |
||
54 | * Drivers with fancier requirements than the default beheviour can override the |
||
55 | * second step with their own code. Teardown is done with drm_fb_helper_fini(). |
||
56 | * |
||
57 | * At runtime drivers should restore the fbdev console by calling |
||
58 | * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They |
||
59 | * should also notify the fb helper code from updates to the output |
||
60 | * configuration by calling drm_fb_helper_hotplug_event(). For easier |
||
61 | * integration with the output polling code in drm_crtc_helper.c the modeset |
||
62 | * code proves a ->output_poll_changed callback. |
||
63 | * |
||
64 | * All other functions exported by the fb helper library can be used to |
||
65 | * implement the fbdev driver interface by the driver. |
||
3192 | Serge | 66 | */ |
67 | |||
3480 | Serge | 68 | /** |
69 | * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev |
||
70 | * emulation helper |
||
71 | * @fb_helper: fbdev initialized with drm_fb_helper_init |
||
72 | * |
||
73 | * This functions adds all the available connectors for use with the given |
||
74 | * fb_helper. This is a separate step to allow drivers to freely assign |
||
75 | * connectors to the fbdev, e.g. if some are reserved for special purposes or |
||
76 | * not adequate to be used for the fbcon. |
||
77 | * |
||
78 | * Since this is part of the initial setup before the fbdev is published, no |
||
79 | * locking is required. |
||
80 | */ |
||
1963 | serge | 81 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) |
1221 | serge | 82 | { |
1963 | serge | 83 | struct drm_device *dev = fb_helper->dev; |
84 | struct drm_connector *connector; |
||
85 | int i; |
||
1221 | serge | 86 | |
1963 | serge | 87 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
88 | struct drm_fb_helper_connector *fb_helper_connector; |
||
89 | |||
90 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); |
||
91 | if (!fb_helper_connector) |
||
92 | goto fail; |
||
93 | |||
94 | fb_helper_connector->connector = connector; |
||
95 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; |
||
96 | } |
||
1221 | serge | 97 | return 0; |
1963 | serge | 98 | fail: |
99 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
100 | kfree(fb_helper->connector_info[i]); |
||
101 | fb_helper->connector_info[i] = NULL; |
||
102 | } |
||
103 | fb_helper->connector_count = 0; |
||
104 | return -ENOMEM; |
||
1221 | serge | 105 | } |
1963 | serge | 106 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
1221 | serge | 107 | |
1963 | serge | 108 | static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) |
1179 | serge | 109 | { |
1963 | serge | 110 | uint16_t *r_base, *g_base, *b_base; |
111 | int i; |
||
1179 | serge | 112 | |
4104 | Serge | 113 | if (helper->funcs->gamma_get == NULL) |
114 | return; |
||
115 | |||
1963 | serge | 116 | r_base = crtc->gamma_store; |
117 | g_base = r_base + crtc->gamma_size; |
||
118 | b_base = g_base + crtc->gamma_size; |
||
1179 | serge | 119 | |
1963 | serge | 120 | for (i = 0; i < crtc->gamma_size; i++) |
121 | helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); |
||
1179 | serge | 122 | } |
123 | |||
1963 | serge | 124 | static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) |
1179 | serge | 125 | { |
1963 | serge | 126 | uint16_t *r_base, *g_base, *b_base; |
127 | |||
3031 | serge | 128 | if (crtc->funcs->gamma_set == NULL) |
129 | return; |
||
130 | |||
1963 | serge | 131 | r_base = crtc->gamma_store; |
132 | g_base = r_base + crtc->gamma_size; |
||
133 | b_base = g_base + crtc->gamma_size; |
||
134 | |||
135 | crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size); |
||
1179 | serge | 136 | } |
137 | |||
138 | |||
3480 | Serge | 139 | static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) |
140 | { |
||
141 | struct drm_device *dev = fb_helper->dev; |
||
142 | struct drm_crtc *crtc; |
||
143 | int bound = 0, crtcs_bound = 0; |
||
1179 | serge | 144 | |
3480 | Serge | 145 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
146 | if (crtc->fb) |
||
147 | crtcs_bound++; |
||
148 | if (crtc->fb == fb_helper->fb) |
||
149 | bound++; |
||
150 | } |
||
151 | |||
152 | if (bound < crtcs_bound) |
||
153 | return false; |
||
4560 | Serge | 154 | |
3480 | Serge | 155 | return true; |
156 | } |
||
157 | |||
4560 | Serge | 158 | #ifdef CONFIG_MAGIC_SYSRQ |
159 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
||
160 | { |
||
161 | bool ret; |
||
162 | ret = drm_fb_helper_force_kernel_mode(); |
||
163 | if (ret == true) |
||
164 | DRM_ERROR("Failed to restore crtc configuration\n"); |
||
165 | } |
||
166 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); |
||
167 | |||
168 | static void drm_fb_helper_sysrq(int dummy1) |
||
169 | { |
||
170 | schedule_work(&drm_fb_helper_restore_work); |
||
171 | } |
||
172 | |||
173 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { |
||
174 | .handler = drm_fb_helper_sysrq, |
||
175 | .help_msg = "force-fb(V)", |
||
176 | .action_msg = "Restore framebuffer console", |
||
177 | }; |
||
178 | #else |
||
179 | |||
180 | #endif |
||
181 | |||
3031 | serge | 182 | static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) |
1179 | serge | 183 | { |
184 | struct drm_fb_helper *fb_helper = info->par; |
||
185 | struct drm_device *dev = fb_helper->dev; |
||
186 | struct drm_crtc *crtc; |
||
1963 | serge | 187 | struct drm_connector *connector; |
188 | int i, j; |
||
1179 | serge | 189 | |
190 | /* |
||
3480 | Serge | 191 | * fbdev->blank can be called from irq context in case of a panic. |
192 | * Since we already have our own special panic handler which will |
||
193 | * restore the fbdev console mode completely, just bail out early. |
||
194 | */ |
||
195 | |||
196 | /* |
||
3031 | serge | 197 | * For each CRTC in this fb, turn the connectors on/off. |
1179 | serge | 198 | */ |
3480 | Serge | 199 | drm_modeset_lock_all(dev); |
200 | if (!drm_fb_helper_is_bound(fb_helper)) { |
||
201 | drm_modeset_unlock_all(dev); |
||
202 | return; |
||
203 | } |
||
204 | |||
1179 | serge | 205 | for (i = 0; i < fb_helper->crtc_count; i++) { |
1963 | serge | 206 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
1179 | serge | 207 | |
1963 | serge | 208 | if (!crtc->enabled) |
1179 | serge | 209 | continue; |
210 | |||
3031 | serge | 211 | /* Walk the connectors & encoders on this fb turning them on/off */ |
1963 | serge | 212 | for (j = 0; j < fb_helper->connector_count; j++) { |
213 | connector = fb_helper->connector_info[j]->connector; |
||
3031 | serge | 214 | connector->funcs->dpms(connector, dpms_mode); |
3192 | Serge | 215 | drm_object_property_set_value(&connector->base, |
3031 | serge | 216 | dev->mode_config.dpms_property, dpms_mode); |
1963 | serge | 217 | } |
218 | } |
||
3480 | Serge | 219 | drm_modeset_unlock_all(dev); |
1179 | serge | 220 | } |
221 | |||
3480 | Serge | 222 | /** |
223 | * drm_fb_helper_blank - implementation for ->fb_blank |
||
224 | * @blank: desired blanking state |
||
225 | * @info: fbdev registered by the helper |
||
226 | */ |
||
1179 | serge | 227 | int drm_fb_helper_blank(int blank, struct fb_info *info) |
228 | { |
||
229 | switch (blank) { |
||
1321 | serge | 230 | /* Display: On; HSync: On, VSync: On */ |
1179 | serge | 231 | case FB_BLANK_UNBLANK: |
3031 | serge | 232 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); |
1179 | serge | 233 | break; |
1321 | serge | 234 | /* Display: Off; HSync: On, VSync: On */ |
1179 | serge | 235 | case FB_BLANK_NORMAL: |
3031 | serge | 236 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
1179 | serge | 237 | break; |
1321 | serge | 238 | /* Display: Off; HSync: Off, VSync: On */ |
1179 | serge | 239 | case FB_BLANK_HSYNC_SUSPEND: |
3031 | serge | 240 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); |
1179 | serge | 241 | break; |
1321 | serge | 242 | /* Display: Off; HSync: On, VSync: Off */ |
1179 | serge | 243 | case FB_BLANK_VSYNC_SUSPEND: |
3031 | serge | 244 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); |
1179 | serge | 245 | break; |
1321 | serge | 246 | /* Display: Off; HSync: Off, VSync: Off */ |
1179 | serge | 247 | case FB_BLANK_POWERDOWN: |
3031 | serge | 248 | drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); |
1179 | serge | 249 | break; |
250 | } |
||
251 | return 0; |
||
252 | } |
||
253 | EXPORT_SYMBOL(drm_fb_helper_blank); |
||
254 | |||
255 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) |
||
256 | { |
||
257 | int i; |
||
258 | |||
1963 | serge | 259 | for (i = 0; i < helper->connector_count; i++) |
260 | kfree(helper->connector_info[i]); |
||
261 | kfree(helper->connector_info); |
||
3031 | serge | 262 | for (i = 0; i < helper->crtc_count; i++) { |
1179 | serge | 263 | kfree(helper->crtc_info[i].mode_set.connectors); |
3031 | serge | 264 | if (helper->crtc_info[i].mode_set.mode) |
265 | drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); |
||
266 | } |
||
1179 | serge | 267 | kfree(helper->crtc_info); |
268 | } |
||
269 | |||
3480 | Serge | 270 | /** |
271 | * drm_fb_helper_init - initialize a drm_fb_helper structure |
||
272 | * @dev: drm device |
||
273 | * @fb_helper: driver-allocated fbdev helper structure to initialize |
||
274 | * @crtc_count: maximum number of crtcs to support in this fbdev emulation |
||
275 | * @max_conn_count: max connector count |
||
276 | * |
||
277 | * This allocates the structures for the fbdev helper with the given limits. |
||
278 | * Note that this won't yet touch the hardware (through the driver interfaces) |
||
279 | * nor register the fbdev. This is only done in drm_fb_helper_initial_config() |
||
280 | * to allow driver writes more control over the exact init sequence. |
||
281 | * |
||
282 | * Drivers must set fb_helper->funcs before calling |
||
283 | * drm_fb_helper_initial_config(). |
||
284 | * |
||
285 | * RETURNS: |
||
286 | * Zero if everything went ok, nonzero otherwise. |
||
287 | */ |
||
1963 | serge | 288 | int drm_fb_helper_init(struct drm_device *dev, |
289 | struct drm_fb_helper *fb_helper, |
||
290 | int crtc_count, int max_conn_count) |
||
1179 | serge | 291 | { |
292 | struct drm_crtc *crtc; |
||
293 | int i; |
||
294 | |||
1963 | serge | 295 | fb_helper->dev = dev; |
296 | |||
297 | INIT_LIST_HEAD(&fb_helper->kernel_fb_list); |
||
298 | |||
299 | fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
||
300 | if (!fb_helper->crtc_info) |
||
1179 | serge | 301 | return -ENOMEM; |
302 | |||
1963 | serge | 303 | fb_helper->crtc_count = crtc_count; |
304 | fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
||
305 | if (!fb_helper->connector_info) { |
||
306 | kfree(fb_helper->crtc_info); |
||
307 | return -ENOMEM; |
||
308 | } |
||
309 | fb_helper->connector_count = 0; |
||
1179 | serge | 310 | |
311 | for (i = 0; i < crtc_count; i++) { |
||
1963 | serge | 312 | fb_helper->crtc_info[i].mode_set.connectors = |
1179 | serge | 313 | kcalloc(max_conn_count, |
314 | sizeof(struct drm_connector *), |
||
315 | GFP_KERNEL); |
||
316 | |||
3031 | serge | 317 | if (!fb_helper->crtc_info[i].mode_set.connectors) |
1179 | serge | 318 | goto out_free; |
1963 | serge | 319 | fb_helper->crtc_info[i].mode_set.num_connectors = 0; |
1179 | serge | 320 | } |
321 | |||
322 | i = 0; |
||
323 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
||
1963 | serge | 324 | fb_helper->crtc_info[i].mode_set.crtc = crtc; |
1179 | serge | 325 | i++; |
326 | } |
||
3031 | serge | 327 | |
1179 | serge | 328 | return 0; |
329 | out_free: |
||
1963 | serge | 330 | drm_fb_helper_crtc_free(fb_helper); |
1179 | serge | 331 | return -ENOMEM; |
332 | } |
||
1963 | serge | 333 | EXPORT_SYMBOL(drm_fb_helper_init); |
1179 | serge | 334 | |
1246 | serge | 335 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
1221 | serge | 336 | u16 blue, u16 regno, struct fb_info *info) |
337 | { |
||
338 | struct drm_fb_helper *fb_helper = info->par; |
||
339 | struct drm_framebuffer *fb = fb_helper->fb; |
||
340 | int pindex; |
||
341 | |||
1246 | serge | 342 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
343 | u32 *palette; |
||
344 | u32 value; |
||
345 | /* place color in psuedopalette */ |
||
346 | if (regno > 16) |
||
347 | return -EINVAL; |
||
348 | palette = (u32 *)info->pseudo_palette; |
||
349 | red >>= (16 - info->var.red.length); |
||
350 | green >>= (16 - info->var.green.length); |
||
351 | blue >>= (16 - info->var.blue.length); |
||
352 | value = (red << info->var.red.offset) | |
||
353 | (green << info->var.green.offset) | |
||
354 | (blue << info->var.blue.offset); |
||
1963 | serge | 355 | if (info->var.transp.length > 0) { |
356 | u32 mask = (1 << info->var.transp.length) - 1; |
||
357 | mask <<= info->var.transp.offset; |
||
358 | value |= mask; |
||
359 | } |
||
1246 | serge | 360 | palette[regno] = value; |
361 | return 0; |
||
362 | } |
||
363 | |||
4104 | Serge | 364 | /* |
365 | * The driver really shouldn't advertise pseudo/directcolor |
||
366 | * visuals if it can't deal with the palette. |
||
367 | */ |
||
368 | if (WARN_ON(!fb_helper->funcs->gamma_set || |
||
369 | !fb_helper->funcs->gamma_get)) |
||
370 | return -EINVAL; |
||
371 | |||
1221 | serge | 372 | pindex = regno; |
373 | |||
374 | if (fb->bits_per_pixel == 16) { |
||
375 | pindex = regno << 3; |
||
376 | |||
377 | if (fb->depth == 16 && regno > 63) |
||
1246 | serge | 378 | return -EINVAL; |
1221 | serge | 379 | if (fb->depth == 15 && regno > 31) |
1246 | serge | 380 | return -EINVAL; |
1221 | serge | 381 | |
382 | if (fb->depth == 16) { |
||
383 | u16 r, g, b; |
||
384 | int i; |
||
385 | if (regno < 32) { |
||
386 | for (i = 0; i < 8; i++) |
||
387 | fb_helper->funcs->gamma_set(crtc, red, |
||
388 | green, blue, pindex + i); |
||
389 | } |
||
390 | |||
391 | fb_helper->funcs->gamma_get(crtc, &r, |
||
392 | &g, &b, |
||
393 | pindex >> 1); |
||
394 | |||
395 | for (i = 0; i < 4; i++) |
||
396 | fb_helper->funcs->gamma_set(crtc, r, |
||
397 | green, b, |
||
398 | (pindex >> 1) + i); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | if (fb->depth != 16) |
||
403 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); |
||
1246 | serge | 404 | return 0; |
1221 | serge | 405 | } |
406 | |||
3480 | Serge | 407 | /** |
408 | * drm_fb_helper_setcmap - implementation for ->fb_setcmap |
||
409 | * @cmap: cmap to set |
||
410 | * @info: fbdev registered by the helper |
||
411 | */ |
||
1221 | serge | 412 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
413 | { |
||
414 | struct drm_fb_helper *fb_helper = info->par; |
||
4104 | Serge | 415 | struct drm_device *dev = fb_helper->dev; |
1963 | serge | 416 | struct drm_crtc_helper_funcs *crtc_funcs; |
1221 | serge | 417 | u16 *red, *green, *blue, *transp; |
418 | struct drm_crtc *crtc; |
||
1963 | serge | 419 | int i, j, rc = 0; |
1221 | serge | 420 | int start; |
421 | |||
4104 | Serge | 422 | drm_modeset_lock_all(dev); |
423 | if (!drm_fb_helper_is_bound(fb_helper)) { |
||
424 | drm_modeset_unlock_all(dev); |
||
425 | return -EBUSY; |
||
426 | } |
||
427 | |||
1221 | serge | 428 | for (i = 0; i < fb_helper->crtc_count; i++) { |
1963 | serge | 429 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
430 | crtc_funcs = crtc->helper_private; |
||
1221 | serge | 431 | |
432 | red = cmap->red; |
||
433 | green = cmap->green; |
||
434 | blue = cmap->blue; |
||
435 | transp = cmap->transp; |
||
436 | start = cmap->start; |
||
437 | |||
1963 | serge | 438 | for (j = 0; j < cmap->len; j++) { |
1221 | serge | 439 | u16 hred, hgreen, hblue, htransp = 0xffff; |
440 | |||
441 | hred = *red++; |
||
442 | hgreen = *green++; |
||
443 | hblue = *blue++; |
||
444 | |||
445 | if (transp) |
||
446 | htransp = *transp++; |
||
447 | |||
1246 | serge | 448 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
449 | if (rc) |
||
4104 | Serge | 450 | goto out; |
1221 | serge | 451 | } |
4104 | Serge | 452 | if (crtc_funcs->load_lut) |
1221 | serge | 453 | crtc_funcs->load_lut(crtc); |
454 | } |
||
4104 | Serge | 455 | out: |
456 | drm_modeset_unlock_all(dev); |
||
1221 | serge | 457 | return rc; |
458 | } |
||
459 | EXPORT_SYMBOL(drm_fb_helper_setcmap); |
||
460 | |||
3480 | Serge | 461 | /** |
462 | * drm_fb_helper_check_var - implementation for ->fb_check_var |
||
463 | * @var: screeninfo to check |
||
464 | * @info: fbdev registered by the helper |
||
465 | */ |
||
1179 | serge | 466 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, |
467 | struct fb_info *info) |
||
468 | { |
||
469 | struct drm_fb_helper *fb_helper = info->par; |
||
470 | struct drm_framebuffer *fb = fb_helper->fb; |
||
471 | int depth; |
||
472 | |||
1963 | serge | 473 | if (var->pixclock != 0 || in_dbg_master()) |
1179 | serge | 474 | return -EINVAL; |
475 | |||
476 | /* Need to resize the fb object !!! */ |
||
3031 | serge | 477 | if (var->bits_per_pixel > fb->bits_per_pixel || |
478 | var->xres > fb->width || var->yres > fb->height || |
||
479 | var->xres_virtual > fb->width || var->yres_virtual > fb->height) { |
||
1404 | serge | 480 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " |
3031 | serge | 481 | "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", |
482 | var->xres, var->yres, var->bits_per_pixel, |
||
483 | var->xres_virtual, var->yres_virtual, |
||
1404 | serge | 484 | fb->width, fb->height, fb->bits_per_pixel); |
1179 | serge | 485 | return -EINVAL; |
486 | } |
||
487 | |||
488 | switch (var->bits_per_pixel) { |
||
489 | case 16: |
||
490 | depth = (var->green.length == 6) ? 16 : 15; |
||
491 | break; |
||
492 | case 32: |
||
493 | depth = (var->transp.length > 0) ? 32 : 24; |
||
494 | break; |
||
495 | default: |
||
496 | depth = var->bits_per_pixel; |
||
497 | break; |
||
498 | } |
||
499 | |||
500 | switch (depth) { |
||
501 | case 8: |
||
502 | var->red.offset = 0; |
||
503 | var->green.offset = 0; |
||
504 | var->blue.offset = 0; |
||
505 | var->red.length = 8; |
||
506 | var->green.length = 8; |
||
507 | var->blue.length = 8; |
||
508 | var->transp.length = 0; |
||
509 | var->transp.offset = 0; |
||
510 | break; |
||
511 | case 15: |
||
512 | var->red.offset = 10; |
||
513 | var->green.offset = 5; |
||
514 | var->blue.offset = 0; |
||
515 | var->red.length = 5; |
||
516 | var->green.length = 5; |
||
517 | var->blue.length = 5; |
||
518 | var->transp.length = 1; |
||
519 | var->transp.offset = 15; |
||
520 | break; |
||
521 | case 16: |
||
522 | var->red.offset = 11; |
||
523 | var->green.offset = 5; |
||
524 | var->blue.offset = 0; |
||
525 | var->red.length = 5; |
||
526 | var->green.length = 6; |
||
527 | var->blue.length = 5; |
||
528 | var->transp.length = 0; |
||
529 | var->transp.offset = 0; |
||
530 | break; |
||
531 | case 24: |
||
532 | var->red.offset = 16; |
||
533 | var->green.offset = 8; |
||
534 | var->blue.offset = 0; |
||
535 | var->red.length = 8; |
||
536 | var->green.length = 8; |
||
537 | var->blue.length = 8; |
||
538 | var->transp.length = 0; |
||
539 | var->transp.offset = 0; |
||
540 | break; |
||
541 | case 32: |
||
542 | var->red.offset = 16; |
||
543 | var->green.offset = 8; |
||
544 | var->blue.offset = 0; |
||
545 | var->red.length = 8; |
||
546 | var->green.length = 8; |
||
547 | var->blue.length = 8; |
||
548 | var->transp.length = 8; |
||
549 | var->transp.offset = 24; |
||
550 | break; |
||
551 | default: |
||
552 | return -EINVAL; |
||
553 | } |
||
554 | return 0; |
||
555 | } |
||
556 | EXPORT_SYMBOL(drm_fb_helper_check_var); |
||
557 | |||
3480 | Serge | 558 | /** |
559 | * drm_fb_helper_set_par - implementation for ->fb_set_par |
||
560 | * @info: fbdev registered by the helper |
||
561 | * |
||
562 | * This will let fbcon do the mode init and is called at initialization time by |
||
563 | * the fbdev core when registering the driver, and later on through the hotplug |
||
564 | * callback. |
||
565 | */ |
||
1179 | serge | 566 | int drm_fb_helper_set_par(struct fb_info *info) |
567 | { |
||
568 | struct drm_fb_helper *fb_helper = info->par; |
||
569 | struct drm_device *dev = fb_helper->dev; |
||
570 | struct fb_var_screeninfo *var = &info->var; |
||
571 | int ret; |
||
572 | int i; |
||
573 | |||
1313 | serge | 574 | if (var->pixclock != 0) { |
1430 | serge | 575 | DRM_ERROR("PIXEL CLOCK SET\n"); |
1179 | serge | 576 | return -EINVAL; |
577 | } |
||
578 | |||
3480 | Serge | 579 | drm_modeset_lock_all(dev); |
1179 | serge | 580 | for (i = 0; i < fb_helper->crtc_count; i++) { |
3480 | Serge | 581 | ret = drm_mode_set_config_internal(&fb_helper->crtc_info[i].mode_set); |
1963 | serge | 582 | if (ret) { |
3480 | Serge | 583 | drm_modeset_unlock_all(dev); |
1179 | serge | 584 | return ret; |
585 | } |
||
586 | } |
||
3480 | Serge | 587 | drm_modeset_unlock_all(dev); |
1963 | serge | 588 | |
589 | if (fb_helper->delayed_hotplug) { |
||
590 | fb_helper->delayed_hotplug = false; |
||
3480 | Serge | 591 | drm_fb_helper_hotplug_event(fb_helper); |
1963 | serge | 592 | } |
1179 | serge | 593 | return 0; |
594 | } |
||
595 | EXPORT_SYMBOL(drm_fb_helper_set_par); |
||
596 | |||
3480 | Serge | 597 | /** |
598 | * drm_fb_helper_pan_display - implementation for ->fb_pan_display |
||
599 | * @var: updated screen information |
||
600 | * @info: fbdev registered by the helper |
||
601 | */ |
||
1179 | serge | 602 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, |
603 | struct fb_info *info) |
||
604 | { |
||
605 | struct drm_fb_helper *fb_helper = info->par; |
||
606 | struct drm_device *dev = fb_helper->dev; |
||
607 | struct drm_mode_set *modeset; |
||
608 | int ret = 0; |
||
609 | int i; |
||
610 | |||
3480 | Serge | 611 | drm_modeset_lock_all(dev); |
612 | if (!drm_fb_helper_is_bound(fb_helper)) { |
||
613 | drm_modeset_unlock_all(dev); |
||
614 | return -EBUSY; |
||
615 | } |
||
616 | |||
1179 | serge | 617 | for (i = 0; i < fb_helper->crtc_count; i++) { |
618 | modeset = &fb_helper->crtc_info[i].mode_set; |
||
619 | |||
620 | modeset->x = var->xoffset; |
||
621 | modeset->y = var->yoffset; |
||
622 | |||
623 | if (modeset->num_connectors) { |
||
3480 | Serge | 624 | ret = drm_mode_set_config_internal(modeset); |
1179 | serge | 625 | if (!ret) { |
626 | info->var.xoffset = var->xoffset; |
||
627 | info->var.yoffset = var->yoffset; |
||
628 | } |
||
629 | } |
||
630 | } |
||
3480 | Serge | 631 | drm_modeset_unlock_all(dev); |
1179 | serge | 632 | return ret; |
633 | } |
||
634 | EXPORT_SYMBOL(drm_fb_helper_pan_display); |
||
635 | |||
3480 | Serge | 636 | /* |
637 | * Allocates the backing storage and sets up the fbdev info structure through |
||
638 | * the ->fb_probe callback and then registers the fbdev and sets up the panic |
||
639 | * notifier. |
||
640 | */ |
||
641 | static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
||
1963 | serge | 642 | int preferred_bpp) |
1179 | serge | 643 | { |
3480 | Serge | 644 | int ret = 0; |
1179 | serge | 645 | int crtc_count = 0; |
1963 | serge | 646 | int i; |
1179 | serge | 647 | struct fb_info *info; |
1963 | serge | 648 | struct drm_fb_helper_surface_size sizes; |
649 | int gamma_size = 0; |
||
1179 | serge | 650 | |
1963 | serge | 651 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); |
652 | sizes.surface_depth = 24; |
||
653 | sizes.surface_bpp = 32; |
||
654 | sizes.fb_width = (unsigned)-1; |
||
655 | sizes.fb_height = (unsigned)-1; |
||
1221 | serge | 656 | |
3031 | serge | 657 | /* if driver picks 8 or 16 by default use that |
658 | for both depth/bpp */ |
||
3192 | Serge | 659 | if (preferred_bpp != sizes.surface_bpp) |
3031 | serge | 660 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; |
3192 | Serge | 661 | |
3031 | serge | 662 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
663 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
664 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; |
||
665 | struct drm_cmdline_mode *cmdline_mode; |
||
666 | |||
667 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
||
668 | |||
669 | if (cmdline_mode->bpp_specified) { |
||
670 | switch (cmdline_mode->bpp) { |
||
671 | case 8: |
||
672 | sizes.surface_depth = sizes.surface_bpp = 8; |
||
673 | break; |
||
674 | case 15: |
||
675 | sizes.surface_depth = 15; |
||
676 | sizes.surface_bpp = 16; |
||
677 | break; |
||
678 | case 16: |
||
679 | sizes.surface_depth = sizes.surface_bpp = 16; |
||
680 | break; |
||
681 | case 24: |
||
682 | sizes.surface_depth = sizes.surface_bpp = 24; |
||
683 | break; |
||
684 | case 32: |
||
1963 | serge | 685 | sizes.surface_depth = 24; |
686 | sizes.surface_bpp = 32; |
||
3031 | serge | 687 | break; |
688 | } |
||
689 | break; |
||
690 | } |
||
691 | } |
||
1221 | serge | 692 | |
1963 | serge | 693 | crtc_count = 0; |
694 | for (i = 0; i < fb_helper->crtc_count; i++) { |
||
695 | struct drm_display_mode *desired_mode; |
||
696 | desired_mode = fb_helper->crtc_info[i].desired_mode; |
||
1221 | serge | 697 | |
1963 | serge | 698 | if (desired_mode) { |
699 | if (gamma_size == 0) |
||
700 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; |
||
701 | if (desired_mode->hdisplay < sizes.fb_width) |
||
702 | sizes.fb_width = desired_mode->hdisplay; |
||
703 | if (desired_mode->vdisplay < sizes.fb_height) |
||
704 | sizes.fb_height = desired_mode->vdisplay; |
||
705 | if (desired_mode->hdisplay > sizes.surface_width) |
||
706 | sizes.surface_width = desired_mode->hdisplay; |
||
707 | if (desired_mode->vdisplay > sizes.surface_height) |
||
708 | sizes.surface_height = desired_mode->vdisplay; |
||
1179 | serge | 709 | crtc_count++; |
4539 | Serge | 710 | } |
1179 | serge | 711 | } |
712 | |||
1963 | serge | 713 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
1179 | serge | 714 | /* hmm everyone went away - assume VGA cable just fell out |
715 | and will come back later. */ |
||
1963 | serge | 716 | DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); |
717 | sizes.fb_width = sizes.surface_width = 1024; |
||
718 | sizes.fb_height = sizes.surface_height = 768; |
||
1179 | serge | 719 | } |
720 | |||
1963 | serge | 721 | /* push down into drivers */ |
3480 | Serge | 722 | ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); |
723 | if (ret < 0) |
||
724 | return ret; |
||
1179 | serge | 725 | |
1963 | serge | 726 | info = fb_helper->fbdev; |
1179 | serge | 727 | |
3480 | Serge | 728 | /* |
729 | * Set the fb pointer - usually drm_setup_crtcs does this for hotplug |
||
730 | * events, but at init time drm_setup_crtcs needs to be called before |
||
731 | * the fb is allocated (since we need to figure out the desired size of |
||
732 | * the fb before we can allocate it ...). Hence we need to fix things up |
||
733 | * here again. |
||
734 | */ |
||
3192 | Serge | 735 | for (i = 0; i < fb_helper->crtc_count; i++) |
3480 | Serge | 736 | if (fb_helper->crtc_info[i].mode_set.num_connectors) |
1963 | serge | 737 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; |
1179 | serge | 738 | |
3480 | Serge | 739 | |
1313 | serge | 740 | info->var.pixclock = 0; |
1963 | serge | 741 | |
742 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); |
||
743 | |||
1179 | serge | 744 | return 0; |
745 | } |
||
746 | |||
3480 | Serge | 747 | /** |
748 | * drm_fb_helper_fill_fix - initializes fixed fbdev information |
||
749 | * @info: fbdev registered by the helper |
||
750 | * @pitch: desired pitch |
||
751 | * @depth: desired depth |
||
752 | * |
||
753 | * Helper to fill in the fixed fbdev information useful for a non-accelerated |
||
754 | * fbdev emulations. Drivers which support acceleration methods which impose |
||
755 | * additional constraints need to set up their own limits. |
||
756 | * |
||
757 | * Drivers should call this (or their equivalent setup code) from their |
||
758 | * ->fb_probe callback. |
||
759 | */ |
||
1221 | serge | 760 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
761 | uint32_t depth) |
||
1179 | serge | 762 | { |
763 | info->fix.type = FB_TYPE_PACKED_PIXELS; |
||
1221 | serge | 764 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : |
1246 | serge | 765 | FB_VISUAL_TRUECOLOR; |
1963 | serge | 766 | info->fix.mmio_start = 0; |
767 | info->fix.mmio_len = 0; |
||
1179 | serge | 768 | info->fix.type_aux = 0; |
769 | info->fix.xpanstep = 1; /* doing it in hw */ |
||
770 | info->fix.ypanstep = 1; /* doing it in hw */ |
||
771 | info->fix.ywrapstep = 0; |
||
772 | info->fix.accel = FB_ACCEL_NONE; |
||
773 | info->fix.type_aux = 0; |
||
774 | |||
775 | info->fix.line_length = pitch; |
||
776 | return; |
||
777 | } |
||
778 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); |
||
779 | |||
3480 | Serge | 780 | /** |
781 | * drm_fb_helper_fill_var - initalizes variable fbdev information |
||
782 | * @info: fbdev instance to set up |
||
783 | * @fb_helper: fb helper instance to use as template |
||
784 | * @fb_width: desired fb width |
||
785 | * @fb_height: desired fb height |
||
786 | * |
||
787 | * Sets up the variable fbdev metainformation from the given fb helper instance |
||
788 | * and the drm framebuffer allocated in fb_helper->fb. |
||
789 | * |
||
790 | * Drivers should call this (or their equivalent setup code) from their |
||
791 | * ->fb_probe callback after having allocated the fbdev backing |
||
792 | * storage framebuffer. |
||
793 | */ |
||
1963 | serge | 794 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
1179 | serge | 795 | uint32_t fb_width, uint32_t fb_height) |
796 | { |
||
1963 | serge | 797 | struct drm_framebuffer *fb = fb_helper->fb; |
798 | info->pseudo_palette = fb_helper->pseudo_palette; |
||
1179 | serge | 799 | info->var.xres_virtual = fb->width; |
800 | info->var.yres_virtual = fb->height; |
||
801 | info->var.bits_per_pixel = fb->bits_per_pixel; |
||
1963 | serge | 802 | info->var.accel_flags = FB_ACCELF_TEXT; |
1179 | serge | 803 | info->var.xoffset = 0; |
804 | info->var.yoffset = 0; |
||
805 | info->var.activate = FB_ACTIVATE_NOW; |
||
806 | info->var.height = -1; |
||
807 | info->var.width = -1; |
||
808 | |||
809 | switch (fb->depth) { |
||
810 | case 8: |
||
811 | info->var.red.offset = 0; |
||
812 | info->var.green.offset = 0; |
||
813 | info->var.blue.offset = 0; |
||
814 | info->var.red.length = 8; /* 8bit DAC */ |
||
815 | info->var.green.length = 8; |
||
816 | info->var.blue.length = 8; |
||
817 | info->var.transp.offset = 0; |
||
818 | info->var.transp.length = 0; |
||
819 | break; |
||
820 | case 15: |
||
821 | info->var.red.offset = 10; |
||
822 | info->var.green.offset = 5; |
||
823 | info->var.blue.offset = 0; |
||
824 | info->var.red.length = 5; |
||
825 | info->var.green.length = 5; |
||
826 | info->var.blue.length = 5; |
||
827 | info->var.transp.offset = 15; |
||
828 | info->var.transp.length = 1; |
||
829 | break; |
||
830 | case 16: |
||
831 | info->var.red.offset = 11; |
||
832 | info->var.green.offset = 5; |
||
833 | info->var.blue.offset = 0; |
||
834 | info->var.red.length = 5; |
||
835 | info->var.green.length = 6; |
||
836 | info->var.blue.length = 5; |
||
837 | info->var.transp.offset = 0; |
||
838 | break; |
||
839 | case 24: |
||
840 | info->var.red.offset = 16; |
||
841 | info->var.green.offset = 8; |
||
842 | info->var.blue.offset = 0; |
||
843 | info->var.red.length = 8; |
||
844 | info->var.green.length = 8; |
||
845 | info->var.blue.length = 8; |
||
846 | info->var.transp.offset = 0; |
||
847 | info->var.transp.length = 0; |
||
848 | break; |
||
849 | case 32: |
||
850 | info->var.red.offset = 16; |
||
851 | info->var.green.offset = 8; |
||
852 | info->var.blue.offset = 0; |
||
853 | info->var.red.length = 8; |
||
854 | info->var.green.length = 8; |
||
855 | info->var.blue.length = 8; |
||
856 | info->var.transp.offset = 24; |
||
857 | info->var.transp.length = 8; |
||
858 | break; |
||
859 | default: |
||
860 | break; |
||
861 | } |
||
862 | |||
863 | info->var.xres = fb_width; |
||
864 | info->var.yres = fb_height; |
||
865 | } |
||
866 | EXPORT_SYMBOL(drm_fb_helper_fill_var); |
||
1963 | serge | 867 | |
868 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
||
869 | uint32_t maxX, |
||
870 | uint32_t maxY) |
||
871 | { |
||
872 | struct drm_connector *connector; |
||
873 | int count = 0; |
||
874 | int i; |
||
875 | |||
876 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
877 | connector = fb_helper->connector_info[i]->connector; |
||
878 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
||
879 | } |
||
880 | |||
881 | return count; |
||
882 | } |
||
883 | |||
884 | static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
||
885 | { |
||
886 | struct drm_display_mode *mode; |
||
887 | |||
888 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
||
889 | if (drm_mode_width(mode) > width || |
||
890 | drm_mode_height(mode) > height) |
||
891 | continue; |
||
892 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
||
893 | return mode; |
||
894 | } |
||
895 | return NULL; |
||
896 | } |
||
897 | |||
898 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
||
899 | { |
||
900 | struct drm_cmdline_mode *cmdline_mode; |
||
901 | cmdline_mode = &fb_connector->cmdline_mode; |
||
902 | return cmdline_mode->specified; |
||
903 | } |
||
904 | |||
3746 | Serge | 905 | static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
906 | int width, int height) |
||
907 | { |
||
908 | struct drm_cmdline_mode *cmdline_mode; |
||
909 | struct drm_display_mode *mode = NULL; |
||
1963 | serge | 910 | |
3746 | Serge | 911 | return NULL; |
912 | |||
913 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
||
914 | if (cmdline_mode->specified == false) |
||
915 | return mode; |
||
916 | |||
917 | /* attempt to find a matching mode in the list of modes |
||
918 | * we have gotten so far, if not add a CVT mode that conforms |
||
919 | */ |
||
920 | if (cmdline_mode->rb || cmdline_mode->margins) |
||
921 | goto create_mode; |
||
922 | |||
923 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
||
924 | /* check width/height */ |
||
925 | if (mode->hdisplay != cmdline_mode->xres || |
||
926 | mode->vdisplay != cmdline_mode->yres) |
||
927 | continue; |
||
928 | |||
929 | if (cmdline_mode->refresh_specified) { |
||
930 | if (mode->vrefresh != cmdline_mode->refresh) |
||
931 | continue; |
||
932 | } |
||
933 | |||
934 | if (cmdline_mode->interlace) { |
||
935 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) |
||
936 | continue; |
||
937 | } |
||
938 | return mode; |
||
939 | } |
||
940 | |||
941 | create_mode: |
||
942 | mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, |
||
943 | cmdline_mode); |
||
944 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
||
945 | return mode; |
||
946 | } |
||
947 | |||
1963 | serge | 948 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) |
949 | { |
||
950 | bool enable; |
||
951 | |||
3192 | Serge | 952 | if (strict) |
1963 | serge | 953 | enable = connector->status == connector_status_connected; |
3192 | Serge | 954 | else |
1963 | serge | 955 | enable = connector->status != connector_status_disconnected; |
3192 | Serge | 956 | |
1963 | serge | 957 | return enable; |
958 | } |
||
959 | |||
960 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
||
961 | bool *enabled) |
||
962 | { |
||
963 | bool any_enabled = false; |
||
964 | struct drm_connector *connector; |
||
965 | int i = 0; |
||
966 | |||
967 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
968 | connector = fb_helper->connector_info[i]->connector; |
||
969 | enabled[i] = drm_connector_enabled(connector, true); |
||
970 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, |
||
971 | enabled[i] ? "yes" : "no"); |
||
972 | any_enabled |= enabled[i]; |
||
973 | } |
||
974 | |||
975 | if (any_enabled) |
||
976 | return; |
||
977 | |||
978 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
979 | connector = fb_helper->connector_info[i]->connector; |
||
980 | enabled[i] = drm_connector_enabled(connector, false); |
||
981 | } |
||
982 | } |
||
983 | |||
3746 | Serge | 984 | static bool drm_target_cloned(struct drm_fb_helper *fb_helper, |
985 | struct drm_display_mode **modes, |
||
986 | bool *enabled, int width, int height) |
||
987 | { |
||
988 | int count, i, j; |
||
989 | bool can_clone = false; |
||
990 | struct drm_fb_helper_connector *fb_helper_conn; |
||
991 | struct drm_display_mode *dmt_mode, *mode; |
||
1963 | serge | 992 | |
3746 | Serge | 993 | /* only contemplate cloning in the single crtc case */ |
994 | if (fb_helper->crtc_count > 1) |
||
995 | return false; |
||
996 | |||
997 | count = 0; |
||
998 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
999 | if (enabled[i]) |
||
1000 | count++; |
||
1001 | } |
||
1002 | |||
1003 | /* only contemplate cloning if more than one connector is enabled */ |
||
1004 | if (count <= 1) |
||
1005 | return false; |
||
1006 | |||
1007 | /* check the command line or if nothing common pick 1024x768 */ |
||
1008 | can_clone = true; |
||
1009 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
1010 | if (!enabled[i]) |
||
1011 | continue; |
||
1012 | fb_helper_conn = fb_helper->connector_info[i]; |
||
1013 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
||
1014 | if (!modes[i]) { |
||
1015 | can_clone = false; |
||
1016 | break; |
||
1017 | } |
||
1018 | for (j = 0; j < i; j++) { |
||
1019 | if (!enabled[j]) |
||
1020 | continue; |
||
1021 | if (!drm_mode_equal(modes[j], modes[i])) |
||
1022 | can_clone = false; |
||
1023 | } |
||
1024 | } |
||
1025 | |||
1026 | if (can_clone) { |
||
1027 | DRM_DEBUG_KMS("can clone using command line\n"); |
||
1028 | return true; |
||
1029 | } |
||
1030 | |||
1031 | /* try and find a 1024x768 mode on each connector */ |
||
1032 | can_clone = true; |
||
1033 | dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); |
||
1034 | |||
1035 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
1036 | |||
1037 | if (!enabled[i]) |
||
1038 | continue; |
||
1039 | |||
1040 | fb_helper_conn = fb_helper->connector_info[i]; |
||
1041 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
||
1042 | if (drm_mode_equal(mode, dmt_mode)) |
||
1043 | modes[i] = mode; |
||
1044 | } |
||
1045 | if (!modes[i]) |
||
1046 | can_clone = false; |
||
1047 | } |
||
1048 | |||
1049 | if (can_clone) { |
||
1050 | DRM_DEBUG_KMS("can clone using 1024x768\n"); |
||
1051 | return true; |
||
1052 | } |
||
1053 | DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); |
||
1054 | return false; |
||
1055 | } |
||
1056 | |||
1963 | serge | 1057 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
1058 | struct drm_display_mode **modes, |
||
1059 | bool *enabled, int width, int height) |
||
1060 | { |
||
1061 | struct drm_fb_helper_connector *fb_helper_conn; |
||
1062 | int i; |
||
1063 | |||
1064 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
1065 | fb_helper_conn = fb_helper->connector_info[i]; |
||
1066 | |||
1067 | if (enabled[i] == false) |
||
1068 | continue; |
||
1069 | |||
1070 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", |
||
1071 | fb_helper_conn->connector->base.id); |
||
1072 | |||
1073 | /* got for command line mode first */ |
||
3746 | Serge | 1074 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
1963 | serge | 1075 | if (!modes[i]) { |
1076 | DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", |
||
1077 | fb_helper_conn->connector->base.id); |
||
1078 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); |
||
1079 | } |
||
1080 | /* No preferred modes, pick one off the list */ |
||
1081 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
||
1082 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) |
||
1083 | break; |
||
1084 | } |
||
1085 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : |
||
1086 | "none"); |
||
1087 | } |
||
1088 | return true; |
||
1089 | } |
||
1090 | |||
1091 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
||
1092 | struct drm_fb_helper_crtc **best_crtcs, |
||
1093 | struct drm_display_mode **modes, |
||
1094 | int n, int width, int height) |
||
1095 | { |
||
1096 | int c, o; |
||
1097 | struct drm_device *dev = fb_helper->dev; |
||
1098 | struct drm_connector *connector; |
||
1099 | struct drm_connector_helper_funcs *connector_funcs; |
||
1100 | struct drm_encoder *encoder; |
||
1101 | int my_score, best_score, score; |
||
1102 | struct drm_fb_helper_crtc **crtcs, *crtc; |
||
1103 | struct drm_fb_helper_connector *fb_helper_conn; |
||
1104 | |||
1105 | if (n == fb_helper->connector_count) |
||
1106 | return 0; |
||
1107 | |||
1108 | fb_helper_conn = fb_helper->connector_info[n]; |
||
1109 | connector = fb_helper_conn->connector; |
||
1110 | |||
1111 | best_crtcs[n] = NULL; |
||
1112 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
||
1113 | if (modes[n] == NULL) |
||
1114 | return best_score; |
||
1115 | |||
1116 | crtcs = kzalloc(dev->mode_config.num_connector * |
||
1117 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
||
1118 | if (!crtcs) |
||
1119 | return best_score; |
||
1120 | |||
1121 | my_score = 1; |
||
1122 | if (connector->status == connector_status_connected) |
||
1123 | my_score++; |
||
1124 | if (drm_has_cmdline_mode(fb_helper_conn)) |
||
1125 | my_score++; |
||
1126 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
||
1127 | my_score++; |
||
1128 | |||
1129 | connector_funcs = connector->helper_private; |
||
1130 | encoder = connector_funcs->best_encoder(connector); |
||
1131 | if (!encoder) |
||
1132 | goto out; |
||
1133 | |||
1134 | /* select a crtc for this connector and then attempt to configure |
||
1135 | remaining connectors */ |
||
1136 | for (c = 0; c < fb_helper->crtc_count; c++) { |
||
1137 | crtc = &fb_helper->crtc_info[c]; |
||
1138 | |||
3192 | Serge | 1139 | if ((encoder->possible_crtcs & (1 << c)) == 0) |
1963 | serge | 1140 | continue; |
1141 | |||
1142 | for (o = 0; o < n; o++) |
||
1143 | if (best_crtcs[o] == crtc) |
||
1144 | break; |
||
1145 | |||
1146 | if (o < n) { |
||
1147 | /* ignore cloning unless only a single crtc */ |
||
1148 | if (fb_helper->crtc_count > 1) |
||
1149 | continue; |
||
1150 | |||
1151 | if (!drm_mode_equal(modes[o], modes[n])) |
||
1152 | continue; |
||
1153 | } |
||
1154 | |||
1155 | crtcs[n] = crtc; |
||
1156 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
||
1157 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, |
||
1158 | width, height); |
||
1159 | if (score > best_score) { |
||
1160 | best_score = score; |
||
1161 | memcpy(best_crtcs, crtcs, |
||
1162 | dev->mode_config.num_connector * |
||
1163 | sizeof(struct drm_fb_helper_crtc *)); |
||
1164 | } |
||
1165 | } |
||
1166 | out: |
||
1167 | kfree(crtcs); |
||
1168 | return best_score; |
||
1169 | } |
||
1170 | |||
1171 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
||
1172 | { |
||
1173 | struct drm_device *dev = fb_helper->dev; |
||
1174 | struct drm_fb_helper_crtc **crtcs; |
||
1175 | struct drm_display_mode **modes; |
||
1176 | struct drm_mode_set *modeset; |
||
1177 | bool *enabled; |
||
1178 | int width, height; |
||
3746 | Serge | 1179 | int i; |
1963 | serge | 1180 | |
1181 | DRM_DEBUG_KMS("\n"); |
||
1182 | |||
1183 | width = dev->mode_config.max_width; |
||
1184 | height = dev->mode_config.max_height; |
||
1185 | |||
1186 | crtcs = kcalloc(dev->mode_config.num_connector, |
||
1187 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
||
1188 | modes = kcalloc(dev->mode_config.num_connector, |
||
1189 | sizeof(struct drm_display_mode *), GFP_KERNEL); |
||
1190 | enabled = kcalloc(dev->mode_config.num_connector, |
||
1191 | sizeof(bool), GFP_KERNEL); |
||
3192 | Serge | 1192 | if (!crtcs || !modes || !enabled) { |
1193 | DRM_ERROR("Memory allocation failed\n"); |
||
1194 | goto out; |
||
1195 | } |
||
1963 | serge | 1196 | |
3192 | Serge | 1197 | |
1963 | serge | 1198 | drm_enable_connectors(fb_helper, enabled); |
1199 | |||
3746 | Serge | 1200 | if (!(fb_helper->funcs->initial_config && |
1201 | fb_helper->funcs->initial_config(fb_helper, crtcs, modes, |
||
1202 | enabled, width, height))) { |
||
1203 | memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); |
||
1204 | memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); |
||
1963 | serge | 1205 | |
3746 | Serge | 1206 | if (!drm_target_cloned(fb_helper, |
1207 | modes, enabled, width, height) && |
||
1208 | !drm_target_preferred(fb_helper, |
||
1209 | modes, enabled, width, height)) |
||
1963 | serge | 1210 | DRM_ERROR("Unable to find initial modes\n"); |
1211 | |||
3746 | Serge | 1212 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", |
1213 | width, height); |
||
1963 | serge | 1214 | |
1215 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
||
3746 | Serge | 1216 | } |
1963 | serge | 1217 | |
1218 | /* need to set the modesets up here for use later */ |
||
1219 | /* fill out the connector<->crtc mappings into the modesets */ |
||
1220 | for (i = 0; i < fb_helper->crtc_count; i++) { |
||
1221 | modeset = &fb_helper->crtc_info[i].mode_set; |
||
1222 | modeset->num_connectors = 0; |
||
3480 | Serge | 1223 | modeset->fb = NULL; |
1963 | serge | 1224 | } |
1225 | |||
1226 | for (i = 0; i < fb_helper->connector_count; i++) { |
||
1227 | struct drm_display_mode *mode = modes[i]; |
||
1228 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
||
1229 | modeset = &fb_crtc->mode_set; |
||
1230 | |||
1231 | if (mode && fb_crtc) { |
||
1232 | DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", |
||
1233 | mode->name, fb_crtc->mode_set.crtc->base.id); |
||
1234 | fb_crtc->desired_mode = mode; |
||
1235 | if (modeset->mode) |
||
1236 | drm_mode_destroy(dev, modeset->mode); |
||
1237 | modeset->mode = drm_mode_duplicate(dev, |
||
1238 | fb_crtc->desired_mode); |
||
1239 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
||
3480 | Serge | 1240 | modeset->fb = fb_helper->fb; |
1963 | serge | 1241 | } |
1242 | } |
||
1243 | |||
3480 | Serge | 1244 | /* Clear out any old modes if there are no more connected outputs. */ |
1245 | for (i = 0; i < fb_helper->crtc_count; i++) { |
||
1246 | modeset = &fb_helper->crtc_info[i].mode_set; |
||
1247 | if (modeset->num_connectors == 0) { |
||
1248 | BUG_ON(modeset->fb); |
||
1249 | BUG_ON(modeset->num_connectors); |
||
1250 | if (modeset->mode) |
||
1251 | drm_mode_destroy(dev, modeset->mode); |
||
1252 | modeset->mode = NULL; |
||
1253 | } |
||
1254 | } |
||
3192 | Serge | 1255 | out: |
1963 | serge | 1256 | kfree(crtcs); |
1257 | kfree(modes); |
||
1258 | kfree(enabled); |
||
1259 | } |
||
1260 | |||
1261 | /** |
||
3480 | Serge | 1262 | * drm_fb_helper_initial_config - setup a sane initial connector configuration |
3192 | Serge | 1263 | * @fb_helper: fb_helper device struct |
1264 | * @bpp_sel: bpp value to use for the framebuffer configuration |
||
1963 | serge | 1265 | * |
3192 | Serge | 1266 | * Scans the CRTCs and connectors and tries to put together an initial setup. |
1963 | serge | 1267 | * At the moment, this is a cloned configuration across all heads with |
1268 | * a new framebuffer object as the backing store. |
||
1269 | * |
||
3480 | Serge | 1270 | * Note that this also registers the fbdev and so allows userspace to call into |
1271 | * the driver through the fbdev interfaces. |
||
1272 | * |
||
1273 | * This function will call down into the ->fb_probe callback to let |
||
1274 | * the driver allocate and initialize the fbdev info structure and the drm |
||
1275 | * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and |
||
1276 | * drm_fb_helper_fill_fix() are provided as helpers to setup simple default |
||
1277 | * values for the fbdev info structure. |
||
1278 | * |
||
1963 | serge | 1279 | * RETURNS: |
1280 | * Zero if everything went ok, nonzero otherwise. |
||
1281 | */ |
||
1282 | bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) |
||
1283 | { |
||
1284 | struct drm_device *dev = fb_helper->dev; |
||
1285 | int count = 0; |
||
1286 | |||
1287 | // drm_fb_helper_parse_command_line(fb_helper); |
||
1288 | |||
1289 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
||
1290 | dev->mode_config.max_width, |
||
1291 | dev->mode_config.max_height); |
||
1292 | /* |
||
1293 | * we shouldn't end up with no modes here. |
||
1294 | */ |
||
3192 | Serge | 1295 | if (count == 0) |
1296 | dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); |
||
1297 | |||
1298 | drm_setup_crtcs(fb_helper); |
||
1299 | |||
3243 | Serge | 1300 | return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); |
3192 | Serge | 1301 | } |
1302 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
||
1303 | |||
1304 | /** |
||
1305 | * drm_fb_helper_hotplug_event - respond to a hotplug notification by |
||
1306 | * probing all the outputs attached to the fb |
||
1307 | * @fb_helper: the drm_fb_helper |
||
1308 | * |
||
1309 | * Scan the connectors attached to the fb_helper and try to put together a |
||
1310 | * setup after *notification of a change in output configuration. |
||
1311 | * |
||
3480 | Serge | 1312 | * Called at runtime, takes the mode config locks to be able to check/change the |
1313 | * modeset configuration. Must be run from process context (which usually means |
||
1314 | * either the output polling work or a work item launched from the driver's |
||
1315 | * hotplug interrupt). |
||
1316 | * |
||
1317 | * Note that the driver must ensure that this is only called _after_ the fb has |
||
1318 | * been fully set up, i.e. after the call to drm_fb_helper_initial_config. |
||
1319 | * |
||
3192 | Serge | 1320 | * RETURNS: |
1321 | * 0 on success and a non-zero error code otherwise. |
||
1322 | */ |
||
1323 | int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) |
||
1324 | { |
||
1325 | struct drm_device *dev = fb_helper->dev; |
||
4560 | Serge | 1326 | u32 max_width, max_height; |
3192 | Serge | 1327 | |
1328 | if (!fb_helper->fb) |
||
1329 | return 0; |
||
1330 | |||
3480 | Serge | 1331 | mutex_lock(&fb_helper->dev->mode_config.mutex); |
1332 | if (!drm_fb_helper_is_bound(fb_helper)) { |
||
3192 | Serge | 1333 | fb_helper->delayed_hotplug = true; |
3480 | Serge | 1334 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
3192 | Serge | 1335 | return 0; |
1336 | } |
||
1337 | DRM_DEBUG_KMS("\n"); |
||
1338 | |||
1339 | max_width = fb_helper->fb->width; |
||
1340 | max_height = fb_helper->fb->height; |
||
1341 | |||
4560 | Serge | 1342 | drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); |
3480 | Serge | 1343 | mutex_unlock(&fb_helper->dev->mode_config.mutex); |
1344 | |||
1345 | drm_modeset_lock_all(dev); |
||
1963 | serge | 1346 | drm_setup_crtcs(fb_helper); |
3480 | Serge | 1347 | drm_modeset_unlock_all(dev); |
1348 | drm_fb_helper_set_par(fb_helper->fbdev); |
||
1963 | serge | 1349 | |
3480 | Serge | 1350 | return 0; |
1963 | serge | 1351 | } |
3192 | Serge | 1352 | EXPORT_SYMBOL(drm_fb_helper_hotplug_event); |
1353 | |||
4560 | Serge | 1354 | /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) |
1355 | * but the module doesn't depend on any fb console symbols. At least |
||
1356 | * attempt to load fbcon to avoid leaving the system without a usable console. |
||
1357 | */ |
||
1358 | #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) |
||
1359 | static int __init drm_fb_helper_modinit(void) |
||
1360 | { |
||
1361 | const char *name = "fbcon"; |
||
1362 | struct module *fbcon; |
||
3192 | Serge | 1363 | |
4560 | Serge | 1364 | mutex_lock(&module_mutex); |
1365 | fbcon = find_module(name); |
||
1366 | mutex_unlock(&module_mutex); |
||
3192 | Serge | 1367 | |
4560 | Serge | 1368 | if (!fbcon) |
1369 | request_module_nowait(name); |
||
1370 | return 0; |
||
1371 | } |
||
1372 | |||
1373 | module_init(drm_fb_helper_modinit); |
||
1374 | #endif>>>->>>><>>>>>>=>>>>>>>>>>>>>>>>>>><>=><=>><>><>><>><>>>>>>>>> |