nouveau_fbcon.c revision 1.1.1.3 1 /* $NetBSD: nouveau_fbcon.c,v 1.1.1.3 2018/08/27 01:34:55 riastradh 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.1.1.3 2018/08/27 01:34:55 riastradh 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 MODULE_PARM_DESC(nofbaccel, "Disable fbcon acceleration");
60 int nouveau_nofbaccel = 0;
61 module_param_named(nofbaccel, nouveau_nofbaccel, int, 0400);
62
63 static void
64 nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
65 {
66 struct nouveau_fbdev *fbcon = info->par;
67 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
68 struct nvif_device *device = &drm->device;
69 int ret;
70
71 if (info->state != FBINFO_STATE_RUNNING)
72 return;
73
74 ret = -ENODEV;
75 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
76 mutex_trylock(&drm->client.mutex)) {
77 if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
78 ret = nv04_fbcon_fillrect(info, rect);
79 else
80 if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
81 ret = nv50_fbcon_fillrect(info, rect);
82 else
83 ret = nvc0_fbcon_fillrect(info, rect);
84 mutex_unlock(&drm->client.mutex);
85 }
86
87 if (ret == 0)
88 return;
89
90 if (ret != -ENODEV)
91 nouveau_fbcon_gpu_lockup(info);
92 drm_fb_helper_cfb_fillrect(info, rect);
93 }
94
95 static void
96 nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
97 {
98 struct nouveau_fbdev *fbcon = info->par;
99 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
100 struct nvif_device *device = &drm->device;
101 int ret;
102
103 if (info->state != FBINFO_STATE_RUNNING)
104 return;
105
106 ret = -ENODEV;
107 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
108 mutex_trylock(&drm->client.mutex)) {
109 if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
110 ret = nv04_fbcon_copyarea(info, image);
111 else
112 if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
113 ret = nv50_fbcon_copyarea(info, image);
114 else
115 ret = nvc0_fbcon_copyarea(info, image);
116 mutex_unlock(&drm->client.mutex);
117 }
118
119 if (ret == 0)
120 return;
121
122 if (ret != -ENODEV)
123 nouveau_fbcon_gpu_lockup(info);
124 drm_fb_helper_cfb_copyarea(info, image);
125 }
126
127 static void
128 nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
129 {
130 struct nouveau_fbdev *fbcon = info->par;
131 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
132 struct nvif_device *device = &drm->device;
133 int ret;
134
135 if (info->state != FBINFO_STATE_RUNNING)
136 return;
137
138 ret = -ENODEV;
139 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
140 mutex_trylock(&drm->client.mutex)) {
141 if (device->info.family < NV_DEVICE_INFO_V0_TESLA)
142 ret = nv04_fbcon_imageblit(info, image);
143 else
144 if (device->info.family < NV_DEVICE_INFO_V0_FERMI)
145 ret = nv50_fbcon_imageblit(info, image);
146 else
147 ret = nvc0_fbcon_imageblit(info, image);
148 mutex_unlock(&drm->client.mutex);
149 }
150
151 if (ret == 0)
152 return;
153
154 if (ret != -ENODEV)
155 nouveau_fbcon_gpu_lockup(info);
156 drm_fb_helper_cfb_imageblit(info, image);
157 }
158
159 static int
160 nouveau_fbcon_sync(struct fb_info *info)
161 {
162 struct nouveau_fbdev *fbcon = info->par;
163 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
164 struct nouveau_channel *chan = drm->channel;
165 int ret;
166
167 if (!chan || !chan->accel_done || in_interrupt() ||
168 info->state != FBINFO_STATE_RUNNING ||
169 info->flags & FBINFO_HWACCEL_DISABLED)
170 return 0;
171
172 if (!mutex_trylock(&drm->client.mutex))
173 return 0;
174
175 ret = nouveau_channel_idle(chan);
176 mutex_unlock(&drm->client.mutex);
177 if (ret) {
178 nouveau_fbcon_gpu_lockup(info);
179 return 0;
180 }
181
182 chan->accel_done = false;
183 return 0;
184 }
185
186 static int
187 nouveau_fbcon_open(struct fb_info *info, int user)
188 {
189 struct nouveau_fbdev *fbcon = info->par;
190 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
191 int ret = pm_runtime_get_sync(drm->dev->dev);
192 if (ret < 0 && ret != -EACCES)
193 return ret;
194 return 0;
195 }
196
197 static int
198 nouveau_fbcon_release(struct fb_info *info, int user)
199 {
200 struct nouveau_fbdev *fbcon = info->par;
201 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
202 pm_runtime_put(drm->dev->dev);
203 return 0;
204 }
205
206 static struct fb_ops nouveau_fbcon_ops = {
207 .owner = THIS_MODULE,
208 .fb_open = nouveau_fbcon_open,
209 .fb_release = nouveau_fbcon_release,
210 .fb_check_var = drm_fb_helper_check_var,
211 .fb_set_par = drm_fb_helper_set_par,
212 .fb_fillrect = nouveau_fbcon_fillrect,
213 .fb_copyarea = nouveau_fbcon_copyarea,
214 .fb_imageblit = nouveau_fbcon_imageblit,
215 .fb_sync = nouveau_fbcon_sync,
216 .fb_pan_display = drm_fb_helper_pan_display,
217 .fb_blank = drm_fb_helper_blank,
218 .fb_setcmap = drm_fb_helper_setcmap,
219 .fb_debug_enter = drm_fb_helper_debug_enter,
220 .fb_debug_leave = drm_fb_helper_debug_leave,
221 };
222
223 static struct fb_ops nouveau_fbcon_sw_ops = {
224 .owner = THIS_MODULE,
225 .fb_open = nouveau_fbcon_open,
226 .fb_release = nouveau_fbcon_release,
227 .fb_check_var = drm_fb_helper_check_var,
228 .fb_set_par = drm_fb_helper_set_par,
229 .fb_fillrect = drm_fb_helper_cfb_fillrect,
230 .fb_copyarea = drm_fb_helper_cfb_copyarea,
231 .fb_imageblit = drm_fb_helper_cfb_imageblit,
232 .fb_pan_display = drm_fb_helper_pan_display,
233 .fb_blank = drm_fb_helper_blank,
234 .fb_setcmap = drm_fb_helper_setcmap,
235 .fb_debug_enter = drm_fb_helper_debug_enter,
236 .fb_debug_leave = drm_fb_helper_debug_leave,
237 };
238
239 void
240 nouveau_fbcon_accel_save_disable(struct drm_device *dev)
241 {
242 struct nouveau_drm *drm = nouveau_drm(dev);
243 if (drm->fbcon) {
244 drm->fbcon->saved_flags = drm->fbcon->helper.fbdev->flags;
245 drm->fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
246 }
247 }
248
249 void
250 nouveau_fbcon_accel_restore(struct drm_device *dev)
251 {
252 struct nouveau_drm *drm = nouveau_drm(dev);
253 if (drm->fbcon) {
254 drm->fbcon->helper.fbdev->flags = drm->fbcon->saved_flags;
255 }
256 }
257
258 static void
259 nouveau_fbcon_accel_fini(struct drm_device *dev)
260 {
261 struct nouveau_drm *drm = nouveau_drm(dev);
262 struct nouveau_fbdev *fbcon = drm->fbcon;
263 if (fbcon && drm->channel) {
264 console_lock();
265 fbcon->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
266 console_unlock();
267 nouveau_channel_idle(drm->channel);
268 nvif_object_fini(&fbcon->twod);
269 nvif_object_fini(&fbcon->blit);
270 nvif_object_fini(&fbcon->gdi);
271 nvif_object_fini(&fbcon->patt);
272 nvif_object_fini(&fbcon->rop);
273 nvif_object_fini(&fbcon->clip);
274 nvif_object_fini(&fbcon->surf2d);
275 }
276 }
277
278 static void
279 nouveau_fbcon_accel_init(struct drm_device *dev)
280 {
281 struct nouveau_drm *drm = nouveau_drm(dev);
282 struct nouveau_fbdev *fbcon = drm->fbcon;
283 struct fb_info *info = fbcon->helper.fbdev;
284 int ret;
285
286 if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA)
287 ret = nv04_fbcon_accel_init(info);
288 else
289 if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI)
290 ret = nv50_fbcon_accel_init(info);
291 else
292 ret = nvc0_fbcon_accel_init(info);
293
294 if (ret == 0)
295 info->fbops = &nouveau_fbcon_ops;
296 }
297
298 static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
299 u16 blue, int regno)
300 {
301 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
302
303 nv_crtc->lut.r[regno] = red;
304 nv_crtc->lut.g[regno] = green;
305 nv_crtc->lut.b[regno] = blue;
306 }
307
308 static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
309 u16 *blue, int regno)
310 {
311 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
312
313 *red = nv_crtc->lut.r[regno];
314 *green = nv_crtc->lut.g[regno];
315 *blue = nv_crtc->lut.b[regno];
316 }
317
318 static void
319 nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *fbcon)
320 {
321 struct fb_info *info = fbcon->helper.fbdev;
322 struct fb_fillrect rect;
323
324 /* Clear the entire fbcon. The drm will program every connector
325 * with it's preferred mode. If the sizes differ, one display will
326 * quite likely have garbage around the console.
327 */
328 rect.dx = rect.dy = 0;
329 rect.width = info->var.xres_virtual;
330 rect.height = info->var.yres_virtual;
331 rect.color = 0;
332 rect.rop = ROP_COPY;
333 info->fbops->fb_fillrect(info, &rect);
334 }
335
336 static int
337 nouveau_fbcon_create(struct drm_fb_helper *helper,
338 struct drm_fb_helper_surface_size *sizes)
339 {
340 struct nouveau_fbdev *fbcon =
341 container_of(helper, struct nouveau_fbdev, helper);
342 struct drm_device *dev = fbcon->dev;
343 struct nouveau_drm *drm = nouveau_drm(dev);
344 struct nvif_device *device = &drm->device;
345 struct fb_info *info;
346 struct drm_framebuffer *fb;
347 struct nouveau_framebuffer *nouveau_fb;
348 struct nouveau_channel *chan;
349 struct nouveau_bo *nvbo;
350 struct drm_mode_fb_cmd2 mode_cmd;
351 int size, ret;
352
353 mode_cmd.width = sizes->surface_width;
354 mode_cmd.height = sizes->surface_height;
355
356 mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
357 mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
358
359 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
360 sizes->surface_depth);
361
362 size = mode_cmd.pitches[0] * mode_cmd.height;
363 size = roundup(size, PAGE_SIZE);
364
365 ret = nouveau_gem_new(dev, size, 0, NOUVEAU_GEM_DOMAIN_VRAM,
366 0, 0x0000, &nvbo);
367 if (ret) {
368 NV_ERROR(drm, "failed to allocate framebuffer\n");
369 goto out;
370 }
371
372 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM, false);
373 if (ret) {
374 NV_ERROR(drm, "failed to pin fb: %d\n", ret);
375 goto out_unref;
376 }
377
378 ret = nouveau_bo_map(nvbo);
379 if (ret) {
380 NV_ERROR(drm, "failed to map fb: %d\n", ret);
381 goto out_unpin;
382 }
383
384 chan = nouveau_nofbaccel ? NULL : drm->channel;
385 if (chan && device->info.family >= NV_DEVICE_INFO_V0_TESLA) {
386 ret = nouveau_bo_vma_add(nvbo, drm->client.vm,
387 &fbcon->nouveau_fb.vma);
388 if (ret) {
389 NV_ERROR(drm, "failed to map fb into chan: %d\n", ret);
390 chan = NULL;
391 }
392 }
393
394 mutex_lock(&dev->struct_mutex);
395
396 info = drm_fb_helper_alloc_fbi(helper);
397 if (IS_ERR(info)) {
398 ret = PTR_ERR(info);
399 goto out_unlock;
400 }
401 info->skip_vt_switch = 1;
402
403 info->par = fbcon;
404
405 nouveau_framebuffer_init(dev, &fbcon->nouveau_fb, &mode_cmd, nvbo);
406
407 nouveau_fb = &fbcon->nouveau_fb;
408 fb = &nouveau_fb->base;
409
410 /* setup helper */
411 fbcon->helper.fb = fb;
412
413 strcpy(info->fix.id, "nouveaufb");
414 if (!chan)
415 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
416 else
417 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
418 FBINFO_HWACCEL_FILLRECT |
419 FBINFO_HWACCEL_IMAGEBLIT;
420 info->flags |= FBINFO_CAN_FORCE_OUTPUT;
421 info->fbops = &nouveau_fbcon_sw_ops;
422 info->fix.smem_start = nvbo->bo.mem.bus.base +
423 nvbo->bo.mem.bus.offset;
424 info->fix.smem_len = size;
425
426 info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
427 info->screen_size = size;
428
429 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
430 drm_fb_helper_fill_var(info, &fbcon->helper, sizes->fb_width, sizes->fb_height);
431
432 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
433
434 mutex_unlock(&dev->struct_mutex);
435
436 if (chan)
437 nouveau_fbcon_accel_init(dev);
438 nouveau_fbcon_zfill(dev, fbcon);
439
440 /* To allow resizeing without swapping buffers */
441 NV_INFO(drm, "allocated %dx%d fb: 0x%llx, bo %p\n",
442 nouveau_fb->base.width, nouveau_fb->base.height,
443 nvbo->bo.offset, nvbo);
444
445 vga_switcheroo_client_fb_set(dev->pdev, info);
446 return 0;
447
448 out_unlock:
449 mutex_unlock(&dev->struct_mutex);
450 if (chan)
451 nouveau_bo_vma_del(nvbo, &fbcon->nouveau_fb.vma);
452 nouveau_bo_unmap(nvbo);
453 out_unpin:
454 nouveau_bo_unpin(nvbo);
455 out_unref:
456 nouveau_bo_ref(NULL, &nvbo);
457 out:
458 return ret;
459 }
460
461 void
462 nouveau_fbcon_output_poll_changed(struct drm_device *dev)
463 {
464 struct nouveau_drm *drm = nouveau_drm(dev);
465 if (drm->fbcon)
466 drm_fb_helper_hotplug_event(&drm->fbcon->helper);
467 }
468
469 static int
470 nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *fbcon)
471 {
472 struct nouveau_framebuffer *nouveau_fb = &fbcon->nouveau_fb;
473
474 drm_fb_helper_unregister_fbi(&fbcon->helper);
475 drm_fb_helper_release_fbi(&fbcon->helper);
476
477 if (nouveau_fb->nvbo) {
478 nouveau_bo_unmap(nouveau_fb->nvbo);
479 nouveau_bo_vma_del(nouveau_fb->nvbo, &nouveau_fb->vma);
480 nouveau_bo_unpin(nouveau_fb->nvbo);
481 drm_gem_object_unreference_unlocked(&nouveau_fb->nvbo->gem);
482 nouveau_fb->nvbo = NULL;
483 }
484 drm_fb_helper_fini(&fbcon->helper);
485 drm_framebuffer_unregister_private(&nouveau_fb->base);
486 drm_framebuffer_cleanup(&nouveau_fb->base);
487 return 0;
488 }
489
490 void nouveau_fbcon_gpu_lockup(struct fb_info *info)
491 {
492 struct nouveau_fbdev *fbcon = info->par;
493 struct nouveau_drm *drm = nouveau_drm(fbcon->dev);
494
495 NV_ERROR(drm, "GPU lockup - switching to software fbcon\n");
496 info->flags |= FBINFO_HWACCEL_DISABLED;
497 }
498
499 static const struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
500 .gamma_set = nouveau_fbcon_gamma_set,
501 .gamma_get = nouveau_fbcon_gamma_get,
502 .fb_probe = nouveau_fbcon_create,
503 };
504
505 void
506 nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
507 {
508 struct nouveau_drm *drm = nouveau_drm(dev);
509 if (drm->fbcon) {
510 console_lock();
511 if (state == FBINFO_STATE_RUNNING)
512 nouveau_fbcon_accel_restore(dev);
513 drm_fb_helper_set_suspend(&drm->fbcon->helper, state);
514 if (state != FBINFO_STATE_RUNNING)
515 nouveau_fbcon_accel_save_disable(dev);
516 console_unlock();
517 }
518 }
519
520 int
521 nouveau_fbcon_init(struct drm_device *dev)
522 {
523 struct nouveau_drm *drm = nouveau_drm(dev);
524 struct nouveau_fbdev *fbcon;
525 int preferred_bpp;
526 int ret;
527
528 if (!dev->mode_config.num_crtc ||
529 (dev->pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
530 return 0;
531
532 fbcon = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
533 if (!fbcon)
534 return -ENOMEM;
535
536 fbcon->dev = dev;
537 drm->fbcon = fbcon;
538
539 drm_fb_helper_prepare(dev, &fbcon->helper, &nouveau_fbcon_helper_funcs);
540
541 ret = drm_fb_helper_init(dev, &fbcon->helper,
542 dev->mode_config.num_crtc, 4);
543 if (ret)
544 goto free;
545
546 ret = drm_fb_helper_single_add_all_connectors(&fbcon->helper);
547 if (ret)
548 goto fini;
549
550 if (drm->device.info.ram_size <= 32 * 1024 * 1024)
551 preferred_bpp = 8;
552 else
553 if (drm->device.info.ram_size <= 64 * 1024 * 1024)
554 preferred_bpp = 16;
555 else
556 preferred_bpp = 32;
557
558 /* disable all the possible outputs/crtcs before entering KMS mode */
559 drm_helper_disable_unused_functions(dev);
560
561 ret = drm_fb_helper_initial_config(&fbcon->helper, preferred_bpp);
562 if (ret)
563 goto fini;
564
565 if (fbcon->helper.fbdev)
566 fbcon->helper.fbdev->pixmap.buf_align = 4;
567 return 0;
568
569 fini:
570 drm_fb_helper_fini(&fbcon->helper);
571 free:
572 kfree(fbcon);
573 return ret;
574 }
575
576 void
577 nouveau_fbcon_fini(struct drm_device *dev)
578 {
579 struct nouveau_drm *drm = nouveau_drm(dev);
580
581 if (!drm->fbcon)
582 return;
583
584 nouveau_fbcon_accel_fini(dev);
585 nouveau_fbcon_destroy(dev, drm->fbcon);
586 kfree(drm->fbcon);
587 drm->fbcon = NULL;
588 }
589