]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_fbcon.c
drm/nouveau: hook up acpi power supply change tracking
[mirror_ubuntu-focal-kernel.git] / drivers / gpu / drm / nouveau / nouveau_fbcon.c
CommitLineData
6ee73861
BS
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>
6ee73861
BS
33#include <linux/sysrq.h>
34#include <linux/delay.h>
35#include <linux/fb.h>
36#include <linux/init.h>
37#include <linux/screen_info.h>
6a9ee8af 38#include <linux/vga_switcheroo.h>
6ee73861
BS
39
40#include "drmP.h"
41#include "drm.h"
42#include "drm_crtc.h"
43#include "drm_crtc_helper.h"
44#include "drm_fb_helper.h"
45#include "nouveau_drv.h"
46#include "nouveau_drm.h"
47#include "nouveau_crtc.h"
48#include "nouveau_fb.h"
49#include "nouveau_fbcon.h"
50#include "nouveau_dma.h"
51
ceed5f30
BS
52static void
53nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
54{
55 struct nouveau_fbdev *nfbdev = info->par;
56 struct drm_device *dev = nfbdev->dev;
57 struct drm_nouveau_private *dev_priv = dev->dev_private;
58 int ret;
59
60 if (info->state != FBINFO_STATE_RUNNING)
61 return;
62
63 ret = -ENODEV;
64 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED)) {
6a6b73f2 65 mutex_lock(&dev_priv->channel->mutex);
ceed5f30
BS
66 if (dev_priv->card_type < NV_50)
67 ret = nv04_fbcon_fillrect(info, rect);
68 else
69 if (dev_priv->card_type < NV_C0)
70 ret = nv50_fbcon_fillrect(info, rect);
6a6b73f2 71 mutex_unlock(&dev_priv->channel->mutex);
ceed5f30
BS
72 }
73
74 if (ret == 0)
75 return;
76
77 if (ret != -ENODEV)
78 nouveau_fbcon_gpu_lockup(info);
79 cfb_fillrect(info, rect);
80}
81
82static void
83nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
84{
85 struct nouveau_fbdev *nfbdev = info->par;
86 struct drm_device *dev = nfbdev->dev;
87 struct drm_nouveau_private *dev_priv = dev->dev_private;
88 int ret;
89
90 if (info->state != FBINFO_STATE_RUNNING)
91 return;
92
93 ret = -ENODEV;
94 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED)) {
6a6b73f2 95 mutex_lock(&dev_priv->channel->mutex);
ceed5f30
BS
96 if (dev_priv->card_type < NV_50)
97 ret = nv04_fbcon_copyarea(info, image);
98 else
99 if (dev_priv->card_type < NV_C0)
100 ret = nv50_fbcon_copyarea(info, image);
6a6b73f2 101 mutex_unlock(&dev_priv->channel->mutex);
ceed5f30
BS
102 }
103
104 if (ret == 0)
105 return;
106
107 if (ret != -ENODEV)
108 nouveau_fbcon_gpu_lockup(info);
109 cfb_copyarea(info, image);
110}
111
112static void
113nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
114{
115 struct nouveau_fbdev *nfbdev = info->par;
116 struct drm_device *dev = nfbdev->dev;
117 struct drm_nouveau_private *dev_priv = dev->dev_private;
118 int ret;
119
120 if (info->state != FBINFO_STATE_RUNNING)
121 return;
122
123 ret = -ENODEV;
124 if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED)) {
6a6b73f2 125 mutex_lock(&dev_priv->channel->mutex);
ceed5f30
BS
126 if (dev_priv->card_type < NV_50)
127 ret = nv04_fbcon_imageblit(info, image);
128 else
129 if (dev_priv->card_type < NV_C0)
130 ret = nv50_fbcon_imageblit(info, image);
6a6b73f2 131 mutex_unlock(&dev_priv->channel->mutex);
ceed5f30
BS
132 }
133
134 if (ret == 0)
135 return;
136
137 if (ret != -ENODEV)
138 nouveau_fbcon_gpu_lockup(info);
139 cfb_imageblit(info, image);
140}
141
6ee73861
BS
142static int
143nouveau_fbcon_sync(struct fb_info *info)
144{
8be48d92
DA
145 struct nouveau_fbdev *nfbdev = info->par;
146 struct drm_device *dev = nfbdev->dev;
6ee73861
BS
147 struct drm_nouveau_private *dev_priv = dev->dev_private;
148 struct nouveau_channel *chan = dev_priv->channel;
149 int ret, i;
150
6a6b73f2 151 if (!chan || !chan->accel_done || in_interrupt() ||
6ee73861
BS
152 info->state != FBINFO_STATE_RUNNING ||
153 info->flags & FBINFO_HWACCEL_DISABLED)
154 return 0;
155
6a6b73f2
BS
156 mutex_lock(&chan->mutex);
157 ret = RING_SPACE(chan, 4);
158 if (ret) {
159 mutex_unlock(&chan->mutex);
846975a9 160 nouveau_fbcon_gpu_lockup(info);
6ee73861
BS
161 return 0;
162 }
163
164 BEGIN_RING(chan, 0, 0x0104, 1);
165 OUT_RING(chan, 0);
166 BEGIN_RING(chan, 0, 0x0100, 1);
167 OUT_RING(chan, 0);
168 nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy + 3, 0xffffffff);
169 FIRE_RING(chan);
6a6b73f2 170 mutex_unlock(&chan->mutex);
6ee73861
BS
171
172 ret = -EBUSY;
173 for (i = 0; i < 100000; i++) {
174 if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy + 3)) {
175 ret = 0;
176 break;
177 }
178 DRM_UDELAY(1);
179 }
180
181 if (ret) {
846975a9 182 nouveau_fbcon_gpu_lockup(info);
6ee73861
BS
183 return 0;
184 }
185
186 chan->accel_done = false;
187 return 0;
188}
189
190static struct fb_ops nouveau_fbcon_ops = {
191 .owner = THIS_MODULE,
192 .fb_check_var = drm_fb_helper_check_var,
193 .fb_set_par = drm_fb_helper_set_par,
ceed5f30
BS
194 .fb_fillrect = nouveau_fbcon_fillrect,
195 .fb_copyarea = nouveau_fbcon_copyarea,
196 .fb_imageblit = nouveau_fbcon_imageblit,
6ee73861
BS
197 .fb_sync = nouveau_fbcon_sync,
198 .fb_pan_display = drm_fb_helper_pan_display,
199 .fb_blank = drm_fb_helper_blank,
200 .fb_setcmap = drm_fb_helper_setcmap,
be64c2bb
CB
201 .fb_debug_enter = drm_fb_helper_debug_enter,
202 .fb_debug_leave = drm_fb_helper_debug_leave,
6ee73861
BS
203};
204
ceed5f30 205static struct fb_ops nouveau_fbcon_sw_ops = {
126b5440
MK
206 .owner = THIS_MODULE,
207 .fb_check_var = drm_fb_helper_check_var,
208 .fb_set_par = drm_fb_helper_set_par,
ceed5f30
BS
209 .fb_fillrect = cfb_fillrect,
210 .fb_copyarea = cfb_copyarea,
211 .fb_imageblit = cfb_imageblit,
126b5440
MK
212 .fb_pan_display = drm_fb_helper_pan_display,
213 .fb_blank = drm_fb_helper_blank,
214 .fb_setcmap = drm_fb_helper_setcmap,
be64c2bb
CB
215 .fb_debug_enter = drm_fb_helper_debug_enter,
216 .fb_debug_leave = drm_fb_helper_debug_leave,
126b5440
MK
217};
218
6ee73861
BS
219static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
220 u16 blue, int regno)
221{
222 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
223
224 nv_crtc->lut.r[regno] = red;
225 nv_crtc->lut.g[regno] = green;
226 nv_crtc->lut.b[regno] = blue;
227}
228
229static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
230 u16 *blue, int regno)
231{
232 struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
233
234 *red = nv_crtc->lut.r[regno];
235 *green = nv_crtc->lut.g[regno];
236 *blue = nv_crtc->lut.b[regno];
237}
238
38651674 239static void
8be48d92 240nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
6ee73861 241{
8be48d92 242 struct fb_info *info = nfbdev->helper.fbdev;
6ee73861
BS
243 struct fb_fillrect rect;
244
245 /* Clear the entire fbcon. The drm will program every connector
246 * with it's preferred mode. If the sizes differ, one display will
247 * quite likely have garbage around the console.
248 */
249 rect.dx = rect.dy = 0;
250 rect.width = info->var.xres_virtual;
251 rect.height = info->var.yres_virtual;
252 rect.color = 0;
253 rect.rop = ROP_COPY;
254 info->fbops->fb_fillrect(info, &rect);
255}
256
257static int
8be48d92
DA
258nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,
259 struct drm_fb_helper_surface_size *sizes)
6ee73861 260{
8be48d92 261 struct drm_device *dev = nfbdev->dev;
6ee73861
BS
262 struct drm_nouveau_private *dev_priv = dev->dev_private;
263 struct fb_info *info;
6ee73861
BS
264 struct drm_framebuffer *fb;
265 struct nouveau_framebuffer *nouveau_fb;
266 struct nouveau_bo *nvbo;
267 struct drm_mode_fb_cmd mode_cmd;
1471ca9a
MS
268 struct pci_dev *pdev = dev->pdev;
269 struct device *device = &pdev->dev;
6ee73861
BS
270 int size, ret;
271
38651674
DA
272 mode_cmd.width = sizes->surface_width;
273 mode_cmd.height = sizes->surface_height;
6ee73861 274
38651674 275 mode_cmd.bpp = sizes->surface_bpp;
6ee73861 276 mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
1c7059e4 277 mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
38651674 278 mode_cmd.depth = sizes->surface_depth;
6ee73861
BS
279
280 size = mode_cmd.pitch * mode_cmd.height;
1c7059e4 281 size = roundup(size, PAGE_SIZE);
6ee73861
BS
282
283 ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM,
284 0, 0x0000, false, true, &nvbo);
285 if (ret) {
286 NV_ERROR(dev, "failed to allocate framebuffer\n");
287 goto out;
288 }
289
290 ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);
291 if (ret) {
292 NV_ERROR(dev, "failed to pin fb: %d\n", ret);
293 nouveau_bo_ref(NULL, &nvbo);
294 goto out;
295 }
296
297 ret = nouveau_bo_map(nvbo);
298 if (ret) {
299 NV_ERROR(dev, "failed to map fb: %d\n", ret);
300 nouveau_bo_unpin(nvbo);
301 nouveau_bo_ref(NULL, &nvbo);
302 goto out;
303 }
304
305 mutex_lock(&dev->struct_mutex);
306
8be48d92 307 info = framebuffer_alloc(0, device);
6ee73861 308 if (!info) {
6ee73861 309 ret = -ENOMEM;
6ee73861
BS
310 goto out_unref;
311 }
312
4abe3520
DA
313 ret = fb_alloc_cmap(&info->cmap, 256, 0);
314 if (ret) {
6ee73861
BS
315 ret = -ENOMEM;
316 goto out_unref;
317 }
318
8be48d92 319 info->par = nfbdev;
38651674 320
8be48d92 321 nouveau_framebuffer_init(dev, &nfbdev->nouveau_fb, &mode_cmd, nvbo);
38651674 322
8be48d92
DA
323 nouveau_fb = &nfbdev->nouveau_fb;
324 fb = &nouveau_fb->base;
38651674 325
8be48d92
DA
326 /* setup helper */
327 nfbdev->helper.fb = fb;
328 nfbdev->helper.fbdev = info;
6ee73861
BS
329
330 strcpy(info->fix.id, "nouveaufb");
a32ed69d
MK
331 if (nouveau_nofbaccel)
332 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
333 else
334 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
335 FBINFO_HWACCEL_FILLRECT |
336 FBINFO_HWACCEL_IMAGEBLIT;
8fd4bd22 337 info->flags |= FBINFO_CAN_FORCE_OUTPUT;
ceed5f30 338 info->fbops = &nouveau_fbcon_sw_ops;
6ee73861
BS
339 info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset -
340 dev_priv->vm_vram_base;
341 info->fix.smem_len = size;
342
343 info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
344 info->screen_size = size;
345
346 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
8be48d92 347 drm_fb_helper_fill_var(info, &nfbdev->helper, sizes->fb_width, sizes->fb_height);
6ee73861
BS
348
349 /* FIXME: we really shouldn't expose mmio space at all */
1471ca9a
MS
350 info->fix.mmio_start = pci_resource_start(pdev, 1);
351 info->fix.mmio_len = pci_resource_len(pdev, 1);
6ee73861
BS
352
353 /* Set aperture base/size for vesafb takeover */
06415c56 354 info->apertures = dev_priv->apertures;
1471ca9a
MS
355 if (!info->apertures) {
356 ret = -ENOMEM;
357 goto out_unref;
358 }
359
6ee73861
BS
360 info->pixmap.size = 64*1024;
361 info->pixmap.buf_align = 8;
362 info->pixmap.access_align = 32;
363 info->pixmap.flags = FB_PIXMAP_SYSTEM;
364 info->pixmap.scan_align = 1;
365
6a6b73f2
BS
366 mutex_unlock(&dev->struct_mutex);
367
a32ed69d 368 if (dev_priv->channel && !nouveau_nofbaccel) {
ceed5f30
BS
369 ret = -ENODEV;
370 if (dev_priv->card_type < NV_50)
371 ret = nv04_fbcon_accel_init(info);
372 else
373 if (dev_priv->card_type < NV_C0)
374 ret = nv50_fbcon_accel_init(info);
375
376 if (ret == 0)
377 info->fbops = &nouveau_fbcon_ops;
0735f62e 378 }
6ee73861 379
8be48d92 380 nouveau_fbcon_zfill(dev, nfbdev);
6ee73861
BS
381
382 /* To allow resizeing without swapping buffers */
383 NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",
384 nouveau_fb->base.width,
385 nouveau_fb->base.height,
386 nvbo->bo.offset, nvbo);
387
6a9ee8af 388 vga_switcheroo_client_fb_set(dev->pdev, info);
6ee73861
BS
389 return 0;
390
391out_unref:
392 mutex_unlock(&dev->struct_mutex);
393out:
394 return ret;
395}
396
38651674 397static int
8be48d92
DA
398nouveau_fbcon_find_or_create_single(struct drm_fb_helper *helper,
399 struct drm_fb_helper_surface_size *sizes)
6ee73861 400{
8be48d92 401 struct nouveau_fbdev *nfbdev = (struct nouveau_fbdev *)helper;
38651674
DA
402 int new_fb = 0;
403 int ret;
404
8be48d92
DA
405 if (!helper->fb) {
406 ret = nouveau_fbcon_create(nfbdev, sizes);
38651674
DA
407 if (ret)
408 return ret;
38651674 409 new_fb = 1;
38651674 410 }
38651674
DA
411 return new_fb;
412}
6ee73861 413
eb1f8e4f
DA
414void
415nouveau_fbcon_output_poll_changed(struct drm_device *dev)
6ee73861 416{
4abe3520 417 struct drm_nouveau_private *dev_priv = dev->dev_private;
eb1f8e4f 418 drm_fb_helper_hotplug_event(&dev_priv->nfbdev->helper);
6ee73861
BS
419}
420
6e86e041 421static int
8be48d92 422nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
6ee73861 423{
8be48d92 424 struct nouveau_framebuffer *nouveau_fb = &nfbdev->nouveau_fb;
6ee73861
BS
425 struct fb_info *info;
426
8be48d92
DA
427 if (nfbdev->helper.fbdev) {
428 info = nfbdev->helper.fbdev;
6ee73861 429 unregister_framebuffer(info);
4abe3520
DA
430 if (info->cmap.len)
431 fb_dealloc_cmap(&info->cmap);
8be48d92
DA
432 framebuffer_release(info);
433 }
6ee73861 434
8be48d92 435 if (nouveau_fb->nvbo) {
6ee73861 436 nouveau_bo_unmap(nouveau_fb->nvbo);
bc9025bd 437 drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem);
6ee73861 438 nouveau_fb->nvbo = NULL;
6ee73861 439 }
4abe3520 440 drm_fb_helper_fini(&nfbdev->helper);
38651674 441 drm_framebuffer_cleanup(&nouveau_fb->base);
6ee73861
BS
442 return 0;
443}
846975a9
MS
444
445void nouveau_fbcon_gpu_lockup(struct fb_info *info)
446{
8be48d92
DA
447 struct nouveau_fbdev *nfbdev = info->par;
448 struct drm_device *dev = nfbdev->dev;
846975a9
MS
449
450 NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
451 info->flags |= FBINFO_HWACCEL_DISABLED;
452}
38651674 453
4abe3520
DA
454static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
455 .gamma_set = nouveau_fbcon_gamma_set,
456 .gamma_get = nouveau_fbcon_gamma_get,
457 .fb_probe = nouveau_fbcon_find_or_create_single,
4abe3520
DA
458};
459
460
38651674
DA
461int nouveau_fbcon_init(struct drm_device *dev)
462{
8be48d92
DA
463 struct drm_nouveau_private *dev_priv = dev->dev_private;
464 struct nouveau_fbdev *nfbdev;
5a79395b 465 int ret;
8be48d92
DA
466
467 nfbdev = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
468 if (!nfbdev)
469 return -ENOMEM;
470
471 nfbdev->dev = dev;
472 dev_priv->nfbdev = nfbdev;
4abe3520 473 nfbdev->helper.funcs = &nouveau_fbcon_helper_funcs;
8be48d92 474
77144554
FJ
475 ret = drm_fb_helper_init(dev, &nfbdev->helper,
476 nv_two_heads(dev) ? 2 : 1, 4);
5a79395b
CW
477 if (ret) {
478 kfree(nfbdev);
479 return ret;
480 }
481
0b4c0f3f 482 drm_fb_helper_single_add_all_connectors(&nfbdev->helper);
4abe3520 483 drm_fb_helper_initial_config(&nfbdev->helper, 32);
38651674
DA
484 return 0;
485}
486
487void nouveau_fbcon_fini(struct drm_device *dev)
488{
489 struct drm_nouveau_private *dev_priv = dev->dev_private;
8be48d92
DA
490
491 if (!dev_priv->nfbdev)
492 return;
493
38651674 494 nouveau_fbcon_destroy(dev, dev_priv->nfbdev);
8be48d92 495 kfree(dev_priv->nfbdev);
38651674
DA
496 dev_priv->nfbdev = NULL;
497}
498
499void nouveau_fbcon_save_disable_accel(struct drm_device *dev)
500{
501 struct drm_nouveau_private *dev_priv = dev->dev_private;
502
503 dev_priv->nfbdev->saved_flags = dev_priv->nfbdev->helper.fbdev->flags;
504 dev_priv->nfbdev->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
505}
506
507void nouveau_fbcon_restore_accel(struct drm_device *dev)
508{
509 struct drm_nouveau_private *dev_priv = dev->dev_private;
510 dev_priv->nfbdev->helper.fbdev->flags = dev_priv->nfbdev->saved_flags;
511}
512
513void nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
514{
515 struct drm_nouveau_private *dev_priv = dev->dev_private;
516 fb_set_suspend(dev_priv->nfbdev->helper.fbdev, state);
517}
518
519void nouveau_fbcon_zfill_all(struct drm_device *dev)
520{
521 struct drm_nouveau_private *dev_priv = dev->dev_private;
522 nouveau_fbcon_zfill(dev, dev_priv->nfbdev);
523}