blob: bfeb96d5cce86fe54f1d5071e15b288a4887f670 [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
Alan Cox4d8d0962011-11-03 18:21:20 +0000114static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
115{
116 struct psb_framebuffer *psbfb = vma->vm_private_data;
117 struct drm_device *dev = psbfb->base.dev;
118 struct drm_psb_private *dev_priv = dev->dev_private;
119 int page_num;
120 int i;
121 unsigned long address;
122 int ret;
123 unsigned long pfn;
124 /* FIXME: assumes fb at stolen base which may not be true */
125 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base;
126
127 page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
128 address = (unsigned long)vmf->virtual_address;
129
130 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
131
132 for (i = 0; i < page_num; i++) {
133 pfn = (phys_addr >> PAGE_SHIFT);
134
135 ret = vm_insert_mixed(vma, address, pfn);
136 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
137 break;
138 else if (unlikely(ret != 0)) {
139 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
140 return ret;
141 }
142 address += PAGE_SIZE;
143 phys_addr += PAGE_SIZE;
144 }
145 return VM_FAULT_NOPAGE;
146}
147
148static void psbfb_vm_open(struct vm_area_struct *vma)
149{
150}
151
152static void psbfb_vm_close(struct vm_area_struct *vma)
153{
154}
155
156static struct vm_operations_struct psbfb_vm_ops = {
157 .fault = psbfb_vm_fault,
158 .open = psbfb_vm_open,
159 .close = psbfb_vm_close
160};
161
162static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
163{
164 struct psb_fbdev *fbdev = info->par;
165 struct psb_framebuffer *psbfb = &fbdev->pfb;
166
167 if (vma->vm_pgoff != 0)
168 return -EINVAL;
169 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
170 return -EINVAL;
171
172 if (!psbfb->addr_space)
173 psbfb->addr_space = vma->vm_file->f_mapping;
174 /*
175 * If this is a GEM object then info->screen_base is the virtual
176 * kernel remapping of the object. FIXME: Review if this is
177 * suitable for our mmap work
178 */
179 vma->vm_ops = &psbfb_vm_ops;
180 vma->vm_private_data = (void *)psbfb;
181 vma->vm_flags |= VM_RESERVED | VM_IO |
182 VM_MIXEDMAP | VM_DONTEXPAND;
183 return 0;
184}
185
186static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
187 unsigned long arg)
188{
189 return -ENOTTY;
190}
191
192static struct fb_ops psbfb_ops = {
193 .owner = THIS_MODULE,
194 .fb_check_var = drm_fb_helper_check_var,
195 .fb_set_par = drm_fb_helper_set_par,
196 .fb_blank = drm_fb_helper_blank,
197 .fb_setcolreg = psbfb_setcolreg,
198 .fb_fillrect = cfb_fillrect,
199 .fb_copyarea = psbfb_copyarea,
200 .fb_imageblit = cfb_imageblit,
201 .fb_mmap = psbfb_mmap,
202 .fb_sync = psbfb_sync,
203 .fb_ioctl = psbfb_ioctl,
204};
205
Alan Coxa6ba5822011-11-29 22:27:22 +0000206static struct fb_ops psbfb_roll_ops = {
207 .owner = THIS_MODULE,
208 .fb_check_var = drm_fb_helper_check_var,
209 .fb_set_par = drm_fb_helper_set_par,
210 .fb_blank = drm_fb_helper_blank,
211 .fb_setcolreg = psbfb_setcolreg,
212 .fb_fillrect = cfb_fillrect,
213 .fb_copyarea = cfb_copyarea,
214 .fb_imageblit = cfb_imageblit,
215 .fb_pan_display = psbfb_pan,
216 .fb_mmap = psbfb_mmap,
217 .fb_sync = psbfb_sync,
218 .fb_ioctl = psbfb_ioctl,
219};
220
Alan Cox4d8d0962011-11-03 18:21:20 +0000221static struct fb_ops psbfb_unaccel_ops = {
222 .owner = THIS_MODULE,
223 .fb_check_var = drm_fb_helper_check_var,
224 .fb_set_par = drm_fb_helper_set_par,
225 .fb_blank = drm_fb_helper_blank,
226 .fb_setcolreg = psbfb_setcolreg,
227 .fb_fillrect = cfb_fillrect,
228 .fb_copyarea = cfb_copyarea,
229 .fb_imageblit = cfb_imageblit,
230 .fb_mmap = psbfb_mmap,
231 .fb_ioctl = psbfb_ioctl,
232};
233
234/**
235 * psb_framebuffer_init - initialize a framebuffer
236 * @dev: our DRM device
237 * @fb: framebuffer to set up
238 * @mode_cmd: mode description
239 * @gt: backing object
240 *
241 * Configure and fill in the boilerplate for our frame buffer. Return
242 * 0 on success or an error code if we fail.
243 */
244static int psb_framebuffer_init(struct drm_device *dev,
245 struct psb_framebuffer *fb,
Dave Airliea9a644a2011-11-28 14:08:46 +0000246 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000247 struct gtt_range *gt)
248{
Dave Airliea9a644a2011-11-28 14:08:46 +0000249 u32 bpp, depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000250 int ret;
251
Dave Airlie248dbc22011-11-29 20:02:54 +0000252 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
Dave Airliea9a644a2011-11-28 14:08:46 +0000253
254 if (mode_cmd->pitches[0] & 63)
Alan Cox4d8d0962011-11-03 18:21:20 +0000255 return -EINVAL;
Dave Airliea9a644a2011-11-28 14:08:46 +0000256 switch (bpp) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000257 case 8:
258 case 16:
259 case 24:
260 case 32:
261 break;
262 default:
263 return -EINVAL;
264 }
265 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
266 if (ret) {
267 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
268 return ret;
269 }
270 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
271 fb->gtt = gt;
272 return 0;
273}
274
275/**
276 * psb_framebuffer_create - create a framebuffer backed by gt
277 * @dev: our DRM device
278 * @mode_cmd: the description of the requested mode
279 * @gt: the backing object
280 *
281 * Create a framebuffer object backed by the gt, and fill in the
282 * boilerplate required
283 *
284 * TODO: review object references
285 */
286
287static struct drm_framebuffer *psb_framebuffer_create
288 (struct drm_device *dev,
Dave Airliea9a644a2011-11-28 14:08:46 +0000289 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000290 struct gtt_range *gt)
291{
292 struct psb_framebuffer *fb;
293 int ret;
294
295 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
296 if (!fb)
297 return ERR_PTR(-ENOMEM);
298
299 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
300 if (ret) {
301 kfree(fb);
302 return ERR_PTR(ret);
303 }
304 return &fb->base;
305}
306
307/**
308 * psbfb_alloc - allocate frame buffer memory
309 * @dev: the DRM device
310 * @aligned_size: space needed
Alan Coxa6ba5822011-11-29 22:27:22 +0000311 * @force: fall back to GEM buffers if need be
Alan Cox4d8d0962011-11-03 18:21:20 +0000312 *
313 * Allocate the frame buffer. In the usual case we get a GTT range that
314 * is stolen memory backed and life is simple. If there isn't sufficient
Alan Coxdffc9ce2011-11-29 22:19:47 +0000315 * we fail as we don't have the virtual mapping space to really vmap it
316 * and the kernel console code can't handle non linear framebuffers.
Alan Cox4d8d0962011-11-03 18:21:20 +0000317 *
Alan Coxdffc9ce2011-11-29 22:19:47 +0000318 * Re-address this as and if the framebuffer layer grows this ability.
Alan Cox4d8d0962011-11-03 18:21:20 +0000319 */
320static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
321{
322 struct gtt_range *backing;
323 /* Begin by trying to use stolen memory backing */
324 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
325 if (backing) {
326 if (drm_gem_private_object_init(dev,
327 &backing->gem, aligned_size) == 0)
328 return backing;
329 psb_gtt_free_range(dev, backing);
330 }
Alan Coxdffc9ce2011-11-29 22:19:47 +0000331 return NULL;
Alan Cox4d8d0962011-11-03 18:21:20 +0000332}
333
334/**
335 * psbfb_create - create a framebuffer
336 * @fbdev: the framebuffer device
337 * @sizes: specification of the layout
338 *
339 * Create a framebuffer to the specifications provided
340 */
341static int psbfb_create(struct psb_fbdev *fbdev,
342 struct drm_fb_helper_surface_size *sizes)
343{
344 struct drm_device *dev = fbdev->psb_fb_helper.dev;
345 struct drm_psb_private *dev_priv = dev->dev_private;
346 struct fb_info *info;
347 struct drm_framebuffer *fb;
348 struct psb_framebuffer *psbfb = &fbdev->pfb;
Dave Airliea9a644a2011-11-28 14:08:46 +0000349 struct drm_mode_fb_cmd2 mode_cmd;
Alan Cox4d8d0962011-11-03 18:21:20 +0000350 struct device *device = &dev->pdev->dev;
351 int size;
352 int ret;
353 struct gtt_range *backing;
Dave Airliea9a644a2011-11-28 14:08:46 +0000354 u32 bpp, depth;
Alan Cox1b223c92011-11-29 22:27:34 +0000355 int gtt_roll = 0;
356 int pitch_lines = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000357
358 mode_cmd.width = sizes->surface_width;
359 mode_cmd.height = sizes->surface_height;
Dave Airliea9a644a2011-11-28 14:08:46 +0000360 bpp = sizes->surface_bpp;
Kirill A. Shutemov6aa1ead2012-03-08 16:02:36 +0000361 depth = sizes->surface_depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000362
363 /* No 24bit packed */
Dave Airliea9a644a2011-11-28 14:08:46 +0000364 if (bpp == 24)
365 bpp = 32;
Alan Cox4d8d0962011-11-03 18:21:20 +0000366
Alan Cox1b223c92011-11-29 22:27:34 +0000367 do {
368 /*
369 * Acceleration via the GTT requires pitch to be
370 * power of two aligned. Preferably page but less
371 * is ok with some fonts
372 */
373 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
Alan Cox4d8d0962011-11-03 18:21:20 +0000374
Alan Cox1b223c92011-11-29 22:27:34 +0000375 size = mode_cmd.pitches[0] * mode_cmd.height;
376 size = ALIGN(size, PAGE_SIZE);
Alan Cox4d8d0962011-11-03 18:21:20 +0000377
Alan Cox1b223c92011-11-29 22:27:34 +0000378 /* Allocate the fb in the GTT with stolen page backing */
379 backing = psbfb_alloc(dev, size);
380
381 if (pitch_lines)
382 pitch_lines *= 2;
383 else
384 pitch_lines = 1;
385 gtt_roll++;
386 } while (backing == NULL && pitch_lines <= 16);
387
388 /* The final pitch we accepted if we succeeded */
389 pitch_lines /= 2;
390
Alan Coxa6ba5822011-11-29 22:27:22 +0000391 if (backing == NULL) {
392 /*
393 * We couldn't get the space we wanted, fall back to the
394 * display engine requirement instead. The HW requires
395 * the pitch to be 64 byte aligned
Alan Coxa6ba5822011-11-29 22:27:22 +0000396 */
397
398 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
Alan Cox1b223c92011-11-29 22:27:34 +0000399 pitch_lines = 64;
Alan Coxa6ba5822011-11-29 22:27:22 +0000400
401 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
402
403 size = mode_cmd.pitches[0] * mode_cmd.height;
404 size = ALIGN(size, PAGE_SIZE);
405
406 /* Allocate the framebuffer in the GTT with stolen page backing */
407 backing = psbfb_alloc(dev, size);
408 if (backing == NULL)
409 return -ENOMEM;
410 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000411
412 mutex_lock(&dev->struct_mutex);
413
414 info = framebuffer_alloc(0, device);
415 if (!info) {
416 ret = -ENOMEM;
417 goto out_err1;
418 }
419 info->par = fbdev;
420
Dave Airliea9a644a2011-11-28 14:08:46 +0000421 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
422
Alan Cox4d8d0962011-11-03 18:21:20 +0000423 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
424 if (ret)
425 goto out_unref;
426
427 fb = &psbfb->base;
428 psbfb->fbdev = info;
429
430 fbdev->psb_fb_helper.fb = fb;
431 fbdev->psb_fb_helper.fbdev = info;
432
Alan Cox45782402012-03-08 16:01:39 +0000433 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Alan Cox4d8d0962011-11-03 18:21:20 +0000434 strcpy(info->fix.id, "psbfb");
435
436 info->flags = FBINFO_DEFAULT;
Alan Cox1b223c92011-11-29 22:27:34 +0000437 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
438 info->fbops = &psbfb_ops;
439 else if (gtt_roll) { /* GTT rolling seems best */
Alan Coxa6ba5822011-11-29 22:27:22 +0000440 info->fbops = &psbfb_roll_ops;
441 info->flags |= FBINFO_HWACCEL_YPAN;
Alan Cox1b223c92011-11-29 22:27:34 +0000442 } else /* Software */
Alan Coxa6ba5822011-11-29 22:27:22 +0000443 info->fbops = &psbfb_unaccel_ops;
Alan Cox4d8d0962011-11-03 18:21:20 +0000444
445 ret = fb_alloc_cmap(&info->cmap, 256, 0);
446 if (ret) {
447 ret = -ENOMEM;
448 goto out_unref;
449 }
450
451 info->fix.smem_start = dev->mode_config.fb_base;
452 info->fix.smem_len = size;
Alan Coxa6ba5822011-11-29 22:27:22 +0000453 info->fix.ywrapstep = gtt_roll;
454 info->fix.ypanstep = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000455
Alan Coxdffc9ce2011-11-29 22:19:47 +0000456 /* Accessed stolen memory directly */
457 info->screen_base = (char *)dev_priv->vram_addr +
Alan Cox4d8d0962011-11-03 18:21:20 +0000458 backing->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000459 info->screen_size = size;
460
461 if (dev_priv->gtt.stolen_size) {
462 info->apertures = alloc_apertures(1);
463 if (!info->apertures) {
464 ret = -ENOMEM;
465 goto out_unref;
466 }
467 info->apertures->ranges[0].base = dev->mode_config.fb_base;
468 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
469 }
470
Alan Cox4d8d0962011-11-03 18:21:20 +0000471 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
472 sizes->fb_width, sizes->fb_height);
473
474 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
475 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
476
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100477 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Alan Cox4d8d0962011-11-03 18:21:20 +0000478
479 dev_info(dev->dev, "allocated %dx%d fb\n",
480 psbfb->base.width, psbfb->base.height);
481
482 mutex_unlock(&dev->struct_mutex);
483 return 0;
484out_unref:
485 if (backing->stolen)
486 psb_gtt_free_range(dev, backing);
Alan Coxdffc9ce2011-11-29 22:19:47 +0000487 else
Alan Cox4d8d0962011-11-03 18:21:20 +0000488 drm_gem_object_unreference(&backing->gem);
Alan Cox4d8d0962011-11-03 18:21:20 +0000489out_err1:
490 mutex_unlock(&dev->struct_mutex);
491 psb_gtt_free_range(dev, backing);
492 return ret;
493}
494
495/**
496 * psb_user_framebuffer_create - create framebuffer
497 * @dev: our DRM device
498 * @filp: client file
499 * @cmd: mode request
500 *
501 * Create a new framebuffer backed by a userspace GEM object
502 */
503static struct drm_framebuffer *psb_user_framebuffer_create
504 (struct drm_device *dev, struct drm_file *filp,
Dave Airliea9a644a2011-11-28 14:08:46 +0000505 struct drm_mode_fb_cmd2 *cmd)
Alan Cox4d8d0962011-11-03 18:21:20 +0000506{
507 struct gtt_range *r;
508 struct drm_gem_object *obj;
509
510 /*
511 * Find the GEM object and thus the gtt range object that is
512 * to back this space
513 */
Dave Airliea9a644a2011-11-28 14:08:46 +0000514 obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
Alan Cox4d8d0962011-11-03 18:21:20 +0000515 if (obj == NULL)
516 return ERR_PTR(-ENOENT);
517
518 /* Let the core code do all the work */
519 r = container_of(obj, struct gtt_range, gem);
520 return psb_framebuffer_create(dev, cmd, r);
521}
522
523static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
524 u16 blue, int regno)
525{
Alan Cox3df546b2012-03-08 16:00:00 +0000526 struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
527
528 intel_crtc->lut_r[regno] = red >> 8;
529 intel_crtc->lut_g[regno] = green >> 8;
530 intel_crtc->lut_b[regno] = blue >> 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000531}
532
533static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
534 u16 *green, u16 *blue, int regno)
535{
Alan Cox3df546b2012-03-08 16:00:00 +0000536 struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
537
538 *red = intel_crtc->lut_r[regno] << 8;
539 *green = intel_crtc->lut_g[regno] << 8;
540 *blue = intel_crtc->lut_b[regno] << 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000541}
542
543static int psbfb_probe(struct drm_fb_helper *helper,
544 struct drm_fb_helper_surface_size *sizes)
545{
546 struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
547 int new_fb = 0;
548 int ret;
549
550 if (!helper->fb) {
551 ret = psbfb_create(psb_fbdev, sizes);
552 if (ret)
553 return ret;
554 new_fb = 1;
555 }
556 return new_fb;
557}
558
559struct drm_fb_helper_funcs psb_fb_helper_funcs = {
560 .gamma_set = psbfb_gamma_set,
561 .gamma_get = psbfb_gamma_get,
562 .fb_probe = psbfb_probe,
563};
564
565int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
566{
567 struct fb_info *info;
568 struct psb_framebuffer *psbfb = &fbdev->pfb;
569
570 if (fbdev->psb_fb_helper.fbdev) {
571 info = fbdev->psb_fb_helper.fbdev;
Alan Cox4d8d0962011-11-03 18:21:20 +0000572 unregister_framebuffer(info);
573 if (info->cmap.len)
574 fb_dealloc_cmap(&info->cmap);
575 framebuffer_release(info);
576 }
577 drm_fb_helper_fini(&fbdev->psb_fb_helper);
578 drm_framebuffer_cleanup(&psbfb->base);
579
580 if (psbfb->gtt)
581 drm_gem_object_unreference(&psbfb->gtt->gem);
582 return 0;
583}
584
585int psb_fbdev_init(struct drm_device *dev)
586{
587 struct psb_fbdev *fbdev;
588 struct drm_psb_private *dev_priv = dev->dev_private;
589
590 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
591 if (!fbdev) {
592 dev_err(dev->dev, "no memory\n");
593 return -ENOMEM;
594 }
595
596 dev_priv->fbdev = fbdev;
597 fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
598
599 drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
600 INTELFB_CONN_LIMIT);
601
602 drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
603 drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
604 return 0;
605}
606
607void psb_fbdev_fini(struct drm_device *dev)
608{
609 struct drm_psb_private *dev_priv = dev->dev_private;
610
611 if (!dev_priv->fbdev)
612 return;
613
614 psb_fbdev_destroy(dev, dev_priv->fbdev);
615 kfree(dev_priv->fbdev);
616 dev_priv->fbdev = NULL;
617}
618
619static void psbfb_output_poll_changed(struct drm_device *dev)
620{
621 struct drm_psb_private *dev_priv = dev->dev_private;
622 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
623 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
624}
625
626/**
627 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
628 * @fb: framebuffer
629 * @file_priv: our DRM file
630 * @handle: returned handle
631 *
632 * Our framebuffer object is a GTT range which also contains a GEM
633 * object. We need to turn it into a handle for userspace. GEM will do
634 * the work for us
635 */
636static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
637 struct drm_file *file_priv,
638 unsigned int *handle)
639{
640 struct psb_framebuffer *psbfb = to_psb_fb(fb);
641 struct gtt_range *r = psbfb->gtt;
642 return drm_gem_handle_create(file_priv, &r->gem, handle);
643}
644
645/**
646 * psb_user_framebuffer_destroy - destruct user created fb
647 * @fb: framebuffer
648 *
649 * User framebuffers are backed by GEM objects so all we have to do is
650 * clean up a bit and drop the reference, GEM will handle the fallout
651 */
652static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
653{
654 struct psb_framebuffer *psbfb = to_psb_fb(fb);
655 struct gtt_range *r = psbfb->gtt;
656 struct drm_device *dev = fb->dev;
657 struct drm_psb_private *dev_priv = dev->dev_private;
658 struct psb_fbdev *fbdev = dev_priv->fbdev;
659 struct drm_crtc *crtc;
660 int reset = 0;
661
662 /* Should never get stolen memory for a user fb */
663 WARN_ON(r->stolen);
664
665 /* Check if we are erroneously live */
666 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
667 if (crtc->fb == fb)
668 reset = 1;
669
670 if (reset)
671 /*
672 * Now force a sane response before we permit the DRM CRTC
673 * layer to do stupid things like blank the display. Instead
674 * we reset this framebuffer as if the user had forced a reset.
675 * We must do this before the cleanup so that the DRM layer
676 * doesn't get a chance to stick its oar in where it isn't
677 * wanted.
678 */
679 drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper);
680
681 /* Let DRM do its clean up */
682 drm_framebuffer_cleanup(fb);
683 /* We are no longer using the resource in GEM */
684 drm_gem_object_unreference_unlocked(&r->gem);
685 kfree(fb);
686}
687
688static const struct drm_mode_config_funcs psb_mode_funcs = {
689 .fb_create = psb_user_framebuffer_create,
690 .output_poll_changed = psbfb_output_poll_changed,
691};
692
693static int psb_create_backlight_property(struct drm_device *dev)
694{
695 struct drm_psb_private *dev_priv = dev->dev_private;
696 struct drm_property *backlight;
697
698 if (dev_priv->backlight_property)
699 return 0;
700
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100701 backlight = drm_property_create_range(dev, 0, "backlight", 0, 100);
Alan Cox4d8d0962011-11-03 18:21:20 +0000702
703 dev_priv->backlight_property = backlight;
704
705 return 0;
706}
707
708static void psb_setup_outputs(struct drm_device *dev)
709{
710 struct drm_psb_private *dev_priv = dev->dev_private;
711 struct drm_connector *connector;
712
713 drm_mode_create_scaling_mode_property(dev);
714 psb_create_backlight_property(dev);
715
716 dev_priv->ops->output_init(dev);
717
718 list_for_each_entry(connector, &dev->mode_config.connector_list,
719 head) {
Patrik Jakobsson1730f892011-12-19 21:40:33 +0000720 struct psb_intel_encoder *psb_intel_encoder =
721 psb_intel_attached_encoder(connector);
722 struct drm_encoder *encoder = &psb_intel_encoder->base;
Alan Cox4d8d0962011-11-03 18:21:20 +0000723 int crtc_mask = 0, clone_mask = 0;
724
725 /* valid crtcs */
Patrik Jakobsson1730f892011-12-19 21:40:33 +0000726 switch (psb_intel_encoder->type) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000727 case INTEL_OUTPUT_ANALOG:
728 crtc_mask = (1 << 0);
729 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
730 break;
731 case INTEL_OUTPUT_SDVO:
732 crtc_mask = ((1 << 0) | (1 << 1));
733 clone_mask = (1 << INTEL_OUTPUT_SDVO);
734 break;
735 case INTEL_OUTPUT_LVDS:
736 if (IS_MRST(dev))
737 crtc_mask = (1 << 0);
738 else
739 crtc_mask = (1 << 1);
740 clone_mask = (1 << INTEL_OUTPUT_LVDS);
741 break;
742 case INTEL_OUTPUT_MIPI:
743 crtc_mask = (1 << 0);
744 clone_mask = (1 << INTEL_OUTPUT_MIPI);
745 break;
746 case INTEL_OUTPUT_MIPI2:
747 crtc_mask = (1 << 2);
748 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
749 break;
750 case INTEL_OUTPUT_HDMI:
751 if (IS_MFLD(dev))
752 crtc_mask = (1 << 1);
753 else
754 crtc_mask = (1 << 0);
755 clone_mask = (1 << INTEL_OUTPUT_HDMI);
756 break;
757 }
758 encoder->possible_crtcs = crtc_mask;
759 encoder->possible_clones =
760 psb_intel_connector_clones(dev, clone_mask);
761 }
762}
763
764void psb_modeset_init(struct drm_device *dev)
765{
766 struct drm_psb_private *dev_priv = dev->dev_private;
767 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
768 int i;
769
770 drm_mode_config_init(dev);
771
772 dev->mode_config.min_width = 0;
773 dev->mode_config.min_height = 0;
774
775 dev->mode_config.funcs = (void *) &psb_mode_funcs;
776
777 /* set memory base */
Alan Coxdffc9ce2011-11-29 22:19:47 +0000778 /* Oaktrail and Poulsbo should use BAR 2*/
Alan Cox4d8d0962011-11-03 18:21:20 +0000779 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
780 &(dev->mode_config.fb_base));
781
782 /* num pipes is 2 for PSB but 1 for Mrst */
783 for (i = 0; i < dev_priv->num_pipe; i++)
784 psb_intel_crtc_init(dev, i, mode_dev);
785
786 dev->mode_config.max_width = 2048;
787 dev->mode_config.max_height = 2048;
788
789 psb_setup_outputs(dev);
790}
791
792void psb_modeset_cleanup(struct drm_device *dev)
793{
794 mutex_lock(&dev->struct_mutex);
795
796 drm_kms_helper_poll_fini(dev);
797 psb_fbdev_fini(dev);
798 drm_mode_config_cleanup(dev);
799
800 mutex_unlock(&dev->struct_mutex);
801}