drm/i915: Use normal fb deref for the fbcon framebuffer
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_fbdev.c
CommitLineData
79e53945
JB
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>
79e53945
JB
33#include <linux/sysrq.h>
34#include <linux/delay.h>
35#include <linux/fb.h>
36#include <linux/init.h>
6a9ee8af 37#include <linux/vga_switcheroo.h>
79e53945 38
760285e7
DH
39#include <drm/drmP.h>
40#include <drm/drm_crtc.h>
41#include <drm/drm_fb_helper.h>
79e53945 42#include "intel_drv.h"
760285e7 43#include <drm/i915_drm.h>
79e53945
JB
44#include "i915_drv.h"
45
79e53945
JB
46static struct fb_ops intelfb_ops = {
47 .owner = THIS_MODULE,
785b93ef
DA
48 .fb_check_var = drm_fb_helper_check_var,
49 .fb_set_par = drm_fb_helper_set_par,
79e53945
JB
50 .fb_fillrect = cfb_fillrect,
51 .fb_copyarea = cfb_copyarea,
52 .fb_imageblit = cfb_imageblit,
785b93ef
DA
53 .fb_pan_display = drm_fb_helper_pan_display,
54 .fb_blank = drm_fb_helper_blank,
068143d3 55 .fb_setcmap = drm_fb_helper_setcmap,
81255565
JB
56 .fb_debug_enter = drm_fb_helper_debug_enter,
57 .fb_debug_leave = drm_fb_helper_debug_leave,
79e53945
JB
58};
59
8b4f49e0
JB
60static int intelfb_alloc(struct drm_fb_helper *helper,
61 struct drm_fb_helper_surface_size *sizes)
79e53945 62{
3430824b
VS
63 struct intel_fbdev *ifbdev =
64 container_of(helper, struct intel_fbdev, helper);
8bcd4553 65 struct intel_framebuffer *fb;
3430824b 66 struct drm_device *dev = helper->dev;
3a7f2f6a 67 struct drm_mode_fb_cmd2 mode_cmd = {};
05394f39 68 struct drm_i915_gem_object *obj;
57084d05 69 int size, ret;
79e53945 70
8bcd4553
JB
71 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
72 if (!fb) {
73 ret = -ENOMEM;
74 goto out;
75 }
76
77 ifbdev->fb = fb;
78
b8c00ac5 79 /* we don't do packed 24bpp */
38651674
DA
80 if (sizes->surface_bpp == 24)
81 sizes->surface_bpp = 32;
b8c00ac5 82
38651674
DA
83 mode_cmd.width = sizes->surface_width;
84 mode_cmd.height = sizes->surface_height;
79e53945 85
de45eaf7
PZ
86 mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
87 DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
308e5bcb
JB
88 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
89 sizes->surface_depth);
79e53945 90
308e5bcb 91 size = mode_cmd.pitches[0] * mode_cmd.height;
79e53945 92 size = ALIGN(size, PAGE_SIZE);
0ffb0ff2
CW
93 obj = i915_gem_object_create_stolen(dev, size);
94 if (obj == NULL)
95 obj = i915_gem_alloc_object(dev, size);
05394f39 96 if (!obj) {
1ae8c0a5 97 DRM_ERROR("failed to allocate framebuffer\n");
79e53945
JB
98 ret = -ENOMEM;
99 goto out;
100 }
79e53945 101
7213342d 102 /* Flush everything out, we'll be doing GTT only from now on */
c43b5634 103 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
79e53945
JB
104 if (ret) {
105 DRM_ERROR("failed to pin fb: %d\n", ret);
106 goto out_unref;
107 }
108
8bcd4553 109 ret = intel_framebuffer_init(dev, ifbdev->fb, &mode_cmd, obj);
8b4f49e0
JB
110 if (ret)
111 goto out_unpin;
112
113 return 0;
114
115out_unpin:
d7f46fc4 116 i915_gem_object_ggtt_unpin(obj);
8b4f49e0
JB
117out_unref:
118 drm_gem_object_unreference(&obj->base);
119out:
120 return ret;
121}
122
123static int intelfb_create(struct drm_fb_helper *helper,
124 struct drm_fb_helper_surface_size *sizes)
125{
126 struct intel_fbdev *ifbdev =
127 container_of(helper, struct intel_fbdev, helper);
8bcd4553 128 struct intel_framebuffer *intel_fb = ifbdev->fb;
8b4f49e0
JB
129 struct drm_device *dev = helper->dev;
130 struct drm_i915_private *dev_priv = dev->dev_private;
131 struct fb_info *info;
132 struct drm_framebuffer *fb;
133 struct drm_i915_gem_object *obj;
134 int size, ret;
135
136 mutex_lock(&dev->struct_mutex);
137
8bcd4553 138 if (!intel_fb || WARN_ON(!intel_fb->obj)) {
48e92120 139 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
8b4f49e0
JB
140 ret = intelfb_alloc(helper, sizes);
141 if (ret)
142 goto out_unlock;
8bcd4553 143 intel_fb = ifbdev->fb;
8b4f49e0 144 } else {
48e92120 145 DRM_DEBUG_KMS("re-using BIOS fb\n");
8b4f49e0
JB
146 sizes->fb_width = intel_fb->base.width;
147 sizes->fb_height = intel_fb->base.height;
148 }
149
150 obj = intel_fb->obj;
151 size = obj->base.size;
152
153 info = framebuffer_alloc(0, &dev->pdev->dev);
79e53945
JB
154 if (!info) {
155 ret = -ENOMEM;
b4476f52 156 goto out_unpin;
79e53945
JB
157 }
158
3430824b 159 info->par = helper;
79e53945 160
8bcd4553 161 fb = &ifbdev->fb->base;
79e53945 162
38651674
DA
163 ifbdev->helper.fb = fb;
164 ifbdev->helper.fbdev = info;
785b93ef 165
79e53945 166 strcpy(info->fix.id, "inteldrmfb");
79e53945 167
8fd4bd22 168 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
79e53945
JB
169 info->fbops = &intelfb_ops;
170
35c3047a
CW
171 ret = fb_alloc_cmap(&info->cmap, 256, 0);
172 if (ret) {
173 ret = -ENOMEM;
174 goto out_unpin;
175 }
4410f391 176 /* setup aperture base/size for vesafb takeover */
1471ca9a
MS
177 info->apertures = alloc_apertures(1);
178 if (!info->apertures) {
179 ret = -ENOMEM;
180 goto out_unpin;
181 }
182 info->apertures->ranges[0].base = dev->mode_config.fb_base;
93d18799 183 info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
4410f391 184
f343c5f6 185 info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
79e53945
JB
186 info->fix.smem_len = size;
187
dd2757f8 188 info->screen_base =
f343c5f6 189 ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
dd2757f8 190 size);
79e53945
JB
191 if (!info->screen_base) {
192 ret = -ENOSPC;
b4476f52 193 goto out_unpin;
79e53945
JB
194 }
195 info->screen_size = size;
196
24576d23
JB
197 /* This driver doesn't need a VT switch to restore the mode on resume */
198 info->skip_vt_switch = true;
199
01f2c773 200 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
38651674 201 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
79e53945 202
88afe715
CW
203 /* If the object is shmemfs backed, it will have given us zeroed pages.
204 * If the object is stolen however, it will be full of whatever
205 * garbage was left in there.
206 */
8bcd4553 207 if (ifbdev->fb->obj->stolen)
88afe715
CW
208 memset_io(info->screen_base, 0, info->screen_size);
209
fb2a99e1 210 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
79e53945 211
f343c5f6 212 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
8be48d92 213 fb->width, fb->height,
f343c5f6 214 i915_gem_obj_ggtt_offset(obj), obj);
79e53945
JB
215
216 mutex_unlock(&dev->struct_mutex);
6a9ee8af 217 vga_switcheroo_client_fb_set(dev->pdev, info);
79e53945
JB
218 return 0;
219
b4476f52 220out_unpin:
d7f46fc4 221 i915_gem_object_ggtt_unpin(obj);
05394f39 222 drm_gem_object_unreference(&obj->base);
8b4f49e0 223out_unlock:
79e53945 224 mutex_unlock(&dev->struct_mutex);
79e53945
JB
225 return ret;
226}
227
67437680
PZ
228/** Sets the color ramps on behalf of RandR */
229static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
230 u16 blue, int regno)
231{
232 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
233
234 intel_crtc->lut_r[regno] = red >> 8;
235 intel_crtc->lut_g[regno] = green >> 8;
236 intel_crtc->lut_b[regno] = blue >> 8;
237}
238
239static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
240 u16 *blue, int regno)
241{
242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
243
244 *red = intel_crtc->lut_r[regno] << 8;
245 *green = intel_crtc->lut_g[regno] << 8;
246 *blue = intel_crtc->lut_b[regno] << 8;
247}
248
4abe3520
DA
249static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
250 .gamma_set = intel_crtc_fb_gamma_set,
251 .gamma_get = intel_crtc_fb_gamma_get,
cd5428a5 252 .fb_probe = intelfb_create,
4abe3520 253};
79e53945 254
7b4f3990
CW
255static void intel_fbdev_destroy(struct drm_device *dev,
256 struct intel_fbdev *ifbdev)
79e53945 257{
8be48d92 258 if (ifbdev->helper.fbdev) {
ddfe1567
CW
259 struct fb_info *info = ifbdev->helper.fbdev;
260
79e53945
JB
261 unregister_framebuffer(info);
262 iounmap(info->screen_base);
4abe3520
DA
263 if (info->cmap.len)
264 fb_dealloc_cmap(&info->cmap);
ddfe1567 265
79e53945
JB
266 framebuffer_release(info);
267 }
268
4abe3520 269 drm_fb_helper_fini(&ifbdev->helper);
79e53945 270
8bcd4553 271 drm_framebuffer_unregister_private(&ifbdev->fb->base);
ef2d633e 272 drm_framebuffer_unreference(&ifbdev->fb->base);
79e53945 273}
38651674
DA
274
275int intel_fbdev_init(struct drm_device *dev)
276{
8be48d92 277 struct intel_fbdev *ifbdev;
b51b32cd 278 struct drm_i915_private *dev_priv = dev->dev_private;
5a79395b 279 int ret;
8be48d92 280
b14c5679 281 ifbdev = kzalloc(sizeof(*ifbdev), GFP_KERNEL);
8be48d92
DA
282 if (!ifbdev)
283 return -ENOMEM;
284
285 dev_priv->fbdev = ifbdev;
4abe3520
DA
286 ifbdev->helper.funcs = &intel_fb_helper_funcs;
287
5a79395b 288 ret = drm_fb_helper_init(dev, &ifbdev->helper,
7eb552ae 289 INTEL_INFO(dev)->num_pipes,
955382f3 290 4);
5a79395b
CW
291 if (ret) {
292 kfree(ifbdev);
293 return ret;
294 }
8be48d92 295
0b4c0f3f 296 drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
20afbda2 297
79e53945
JB
298 return 0;
299}
38651674 300
20afbda2
DV
301void intel_fbdev_initial_config(struct drm_device *dev)
302{
b51b32cd 303 struct drm_i915_private *dev_priv = dev->dev_private;
20afbda2
DV
304
305 /* Due to peculiar init order wrt to hpd handling this is separate. */
306 drm_fb_helper_initial_config(&dev_priv->fbdev->helper, 32);
307}
308
38651674
DA
309void intel_fbdev_fini(struct drm_device *dev)
310{
b51b32cd 311 struct drm_i915_private *dev_priv = dev->dev_private;
8be48d92
DA
312 if (!dev_priv->fbdev)
313 return;
314
38651674 315 intel_fbdev_destroy(dev, dev_priv->fbdev);
8be48d92 316 kfree(dev_priv->fbdev);
38651674
DA
317 dev_priv->fbdev = NULL;
318}
3fa016a0
DA
319
320void intel_fbdev_set_suspend(struct drm_device *dev, int state)
321{
b51b32cd 322 struct drm_i915_private *dev_priv = dev->dev_private;
1ffc5289
JN
323 struct intel_fbdev *ifbdev = dev_priv->fbdev;
324 struct fb_info *info;
325
326 if (!ifbdev)
3fa016a0
DA
327 return;
328
1ffc5289
JN
329 info = ifbdev->helper.fbdev;
330
331 /* On resume from hibernation: If the object is shmemfs backed, it has
332 * been restored from swap. If the object is stolen however, it will be
333 * full of whatever garbage was left in there.
334 */
8bcd4553 335 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
1ffc5289
JN
336 memset_io(info->screen_base, 0, info->screen_size);
337
338 fb_set_suspend(info, state);
3fa016a0
DA
339}
340
0632fef6 341void intel_fbdev_output_poll_changed(struct drm_device *dev)
eb1f8e4f 342{
b51b32cd 343 struct drm_i915_private *dev_priv = dev->dev_private;
eb1f8e4f
DA
344 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
345}
e8e7a2b8 346
0632fef6 347void intel_fbdev_restore_mode(struct drm_device *dev)
e8e7a2b8
DA
348{
349 int ret;
b51b32cd 350 struct drm_i915_private *dev_priv = dev->dev_private;
e8e7a2b8 351
e3c74757
BW
352 if (INTEL_INFO(dev)->num_pipes == 0)
353 return;
354
a0e99e68 355 drm_modeset_lock_all(dev);
c291be9d 356
e8e7a2b8
DA
357 ret = drm_fb_helper_restore_fbdev_mode(&dev_priv->fbdev->helper);
358 if (ret)
359 DRM_DEBUG("failed to restore crtc mode\n");
b840d907 360
a0e99e68 361 drm_modeset_unlock_all(dev);
e8e7a2b8 362}
This page took 0.341824 seconds and 5 git commands to generate.