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