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