Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 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> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/sysrq.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/fb.h> |
| 37 | #include <linux/init.h> |
| 38 | |
| 39 | #include "drmP.h" |
| 40 | #include "drm.h" |
| 41 | #include "drm_crtc.h" |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 42 | #include "drm_fb_helper.h" |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 43 | #include "intel_drv.h" |
| 44 | #include "i915_drm.h" |
| 45 | #include "i915_drv.h" |
| 46 | |
| 47 | struct intelfb_par { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 48 | struct drm_fb_helper helper; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 49 | struct intel_framebuffer *intel_fb; |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 50 | struct drm_display_mode *our_mode; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 53 | static struct fb_ops intelfb_ops = { |
| 54 | .owner = THIS_MODULE, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 55 | .fb_check_var = drm_fb_helper_check_var, |
| 56 | .fb_set_par = drm_fb_helper_set_par, |
| 57 | .fb_setcolreg = drm_fb_helper_setcolreg, |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 58 | .fb_fillrect = cfb_fillrect, |
| 59 | .fb_copyarea = cfb_copyarea, |
| 60 | .fb_imageblit = cfb_imageblit, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 61 | .fb_pan_display = drm_fb_helper_pan_display, |
| 62 | .fb_blank = drm_fb_helper_blank, |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 63 | .fb_setcmap = drm_fb_helper_setcmap, |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 66 | static struct drm_fb_helper_funcs intel_fb_helper_funcs = { |
| 67 | .gamma_set = intel_crtc_fb_gamma_set, |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 68 | .gamma_get = intel_crtc_fb_gamma_get, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 72 | /** |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 73 | * Currently it is assumed that the old framebuffer is reused. |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 74 | * |
| 75 | * LOCKING |
| 76 | * caller should hold the mode config lock. |
| 77 | * |
| 78 | */ |
| 79 | int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc) |
| 80 | { |
| 81 | struct fb_info *info; |
| 82 | struct drm_framebuffer *fb; |
| 83 | struct drm_display_mode *mode = crtc->desired_mode; |
| 84 | |
| 85 | fb = crtc->fb; |
| 86 | if (!fb) |
| 87 | return 1; |
| 88 | |
| 89 | info = fb->fbdev; |
| 90 | if (!info) |
| 91 | return 1; |
| 92 | |
| 93 | if (!mode) |
| 94 | return 1; |
| 95 | |
| 96 | info->var.xres = mode->hdisplay; |
| 97 | info->var.right_margin = mode->hsync_start - mode->hdisplay; |
| 98 | info->var.hsync_len = mode->hsync_end - mode->hsync_start; |
| 99 | info->var.left_margin = mode->htotal - mode->hsync_end; |
| 100 | info->var.yres = mode->vdisplay; |
| 101 | info->var.lower_margin = mode->vsync_start - mode->vdisplay; |
| 102 | info->var.vsync_len = mode->vsync_end - mode->vsync_start; |
| 103 | info->var.upper_margin = mode->vtotal - mode->vsync_end; |
| 104 | info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100; |
| 105 | /* avoid overflow */ |
| 106 | info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | EXPORT_SYMBOL(intelfb_resize); |
| 111 | |
Hannes Eder | b358d0a | 2008-12-18 21:18:47 +0100 | [diff] [blame] | 112 | static int intelfb_create(struct drm_device *dev, uint32_t fb_width, |
| 113 | uint32_t fb_height, uint32_t surface_width, |
| 114 | uint32_t surface_height, |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 115 | uint32_t surface_depth, uint32_t surface_bpp, |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 116 | struct drm_framebuffer **fb_p) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 117 | { |
| 118 | struct fb_info *info; |
| 119 | struct intelfb_par *par; |
| 120 | struct drm_framebuffer *fb; |
| 121 | struct intel_framebuffer *intel_fb; |
| 122 | struct drm_mode_fb_cmd mode_cmd; |
| 123 | struct drm_gem_object *fbo = NULL; |
| 124 | struct drm_i915_gem_object *obj_priv; |
| 125 | struct device *device = &dev->pdev->dev; |
| 126 | int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1; |
| 127 | |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 128 | /* we don't do packed 24bpp */ |
| 129 | if (surface_bpp == 24) |
| 130 | surface_bpp = 32; |
| 131 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 132 | mode_cmd.width = surface_width; |
| 133 | mode_cmd.height = surface_height; |
| 134 | |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 135 | mode_cmd.bpp = surface_bpp; |
Eric Anholt | e7da40f | 2008-12-10 17:23:00 -0800 | [diff] [blame] | 136 | mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64); |
Dave Airlie | d50ba25 | 2009-09-23 14:44:08 +1000 | [diff] [blame] | 137 | mode_cmd.depth = surface_depth; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 138 | |
| 139 | size = mode_cmd.pitch * mode_cmd.height; |
| 140 | size = ALIGN(size, PAGE_SIZE); |
| 141 | fbo = drm_gem_object_alloc(dev, size); |
| 142 | if (!fbo) { |
Keith Packard | 1ae8c0a | 2009-06-28 15:42:17 -0700 | [diff] [blame] | 143 | DRM_ERROR("failed to allocate framebuffer\n"); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 144 | ret = -ENOMEM; |
| 145 | goto out; |
| 146 | } |
| 147 | obj_priv = fbo->driver_private; |
| 148 | |
| 149 | mutex_lock(&dev->struct_mutex); |
| 150 | |
Chris Wilson | fd2e8ea | 2010-02-09 14:14:36 +0000 | [diff] [blame] | 151 | ret = i915_gem_object_pin(fbo, 64*1024); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 152 | if (ret) { |
| 153 | DRM_ERROR("failed to pin fb: %d\n", ret); |
| 154 | goto out_unref; |
| 155 | } |
| 156 | |
| 157 | /* Flush everything out, we'll be doing GTT only from now on */ |
| 158 | i915_gem_object_set_to_gtt_domain(fbo, 1); |
| 159 | |
| 160 | ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo); |
| 161 | if (ret) { |
| 162 | DRM_ERROR("failed to allocate fb.\n"); |
Chris Wilson | b4476f5 | 2009-02-11 14:26:36 +0000 | [diff] [blame] | 163 | goto out_unpin; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list); |
| 167 | |
| 168 | intel_fb = to_intel_framebuffer(fb); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 169 | *fb_p = fb; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 170 | |
| 171 | info = framebuffer_alloc(sizeof(struct intelfb_par), device); |
| 172 | if (!info) { |
| 173 | ret = -ENOMEM; |
Chris Wilson | b4476f5 | 2009-02-11 14:26:36 +0000 | [diff] [blame] | 174 | goto out_unpin; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | par = info->par; |
| 178 | |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 179 | par->helper.funcs = &intel_fb_helper_funcs; |
| 180 | par->helper.dev = dev; |
| 181 | ret = drm_fb_helper_init_crtc_count(&par->helper, 2, |
| 182 | INTELFB_CONN_LIMIT); |
| 183 | if (ret) |
| 184 | goto out_unref; |
| 185 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 186 | strcpy(info->fix.id, "inteldrmfb"); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 187 | |
| 188 | info->flags = FBINFO_DEFAULT; |
| 189 | |
| 190 | info->fbops = &intelfb_ops; |
| 191 | |
Dave Airlie | 4410f39 | 2009-06-16 15:34:38 -0700 | [diff] [blame] | 192 | |
| 193 | /* setup aperture base/size for vesafb takeover */ |
| 194 | info->aperture_base = dev->mode_config.fb_base; |
| 195 | if (IS_I9XX(dev)) |
| 196 | info->aperture_size = pci_resource_len(dev->pdev, 2); |
| 197 | else |
| 198 | info->aperture_size = pci_resource_len(dev->pdev, 0); |
| 199 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 200 | info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset; |
| 201 | info->fix.smem_len = size; |
| 202 | |
| 203 | info->flags = FBINFO_DEFAULT; |
| 204 | |
| 205 | info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset, |
| 206 | size); |
| 207 | if (!info->screen_base) { |
| 208 | ret = -ENOSPC; |
Chris Wilson | b4476f5 | 2009-02-11 14:26:36 +0000 | [diff] [blame] | 209 | goto out_unpin; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 210 | } |
| 211 | info->screen_size = size; |
| 212 | |
| 213 | // memset(info->screen_base, 0, size); |
| 214 | |
Dave Airlie | 068143d | 2009-10-05 09:58:02 +1000 | [diff] [blame] | 215 | drm_fb_helper_fill_fix(info, fb->pitch, fb->depth); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 216 | drm_fb_helper_fill_var(info, fb, fb_width, fb_height); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 217 | |
| 218 | /* FIXME: we really shouldn't expose mmio space at all */ |
| 219 | info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar); |
| 220 | info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar); |
| 221 | |
| 222 | info->pixmap.size = 64*1024; |
| 223 | info->pixmap.buf_align = 8; |
| 224 | info->pixmap.access_align = 32; |
| 225 | info->pixmap.flags = FB_PIXMAP_SYSTEM; |
| 226 | info->pixmap.scan_align = 1; |
| 227 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 228 | fb->fbdev = info; |
| 229 | |
| 230 | par->intel_fb = intel_fb; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 231 | |
| 232 | /* To allow resizeing without swapping buffers */ |
Zhao Yakui | 28c9773 | 2009-10-09 11:39:41 +0800 | [diff] [blame] | 233 | DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x, bo %p\n", |
| 234 | intel_fb->base.width, intel_fb->base.height, |
| 235 | obj_priv->gtt_offset, fbo); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 236 | |
| 237 | mutex_unlock(&dev->struct_mutex); |
| 238 | return 0; |
| 239 | |
Chris Wilson | b4476f5 | 2009-02-11 14:26:36 +0000 | [diff] [blame] | 240 | out_unpin: |
| 241 | i915_gem_object_unpin(fbo); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 242 | out_unref: |
| 243 | drm_gem_object_unreference(fbo); |
| 244 | mutex_unlock(&dev->struct_mutex); |
| 245 | out: |
| 246 | return ret; |
| 247 | } |
| 248 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 249 | int intelfb_probe(struct drm_device *dev) |
| 250 | { |
| 251 | int ret; |
| 252 | |
Zhao Yakui | 28c9773 | 2009-10-09 11:39:41 +0800 | [diff] [blame] | 253 | DRM_DEBUG_KMS("\n"); |
Dave Airlie | b8c00ac | 2009-10-06 13:54:01 +1000 | [diff] [blame] | 254 | ret = drm_fb_helper_single_fb_probe(dev, 32, intelfb_create); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | EXPORT_SYMBOL(intelfb_probe); |
| 258 | |
| 259 | int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb) |
| 260 | { |
| 261 | struct fb_info *info; |
| 262 | |
| 263 | if (!fb) |
| 264 | return -EINVAL; |
| 265 | |
| 266 | info = fb->fbdev; |
| 267 | |
| 268 | if (info) { |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 269 | struct intelfb_par *par = info->par; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 270 | unregister_framebuffer(info); |
| 271 | iounmap(info->screen_base); |
Dave Airlie | 785b93e | 2009-08-28 15:46:53 +1000 | [diff] [blame] | 272 | if (info->par) |
| 273 | drm_fb_helper_free(&par->helper); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 274 | framebuffer_release(info); |
| 275 | } |
| 276 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 277 | return 0; |
| 278 | } |
| 279 | EXPORT_SYMBOL(intelfb_remove); |
| 280 | MODULE_LICENSE("GPL and additional rights"); |