nouveau_fbcon.c revision 1.11 1 /* $NetBSD: nouveau_fbcon.c,v 1.11 2021/08/07 16:19:17 thorpej 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.11 2021/08/07 16:19:17 thorpej 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 console_lock();
277 #ifndef __NetBSD__ /* XXX nouveau fbaccel */
278 fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
279 #endif
280 console_unlock();
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 #ifdef __NetBSD__
360 static int
361 nouveau_fbcon_print(void *aux, const char *pnp)
362 {
363 if (pnp)
364 aprint_normal("nouveaufbbus at %s", pnp);
365
366 return (UNCONF);
367 }
368 #endif
369
370 static int
371 nouveau_fbcon_create(struct drm_fb_helper *helper,
372 struct drm_fb_helper_surface_size *sizes)
373 {
374 struct nouveau_fbdev *fbcon =
375 container_of(helper, struct nouveau_fbdev, helper);
376 struct drm_device *dev = fbcon->dev;
377 struct nouveau_drm *drm = nouveau_drm(dev);
378 struct nvif_device *device = &drm->device;
379 #ifndef __NetBSD__
380 struct fb_info *info;
381 #endif
382 struct drm_framebuffer *fb;
383 struct nouveau_framebuffer *nouveau_fb;
384 struct nouveau_channel *chan;
385 struct nouveau_bo *nvbo;
386 struct drm_mode_fb_cmd2 mode_cmd;
387 int size, ret;
388
389 mode_cmd.width = sizes->surface_width;
390 mode_cmd.height = sizes->surface_height;
391
392 mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
393 mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
394
395 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
396 sizes->surface_depth);
397
398 size = mode_cmd.pitches[0] * mode_cmd.height;
399 size = roundup(size, PAGE_SIZE);
400
401 ret = nouveau_gem_new(dev, size, 0, NOUVEAU_GEM_DOMAIN_VRAM,
402 0, 0x0000, &nvbo);
403 if (ret) {
404 NV_ERROR(drm, "failed to allocate framebuffer\n");
405 goto out;
406 }
407
408 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, false);
409 if (ret) {
410 NV_ERROR(drm, "failed to pin fb: %d\n", ret);
411 goto out_unref;
412 }
413
414 ret = nouveau_bo_map(nvbo);
415 if (ret) {
416 NV_ERROR(drm, "failed to map fb: %d\n", ret);
417 goto out_unpin;
418 }
419
420 chan = nouveau_nofbaccel ? NULL : drm->channel;
421 if (chan && device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
422 ret = nouveau_bo_vma_add(nvbo, drm->client.vm,
423 &fbcon->nouveau_fb.vma);
424 if (ret) {
425 NV_ERROR(drm, "failed to map fb into chan: %d\n", ret);
426 chan = NULL;
427 }
428 }
429
430 #ifdef __NetBSD__
431 nouveau_framebuffer_init(dev, &fbcon->nouveau_fb, &mode_cmd, nvbo);
432 nouveau_fb = &fbcon->nouveau_fb;
433 fb = &nouveau_fb->base;
434
435 nouveau_fbcon_zfill(dev, fbcon);
436
437 struct nouveaufb_attach_args nfa;
438
439 memset(&nfa, 0, sizeof(nfa));
440 nfa.nfa_fb_helper = helper;
441 nfa.nfa_fb_sizes = *sizes;
442 nfa.nfa_fb_ptr = nvbo_kmap_obj_iovirtual(nvbo);
443 nfa.nfa_fb_linebytes = mode_cmd.pitches[0];
444
445 helper->fbdev = config_found(dev->dev, &nfa, nouveau_fbcon_print,
446 CFARGS(.iattr = "nouveaufbbus"));
447 if (helper->fbdev == NULL) {
448 goto out_unlock;
449 }
450
451 helper->fb = fb;
452
453 return 0;
454 #else
455 mutex_lock(&dev->struct_mutex);
456
457 info = drm_fb_helper_alloc_fbi(helper);
458 if (IS_ERR(info)) {
459 ret = PTR_ERR(info);
460 goto out_unlock;
461 }
462 info->skip_vt_switch = 1;
463
464 info->par = fbcon;
465
466 nouveau_framebuffer_init(dev, &fbcon->nouveau_fb, &mode_cmd, nvbo);
467
468 nouveau_fb = &fbcon->nouveau_fb;
469 fb = &nouveau_fb->base;
470
471 /* setup helper */
472 fbcon->helper.fb = fb;
473
474 strcpy(info->fix.id, "nouveaufb");
475 if (!chan)
476 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
477 else
478 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
479 FBINFO_HWACCEL_FILLRECT |
480 FBINFO_HWACCEL_IMAGEBLIT;
481 info->flags |= FBINFO_CAN_FORCE_OUTPUT;
482 info->fbops = &nouveau_fbcon_sw_ops;
483 info->fix.smem_start = nvbo->bo.mem.bus.base +
484 nvbo->bo.mem.bus.offset;
485 info->fix.smem_len = size;
486
487 info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
488 info->screen_size = size;
489
490 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
491 drm_fb_helper_fill_var(info, &fbcon->helper, sizes->fb_width, sizes->fb_height);
492
493 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
494
495 mutex_unlock(&dev->struct_mutex);
496
497 if (chan)
498 nouveau_fbcon_accel_init(dev);
499 nouveau_fbcon_zfill(dev, fbcon);
500
501 /* To allow resizeing without swapping buffers */
502 NV_INFO(drm, "allocated %dx%d fb: 0x%jx, bo %p\n",
503 nouveau_fb->base.width, nouveau_fb->base.height,
504 (uintmax_t)nvbo->bo.offset, nvbo);
505
506 vga_switcheroo_client_fb_set(dev->pdev, info);
507 return 0;
508 #endif /* defined(__NetBSD__) */
509
510 out_unlock:
511 #ifndef __NetBSD__
512 mutex_unlock(&dev->struct_mutex);
513 #endif
514 if (chan)
515 nouveau_bo_vma_del(nvbo, &fbcon->nouveau_fb.vma);
516 nouveau_bo_unmap(nvbo);
517 out_unpin:
518 nouveau_bo_unpin(nvbo);
519 out_unref:
520 nouveau_gem_object_del(&nvbo->gem);
521 out:
522 return ret;
523 }
524
525 void
526 nouveau_fbcon_output_poll_changed(struct drm_device *dev)
527 {
528 struct nouveau_drm *drm = nouveau_drm(dev);
529 if (drm->fbcon)
530 drm_fb_helper_hotplug_event(&drm->fbcon->helper);
531 }
532
533 static int
534 nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *fbcon)
535 {
536 struct nouveau_framebuffer *nouveau_fb = &fbcon->nouveau_fb;
537
538 #ifdef __NetBSD__
539 if (fbcon->helper.fbdev) {
540 int ret;
541
542 /* XXX errno NetBSD->Linux */
543 ret = -config_detach(fbcon->helper.fbdev, DETACH_FORCE);
544 if (ret)
545 DRM_ERROR("failed to detach nouveaufb: %d\n", ret);
546 fbcon->helper.fbdev = NULL;
547 }
548 #else
549 drm_fb_helper_unregister_fbi(&fbcon->helper);
550 drm_fb_helper_release_fbi(&fbcon->helper);
551 #endif
552
553 if (nouveau_fb->nvbo) {
554 nouveau_bo_unmap(nouveau_fb->nvbo);
555 nouveau_bo_vma_del(nouveau_fb->nvbo, &nouveau_fb->vma);
556 nouveau_bo_unpin(nouveau_fb->nvbo);
557 drm_gem_object_unreference_unlocked(&nouveau_fb->nvbo->gem);
558 nouveau_fb->nvbo = NULL;
559 }
560 drm_fb_helper_fini(&fbcon->helper);
561 drm_framebuffer_unregister_private(&nouveau_fb->base);
562 drm_framebuffer_cleanup(&nouveau_fb->base);
563 return 0;
564 }
565
566 #ifndef __NetBSD__ /* XXX nouveau fbaccel */
567 void nouveau_fbcon_gpu_lockup(struct fb_info *info)
568 {
569 struct nouveau_fbdev *fbcon = info->par;
570 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
571
572 NV_ERROR(drm, "GPU lockup - switching to software fbcon\n");
573 info->flags |= FBINFO_HWACCEL_DISABLED;
574 }
575 #endif
576
577 static const struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
578 .gamma_set = nouveau_fbcon_gamma_set,
579 .gamma_get = nouveau_fbcon_gamma_get,
580 .fb_probe = nouveau_fbcon_create,
581 };
582
583 void
584 nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
585 {
586 struct nouveau_drm *drm = nouveau_drm(dev);
587 if (drm->fbcon) {
588 console_lock();
589 if (state == FBINFO_STATE_RUNNING)
590 nouveau_fbcon_accel_restore(dev);
591 drm_fb_helper_set_suspend(&drm->fbcon->helper, state);
592 if (state != FBINFO_STATE_RUNNING)
593 nouveau_fbcon_accel_save_disable(dev);
594 console_unlock();
595 }
596 }
597
598 int
599 nouveau_fbcon_init(struct drm_device *dev)
600 {
601 struct nouveau_drm *drm = nouveau_drm(dev);
602 struct nouveau_fbdev *fbcon;
603 int preferred_bpp;
604 int ret;
605
606 if (!dev->mode_config.num_crtc ||
607 (dev->pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
608 return 0;
609
610 fbcon = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
611 if (!fbcon)
612 return -ENOMEM;
613
614 fbcon->dev = dev;
615 drm->fbcon = fbcon;
616
617 drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
618
619 ret = drm_fb_helper_init(dev, &fbcon->helper,
620 dev->mode_config.num_crtc, 4);
621 if (ret)
622 goto free;
623
624 ret = drm_fb_helper_single_add_all_connectors(&fbcon->helper);
625 if (ret)
626 goto fini;
627
628 if (drm->device.info.ram_size <= 32 * 1024 * 1024)
629 preferred_bpp = 8;
630 else
631 if (drm->device.info.ram_size <= 64 * 1024 * 1024)
632 preferred_bpp = 16;
633 else
634 preferred_bpp = 32;
635
636 /* disable all the possible outputs/crtcs before entering KMS mode */
637 drm_helper_disable_unused_functions(dev);
638
639 ret = drm_fb_helper_initial_config(&fbcon->helper, preferred_bpp);
640 if (ret)
641 goto fini;
642
643 #ifndef __NetBSD__
644 if (fbcon->helper.fbdev)
645 fbcon->helper.fbdev->pixmap.buf_align = 4;
646 #endif
647 return 0;
648
649 fini:
650 drm_fb_helper_fini(&fbcon->helper);
651 free:
652 kfree(fbcon);
653 return ret;
654 }
655
656 void
657 nouveau_fbcon_fini(struct drm_device *dev)
658 {
659 struct nouveau_drm *drm = nouveau_drm(dev);
660
661 if (!drm->fbcon)
662 return;
663
664 nouveau_fbcon_accel_fini(dev);
665 nouveau_fbcon_destroy(dev, drm->fbcon);
666 kfree(drm->fbcon);
667 drm->fbcon = NULL;
668 }
669