blob: c1c4dc174fa27bb51245360b5db7f884d425e858 [file] [log] [blame]
Alan Cox4d8d0962011-11-03 18:21:20 +00001/**************************************************************************
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 Airliea9a644a2011-11-28 14:08:46 +000035#include <drm/drm_fb_helper.h>
Alan Cox4d8d0962011-11-03 18:21:20 +000036
37#include "psb_drv.h"
38#include "psb_intel_reg.h"
39#include "psb_intel_drv.h"
40#include "framebuffer.h"
Alan Coxa6ba5822011-11-29 22:27:22 +000041#include "gtt.h"
Alan Cox4d8d0962011-11-03 18:21:20 +000042
43static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
44static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
45 struct drm_file *file_priv,
46 unsigned int *handle);
47
48static 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
55static 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 Coxa6ba5822011-11-29 22:27:22 +000094static 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 Cox4d8d0962011-11-03 18:21:20 +0000113
114void psbfb_suspend(struct drm_device *dev)
115{
Ryan Mallon2d8357e2012-01-27 17:28:24 +1100116 struct drm_framebuffer *fb;
Alan Cox4d8d0962011-11-03 18:21:20 +0000117
118 console_lock();
119 mutex_lock(&dev->mode_config.mutex);
120 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
Ryan Mallon2d8357e2012-01-27 17:28:24 +1100121 struct psb_framebuffer *psbfb = to_psb_fb(fb);
Alan Cox4d8d0962011-11-03 18:21:20 +0000122 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
130void psbfb_resume(struct drm_device *dev)
131{
Ryan Mallon2d8357e2012-01-27 17:28:24 +1100132 struct drm_framebuffer *fb;
Alan Cox4d8d0962011-11-03 18:21:20 +0000133
134 console_lock();
135 mutex_lock(&dev->mode_config.mutex);
136 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
Ryan Mallon2d8357e2012-01-27 17:28:24 +1100137 struct psb_framebuffer *psbfb = to_psb_fb(fb);
Alan Cox4d8d0962011-11-03 18:21:20 +0000138 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
147static 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
181static void psbfb_vm_open(struct vm_area_struct *vma)
182{
183}
184
185static void psbfb_vm_close(struct vm_area_struct *vma)
186{
187}
188
189static struct vm_operations_struct psbfb_vm_ops = {
190 .fault = psbfb_vm_fault,
191 .open = psbfb_vm_open,
192 .close = psbfb_vm_close
193};
194
195static 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
219static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
220 unsigned long arg)
221{
222 return -ENOTTY;
223}
224
225static 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 Coxa6ba5822011-11-29 22:27:22 +0000239static 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 Cox4d8d0962011-11-03 18:21:20 +0000254static 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 */
277static int psb_framebuffer_init(struct drm_device *dev,
278 struct psb_framebuffer *fb,
Dave Airliea9a644a2011-11-28 14:08:46 +0000279 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000280 struct gtt_range *gt)
281{
Dave Airliea9a644a2011-11-28 14:08:46 +0000282 u32 bpp, depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000283 int ret;
284
Dave Airlie248dbc22011-11-29 20:02:54 +0000285 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
Dave Airliea9a644a2011-11-28 14:08:46 +0000286
287 if (mode_cmd->pitches[0] & 63)
Alan Cox4d8d0962011-11-03 18:21:20 +0000288 return -EINVAL;
Dave Airliea9a644a2011-11-28 14:08:46 +0000289 switch (bpp) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000290 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
320static struct drm_framebuffer *psb_framebuffer_create
321 (struct drm_device *dev,
Dave Airliea9a644a2011-11-28 14:08:46 +0000322 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000323 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 Coxa6ba5822011-11-29 22:27:22 +0000344 * @force: fall back to GEM buffers if need be
Alan Cox4d8d0962011-11-03 18:21:20 +0000345 *
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 Coxdffc9ce2011-11-29 22:19:47 +0000348 * 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 Cox4d8d0962011-11-03 18:21:20 +0000350 *
Alan Coxdffc9ce2011-11-29 22:19:47 +0000351 * Re-address this as and if the framebuffer layer grows this ability.
Alan Cox4d8d0962011-11-03 18:21:20 +0000352 */
353static 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 Coxdffc9ce2011-11-29 22:19:47 +0000364 return NULL;
Alan Cox4d8d0962011-11-03 18:21:20 +0000365}
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 */
374static 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 Airliea9a644a2011-11-28 14:08:46 +0000382 struct drm_mode_fb_cmd2 mode_cmd;
Alan Cox4d8d0962011-11-03 18:21:20 +0000383 struct device *device = &dev->pdev->dev;
384 int size;
385 int ret;
386 struct gtt_range *backing;
Dave Airliea9a644a2011-11-28 14:08:46 +0000387 u32 bpp, depth;
Alan Cox1b223c92011-11-29 22:27:34 +0000388 int gtt_roll = 0;
389 int pitch_lines = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000390
391 mode_cmd.width = sizes->surface_width;
392 mode_cmd.height = sizes->surface_height;
Dave Airliea9a644a2011-11-28 14:08:46 +0000393 bpp = sizes->surface_bpp;
Alan Cox4d8d0962011-11-03 18:21:20 +0000394
395 /* No 24bit packed */
Dave Airliea9a644a2011-11-28 14:08:46 +0000396 if (bpp == 24)
397 bpp = 32;
Alan Cox4d8d0962011-11-03 18:21:20 +0000398
Alan Cox1b223c92011-11-29 22:27:34 +0000399 do {
400 /*
401 * Acceleration via the GTT requires pitch to be
402 * power of two aligned. Preferably page but less
403 * is ok with some fonts
404 */
405 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
406 depth = sizes->surface_depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000407
Alan Cox1b223c92011-11-29 22:27:34 +0000408 size = mode_cmd.pitches[0] * mode_cmd.height;
409 size = ALIGN(size, PAGE_SIZE);
Alan Cox4d8d0962011-11-03 18:21:20 +0000410
Alan Cox1b223c92011-11-29 22:27:34 +0000411 /* Allocate the fb in the GTT with stolen page backing */
412 backing = psbfb_alloc(dev, size);
413
414 if (pitch_lines)
415 pitch_lines *= 2;
416 else
417 pitch_lines = 1;
418 gtt_roll++;
419 } while (backing == NULL && pitch_lines <= 16);
420
421 /* The final pitch we accepted if we succeeded */
422 pitch_lines /= 2;
423
Alan Coxa6ba5822011-11-29 22:27:22 +0000424 if (backing == NULL) {
425 /*
426 * We couldn't get the space we wanted, fall back to the
427 * display engine requirement instead. The HW requires
428 * the pitch to be 64 byte aligned
Alan Coxa6ba5822011-11-29 22:27:22 +0000429 */
430
431 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
Alan Cox1b223c92011-11-29 22:27:34 +0000432 pitch_lines = 64;
Alan Coxa6ba5822011-11-29 22:27:22 +0000433
434 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
435
436 size = mode_cmd.pitches[0] * mode_cmd.height;
437 size = ALIGN(size, PAGE_SIZE);
438
439 /* Allocate the framebuffer in the GTT with stolen page backing */
440 backing = psbfb_alloc(dev, size);
441 if (backing == NULL)
442 return -ENOMEM;
443 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000444
445 mutex_lock(&dev->struct_mutex);
446
447 info = framebuffer_alloc(0, device);
448 if (!info) {
449 ret = -ENOMEM;
450 goto out_err1;
451 }
452 info->par = fbdev;
453
Dave Airliea9a644a2011-11-28 14:08:46 +0000454 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
455
Alan Cox4d8d0962011-11-03 18:21:20 +0000456 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
457 if (ret)
458 goto out_unref;
459
460 fb = &psbfb->base;
461 psbfb->fbdev = info;
462
463 fbdev->psb_fb_helper.fb = fb;
464 fbdev->psb_fb_helper.fbdev = info;
465
466 strcpy(info->fix.id, "psbfb");
467
468 info->flags = FBINFO_DEFAULT;
Alan Cox1b223c92011-11-29 22:27:34 +0000469 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
470 info->fbops = &psbfb_ops;
471 else if (gtt_roll) { /* GTT rolling seems best */
Alan Coxa6ba5822011-11-29 22:27:22 +0000472 info->fbops = &psbfb_roll_ops;
473 info->flags |= FBINFO_HWACCEL_YPAN;
Alan Cox1b223c92011-11-29 22:27:34 +0000474 } else /* Software */
Alan Coxa6ba5822011-11-29 22:27:22 +0000475 info->fbops = &psbfb_unaccel_ops;
Alan Cox4d8d0962011-11-03 18:21:20 +0000476
477 ret = fb_alloc_cmap(&info->cmap, 256, 0);
478 if (ret) {
479 ret = -ENOMEM;
480 goto out_unref;
481 }
482
483 info->fix.smem_start = dev->mode_config.fb_base;
484 info->fix.smem_len = size;
Alan Coxa6ba5822011-11-29 22:27:22 +0000485 info->fix.ywrapstep = gtt_roll;
486 info->fix.ypanstep = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000487
Alan Coxdffc9ce2011-11-29 22:19:47 +0000488 /* Accessed stolen memory directly */
489 info->screen_base = (char *)dev_priv->vram_addr +
Alan Cox4d8d0962011-11-03 18:21:20 +0000490 backing->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000491 info->screen_size = size;
492
493 if (dev_priv->gtt.stolen_size) {
494 info->apertures = alloc_apertures(1);
495 if (!info->apertures) {
496 ret = -ENOMEM;
497 goto out_unref;
498 }
499 info->apertures->ranges[0].base = dev->mode_config.fb_base;
500 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
501 }
502
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200503 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Alan Cox4d8d0962011-11-03 18:21:20 +0000504 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
505 sizes->fb_width, sizes->fb_height);
506
507 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
508 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
509
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100510 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Alan Cox4d8d0962011-11-03 18:21:20 +0000511
512 dev_info(dev->dev, "allocated %dx%d fb\n",
513 psbfb->base.width, psbfb->base.height);
514
515 mutex_unlock(&dev->struct_mutex);
516 return 0;
517out_unref:
518 if (backing->stolen)
519 psb_gtt_free_range(dev, backing);
Alan Coxdffc9ce2011-11-29 22:19:47 +0000520 else
Alan Cox4d8d0962011-11-03 18:21:20 +0000521 drm_gem_object_unreference(&backing->gem);
Alan Cox4d8d0962011-11-03 18:21:20 +0000522out_err1:
523 mutex_unlock(&dev->struct_mutex);
524 psb_gtt_free_range(dev, backing);
525 return ret;
526}
527
528/**
529 * psb_user_framebuffer_create - create framebuffer
530 * @dev: our DRM device
531 * @filp: client file
532 * @cmd: mode request
533 *
534 * Create a new framebuffer backed by a userspace GEM object
535 */
536static struct drm_framebuffer *psb_user_framebuffer_create
537 (struct drm_device *dev, struct drm_file *filp,
Dave Airliea9a644a2011-11-28 14:08:46 +0000538 struct drm_mode_fb_cmd2 *cmd)
Alan Cox4d8d0962011-11-03 18:21:20 +0000539{
540 struct gtt_range *r;
541 struct drm_gem_object *obj;
542
543 /*
544 * Find the GEM object and thus the gtt range object that is
545 * to back this space
546 */
Dave Airliea9a644a2011-11-28 14:08:46 +0000547 obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
Alan Cox4d8d0962011-11-03 18:21:20 +0000548 if (obj == NULL)
549 return ERR_PTR(-ENOENT);
550
551 /* Let the core code do all the work */
552 r = container_of(obj, struct gtt_range, gem);
553 return psb_framebuffer_create(dev, cmd, r);
554}
555
556static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
557 u16 blue, int regno)
558{
559}
560
561static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
562 u16 *green, u16 *blue, int regno)
563{
564}
565
566static int psbfb_probe(struct drm_fb_helper *helper,
567 struct drm_fb_helper_surface_size *sizes)
568{
569 struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
570 int new_fb = 0;
571 int ret;
572
573 if (!helper->fb) {
574 ret = psbfb_create(psb_fbdev, sizes);
575 if (ret)
576 return ret;
577 new_fb = 1;
578 }
579 return new_fb;
580}
581
582struct drm_fb_helper_funcs psb_fb_helper_funcs = {
583 .gamma_set = psbfb_gamma_set,
584 .gamma_get = psbfb_gamma_get,
585 .fb_probe = psbfb_probe,
586};
587
588int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
589{
590 struct fb_info *info;
591 struct psb_framebuffer *psbfb = &fbdev->pfb;
592
593 if (fbdev->psb_fb_helper.fbdev) {
594 info = fbdev->psb_fb_helper.fbdev;
Alan Cox4d8d0962011-11-03 18:21:20 +0000595 unregister_framebuffer(info);
596 if (info->cmap.len)
597 fb_dealloc_cmap(&info->cmap);
598 framebuffer_release(info);
599 }
600 drm_fb_helper_fini(&fbdev->psb_fb_helper);
601 drm_framebuffer_cleanup(&psbfb->base);
602
603 if (psbfb->gtt)
604 drm_gem_object_unreference(&psbfb->gtt->gem);
605 return 0;
606}
607
608int psb_fbdev_init(struct drm_device *dev)
609{
610 struct psb_fbdev *fbdev;
611 struct drm_psb_private *dev_priv = dev->dev_private;
612
613 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
614 if (!fbdev) {
615 dev_err(dev->dev, "no memory\n");
616 return -ENOMEM;
617 }
618
619 dev_priv->fbdev = fbdev;
620 fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
621
622 drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
623 INTELFB_CONN_LIMIT);
624
625 drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
626 drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
627 return 0;
628}
629
630void psb_fbdev_fini(struct drm_device *dev)
631{
632 struct drm_psb_private *dev_priv = dev->dev_private;
633
634 if (!dev_priv->fbdev)
635 return;
636
637 psb_fbdev_destroy(dev, dev_priv->fbdev);
638 kfree(dev_priv->fbdev);
639 dev_priv->fbdev = NULL;
640}
641
642static void psbfb_output_poll_changed(struct drm_device *dev)
643{
644 struct drm_psb_private *dev_priv = dev->dev_private;
645 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
646 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
647}
648
649/**
650 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
651 * @fb: framebuffer
652 * @file_priv: our DRM file
653 * @handle: returned handle
654 *
655 * Our framebuffer object is a GTT range which also contains a GEM
656 * object. We need to turn it into a handle for userspace. GEM will do
657 * the work for us
658 */
659static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
660 struct drm_file *file_priv,
661 unsigned int *handle)
662{
663 struct psb_framebuffer *psbfb = to_psb_fb(fb);
664 struct gtt_range *r = psbfb->gtt;
665 return drm_gem_handle_create(file_priv, &r->gem, handle);
666}
667
668/**
669 * psb_user_framebuffer_destroy - destruct user created fb
670 * @fb: framebuffer
671 *
672 * User framebuffers are backed by GEM objects so all we have to do is
673 * clean up a bit and drop the reference, GEM will handle the fallout
674 */
675static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
676{
677 struct psb_framebuffer *psbfb = to_psb_fb(fb);
678 struct gtt_range *r = psbfb->gtt;
679 struct drm_device *dev = fb->dev;
680 struct drm_psb_private *dev_priv = dev->dev_private;
681 struct psb_fbdev *fbdev = dev_priv->fbdev;
682 struct drm_crtc *crtc;
683 int reset = 0;
684
685 /* Should never get stolen memory for a user fb */
686 WARN_ON(r->stolen);
687
688 /* Check if we are erroneously live */
689 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
690 if (crtc->fb == fb)
691 reset = 1;
692
693 if (reset)
694 /*
695 * Now force a sane response before we permit the DRM CRTC
696 * layer to do stupid things like blank the display. Instead
697 * we reset this framebuffer as if the user had forced a reset.
698 * We must do this before the cleanup so that the DRM layer
699 * doesn't get a chance to stick its oar in where it isn't
700 * wanted.
701 */
702 drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
703
704 /* Let DRM do its clean up */
705 drm_framebuffer_cleanup(fb);
706 /* We are no longer using the resource in GEM */
707 drm_gem_object_unreference_unlocked(&r->gem);
708 kfree(fb);
709}
710
711static const struct drm_mode_config_funcs psb_mode_funcs = {
712 .fb_create = psb_user_framebuffer_create,
713 .output_poll_changed = psbfb_output_poll_changed,
714};
715
716static int psb_create_backlight_property(struct drm_device *dev)
717{
718 struct drm_psb_private *dev_priv = dev->dev_private;
719 struct drm_property *backlight;
720
721 if (dev_priv->backlight_property)
722 return 0;
723
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100724 backlight = drm_property_create_range(dev, 0, "backlight", 0, 100);
Alan Cox4d8d0962011-11-03 18:21:20 +0000725
726 dev_priv->backlight_property = backlight;
727
728 return 0;
729}
730
731static void psb_setup_outputs(struct drm_device *dev)
732{
733 struct drm_psb_private *dev_priv = dev->dev_private;
734 struct drm_connector *connector;
735
736 drm_mode_create_scaling_mode_property(dev);
737 psb_create_backlight_property(dev);
738
739 dev_priv->ops->output_init(dev);
740
741 list_for_each_entry(connector, &dev->mode_config.connector_list,
742 head) {
Patrik Jakobsson1730f892011-12-19 21:40:33 +0000743 struct psb_intel_encoder *psb_intel_encoder =
744 psb_intel_attached_encoder(connector);
745 struct drm_encoder *encoder = &psb_intel_encoder->base;
Alan Cox4d8d0962011-11-03 18:21:20 +0000746 int crtc_mask = 0, clone_mask = 0;
747
748 /* valid crtcs */
Patrik Jakobsson1730f892011-12-19 21:40:33 +0000749 switch (psb_intel_encoder->type) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000750 case INTEL_OUTPUT_ANALOG:
751 crtc_mask = (1 << 0);
752 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
753 break;
754 case INTEL_OUTPUT_SDVO:
755 crtc_mask = ((1 << 0) | (1 << 1));
756 clone_mask = (1 << INTEL_OUTPUT_SDVO);
757 break;
758 case INTEL_OUTPUT_LVDS:
759 if (IS_MRST(dev))
760 crtc_mask = (1 << 0);
761 else
762 crtc_mask = (1 << 1);
763 clone_mask = (1 << INTEL_OUTPUT_LVDS);
764 break;
765 case INTEL_OUTPUT_MIPI:
766 crtc_mask = (1 << 0);
767 clone_mask = (1 << INTEL_OUTPUT_MIPI);
768 break;
769 case INTEL_OUTPUT_MIPI2:
770 crtc_mask = (1 << 2);
771 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
772 break;
773 case INTEL_OUTPUT_HDMI:
774 if (IS_MFLD(dev))
775 crtc_mask = (1 << 1);
776 else
777 crtc_mask = (1 << 0);
778 clone_mask = (1 << INTEL_OUTPUT_HDMI);
779 break;
780 }
781 encoder->possible_crtcs = crtc_mask;
782 encoder->possible_clones =
783 psb_intel_connector_clones(dev, clone_mask);
784 }
785}
786
787void psb_modeset_init(struct drm_device *dev)
788{
789 struct drm_psb_private *dev_priv = dev->dev_private;
790 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
791 int i;
792
793 drm_mode_config_init(dev);
794
795 dev->mode_config.min_width = 0;
796 dev->mode_config.min_height = 0;
797
798 dev->mode_config.funcs = (void *) &psb_mode_funcs;
799
800 /* set memory base */
Alan Coxdffc9ce2011-11-29 22:19:47 +0000801 /* Oaktrail and Poulsbo should use BAR 2*/
Alan Cox4d8d0962011-11-03 18:21:20 +0000802 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
803 &(dev->mode_config.fb_base));
804
805 /* num pipes is 2 for PSB but 1 for Mrst */
806 for (i = 0; i < dev_priv->num_pipe; i++)
807 psb_intel_crtc_init(dev, i, mode_dev);
808
809 dev->mode_config.max_width = 2048;
810 dev->mode_config.max_height = 2048;
811
812 psb_setup_outputs(dev);
813}
814
815void psb_modeset_cleanup(struct drm_device *dev)
816{
817 mutex_lock(&dev->struct_mutex);
818
819 drm_kms_helper_poll_fini(dev);
820 psb_fbdev_fini(dev);
821 drm_mode_config_cleanup(dev);
822
823 mutex_unlock(&dev->struct_mutex);
824}