xref: /dragonfly/sys/dev/drm/drm_fb_helper.c (revision de5eb4c5)
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 <linux/kernel.h>
32 #include <linux/fb.h>
33 #include <linux/module.h>
34 #include <drm/drmP.h>
35 #include <drm/drm_crtc.h>
36 #include <drm/drm_fb_helper.h>
37 #include <drm/drm_crtc_helper.h>
38 #include <drm/drm_atomic.h>
39 #include <drm/drm_atomic_helper.h>
40 
41 static bool drm_fbdev_emulation = true;
42 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
43 MODULE_PARM_DESC(fbdev_emulation,
44 		 "Enable legacy fbdev emulation [default=true]");
45 
46 static LINUX_LIST_HEAD(kernel_fb_helper_list);
47 
48 /**
49  * DOC: fbdev helpers
50  *
51  * The fb helper functions are useful to provide an fbdev on top of a drm kernel
52  * mode setting driver. They can be used mostly independently from the crtc
53  * helper functions used by many drivers to implement the kernel mode setting
54  * interfaces.
55  *
56  * Initialization is done as a four-step process with drm_fb_helper_prepare(),
57  * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
58  * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
59  * default behaviour can override the third step with their own code.
60  * Teardown is done with drm_fb_helper_fini().
61  *
62  * At runtime drivers should restore the fbdev console by calling
63  * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback.
64  * They should also notify the fb helper code from updates to the output
65  * configuration by calling drm_fb_helper_hotplug_event(). For easier
66  * integration with the output polling code in drm_crtc_helper.c the modeset
67  * code provides a ->output_poll_changed callback.
68  *
69  * All other functions exported by the fb helper library can be used to
70  * implement the fbdev driver interface by the driver.
71  *
72  * It is possible, though perhaps somewhat tricky, to implement race-free
73  * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
74  * helper must be called first to initialize the minimum required to make
75  * hotplug detection work. Drivers also need to make sure to properly set up
76  * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init()
77  * it is safe to enable interrupts and start processing hotplug events. At the
78  * same time, drivers should initialize all modeset objects such as CRTCs,
79  * encoders and connectors. To finish up the fbdev helper initialization, the
80  * drm_fb_helper_init() function is called. To probe for all attached displays
81  * and set up an initial configuration using the detected hardware, drivers
82  * should call drm_fb_helper_single_add_all_connectors() followed by
83  * drm_fb_helper_initial_config().
84  *
85  * If &drm_framebuffer_funcs ->dirty is set, the
86  * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
87  * accumulate changes and schedule &drm_fb_helper ->dirty_work to run right
88  * away. This worker then calls the dirty() function ensuring that it will
89  * always run in process context since the fb_*() function could be running in
90  * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
91  * callback it will also schedule dirty_work with the damage collected from the
92  * mmap page writes.
93  */
94 
95 /**
96  * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
97  * 					       emulation helper
98  * @fb_helper: fbdev initialized with drm_fb_helper_init
99  *
100  * This functions adds all the available connectors for use with the given
101  * fb_helper. This is a separate step to allow drivers to freely assign
102  * connectors to the fbdev, e.g. if some are reserved for special purposes or
103  * not adequate to be used for the fbcon.
104  *
105  * This function is protected against concurrent connector hotadds/removals
106  * using drm_fb_helper_add_one_connector() and
107  * drm_fb_helper_remove_one_connector().
108  */
109 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
110 {
111 	struct drm_device *dev = fb_helper->dev;
112 	struct drm_connector *connector;
113 	int i, ret;
114 
115 	if (!drm_fbdev_emulation)
116 		return 0;
117 
118 	mutex_lock(&dev->mode_config.mutex);
119 	drm_for_each_connector(connector, dev) {
120 		ret = drm_fb_helper_add_one_connector(fb_helper, connector);
121 
122 		if (ret)
123 			goto fail;
124 	}
125 	mutex_unlock(&dev->mode_config.mutex);
126 	return 0;
127 fail:
128 	for (i = 0; i < fb_helper->connector_count; i++) {
129 		kfree(fb_helper->connector_info[i]);
130 		fb_helper->connector_info[i] = NULL;
131 	}
132 	fb_helper->connector_count = 0;
133 	mutex_unlock(&dev->mode_config.mutex);
134 
135 	return ret;
136 }
137 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
138 
139 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector)
140 {
141 	struct drm_fb_helper_connector **temp;
142 	struct drm_fb_helper_connector *fb_helper_connector;
143 
144 	if (!drm_fbdev_emulation)
145 		return 0;
146 
147 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
148 	if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) {
149 		temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK);
150 		if (!temp)
151 			return -ENOMEM;
152 
153 		fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1;
154 		fb_helper->connector_info = temp;
155 	}
156 
157 
158 	fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
159 	if (!fb_helper_connector)
160 		return -ENOMEM;
161 
162 	drm_connector_reference(connector);
163 	fb_helper_connector->connector = connector;
164 	fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
165 	return 0;
166 }
167 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
168 
169 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
170 				       struct drm_connector *connector)
171 {
172 	struct drm_fb_helper_connector *fb_helper_connector;
173 	int i, j;
174 
175 	if (!drm_fbdev_emulation)
176 		return 0;
177 
178 	WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
179 
180 	for (i = 0; i < fb_helper->connector_count; i++) {
181 		if (fb_helper->connector_info[i]->connector == connector)
182 			break;
183 	}
184 
185 	if (i == fb_helper->connector_count)
186 		return -EINVAL;
187 	fb_helper_connector = fb_helper->connector_info[i];
188 	drm_connector_unreference(fb_helper_connector->connector);
189 
190 	for (j = i + 1; j < fb_helper->connector_count; j++) {
191 		fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
192 	}
193 	fb_helper->connector_count--;
194 	kfree(fb_helper_connector);
195 
196 	return 0;
197 }
198 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
199 
200 #if 0
201 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
202 {
203 	uint16_t *r_base, *g_base, *b_base;
204 	int i;
205 
206 	if (helper->funcs->gamma_get == NULL)
207 		return;
208 
209 	r_base = crtc->gamma_store;
210 	g_base = r_base + crtc->gamma_size;
211 	b_base = g_base + crtc->gamma_size;
212 
213 	for (i = 0; i < crtc->gamma_size; i++)
214 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
215 }
216 
217 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
218 {
219 	uint16_t *r_base, *g_base, *b_base;
220 
221 	if (crtc->funcs->gamma_set == NULL)
222 		return;
223 
224 	r_base = crtc->gamma_store;
225 	g_base = r_base + crtc->gamma_size;
226 	b_base = g_base + crtc->gamma_size;
227 
228 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
229 }
230 #endif
231 
232 /**
233  * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter
234  * @info: fbdev registered by the helper
235  */
236 int drm_fb_helper_debug_enter(struct fb_info *info)
237 {
238 	struct drm_fb_helper *helper = info->par;
239 	const struct drm_crtc_helper_funcs *funcs;
240 	int i;
241 
242 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
243 		for (i = 0; i < helper->crtc_count; i++) {
244 			struct drm_mode_set *mode_set =
245 				&helper->crtc_info[i].mode_set;
246 
247 			if (!mode_set->crtc->enabled)
248 				continue;
249 
250 			funcs =	mode_set->crtc->helper_private;
251 #if 0
252 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
253 #endif
254 			funcs->mode_set_base_atomic(mode_set->crtc,
255 						    mode_set->fb,
256 						    mode_set->x,
257 						    mode_set->y,
258 						    ENTER_ATOMIC_MODE_SET);
259 		}
260 	}
261 
262 	return 0;
263 }
264 EXPORT_SYMBOL(drm_fb_helper_debug_enter);
265 
266 #if 0
267 /* Find the real fb for a given fb helper CRTC */
268 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
269 {
270 	struct drm_device *dev = crtc->dev;
271 	struct drm_crtc *c;
272 
273 	drm_for_each_crtc(c, dev) {
274 		if (crtc->base.id == c->base.id)
275 			return c->primary->fb;
276 	}
277 
278 	return NULL;
279 }
280 
281 /**
282  * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave
283  * @info: fbdev registered by the helper
284  */
285 int drm_fb_helper_debug_leave(struct fb_info *info)
286 {
287 	struct drm_fb_helper *helper = info->par;
288 	struct drm_crtc *crtc;
289 	const struct drm_crtc_helper_funcs *funcs;
290 	struct drm_framebuffer *fb;
291 	int i;
292 
293 	for (i = 0; i < helper->crtc_count; i++) {
294 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
295 		crtc = mode_set->crtc;
296 		funcs = crtc->helper_private;
297 		fb = drm_mode_config_fb(crtc);
298 
299 		if (!crtc->enabled)
300 			continue;
301 
302 		if (!fb) {
303 			DRM_ERROR("no fb to restore??\n");
304 			continue;
305 		}
306 
307 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
308 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
309 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
310 	}
311 
312 	return 0;
313 }
314 EXPORT_SYMBOL(drm_fb_helper_debug_leave);
315 #endif
316 
317 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
318 {
319 	struct drm_device *dev = fb_helper->dev;
320 	struct drm_plane *plane;
321 	struct drm_atomic_state *state;
322 	int i, ret;
323 	unsigned plane_mask;
324 
325 	state = drm_atomic_state_alloc(dev);
326 	if (!state)
327 		return -ENOMEM;
328 
329 	state->acquire_ctx = dev->mode_config.acquire_ctx;
330 retry:
331 	plane_mask = 0;
332 	drm_for_each_plane(plane, dev) {
333 		struct drm_plane_state *plane_state;
334 
335 		plane_state = drm_atomic_get_plane_state(state, plane);
336 		if (IS_ERR(plane_state)) {
337 			ret = PTR_ERR(plane_state);
338 			goto fail;
339 		}
340 
341 		plane_state->rotation = BIT(DRM_ROTATE_0);
342 
343 		plane->old_fb = plane->fb;
344 		plane_mask |= 1 << drm_plane_index(plane);
345 
346 		/* disable non-primary: */
347 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
348 			continue;
349 
350 		ret = __drm_atomic_helper_disable_plane(plane, plane_state);
351 		if (ret != 0)
352 			goto fail;
353 	}
354 
355 	for(i = 0; i < fb_helper->crtc_count; i++) {
356 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
357 
358 		ret = __drm_atomic_helper_set_config(mode_set, state);
359 		if (ret != 0)
360 			goto fail;
361 	}
362 
363 	ret = drm_atomic_commit(state);
364 
365 fail:
366 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
367 
368 	if (ret == -EDEADLK)
369 		goto backoff;
370 
371 	if (ret != 0)
372 		drm_atomic_state_free(state);
373 
374 	return ret;
375 
376 backoff:
377 	drm_atomic_state_clear(state);
378 	drm_atomic_legacy_backoff(state);
379 
380 	goto retry;
381 }
382 
383 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper)
384 {
385 	struct drm_device *dev = fb_helper->dev;
386 	struct drm_plane *plane;
387 	bool error = false;
388 	int i;
389 
390 	drm_warn_on_modeset_not_all_locked(dev);
391 
392 	if (fb_helper->atomic)
393 		return restore_fbdev_mode_atomic(fb_helper);
394 
395 	drm_for_each_plane(plane, dev) {
396 		if (plane->type != DRM_PLANE_TYPE_PRIMARY)
397 			drm_plane_force_disable(plane);
398 
399 		if (dev->mode_config.rotation_property) {
400 			drm_mode_plane_set_obj_prop(plane,
401 						    dev->mode_config.rotation_property,
402 						    BIT(DRM_ROTATE_0));
403 		}
404 	}
405 
406 	for (i = 0; i < fb_helper->crtc_count; i++) {
407 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
408 		struct drm_crtc *crtc = mode_set->crtc;
409 		int ret;
410 
411 		if (crtc->funcs->cursor_set) {
412 			ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
413 			if (ret)
414 				error = true;
415 		}
416 
417 		ret = drm_mode_set_config_internal(mode_set);
418 		if (ret)
419 			error = true;
420 	}
421 	return error;
422 }
423 
424 /**
425  * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
426  * @fb_helper: fbcon to restore
427  *
428  * This should be called from driver's drm ->lastclose callback
429  * when implementing an fbcon on top of kms using this helper. This ensures that
430  * the user isn't greeted with a black screen when e.g. X dies.
431  *
432  * RETURNS:
433  * Zero if everything went ok, negative error code otherwise.
434  */
435 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
436 {
437 	struct drm_device *dev = fb_helper->dev;
438 	bool do_delayed;
439 	int ret;
440 
441 	if (!drm_fbdev_emulation)
442 		return -ENODEV;
443 
444 	drm_modeset_lock_all(dev);
445 	ret = restore_fbdev_mode(fb_helper);
446 
447 	do_delayed = fb_helper->delayed_hotplug;
448 	if (do_delayed)
449 		fb_helper->delayed_hotplug = false;
450 	drm_modeset_unlock_all(dev);
451 
452 	if (do_delayed)
453 		drm_fb_helper_hotplug_event(fb_helper);
454 	return ret;
455 }
456 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
457 
458 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
459 {
460 	struct drm_device *dev = fb_helper->dev;
461 	struct drm_crtc *crtc;
462 	int bound = 0, crtcs_bound = 0;
463 
464 	/* Sometimes user space wants everything disabled, so don't steal the
465 	 * display if there's a master. */
466 #if 0
467 	if (dev->primary->master)
468 		return false;
469 #endif
470 
471 	drm_for_each_crtc(crtc, dev) {
472 		if (crtc->primary->fb)
473 			crtcs_bound++;
474 		if (crtc->primary->fb == fb_helper->fb)
475 			bound++;
476 	}
477 
478 	if (bound < crtcs_bound)
479 		return false;
480 
481 	return true;
482 }
483 
484 #ifdef CONFIG_MAGIC_SYSRQ
485 /*
486  * restore fbcon display for all kms driver's using this helper, used for sysrq
487  * and panic handling.
488  */
489 static bool drm_fb_helper_force_kernel_mode(void)
490 {
491 	bool ret, error = false;
492 	struct drm_fb_helper *helper;
493 
494 	if (list_empty(&kernel_fb_helper_list))
495 		return false;
496 
497 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
498 		struct drm_device *dev = helper->dev;
499 
500 		if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
501 			continue;
502 
503 		drm_modeset_lock_all(dev);
504 		ret = restore_fbdev_mode(helper);
505 		if (ret)
506 			error = true;
507 		drm_modeset_unlock_all(dev);
508 	}
509 	return error;
510 }
511 
512 #if 0
513 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
514 {
515 	bool ret;
516 	ret = drm_fb_helper_force_kernel_mode();
517 	if (ret == true)
518 		DRM_ERROR("Failed to restore crtc configuration\n");
519 }
520 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
521 
522 static void drm_fb_helper_sysrq(int dummy1)
523 {
524 	schedule_work(&drm_fb_helper_restore_work);
525 }
526 
527 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
528 	.handler = drm_fb_helper_sysrq,
529 	.help_msg = "force-fb(V)",
530 	.action_msg = "Restore framebuffer console",
531 };
532 #else
533 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
534 #endif
535 #endif
536 
537 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
538 {
539 	struct drm_fb_helper *fb_helper = info->par;
540 	struct drm_device *dev = fb_helper->dev;
541 	struct drm_crtc *crtc;
542 	struct drm_connector *connector;
543 	int i, j;
544 
545 	/*
546 	 * For each CRTC in this fb, turn the connectors on/off.
547 	 */
548 	drm_modeset_lock_all(dev);
549 	if (!drm_fb_helper_is_bound(fb_helper)) {
550 		drm_modeset_unlock_all(dev);
551 		return;
552 	}
553 
554 	for (i = 0; i < fb_helper->crtc_count; i++) {
555 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
556 
557 		if (!crtc->enabled)
558 			continue;
559 
560 		/* Walk the connectors & encoders on this fb turning them on/off */
561 		for (j = 0; j < fb_helper->connector_count; j++) {
562 			connector = fb_helper->connector_info[j]->connector;
563 			connector->funcs->dpms(connector, dpms_mode);
564 			drm_object_property_set_value(&connector->base,
565 				dev->mode_config.dpms_property, dpms_mode);
566 		}
567 	}
568 	drm_modeset_unlock_all(dev);
569 }
570 
571 /**
572  * drm_fb_helper_blank - implementation for ->fb_blank
573  * @blank: desired blanking state
574  * @info: fbdev registered by the helper
575  */
576 int drm_fb_helper_blank(int blank, struct fb_info *info)
577 {
578 #ifdef __DragonFly__
579 	if (panicstr)
580 		return -EBUSY;
581 #else
582 	if (oops_in_progress)
583 		return -EBUSY;
584 #endif
585 
586 	switch (blank) {
587 	/* Display: On; HSync: On, VSync: On */
588 	case FB_BLANK_UNBLANK:
589 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
590 		break;
591 #if 0
592 	/* Display: Off; HSync: On, VSync: On */
593 	case FB_BLANK_NORMAL:
594 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
595 		break;
596 	/* Display: Off; HSync: Off, VSync: On */
597 	case FB_BLANK_HSYNC_SUSPEND:
598 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
599 		break;
600 	/* Display: Off; HSync: On, VSync: Off */
601 	case FB_BLANK_VSYNC_SUSPEND:
602 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
603 		break;
604 #endif
605 	/* Display: Off; HSync: Off, VSync: Off */
606 	case FB_BLANK_POWERDOWN:
607 		drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
608 		break;
609 	}
610 	return 0;
611 }
612 EXPORT_SYMBOL(drm_fb_helper_blank);
613 
614 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
615 {
616 	int i;
617 
618 	for (i = 0; i < helper->connector_count; i++) {
619 		drm_connector_unreference(helper->connector_info[i]->connector);
620 		kfree(helper->connector_info[i]);
621 	}
622 	kfree(helper->connector_info);
623 	for (i = 0; i < helper->crtc_count; i++) {
624 		kfree(helper->crtc_info[i].mode_set.connectors);
625 		if (helper->crtc_info[i].mode_set.mode)
626 			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
627 	}
628 	kfree(helper->crtc_info);
629 }
630 
631 static void drm_fb_helper_dirty_work(struct work_struct *work)
632 {
633 	struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
634 						    dirty_work);
635 	struct drm_clip_rect *clip = &helper->dirty_clip;
636 	struct drm_clip_rect clip_copy;
637 	unsigned long flags;
638 
639 	spin_lock_irqsave(&helper->dirty_lock, flags);
640 	clip_copy = *clip;
641 	clip->x1 = clip->y1 = ~0;
642 	clip->x2 = clip->y2 = 0;
643 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
644 
645 	helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
646 }
647 
648 /**
649  * drm_fb_helper_prepare - setup a drm_fb_helper structure
650  * @dev: DRM device
651  * @helper: driver-allocated fbdev helper structure to set up
652  * @funcs: pointer to structure of functions associate with this helper
653  *
654  * Sets up the bare minimum to make the framebuffer helper usable. This is
655  * useful to implement race-free initialization of the polling helpers.
656  */
657 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
658 			   const struct drm_fb_helper_funcs *funcs)
659 {
660 	INIT_LIST_HEAD(&helper->kernel_fb_list);
661 	lockinit(&helper->dirty_lock, "drm_fb_helper dirty_lock", 0, LK_CANRECURSE);
662 	INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
663 	helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
664 	helper->funcs = funcs;
665 	helper->dev = dev;
666 }
667 EXPORT_SYMBOL(drm_fb_helper_prepare);
668 
669 /**
670  * drm_fb_helper_init - initialize a drm_fb_helper structure
671  * @dev: drm device
672  * @fb_helper: driver-allocated fbdev helper structure to initialize
673  * @crtc_count: maximum number of crtcs to support in this fbdev emulation
674  * @max_conn_count: max connector count
675  *
676  * This allocates the structures for the fbdev helper with the given limits.
677  * Note that this won't yet touch the hardware (through the driver interfaces)
678  * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
679  * to allow driver writes more control over the exact init sequence.
680  *
681  * Drivers must call drm_fb_helper_prepare() before calling this function.
682  *
683  * RETURNS:
684  * Zero if everything went ok, nonzero otherwise.
685  */
686 int drm_fb_helper_init(struct drm_device *dev,
687 		       struct drm_fb_helper *fb_helper,
688 		       int crtc_count, int max_conn_count)
689 {
690 	struct drm_crtc *crtc;
691 	int i;
692 
693 	if (!drm_fbdev_emulation)
694 		return 0;
695 
696 	if (!max_conn_count)
697 		return -EINVAL;
698 
699 	fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
700 	if (!fb_helper->crtc_info)
701 		return -ENOMEM;
702 
703 	fb_helper->crtc_count = crtc_count;
704 	fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
705 	if (!fb_helper->connector_info) {
706 		kfree(fb_helper->crtc_info);
707 		return -ENOMEM;
708 	}
709 	fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
710 	fb_helper->connector_count = 0;
711 
712 	for (i = 0; i < crtc_count; i++) {
713 		fb_helper->crtc_info[i].mode_set.connectors =
714 			kcalloc(max_conn_count,
715 				sizeof(struct drm_connector *),
716 				GFP_KERNEL);
717 
718 		if (!fb_helper->crtc_info[i].mode_set.connectors)
719 			goto out_free;
720 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
721 	}
722 
723 	i = 0;
724 	drm_for_each_crtc(crtc, dev) {
725 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
726 		i++;
727 	}
728 
729 	fb_helper->atomic = !!drm_core_check_feature(dev, DRIVER_ATOMIC);
730 
731 	return 0;
732 out_free:
733 	drm_fb_helper_crtc_free(fb_helper);
734 	return -ENOMEM;
735 }
736 EXPORT_SYMBOL(drm_fb_helper_init);
737 
738 /**
739  * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
740  * @fb_helper: driver-allocated fbdev helper
741  *
742  * A helper to alloc fb_info and the members cmap and apertures. Called
743  * by the driver within the fb_probe fb_helper callback function.
744  *
745  * RETURNS:
746  * fb_info pointer if things went okay, pointer containing error code
747  * otherwise
748  */
749 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
750 {
751 	struct fb_info *info;
752 
753 #ifdef __DragonFly__
754 	info = kzalloc(sizeof(struct fb_info), GFP_KERNEL);
755 #else
756 	info = framebuffer_alloc(0, dev);
757 #endif
758 	if (!info)
759 		return ERR_PTR(-ENOMEM);
760 
761 #if 0
762 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
763 	if (ret)
764 		goto err_release;
765 
766 	info->apertures = alloc_apertures(1);
767 	if (!info->apertures) {
768 		ret = -ENOMEM;
769 		goto err_free_cmap;
770 	}
771 #endif
772 
773 	fb_helper->fbdev = info;
774 
775 	return info;
776 
777 #if 0
778 err_free_cmap:
779 	fb_dealloc_cmap(&info->cmap);
780 err_release:
781 	framebuffer_release(info);
782 	return ERR_PTR(ret);
783 #endif
784 }
785 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
786 
787 /**
788  * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
789  * @fb_helper: driver-allocated fbdev helper
790  *
791  * A wrapper around unregister_framebuffer, to release the fb_info
792  * framebuffer device
793  */
794 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
795 {
796 	if (fb_helper && fb_helper->fbdev)
797 		unregister_framebuffer(fb_helper->fbdev);
798 }
799 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
800 
801 /**
802  * drm_fb_helper_release_fbi - dealloc fb_info and its members
803  * @fb_helper: driver-allocated fbdev helper
804  *
805  * A helper to free memory taken by fb_info and the members cmap and
806  * apertures
807  */
808 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
809 {
810 	if (fb_helper) {
811 		struct fb_info *info = fb_helper->fbdev;
812 
813 		if (info) {
814 #ifdef __DragonFly__
815 			kfree(info);
816 #else
817 			if (info->cmap.len)
818 				fb_dealloc_cmap(&info->cmap);
819 			framebuffer_release(info);
820 #endif
821 		}
822 
823 		fb_helper->fbdev = NULL;
824 	}
825 }
826 EXPORT_SYMBOL(drm_fb_helper_release_fbi);
827 
828 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
829 {
830 	if (!drm_fbdev_emulation)
831 		return;
832 
833 	if (!list_empty(&fb_helper->kernel_fb_list)) {
834 		list_del(&fb_helper->kernel_fb_list);
835 		if (list_empty(&kernel_fb_helper_list)) {
836 #if 0
837 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
838 #endif
839 		}
840 	}
841 
842 	drm_fb_helper_crtc_free(fb_helper);
843 
844 }
845 EXPORT_SYMBOL(drm_fb_helper_fini);
846 
847 #if 0
848 /**
849  * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
850  * @fb_helper: driver-allocated fbdev helper
851  *
852  * A wrapper around unlink_framebuffer implemented by fbdev core
853  */
854 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
855 {
856 	if (fb_helper && fb_helper->fbdev)
857 		unlink_framebuffer(fb_helper->fbdev);
858 }
859 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
860 
861 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
862 				u32 width, u32 height)
863 {
864 	struct drm_fb_helper *helper = info->par;
865 	struct drm_clip_rect *clip = &helper->dirty_clip;
866 	unsigned long flags;
867 
868 	if (!helper->fb->funcs->dirty)
869 		return;
870 
871 	spin_lock_irqsave(&helper->dirty_lock, flags);
872 	clip->x1 = min_t(u32, clip->x1, x);
873 	clip->y1 = min_t(u32, clip->y1, y);
874 	clip->x2 = max_t(u32, clip->x2, x + width);
875 	clip->y2 = max_t(u32, clip->y2, y + height);
876 	spin_unlock_irqrestore(&helper->dirty_lock, flags);
877 
878 	schedule_work(&helper->dirty_work);
879 }
880 
881 /**
882  * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
883  * @info: fb_info struct pointer
884  * @pagelist: list of dirty mmap framebuffer pages
885  *
886  * This function is used as the &fb_deferred_io ->deferred_io
887  * callback function for flushing the fbdev mmap writes.
888  */
889 void drm_fb_helper_deferred_io(struct fb_info *info,
890 			       struct list_head *pagelist)
891 {
892 	unsigned long start, end, min, max;
893 	struct page *page;
894 	u32 y1, y2;
895 
896 	min = ULONG_MAX;
897 	max = 0;
898 	list_for_each_entry(page, pagelist, lru) {
899 		start = page->index << PAGE_SHIFT;
900 		end = start + PAGE_SIZE - 1;
901 		min = min(min, start);
902 		max = max(max, end);
903 	}
904 
905 	if (min < max) {
906 		y1 = min / info->fix.line_length;
907 		y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
908 			   info->var.yres);
909 		drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
910 	}
911 }
912 EXPORT_SYMBOL(drm_fb_helper_deferred_io);
913 
914 /**
915  * drm_fb_helper_sys_read - wrapper around fb_sys_read
916  * @info: fb_info struct pointer
917  * @buf: userspace buffer to read from framebuffer memory
918  * @count: number of bytes to read from framebuffer memory
919  * @ppos: read offset within framebuffer memory
920  *
921  * A wrapper around fb_sys_read implemented by fbdev core
922  */
923 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
924 			       size_t count, loff_t *ppos)
925 {
926 	return fb_sys_read(info, buf, count, ppos);
927 }
928 EXPORT_SYMBOL(drm_fb_helper_sys_read);
929 
930 /**
931  * drm_fb_helper_sys_write - wrapper around fb_sys_write
932  * @info: fb_info struct pointer
933  * @buf: userspace buffer to write to framebuffer memory
934  * @count: number of bytes to write to framebuffer memory
935  * @ppos: write offset within framebuffer memory
936  *
937  * A wrapper around fb_sys_write implemented by fbdev core
938  */
939 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
940 				size_t count, loff_t *ppos)
941 {
942 	ssize_t ret;
943 
944 	ret = fb_sys_write(info, buf, count, ppos);
945 	if (ret > 0)
946 		drm_fb_helper_dirty(info, 0, 0, info->var.xres,
947 				    info->var.yres);
948 
949 	return ret;
950 }
951 EXPORT_SYMBOL(drm_fb_helper_sys_write);
952 
953 /**
954  * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
955  * @info: fbdev registered by the helper
956  * @rect: info about rectangle to fill
957  *
958  * A wrapper around sys_fillrect implemented by fbdev core
959  */
960 void drm_fb_helper_sys_fillrect(struct fb_info *info,
961 				const struct fb_fillrect *rect)
962 {
963 	sys_fillrect(info, rect);
964 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
965 			    rect->width, rect->height);
966 }
967 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
968 
969 /**
970  * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
971  * @info: fbdev registered by the helper
972  * @area: info about area to copy
973  *
974  * A wrapper around sys_copyarea implemented by fbdev core
975  */
976 void drm_fb_helper_sys_copyarea(struct fb_info *info,
977 				const struct fb_copyarea *area)
978 {
979 	sys_copyarea(info, area);
980 	drm_fb_helper_dirty(info, area->dx, area->dy,
981 			    area->width, area->height);
982 }
983 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
984 
985 /**
986  * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
987  * @info: fbdev registered by the helper
988  * @image: info about image to blit
989  *
990  * A wrapper around sys_imageblit implemented by fbdev core
991  */
992 void drm_fb_helper_sys_imageblit(struct fb_info *info,
993 				 const struct fb_image *image)
994 {
995 	sys_imageblit(info, image);
996 	drm_fb_helper_dirty(info, image->dx, image->dy,
997 			    image->width, image->height);
998 }
999 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
1000 
1001 /**
1002  * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
1003  * @info: fbdev registered by the helper
1004  * @rect: info about rectangle to fill
1005  *
1006  * A wrapper around cfb_imageblit implemented by fbdev core
1007  */
1008 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
1009 				const struct fb_fillrect *rect)
1010 {
1011 	cfb_fillrect(info, rect);
1012 	drm_fb_helper_dirty(info, rect->dx, rect->dy,
1013 			    rect->width, rect->height);
1014 }
1015 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
1016 
1017 /**
1018  * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
1019  * @info: fbdev registered by the helper
1020  * @area: info about area to copy
1021  *
1022  * A wrapper around cfb_copyarea implemented by fbdev core
1023  */
1024 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
1025 				const struct fb_copyarea *area)
1026 {
1027 	cfb_copyarea(info, area);
1028 	drm_fb_helper_dirty(info, area->dx, area->dy,
1029 			    area->width, area->height);
1030 }
1031 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
1032 
1033 /**
1034  * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
1035  * @info: fbdev registered by the helper
1036  * @image: info about image to blit
1037  *
1038  * A wrapper around cfb_imageblit implemented by fbdev core
1039  */
1040 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
1041 				 const struct fb_image *image)
1042 {
1043 	cfb_imageblit(info, image);
1044 	drm_fb_helper_dirty(info, image->dx, image->dy,
1045 			    image->width, image->height);
1046 }
1047 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
1048 
1049 /**
1050  * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
1051  * @fb_helper: driver-allocated fbdev helper
1052  * @state: desired state, zero to resume, non-zero to suspend
1053  *
1054  * A wrapper around fb_set_suspend implemented by fbdev core
1055  */
1056 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state)
1057 {
1058 	if (fb_helper && fb_helper->fbdev)
1059 		fb_set_suspend(fb_helper->fbdev, state);
1060 }
1061 EXPORT_SYMBOL(drm_fb_helper_set_suspend);
1062 
1063 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
1064 		     u16 blue, u16 regno, struct fb_info *info)
1065 {
1066 	struct drm_fb_helper *fb_helper = info->par;
1067 	struct drm_framebuffer *fb = fb_helper->fb;
1068 	int pindex;
1069 
1070 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
1071 		u32 *palette;
1072 		u32 value;
1073 		/* place color in psuedopalette */
1074 		if (regno > 16)
1075 			return -EINVAL;
1076 		palette = (u32 *)info->pseudo_palette;
1077 		red >>= (16 - info->var.red.length);
1078 		green >>= (16 - info->var.green.length);
1079 		blue >>= (16 - info->var.blue.length);
1080 		value = (red << info->var.red.offset) |
1081 			(green << info->var.green.offset) |
1082 			(blue << info->var.blue.offset);
1083 		if (info->var.transp.length > 0) {
1084 			u32 mask = (1 << info->var.transp.length) - 1;
1085 			mask <<= info->var.transp.offset;
1086 			value |= mask;
1087 		}
1088 		palette[regno] = value;
1089 		return 0;
1090 	}
1091 
1092 	/*
1093 	 * The driver really shouldn't advertise pseudo/directcolor
1094 	 * visuals if it can't deal with the palette.
1095 	 */
1096 	if (WARN_ON(!fb_helper->funcs->gamma_set ||
1097 		    !fb_helper->funcs->gamma_get))
1098 		return -EINVAL;
1099 
1100 	pindex = regno;
1101 
1102 	if (fb->bits_per_pixel == 16) {
1103 		pindex = regno << 3;
1104 
1105 		if (fb->depth == 16 && regno > 63)
1106 			return -EINVAL;
1107 		if (fb->depth == 15 && regno > 31)
1108 			return -EINVAL;
1109 
1110 		if (fb->depth == 16) {
1111 			u16 r, g, b;
1112 			int i;
1113 			if (regno < 32) {
1114 				for (i = 0; i < 8; i++)
1115 					fb_helper->funcs->gamma_set(crtc, red,
1116 						green, blue, pindex + i);
1117 			}
1118 
1119 			fb_helper->funcs->gamma_get(crtc, &r,
1120 						    &g, &b,
1121 						    pindex >> 1);
1122 
1123 			for (i = 0; i < 4; i++)
1124 				fb_helper->funcs->gamma_set(crtc, r,
1125 							    green, b,
1126 							    (pindex >> 1) + i);
1127 		}
1128 	}
1129 
1130 	if (fb->depth != 16)
1131 		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
1132 	return 0;
1133 }
1134 
1135 /**
1136  * drm_fb_helper_setcmap - implementation for ->fb_setcmap
1137  * @cmap: cmap to set
1138  * @info: fbdev registered by the helper
1139  */
1140 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1141 {
1142 	struct drm_fb_helper *fb_helper = info->par;
1143 	struct drm_device *dev = fb_helper->dev;
1144 	const struct drm_crtc_helper_funcs *crtc_funcs;
1145 	u16 *red, *green, *blue, *transp;
1146 	struct drm_crtc *crtc;
1147 	int i, j, rc = 0;
1148 	int start;
1149 
1150 	if (oops_in_progress)
1151 		return -EBUSY;
1152 
1153 	drm_modeset_lock_all(dev);
1154 	if (!drm_fb_helper_is_bound(fb_helper)) {
1155 		drm_modeset_unlock_all(dev);
1156 		return -EBUSY;
1157 	}
1158 
1159 	for (i = 0; i < fb_helper->crtc_count; i++) {
1160 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
1161 		crtc_funcs = crtc->helper_private;
1162 
1163 		red = cmap->red;
1164 		green = cmap->green;
1165 		blue = cmap->blue;
1166 		transp = cmap->transp;
1167 		start = cmap->start;
1168 
1169 		for (j = 0; j < cmap->len; j++) {
1170 			u16 hred, hgreen, hblue, htransp = 0xffff;
1171 
1172 			hred = *red++;
1173 			hgreen = *green++;
1174 			hblue = *blue++;
1175 
1176 			if (transp)
1177 				htransp = *transp++;
1178 
1179 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
1180 			if (rc)
1181 				goto out;
1182 		}
1183 		if (crtc_funcs->load_lut)
1184 			crtc_funcs->load_lut(crtc);
1185 	}
1186  out:
1187 	drm_modeset_unlock_all(dev);
1188 	return rc;
1189 }
1190 EXPORT_SYMBOL(drm_fb_helper_setcmap);
1191 
1192 /**
1193  * drm_fb_helper_check_var - implementation for ->fb_check_var
1194  * @var: screeninfo to check
1195  * @info: fbdev registered by the helper
1196  */
1197 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1198 			    struct fb_info *info)
1199 {
1200 	struct drm_fb_helper *fb_helper = info->par;
1201 	struct drm_framebuffer *fb = fb_helper->fb;
1202 	int depth;
1203 
1204 	if (var->pixclock != 0 || in_dbg_master())
1205 		return -EINVAL;
1206 
1207 	/* Need to resize the fb object !!! */
1208 	if (var->bits_per_pixel > fb->bits_per_pixel ||
1209 	    var->xres > fb->width || var->yres > fb->height ||
1210 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1211 		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
1212 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1213 			  var->xres, var->yres, var->bits_per_pixel,
1214 			  var->xres_virtual, var->yres_virtual,
1215 			  fb->width, fb->height, fb->bits_per_pixel);
1216 		return -EINVAL;
1217 	}
1218 
1219 	switch (var->bits_per_pixel) {
1220 	case 16:
1221 		depth = (var->green.length == 6) ? 16 : 15;
1222 		break;
1223 	case 32:
1224 		depth = (var->transp.length > 0) ? 32 : 24;
1225 		break;
1226 	default:
1227 		depth = var->bits_per_pixel;
1228 		break;
1229 	}
1230 
1231 	switch (depth) {
1232 	case 8:
1233 		var->red.offset = 0;
1234 		var->green.offset = 0;
1235 		var->blue.offset = 0;
1236 		var->red.length = 8;
1237 		var->green.length = 8;
1238 		var->blue.length = 8;
1239 		var->transp.length = 0;
1240 		var->transp.offset = 0;
1241 		break;
1242 	case 15:
1243 		var->red.offset = 10;
1244 		var->green.offset = 5;
1245 		var->blue.offset = 0;
1246 		var->red.length = 5;
1247 		var->green.length = 5;
1248 		var->blue.length = 5;
1249 		var->transp.length = 1;
1250 		var->transp.offset = 15;
1251 		break;
1252 	case 16:
1253 		var->red.offset = 11;
1254 		var->green.offset = 5;
1255 		var->blue.offset = 0;
1256 		var->red.length = 5;
1257 		var->green.length = 6;
1258 		var->blue.length = 5;
1259 		var->transp.length = 0;
1260 		var->transp.offset = 0;
1261 		break;
1262 	case 24:
1263 		var->red.offset = 16;
1264 		var->green.offset = 8;
1265 		var->blue.offset = 0;
1266 		var->red.length = 8;
1267 		var->green.length = 8;
1268 		var->blue.length = 8;
1269 		var->transp.length = 0;
1270 		var->transp.offset = 0;
1271 		break;
1272 	case 32:
1273 		var->red.offset = 16;
1274 		var->green.offset = 8;
1275 		var->blue.offset = 0;
1276 		var->red.length = 8;
1277 		var->green.length = 8;
1278 		var->blue.length = 8;
1279 		var->transp.length = 8;
1280 		var->transp.offset = 24;
1281 		break;
1282 	default:
1283 		return -EINVAL;
1284 	}
1285 	return 0;
1286 }
1287 EXPORT_SYMBOL(drm_fb_helper_check_var);
1288 #endif
1289 
1290 /**
1291  * drm_fb_helper_set_par - implementation for ->fb_set_par
1292  * @info: fbdev registered by the helper
1293  *
1294  * This will let fbcon do the mode init and is called at initialization time by
1295  * the fbdev core when registering the driver, and later on through the hotplug
1296  * callback.
1297  */
1298 int drm_fb_helper_set_par(struct fb_info *info)
1299 {
1300 	struct drm_fb_helper *fb_helper = info->par;
1301 #if 0
1302 	struct fb_var_screeninfo *var = &info->var;
1303 #endif
1304 
1305 #ifdef __DragonFly__
1306 	if (panicstr)
1307 		return -EBUSY;
1308 #else
1309 	if (oops_in_progress)
1310 		return -EBUSY;
1311 #endif
1312 
1313 #if 0
1314 	if (var->pixclock != 0) {
1315 		DRM_ERROR("PIXEL CLOCK SET\n");
1316 		return -EINVAL;
1317 	}
1318 #endif
1319 
1320 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1321 
1322 	return 0;
1323 }
1324 EXPORT_SYMBOL(drm_fb_helper_set_par);
1325 
1326 #if 0
1327 static int pan_display_atomic(struct fb_var_screeninfo *var,
1328 			      struct fb_info *info)
1329 {
1330 	struct drm_fb_helper *fb_helper = info->par;
1331 	struct drm_device *dev = fb_helper->dev;
1332 	struct drm_atomic_state *state;
1333 	struct drm_plane *plane;
1334 	int i, ret;
1335 	unsigned plane_mask;
1336 
1337 	state = drm_atomic_state_alloc(dev);
1338 	if (!state)
1339 		return -ENOMEM;
1340 
1341 	state->acquire_ctx = dev->mode_config.acquire_ctx;
1342 retry:
1343 	plane_mask = 0;
1344 	for(i = 0; i < fb_helper->crtc_count; i++) {
1345 		struct drm_mode_set *mode_set;
1346 
1347 		mode_set = &fb_helper->crtc_info[i].mode_set;
1348 
1349 		mode_set->x = var->xoffset;
1350 		mode_set->y = var->yoffset;
1351 
1352 		ret = __drm_atomic_helper_set_config(mode_set, state);
1353 		if (ret != 0)
1354 			goto fail;
1355 
1356 		plane = mode_set->crtc->primary;
1357 		plane_mask |= (1 << drm_plane_index(plane));
1358 		plane->old_fb = plane->fb;
1359 	}
1360 
1361 	ret = drm_atomic_commit(state);
1362 	if (ret != 0)
1363 		goto fail;
1364 
1365 	info->var.xoffset = var->xoffset;
1366 	info->var.yoffset = var->yoffset;
1367 
1368 
1369 fail:
1370 	drm_atomic_clean_old_fb(dev, plane_mask, ret);
1371 
1372 	if (ret == -EDEADLK)
1373 		goto backoff;
1374 
1375 	if (ret != 0)
1376 		drm_atomic_state_free(state);
1377 
1378 	return ret;
1379 
1380 backoff:
1381 	drm_atomic_state_clear(state);
1382 	drm_atomic_legacy_backoff(state);
1383 
1384 	goto retry;
1385 }
1386 
1387 /**
1388  * drm_fb_helper_pan_display - implementation for ->fb_pan_display
1389  * @var: updated screen information
1390  * @info: fbdev registered by the helper
1391  */
1392 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1393 			      struct fb_info *info)
1394 {
1395 	struct drm_fb_helper *fb_helper = info->par;
1396 	struct drm_device *dev = fb_helper->dev;
1397 	struct drm_mode_set *modeset;
1398 	int ret = 0;
1399 	int i;
1400 
1401 	if (oops_in_progress)
1402 		return -EBUSY;
1403 
1404 	drm_modeset_lock_all(dev);
1405 	if (!drm_fb_helper_is_bound(fb_helper)) {
1406 		drm_modeset_unlock_all(dev);
1407 		return -EBUSY;
1408 	}
1409 
1410 	if (fb_helper->atomic) {
1411 		ret = pan_display_atomic(var, info);
1412 		goto unlock;
1413 	}
1414 
1415 	for (i = 0; i < fb_helper->crtc_count; i++) {
1416 		modeset = &fb_helper->crtc_info[i].mode_set;
1417 
1418 		modeset->x = var->xoffset;
1419 		modeset->y = var->yoffset;
1420 
1421 		if (modeset->num_connectors) {
1422 			ret = drm_mode_set_config_internal(modeset);
1423 			if (!ret) {
1424 				info->var.xoffset = var->xoffset;
1425 				info->var.yoffset = var->yoffset;
1426 			}
1427 		}
1428 	}
1429 unlock:
1430 	drm_modeset_unlock_all(dev);
1431 	return ret;
1432 }
1433 EXPORT_SYMBOL(drm_fb_helper_pan_display);
1434 #endif
1435 
1436 /*
1437  * Allocates the backing storage and sets up the fbdev info structure through
1438  * the ->fb_probe callback and then registers the fbdev and sets up the panic
1439  * notifier.
1440  */
1441 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1442 					 int preferred_bpp)
1443 {
1444 	int ret = 0;
1445 	int crtc_count = 0;
1446 	int i;
1447 	struct fb_info *info;
1448 	struct drm_fb_helper_surface_size sizes;
1449 	int gamma_size = 0;
1450 #ifdef __DragonFly__
1451 	int kms_console = 1;
1452 #endif
1453 
1454 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1455 	sizes.surface_depth = 24;
1456 	sizes.surface_bpp = 32;
1457 	sizes.fb_width = (unsigned)-1;
1458 	sizes.fb_height = (unsigned)-1;
1459 
1460 	/* if driver picks 8 or 16 by default use that
1461 	   for both depth/bpp */
1462 	if (preferred_bpp != sizes.surface_bpp)
1463 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1464 
1465 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
1466 	for (i = 0; i < fb_helper->connector_count; i++) {
1467 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1468 		struct drm_cmdline_mode *cmdline_mode;
1469 
1470 		cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1471 
1472 		if (cmdline_mode->bpp_specified) {
1473 			switch (cmdline_mode->bpp) {
1474 			case 8:
1475 				sizes.surface_depth = sizes.surface_bpp = 8;
1476 				break;
1477 			case 15:
1478 				sizes.surface_depth = 15;
1479 				sizes.surface_bpp = 16;
1480 				break;
1481 			case 16:
1482 				sizes.surface_depth = sizes.surface_bpp = 16;
1483 				break;
1484 			case 24:
1485 				sizes.surface_depth = sizes.surface_bpp = 24;
1486 				break;
1487 			case 32:
1488 				sizes.surface_depth = 24;
1489 				sizes.surface_bpp = 32;
1490 				break;
1491 			}
1492 			break;
1493 		}
1494 	}
1495 
1496 	crtc_count = 0;
1497 	for (i = 0; i < fb_helper->crtc_count; i++) {
1498 		struct drm_display_mode *desired_mode;
1499 		struct drm_mode_set *mode_set;
1500 		int x, y, j;
1501 		/* in case of tile group, are we the last tile vert or horiz?
1502 		 * If no tile group you are always the last one both vertically
1503 		 * and horizontally
1504 		 */
1505 		bool lastv = true, lasth = true;
1506 
1507 		desired_mode = fb_helper->crtc_info[i].desired_mode;
1508 		mode_set = &fb_helper->crtc_info[i].mode_set;
1509 
1510 		if (!desired_mode)
1511 			continue;
1512 
1513 		crtc_count++;
1514 
1515 		x = fb_helper->crtc_info[i].x;
1516 		y = fb_helper->crtc_info[i].y;
1517 
1518 		if (gamma_size == 0)
1519 			gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1520 
1521 		sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1522 		sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1523 
1524 		for (j = 0; j < mode_set->num_connectors; j++) {
1525 			struct drm_connector *connector = mode_set->connectors[j];
1526 			if (connector->has_tile) {
1527 				lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1528 				lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1529 				/* cloning to multiple tiles is just crazy-talk, so: */
1530 				break;
1531 			}
1532 		}
1533 
1534 		if (lasth)
1535 			sizes.fb_width  = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1536 		if (lastv)
1537 			sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1538 	}
1539 
1540 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1541 		/* hmm everyone went away - assume VGA cable just fell out
1542 		   and will come back later. */
1543 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1544 		sizes.fb_width = sizes.surface_width = 1024;
1545 		sizes.fb_height = sizes.surface_height = 768;
1546 	}
1547 
1548 	/* push down into drivers */
1549 	ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1550 	if (ret < 0)
1551 		return ret;
1552 
1553 	info = fb_helper->fbdev;
1554 
1555 	/*
1556 	 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1557 	 * events, but at init time drm_setup_crtcs needs to be called before
1558 	 * the fb is allocated (since we need to figure out the desired size of
1559 	 * the fb before we can allocate it ...). Hence we need to fix things up
1560 	 * here again.
1561 	 */
1562 	for (i = 0; i < fb_helper->crtc_count; i++)
1563 		if (fb_helper->crtc_info[i].mode_set.num_connectors)
1564 			fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1565 
1566 #ifdef __DragonFly__
1567 	TUNABLE_INT_FETCH("kern.kms_console", &kms_console);
1568 	if (kms_console) {
1569 		if (register_framebuffer(info) < 0)
1570 			return -EINVAL;
1571 
1572 		list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1573 	}
1574 #else
1575 	info->var.pixclock = 0;
1576 	if (register_framebuffer(info) < 0)
1577 		return -EINVAL;
1578 
1579 	dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
1580 			info->node, info->fix.id);
1581 
1582 	if (list_empty(&kernel_fb_helper_list)) {
1583 		register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1584 	}
1585 
1586 	list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1587 #endif
1588 
1589 	return 0;
1590 }
1591 
1592 #if 0
1593 /**
1594  * drm_fb_helper_fill_fix - initializes fixed fbdev information
1595  * @info: fbdev registered by the helper
1596  * @pitch: desired pitch
1597  * @depth: desired depth
1598  *
1599  * Helper to fill in the fixed fbdev information useful for a non-accelerated
1600  * fbdev emulations. Drivers which support acceleration methods which impose
1601  * additional constraints need to set up their own limits.
1602  *
1603  * Drivers should call this (or their equivalent setup code) from their
1604  * ->fb_probe callback.
1605  */
1606 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1607 			    uint32_t depth)
1608 {
1609 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1610 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1611 		FB_VISUAL_TRUECOLOR;
1612 	info->fix.mmio_start = 0;
1613 	info->fix.mmio_len = 0;
1614 	info->fix.type_aux = 0;
1615 	info->fix.xpanstep = 1; /* doing it in hw */
1616 	info->fix.ypanstep = 1; /* doing it in hw */
1617 	info->fix.ywrapstep = 0;
1618 	info->fix.accel = FB_ACCEL_NONE;
1619 
1620 	info->fix.line_length = pitch;
1621 	return;
1622 }
1623 EXPORT_SYMBOL(drm_fb_helper_fill_fix);
1624 
1625 /**
1626  * drm_fb_helper_fill_var - initalizes variable fbdev information
1627  * @info: fbdev instance to set up
1628  * @fb_helper: fb helper instance to use as template
1629  * @fb_width: desired fb width
1630  * @fb_height: desired fb height
1631  *
1632  * Sets up the variable fbdev metainformation from the given fb helper instance
1633  * and the drm framebuffer allocated in fb_helper->fb.
1634  *
1635  * Drivers should call this (or their equivalent setup code) from their
1636  * ->fb_probe callback after having allocated the fbdev backing
1637  * storage framebuffer.
1638  */
1639 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
1640 			    uint32_t fb_width, uint32_t fb_height)
1641 {
1642 	struct drm_framebuffer *fb = fb_helper->fb;
1643 	info->pseudo_palette = fb_helper->pseudo_palette;
1644 	info->var.xres_virtual = fb->width;
1645 	info->var.yres_virtual = fb->height;
1646 	info->var.bits_per_pixel = fb->bits_per_pixel;
1647 	info->var.accel_flags = FB_ACCELF_TEXT;
1648 	info->var.xoffset = 0;
1649 	info->var.yoffset = 0;
1650 	info->var.activate = FB_ACTIVATE_NOW;
1651 	info->var.height = -1;
1652 	info->var.width = -1;
1653 
1654 	switch (fb->depth) {
1655 	case 8:
1656 		info->var.red.offset = 0;
1657 		info->var.green.offset = 0;
1658 		info->var.blue.offset = 0;
1659 		info->var.red.length = 8; /* 8bit DAC */
1660 		info->var.green.length = 8;
1661 		info->var.blue.length = 8;
1662 		info->var.transp.offset = 0;
1663 		info->var.transp.length = 0;
1664 		break;
1665 	case 15:
1666 		info->var.red.offset = 10;
1667 		info->var.green.offset = 5;
1668 		info->var.blue.offset = 0;
1669 		info->var.red.length = 5;
1670 		info->var.green.length = 5;
1671 		info->var.blue.length = 5;
1672 		info->var.transp.offset = 15;
1673 		info->var.transp.length = 1;
1674 		break;
1675 	case 16:
1676 		info->var.red.offset = 11;
1677 		info->var.green.offset = 5;
1678 		info->var.blue.offset = 0;
1679 		info->var.red.length = 5;
1680 		info->var.green.length = 6;
1681 		info->var.blue.length = 5;
1682 		info->var.transp.offset = 0;
1683 		break;
1684 	case 24:
1685 		info->var.red.offset = 16;
1686 		info->var.green.offset = 8;
1687 		info->var.blue.offset = 0;
1688 		info->var.red.length = 8;
1689 		info->var.green.length = 8;
1690 		info->var.blue.length = 8;
1691 		info->var.transp.offset = 0;
1692 		info->var.transp.length = 0;
1693 		break;
1694 	case 32:
1695 		info->var.red.offset = 16;
1696 		info->var.green.offset = 8;
1697 		info->var.blue.offset = 0;
1698 		info->var.red.length = 8;
1699 		info->var.green.length = 8;
1700 		info->var.blue.length = 8;
1701 		info->var.transp.offset = 24;
1702 		info->var.transp.length = 8;
1703 		break;
1704 	default:
1705 		break;
1706 	}
1707 
1708 	info->var.xres = fb_width;
1709 	info->var.yres = fb_height;
1710 }
1711 EXPORT_SYMBOL(drm_fb_helper_fill_var);
1712 #endif
1713 
1714 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1715 					       uint32_t maxX,
1716 					       uint32_t maxY)
1717 {
1718 	struct drm_connector *connector;
1719 	int count = 0;
1720 	int i;
1721 
1722 	for (i = 0; i < fb_helper->connector_count; i++) {
1723 		connector = fb_helper->connector_info[i]->connector;
1724 		count += connector->funcs->fill_modes(connector, maxX, maxY);
1725 	}
1726 
1727 	return count;
1728 }
1729 
1730 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1731 {
1732 	struct drm_display_mode *mode;
1733 
1734 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
1735 		if (mode->hdisplay > width ||
1736 		    mode->vdisplay > height)
1737 			continue;
1738 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
1739 			return mode;
1740 	}
1741 	return NULL;
1742 }
1743 EXPORT_SYMBOL(drm_has_preferred_mode);
1744 
1745 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1746 {
1747 	return fb_connector->connector->cmdline_mode.specified;
1748 }
1749 
1750 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1751 						      int width, int height)
1752 {
1753 	struct drm_cmdline_mode *cmdline_mode;
1754 	struct drm_display_mode *mode;
1755 	bool prefer_non_interlace;
1756 
1757 	cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
1758 	if (cmdline_mode->specified == false)
1759 		return NULL;
1760 
1761 	/* attempt to find a matching mode in the list of modes
1762 	 *  we have gotten so far, if not add a CVT mode that conforms
1763 	 */
1764 	if (cmdline_mode->rb || cmdline_mode->margins)
1765 		goto create_mode;
1766 
1767 	prefer_non_interlace = !cmdline_mode->interlace;
1768 again:
1769 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1770 		/* check width/height */
1771 		if (mode->hdisplay != cmdline_mode->xres ||
1772 		    mode->vdisplay != cmdline_mode->yres)
1773 			continue;
1774 
1775 		if (cmdline_mode->refresh_specified) {
1776 			if (mode->vrefresh != cmdline_mode->refresh)
1777 				continue;
1778 		}
1779 
1780 		if (cmdline_mode->interlace) {
1781 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1782 				continue;
1783 		} else if (prefer_non_interlace) {
1784 			if (mode->flags & DRM_MODE_FLAG_INTERLACE)
1785 				continue;
1786 		}
1787 		return mode;
1788 	}
1789 
1790 	if (prefer_non_interlace) {
1791 		prefer_non_interlace = false;
1792 		goto again;
1793 	}
1794 
1795 create_mode:
1796 	mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1797 						 cmdline_mode);
1798 	list_add(&mode->head, &fb_helper_conn->connector->modes);
1799 	return mode;
1800 }
1801 EXPORT_SYMBOL(drm_pick_cmdline_mode);
1802 
1803 static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1804 {
1805 	bool enable;
1806 
1807 	if (strict)
1808 		enable = connector->status == connector_status_connected;
1809 	else
1810 		enable = connector->status != connector_status_disconnected;
1811 
1812 	return enable;
1813 }
1814 
1815 static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1816 				  bool *enabled)
1817 {
1818 	bool any_enabled = false;
1819 	struct drm_connector *connector;
1820 	int i = 0;
1821 
1822 	for (i = 0; i < fb_helper->connector_count; i++) {
1823 		connector = fb_helper->connector_info[i]->connector;
1824 		enabled[i] = drm_connector_enabled(connector, true);
1825 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1826 			  enabled[i] ? "yes" : "no");
1827 		any_enabled |= enabled[i];
1828 	}
1829 
1830 	if (any_enabled)
1831 		return;
1832 
1833 	for (i = 0; i < fb_helper->connector_count; i++) {
1834 		connector = fb_helper->connector_info[i]->connector;
1835 		enabled[i] = drm_connector_enabled(connector, false);
1836 	}
1837 }
1838 
1839 static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1840 			      struct drm_display_mode **modes,
1841 			      struct drm_fb_offset *offsets,
1842 			      bool *enabled, int width, int height)
1843 {
1844 	int count, i, j;
1845 	bool can_clone = false;
1846 	struct drm_fb_helper_connector *fb_helper_conn;
1847 	struct drm_display_mode *dmt_mode, *mode;
1848 
1849 	/* only contemplate cloning in the single crtc case */
1850 	if (fb_helper->crtc_count > 1)
1851 		return false;
1852 
1853 	count = 0;
1854 	for (i = 0; i < fb_helper->connector_count; i++) {
1855 		if (enabled[i])
1856 			count++;
1857 	}
1858 
1859 	/* only contemplate cloning if more than one connector is enabled */
1860 	if (count <= 1)
1861 		return false;
1862 
1863 	/* check the command line or if nothing common pick 1024x768 */
1864 	can_clone = true;
1865 	for (i = 0; i < fb_helper->connector_count; i++) {
1866 		if (!enabled[i])
1867 			continue;
1868 		fb_helper_conn = fb_helper->connector_info[i];
1869 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1870 		if (!modes[i]) {
1871 			can_clone = false;
1872 			break;
1873 		}
1874 		for (j = 0; j < i; j++) {
1875 			if (!enabled[j])
1876 				continue;
1877 			if (!drm_mode_equal(modes[j], modes[i]))
1878 				can_clone = false;
1879 		}
1880 	}
1881 
1882 	if (can_clone) {
1883 		DRM_DEBUG_KMS("can clone using command line\n");
1884 		return true;
1885 	}
1886 
1887 	/* try and find a 1024x768 mode on each connector */
1888 	can_clone = true;
1889 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1890 
1891 	for (i = 0; i < fb_helper->connector_count; i++) {
1892 
1893 		if (!enabled[i])
1894 			continue;
1895 
1896 		fb_helper_conn = fb_helper->connector_info[i];
1897 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
1898 			if (drm_mode_equal(mode, dmt_mode))
1899 				modes[i] = mode;
1900 		}
1901 		if (!modes[i])
1902 			can_clone = false;
1903 	}
1904 
1905 	if (can_clone) {
1906 		DRM_DEBUG_KMS("can clone using 1024x768\n");
1907 		return true;
1908 	}
1909 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1910 	return false;
1911 }
1912 
1913 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
1914 				struct drm_display_mode **modes,
1915 				struct drm_fb_offset *offsets,
1916 				int idx,
1917 				int h_idx, int v_idx)
1918 {
1919 	struct drm_fb_helper_connector *fb_helper_conn;
1920 	int i;
1921 	int hoffset = 0, voffset = 0;
1922 
1923 	for (i = 0; i < fb_helper->connector_count; i++) {
1924 		fb_helper_conn = fb_helper->connector_info[i];
1925 		if (!fb_helper_conn->connector->has_tile)
1926 			continue;
1927 
1928 		if (!modes[i] && (h_idx || v_idx)) {
1929 			DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
1930 				      fb_helper_conn->connector->base.id);
1931 			continue;
1932 		}
1933 		if (fb_helper_conn->connector->tile_h_loc < h_idx)
1934 			hoffset += modes[i]->hdisplay;
1935 
1936 		if (fb_helper_conn->connector->tile_v_loc < v_idx)
1937 			voffset += modes[i]->vdisplay;
1938 	}
1939 	offsets[idx].x = hoffset;
1940 	offsets[idx].y = voffset;
1941 	DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
1942 	return 0;
1943 }
1944 
1945 static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1946 				 struct drm_display_mode **modes,
1947 				 struct drm_fb_offset *offsets,
1948 				 bool *enabled, int width, int height)
1949 {
1950 	struct drm_fb_helper_connector *fb_helper_conn;
1951 	int i;
1952 	uint64_t conn_configured = 0, mask;
1953 	int tile_pass = 0;
1954 	mask = (1 << fb_helper->connector_count) - 1;
1955 retry:
1956 	for (i = 0; i < fb_helper->connector_count; i++) {
1957 		fb_helper_conn = fb_helper->connector_info[i];
1958 
1959 		if (conn_configured & (1 << i))
1960 			continue;
1961 
1962 		if (enabled[i] == false) {
1963 			conn_configured |= (1 << i);
1964 			continue;
1965 		}
1966 
1967 		/* first pass over all the untiled connectors */
1968 		if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
1969 			continue;
1970 
1971 		if (tile_pass == 1) {
1972 			if (fb_helper_conn->connector->tile_h_loc != 0 ||
1973 			    fb_helper_conn->connector->tile_v_loc != 0)
1974 				continue;
1975 
1976 		} else {
1977 			if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
1978 			    fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
1979 			/* if this tile_pass doesn't cover any of the tiles - keep going */
1980 				continue;
1981 
1982 			/* find the tile offsets for this pass - need
1983 			   to find all tiles left and above */
1984 			drm_get_tile_offsets(fb_helper, modes, offsets,
1985 					     i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
1986 		}
1987 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1988 			      fb_helper_conn->connector->base.id);
1989 
1990 		/* got for command line mode first */
1991 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1992 		if (!modes[i]) {
1993 			DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
1994 				      fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
1995 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1996 		}
1997 		/* No preferred modes, pick one off the list */
1998 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1999 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
2000 				break;
2001 		}
2002 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
2003 			  "none");
2004 		conn_configured |= (1 << i);
2005 	}
2006 
2007 	if ((conn_configured & mask) != mask) {
2008 		tile_pass++;
2009 		goto retry;
2010 	}
2011 	return true;
2012 }
2013 
2014 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
2015 			  struct drm_fb_helper_crtc **best_crtcs,
2016 			  struct drm_display_mode **modes,
2017 			  int n, int width, int height)
2018 {
2019 	int c, o;
2020 	struct drm_connector *connector;
2021 	const struct drm_connector_helper_funcs *connector_funcs;
2022 	struct drm_encoder *encoder;
2023 	int my_score, best_score, score;
2024 	struct drm_fb_helper_crtc **crtcs, *crtc;
2025 	struct drm_fb_helper_connector *fb_helper_conn;
2026 
2027 	if (n == fb_helper->connector_count)
2028 		return 0;
2029 
2030 	fb_helper_conn = fb_helper->connector_info[n];
2031 	connector = fb_helper_conn->connector;
2032 
2033 	best_crtcs[n] = NULL;
2034 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
2035 	if (modes[n] == NULL)
2036 		return best_score;
2037 
2038 	crtcs = kzalloc(fb_helper->connector_count *
2039 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2040 	if (!crtcs)
2041 		return best_score;
2042 
2043 	my_score = 1;
2044 	if (connector->status == connector_status_connected)
2045 		my_score++;
2046 	if (drm_has_cmdline_mode(fb_helper_conn))
2047 		my_score++;
2048 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
2049 		my_score++;
2050 
2051 	connector_funcs = connector->helper_private;
2052 	encoder = connector_funcs->best_encoder(connector);
2053 	if (!encoder)
2054 		goto out;
2055 
2056 	/* select a crtc for this connector and then attempt to configure
2057 	   remaining connectors */
2058 	for (c = 0; c < fb_helper->crtc_count; c++) {
2059 		crtc = &fb_helper->crtc_info[c];
2060 
2061 		if ((encoder->possible_crtcs & (1 << c)) == 0)
2062 			continue;
2063 
2064 		for (o = 0; o < n; o++)
2065 			if (best_crtcs[o] == crtc)
2066 				break;
2067 
2068 		if (o < n) {
2069 			/* ignore cloning unless only a single crtc */
2070 			if (fb_helper->crtc_count > 1)
2071 				continue;
2072 
2073 			if (!drm_mode_equal(modes[o], modes[n]))
2074 				continue;
2075 		}
2076 
2077 		crtcs[n] = crtc;
2078 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
2079 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
2080 						  width, height);
2081 		if (score > best_score) {
2082 			best_score = score;
2083 			memcpy(best_crtcs, crtcs,
2084 			       fb_helper->connector_count *
2085 			       sizeof(struct drm_fb_helper_crtc *));
2086 		}
2087 	}
2088 out:
2089 	kfree(crtcs);
2090 	return best_score;
2091 }
2092 
2093 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
2094 {
2095 	struct drm_device *dev = fb_helper->dev;
2096 	struct drm_fb_helper_crtc **crtcs;
2097 	struct drm_display_mode **modes;
2098 	struct drm_fb_offset *offsets;
2099 	struct drm_mode_set *modeset;
2100 	bool *enabled;
2101 	int width, height;
2102 	int i;
2103 
2104 	DRM_DEBUG_KMS("\n");
2105 
2106 	width = dev->mode_config.max_width;
2107 	height = dev->mode_config.max_height;
2108 
2109 	crtcs = kcalloc(fb_helper->connector_count,
2110 			sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
2111 	modes = kcalloc(fb_helper->connector_count,
2112 			sizeof(struct drm_display_mode *), GFP_KERNEL);
2113 	offsets = kcalloc(fb_helper->connector_count,
2114 			  sizeof(struct drm_fb_offset), GFP_KERNEL);
2115 	enabled = kcalloc(fb_helper->connector_count,
2116 			  sizeof(bool), GFP_KERNEL);
2117 	if (!crtcs || !modes || !enabled || !offsets) {
2118 		DRM_ERROR("Memory allocation failed\n");
2119 		goto out;
2120 	}
2121 
2122 
2123 	drm_enable_connectors(fb_helper, enabled);
2124 
2125 	if (!(fb_helper->funcs->initial_config &&
2126 	      fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
2127 					       offsets,
2128 					       enabled, width, height))) {
2129 		memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
2130 		memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
2131 		memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
2132 
2133 		if (!drm_target_cloned(fb_helper, modes, offsets,
2134 				       enabled, width, height) &&
2135 		    !drm_target_preferred(fb_helper, modes, offsets,
2136 					  enabled, width, height))
2137 			DRM_ERROR("Unable to find initial modes\n");
2138 
2139 		DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
2140 			      width, height);
2141 
2142 		drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
2143 	}
2144 
2145 	/* need to set the modesets up here for use later */
2146 	/* fill out the connector<->crtc mappings into the modesets */
2147 	for (i = 0; i < fb_helper->crtc_count; i++) {
2148 		modeset = &fb_helper->crtc_info[i].mode_set;
2149 		modeset->num_connectors = 0;
2150 		modeset->fb = NULL;
2151 	}
2152 
2153 	for (i = 0; i < fb_helper->connector_count; i++) {
2154 		struct drm_display_mode *mode = modes[i];
2155 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
2156 		struct drm_fb_offset *offset = &offsets[i];
2157 		modeset = &fb_crtc->mode_set;
2158 
2159 		if (mode && fb_crtc) {
2160 			DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
2161 				      mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
2162 			fb_crtc->desired_mode = mode;
2163 			fb_crtc->x = offset->x;
2164 			fb_crtc->y = offset->y;
2165 			if (modeset->mode)
2166 				drm_mode_destroy(dev, modeset->mode);
2167 			modeset->mode = drm_mode_duplicate(dev,
2168 							   fb_crtc->desired_mode);
2169 			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
2170 			modeset->fb = fb_helper->fb;
2171 			modeset->x = offset->x;
2172 			modeset->y = offset->y;
2173 		}
2174 	}
2175 
2176 	/* Clear out any old modes if there are no more connected outputs. */
2177 	for (i = 0; i < fb_helper->crtc_count; i++) {
2178 		modeset = &fb_helper->crtc_info[i].mode_set;
2179 		if (modeset->num_connectors == 0) {
2180 			BUG_ON(modeset->fb);
2181 			if (modeset->mode)
2182 				drm_mode_destroy(dev, modeset->mode);
2183 			modeset->mode = NULL;
2184 		}
2185 	}
2186 out:
2187 	kfree(crtcs);
2188 	kfree(modes);
2189 	kfree(offsets);
2190 	kfree(enabled);
2191 }
2192 
2193 /**
2194  * drm_fb_helper_initial_config - setup a sane initial connector configuration
2195  * @fb_helper: fb_helper device struct
2196  * @bpp_sel: bpp value to use for the framebuffer configuration
2197  *
2198  * Scans the CRTCs and connectors and tries to put together an initial setup.
2199  * At the moment, this is a cloned configuration across all heads with
2200  * a new framebuffer object as the backing store.
2201  *
2202  * Note that this also registers the fbdev and so allows userspace to call into
2203  * the driver through the fbdev interfaces.
2204  *
2205  * This function will call down into the ->fb_probe callback to let
2206  * the driver allocate and initialize the fbdev info structure and the drm
2207  * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
2208  * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
2209  * values for the fbdev info structure.
2210  *
2211  * HANG DEBUGGING:
2212  *
2213  * When you have fbcon support built-in or already loaded, this function will do
2214  * a full modeset to setup the fbdev console. Due to locking misdesign in the
2215  * VT/fbdev subsystem that entire modeset sequence has to be done while holding
2216  * console_lock. Until console_unlock is called no dmesg lines will be sent out
2217  * to consoles, not even serial console. This means when your driver crashes,
2218  * you will see absolutely nothing else but a system stuck in this function,
2219  * with no further output. Any kind of printk() you place within your own driver
2220  * or in the drm core modeset code will also never show up.
2221  *
2222  * Standard debug practice is to run the fbcon setup without taking the
2223  * console_lock as a hack, to be able to see backtraces and crashes on the
2224  * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
2225  * cmdline option.
2226  *
2227  * The other option is to just disable fbdev emulation since very likely the
2228  * first modeset from userspace will crash in the same way, and is even easier
2229  * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
2230  * kernel cmdline option.
2231  *
2232  * RETURNS:
2233  * Zero if everything went ok, nonzero otherwise.
2234  */
2235 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
2236 {
2237 	struct drm_device *dev = fb_helper->dev;
2238 	int count = 0;
2239 
2240 	if (!drm_fbdev_emulation)
2241 		return 0;
2242 
2243 	mutex_lock(&dev->mode_config.mutex);
2244 	count = drm_fb_helper_probe_connector_modes(fb_helper,
2245 						    dev->mode_config.max_width,
2246 						    dev->mode_config.max_height);
2247 	mutex_unlock(&dev->mode_config.mutex);
2248 	/*
2249 	 * we shouldn't end up with no modes here.
2250 	 */
2251 	if (count == 0)
2252 		dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
2253 
2254 	drm_setup_crtcs(fb_helper);
2255 
2256 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
2257 }
2258 EXPORT_SYMBOL(drm_fb_helper_initial_config);
2259 
2260 /**
2261  * drm_fb_helper_hotplug_event - respond to a hotplug notification by
2262  *                               probing all the outputs attached to the fb
2263  * @fb_helper: the drm_fb_helper
2264  *
2265  * Scan the connectors attached to the fb_helper and try to put together a
2266  * setup after *notification of a change in output configuration.
2267  *
2268  * Called at runtime, takes the mode config locks to be able to check/change the
2269  * modeset configuration. Must be run from process context (which usually means
2270  * either the output polling work or a work item launched from the driver's
2271  * hotplug interrupt).
2272  *
2273  * Note that drivers may call this even before calling
2274  * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
2275  * for a race-free fbcon setup and will make sure that the fbdev emulation will
2276  * not miss any hotplug events.
2277  *
2278  * RETURNS:
2279  * 0 on success and a non-zero error code otherwise.
2280  */
2281 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
2282 {
2283 	struct drm_device *dev = fb_helper->dev;
2284 	u32 max_width, max_height;
2285 
2286 	if (!drm_fbdev_emulation)
2287 		return 0;
2288 
2289 	mutex_lock(&fb_helper->dev->mode_config.mutex);
2290 	if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
2291 		fb_helper->delayed_hotplug = true;
2292 		mutex_unlock(&fb_helper->dev->mode_config.mutex);
2293 		return 0;
2294 	}
2295 	DRM_DEBUG_KMS("\n");
2296 
2297 	max_width = fb_helper->fb->width;
2298 	max_height = fb_helper->fb->height;
2299 
2300 	drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
2301 	mutex_unlock(&fb_helper->dev->mode_config.mutex);
2302 
2303 	drm_modeset_lock_all(dev);
2304 	drm_setup_crtcs(fb_helper);
2305 	drm_modeset_unlock_all(dev);
2306 	drm_fb_helper_set_par(fb_helper->fbdev);
2307 
2308 	return 0;
2309 }
2310 EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
2311 
2312 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
2313  * but the module doesn't depend on any fb console symbols.  At least
2314  * attempt to load fbcon to avoid leaving the system without a usable console.
2315  */
2316 int __init drm_fb_helper_modinit(void)
2317 {
2318 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
2319 	const char *name = "fbcon";
2320 	struct module *fbcon;
2321 
2322 	mutex_lock(&module_mutex);
2323 	fbcon = find_module(name);
2324 	mutex_unlock(&module_mutex);
2325 
2326 	if (!fbcon)
2327 		request_module_nowait(name);
2328 #endif
2329 	return 0;
2330 }
2331 EXPORT_SYMBOL(drm_fb_helper_modinit);
2332