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