1 /*
2  * Copyright © 2007 David Airlie
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *     David Airlie
25  */
26 
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/string.h>
31 #include <linux/mm.h>
32 #include <linux/tty.h>
33 #include <linux/sysrq.h>
34 #include <linux/delay.h>
35 #include <linux/init.h>
36 #include <linux/screen_info.h>
37 #include <linux/vga_switcheroo.h>
38 #include <linux/console.h>
39 
40 #include <drm/drm_crtc.h>
41 #include <drm/drm_crtc_helper.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_fb_helper.h>
44 #include <drm/drm_fourcc.h>
45 #include <drm/drm_atomic.h>
46 
47 #include "nouveau_drv.h"
48 #include "nouveau_gem.h"
49 #include "nouveau_bo.h"
50 #include "nouveau_fbcon.h"
51 #include "nouveau_chan.h"
52 #include "nouveau_vmm.h"
53 
54 #include "nouveau_crtc.h"
55 
56 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
57 int nouveau_nofbaccel = 0;
58 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
59 
60 MODULE_PARM_DESC(fbcon_bpp, "fbcon bits-per-pixel (default: auto)");
61 static int nouveau_fbcon_bpp;
62 module_param_named(fbcon_bpp, nouveau_fbcon_bpp, int, 0400);
63 
64 static void
nouveau_fbcon_fillrect(struct fb_info * info,const struct fb_fillrect * rect)65 nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
66 {
67 	struct nouveau_fbdev *fbcon = info->par;
68 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
69 	struct nvif_device *device = &drm->client.device;
70 	int ret;
71 
72 	if (info->state != FBINFO_STATE_RUNNING)
73 		return;
74 
75 	ret = -ENODEV;
76 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
77 	    mutex_trylock(&drm->client.mutex)) {
78 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
79 			ret = nv04_fbcon_fillrect(info, rect);
80 		else
81 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
82 			ret = nv50_fbcon_fillrect(info, rect);
83 		else
84 			ret = nvc0_fbcon_fillrect(info, rect);
85 		mutex_unlock(&drm->client.mutex);
86 	}
87 
88 	if (ret == 0)
89 		return;
90 
91 	if (ret != -ENODEV)
92 		nouveau_fbcon_gpu_lockup(info);
93 	drm_fb_helper_cfb_fillrect(info, rect);
94 }
95 
96 static void
nouveau_fbcon_copyarea(struct fb_info * info,const struct fb_copyarea * image)97 nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
98 {
99 	struct nouveau_fbdev *fbcon = info->par;
100 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
101 	struct nvif_device *device = &drm->client.device;
102 	int ret;
103 
104 	if (info->state != FBINFO_STATE_RUNNING)
105 		return;
106 
107 	ret = -ENODEV;
108 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
109 	    mutex_trylock(&drm->client.mutex)) {
110 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
111 			ret = nv04_fbcon_copyarea(info, image);
112 		else
113 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
114 			ret = nv50_fbcon_copyarea(info, image);
115 		else
116 			ret = nvc0_fbcon_copyarea(info, image);
117 		mutex_unlock(&drm->client.mutex);
118 	}
119 
120 	if (ret == 0)
121 		return;
122 
123 	if (ret != -ENODEV)
124 		nouveau_fbcon_gpu_lockup(info);
125 	drm_fb_helper_cfb_copyarea(info, image);
126 }
127 
128 static void
nouveau_fbcon_imageblit(struct fb_info * info,const struct fb_image * image)129 nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
130 {
131 	struct nouveau_fbdev *fbcon = info->par;
132 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
133 	struct nvif_device *device = &drm->client.device;
134 	int ret;
135 
136 	if (info->state != FBINFO_STATE_RUNNING)
137 		return;
138 
139 	ret = -ENODEV;
140 	if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
141 	    mutex_trylock(&drm->client.mutex)) {
142 		if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
143 			ret = nv04_fbcon_imageblit(info, image);
144 		else
145 		if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
146 			ret = nv50_fbcon_imageblit(info, image);
147 		else
148 			ret = nvc0_fbcon_imageblit(info, image);
149 		mutex_unlock(&drm->client.mutex);
150 	}
151 
152 	if (ret == 0)
153 		return;
154 
155 	if (ret != -ENODEV)
156 		nouveau_fbcon_gpu_lockup(info);
157 	drm_fb_helper_cfb_imageblit(info, image);
158 }
159 
160 static int
nouveau_fbcon_sync(struct fb_info * info)161 nouveau_fbcon_sync(struct fb_info *info)
162 {
163 	struct nouveau_fbdev *fbcon = info->par;
164 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
165 	struct nouveau_channel *chan = drm->channel;
166 	int ret;
167 
168 	if (!chan || !chan->accel_done || in_interrupt() ||
169 	    info->state != FBINFO_STATE_RUNNING ||
170 	    info->flags & FBINFO_HWACCEL_DISABLED)
171 		return 0;
172 
173 	if (!mutex_trylock(&drm->client.mutex))
174 		return 0;
175 
176 	ret = nouveau_channel_idle(chan);
177 	mutex_unlock(&drm->client.mutex);
178 	if (ret) {
179 		nouveau_fbcon_gpu_lockup(info);
180 		return 0;
181 	}
182 
183 	chan->accel_done = false;
184 	return 0;
185 }
186 
187 static int
nouveau_fbcon_open(struct fb_info * info,int user)188 nouveau_fbcon_open(struct fb_info *info, int user)
189 {
190 	struct nouveau_fbdev *fbcon = info->par;
191 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
192 	int ret = pm_runtime_get_sync(drm->dev->dev);
193 	if (ret < 0 && ret != -EACCES) {
194 		pm_runtime_put(drm->dev->dev);
195 		return ret;
196 	}
197 	return 0;
198 }
199 
200 static int
nouveau_fbcon_release(struct fb_info * info,int user)201 nouveau_fbcon_release(struct fb_info *info, int user)
202 {
203 	struct nouveau_fbdev *fbcon = info->par;
204 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
205 	pm_runtime_put(drm->dev->dev);
206 	return 0;
207 }
208 
209 static const struct fb_ops nouveau_fbcon_ops = {
210 	.owner = THIS_MODULE,
211 	DRM_FB_HELPER_DEFAULT_OPS,
212 	.fb_open = nouveau_fbcon_open,
213 	.fb_release = nouveau_fbcon_release,
214 	.fb_fillrect = nouveau_fbcon_fillrect,
215 	.fb_copyarea = nouveau_fbcon_copyarea,
216 	.fb_imageblit = nouveau_fbcon_imageblit,
217 	.fb_sync = nouveau_fbcon_sync,
218 };
219 
220 static const struct fb_ops nouveau_fbcon_sw_ops = {
221 	.owner = THIS_MODULE,
222 	DRM_FB_HELPER_DEFAULT_OPS,
223 	.fb_open = nouveau_fbcon_open,
224 	.fb_release = nouveau_fbcon_release,
225 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
226 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
227 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
228 };
229 
230 void
nouveau_fbcon_accel_save_disable(struct drm_device * dev)231 nouveau_fbcon_accel_save_disable(struct drm_device *dev)
232 {
233 	struct nouveau_drm *drm = nouveau_drm(dev);
234 	if (drm->fbcon && drm->fbcon->helper.fbdev) {
235 		drm->fbcon->saved_flags = drm->fbcon->helper.fbdev->flags;
236 		drm->fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
237 	}
238 }
239 
240 void
nouveau_fbcon_accel_restore(struct drm_device * dev)241 nouveau_fbcon_accel_restore(struct drm_device *dev)
242 {
243 	struct nouveau_drm *drm = nouveau_drm(dev);
244 	if (drm->fbcon && drm->fbcon->helper.fbdev) {
245 		drm->fbcon->helper.fbdev->flags = drm->fbcon->saved_flags;
246 	}
247 }
248 
249 static void
nouveau_fbcon_accel_fini(struct drm_device * dev)250 nouveau_fbcon_accel_fini(struct drm_device *dev)
251 {
252 	struct nouveau_drm *drm = nouveau_drm(dev);
253 	struct nouveau_fbdev *fbcon = drm->fbcon;
254 	if (fbcon && drm->channel) {
255 		console_lock();
256 		if (fbcon->helper.fbdev)
257 			fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
258 		console_unlock();
259 		nouveau_channel_idle(drm->channel);
260 		nvif_object_dtor(&fbcon->twod);
261 		nvif_object_dtor(&fbcon->blit);
262 		nvif_object_dtor(&fbcon->gdi);
263 		nvif_object_dtor(&fbcon->patt);
264 		nvif_object_dtor(&fbcon->rop);
265 		nvif_object_dtor(&fbcon->clip);
266 		nvif_object_dtor(&fbcon->surf2d);
267 	}
268 }
269 
270 static void
nouveau_fbcon_accel_init(struct drm_device * dev)271 nouveau_fbcon_accel_init(struct drm_device *dev)
272 {
273 	struct nouveau_drm *drm = nouveau_drm(dev);
274 	struct nouveau_fbdev *fbcon = drm->fbcon;
275 	struct fb_info *info = fbcon->helper.fbdev;
276 	int ret;
277 
278 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA)
279 		ret = nv04_fbcon_accel_init(info);
280 	else
281 	if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI)
282 		ret = nv50_fbcon_accel_init(info);
283 	else
284 		ret = nvc0_fbcon_accel_init(info);
285 
286 	if (ret == 0)
287 		info->fbops = &nouveau_fbcon_ops;
288 }
289 
290 static void
nouveau_fbcon_zfill(struct drm_device * dev,struct nouveau_fbdev * fbcon)291 nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *fbcon)
292 {
293 	struct fb_info *info = fbcon->helper.fbdev;
294 	struct fb_fillrect rect;
295 
296 	/* Clear the entire fbcon.  The drm will program every connector
297 	 * with it's preferred mode.  If the sizes differ, one display will
298 	 * quite likely have garbage around the console.
299 	 */
300 	rect.dx = rect.dy = 0;
301 	rect.width = info->var.xres_virtual;
302 	rect.height = info->var.yres_virtual;
303 	rect.color = 0;
304 	rect.rop = ROP_COPY;
305 	info->fbops->fb_fillrect(info, &rect);
306 }
307 
308 static int
nouveau_fbcon_create(struct drm_fb_helper * helper,struct drm_fb_helper_surface_size * sizes)309 nouveau_fbcon_create(struct drm_fb_helper *helper,
310 		     struct drm_fb_helper_surface_size *sizes)
311 {
312 	struct nouveau_fbdev *fbcon =
313 		container_of(helper, struct nouveau_fbdev, helper);
314 	struct drm_device *dev = fbcon->helper.dev;
315 	struct nouveau_drm *drm = nouveau_drm(dev);
316 	struct nvif_device *device = &drm->client.device;
317 	struct fb_info *info;
318 	struct drm_framebuffer *fb;
319 	struct nouveau_channel *chan;
320 	struct nouveau_bo *nvbo;
321 	struct drm_mode_fb_cmd2 mode_cmd = {};
322 	int ret;
323 
324 	mode_cmd.width = sizes->surface_width;
325 	mode_cmd.height = sizes->surface_height;
326 
327 	mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
328 	mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
329 
330 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
331 							  sizes->surface_depth);
332 
333 	ret = nouveau_gem_new(&drm->client, mode_cmd.pitches[0] *
334 			      mode_cmd.height, 0, NOUVEAU_GEM_DOMAIN_VRAM,
335 			      0, 0x0000, &nvbo);
336 	if (ret) {
337 		NV_ERROR(drm, "failed to allocate framebuffer\n");
338 		goto out;
339 	}
340 
341 	ret = nouveau_framebuffer_new(dev, &mode_cmd, &nvbo->bo.base, &fb);
342 	if (ret)
343 		goto out_unref;
344 
345 	ret = nouveau_bo_pin(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, false);
346 	if (ret) {
347 		NV_ERROR(drm, "failed to pin fb: %d\n", ret);
348 		goto out_unref;
349 	}
350 
351 	ret = nouveau_bo_map(nvbo);
352 	if (ret) {
353 		NV_ERROR(drm, "failed to map fb: %d\n", ret);
354 		goto out_unpin;
355 	}
356 
357 	chan = nouveau_nofbaccel ? NULL : drm->channel;
358 	if (chan && device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
359 		ret = nouveau_vma_new(nvbo, chan->vmm, &fbcon->vma);
360 		if (ret) {
361 			NV_ERROR(drm, "failed to map fb into chan: %d\n", ret);
362 			chan = NULL;
363 		}
364 	}
365 
366 	info = drm_fb_helper_alloc_fbi(helper);
367 	if (IS_ERR(info)) {
368 		ret = PTR_ERR(info);
369 		goto out_unlock;
370 	}
371 
372 	/* setup helper */
373 	fbcon->helper.fb = fb;
374 
375 	if (!chan)
376 		info->flags = FBINFO_HWACCEL_DISABLED;
377 	else
378 		info->flags = FBINFO_HWACCEL_COPYAREA |
379 			      FBINFO_HWACCEL_FILLRECT |
380 			      FBINFO_HWACCEL_IMAGEBLIT;
381 	info->fbops = &nouveau_fbcon_sw_ops;
382 	info->fix.smem_start = nvbo->bo.resource->bus.offset;
383 	info->fix.smem_len = nvbo->bo.base.size;
384 
385 	info->screen_base = nvbo_kmap_obj_iovirtual(nvbo);
386 	info->screen_size = nvbo->bo.base.size;
387 
388 	drm_fb_helper_fill_info(info, &fbcon->helper, sizes);
389 
390 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
391 
392 	if (chan)
393 		nouveau_fbcon_accel_init(dev);
394 	nouveau_fbcon_zfill(dev, fbcon);
395 
396 	/* To allow resizeing without swapping buffers */
397 	NV_INFO(drm, "allocated %dx%d fb: 0x%llx, bo %p\n",
398 		fb->width, fb->height, nvbo->offset, nvbo);
399 
400 	if (dev_is_pci(dev->dev))
401 		vga_switcheroo_client_fb_set(to_pci_dev(dev->dev), info);
402 
403 	return 0;
404 
405 out_unlock:
406 	if (chan)
407 		nouveau_vma_del(&fbcon->vma);
408 	nouveau_bo_unmap(nvbo);
409 out_unpin:
410 	nouveau_bo_unpin(nvbo);
411 out_unref:
412 	nouveau_bo_ref(NULL, &nvbo);
413 out:
414 	return ret;
415 }
416 
417 static int
nouveau_fbcon_destroy(struct drm_device * dev,struct nouveau_fbdev * fbcon)418 nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *fbcon)
419 {
420 	struct drm_framebuffer *fb = fbcon->helper.fb;
421 	struct nouveau_bo *nvbo;
422 
423 	drm_fb_helper_unregister_fbi(&fbcon->helper);
424 	drm_fb_helper_fini(&fbcon->helper);
425 
426 	if (fb && fb->obj[0]) {
427 		nvbo = nouveau_gem_object(fb->obj[0]);
428 		nouveau_vma_del(&fbcon->vma);
429 		nouveau_bo_unmap(nvbo);
430 		nouveau_bo_unpin(nvbo);
431 		drm_framebuffer_put(fb);
432 	}
433 
434 	return 0;
435 }
436 
nouveau_fbcon_gpu_lockup(struct fb_info * info)437 void nouveau_fbcon_gpu_lockup(struct fb_info *info)
438 {
439 	struct nouveau_fbdev *fbcon = info->par;
440 	struct nouveau_drm *drm = nouveau_drm(fbcon->helper.dev);
441 
442 	NV_ERROR(drm, "GPU lockup - switching to software fbcon\n");
443 	info->flags |= FBINFO_HWACCEL_DISABLED;
444 }
445 
446 static const struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
447 	.fb_probe = nouveau_fbcon_create,
448 };
449 
450 static void
nouveau_fbcon_set_suspend_work(struct work_struct * work)451 nouveau_fbcon_set_suspend_work(struct work_struct *work)
452 {
453 	struct nouveau_drm *drm = container_of(work, typeof(*drm), fbcon_work);
454 	int state = READ_ONCE(drm->fbcon_new_state);
455 
456 	if (state == FBINFO_STATE_RUNNING)
457 		pm_runtime_get_sync(drm->dev->dev);
458 
459 	console_lock();
460 	if (state == FBINFO_STATE_RUNNING)
461 		nouveau_fbcon_accel_restore(drm->dev);
462 	drm_fb_helper_set_suspend(&drm->fbcon->helper, state);
463 	if (state != FBINFO_STATE_RUNNING)
464 		nouveau_fbcon_accel_save_disable(drm->dev);
465 	console_unlock();
466 
467 	if (state == FBINFO_STATE_RUNNING) {
468 		nouveau_fbcon_hotplug_resume(drm->fbcon);
469 		pm_runtime_mark_last_busy(drm->dev->dev);
470 		pm_runtime_put_autosuspend(drm->dev->dev);
471 	}
472 }
473 
474 void
nouveau_fbcon_set_suspend(struct drm_device * dev,int state)475 nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
476 {
477 	struct nouveau_drm *drm = nouveau_drm(dev);
478 
479 	if (!drm->fbcon)
480 		return;
481 
482 	drm->fbcon_new_state = state;
483 	/* Since runtime resume can happen as a result of a sysfs operation,
484 	 * it's possible we already have the console locked. So handle fbcon
485 	 * init/deinit from a seperate work thread
486 	 */
487 	schedule_work(&drm->fbcon_work);
488 }
489 
490 void
nouveau_fbcon_output_poll_changed(struct drm_device * dev)491 nouveau_fbcon_output_poll_changed(struct drm_device *dev)
492 {
493 	struct nouveau_drm *drm = nouveau_drm(dev);
494 	struct nouveau_fbdev *fbcon = drm->fbcon;
495 	int ret;
496 
497 	if (!fbcon)
498 		return;
499 
500 	mutex_lock(&fbcon->hotplug_lock);
501 
502 	ret = pm_runtime_get(dev->dev);
503 	if (ret == 1 || ret == -EACCES) {
504 		drm_fb_helper_hotplug_event(&fbcon->helper);
505 
506 		pm_runtime_mark_last_busy(dev->dev);
507 		pm_runtime_put_autosuspend(dev->dev);
508 	} else if (ret == 0) {
509 		/* If the GPU was already in the process of suspending before
510 		 * this event happened, then we can't block here as we'll
511 		 * deadlock the runtime pmops since they wait for us to
512 		 * finish. So, just defer this event for when we runtime
513 		 * resume again. It will be handled by fbcon_work.
514 		 */
515 		NV_DEBUG(drm, "fbcon HPD event deferred until runtime resume\n");
516 		fbcon->hotplug_waiting = true;
517 		pm_runtime_put_noidle(drm->dev->dev);
518 	} else {
519 		DRM_WARN("fbcon HPD event lost due to RPM failure: %d\n",
520 			 ret);
521 	}
522 
523 	mutex_unlock(&fbcon->hotplug_lock);
524 }
525 
526 void
nouveau_fbcon_hotplug_resume(struct nouveau_fbdev * fbcon)527 nouveau_fbcon_hotplug_resume(struct nouveau_fbdev *fbcon)
528 {
529 	struct nouveau_drm *drm;
530 
531 	if (!fbcon)
532 		return;
533 	drm = nouveau_drm(fbcon->helper.dev);
534 
535 	mutex_lock(&fbcon->hotplug_lock);
536 	if (fbcon->hotplug_waiting) {
537 		fbcon->hotplug_waiting = false;
538 
539 		NV_DEBUG(drm, "Handling deferred fbcon HPD events\n");
540 		drm_fb_helper_hotplug_event(&fbcon->helper);
541 	}
542 	mutex_unlock(&fbcon->hotplug_lock);
543 }
544 
545 int
nouveau_fbcon_init(struct drm_device * dev)546 nouveau_fbcon_init(struct drm_device *dev)
547 {
548 	struct nouveau_drm *drm = nouveau_drm(dev);
549 	struct nouveau_fbdev *fbcon;
550 	int preferred_bpp = nouveau_fbcon_bpp;
551 	int ret;
552 
553 	if (!dev->mode_config.num_crtc ||
554 	    (to_pci_dev(dev->dev)->class >> 8) != PCI_CLASS_DISPLAY_VGA)
555 		return 0;
556 
557 	fbcon = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
558 	if (!fbcon)
559 		return -ENOMEM;
560 
561 	drm->fbcon = fbcon;
562 	INIT_WORK(&drm->fbcon_work, nouveau_fbcon_set_suspend_work);
563 	mutex_init(&fbcon->hotplug_lock);
564 
565 	drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
566 
567 	ret = drm_fb_helper_init(dev, &fbcon->helper);
568 	if (ret)
569 		goto free;
570 
571 	if (preferred_bpp != 8 && preferred_bpp != 16 && preferred_bpp != 32) {
572 		if (drm->client.device.info.ram_size <= 32 * 1024 * 1024)
573 			preferred_bpp = 8;
574 		else
575 		if (drm->client.device.info.ram_size <= 64 * 1024 * 1024)
576 			preferred_bpp = 16;
577 		else
578 			preferred_bpp = 32;
579 	}
580 
581 	/* disable all the possible outputs/crtcs before entering KMS mode */
582 	if (!drm_drv_uses_atomic_modeset(dev))
583 		drm_helper_disable_unused_functions(dev);
584 
585 	ret = drm_fb_helper_initial_config(&fbcon->helper, preferred_bpp);
586 	if (ret)
587 		goto fini;
588 
589 	if (fbcon->helper.fbdev)
590 		fbcon->helper.fbdev->pixmap.buf_align = 4;
591 	return 0;
592 
593 fini:
594 	drm_fb_helper_fini(&fbcon->helper);
595 free:
596 	kfree(fbcon);
597 	drm->fbcon = NULL;
598 	return ret;
599 }
600 
601 void
nouveau_fbcon_fini(struct drm_device * dev)602 nouveau_fbcon_fini(struct drm_device *dev)
603 {
604 	struct nouveau_drm *drm = nouveau_drm(dev);
605 
606 	if (!drm->fbcon)
607 		return;
608 
609 	drm_kms_helper_poll_fini(dev);
610 	nouveau_fbcon_accel_fini(dev);
611 	nouveau_fbcon_destroy(dev, drm->fbcon);
612 	kfree(drm->fbcon);
613 	drm->fbcon = NULL;
614 }
615