Deleted Added
full compact
drm_fb_helper.c (248061) drm_fb_helper.c (259016)
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 */
30
31#include <sys/cdefs.h>
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 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/drm2/drm_fb_helper.c 248061 2013-03-08 18:15:07Z dumbbell $");
32__FBSDID("$FreeBSD: head/sys/dev/drm2/drm_fb_helper.c 259016 2013-12-05 22:38:53Z ray $");
33
34#include <dev/drm2/drmP.h>
35#include <dev/drm2/drm_crtc.h>
36#include <dev/drm2/drm_fb_helper.h>
37#include <dev/drm2/drm_crtc_helper.h>
38
33
34#include <dev/drm2/drmP.h>
35#include <dev/drm2/drm_crtc.h>
36#include <dev/drm2/drm_fb_helper.h>
37#include <dev/drm2/drm_crtc_helper.h>
38
39#if defined(__FreeBSD__)
40struct vt_kms_softc {
41 struct drm_fb_helper *fb_helper;
42 struct task fb_mode_task;
43};
44
45static fb_enter_t vt_kms_postswitch;
46static void vt_restore_fbdev_mode(void *, int);
47
48/* Call restore out of vt(9) locks. */
49static void
50vt_restore_fbdev_mode(void *arg, int pending)
51{
52 struct drm_fb_helper *fb_helper;
53 struct vt_kms_softc *sc;
54
55 sc = (struct vt_kms_softc *)arg;
56 fb_helper = sc->fb_helper;
57 sx_xlock(&fb_helper->dev->mode_config.mutex);
58 drm_fb_helper_restore_fbdev_mode(fb_helper);
59 sx_xunlock(&fb_helper->dev->mode_config.mutex);
60}
61
62static int
63vt_kms_postswitch(void *arg)
64{
65 struct vt_kms_softc *sc;
66
67 sc = (struct vt_kms_softc *)arg;
68 taskqueue_enqueue_fast(taskqueue_thread, &sc->fb_mode_task);
69
70 return (0);
71}
72#endif
73
39static DRM_LIST_HEAD(kernel_fb_helper_list);
40
41/* simple single crtc case helper function */
42int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
43{
44 struct drm_device *dev = fb_helper->dev;
45 struct drm_connector *connector;
46
47 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
48 struct drm_fb_helper_connector *fb_helper_connector;
49
50 fb_helper_connector = malloc(
51 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
52 M_WAITOK | M_ZERO);
53
54 fb_helper_connector->connector = connector;
55 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
56 }
57 return 0;
58}
59
60const char *fb_mode_option;
61
62/**
63 * drm_fb_helper_connector_parse_command_line - parse command line for connector
64 * @connector - connector to parse line for
65 * @mode_option - per connector mode option
66 *
67 * This parses the connector specific then generic command lines for
68 * modes and options to configure the connector.
69 *
70 * This uses the same parameters as the fb modedb.c, except for extra
71 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
72 *
73 * enable/enable Digital/disable bit at the end
74 */
75static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
76 const char *mode_option)
77{
78 const char *name;
79 unsigned int namelen;
80 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
81 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
82 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
83 int i;
84 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
85 struct drm_fb_helper_cmdline_mode *cmdline_mode;
86 struct drm_connector *connector;
87
88 if (!fb_helper_conn)
89 return false;
90 connector = fb_helper_conn->connector;
91
92 cmdline_mode = &fb_helper_conn->cmdline_mode;
93 if (!mode_option)
94 mode_option = fb_mode_option;
95
96 if (!mode_option) {
97 cmdline_mode->specified = false;
98 return false;
99 }
100
101 name = mode_option;
102 namelen = strlen(name);
103 for (i = namelen-1; i >= 0; i--) {
104 switch (name[i]) {
105 case '@':
106 namelen = i;
107 if (!refresh_specified && !bpp_specified &&
108 !yres_specified) {
109 refresh = strtol(&name[i+1], NULL, 10);
110 refresh_specified = 1;
111 if (cvt || rb)
112 cvt = 0;
113 } else
114 goto done;
115 break;
116 case '-':
117 namelen = i;
118 if (!bpp_specified && !yres_specified) {
119 bpp = strtol(&name[i+1], NULL, 10);
120 bpp_specified = 1;
121 if (cvt || rb)
122 cvt = 0;
123 } else
124 goto done;
125 break;
126 case 'x':
127 if (!yres_specified) {
128 yres = strtol(&name[i+1], NULL, 10);
129 yres_specified = 1;
130 } else
131 goto done;
132 case '0' ... '9':
133 break;
134 case 'M':
135 if (!yres_specified)
136 cvt = 1;
137 break;
138 case 'R':
139 if (cvt)
140 rb = 1;
141 break;
142 case 'm':
143 if (!cvt)
144 margins = 1;
145 break;
146 case 'i':
147 if (!cvt)
148 interlace = 1;
149 break;
150 case 'e':
151 force = DRM_FORCE_ON;
152 break;
153 case 'D':
154 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
155 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
156 force = DRM_FORCE_ON;
157 else
158 force = DRM_FORCE_ON_DIGITAL;
159 break;
160 case 'd':
161 force = DRM_FORCE_OFF;
162 break;
163 default:
164 goto done;
165 }
166 }
167 if (i < 0 && yres_specified) {
168 xres = strtol(name, NULL, 10);
169 res_specified = 1;
170 }
171done:
172
173 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
174 drm_get_connector_name(connector), xres, yres,
175 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
176 "", (margins) ? " with margins" : "", (interlace) ?
177 " interlaced" : "");
178
179 if (force) {
180 const char *s;
181 switch (force) {
182 case DRM_FORCE_OFF: s = "OFF"; break;
183 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
184 default:
185 case DRM_FORCE_ON: s = "ON"; break;
186 }
187
188 DRM_INFO("forcing %s connector %s\n",
189 drm_get_connector_name(connector), s);
190 connector->force = force;
191 }
192
193 if (res_specified) {
194 cmdline_mode->specified = true;
195 cmdline_mode->xres = xres;
196 cmdline_mode->yres = yres;
197 }
198
199 if (refresh_specified) {
200 cmdline_mode->refresh_specified = true;
201 cmdline_mode->refresh = refresh;
202 }
203
204 if (bpp_specified) {
205 cmdline_mode->bpp_specified = true;
206 cmdline_mode->bpp = bpp;
207 }
208 cmdline_mode->rb = rb ? true : false;
209 cmdline_mode->cvt = cvt ? true : false;
210 cmdline_mode->interlace = interlace ? true : false;
211
212 return true;
213}
214
215static int
216fb_get_options(const char *connector_name, char **option)
217{
218
74static DRM_LIST_HEAD(kernel_fb_helper_list);
75
76/* simple single crtc case helper function */
77int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
78{
79 struct drm_device *dev = fb_helper->dev;
80 struct drm_connector *connector;
81
82 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
83 struct drm_fb_helper_connector *fb_helper_connector;
84
85 fb_helper_connector = malloc(
86 sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
87 M_WAITOK | M_ZERO);
88
89 fb_helper_connector->connector = connector;
90 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
91 }
92 return 0;
93}
94
95const char *fb_mode_option;
96
97/**
98 * drm_fb_helper_connector_parse_command_line - parse command line for connector
99 * @connector - connector to parse line for
100 * @mode_option - per connector mode option
101 *
102 * This parses the connector specific then generic command lines for
103 * modes and options to configure the connector.
104 *
105 * This uses the same parameters as the fb modedb.c, except for extra
106 * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
107 *
108 * enable/enable Digital/disable bit at the end
109 */
110static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
111 const char *mode_option)
112{
113 const char *name;
114 unsigned int namelen;
115 int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
116 unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
117 int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
118 int i;
119 enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
120 struct drm_fb_helper_cmdline_mode *cmdline_mode;
121 struct drm_connector *connector;
122
123 if (!fb_helper_conn)
124 return false;
125 connector = fb_helper_conn->connector;
126
127 cmdline_mode = &fb_helper_conn->cmdline_mode;
128 if (!mode_option)
129 mode_option = fb_mode_option;
130
131 if (!mode_option) {
132 cmdline_mode->specified = false;
133 return false;
134 }
135
136 name = mode_option;
137 namelen = strlen(name);
138 for (i = namelen-1; i >= 0; i--) {
139 switch (name[i]) {
140 case '@':
141 namelen = i;
142 if (!refresh_specified && !bpp_specified &&
143 !yres_specified) {
144 refresh = strtol(&name[i+1], NULL, 10);
145 refresh_specified = 1;
146 if (cvt || rb)
147 cvt = 0;
148 } else
149 goto done;
150 break;
151 case '-':
152 namelen = i;
153 if (!bpp_specified && !yres_specified) {
154 bpp = strtol(&name[i+1], NULL, 10);
155 bpp_specified = 1;
156 if (cvt || rb)
157 cvt = 0;
158 } else
159 goto done;
160 break;
161 case 'x':
162 if (!yres_specified) {
163 yres = strtol(&name[i+1], NULL, 10);
164 yres_specified = 1;
165 } else
166 goto done;
167 case '0' ... '9':
168 break;
169 case 'M':
170 if (!yres_specified)
171 cvt = 1;
172 break;
173 case 'R':
174 if (cvt)
175 rb = 1;
176 break;
177 case 'm':
178 if (!cvt)
179 margins = 1;
180 break;
181 case 'i':
182 if (!cvt)
183 interlace = 1;
184 break;
185 case 'e':
186 force = DRM_FORCE_ON;
187 break;
188 case 'D':
189 if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
190 (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
191 force = DRM_FORCE_ON;
192 else
193 force = DRM_FORCE_ON_DIGITAL;
194 break;
195 case 'd':
196 force = DRM_FORCE_OFF;
197 break;
198 default:
199 goto done;
200 }
201 }
202 if (i < 0 && yres_specified) {
203 xres = strtol(name, NULL, 10);
204 res_specified = 1;
205 }
206done:
207
208 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
209 drm_get_connector_name(connector), xres, yres,
210 (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
211 "", (margins) ? " with margins" : "", (interlace) ?
212 " interlaced" : "");
213
214 if (force) {
215 const char *s;
216 switch (force) {
217 case DRM_FORCE_OFF: s = "OFF"; break;
218 case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
219 default:
220 case DRM_FORCE_ON: s = "ON"; break;
221 }
222
223 DRM_INFO("forcing %s connector %s\n",
224 drm_get_connector_name(connector), s);
225 connector->force = force;
226 }
227
228 if (res_specified) {
229 cmdline_mode->specified = true;
230 cmdline_mode->xres = xres;
231 cmdline_mode->yres = yres;
232 }
233
234 if (refresh_specified) {
235 cmdline_mode->refresh_specified = true;
236 cmdline_mode->refresh = refresh;
237 }
238
239 if (bpp_specified) {
240 cmdline_mode->bpp_specified = true;
241 cmdline_mode->bpp = bpp;
242 }
243 cmdline_mode->rb = rb ? true : false;
244 cmdline_mode->cvt = cvt ? true : false;
245 cmdline_mode->interlace = interlace ? true : false;
246
247 return true;
248}
249
250static int
251fb_get_options(const char *connector_name, char **option)
252{
253
254 /*
255 * TODO: store mode options pointer in ${option} for connector with
256 * name ${connector_name}
257 */
219 return (1);
220}
221
222static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
223{
224 struct drm_fb_helper_connector *fb_helper_conn;
225 int i;
226
227 for (i = 0; i < fb_helper->connector_count; i++) {
228 char *option = NULL;
229
230 fb_helper_conn = fb_helper->connector_info[i];
231
232 /* do something on return - turn off connector maybe */
233 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
234 continue;
235
236 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
237 }
238 return 0;
239}
240
241#if 0
242static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
243{
244 uint16_t *r_base, *g_base, *b_base;
245 int i;
246
247 r_base = crtc->gamma_store;
248 g_base = r_base + crtc->gamma_size;
249 b_base = g_base + crtc->gamma_size;
250
251 for (i = 0; i < crtc->gamma_size; i++)
252 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
253}
254
255static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
256{
257 uint16_t *r_base, *g_base, *b_base;
258
259 r_base = crtc->gamma_store;
260 g_base = r_base + crtc->gamma_size;
261 b_base = g_base + crtc->gamma_size;
262
263 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
264}
265#endif
266
267#if 0
268int drm_fb_helper_debug_enter(struct fb_info *info)
269{
270 struct drm_fb_helper *helper = info->par;
271 struct drm_crtc_helper_funcs *funcs;
272 int i;
273
274 if (list_empty(&kernel_fb_helper_list))
275 return false;
276
277 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
278 for (i = 0; i < helper->crtc_count; i++) {
279 struct drm_mode_set *mode_set =
280 &helper->crtc_info[i].mode_set;
281
282 if (!mode_set->crtc->enabled)
283 continue;
284
285 funcs = mode_set->crtc->helper_private;
286 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
287 funcs->mode_set_base_atomic(mode_set->crtc,
288 mode_set->fb,
289 mode_set->x,
290 mode_set->y,
291 ENTER_ATOMIC_MODE_SET);
292 }
293 }
294
295 return 0;
296}
297#endif
298
299#if 0
300/* Find the real fb for a given fb helper CRTC */
301static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
302{
303 struct drm_device *dev = crtc->dev;
304 struct drm_crtc *c;
305
306 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
307 if (crtc->base.id == c->base.id)
308 return c->fb;
309 }
310
311 return NULL;
312}
313#endif
314
315#if 0
316int drm_fb_helper_debug_leave(struct fb_info *info)
317{
318 struct drm_fb_helper *helper = info->par;
319 struct drm_crtc *crtc;
320 struct drm_crtc_helper_funcs *funcs;
321 struct drm_framebuffer *fb;
322 int i;
323
324 for (i = 0; i < helper->crtc_count; i++) {
325 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
326 crtc = mode_set->crtc;
327 funcs = crtc->helper_private;
328 fb = drm_mode_config_fb(crtc);
329
330 if (!crtc->enabled)
331 continue;
332
333 if (!fb) {
334 DRM_ERROR("no fb to restore??\n");
335 continue;
336 }
337
338 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
339 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
340 crtc->y, LEAVE_ATOMIC_MODE_SET);
341 }
342
343 return 0;
344}
345#endif
346
347bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
348{
349 bool error = false;
350 int i, ret;
351 for (i = 0; i < fb_helper->crtc_count; i++) {
352 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
353 ret = drm_crtc_helper_set_config(mode_set);
354 if (ret)
355 error = true;
356 }
357 return error;
358}
359
360#if 0
361bool drm_fb_helper_force_kernel_mode(void)
362{
363 bool ret, error = false;
364 struct drm_fb_helper *helper;
365
366 if (list_empty(&kernel_fb_helper_list))
367 return false;
368
369 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
370 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
371 continue;
372
373 ret = drm_fb_helper_restore_fbdev_mode(helper);
374 if (ret)
375 error = true;
376 }
377 return error;
378}
379#endif
380
381#if 0
382int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
383 void *panic_str)
384{
385 printf("panic occurred, switching back to text console\n");
386 return drm_fb_helper_force_kernel_mode();
387 return 0;
388}
389
390static struct notifier_block paniced = {
391 .notifier_call = drm_fb_helper_panic,
392};
393
394/**
395 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
396 *
397 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
398 */
399void drm_fb_helper_restore(void)
400{
401 bool ret;
402 ret = drm_fb_helper_force_kernel_mode();
403 if (ret == true)
404 DRM_ERROR("Failed to restore crtc configuration\n");
405}
406
407#ifdef CONFIG_MAGIC_SYSRQ
408static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
409{
410 drm_fb_helper_restore();
411}
412static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
413
414static void drm_fb_helper_sysrq(int dummy1)
415{
416 schedule_work(&drm_fb_helper_restore_work);
417}
418
419static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
420 .handler = drm_fb_helper_sysrq,
421 .help_msg = "force-fb(V)",
422 .action_msg = "Restore framebuffer console",
423};
424#else
425static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
426#endif
427#endif
428
429#if 0
430static void drm_fb_helper_on(struct fb_info *info)
431{
432 struct drm_fb_helper *fb_helper = info->par;
433 struct drm_device *dev = fb_helper->dev;
434 struct drm_crtc *crtc;
435 struct drm_crtc_helper_funcs *crtc_funcs;
436 struct drm_connector *connector;
437 struct drm_encoder *encoder;
438 int i, j;
439
440 /*
441 * For each CRTC in this fb, turn the crtc on then,
442 * find all associated encoders and turn them on.
443 */
444 sx_xlock(&dev->mode_config.mutex);
445 for (i = 0; i < fb_helper->crtc_count; i++) {
446 crtc = fb_helper->crtc_info[i].mode_set.crtc;
447 crtc_funcs = crtc->helper_private;
448
449 if (!crtc->enabled)
450 continue;
451
452 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
453
454 /* Walk the connectors & encoders on this fb turning them on */
455 for (j = 0; j < fb_helper->connector_count; j++) {
456 connector = fb_helper->connector_info[j]->connector;
457 connector->dpms = DRM_MODE_DPMS_ON;
458 drm_connector_property_set_value(connector,
459 dev->mode_config.dpms_property,
460 DRM_MODE_DPMS_ON);
461 }
462 /* Found a CRTC on this fb, now find encoders */
463 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
464 if (encoder->crtc == crtc) {
465 struct drm_encoder_helper_funcs *encoder_funcs;
466
467 encoder_funcs = encoder->helper_private;
468 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
469 }
470 }
471 }
472 sx_xunlock(&dev->mode_config.mutex);
473}
474#endif
475
476#if 0
477static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
478{
479 struct drm_fb_helper *fb_helper = info->par;
480 struct drm_device *dev = fb_helper->dev;
481 struct drm_crtc *crtc;
482 struct drm_crtc_helper_funcs *crtc_funcs;
483 struct drm_connector *connector;
484 struct drm_encoder *encoder;
485 int i, j;
486
487 /*
488 * For each CRTC in this fb, find all associated encoders
489 * and turn them off, then turn off the CRTC.
490 */
491 sx_xlock(&dev->mode_config.mutex);
492 for (i = 0; i < fb_helper->crtc_count; i++) {
493 crtc = fb_helper->crtc_info[i].mode_set.crtc;
494 crtc_funcs = crtc->helper_private;
495
496 if (!crtc->enabled)
497 continue;
498
499 /* Walk the connectors on this fb and mark them off */
500 for (j = 0; j < fb_helper->connector_count; j++) {
501 connector = fb_helper->connector_info[j]->connector;
502 connector->dpms = dpms_mode;
503 drm_connector_property_set_value(connector,
504 dev->mode_config.dpms_property,
505 dpms_mode);
506 }
507 /* Found a CRTC on this fb, now find encoders */
508 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
509 if (encoder->crtc == crtc) {
510 struct drm_encoder_helper_funcs *encoder_funcs;
511
512 encoder_funcs = encoder->helper_private;
513 encoder_funcs->dpms(encoder, dpms_mode);
514 }
515 }
516 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
517 }
518 sx_xunlock(&dev->mode_config.mutex);
519}
520#endif
521
522#if 0
523int drm_fb_helper_blank(int blank, struct fb_info *info)
524{
525 switch (blank) {
526 /* Display: On; HSync: On, VSync: On */
527 case FB_BLANK_UNBLANK:
528 drm_fb_helper_on(info);
529 break;
530 /* Display: Off; HSync: On, VSync: On */
531 case FB_BLANK_NORMAL:
532 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
533 break;
534 /* Display: Off; HSync: Off, VSync: On */
535 case FB_BLANK_HSYNC_SUSPEND:
536 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
537 break;
538 /* Display: Off; HSync: On, VSync: Off */
539 case FB_BLANK_VSYNC_SUSPEND:
540 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
541 break;
542 /* Display: Off; HSync: Off, VSync: Off */
543 case FB_BLANK_POWERDOWN:
544 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
545 break;
546 }
547 return 0;
548}
549#endif
550
551static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
552{
553 int i;
554
555 for (i = 0; i < helper->connector_count; i++)
556 free(helper->connector_info[i], DRM_MEM_KMS);
557 free(helper->connector_info, DRM_MEM_KMS);
558 for (i = 0; i < helper->crtc_count; i++) {
559 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
560 if (helper->crtc_info[i].mode_set.mode)
561 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
562 }
563 free(helper->crtc_info, DRM_MEM_KMS);
564}
565
566int drm_fb_helper_init(struct drm_device *dev,
567 struct drm_fb_helper *fb_helper,
568 int crtc_count, int max_conn_count)
569{
570 struct drm_crtc *crtc;
571 int i;
572
573 fb_helper->dev = dev;
574
575 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
576
577 fb_helper->crtc_info = malloc(crtc_count *
578 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
579
580 fb_helper->crtc_count = crtc_count;
581 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
582 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
583 M_WAITOK | M_ZERO);
584 fb_helper->connector_count = 0;
585
586 for (i = 0; i < crtc_count; i++) {
587 fb_helper->crtc_info[i].mode_set.connectors =
588 malloc(max_conn_count * sizeof(struct drm_connector *),
589 DRM_MEM_KMS, M_WAITOK | M_ZERO);
590
591 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
592 }
593
594 i = 0;
595 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
596 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
597 fb_helper->crtc_info[i].mode_set.crtc = crtc;
598 i++;
599 }
600 fb_helper->conn_limit = max_conn_count;
601 return 0;
602}
603
604void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
605{
606 if (!list_empty(&fb_helper->kernel_fb_list)) {
607 list_del(&fb_helper->kernel_fb_list);
608 if (list_empty(&kernel_fb_helper_list)) {
609#if 0
610 printk(KERN_INFO "drm: unregistered panic notifier\n");
611 atomic_notifier_chain_unregister(&panic_notifier_list,
612 &paniced);
613 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
614#endif
615 }
616 }
617
618 drm_fb_helper_crtc_free(fb_helper);
619
620}
621
622#if 0
623static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
624 u16 blue, u16 regno, struct fb_info *info)
625{
626 struct drm_fb_helper *fb_helper = info->par;
627 struct drm_framebuffer *fb = fb_helper->fb;
628 int pindex;
629
630 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
631 u32 *palette;
632 u32 value;
633 /* place color in psuedopalette */
634 if (regno > 16)
635 return -EINVAL;
636 palette = (u32 *)info->pseudo_palette;
637 red >>= (16 - info->var.red.length);
638 green >>= (16 - info->var.green.length);
639 blue >>= (16 - info->var.blue.length);
640 value = (red << info->var.red.offset) |
641 (green << info->var.green.offset) |
642 (blue << info->var.blue.offset);
643 if (info->var.transp.length > 0) {
644 u32 mask = (1 << info->var.transp.length) - 1;
645 mask <<= info->var.transp.offset;
646 value |= mask;
647 }
648 palette[regno] = value;
649 return 0;
650 }
651
652 pindex = regno;
653
654 if (fb->bits_per_pixel == 16) {
655 pindex = regno << 3;
656
657 if (fb->depth == 16 && regno > 63)
658 return -EINVAL;
659 if (fb->depth == 15 && regno > 31)
660 return -EINVAL;
661
662 if (fb->depth == 16) {
663 u16 r, g, b;
664 int i;
665 if (regno < 32) {
666 for (i = 0; i < 8; i++)
667 fb_helper->funcs->gamma_set(crtc, red,
668 green, blue, pindex + i);
669 }
670
671 fb_helper->funcs->gamma_get(crtc, &r,
672 &g, &b,
673 pindex >> 1);
674
675 for (i = 0; i < 4; i++)
676 fb_helper->funcs->gamma_set(crtc, r,
677 green, b,
678 (pindex >> 1) + i);
679 }
680 }
681
682 if (fb->depth != 16)
683 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
684 return 0;
685}
686#endif
687
688#if 0
689int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
690{
691 struct drm_fb_helper *fb_helper = info->par;
692 struct drm_crtc_helper_funcs *crtc_funcs;
693 u16 *red, *green, *blue, *transp;
694 struct drm_crtc *crtc;
695 int i, j, rc = 0;
696 int start;
697
698 for (i = 0; i < fb_helper->crtc_count; i++) {
699 crtc = fb_helper->crtc_info[i].mode_set.crtc;
700 crtc_funcs = crtc->helper_private;
701
702 red = cmap->red;
703 green = cmap->green;
704 blue = cmap->blue;
705 transp = cmap->transp;
706 start = cmap->start;
707
708 for (j = 0; j < cmap->len; j++) {
709 u16 hred, hgreen, hblue, htransp = 0xffff;
710
711 hred = *red++;
712 hgreen = *green++;
713 hblue = *blue++;
714
715 if (transp)
716 htransp = *transp++;
717
718 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
719 if (rc)
720 return rc;
721 }
722 crtc_funcs->load_lut(crtc);
723 }
724 return rc;
725}
726#endif
727
728#if 0
729int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
730 struct fb_info *info)
731{
732 struct drm_fb_helper *fb_helper = info->par;
733 struct drm_framebuffer *fb = fb_helper->fb;
734 int depth;
735
736 if (var->pixclock != 0 || in_dbg_master())
737 return -EINVAL;
738
739 /* Need to resize the fb object !!! */
740 if (var->bits_per_pixel > fb->bits_per_pixel ||
741 var->xres > fb->width || var->yres > fb->height ||
742 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
743 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
744 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
745 var->xres, var->yres, var->bits_per_pixel,
746 var->xres_virtual, var->yres_virtual,
747 fb->width, fb->height, fb->bits_per_pixel);
748 return -EINVAL;
749 }
750
751 switch (var->bits_per_pixel) {
752 case 16:
753 depth = (var->green.length == 6) ? 16 : 15;
754 break;
755 case 32:
756 depth = (var->transp.length > 0) ? 32 : 24;
757 break;
758 default:
759 depth = var->bits_per_pixel;
760 break;
761 }
762
763 switch (depth) {
764 case 8:
765 var->red.offset = 0;
766 var->green.offset = 0;
767 var->blue.offset = 0;
768 var->red.length = 8;
769 var->green.length = 8;
770 var->blue.length = 8;
771 var->transp.length = 0;
772 var->transp.offset = 0;
773 break;
774 case 15:
775 var->red.offset = 10;
776 var->green.offset = 5;
777 var->blue.offset = 0;
778 var->red.length = 5;
779 var->green.length = 5;
780 var->blue.length = 5;
781 var->transp.length = 1;
782 var->transp.offset = 15;
783 break;
784 case 16:
785 var->red.offset = 11;
786 var->green.offset = 5;
787 var->blue.offset = 0;
788 var->red.length = 5;
789 var->green.length = 6;
790 var->blue.length = 5;
791 var->transp.length = 0;
792 var->transp.offset = 0;
793 break;
794 case 24:
795 var->red.offset = 16;
796 var->green.offset = 8;
797 var->blue.offset = 0;
798 var->red.length = 8;
799 var->green.length = 8;
800 var->blue.length = 8;
801 var->transp.length = 0;
802 var->transp.offset = 0;
803 break;
804 case 32:
805 var->red.offset = 16;
806 var->green.offset = 8;
807 var->blue.offset = 0;
808 var->red.length = 8;
809 var->green.length = 8;
810 var->blue.length = 8;
811 var->transp.length = 8;
812 var->transp.offset = 24;
813 break;
814 default:
815 return -EINVAL;
816 }
817 return 0;
818}
819#endif
820
821#if 0
822/* this will let fbcon do the mode init */
823int drm_fb_helper_set_par(struct fb_info *info)
824{
825 struct drm_fb_helper *fb_helper = info->par;
826 struct drm_device *dev = fb_helper->dev;
827 struct fb_var_screeninfo *var = &info->var;
828 struct drm_crtc *crtc;
829 int ret;
830 int i;
831
832 if (var->pixclock != 0) {
833 DRM_ERROR("PIXEL CLOCK SET\n");
834 return -EINVAL;
835 }
836
837 mutex_lock(&dev->mode_config.mutex);
838 for (i = 0; i < fb_helper->crtc_count; i++) {
839 crtc = fb_helper->crtc_info[i].mode_set.crtc;
840 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
841 if (ret) {
842 mutex_unlock(&dev->mode_config.mutex);
843 return ret;
844 }
845 }
846 mutex_unlock(&dev->mode_config.mutex);
847
848 if (fb_helper->delayed_hotplug) {
849 fb_helper->delayed_hotplug = false;
850 drm_fb_helper_hotplug_event(fb_helper);
851 }
852 return 0;
853}
854#endif
855
856#if 0
857int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
858 struct fb_info *info)
859{
860 struct drm_fb_helper *fb_helper = info->par;
861 struct drm_device *dev = fb_helper->dev;
862 struct drm_mode_set *modeset;
863 struct drm_crtc *crtc;
864 int ret = 0;
865 int i;
866
867 mutex_lock(&dev->mode_config.mutex);
868 for (i = 0; i < fb_helper->crtc_count; i++) {
869 crtc = fb_helper->crtc_info[i].mode_set.crtc;
870
871 modeset = &fb_helper->crtc_info[i].mode_set;
872
873 modeset->x = var->xoffset;
874 modeset->y = var->yoffset;
875
876 if (modeset->num_connectors) {
877 ret = crtc->funcs->set_config(modeset);
878 if (!ret) {
879 info->var.xoffset = var->xoffset;
880 info->var.yoffset = var->yoffset;
881 }
882 }
883 }
884 mutex_unlock(&dev->mode_config.mutex);
885 return ret;
886}
887#endif
888
889int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
890 int preferred_bpp)
891{
892 int new_fb = 0;
893 int crtc_count = 0;
894 int i;
258 return (1);
259}
260
261static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
262{
263 struct drm_fb_helper_connector *fb_helper_conn;
264 int i;
265
266 for (i = 0; i < fb_helper->connector_count; i++) {
267 char *option = NULL;
268
269 fb_helper_conn = fb_helper->connector_info[i];
270
271 /* do something on return - turn off connector maybe */
272 if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
273 continue;
274
275 drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
276 }
277 return 0;
278}
279
280#if 0
281static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
282{
283 uint16_t *r_base, *g_base, *b_base;
284 int i;
285
286 r_base = crtc->gamma_store;
287 g_base = r_base + crtc->gamma_size;
288 b_base = g_base + crtc->gamma_size;
289
290 for (i = 0; i < crtc->gamma_size; i++)
291 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
292}
293
294static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
295{
296 uint16_t *r_base, *g_base, *b_base;
297
298 r_base = crtc->gamma_store;
299 g_base = r_base + crtc->gamma_size;
300 b_base = g_base + crtc->gamma_size;
301
302 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
303}
304#endif
305
306#if 0
307int drm_fb_helper_debug_enter(struct fb_info *info)
308{
309 struct drm_fb_helper *helper = info->par;
310 struct drm_crtc_helper_funcs *funcs;
311 int i;
312
313 if (list_empty(&kernel_fb_helper_list))
314 return false;
315
316 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
317 for (i = 0; i < helper->crtc_count; i++) {
318 struct drm_mode_set *mode_set =
319 &helper->crtc_info[i].mode_set;
320
321 if (!mode_set->crtc->enabled)
322 continue;
323
324 funcs = mode_set->crtc->helper_private;
325 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
326 funcs->mode_set_base_atomic(mode_set->crtc,
327 mode_set->fb,
328 mode_set->x,
329 mode_set->y,
330 ENTER_ATOMIC_MODE_SET);
331 }
332 }
333
334 return 0;
335}
336#endif
337
338#if 0
339/* Find the real fb for a given fb helper CRTC */
340static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
341{
342 struct drm_device *dev = crtc->dev;
343 struct drm_crtc *c;
344
345 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
346 if (crtc->base.id == c->base.id)
347 return c->fb;
348 }
349
350 return NULL;
351}
352#endif
353
354#if 0
355int drm_fb_helper_debug_leave(struct fb_info *info)
356{
357 struct drm_fb_helper *helper = info->par;
358 struct drm_crtc *crtc;
359 struct drm_crtc_helper_funcs *funcs;
360 struct drm_framebuffer *fb;
361 int i;
362
363 for (i = 0; i < helper->crtc_count; i++) {
364 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
365 crtc = mode_set->crtc;
366 funcs = crtc->helper_private;
367 fb = drm_mode_config_fb(crtc);
368
369 if (!crtc->enabled)
370 continue;
371
372 if (!fb) {
373 DRM_ERROR("no fb to restore??\n");
374 continue;
375 }
376
377 drm_fb_helper_restore_lut_atomic(mode_set->crtc);
378 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
379 crtc->y, LEAVE_ATOMIC_MODE_SET);
380 }
381
382 return 0;
383}
384#endif
385
386bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
387{
388 bool error = false;
389 int i, ret;
390 for (i = 0; i < fb_helper->crtc_count; i++) {
391 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
392 ret = drm_crtc_helper_set_config(mode_set);
393 if (ret)
394 error = true;
395 }
396 return error;
397}
398
399#if 0
400bool drm_fb_helper_force_kernel_mode(void)
401{
402 bool ret, error = false;
403 struct drm_fb_helper *helper;
404
405 if (list_empty(&kernel_fb_helper_list))
406 return false;
407
408 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
409 if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
410 continue;
411
412 ret = drm_fb_helper_restore_fbdev_mode(helper);
413 if (ret)
414 error = true;
415 }
416 return error;
417}
418#endif
419
420#if 0
421int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
422 void *panic_str)
423{
424 printf("panic occurred, switching back to text console\n");
425 return drm_fb_helper_force_kernel_mode();
426 return 0;
427}
428
429static struct notifier_block paniced = {
430 .notifier_call = drm_fb_helper_panic,
431};
432
433/**
434 * drm_fb_helper_restore - restore the framebuffer console (kernel) config
435 *
436 * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
437 */
438void drm_fb_helper_restore(void)
439{
440 bool ret;
441 ret = drm_fb_helper_force_kernel_mode();
442 if (ret == true)
443 DRM_ERROR("Failed to restore crtc configuration\n");
444}
445
446#ifdef CONFIG_MAGIC_SYSRQ
447static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
448{
449 drm_fb_helper_restore();
450}
451static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
452
453static void drm_fb_helper_sysrq(int dummy1)
454{
455 schedule_work(&drm_fb_helper_restore_work);
456}
457
458static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
459 .handler = drm_fb_helper_sysrq,
460 .help_msg = "force-fb(V)",
461 .action_msg = "Restore framebuffer console",
462};
463#else
464static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
465#endif
466#endif
467
468#if 0
469static void drm_fb_helper_on(struct fb_info *info)
470{
471 struct drm_fb_helper *fb_helper = info->par;
472 struct drm_device *dev = fb_helper->dev;
473 struct drm_crtc *crtc;
474 struct drm_crtc_helper_funcs *crtc_funcs;
475 struct drm_connector *connector;
476 struct drm_encoder *encoder;
477 int i, j;
478
479 /*
480 * For each CRTC in this fb, turn the crtc on then,
481 * find all associated encoders and turn them on.
482 */
483 sx_xlock(&dev->mode_config.mutex);
484 for (i = 0; i < fb_helper->crtc_count; i++) {
485 crtc = fb_helper->crtc_info[i].mode_set.crtc;
486 crtc_funcs = crtc->helper_private;
487
488 if (!crtc->enabled)
489 continue;
490
491 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
492
493 /* Walk the connectors & encoders on this fb turning them on */
494 for (j = 0; j < fb_helper->connector_count; j++) {
495 connector = fb_helper->connector_info[j]->connector;
496 connector->dpms = DRM_MODE_DPMS_ON;
497 drm_connector_property_set_value(connector,
498 dev->mode_config.dpms_property,
499 DRM_MODE_DPMS_ON);
500 }
501 /* Found a CRTC on this fb, now find encoders */
502 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
503 if (encoder->crtc == crtc) {
504 struct drm_encoder_helper_funcs *encoder_funcs;
505
506 encoder_funcs = encoder->helper_private;
507 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
508 }
509 }
510 }
511 sx_xunlock(&dev->mode_config.mutex);
512}
513#endif
514
515#if 0
516static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
517{
518 struct drm_fb_helper *fb_helper = info->par;
519 struct drm_device *dev = fb_helper->dev;
520 struct drm_crtc *crtc;
521 struct drm_crtc_helper_funcs *crtc_funcs;
522 struct drm_connector *connector;
523 struct drm_encoder *encoder;
524 int i, j;
525
526 /*
527 * For each CRTC in this fb, find all associated encoders
528 * and turn them off, then turn off the CRTC.
529 */
530 sx_xlock(&dev->mode_config.mutex);
531 for (i = 0; i < fb_helper->crtc_count; i++) {
532 crtc = fb_helper->crtc_info[i].mode_set.crtc;
533 crtc_funcs = crtc->helper_private;
534
535 if (!crtc->enabled)
536 continue;
537
538 /* Walk the connectors on this fb and mark them off */
539 for (j = 0; j < fb_helper->connector_count; j++) {
540 connector = fb_helper->connector_info[j]->connector;
541 connector->dpms = dpms_mode;
542 drm_connector_property_set_value(connector,
543 dev->mode_config.dpms_property,
544 dpms_mode);
545 }
546 /* Found a CRTC on this fb, now find encoders */
547 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
548 if (encoder->crtc == crtc) {
549 struct drm_encoder_helper_funcs *encoder_funcs;
550
551 encoder_funcs = encoder->helper_private;
552 encoder_funcs->dpms(encoder, dpms_mode);
553 }
554 }
555 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
556 }
557 sx_xunlock(&dev->mode_config.mutex);
558}
559#endif
560
561#if 0
562int drm_fb_helper_blank(int blank, struct fb_info *info)
563{
564 switch (blank) {
565 /* Display: On; HSync: On, VSync: On */
566 case FB_BLANK_UNBLANK:
567 drm_fb_helper_on(info);
568 break;
569 /* Display: Off; HSync: On, VSync: On */
570 case FB_BLANK_NORMAL:
571 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
572 break;
573 /* Display: Off; HSync: Off, VSync: On */
574 case FB_BLANK_HSYNC_SUSPEND:
575 drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
576 break;
577 /* Display: Off; HSync: On, VSync: Off */
578 case FB_BLANK_VSYNC_SUSPEND:
579 drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
580 break;
581 /* Display: Off; HSync: Off, VSync: Off */
582 case FB_BLANK_POWERDOWN:
583 drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
584 break;
585 }
586 return 0;
587}
588#endif
589
590static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
591{
592 int i;
593
594 for (i = 0; i < helper->connector_count; i++)
595 free(helper->connector_info[i], DRM_MEM_KMS);
596 free(helper->connector_info, DRM_MEM_KMS);
597 for (i = 0; i < helper->crtc_count; i++) {
598 free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
599 if (helper->crtc_info[i].mode_set.mode)
600 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
601 }
602 free(helper->crtc_info, DRM_MEM_KMS);
603}
604
605int drm_fb_helper_init(struct drm_device *dev,
606 struct drm_fb_helper *fb_helper,
607 int crtc_count, int max_conn_count)
608{
609 struct drm_crtc *crtc;
610 int i;
611
612 fb_helper->dev = dev;
613
614 INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
615
616 fb_helper->crtc_info = malloc(crtc_count *
617 sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
618
619 fb_helper->crtc_count = crtc_count;
620 fb_helper->connector_info = malloc(dev->mode_config.num_connector *
621 sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
622 M_WAITOK | M_ZERO);
623 fb_helper->connector_count = 0;
624
625 for (i = 0; i < crtc_count; i++) {
626 fb_helper->crtc_info[i].mode_set.connectors =
627 malloc(max_conn_count * sizeof(struct drm_connector *),
628 DRM_MEM_KMS, M_WAITOK | M_ZERO);
629
630 fb_helper->crtc_info[i].mode_set.num_connectors = 0;
631 }
632
633 i = 0;
634 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
635 fb_helper->crtc_info[i].crtc_id = crtc->base.id;
636 fb_helper->crtc_info[i].mode_set.crtc = crtc;
637 i++;
638 }
639 fb_helper->conn_limit = max_conn_count;
640 return 0;
641}
642
643void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
644{
645 if (!list_empty(&fb_helper->kernel_fb_list)) {
646 list_del(&fb_helper->kernel_fb_list);
647 if (list_empty(&kernel_fb_helper_list)) {
648#if 0
649 printk(KERN_INFO "drm: unregistered panic notifier\n");
650 atomic_notifier_chain_unregister(&panic_notifier_list,
651 &paniced);
652 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
653#endif
654 }
655 }
656
657 drm_fb_helper_crtc_free(fb_helper);
658
659}
660
661#if 0
662static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
663 u16 blue, u16 regno, struct fb_info *info)
664{
665 struct drm_fb_helper *fb_helper = info->par;
666 struct drm_framebuffer *fb = fb_helper->fb;
667 int pindex;
668
669 if (info->fix.visual == FB_VISUAL_trueCOLOR) {
670 u32 *palette;
671 u32 value;
672 /* place color in psuedopalette */
673 if (regno > 16)
674 return -EINVAL;
675 palette = (u32 *)info->pseudo_palette;
676 red >>= (16 - info->var.red.length);
677 green >>= (16 - info->var.green.length);
678 blue >>= (16 - info->var.blue.length);
679 value = (red << info->var.red.offset) |
680 (green << info->var.green.offset) |
681 (blue << info->var.blue.offset);
682 if (info->var.transp.length > 0) {
683 u32 mask = (1 << info->var.transp.length) - 1;
684 mask <<= info->var.transp.offset;
685 value |= mask;
686 }
687 palette[regno] = value;
688 return 0;
689 }
690
691 pindex = regno;
692
693 if (fb->bits_per_pixel == 16) {
694 pindex = regno << 3;
695
696 if (fb->depth == 16 && regno > 63)
697 return -EINVAL;
698 if (fb->depth == 15 && regno > 31)
699 return -EINVAL;
700
701 if (fb->depth == 16) {
702 u16 r, g, b;
703 int i;
704 if (regno < 32) {
705 for (i = 0; i < 8; i++)
706 fb_helper->funcs->gamma_set(crtc, red,
707 green, blue, pindex + i);
708 }
709
710 fb_helper->funcs->gamma_get(crtc, &r,
711 &g, &b,
712 pindex >> 1);
713
714 for (i = 0; i < 4; i++)
715 fb_helper->funcs->gamma_set(crtc, r,
716 green, b,
717 (pindex >> 1) + i);
718 }
719 }
720
721 if (fb->depth != 16)
722 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
723 return 0;
724}
725#endif
726
727#if 0
728int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
729{
730 struct drm_fb_helper *fb_helper = info->par;
731 struct drm_crtc_helper_funcs *crtc_funcs;
732 u16 *red, *green, *blue, *transp;
733 struct drm_crtc *crtc;
734 int i, j, rc = 0;
735 int start;
736
737 for (i = 0; i < fb_helper->crtc_count; i++) {
738 crtc = fb_helper->crtc_info[i].mode_set.crtc;
739 crtc_funcs = crtc->helper_private;
740
741 red = cmap->red;
742 green = cmap->green;
743 blue = cmap->blue;
744 transp = cmap->transp;
745 start = cmap->start;
746
747 for (j = 0; j < cmap->len; j++) {
748 u16 hred, hgreen, hblue, htransp = 0xffff;
749
750 hred = *red++;
751 hgreen = *green++;
752 hblue = *blue++;
753
754 if (transp)
755 htransp = *transp++;
756
757 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
758 if (rc)
759 return rc;
760 }
761 crtc_funcs->load_lut(crtc);
762 }
763 return rc;
764}
765#endif
766
767#if 0
768int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
769 struct fb_info *info)
770{
771 struct drm_fb_helper *fb_helper = info->par;
772 struct drm_framebuffer *fb = fb_helper->fb;
773 int depth;
774
775 if (var->pixclock != 0 || in_dbg_master())
776 return -EINVAL;
777
778 /* Need to resize the fb object !!! */
779 if (var->bits_per_pixel > fb->bits_per_pixel ||
780 var->xres > fb->width || var->yres > fb->height ||
781 var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
782 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
783 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
784 var->xres, var->yres, var->bits_per_pixel,
785 var->xres_virtual, var->yres_virtual,
786 fb->width, fb->height, fb->bits_per_pixel);
787 return -EINVAL;
788 }
789
790 switch (var->bits_per_pixel) {
791 case 16:
792 depth = (var->green.length == 6) ? 16 : 15;
793 break;
794 case 32:
795 depth = (var->transp.length > 0) ? 32 : 24;
796 break;
797 default:
798 depth = var->bits_per_pixel;
799 break;
800 }
801
802 switch (depth) {
803 case 8:
804 var->red.offset = 0;
805 var->green.offset = 0;
806 var->blue.offset = 0;
807 var->red.length = 8;
808 var->green.length = 8;
809 var->blue.length = 8;
810 var->transp.length = 0;
811 var->transp.offset = 0;
812 break;
813 case 15:
814 var->red.offset = 10;
815 var->green.offset = 5;
816 var->blue.offset = 0;
817 var->red.length = 5;
818 var->green.length = 5;
819 var->blue.length = 5;
820 var->transp.length = 1;
821 var->transp.offset = 15;
822 break;
823 case 16:
824 var->red.offset = 11;
825 var->green.offset = 5;
826 var->blue.offset = 0;
827 var->red.length = 5;
828 var->green.length = 6;
829 var->blue.length = 5;
830 var->transp.length = 0;
831 var->transp.offset = 0;
832 break;
833 case 24:
834 var->red.offset = 16;
835 var->green.offset = 8;
836 var->blue.offset = 0;
837 var->red.length = 8;
838 var->green.length = 8;
839 var->blue.length = 8;
840 var->transp.length = 0;
841 var->transp.offset = 0;
842 break;
843 case 32:
844 var->red.offset = 16;
845 var->green.offset = 8;
846 var->blue.offset = 0;
847 var->red.length = 8;
848 var->green.length = 8;
849 var->blue.length = 8;
850 var->transp.length = 8;
851 var->transp.offset = 24;
852 break;
853 default:
854 return -EINVAL;
855 }
856 return 0;
857}
858#endif
859
860#if 0
861/* this will let fbcon do the mode init */
862int drm_fb_helper_set_par(struct fb_info *info)
863{
864 struct drm_fb_helper *fb_helper = info->par;
865 struct drm_device *dev = fb_helper->dev;
866 struct fb_var_screeninfo *var = &info->var;
867 struct drm_crtc *crtc;
868 int ret;
869 int i;
870
871 if (var->pixclock != 0) {
872 DRM_ERROR("PIXEL CLOCK SET\n");
873 return -EINVAL;
874 }
875
876 mutex_lock(&dev->mode_config.mutex);
877 for (i = 0; i < fb_helper->crtc_count; i++) {
878 crtc = fb_helper->crtc_info[i].mode_set.crtc;
879 ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
880 if (ret) {
881 mutex_unlock(&dev->mode_config.mutex);
882 return ret;
883 }
884 }
885 mutex_unlock(&dev->mode_config.mutex);
886
887 if (fb_helper->delayed_hotplug) {
888 fb_helper->delayed_hotplug = false;
889 drm_fb_helper_hotplug_event(fb_helper);
890 }
891 return 0;
892}
893#endif
894
895#if 0
896int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
897 struct fb_info *info)
898{
899 struct drm_fb_helper *fb_helper = info->par;
900 struct drm_device *dev = fb_helper->dev;
901 struct drm_mode_set *modeset;
902 struct drm_crtc *crtc;
903 int ret = 0;
904 int i;
905
906 mutex_lock(&dev->mode_config.mutex);
907 for (i = 0; i < fb_helper->crtc_count; i++) {
908 crtc = fb_helper->crtc_info[i].mode_set.crtc;
909
910 modeset = &fb_helper->crtc_info[i].mode_set;
911
912 modeset->x = var->xoffset;
913 modeset->y = var->yoffset;
914
915 if (modeset->num_connectors) {
916 ret = crtc->funcs->set_config(modeset);
917 if (!ret) {
918 info->var.xoffset = var->xoffset;
919 info->var.yoffset = var->yoffset;
920 }
921 }
922 }
923 mutex_unlock(&dev->mode_config.mutex);
924 return ret;
925}
926#endif
927
928int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
929 int preferred_bpp)
930{
931 int new_fb = 0;
932 int crtc_count = 0;
933 int i;
895#if 0
896 struct fb_info *info;
934 struct fb_info *info;
897#endif
898 struct drm_fb_helper_surface_size sizes;
899 int gamma_size = 0;
935 struct drm_fb_helper_surface_size sizes;
936 int gamma_size = 0;
937#if defined(__FreeBSD__)
938 struct vt_kms_softc *sc;
939 device_t kdev;
940#endif
900
901 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
902 sizes.surface_depth = 24;
903 sizes.surface_bpp = 32;
904 sizes.fb_width = (unsigned)-1;
905 sizes.fb_height = (unsigned)-1;
906
907 /* if driver picks 8 or 16 by default use that
908 for both depth/bpp */
909 if (preferred_bpp != sizes.surface_bpp) {
910 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
911 }
912 /* first up get a count of crtcs now in use and new min/maxes width/heights */
913 for (i = 0; i < fb_helper->connector_count; i++) {
914 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
915 struct drm_fb_helper_cmdline_mode *cmdline_mode;
916
917 cmdline_mode = &fb_helper_conn->cmdline_mode;
918
919 if (cmdline_mode->bpp_specified) {
920 switch (cmdline_mode->bpp) {
921 case 8:
922 sizes.surface_depth = sizes.surface_bpp = 8;
923 break;
924 case 15:
925 sizes.surface_depth = 15;
926 sizes.surface_bpp = 16;
927 break;
928 case 16:
929 sizes.surface_depth = sizes.surface_bpp = 16;
930 break;
931 case 24:
932 sizes.surface_depth = sizes.surface_bpp = 24;
933 break;
934 case 32:
935 sizes.surface_depth = 24;
936 sizes.surface_bpp = 32;
937 break;
938 }
939 break;
940 }
941 }
942
943 crtc_count = 0;
944 for (i = 0; i < fb_helper->crtc_count; i++) {
945 struct drm_display_mode *desired_mode;
946 desired_mode = fb_helper->crtc_info[i].desired_mode;
947
948 if (desired_mode) {
949 if (gamma_size == 0)
950 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
951 if (desired_mode->hdisplay < sizes.fb_width)
952 sizes.fb_width = desired_mode->hdisplay;
953 if (desired_mode->vdisplay < sizes.fb_height)
954 sizes.fb_height = desired_mode->vdisplay;
955 if (desired_mode->hdisplay > sizes.surface_width)
956 sizes.surface_width = desired_mode->hdisplay;
957 if (desired_mode->vdisplay > sizes.surface_height)
958 sizes.surface_height = desired_mode->vdisplay;
959 crtc_count++;
960 }
961 }
962
963 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
964 /* hmm everyone went away - assume VGA cable just fell out
965 and will come back later. */
966 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
967 sizes.fb_width = sizes.surface_width = 1024;
968 sizes.fb_height = sizes.surface_height = 768;
969 }
970
971 /* push down into drivers */
972 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
973 if (new_fb < 0)
974 return new_fb;
975
941
942 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
943 sizes.surface_depth = 24;
944 sizes.surface_bpp = 32;
945 sizes.fb_width = (unsigned)-1;
946 sizes.fb_height = (unsigned)-1;
947
948 /* if driver picks 8 or 16 by default use that
949 for both depth/bpp */
950 if (preferred_bpp != sizes.surface_bpp) {
951 sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
952 }
953 /* first up get a count of crtcs now in use and new min/maxes width/heights */
954 for (i = 0; i < fb_helper->connector_count; i++) {
955 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
956 struct drm_fb_helper_cmdline_mode *cmdline_mode;
957
958 cmdline_mode = &fb_helper_conn->cmdline_mode;
959
960 if (cmdline_mode->bpp_specified) {
961 switch (cmdline_mode->bpp) {
962 case 8:
963 sizes.surface_depth = sizes.surface_bpp = 8;
964 break;
965 case 15:
966 sizes.surface_depth = 15;
967 sizes.surface_bpp = 16;
968 break;
969 case 16:
970 sizes.surface_depth = sizes.surface_bpp = 16;
971 break;
972 case 24:
973 sizes.surface_depth = sizes.surface_bpp = 24;
974 break;
975 case 32:
976 sizes.surface_depth = 24;
977 sizes.surface_bpp = 32;
978 break;
979 }
980 break;
981 }
982 }
983
984 crtc_count = 0;
985 for (i = 0; i < fb_helper->crtc_count; i++) {
986 struct drm_display_mode *desired_mode;
987 desired_mode = fb_helper->crtc_info[i].desired_mode;
988
989 if (desired_mode) {
990 if (gamma_size == 0)
991 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
992 if (desired_mode->hdisplay < sizes.fb_width)
993 sizes.fb_width = desired_mode->hdisplay;
994 if (desired_mode->vdisplay < sizes.fb_height)
995 sizes.fb_height = desired_mode->vdisplay;
996 if (desired_mode->hdisplay > sizes.surface_width)
997 sizes.surface_width = desired_mode->hdisplay;
998 if (desired_mode->vdisplay > sizes.surface_height)
999 sizes.surface_height = desired_mode->vdisplay;
1000 crtc_count++;
1001 }
1002 }
1003
1004 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1005 /* hmm everyone went away - assume VGA cable just fell out
1006 and will come back later. */
1007 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1008 sizes.fb_width = sizes.surface_width = 1024;
1009 sizes.fb_height = sizes.surface_height = 768;
1010 }
1011
1012 /* push down into drivers */
1013 new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1014 if (new_fb < 0)
1015 return new_fb;
1016
976#if 0
1017#if defined(__FreeBSD__)
1018 sc = malloc(sizeof(struct vt_kms_softc), DRM_MEM_KMS,
1019 M_WAITOK | M_ZERO);
1020 sc->fb_helper = fb_helper;
1021 TASK_INIT(&sc->fb_mode_task, 0, vt_restore_fbdev_mode, sc);
1022
977 info = fb_helper->fbdev;
1023 info = fb_helper->fbdev;
1024
1025 info->fb_name = device_get_nameunit(fb_helper->dev->device);
1026 info->fb_depth = fb_helper->fb->bits_per_pixel;
1027 info->fb_height = fb_helper->fb->height;
1028 info->fb_width = fb_helper->fb->width;
1029 info->fb_stride = fb_helper->fb->pitches[0];
1030 info->fb_priv = sc;
1031 info->enter = &vt_kms_postswitch;
978#endif
979
980 /* set the fb pointer */
981 for (i = 0; i < fb_helper->crtc_count; i++) {
982 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
983 }
984
1032#endif
1033
1034 /* set the fb pointer */
1035 for (i = 0; i < fb_helper->crtc_count; i++) {
1036 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1037 }
1038
985#if 0
1039#if defined(__FreeBSD__)
986 if (new_fb) {
1040 if (new_fb) {
1041 device_t fbd;
1042 int ret;
1043
1044 kdev = fb_helper->dev->device;
1045 fbd = device_add_child(kdev, "fbd", device_get_unit(kdev));
1046 ret = device_probe_and_attach(fbd);
1047 if (ret != 0)
1048 DRM_ERROR("Failed to attach fbd device: %d\n", ret);
1049 }
1050#else
1051 if (new_fb) {
987 info->var.pixclock = 0;
988 if (register_framebuffer(info) < 0) {
989 return -EINVAL;
990 }
991
992 printf("fb%d: %s frame buffer device\n", info->node,
993 info->fix.id);
994
995 } else {
996 drm_fb_helper_set_par(info);
997 }
998
999 /* Switch back to kernel console on panic */
1000 /* multi card linked list maybe */
1001 if (list_empty(&kernel_fb_helper_list)) {
1002 printf("drm: registered panic notifier\n");
1003 atomic_notifier_chain_register(&panic_notifier_list,
1004 &paniced);
1005 }
1006 if (new_fb)
1007 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1008#endif
1052 info->var.pixclock = 0;
1053 if (register_framebuffer(info) < 0) {
1054 return -EINVAL;
1055 }
1056
1057 printf("fb%d: %s frame buffer device\n", info->node,
1058 info->fix.id);
1059
1060 } else {
1061 drm_fb_helper_set_par(info);
1062 }
1063
1064 /* Switch back to kernel console on panic */
1065 /* multi card linked list maybe */
1066 if (list_empty(&kernel_fb_helper_list)) {
1067 printf("drm: registered panic notifier\n");
1068 atomic_notifier_chain_register(&panic_notifier_list,
1069 &paniced);
1070 }
1071 if (new_fb)
1072 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1073#endif
1009
1010 return 0;
1011}
1012
1013#if 0
1014void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1015 uint32_t depth)
1016{
1017 info->fix.type = FB_TYPE_PACKED_PIXELS;
1018 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1019 FB_VISUAL_trueCOLOR;
1020 info->fix.mmio_start = 0;
1021 info->fix.mmio_len = 0;
1022 info->fix.type_aux = 0;
1023 info->fix.xpanstep = 1; /* doing it in hw */
1024 info->fix.ypanstep = 1; /* doing it in hw */
1025 info->fix.ywrapstep = 0;
1026 info->fix.accel = FB_ACCEL_NONE;
1027 info->fix.type_aux = 0;
1028
1029 info->fix.line_length = pitch;
1030 return;
1031}
1032
1033void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1034 uint32_t fb_width, uint32_t fb_height)
1035{
1036 struct drm_framebuffer *fb = fb_helper->fb;
1037 info->pseudo_palette = fb_helper->pseudo_palette;
1038 info->var.xres_virtual = fb->width;
1039 info->var.yres_virtual = fb->height;
1040 info->var.bits_per_pixel = fb->bits_per_pixel;
1041 info->var.accel_flags = FB_ACCELF_TEXT;
1042 info->var.xoffset = 0;
1043 info->var.yoffset = 0;
1044 info->var.activate = FB_ACTIVATE_NOW;
1045 info->var.height = -1;
1046 info->var.width = -1;
1047
1048 switch (fb->depth) {
1049 case 8:
1050 info->var.red.offset = 0;
1051 info->var.green.offset = 0;
1052 info->var.blue.offset = 0;
1053 info->var.red.length = 8; /* 8bit DAC */
1054 info->var.green.length = 8;
1055 info->var.blue.length = 8;
1056 info->var.transp.offset = 0;
1057 info->var.transp.length = 0;
1058 break;
1059 case 15:
1060 info->var.red.offset = 10;
1061 info->var.green.offset = 5;
1062 info->var.blue.offset = 0;
1063 info->var.red.length = 5;
1064 info->var.green.length = 5;
1065 info->var.blue.length = 5;
1066 info->var.transp.offset = 15;
1067 info->var.transp.length = 1;
1068 break;
1069 case 16:
1070 info->var.red.offset = 11;
1071 info->var.green.offset = 5;
1072 info->var.blue.offset = 0;
1073 info->var.red.length = 5;
1074 info->var.green.length = 6;
1075 info->var.blue.length = 5;
1076 info->var.transp.offset = 0;
1077 break;
1078 case 24:
1079 info->var.red.offset = 16;
1080 info->var.green.offset = 8;
1081 info->var.blue.offset = 0;
1082 info->var.red.length = 8;
1083 info->var.green.length = 8;
1084 info->var.blue.length = 8;
1085 info->var.transp.offset = 0;
1086 info->var.transp.length = 0;
1087 break;
1088 case 32:
1089 info->var.red.offset = 16;
1090 info->var.green.offset = 8;
1091 info->var.blue.offset = 0;
1092 info->var.red.length = 8;
1093 info->var.green.length = 8;
1094 info->var.blue.length = 8;
1095 info->var.transp.offset = 24;
1096 info->var.transp.length = 8;
1097 break;
1098 default:
1099 break;
1100 }
1101
1102 info->var.xres = fb_width;
1103 info->var.yres = fb_height;
1104}
1105#endif
1106
1107static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1108 uint32_t maxX,
1109 uint32_t maxY)
1110{
1111 struct drm_connector *connector;
1112 int count = 0;
1113 int i;
1114
1115 for (i = 0; i < fb_helper->connector_count; i++) {
1116 connector = fb_helper->connector_info[i]->connector;
1117 count += connector->funcs->fill_modes(connector, maxX, maxY);
1118 }
1119
1120 return count;
1121}
1122
1123static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1124{
1125 struct drm_display_mode *mode;
1126
1127 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1128 if (drm_mode_width(mode) > width ||
1129 drm_mode_height(mode) > height)
1130 continue;
1131 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1132 return mode;
1133 }
1134 return NULL;
1135}
1136
1137static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1138{
1139 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1140 cmdline_mode = &fb_connector->cmdline_mode;
1141 return cmdline_mode->specified;
1142}
1143
1144static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1145 int width, int height)
1146{
1147 struct drm_cmdline_mode *cmdline_mode;
1148 struct drm_display_mode *mode = NULL;
1149
1150 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1151 if (cmdline_mode->specified == false &&
1152 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1153 cmdline_mode))
1154 return (NULL);
1155
1156 /* attempt to find a matching mode in the list of modes
1157 * we have gotten so far, if not add a CVT mode that conforms
1158 */
1159 if (cmdline_mode->rb || cmdline_mode->margins)
1160 goto create_mode;
1161
1162 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1163 /* check width/height */
1164 if (mode->hdisplay != cmdline_mode->xres ||
1165 mode->vdisplay != cmdline_mode->yres)
1166 continue;
1167
1168 if (cmdline_mode->refresh_specified) {
1169 if (mode->vrefresh != cmdline_mode->refresh)
1170 continue;
1171 }
1172
1173 if (cmdline_mode->interlace) {
1174 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1175 continue;
1176 }
1177 return mode;
1178 }
1179
1180create_mode:
1181 if (cmdline_mode->cvt)
1182 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1183 cmdline_mode->xres, cmdline_mode->yres,
1184 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1185 cmdline_mode->rb, cmdline_mode->interlace,
1186 cmdline_mode->margins);
1187 else
1188 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1189 cmdline_mode->xres, cmdline_mode->yres,
1190 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1191 cmdline_mode->interlace,
1192 cmdline_mode->margins);
1193 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1194 list_add(&mode->head, &fb_helper_conn->connector->modes);
1195 return mode;
1196}
1197
1198static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1199{
1200 bool enable;
1201
1202 if (strict) {
1203 enable = connector->status == connector_status_connected;
1204 } else {
1205 enable = connector->status != connector_status_disconnected;
1206 }
1207 return enable;
1208}
1209
1210static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1211 bool *enabled)
1212{
1213 bool any_enabled = false;
1214 struct drm_connector *connector;
1215 int i = 0;
1216
1217 for (i = 0; i < fb_helper->connector_count; i++) {
1218 connector = fb_helper->connector_info[i]->connector;
1219 enabled[i] = drm_connector_enabled(connector, true);
1220 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1221 enabled[i] ? "yes" : "no");
1222 any_enabled |= enabled[i];
1223 }
1224
1225 if (any_enabled)
1226 return;
1227
1228 for (i = 0; i < fb_helper->connector_count; i++) {
1229 connector = fb_helper->connector_info[i]->connector;
1230 enabled[i] = drm_connector_enabled(connector, false);
1231 }
1232}
1233
1234static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1235 struct drm_display_mode **modes,
1236 bool *enabled, int width, int height)
1237{
1238 int count, i, j;
1239 bool can_clone = false;
1240 struct drm_fb_helper_connector *fb_helper_conn;
1241 struct drm_display_mode *dmt_mode, *mode;
1242
1243 /* only contemplate cloning in the single crtc case */
1244 if (fb_helper->crtc_count > 1)
1245 return false;
1246
1247 count = 0;
1248 for (i = 0; i < fb_helper->connector_count; i++) {
1249 if (enabled[i])
1250 count++;
1251 }
1252
1253 /* only contemplate cloning if more than one connector is enabled */
1254 if (count <= 1)
1255 return false;
1256
1257 /* check the command line or if nothing common pick 1024x768 */
1258 can_clone = true;
1259 for (i = 0; i < fb_helper->connector_count; i++) {
1260 if (!enabled[i])
1261 continue;
1262 fb_helper_conn = fb_helper->connector_info[i];
1263 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1264 if (!modes[i]) {
1265 can_clone = false;
1266 break;
1267 }
1268 for (j = 0; j < i; j++) {
1269 if (!enabled[j])
1270 continue;
1271 if (!drm_mode_equal(modes[j], modes[i]))
1272 can_clone = false;
1273 }
1274 }
1275
1276 if (can_clone) {
1277 DRM_DEBUG_KMS("can clone using command line\n");
1278 return true;
1279 }
1280
1281 /* try and find a 1024x768 mode on each connector */
1282 can_clone = true;
1283 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1284
1285 for (i = 0; i < fb_helper->connector_count; i++) {
1286
1287 if (!enabled[i])
1288 continue;
1289
1290 fb_helper_conn = fb_helper->connector_info[i];
1291 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1292 if (drm_mode_equal(mode, dmt_mode))
1293 modes[i] = mode;
1294 }
1295 if (!modes[i])
1296 can_clone = false;
1297 }
1298
1299 if (can_clone) {
1300 DRM_DEBUG_KMS("can clone using 1024x768\n");
1301 return true;
1302 }
1303 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1304 return false;
1305}
1306
1307static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1308 struct drm_display_mode **modes,
1309 bool *enabled, int width, int height)
1310{
1311 struct drm_fb_helper_connector *fb_helper_conn;
1312 int i;
1313
1314 for (i = 0; i < fb_helper->connector_count; i++) {
1315 fb_helper_conn = fb_helper->connector_info[i];
1316
1317 if (enabled[i] == false)
1318 continue;
1319
1320 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1321 fb_helper_conn->connector->base.id);
1322
1323 /* got for command line mode first */
1324 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1325 if (!modes[i]) {
1326 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1327 fb_helper_conn->connector->base.id);
1328 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1329 }
1330 /* No preferred modes, pick one off the list */
1331 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1332 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1333 break;
1334 }
1335 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1336 "none");
1337 }
1338 return true;
1339}
1340
1341static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1342 struct drm_fb_helper_crtc **best_crtcs,
1343 struct drm_display_mode **modes,
1344 int n, int width, int height)
1345{
1346 int c, o;
1347 struct drm_device *dev = fb_helper->dev;
1348 struct drm_connector *connector;
1349 struct drm_connector_helper_funcs *connector_funcs;
1350 struct drm_encoder *encoder;
1351 struct drm_fb_helper_crtc *best_crtc;
1352 int my_score, best_score, score;
1353 struct drm_fb_helper_crtc **crtcs, *crtc;
1354 struct drm_fb_helper_connector *fb_helper_conn;
1355
1356 if (n == fb_helper->connector_count)
1357 return 0;
1358
1359 fb_helper_conn = fb_helper->connector_info[n];
1360 connector = fb_helper_conn->connector;
1361
1362 best_crtcs[n] = NULL;
1363 best_crtc = NULL;
1364 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1365 if (modes[n] == NULL)
1366 return best_score;
1367
1368 crtcs = malloc(dev->mode_config.num_connector *
1369 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1370 M_WAITOK | M_ZERO);
1371
1372 my_score = 1;
1373 if (connector->status == connector_status_connected)
1374 my_score++;
1375 if (drm_has_cmdline_mode(fb_helper_conn))
1376 my_score++;
1377 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1378 my_score++;
1379
1380 connector_funcs = connector->helper_private;
1381 encoder = connector_funcs->best_encoder(connector);
1382 if (!encoder)
1383 goto out;
1384
1385 /* select a crtc for this connector and then attempt to configure
1386 remaining connectors */
1387 for (c = 0; c < fb_helper->crtc_count; c++) {
1388 crtc = &fb_helper->crtc_info[c];
1389
1390 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1391 continue;
1392 }
1393
1394 for (o = 0; o < n; o++)
1395 if (best_crtcs[o] == crtc)
1396 break;
1397
1398 if (o < n) {
1399 /* ignore cloning unless only a single crtc */
1400 if (fb_helper->crtc_count > 1)
1401 continue;
1402
1403 if (!drm_mode_equal(modes[o], modes[n]))
1404 continue;
1405 }
1406
1407 crtcs[n] = crtc;
1408 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1409 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1410 width, height);
1411 if (score > best_score) {
1412 best_crtc = crtc;
1413 best_score = score;
1414 memcpy(best_crtcs, crtcs,
1415 dev->mode_config.num_connector *
1416 sizeof(struct drm_fb_helper_crtc *));
1417 }
1418 }
1419out:
1420 free(crtcs, DRM_MEM_KMS);
1421 return best_score;
1422}
1423
1424static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1425{
1426 struct drm_device *dev = fb_helper->dev;
1427 struct drm_fb_helper_crtc **crtcs;
1428 struct drm_display_mode **modes;
1429 struct drm_encoder *encoder;
1430 struct drm_mode_set *modeset;
1431 bool *enabled;
1432 int width, height;
1433 int i, ret;
1434
1435 DRM_DEBUG_KMS("\n");
1436
1437 width = dev->mode_config.max_width;
1438 height = dev->mode_config.max_height;
1439
1440 /* clean out all the encoder/crtc combos */
1441 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1442 encoder->crtc = NULL;
1443 }
1444
1445 crtcs = malloc(dev->mode_config.num_connector *
1446 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1447 M_WAITOK | M_ZERO);
1448 modes = malloc(dev->mode_config.num_connector *
1449 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1450 M_WAITOK | M_ZERO);
1451 enabled = malloc(dev->mode_config.num_connector *
1452 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1453
1454 drm_enable_connectors(fb_helper, enabled);
1455
1456 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1457 if (!ret) {
1458 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1459 if (!ret)
1460 DRM_ERROR("Unable to find initial modes\n");
1461 }
1462
1463 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1464
1465 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1466
1467 /* need to set the modesets up here for use later */
1468 /* fill out the connector<->crtc mappings into the modesets */
1469 for (i = 0; i < fb_helper->crtc_count; i++) {
1470 modeset = &fb_helper->crtc_info[i].mode_set;
1471 modeset->num_connectors = 0;
1472 }
1473
1474 for (i = 0; i < fb_helper->connector_count; i++) {
1475 struct drm_display_mode *mode = modes[i];
1476 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1477 modeset = &fb_crtc->mode_set;
1478
1479 if (mode && fb_crtc) {
1480 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1481 mode->name, fb_crtc->mode_set.crtc->base.id);
1482 fb_crtc->desired_mode = mode;
1483 if (modeset->mode)
1484 drm_mode_destroy(dev, modeset->mode);
1485 modeset->mode = drm_mode_duplicate(dev,
1486 fb_crtc->desired_mode);
1487 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1488 }
1489 }
1490
1491 free(crtcs, DRM_MEM_KMS);
1492 free(modes, DRM_MEM_KMS);
1493 free(enabled, DRM_MEM_KMS);
1494}
1495
1496/**
1497 * drm_helper_initial_config - setup a sane initial connector configuration
1498 * @dev: DRM device
1499 *
1500 * LOCKING:
1501 * Called at init time, must take mode config lock.
1502 *
1503 * Scan the CRTCs and connectors and try to put together an initial setup.
1504 * At the moment, this is a cloned configuration across all heads with
1505 * a new framebuffer object as the backing store.
1506 *
1507 * RETURNS:
1508 * Zero if everything went ok, nonzero otherwise.
1509 */
1510bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1511{
1512 struct drm_device *dev = fb_helper->dev;
1513 int count = 0;
1514
1515 /* disable all the possible outputs/crtcs before entering KMS mode */
1516 drm_helper_disable_unused_functions(fb_helper->dev);
1517
1518 drm_fb_helper_parse_command_line(fb_helper);
1519
1520 count = drm_fb_helper_probe_connector_modes(fb_helper,
1521 dev->mode_config.max_width,
1522 dev->mode_config.max_height);
1523 /*
1524 * we shouldn't end up with no modes here.
1525 */
1526 if (count == 0) {
1527 printf("No connectors reported connected with modes\n");
1528 }
1529 drm_setup_crtcs(fb_helper);
1530
1531 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1532}
1533
1534int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1535{
1536 struct drm_device *dev = fb_helper->dev;
1537 int count = 0;
1538 u32 max_width, max_height, bpp_sel;
1539 bool bound = false, crtcs_bound = false;
1540 struct drm_crtc *crtc;
1541
1542 if (!fb_helper->fb)
1543 return 0;
1544
1545 sx_xlock(&dev->mode_config.mutex);
1546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1547 if (crtc->fb)
1548 crtcs_bound = true;
1549 if (crtc->fb == fb_helper->fb)
1550 bound = true;
1551 }
1552
1553 if (!bound && crtcs_bound) {
1554 fb_helper->delayed_hotplug = true;
1555 sx_xunlock(&dev->mode_config.mutex);
1556 return 0;
1557 }
1558 DRM_DEBUG_KMS("\n");
1559
1560 max_width = fb_helper->fb->width;
1561 max_height = fb_helper->fb->height;
1562 bpp_sel = fb_helper->fb->bits_per_pixel;
1563
1564 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1565 max_height);
1566 drm_setup_crtcs(fb_helper);
1567 sx_xunlock(&dev->mode_config.mutex);
1568
1569 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1570}
1571
1074 return 0;
1075}
1076
1077#if 0
1078void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1079 uint32_t depth)
1080{
1081 info->fix.type = FB_TYPE_PACKED_PIXELS;
1082 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1083 FB_VISUAL_trueCOLOR;
1084 info->fix.mmio_start = 0;
1085 info->fix.mmio_len = 0;
1086 info->fix.type_aux = 0;
1087 info->fix.xpanstep = 1; /* doing it in hw */
1088 info->fix.ypanstep = 1; /* doing it in hw */
1089 info->fix.ywrapstep = 0;
1090 info->fix.accel = FB_ACCEL_NONE;
1091 info->fix.type_aux = 0;
1092
1093 info->fix.line_length = pitch;
1094 return;
1095}
1096
1097void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1098 uint32_t fb_width, uint32_t fb_height)
1099{
1100 struct drm_framebuffer *fb = fb_helper->fb;
1101 info->pseudo_palette = fb_helper->pseudo_palette;
1102 info->var.xres_virtual = fb->width;
1103 info->var.yres_virtual = fb->height;
1104 info->var.bits_per_pixel = fb->bits_per_pixel;
1105 info->var.accel_flags = FB_ACCELF_TEXT;
1106 info->var.xoffset = 0;
1107 info->var.yoffset = 0;
1108 info->var.activate = FB_ACTIVATE_NOW;
1109 info->var.height = -1;
1110 info->var.width = -1;
1111
1112 switch (fb->depth) {
1113 case 8:
1114 info->var.red.offset = 0;
1115 info->var.green.offset = 0;
1116 info->var.blue.offset = 0;
1117 info->var.red.length = 8; /* 8bit DAC */
1118 info->var.green.length = 8;
1119 info->var.blue.length = 8;
1120 info->var.transp.offset = 0;
1121 info->var.transp.length = 0;
1122 break;
1123 case 15:
1124 info->var.red.offset = 10;
1125 info->var.green.offset = 5;
1126 info->var.blue.offset = 0;
1127 info->var.red.length = 5;
1128 info->var.green.length = 5;
1129 info->var.blue.length = 5;
1130 info->var.transp.offset = 15;
1131 info->var.transp.length = 1;
1132 break;
1133 case 16:
1134 info->var.red.offset = 11;
1135 info->var.green.offset = 5;
1136 info->var.blue.offset = 0;
1137 info->var.red.length = 5;
1138 info->var.green.length = 6;
1139 info->var.blue.length = 5;
1140 info->var.transp.offset = 0;
1141 break;
1142 case 24:
1143 info->var.red.offset = 16;
1144 info->var.green.offset = 8;
1145 info->var.blue.offset = 0;
1146 info->var.red.length = 8;
1147 info->var.green.length = 8;
1148 info->var.blue.length = 8;
1149 info->var.transp.offset = 0;
1150 info->var.transp.length = 0;
1151 break;
1152 case 32:
1153 info->var.red.offset = 16;
1154 info->var.green.offset = 8;
1155 info->var.blue.offset = 0;
1156 info->var.red.length = 8;
1157 info->var.green.length = 8;
1158 info->var.blue.length = 8;
1159 info->var.transp.offset = 24;
1160 info->var.transp.length = 8;
1161 break;
1162 default:
1163 break;
1164 }
1165
1166 info->var.xres = fb_width;
1167 info->var.yres = fb_height;
1168}
1169#endif
1170
1171static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1172 uint32_t maxX,
1173 uint32_t maxY)
1174{
1175 struct drm_connector *connector;
1176 int count = 0;
1177 int i;
1178
1179 for (i = 0; i < fb_helper->connector_count; i++) {
1180 connector = fb_helper->connector_info[i]->connector;
1181 count += connector->funcs->fill_modes(connector, maxX, maxY);
1182 }
1183
1184 return count;
1185}
1186
1187static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1188{
1189 struct drm_display_mode *mode;
1190
1191 list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1192 if (drm_mode_width(mode) > width ||
1193 drm_mode_height(mode) > height)
1194 continue;
1195 if (mode->type & DRM_MODE_TYPE_PREFERRED)
1196 return mode;
1197 }
1198 return NULL;
1199}
1200
1201static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1202{
1203 struct drm_fb_helper_cmdline_mode *cmdline_mode;
1204 cmdline_mode = &fb_connector->cmdline_mode;
1205 return cmdline_mode->specified;
1206}
1207
1208static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1209 int width, int height)
1210{
1211 struct drm_cmdline_mode *cmdline_mode;
1212 struct drm_display_mode *mode = NULL;
1213
1214 cmdline_mode = &fb_helper_conn->cmdline_mode1;
1215 if (cmdline_mode->specified == false &&
1216 !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1217 cmdline_mode))
1218 return (NULL);
1219
1220 /* attempt to find a matching mode in the list of modes
1221 * we have gotten so far, if not add a CVT mode that conforms
1222 */
1223 if (cmdline_mode->rb || cmdline_mode->margins)
1224 goto create_mode;
1225
1226 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1227 /* check width/height */
1228 if (mode->hdisplay != cmdline_mode->xres ||
1229 mode->vdisplay != cmdline_mode->yres)
1230 continue;
1231
1232 if (cmdline_mode->refresh_specified) {
1233 if (mode->vrefresh != cmdline_mode->refresh)
1234 continue;
1235 }
1236
1237 if (cmdline_mode->interlace) {
1238 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1239 continue;
1240 }
1241 return mode;
1242 }
1243
1244create_mode:
1245 if (cmdline_mode->cvt)
1246 mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1247 cmdline_mode->xres, cmdline_mode->yres,
1248 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1249 cmdline_mode->rb, cmdline_mode->interlace,
1250 cmdline_mode->margins);
1251 else
1252 mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1253 cmdline_mode->xres, cmdline_mode->yres,
1254 cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1255 cmdline_mode->interlace,
1256 cmdline_mode->margins);
1257 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1258 list_add(&mode->head, &fb_helper_conn->connector->modes);
1259 return mode;
1260}
1261
1262static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1263{
1264 bool enable;
1265
1266 if (strict) {
1267 enable = connector->status == connector_status_connected;
1268 } else {
1269 enable = connector->status != connector_status_disconnected;
1270 }
1271 return enable;
1272}
1273
1274static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1275 bool *enabled)
1276{
1277 bool any_enabled = false;
1278 struct drm_connector *connector;
1279 int i = 0;
1280
1281 for (i = 0; i < fb_helper->connector_count; i++) {
1282 connector = fb_helper->connector_info[i]->connector;
1283 enabled[i] = drm_connector_enabled(connector, true);
1284 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1285 enabled[i] ? "yes" : "no");
1286 any_enabled |= enabled[i];
1287 }
1288
1289 if (any_enabled)
1290 return;
1291
1292 for (i = 0; i < fb_helper->connector_count; i++) {
1293 connector = fb_helper->connector_info[i]->connector;
1294 enabled[i] = drm_connector_enabled(connector, false);
1295 }
1296}
1297
1298static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1299 struct drm_display_mode **modes,
1300 bool *enabled, int width, int height)
1301{
1302 int count, i, j;
1303 bool can_clone = false;
1304 struct drm_fb_helper_connector *fb_helper_conn;
1305 struct drm_display_mode *dmt_mode, *mode;
1306
1307 /* only contemplate cloning in the single crtc case */
1308 if (fb_helper->crtc_count > 1)
1309 return false;
1310
1311 count = 0;
1312 for (i = 0; i < fb_helper->connector_count; i++) {
1313 if (enabled[i])
1314 count++;
1315 }
1316
1317 /* only contemplate cloning if more than one connector is enabled */
1318 if (count <= 1)
1319 return false;
1320
1321 /* check the command line or if nothing common pick 1024x768 */
1322 can_clone = true;
1323 for (i = 0; i < fb_helper->connector_count; i++) {
1324 if (!enabled[i])
1325 continue;
1326 fb_helper_conn = fb_helper->connector_info[i];
1327 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1328 if (!modes[i]) {
1329 can_clone = false;
1330 break;
1331 }
1332 for (j = 0; j < i; j++) {
1333 if (!enabled[j])
1334 continue;
1335 if (!drm_mode_equal(modes[j], modes[i]))
1336 can_clone = false;
1337 }
1338 }
1339
1340 if (can_clone) {
1341 DRM_DEBUG_KMS("can clone using command line\n");
1342 return true;
1343 }
1344
1345 /* try and find a 1024x768 mode on each connector */
1346 can_clone = true;
1347 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1348
1349 for (i = 0; i < fb_helper->connector_count; i++) {
1350
1351 if (!enabled[i])
1352 continue;
1353
1354 fb_helper_conn = fb_helper->connector_info[i];
1355 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1356 if (drm_mode_equal(mode, dmt_mode))
1357 modes[i] = mode;
1358 }
1359 if (!modes[i])
1360 can_clone = false;
1361 }
1362
1363 if (can_clone) {
1364 DRM_DEBUG_KMS("can clone using 1024x768\n");
1365 return true;
1366 }
1367 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1368 return false;
1369}
1370
1371static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1372 struct drm_display_mode **modes,
1373 bool *enabled, int width, int height)
1374{
1375 struct drm_fb_helper_connector *fb_helper_conn;
1376 int i;
1377
1378 for (i = 0; i < fb_helper->connector_count; i++) {
1379 fb_helper_conn = fb_helper->connector_info[i];
1380
1381 if (enabled[i] == false)
1382 continue;
1383
1384 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1385 fb_helper_conn->connector->base.id);
1386
1387 /* got for command line mode first */
1388 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1389 if (!modes[i]) {
1390 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1391 fb_helper_conn->connector->base.id);
1392 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1393 }
1394 /* No preferred modes, pick one off the list */
1395 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1396 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1397 break;
1398 }
1399 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1400 "none");
1401 }
1402 return true;
1403}
1404
1405static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1406 struct drm_fb_helper_crtc **best_crtcs,
1407 struct drm_display_mode **modes,
1408 int n, int width, int height)
1409{
1410 int c, o;
1411 struct drm_device *dev = fb_helper->dev;
1412 struct drm_connector *connector;
1413 struct drm_connector_helper_funcs *connector_funcs;
1414 struct drm_encoder *encoder;
1415 struct drm_fb_helper_crtc *best_crtc;
1416 int my_score, best_score, score;
1417 struct drm_fb_helper_crtc **crtcs, *crtc;
1418 struct drm_fb_helper_connector *fb_helper_conn;
1419
1420 if (n == fb_helper->connector_count)
1421 return 0;
1422
1423 fb_helper_conn = fb_helper->connector_info[n];
1424 connector = fb_helper_conn->connector;
1425
1426 best_crtcs[n] = NULL;
1427 best_crtc = NULL;
1428 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1429 if (modes[n] == NULL)
1430 return best_score;
1431
1432 crtcs = malloc(dev->mode_config.num_connector *
1433 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1434 M_WAITOK | M_ZERO);
1435
1436 my_score = 1;
1437 if (connector->status == connector_status_connected)
1438 my_score++;
1439 if (drm_has_cmdline_mode(fb_helper_conn))
1440 my_score++;
1441 if (drm_has_preferred_mode(fb_helper_conn, width, height))
1442 my_score++;
1443
1444 connector_funcs = connector->helper_private;
1445 encoder = connector_funcs->best_encoder(connector);
1446 if (!encoder)
1447 goto out;
1448
1449 /* select a crtc for this connector and then attempt to configure
1450 remaining connectors */
1451 for (c = 0; c < fb_helper->crtc_count; c++) {
1452 crtc = &fb_helper->crtc_info[c];
1453
1454 if ((encoder->possible_crtcs & (1 << c)) == 0) {
1455 continue;
1456 }
1457
1458 for (o = 0; o < n; o++)
1459 if (best_crtcs[o] == crtc)
1460 break;
1461
1462 if (o < n) {
1463 /* ignore cloning unless only a single crtc */
1464 if (fb_helper->crtc_count > 1)
1465 continue;
1466
1467 if (!drm_mode_equal(modes[o], modes[n]))
1468 continue;
1469 }
1470
1471 crtcs[n] = crtc;
1472 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1473 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1474 width, height);
1475 if (score > best_score) {
1476 best_crtc = crtc;
1477 best_score = score;
1478 memcpy(best_crtcs, crtcs,
1479 dev->mode_config.num_connector *
1480 sizeof(struct drm_fb_helper_crtc *));
1481 }
1482 }
1483out:
1484 free(crtcs, DRM_MEM_KMS);
1485 return best_score;
1486}
1487
1488static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1489{
1490 struct drm_device *dev = fb_helper->dev;
1491 struct drm_fb_helper_crtc **crtcs;
1492 struct drm_display_mode **modes;
1493 struct drm_encoder *encoder;
1494 struct drm_mode_set *modeset;
1495 bool *enabled;
1496 int width, height;
1497 int i, ret;
1498
1499 DRM_DEBUG_KMS("\n");
1500
1501 width = dev->mode_config.max_width;
1502 height = dev->mode_config.max_height;
1503
1504 /* clean out all the encoder/crtc combos */
1505 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1506 encoder->crtc = NULL;
1507 }
1508
1509 crtcs = malloc(dev->mode_config.num_connector *
1510 sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1511 M_WAITOK | M_ZERO);
1512 modes = malloc(dev->mode_config.num_connector *
1513 sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1514 M_WAITOK | M_ZERO);
1515 enabled = malloc(dev->mode_config.num_connector *
1516 sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1517
1518 drm_enable_connectors(fb_helper, enabled);
1519
1520 ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1521 if (!ret) {
1522 ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1523 if (!ret)
1524 DRM_ERROR("Unable to find initial modes\n");
1525 }
1526
1527 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1528
1529 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1530
1531 /* need to set the modesets up here for use later */
1532 /* fill out the connector<->crtc mappings into the modesets */
1533 for (i = 0; i < fb_helper->crtc_count; i++) {
1534 modeset = &fb_helper->crtc_info[i].mode_set;
1535 modeset->num_connectors = 0;
1536 }
1537
1538 for (i = 0; i < fb_helper->connector_count; i++) {
1539 struct drm_display_mode *mode = modes[i];
1540 struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1541 modeset = &fb_crtc->mode_set;
1542
1543 if (mode && fb_crtc) {
1544 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1545 mode->name, fb_crtc->mode_set.crtc->base.id);
1546 fb_crtc->desired_mode = mode;
1547 if (modeset->mode)
1548 drm_mode_destroy(dev, modeset->mode);
1549 modeset->mode = drm_mode_duplicate(dev,
1550 fb_crtc->desired_mode);
1551 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1552 }
1553 }
1554
1555 free(crtcs, DRM_MEM_KMS);
1556 free(modes, DRM_MEM_KMS);
1557 free(enabled, DRM_MEM_KMS);
1558}
1559
1560/**
1561 * drm_helper_initial_config - setup a sane initial connector configuration
1562 * @dev: DRM device
1563 *
1564 * LOCKING:
1565 * Called at init time, must take mode config lock.
1566 *
1567 * Scan the CRTCs and connectors and try to put together an initial setup.
1568 * At the moment, this is a cloned configuration across all heads with
1569 * a new framebuffer object as the backing store.
1570 *
1571 * RETURNS:
1572 * Zero if everything went ok, nonzero otherwise.
1573 */
1574bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1575{
1576 struct drm_device *dev = fb_helper->dev;
1577 int count = 0;
1578
1579 /* disable all the possible outputs/crtcs before entering KMS mode */
1580 drm_helper_disable_unused_functions(fb_helper->dev);
1581
1582 drm_fb_helper_parse_command_line(fb_helper);
1583
1584 count = drm_fb_helper_probe_connector_modes(fb_helper,
1585 dev->mode_config.max_width,
1586 dev->mode_config.max_height);
1587 /*
1588 * we shouldn't end up with no modes here.
1589 */
1590 if (count == 0) {
1591 printf("No connectors reported connected with modes\n");
1592 }
1593 drm_setup_crtcs(fb_helper);
1594
1595 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1596}
1597
1598int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1599{
1600 struct drm_device *dev = fb_helper->dev;
1601 int count = 0;
1602 u32 max_width, max_height, bpp_sel;
1603 bool bound = false, crtcs_bound = false;
1604 struct drm_crtc *crtc;
1605
1606 if (!fb_helper->fb)
1607 return 0;
1608
1609 sx_xlock(&dev->mode_config.mutex);
1610 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1611 if (crtc->fb)
1612 crtcs_bound = true;
1613 if (crtc->fb == fb_helper->fb)
1614 bound = true;
1615 }
1616
1617 if (!bound && crtcs_bound) {
1618 fb_helper->delayed_hotplug = true;
1619 sx_xunlock(&dev->mode_config.mutex);
1620 return 0;
1621 }
1622 DRM_DEBUG_KMS("\n");
1623
1624 max_width = fb_helper->fb->width;
1625 max_height = fb_helper->fb->height;
1626 bpp_sel = fb_helper->fb->bits_per_pixel;
1627
1628 count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1629 max_height);
1630 drm_setup_crtcs(fb_helper);
1631 sx_xunlock(&dev->mode_config.mutex);
1632
1633 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1634}
1635