Alan Cox | 4d8d096 | 2011-11-03 18:21:20 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * Copyright (c) 2007-2011, Intel Corporation. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | **************************************************************************/ |
| 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/tty.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/fb.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/console.h> |
| 31 | |
| 32 | #include <drm/drmP.h> |
| 33 | #include <drm/drm.h> |
| 34 | #include <drm/drm_crtc.h> |
| 35 | |
| 36 | #include "psb_drv.h" |
| 37 | #include "psb_intel_reg.h" |
| 38 | #include "psb_intel_drv.h" |
| 39 | #include "framebuffer.h" |
| 40 | |
| 41 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb); |
| 42 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, |
| 43 | struct drm_file *file_priv, |
| 44 | unsigned int *handle); |
| 45 | |
| 46 | static const struct drm_framebuffer_funcs psb_fb_funcs = { |
| 47 | .destroy = psb_user_framebuffer_destroy, |
| 48 | .create_handle = psb_user_framebuffer_create_handle, |
| 49 | }; |
| 50 | |
| 51 | #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16) |
| 52 | |
| 53 | static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green, |
| 54 | unsigned blue, unsigned transp, |
| 55 | struct fb_info *info) |
| 56 | { |
| 57 | struct psb_fbdev *fbdev = info->par; |
| 58 | struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb; |
| 59 | uint32_t v; |
| 60 | |
| 61 | if (!fb) |
| 62 | return -ENOMEM; |
| 63 | |
| 64 | if (regno > 255) |
| 65 | return 1; |
| 66 | |
| 67 | red = CMAP_TOHW(red, info->var.red.length); |
| 68 | blue = CMAP_TOHW(blue, info->var.blue.length); |
| 69 | green = CMAP_TOHW(green, info->var.green.length); |
| 70 | transp = CMAP_TOHW(transp, info->var.transp.length); |
| 71 | |
| 72 | v = (red << info->var.red.offset) | |
| 73 | (green << info->var.green.offset) | |
| 74 | (blue << info->var.blue.offset) | |
| 75 | (transp << info->var.transp.offset); |
| 76 | |
| 77 | if (regno < 16) { |
| 78 | switch (fb->bits_per_pixel) { |
| 79 | case 16: |
| 80 | ((uint32_t *) info->pseudo_palette)[regno] = v; |
| 81 | break; |
| 82 | case 24: |
| 83 | case 32: |
| 84 | ((uint32_t *) info->pseudo_palette)[regno] = v; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void psbfb_suspend(struct drm_device *dev) |
| 94 | { |
| 95 | struct drm_framebuffer *fb = 0; |
| 96 | struct psb_framebuffer *psbfb = to_psb_fb(fb); |
| 97 | |
| 98 | console_lock(); |
| 99 | mutex_lock(&dev->mode_config.mutex); |
| 100 | list_for_each_entry(fb, &dev->mode_config.fb_list, head) { |
| 101 | struct fb_info *info = psbfb->fbdev; |
| 102 | fb_set_suspend(info, 1); |
| 103 | drm_fb_helper_blank(FB_BLANK_POWERDOWN, info); |
| 104 | } |
| 105 | mutex_unlock(&dev->mode_config.mutex); |
| 106 | console_unlock(); |
| 107 | } |
| 108 | |
| 109 | void psbfb_resume(struct drm_device *dev) |
| 110 | { |
| 111 | struct drm_framebuffer *fb = 0; |
| 112 | struct psb_framebuffer *psbfb = to_psb_fb(fb); |
| 113 | |
| 114 | console_lock(); |
| 115 | mutex_lock(&dev->mode_config.mutex); |
| 116 | list_for_each_entry(fb, &dev->mode_config.fb_list, head) { |
| 117 | struct fb_info *info = psbfb->fbdev; |
| 118 | fb_set_suspend(info, 0); |
| 119 | drm_fb_helper_blank(FB_BLANK_UNBLANK, info); |
| 120 | } |
| 121 | mutex_unlock(&dev->mode_config.mutex); |
| 122 | console_unlock(); |
| 123 | drm_helper_disable_unused_functions(dev); |
| 124 | } |
| 125 | |
| 126 | static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 127 | { |
| 128 | struct psb_framebuffer *psbfb = vma->vm_private_data; |
| 129 | struct drm_device *dev = psbfb->base.dev; |
| 130 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 131 | int page_num; |
| 132 | int i; |
| 133 | unsigned long address; |
| 134 | int ret; |
| 135 | unsigned long pfn; |
| 136 | /* FIXME: assumes fb at stolen base which may not be true */ |
| 137 | unsigned long phys_addr = (unsigned long)dev_priv->stolen_base; |
| 138 | |
| 139 | page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; |
| 140 | address = (unsigned long)vmf->virtual_address; |
| 141 | |
| 142 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 143 | |
| 144 | for (i = 0; i < page_num; i++) { |
| 145 | pfn = (phys_addr >> PAGE_SHIFT); |
| 146 | |
| 147 | ret = vm_insert_mixed(vma, address, pfn); |
| 148 | if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0))) |
| 149 | break; |
| 150 | else if (unlikely(ret != 0)) { |
| 151 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; |
| 152 | return ret; |
| 153 | } |
| 154 | address += PAGE_SIZE; |
| 155 | phys_addr += PAGE_SIZE; |
| 156 | } |
| 157 | return VM_FAULT_NOPAGE; |
| 158 | } |
| 159 | |
| 160 | static void psbfb_vm_open(struct vm_area_struct *vma) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | static void psbfb_vm_close(struct vm_area_struct *vma) |
| 165 | { |
| 166 | } |
| 167 | |
| 168 | static struct vm_operations_struct psbfb_vm_ops = { |
| 169 | .fault = psbfb_vm_fault, |
| 170 | .open = psbfb_vm_open, |
| 171 | .close = psbfb_vm_close |
| 172 | }; |
| 173 | |
| 174 | static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma) |
| 175 | { |
| 176 | struct psb_fbdev *fbdev = info->par; |
| 177 | struct psb_framebuffer *psbfb = &fbdev->pfb; |
| 178 | |
| 179 | if (vma->vm_pgoff != 0) |
| 180 | return -EINVAL; |
| 181 | if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | if (!psbfb->addr_space) |
| 185 | psbfb->addr_space = vma->vm_file->f_mapping; |
| 186 | /* |
| 187 | * If this is a GEM object then info->screen_base is the virtual |
| 188 | * kernel remapping of the object. FIXME: Review if this is |
| 189 | * suitable for our mmap work |
| 190 | */ |
| 191 | vma->vm_ops = &psbfb_vm_ops; |
| 192 | vma->vm_private_data = (void *)psbfb; |
| 193 | vma->vm_flags |= VM_RESERVED | VM_IO | |
| 194 | VM_MIXEDMAP | VM_DONTEXPAND; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int psbfb_ioctl(struct fb_info *info, unsigned int cmd, |
| 199 | unsigned long arg) |
| 200 | { |
| 201 | return -ENOTTY; |
| 202 | } |
| 203 | |
| 204 | static struct fb_ops psbfb_ops = { |
| 205 | .owner = THIS_MODULE, |
| 206 | .fb_check_var = drm_fb_helper_check_var, |
| 207 | .fb_set_par = drm_fb_helper_set_par, |
| 208 | .fb_blank = drm_fb_helper_blank, |
| 209 | .fb_setcolreg = psbfb_setcolreg, |
| 210 | .fb_fillrect = cfb_fillrect, |
| 211 | .fb_copyarea = psbfb_copyarea, |
| 212 | .fb_imageblit = cfb_imageblit, |
| 213 | .fb_mmap = psbfb_mmap, |
| 214 | .fb_sync = psbfb_sync, |
| 215 | .fb_ioctl = psbfb_ioctl, |
| 216 | }; |
| 217 | |
| 218 | static struct fb_ops psbfb_unaccel_ops = { |
| 219 | .owner = THIS_MODULE, |
| 220 | .fb_check_var = drm_fb_helper_check_var, |
| 221 | .fb_set_par = drm_fb_helper_set_par, |
| 222 | .fb_blank = drm_fb_helper_blank, |
| 223 | .fb_setcolreg = psbfb_setcolreg, |
| 224 | .fb_fillrect = cfb_fillrect, |
| 225 | .fb_copyarea = cfb_copyarea, |
| 226 | .fb_imageblit = cfb_imageblit, |
| 227 | .fb_mmap = psbfb_mmap, |
| 228 | .fb_ioctl = psbfb_ioctl, |
| 229 | }; |
| 230 | |
| 231 | /** |
| 232 | * psb_framebuffer_init - initialize a framebuffer |
| 233 | * @dev: our DRM device |
| 234 | * @fb: framebuffer to set up |
| 235 | * @mode_cmd: mode description |
| 236 | * @gt: backing object |
| 237 | * |
| 238 | * Configure and fill in the boilerplate for our frame buffer. Return |
| 239 | * 0 on success or an error code if we fail. |
| 240 | */ |
| 241 | static int psb_framebuffer_init(struct drm_device *dev, |
| 242 | struct psb_framebuffer *fb, |
| 243 | struct drm_mode_fb_cmd *mode_cmd, |
| 244 | struct gtt_range *gt) |
| 245 | { |
| 246 | int ret; |
| 247 | |
| 248 | if (mode_cmd->pitch & 63) |
| 249 | return -EINVAL; |
| 250 | switch (mode_cmd->bpp) { |
| 251 | case 8: |
| 252 | case 16: |
| 253 | case 24: |
| 254 | case 32: |
| 255 | break; |
| 256 | default: |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs); |
| 260 | if (ret) { |
| 261 | dev_err(dev->dev, "framebuffer init failed: %d\n", ret); |
| 262 | return ret; |
| 263 | } |
| 264 | drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd); |
| 265 | fb->gtt = gt; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * psb_framebuffer_create - create a framebuffer backed by gt |
| 271 | * @dev: our DRM device |
| 272 | * @mode_cmd: the description of the requested mode |
| 273 | * @gt: the backing object |
| 274 | * |
| 275 | * Create a framebuffer object backed by the gt, and fill in the |
| 276 | * boilerplate required |
| 277 | * |
| 278 | * TODO: review object references |
| 279 | */ |
| 280 | |
| 281 | static struct drm_framebuffer *psb_framebuffer_create |
| 282 | (struct drm_device *dev, |
| 283 | struct drm_mode_fb_cmd *mode_cmd, |
| 284 | struct gtt_range *gt) |
| 285 | { |
| 286 | struct psb_framebuffer *fb; |
| 287 | int ret; |
| 288 | |
| 289 | fb = kzalloc(sizeof(*fb), GFP_KERNEL); |
| 290 | if (!fb) |
| 291 | return ERR_PTR(-ENOMEM); |
| 292 | |
| 293 | ret = psb_framebuffer_init(dev, fb, mode_cmd, gt); |
| 294 | if (ret) { |
| 295 | kfree(fb); |
| 296 | return ERR_PTR(ret); |
| 297 | } |
| 298 | return &fb->base; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * psbfb_alloc - allocate frame buffer memory |
| 303 | * @dev: the DRM device |
| 304 | * @aligned_size: space needed |
| 305 | * |
| 306 | * Allocate the frame buffer. In the usual case we get a GTT range that |
| 307 | * is stolen memory backed and life is simple. If there isn't sufficient |
| 308 | * stolen memory or the system has no stolen memory we allocate a range |
| 309 | * and back it with a GEM object. |
| 310 | * |
| 311 | * In this case the GEM object has no handle. |
| 312 | * |
| 313 | * FIXME: console speed up - allocate twice the space if room and use |
| 314 | * hardware scrolling for acceleration. |
| 315 | */ |
| 316 | static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size) |
| 317 | { |
| 318 | struct gtt_range *backing; |
| 319 | /* Begin by trying to use stolen memory backing */ |
| 320 | backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1); |
| 321 | if (backing) { |
| 322 | if (drm_gem_private_object_init(dev, |
| 323 | &backing->gem, aligned_size) == 0) |
| 324 | return backing; |
| 325 | psb_gtt_free_range(dev, backing); |
| 326 | } |
| 327 | /* Next try using GEM host memory */ |
| 328 | backing = psb_gtt_alloc_range(dev, aligned_size, "fb(gem)", 0); |
| 329 | if (backing == NULL) |
| 330 | return NULL; |
| 331 | |
| 332 | /* Now back it with an object */ |
| 333 | if (drm_gem_object_init(dev, &backing->gem, aligned_size) != 0) { |
| 334 | psb_gtt_free_range(dev, backing); |
| 335 | return NULL; |
| 336 | } |
| 337 | return backing; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * psbfb_create - create a framebuffer |
| 342 | * @fbdev: the framebuffer device |
| 343 | * @sizes: specification of the layout |
| 344 | * |
| 345 | * Create a framebuffer to the specifications provided |
| 346 | */ |
| 347 | static int psbfb_create(struct psb_fbdev *fbdev, |
| 348 | struct drm_fb_helper_surface_size *sizes) |
| 349 | { |
| 350 | struct drm_device *dev = fbdev->psb_fb_helper.dev; |
| 351 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 352 | struct fb_info *info; |
| 353 | struct drm_framebuffer *fb; |
| 354 | struct psb_framebuffer *psbfb = &fbdev->pfb; |
| 355 | struct drm_mode_fb_cmd mode_cmd; |
| 356 | struct device *device = &dev->pdev->dev; |
| 357 | int size; |
| 358 | int ret; |
| 359 | struct gtt_range *backing; |
| 360 | |
| 361 | mode_cmd.width = sizes->surface_width; |
| 362 | mode_cmd.height = sizes->surface_height; |
| 363 | mode_cmd.bpp = sizes->surface_bpp; |
| 364 | |
| 365 | /* No 24bit packed */ |
| 366 | if (mode_cmd.bpp == 24) |
| 367 | mode_cmd.bpp = 32; |
| 368 | |
| 369 | /* HW requires pitch to be 64 byte aligned */ |
| 370 | mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 7) / 8), 64); |
| 371 | mode_cmd.depth = sizes->surface_depth; |
| 372 | |
| 373 | size = mode_cmd.pitch * mode_cmd.height; |
| 374 | size = ALIGN(size, PAGE_SIZE); |
| 375 | |
| 376 | /* Allocate the framebuffer in the GTT with stolen page backing */ |
| 377 | backing = psbfb_alloc(dev, size); |
| 378 | if (backing == NULL) |
| 379 | return -ENOMEM; |
| 380 | |
| 381 | mutex_lock(&dev->struct_mutex); |
| 382 | |
| 383 | info = framebuffer_alloc(0, device); |
| 384 | if (!info) { |
| 385 | ret = -ENOMEM; |
| 386 | goto out_err1; |
| 387 | } |
| 388 | info->par = fbdev; |
| 389 | |
| 390 | ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing); |
| 391 | if (ret) |
| 392 | goto out_unref; |
| 393 | |
| 394 | fb = &psbfb->base; |
| 395 | psbfb->fbdev = info; |
| 396 | |
| 397 | fbdev->psb_fb_helper.fb = fb; |
| 398 | fbdev->psb_fb_helper.fbdev = info; |
| 399 | |
| 400 | strcpy(info->fix.id, "psbfb"); |
| 401 | |
| 402 | info->flags = FBINFO_DEFAULT; |
| 403 | /* No 2D engine */ |
| 404 | if (!dev_priv->ops->accel_2d) |
| 405 | info->fbops = &psbfb_unaccel_ops; |
| 406 | else |
| 407 | info->fbops = &psbfb_ops; |
| 408 | |
| 409 | ret = fb_alloc_cmap(&info->cmap, 256, 0); |
| 410 | if (ret) { |
| 411 | ret = -ENOMEM; |
| 412 | goto out_unref; |
| 413 | } |
| 414 | |
| 415 | info->fix.smem_start = dev->mode_config.fb_base; |
| 416 | info->fix.smem_len = size; |
| 417 | |
| 418 | if (backing->stolen) { |
| 419 | /* Accessed stolen memory directly */ |
| 420 | info->screen_base = (char *)dev_priv->vram_addr + |
| 421 | backing->offset; |
| 422 | } else { |
| 423 | /* Pin the pages into the GTT and create a mapping to them */ |
| 424 | psb_gtt_pin(backing); |
| 425 | info->screen_base = vm_map_ram(backing->pages, backing->npage, |
| 426 | -1, PAGE_KERNEL); |
| 427 | if (info->screen_base == NULL) { |
| 428 | psb_gtt_unpin(backing); |
| 429 | ret = -ENOMEM; |
| 430 | goto out_unref; |
| 431 | } |
| 432 | psbfb->vm_map = 1; |
| 433 | } |
| 434 | info->screen_size = size; |
| 435 | |
| 436 | if (dev_priv->gtt.stolen_size) { |
| 437 | info->apertures = alloc_apertures(1); |
| 438 | if (!info->apertures) { |
| 439 | ret = -ENOMEM; |
| 440 | goto out_unref; |
| 441 | } |
| 442 | info->apertures->ranges[0].base = dev->mode_config.fb_base; |
| 443 | info->apertures->ranges[0].size = dev_priv->gtt.stolen_size; |
| 444 | } |
| 445 | |
| 446 | drm_fb_helper_fill_fix(info, fb->pitch, fb->depth); |
| 447 | drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper, |
| 448 | sizes->fb_width, sizes->fb_height); |
| 449 | |
| 450 | info->fix.mmio_start = pci_resource_start(dev->pdev, 0); |
| 451 | info->fix.mmio_len = pci_resource_len(dev->pdev, 0); |
| 452 | |
| 453 | info->pixmap.size = 64 * 1024; |
| 454 | info->pixmap.buf_align = 8; |
| 455 | info->pixmap.access_align = 32; |
| 456 | info->pixmap.flags = FB_PIXMAP_SYSTEM; |
| 457 | info->pixmap.scan_align = 1; |
| 458 | |
| 459 | dev_info(dev->dev, "allocated %dx%d fb\n", |
| 460 | psbfb->base.width, psbfb->base.height); |
| 461 | |
| 462 | mutex_unlock(&dev->struct_mutex); |
| 463 | return 0; |
| 464 | out_unref: |
| 465 | if (backing->stolen) |
| 466 | psb_gtt_free_range(dev, backing); |
| 467 | else { |
| 468 | if (psbfb->vm_map) |
| 469 | vm_unmap_ram(info->screen_base, backing->npage); |
| 470 | drm_gem_object_unreference(&backing->gem); |
| 471 | } |
| 472 | out_err1: |
| 473 | mutex_unlock(&dev->struct_mutex); |
| 474 | psb_gtt_free_range(dev, backing); |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * psb_user_framebuffer_create - create framebuffer |
| 480 | * @dev: our DRM device |
| 481 | * @filp: client file |
| 482 | * @cmd: mode request |
| 483 | * |
| 484 | * Create a new framebuffer backed by a userspace GEM object |
| 485 | */ |
| 486 | static struct drm_framebuffer *psb_user_framebuffer_create |
| 487 | (struct drm_device *dev, struct drm_file *filp, |
| 488 | struct drm_mode_fb_cmd *cmd) |
| 489 | { |
| 490 | struct gtt_range *r; |
| 491 | struct drm_gem_object *obj; |
| 492 | |
| 493 | /* |
| 494 | * Find the GEM object and thus the gtt range object that is |
| 495 | * to back this space |
| 496 | */ |
| 497 | obj = drm_gem_object_lookup(dev, filp, cmd->handle); |
| 498 | if (obj == NULL) |
| 499 | return ERR_PTR(-ENOENT); |
| 500 | |
| 501 | /* Let the core code do all the work */ |
| 502 | r = container_of(obj, struct gtt_range, gem); |
| 503 | return psb_framebuffer_create(dev, cmd, r); |
| 504 | } |
| 505 | |
| 506 | static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, |
| 507 | u16 blue, int regno) |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red, |
| 512 | u16 *green, u16 *blue, int regno) |
| 513 | { |
| 514 | } |
| 515 | |
| 516 | static int psbfb_probe(struct drm_fb_helper *helper, |
| 517 | struct drm_fb_helper_surface_size *sizes) |
| 518 | { |
| 519 | struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper; |
| 520 | int new_fb = 0; |
| 521 | int ret; |
| 522 | |
| 523 | if (!helper->fb) { |
| 524 | ret = psbfb_create(psb_fbdev, sizes); |
| 525 | if (ret) |
| 526 | return ret; |
| 527 | new_fb = 1; |
| 528 | } |
| 529 | return new_fb; |
| 530 | } |
| 531 | |
| 532 | struct drm_fb_helper_funcs psb_fb_helper_funcs = { |
| 533 | .gamma_set = psbfb_gamma_set, |
| 534 | .gamma_get = psbfb_gamma_get, |
| 535 | .fb_probe = psbfb_probe, |
| 536 | }; |
| 537 | |
| 538 | int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev) |
| 539 | { |
| 540 | struct fb_info *info; |
| 541 | struct psb_framebuffer *psbfb = &fbdev->pfb; |
| 542 | |
| 543 | if (fbdev->psb_fb_helper.fbdev) { |
| 544 | info = fbdev->psb_fb_helper.fbdev; |
| 545 | |
| 546 | /* If this is our base framebuffer then kill any virtual map |
| 547 | for the framebuffer layer and unpin it */ |
| 548 | if (psbfb->vm_map) { |
| 549 | vm_unmap_ram(info->screen_base, psbfb->gtt->npage); |
| 550 | psb_gtt_unpin(psbfb->gtt); |
| 551 | } |
| 552 | unregister_framebuffer(info); |
| 553 | if (info->cmap.len) |
| 554 | fb_dealloc_cmap(&info->cmap); |
| 555 | framebuffer_release(info); |
| 556 | } |
| 557 | drm_fb_helper_fini(&fbdev->psb_fb_helper); |
| 558 | drm_framebuffer_cleanup(&psbfb->base); |
| 559 | |
| 560 | if (psbfb->gtt) |
| 561 | drm_gem_object_unreference(&psbfb->gtt->gem); |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | int psb_fbdev_init(struct drm_device *dev) |
| 566 | { |
| 567 | struct psb_fbdev *fbdev; |
| 568 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 569 | |
| 570 | fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL); |
| 571 | if (!fbdev) { |
| 572 | dev_err(dev->dev, "no memory\n"); |
| 573 | return -ENOMEM; |
| 574 | } |
| 575 | |
| 576 | dev_priv->fbdev = fbdev; |
| 577 | fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs; |
| 578 | |
| 579 | drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs, |
| 580 | INTELFB_CONN_LIMIT); |
| 581 | |
| 582 | drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper); |
| 583 | drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | void psb_fbdev_fini(struct drm_device *dev) |
| 588 | { |
| 589 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 590 | |
| 591 | if (!dev_priv->fbdev) |
| 592 | return; |
| 593 | |
| 594 | psb_fbdev_destroy(dev, dev_priv->fbdev); |
| 595 | kfree(dev_priv->fbdev); |
| 596 | dev_priv->fbdev = NULL; |
| 597 | } |
| 598 | |
| 599 | static void psbfb_output_poll_changed(struct drm_device *dev) |
| 600 | { |
| 601 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 602 | struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev; |
| 603 | drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * psb_user_framebuffer_create_handle - add hamdle to a framebuffer |
| 608 | * @fb: framebuffer |
| 609 | * @file_priv: our DRM file |
| 610 | * @handle: returned handle |
| 611 | * |
| 612 | * Our framebuffer object is a GTT range which also contains a GEM |
| 613 | * object. We need to turn it into a handle for userspace. GEM will do |
| 614 | * the work for us |
| 615 | */ |
| 616 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, |
| 617 | struct drm_file *file_priv, |
| 618 | unsigned int *handle) |
| 619 | { |
| 620 | struct psb_framebuffer *psbfb = to_psb_fb(fb); |
| 621 | struct gtt_range *r = psbfb->gtt; |
| 622 | return drm_gem_handle_create(file_priv, &r->gem, handle); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * psb_user_framebuffer_destroy - destruct user created fb |
| 627 | * @fb: framebuffer |
| 628 | * |
| 629 | * User framebuffers are backed by GEM objects so all we have to do is |
| 630 | * clean up a bit and drop the reference, GEM will handle the fallout |
| 631 | */ |
| 632 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb) |
| 633 | { |
| 634 | struct psb_framebuffer *psbfb = to_psb_fb(fb); |
| 635 | struct gtt_range *r = psbfb->gtt; |
| 636 | struct drm_device *dev = fb->dev; |
| 637 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 638 | struct psb_fbdev *fbdev = dev_priv->fbdev; |
| 639 | struct drm_crtc *crtc; |
| 640 | int reset = 0; |
| 641 | |
| 642 | /* Should never get stolen memory for a user fb */ |
| 643 | WARN_ON(r->stolen); |
| 644 | |
| 645 | /* Check if we are erroneously live */ |
| 646 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 647 | if (crtc->fb == fb) |
| 648 | reset = 1; |
| 649 | |
| 650 | if (reset) |
| 651 | /* |
| 652 | * Now force a sane response before we permit the DRM CRTC |
| 653 | * layer to do stupid things like blank the display. Instead |
| 654 | * we reset this framebuffer as if the user had forced a reset. |
| 655 | * We must do this before the cleanup so that the DRM layer |
| 656 | * doesn't get a chance to stick its oar in where it isn't |
| 657 | * wanted. |
| 658 | */ |
| 659 | drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper); |
| 660 | |
| 661 | /* Let DRM do its clean up */ |
| 662 | drm_framebuffer_cleanup(fb); |
| 663 | /* We are no longer using the resource in GEM */ |
| 664 | drm_gem_object_unreference_unlocked(&r->gem); |
| 665 | kfree(fb); |
| 666 | } |
| 667 | |
| 668 | static const struct drm_mode_config_funcs psb_mode_funcs = { |
| 669 | .fb_create = psb_user_framebuffer_create, |
| 670 | .output_poll_changed = psbfb_output_poll_changed, |
| 671 | }; |
| 672 | |
| 673 | static int psb_create_backlight_property(struct drm_device *dev) |
| 674 | { |
| 675 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 676 | struct drm_property *backlight; |
| 677 | |
| 678 | if (dev_priv->backlight_property) |
| 679 | return 0; |
| 680 | |
| 681 | backlight = drm_property_create(dev, DRM_MODE_PROP_RANGE, |
| 682 | "backlight", 2); |
| 683 | backlight->values[0] = 0; |
| 684 | backlight->values[1] = 100; |
| 685 | |
| 686 | dev_priv->backlight_property = backlight; |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static void psb_setup_outputs(struct drm_device *dev) |
| 692 | { |
| 693 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 694 | struct drm_connector *connector; |
| 695 | |
| 696 | drm_mode_create_scaling_mode_property(dev); |
| 697 | psb_create_backlight_property(dev); |
| 698 | |
| 699 | dev_priv->ops->output_init(dev); |
| 700 | |
| 701 | list_for_each_entry(connector, &dev->mode_config.connector_list, |
| 702 | head) { |
| 703 | struct psb_intel_output *psb_intel_output = |
| 704 | to_psb_intel_output(connector); |
| 705 | struct drm_encoder *encoder = &psb_intel_output->enc; |
| 706 | int crtc_mask = 0, clone_mask = 0; |
| 707 | |
| 708 | /* valid crtcs */ |
| 709 | switch (psb_intel_output->type) { |
| 710 | case INTEL_OUTPUT_ANALOG: |
| 711 | crtc_mask = (1 << 0); |
| 712 | clone_mask = (1 << INTEL_OUTPUT_ANALOG); |
| 713 | break; |
| 714 | case INTEL_OUTPUT_SDVO: |
| 715 | crtc_mask = ((1 << 0) | (1 << 1)); |
| 716 | clone_mask = (1 << INTEL_OUTPUT_SDVO); |
| 717 | break; |
| 718 | case INTEL_OUTPUT_LVDS: |
| 719 | if (IS_MRST(dev)) |
| 720 | crtc_mask = (1 << 0); |
| 721 | else |
| 722 | crtc_mask = (1 << 1); |
| 723 | clone_mask = (1 << INTEL_OUTPUT_LVDS); |
| 724 | break; |
| 725 | case INTEL_OUTPUT_MIPI: |
| 726 | crtc_mask = (1 << 0); |
| 727 | clone_mask = (1 << INTEL_OUTPUT_MIPI); |
| 728 | break; |
| 729 | case INTEL_OUTPUT_MIPI2: |
| 730 | crtc_mask = (1 << 2); |
| 731 | clone_mask = (1 << INTEL_OUTPUT_MIPI2); |
| 732 | break; |
| 733 | case INTEL_OUTPUT_HDMI: |
| 734 | if (IS_MFLD(dev)) |
| 735 | crtc_mask = (1 << 1); |
| 736 | else |
| 737 | crtc_mask = (1 << 0); |
| 738 | clone_mask = (1 << INTEL_OUTPUT_HDMI); |
| 739 | break; |
| 740 | } |
| 741 | encoder->possible_crtcs = crtc_mask; |
| 742 | encoder->possible_clones = |
| 743 | psb_intel_connector_clones(dev, clone_mask); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | void psb_modeset_init(struct drm_device *dev) |
| 748 | { |
| 749 | struct drm_psb_private *dev_priv = dev->dev_private; |
| 750 | struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev; |
| 751 | int i; |
| 752 | |
| 753 | drm_mode_config_init(dev); |
| 754 | |
| 755 | dev->mode_config.min_width = 0; |
| 756 | dev->mode_config.min_height = 0; |
| 757 | |
| 758 | dev->mode_config.funcs = (void *) &psb_mode_funcs; |
| 759 | |
| 760 | /* set memory base */ |
| 761 | /* MRST and PSB should use BAR 2*/ |
| 762 | pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *) |
| 763 | &(dev->mode_config.fb_base)); |
| 764 | |
| 765 | /* num pipes is 2 for PSB but 1 for Mrst */ |
| 766 | for (i = 0; i < dev_priv->num_pipe; i++) |
| 767 | psb_intel_crtc_init(dev, i, mode_dev); |
| 768 | |
| 769 | dev->mode_config.max_width = 2048; |
| 770 | dev->mode_config.max_height = 2048; |
| 771 | |
| 772 | psb_setup_outputs(dev); |
| 773 | } |
| 774 | |
| 775 | void psb_modeset_cleanup(struct drm_device *dev) |
| 776 | { |
| 777 | mutex_lock(&dev->struct_mutex); |
| 778 | |
| 779 | drm_kms_helper_poll_fini(dev); |
| 780 | psb_fbdev_fini(dev); |
| 781 | drm_mode_config_cleanup(dev); |
| 782 | |
| 783 | mutex_unlock(&dev->struct_mutex); |
| 784 | } |