drm_fb_helper.c revision 235783
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 235783 2012-05-22 11:07:44Z kib $");
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
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
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	free(helper->crtc_info, DRM_MEM_KMS);
561}
562
563int drm_fb_helper_init(struct drm_device *dev,
564		       struct drm_fb_helper *fb_helper,
565		       int crtc_count, int max_conn_count)
566{
567	struct drm_crtc *crtc;
568	int i;
569
570	fb_helper->dev = dev;
571
572	INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
573
574	fb_helper->crtc_info = malloc(crtc_count *
575	    sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
576
577	fb_helper->crtc_count = crtc_count;
578	fb_helper->connector_info = malloc(dev->mode_config.num_connector *
579	    sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
580	    M_WAITOK | M_ZERO);
581	fb_helper->connector_count = 0;
582
583	for (i = 0; i < crtc_count; i++) {
584		fb_helper->crtc_info[i].mode_set.connectors =
585			malloc(max_conn_count * sizeof(struct drm_connector *),
586			    DRM_MEM_KMS, M_WAITOK | M_ZERO);
587
588		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
589	}
590
591	i = 0;
592	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
593		fb_helper->crtc_info[i].crtc_id = crtc->base.id;
594		fb_helper->crtc_info[i].mode_set.crtc = crtc;
595		i++;
596	}
597	fb_helper->conn_limit = max_conn_count;
598	return 0;
599}
600
601void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
602{
603	if (!list_empty(&fb_helper->kernel_fb_list)) {
604		list_del(&fb_helper->kernel_fb_list);
605		if (list_empty(&kernel_fb_helper_list)) {
606#if 0
607			printk(KERN_INFO "drm: unregistered panic notifier\n");
608			atomic_notifier_chain_unregister(&panic_notifier_list,
609							 &paniced);
610			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
611#endif
612		}
613	}
614
615	drm_fb_helper_crtc_free(fb_helper);
616
617}
618
619#if 0
620static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
621		     u16 blue, u16 regno, struct fb_info *info)
622{
623	struct drm_fb_helper *fb_helper = info->par;
624	struct drm_framebuffer *fb = fb_helper->fb;
625	int pindex;
626
627	if (info->fix.visual == FB_VISUAL_trueCOLOR) {
628		u32 *palette;
629		u32 value;
630		/* place color in psuedopalette */
631		if (regno > 16)
632			return -EINVAL;
633		palette = (u32 *)info->pseudo_palette;
634		red >>= (16 - info->var.red.length);
635		green >>= (16 - info->var.green.length);
636		blue >>= (16 - info->var.blue.length);
637		value = (red << info->var.red.offset) |
638			(green << info->var.green.offset) |
639			(blue << info->var.blue.offset);
640		if (info->var.transp.length > 0) {
641			u32 mask = (1 << info->var.transp.length) - 1;
642			mask <<= info->var.transp.offset;
643			value |= mask;
644		}
645		palette[regno] = value;
646		return 0;
647	}
648
649	pindex = regno;
650
651	if (fb->bits_per_pixel == 16) {
652		pindex = regno << 3;
653
654		if (fb->depth == 16 && regno > 63)
655			return -EINVAL;
656		if (fb->depth == 15 && regno > 31)
657			return -EINVAL;
658
659		if (fb->depth == 16) {
660			u16 r, g, b;
661			int i;
662			if (regno < 32) {
663				for (i = 0; i < 8; i++)
664					fb_helper->funcs->gamma_set(crtc, red,
665						green, blue, pindex + i);
666			}
667
668			fb_helper->funcs->gamma_get(crtc, &r,
669						    &g, &b,
670						    pindex >> 1);
671
672			for (i = 0; i < 4; i++)
673				fb_helper->funcs->gamma_set(crtc, r,
674							    green, b,
675							    (pindex >> 1) + i);
676		}
677	}
678
679	if (fb->depth != 16)
680		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
681	return 0;
682}
683#endif
684
685#if 0
686int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
687{
688	struct drm_fb_helper *fb_helper = info->par;
689	struct drm_crtc_helper_funcs *crtc_funcs;
690	u16 *red, *green, *blue, *transp;
691	struct drm_crtc *crtc;
692	int i, j, rc = 0;
693	int start;
694
695	for (i = 0; i < fb_helper->crtc_count; i++) {
696		crtc = fb_helper->crtc_info[i].mode_set.crtc;
697		crtc_funcs = crtc->helper_private;
698
699		red = cmap->red;
700		green = cmap->green;
701		blue = cmap->blue;
702		transp = cmap->transp;
703		start = cmap->start;
704
705		for (j = 0; j < cmap->len; j++) {
706			u16 hred, hgreen, hblue, htransp = 0xffff;
707
708			hred = *red++;
709			hgreen = *green++;
710			hblue = *blue++;
711
712			if (transp)
713				htransp = *transp++;
714
715			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
716			if (rc)
717				return rc;
718		}
719		crtc_funcs->load_lut(crtc);
720	}
721	return rc;
722}
723#endif
724
725#if 0
726int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
727			    struct fb_info *info)
728{
729	struct drm_fb_helper *fb_helper = info->par;
730	struct drm_framebuffer *fb = fb_helper->fb;
731	int depth;
732
733	if (var->pixclock != 0 || in_dbg_master())
734		return -EINVAL;
735
736	/* Need to resize the fb object !!! */
737	if (var->bits_per_pixel > fb->bits_per_pixel ||
738	    var->xres > fb->width || var->yres > fb->height ||
739	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
740		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
741			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
742			  var->xres, var->yres, var->bits_per_pixel,
743			  var->xres_virtual, var->yres_virtual,
744			  fb->width, fb->height, fb->bits_per_pixel);
745		return -EINVAL;
746	}
747
748	switch (var->bits_per_pixel) {
749	case 16:
750		depth = (var->green.length == 6) ? 16 : 15;
751		break;
752	case 32:
753		depth = (var->transp.length > 0) ? 32 : 24;
754		break;
755	default:
756		depth = var->bits_per_pixel;
757		break;
758	}
759
760	switch (depth) {
761	case 8:
762		var->red.offset = 0;
763		var->green.offset = 0;
764		var->blue.offset = 0;
765		var->red.length = 8;
766		var->green.length = 8;
767		var->blue.length = 8;
768		var->transp.length = 0;
769		var->transp.offset = 0;
770		break;
771	case 15:
772		var->red.offset = 10;
773		var->green.offset = 5;
774		var->blue.offset = 0;
775		var->red.length = 5;
776		var->green.length = 5;
777		var->blue.length = 5;
778		var->transp.length = 1;
779		var->transp.offset = 15;
780		break;
781	case 16:
782		var->red.offset = 11;
783		var->green.offset = 5;
784		var->blue.offset = 0;
785		var->red.length = 5;
786		var->green.length = 6;
787		var->blue.length = 5;
788		var->transp.length = 0;
789		var->transp.offset = 0;
790		break;
791	case 24:
792		var->red.offset = 16;
793		var->green.offset = 8;
794		var->blue.offset = 0;
795		var->red.length = 8;
796		var->green.length = 8;
797		var->blue.length = 8;
798		var->transp.length = 0;
799		var->transp.offset = 0;
800		break;
801	case 32:
802		var->red.offset = 16;
803		var->green.offset = 8;
804		var->blue.offset = 0;
805		var->red.length = 8;
806		var->green.length = 8;
807		var->blue.length = 8;
808		var->transp.length = 8;
809		var->transp.offset = 24;
810		break;
811	default:
812		return -EINVAL;
813	}
814	return 0;
815}
816#endif
817
818#if 0
819/* this will let fbcon do the mode init */
820int drm_fb_helper_set_par(struct fb_info *info)
821{
822	struct drm_fb_helper *fb_helper = info->par;
823	struct drm_device *dev = fb_helper->dev;
824	struct fb_var_screeninfo *var = &info->var;
825	struct drm_crtc *crtc;
826	int ret;
827	int i;
828
829	if (var->pixclock != 0) {
830		DRM_ERROR("PIXEL CLOCK SET\n");
831		return -EINVAL;
832	}
833
834	mutex_lock(&dev->mode_config.mutex);
835	for (i = 0; i < fb_helper->crtc_count; i++) {
836		crtc = fb_helper->crtc_info[i].mode_set.crtc;
837		ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
838		if (ret) {
839			mutex_unlock(&dev->mode_config.mutex);
840			return ret;
841		}
842	}
843	mutex_unlock(&dev->mode_config.mutex);
844
845	if (fb_helper->delayed_hotplug) {
846		fb_helper->delayed_hotplug = false;
847		drm_fb_helper_hotplug_event(fb_helper);
848	}
849	return 0;
850}
851#endif
852
853#if 0
854int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
855			      struct fb_info *info)
856{
857	struct drm_fb_helper *fb_helper = info->par;
858	struct drm_device *dev = fb_helper->dev;
859	struct drm_mode_set *modeset;
860	struct drm_crtc *crtc;
861	int ret = 0;
862	int i;
863
864	mutex_lock(&dev->mode_config.mutex);
865	for (i = 0; i < fb_helper->crtc_count; i++) {
866		crtc = fb_helper->crtc_info[i].mode_set.crtc;
867
868		modeset = &fb_helper->crtc_info[i].mode_set;
869
870		modeset->x = var->xoffset;
871		modeset->y = var->yoffset;
872
873		if (modeset->num_connectors) {
874			ret = crtc->funcs->set_config(modeset);
875			if (!ret) {
876				info->var.xoffset = var->xoffset;
877				info->var.yoffset = var->yoffset;
878			}
879		}
880	}
881	mutex_unlock(&dev->mode_config.mutex);
882	return ret;
883}
884#endif
885
886int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
887				  int preferred_bpp)
888{
889	int new_fb = 0;
890	int crtc_count = 0;
891	int i;
892#if 0
893	struct fb_info *info;
894#endif
895	struct drm_fb_helper_surface_size sizes;
896	int gamma_size = 0;
897
898	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
899	sizes.surface_depth = 24;
900	sizes.surface_bpp = 32;
901	sizes.fb_width = (unsigned)-1;
902	sizes.fb_height = (unsigned)-1;
903
904	/* if driver picks 8 or 16 by default use that
905	   for both depth/bpp */
906	if (preferred_bpp != sizes.surface_bpp) {
907		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
908	}
909	/* first up get a count of crtcs now in use and new min/maxes width/heights */
910	for (i = 0; i < fb_helper->connector_count; i++) {
911		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
912		struct drm_fb_helper_cmdline_mode *cmdline_mode;
913
914		cmdline_mode = &fb_helper_conn->cmdline_mode;
915
916		if (cmdline_mode->bpp_specified) {
917			switch (cmdline_mode->bpp) {
918			case 8:
919				sizes.surface_depth = sizes.surface_bpp = 8;
920				break;
921			case 15:
922				sizes.surface_depth = 15;
923				sizes.surface_bpp = 16;
924				break;
925			case 16:
926				sizes.surface_depth = sizes.surface_bpp = 16;
927				break;
928			case 24:
929				sizes.surface_depth = sizes.surface_bpp = 24;
930				break;
931			case 32:
932				sizes.surface_depth = 24;
933				sizes.surface_bpp = 32;
934				break;
935			}
936			break;
937		}
938	}
939
940	crtc_count = 0;
941	for (i = 0; i < fb_helper->crtc_count; i++) {
942		struct drm_display_mode *desired_mode;
943		desired_mode = fb_helper->crtc_info[i].desired_mode;
944
945		if (desired_mode) {
946			if (gamma_size == 0)
947				gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
948			if (desired_mode->hdisplay < sizes.fb_width)
949				sizes.fb_width = desired_mode->hdisplay;
950			if (desired_mode->vdisplay < sizes.fb_height)
951				sizes.fb_height = desired_mode->vdisplay;
952			if (desired_mode->hdisplay > sizes.surface_width)
953				sizes.surface_width = desired_mode->hdisplay;
954			if (desired_mode->vdisplay > sizes.surface_height)
955				sizes.surface_height = desired_mode->vdisplay;
956			crtc_count++;
957		}
958	}
959
960	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
961		/* hmm everyone went away - assume VGA cable just fell out
962		   and will come back later. */
963		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
964		sizes.fb_width = sizes.surface_width = 1024;
965		sizes.fb_height = sizes.surface_height = 768;
966	}
967
968	/* push down into drivers */
969	new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
970	if (new_fb < 0)
971		return new_fb;
972
973#if 0
974	info = fb_helper->fbdev;
975#endif
976
977	/* set the fb pointer */
978	for (i = 0; i < fb_helper->crtc_count; i++) {
979		fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
980	}
981
982#if 0
983	if (new_fb) {
984		info->var.pixclock = 0;
985		if (register_framebuffer(info) < 0) {
986			return -EINVAL;
987		}
988
989		printf("fb%d: %s frame buffer device\n", info->node,
990		       info->fix.id);
991
992	} else {
993		drm_fb_helper_set_par(info);
994	}
995
996	/* Switch back to kernel console on panic */
997	/* multi card linked list maybe */
998	if (list_empty(&kernel_fb_helper_list)) {
999		printf("drm: registered panic notifier\n");
1000		atomic_notifier_chain_register(&panic_notifier_list,
1001					       &paniced);
1002	}
1003	if (new_fb)
1004		list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1005#endif
1006
1007	return 0;
1008}
1009
1010#if 0
1011void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1012			    uint32_t depth)
1013{
1014	info->fix.type = FB_TYPE_PACKED_PIXELS;
1015	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1016		FB_VISUAL_trueCOLOR;
1017	info->fix.mmio_start = 0;
1018	info->fix.mmio_len = 0;
1019	info->fix.type_aux = 0;
1020	info->fix.xpanstep = 1; /* doing it in hw */
1021	info->fix.ypanstep = 1; /* doing it in hw */
1022	info->fix.ywrapstep = 0;
1023	info->fix.accel = FB_ACCEL_NONE;
1024	info->fix.type_aux = 0;
1025
1026	info->fix.line_length = pitch;
1027	return;
1028}
1029
1030void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1031			    uint32_t fb_width, uint32_t fb_height)
1032{
1033	struct drm_framebuffer *fb = fb_helper->fb;
1034	info->pseudo_palette = fb_helper->pseudo_palette;
1035	info->var.xres_virtual = fb->width;
1036	info->var.yres_virtual = fb->height;
1037	info->var.bits_per_pixel = fb->bits_per_pixel;
1038	info->var.accel_flags = FB_ACCELF_TEXT;
1039	info->var.xoffset = 0;
1040	info->var.yoffset = 0;
1041	info->var.activate = FB_ACTIVATE_NOW;
1042	info->var.height = -1;
1043	info->var.width = -1;
1044
1045	switch (fb->depth) {
1046	case 8:
1047		info->var.red.offset = 0;
1048		info->var.green.offset = 0;
1049		info->var.blue.offset = 0;
1050		info->var.red.length = 8; /* 8bit DAC */
1051		info->var.green.length = 8;
1052		info->var.blue.length = 8;
1053		info->var.transp.offset = 0;
1054		info->var.transp.length = 0;
1055		break;
1056	case 15:
1057		info->var.red.offset = 10;
1058		info->var.green.offset = 5;
1059		info->var.blue.offset = 0;
1060		info->var.red.length = 5;
1061		info->var.green.length = 5;
1062		info->var.blue.length = 5;
1063		info->var.transp.offset = 15;
1064		info->var.transp.length = 1;
1065		break;
1066	case 16:
1067		info->var.red.offset = 11;
1068		info->var.green.offset = 5;
1069		info->var.blue.offset = 0;
1070		info->var.red.length = 5;
1071		info->var.green.length = 6;
1072		info->var.blue.length = 5;
1073		info->var.transp.offset = 0;
1074		break;
1075	case 24:
1076		info->var.red.offset = 16;
1077		info->var.green.offset = 8;
1078		info->var.blue.offset = 0;
1079		info->var.red.length = 8;
1080		info->var.green.length = 8;
1081		info->var.blue.length = 8;
1082		info->var.transp.offset = 0;
1083		info->var.transp.length = 0;
1084		break;
1085	case 32:
1086		info->var.red.offset = 16;
1087		info->var.green.offset = 8;
1088		info->var.blue.offset = 0;
1089		info->var.red.length = 8;
1090		info->var.green.length = 8;
1091		info->var.blue.length = 8;
1092		info->var.transp.offset = 24;
1093		info->var.transp.length = 8;
1094		break;
1095	default:
1096		break;
1097	}
1098
1099	info->var.xres = fb_width;
1100	info->var.yres = fb_height;
1101}
1102#endif
1103
1104static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1105					       uint32_t maxX,
1106					       uint32_t maxY)
1107{
1108	struct drm_connector *connector;
1109	int count = 0;
1110	int i;
1111
1112	for (i = 0; i < fb_helper->connector_count; i++) {
1113		connector = fb_helper->connector_info[i]->connector;
1114		count += connector->funcs->fill_modes(connector, maxX, maxY);
1115	}
1116
1117	return count;
1118}
1119
1120static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1121{
1122	struct drm_display_mode *mode;
1123
1124	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1125		if (drm_mode_width(mode) > width ||
1126		    drm_mode_height(mode) > height)
1127			continue;
1128		if (mode->type & DRM_MODE_TYPE_PREFERRED)
1129			return mode;
1130	}
1131	return NULL;
1132}
1133
1134static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1135{
1136	struct drm_fb_helper_cmdline_mode *cmdline_mode;
1137	cmdline_mode = &fb_connector->cmdline_mode;
1138	return cmdline_mode->specified;
1139}
1140
1141static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1142						      int width, int height)
1143{
1144	struct drm_cmdline_mode *cmdline_mode;
1145	struct drm_display_mode *mode = NULL;
1146
1147	cmdline_mode = &fb_helper_conn->cmdline_mode1;
1148	if (cmdline_mode->specified == false &&
1149	    !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
1150	    cmdline_mode))
1151			return (NULL);
1152
1153	/* attempt to find a matching mode in the list of modes
1154	 *  we have gotten so far, if not add a CVT mode that conforms
1155	 */
1156	if (cmdline_mode->rb || cmdline_mode->margins)
1157		goto create_mode;
1158
1159	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1160		/* check width/height */
1161		if (mode->hdisplay != cmdline_mode->xres ||
1162		    mode->vdisplay != cmdline_mode->yres)
1163			continue;
1164
1165		if (cmdline_mode->refresh_specified) {
1166			if (mode->vrefresh != cmdline_mode->refresh)
1167				continue;
1168		}
1169
1170		if (cmdline_mode->interlace) {
1171			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1172				continue;
1173		}
1174		return mode;
1175	}
1176
1177create_mode:
1178	if (cmdline_mode->cvt)
1179		mode = drm_cvt_mode(fb_helper_conn->connector->dev,
1180				    cmdline_mode->xres, cmdline_mode->yres,
1181				    cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1182				    cmdline_mode->rb, cmdline_mode->interlace,
1183				    cmdline_mode->margins);
1184	else
1185		mode = drm_gtf_mode(fb_helper_conn->connector->dev,
1186				    cmdline_mode->xres, cmdline_mode->yres,
1187				    cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
1188				    cmdline_mode->interlace,
1189				    cmdline_mode->margins);
1190	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1191	list_add(&mode->head, &fb_helper_conn->connector->modes);
1192	return mode;
1193}
1194
1195static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1196{
1197	bool enable;
1198
1199	if (strict) {
1200		enable = connector->status == connector_status_connected;
1201	} else {
1202		enable = connector->status != connector_status_disconnected;
1203	}
1204	return enable;
1205}
1206
1207static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1208				  bool *enabled)
1209{
1210	bool any_enabled = false;
1211	struct drm_connector *connector;
1212	int i = 0;
1213
1214	for (i = 0; i < fb_helper->connector_count; i++) {
1215		connector = fb_helper->connector_info[i]->connector;
1216		enabled[i] = drm_connector_enabled(connector, true);
1217		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1218			  enabled[i] ? "yes" : "no");
1219		any_enabled |= enabled[i];
1220	}
1221
1222	if (any_enabled)
1223		return;
1224
1225	for (i = 0; i < fb_helper->connector_count; i++) {
1226		connector = fb_helper->connector_info[i]->connector;
1227		enabled[i] = drm_connector_enabled(connector, false);
1228	}
1229}
1230
1231static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1232			      struct drm_display_mode **modes,
1233			      bool *enabled, int width, int height)
1234{
1235	int count, i, j;
1236	bool can_clone = false;
1237	struct drm_fb_helper_connector *fb_helper_conn;
1238	struct drm_display_mode *dmt_mode, *mode;
1239
1240	/* only contemplate cloning in the single crtc case */
1241	if (fb_helper->crtc_count > 1)
1242		return false;
1243
1244	count = 0;
1245	for (i = 0; i < fb_helper->connector_count; i++) {
1246		if (enabled[i])
1247			count++;
1248	}
1249
1250	/* only contemplate cloning if more than one connector is enabled */
1251	if (count <= 1)
1252		return false;
1253
1254	/* check the command line or if nothing common pick 1024x768 */
1255	can_clone = true;
1256	for (i = 0; i < fb_helper->connector_count; i++) {
1257		if (!enabled[i])
1258			continue;
1259		fb_helper_conn = fb_helper->connector_info[i];
1260		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1261		if (!modes[i]) {
1262			can_clone = false;
1263			break;
1264		}
1265		for (j = 0; j < i; j++) {
1266			if (!enabled[j])
1267				continue;
1268			if (!drm_mode_equal(modes[j], modes[i]))
1269				can_clone = false;
1270		}
1271	}
1272
1273	if (can_clone) {
1274		DRM_DEBUG_KMS("can clone using command line\n");
1275		return true;
1276	}
1277
1278	/* try and find a 1024x768 mode on each connector */
1279	can_clone = true;
1280	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
1281
1282	for (i = 0; i < fb_helper->connector_count; i++) {
1283
1284		if (!enabled[i])
1285			continue;
1286
1287		fb_helper_conn = fb_helper->connector_info[i];
1288		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1289			if (drm_mode_equal(mode, dmt_mode))
1290				modes[i] = mode;
1291		}
1292		if (!modes[i])
1293			can_clone = false;
1294	}
1295
1296	if (can_clone) {
1297		DRM_DEBUG_KMS("can clone using 1024x768\n");
1298		return true;
1299	}
1300	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1301	return false;
1302}
1303
1304static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1305				 struct drm_display_mode **modes,
1306				 bool *enabled, int width, int height)
1307{
1308	struct drm_fb_helper_connector *fb_helper_conn;
1309	int i;
1310
1311	for (i = 0; i < fb_helper->connector_count; i++) {
1312		fb_helper_conn = fb_helper->connector_info[i];
1313
1314		if (enabled[i] == false)
1315			continue;
1316
1317		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1318			      fb_helper_conn->connector->base.id);
1319
1320		/* got for command line mode first */
1321		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1322		if (!modes[i]) {
1323			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1324				      fb_helper_conn->connector->base.id);
1325			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1326		}
1327		/* No preferred modes, pick one off the list */
1328		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1329			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
1330				break;
1331		}
1332		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1333			  "none");
1334	}
1335	return true;
1336}
1337
1338static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1339			  struct drm_fb_helper_crtc **best_crtcs,
1340			  struct drm_display_mode **modes,
1341			  int n, int width, int height)
1342{
1343	int c, o;
1344	struct drm_device *dev = fb_helper->dev;
1345	struct drm_connector *connector;
1346	struct drm_connector_helper_funcs *connector_funcs;
1347	struct drm_encoder *encoder;
1348	struct drm_fb_helper_crtc *best_crtc;
1349	int my_score, best_score, score;
1350	struct drm_fb_helper_crtc **crtcs, *crtc;
1351	struct drm_fb_helper_connector *fb_helper_conn;
1352
1353	if (n == fb_helper->connector_count)
1354		return 0;
1355
1356	fb_helper_conn = fb_helper->connector_info[n];
1357	connector = fb_helper_conn->connector;
1358
1359	best_crtcs[n] = NULL;
1360	best_crtc = NULL;
1361	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1362	if (modes[n] == NULL)
1363		return best_score;
1364
1365	crtcs = malloc(dev->mode_config.num_connector *
1366	    sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1367	    M_WAITOK | M_ZERO);
1368
1369	my_score = 1;
1370	if (connector->status == connector_status_connected)
1371		my_score++;
1372	if (drm_has_cmdline_mode(fb_helper_conn))
1373		my_score++;
1374	if (drm_has_preferred_mode(fb_helper_conn, width, height))
1375		my_score++;
1376
1377	connector_funcs = connector->helper_private;
1378	encoder = connector_funcs->best_encoder(connector);
1379	if (!encoder)
1380		goto out;
1381
1382	/* select a crtc for this connector and then attempt to configure
1383	   remaining connectors */
1384	for (c = 0; c < fb_helper->crtc_count; c++) {
1385		crtc = &fb_helper->crtc_info[c];
1386
1387		if ((encoder->possible_crtcs & (1 << c)) == 0) {
1388			continue;
1389		}
1390
1391		for (o = 0; o < n; o++)
1392			if (best_crtcs[o] == crtc)
1393				break;
1394
1395		if (o < n) {
1396			/* ignore cloning unless only a single crtc */
1397			if (fb_helper->crtc_count > 1)
1398				continue;
1399
1400			if (!drm_mode_equal(modes[o], modes[n]))
1401				continue;
1402		}
1403
1404		crtcs[n] = crtc;
1405		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1406		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1407						  width, height);
1408		if (score > best_score) {
1409			best_crtc = crtc;
1410			best_score = score;
1411			memcpy(best_crtcs, crtcs,
1412			       dev->mode_config.num_connector *
1413			       sizeof(struct drm_fb_helper_crtc *));
1414		}
1415	}
1416out:
1417	free(crtcs, DRM_MEM_KMS);
1418	return best_score;
1419}
1420
1421static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1422{
1423	struct drm_device *dev = fb_helper->dev;
1424	struct drm_fb_helper_crtc **crtcs;
1425	struct drm_display_mode **modes;
1426	struct drm_encoder *encoder;
1427	struct drm_mode_set *modeset;
1428	bool *enabled;
1429	int width, height;
1430	int i, ret;
1431
1432	DRM_DEBUG_KMS("\n");
1433
1434	width = dev->mode_config.max_width;
1435	height = dev->mode_config.max_height;
1436
1437	/* clean out all the encoder/crtc combos */
1438	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1439		encoder->crtc = NULL;
1440	}
1441
1442	crtcs = malloc(dev->mode_config.num_connector *
1443	    sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
1444	    M_WAITOK | M_ZERO);
1445	modes = malloc(dev->mode_config.num_connector *
1446	    sizeof(struct drm_display_mode *), DRM_MEM_KMS,
1447	    M_WAITOK | M_ZERO);
1448	enabled = malloc(dev->mode_config.num_connector *
1449	    sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
1450
1451	drm_enable_connectors(fb_helper, enabled);
1452
1453	ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
1454	if (!ret) {
1455		ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
1456		if (!ret)
1457			DRM_ERROR("Unable to find initial modes\n");
1458	}
1459
1460	DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
1461
1462	drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1463
1464	/* need to set the modesets up here for use later */
1465	/* fill out the connector<->crtc mappings into the modesets */
1466	for (i = 0; i < fb_helper->crtc_count; i++) {
1467		modeset = &fb_helper->crtc_info[i].mode_set;
1468		modeset->num_connectors = 0;
1469	}
1470
1471	for (i = 0; i < fb_helper->connector_count; i++) {
1472		struct drm_display_mode *mode = modes[i];
1473		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1474		modeset = &fb_crtc->mode_set;
1475
1476		if (mode && fb_crtc) {
1477			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1478				      mode->name, fb_crtc->mode_set.crtc->base.id);
1479			fb_crtc->desired_mode = mode;
1480			if (modeset->mode)
1481				drm_mode_destroy(dev, modeset->mode);
1482			modeset->mode = drm_mode_duplicate(dev,
1483							   fb_crtc->desired_mode);
1484			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1485		}
1486	}
1487
1488	free(crtcs, DRM_MEM_KMS);
1489	free(modes, DRM_MEM_KMS);
1490	free(enabled, DRM_MEM_KMS);
1491}
1492
1493/**
1494 * drm_helper_initial_config - setup a sane initial connector configuration
1495 * @dev: DRM device
1496 *
1497 * LOCKING:
1498 * Called at init time, must take mode config lock.
1499 *
1500 * Scan the CRTCs and connectors and try to put together an initial setup.
1501 * At the moment, this is a cloned configuration across all heads with
1502 * a new framebuffer object as the backing store.
1503 *
1504 * RETURNS:
1505 * Zero if everything went ok, nonzero otherwise.
1506 */
1507bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1508{
1509	struct drm_device *dev = fb_helper->dev;
1510	int count = 0;
1511
1512	/* disable all the possible outputs/crtcs before entering KMS mode */
1513	drm_helper_disable_unused_functions(fb_helper->dev);
1514
1515	drm_fb_helper_parse_command_line(fb_helper);
1516
1517	count = drm_fb_helper_probe_connector_modes(fb_helper,
1518						    dev->mode_config.max_width,
1519						    dev->mode_config.max_height);
1520	/*
1521	 * we shouldn't end up with no modes here.
1522	 */
1523	if (count == 0) {
1524		printf("No connectors reported connected with modes\n");
1525	}
1526	drm_setup_crtcs(fb_helper);
1527
1528	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1529}
1530
1531int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1532{
1533	struct drm_device *dev = fb_helper->dev;
1534	int count = 0;
1535	u32 max_width, max_height, bpp_sel;
1536	bool bound = false, crtcs_bound = false;
1537	struct drm_crtc *crtc;
1538
1539	if (!fb_helper->fb)
1540		return 0;
1541
1542	sx_xlock(&dev->mode_config.mutex);
1543	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1544		if (crtc->fb)
1545			crtcs_bound = true;
1546		if (crtc->fb == fb_helper->fb)
1547			bound = true;
1548	}
1549
1550	if (!bound && crtcs_bound) {
1551		fb_helper->delayed_hotplug = true;
1552		sx_xunlock(&dev->mode_config.mutex);
1553		return 0;
1554	}
1555	DRM_DEBUG_KMS("\n");
1556
1557	max_width = fb_helper->fb->width;
1558	max_height = fb_helper->fb->height;
1559	bpp_sel = fb_helper->fb->bits_per_pixel;
1560
1561	count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1562						    max_height);
1563	drm_setup_crtcs(fb_helper);
1564	sx_xunlock(&dev->mode_config.mutex);
1565
1566	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1567}
1568
1569