blob: 94b3fec22c280475dfe1f08d43cf00ed1ca571d2 [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;
Patrik Jakobsson61bb3fe2013-05-14 14:37:10 +0200124 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
125 psbfb->gtt->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000126
127 page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
Yoichi Yuasa1278f7d2012-03-15 14:50:16 +0000128 address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
Alan Cox4d8d0962011-11-03 18:21:20 +0000129
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
Laurent Pinchart78b68552012-05-17 13:27:22 +0200156static const struct vm_operations_struct psbfb_vm_ops = {
Alan Cox4d8d0962011-11-03 18:21:20 +0000157 .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;
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -0700181 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
Alan Cox4d8d0962011-11-03 18:21:20 +0000182 return 0;
183}
184
185static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
186 unsigned long arg)
187{
188 return -ENOTTY;
189}
190
191static struct fb_ops psbfb_ops = {
192 .owner = THIS_MODULE,
193 .fb_check_var = drm_fb_helper_check_var,
194 .fb_set_par = drm_fb_helper_set_par,
195 .fb_blank = drm_fb_helper_blank,
196 .fb_setcolreg = psbfb_setcolreg,
197 .fb_fillrect = cfb_fillrect,
198 .fb_copyarea = psbfb_copyarea,
199 .fb_imageblit = cfb_imageblit,
200 .fb_mmap = psbfb_mmap,
201 .fb_sync = psbfb_sync,
202 .fb_ioctl = psbfb_ioctl,
203};
204
Alan Coxa6ba5822011-11-29 22:27:22 +0000205static struct fb_ops psbfb_roll_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 = cfb_copyarea,
213 .fb_imageblit = cfb_imageblit,
214 .fb_pan_display = psbfb_pan,
215 .fb_mmap = psbfb_mmap,
Alan Coxa6ba5822011-11-29 22:27:22 +0000216 .fb_ioctl = psbfb_ioctl,
217};
218
Alan Cox4d8d0962011-11-03 18:21:20 +0000219static 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 */
242static int psb_framebuffer_init(struct drm_device *dev,
243 struct psb_framebuffer *fb,
Dave Airliea9a644a2011-11-28 14:08:46 +0000244 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000245 struct gtt_range *gt)
246{
Dave Airliea9a644a2011-11-28 14:08:46 +0000247 u32 bpp, depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000248 int ret;
249
Dave Airlie248dbc22011-11-29 20:02:54 +0000250 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
Dave Airliea9a644a2011-11-28 14:08:46 +0000251
252 if (mode_cmd->pitches[0] & 63)
Alan Cox4d8d0962011-11-03 18:21:20 +0000253 return -EINVAL;
Dave Airliea9a644a2011-11-28 14:08:46 +0000254 switch (bpp) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000255 case 8:
256 case 16:
257 case 24:
258 case 32:
259 break;
260 default:
261 return -EINVAL;
262 }
Daniel Vetterc7d73f62012-12-13 23:38:38 +0100263 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
264 fb->gtt = gt;
Alan Cox4d8d0962011-11-03 18:21:20 +0000265 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 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000270 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
285static struct drm_framebuffer *psb_framebuffer_create
286 (struct drm_device *dev,
Dave Airliea9a644a2011-11-28 14:08:46 +0000287 struct drm_mode_fb_cmd2 *mode_cmd,
Alan Cox4d8d0962011-11-03 18:21:20 +0000288 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
Alan Coxa6ba5822011-11-29 22:27:22 +0000309 * @force: fall back to GEM buffers if need be
Alan Cox4d8d0962011-11-03 18:21:20 +0000310 *
311 * Allocate the frame buffer. In the usual case we get a GTT range that
312 * is stolen memory backed and life is simple. If there isn't sufficient
Alan Coxdffc9ce2011-11-29 22:19:47 +0000313 * we fail as we don't have the virtual mapping space to really vmap it
314 * and the kernel console code can't handle non linear framebuffers.
Alan Cox4d8d0962011-11-03 18:21:20 +0000315 *
Alan Coxdffc9ce2011-11-29 22:19:47 +0000316 * Re-address this as and if the framebuffer layer grows this ability.
Alan Cox4d8d0962011-11-03 18:21:20 +0000317 */
318static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
319{
320 struct gtt_range *backing;
321 /* Begin by trying to use stolen memory backing */
322 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1);
323 if (backing) {
David Herrmann89c82332013-07-11 11:56:32 +0200324 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
325 return backing;
Alan Cox4d8d0962011-11-03 18:21:20 +0000326 }
Alan Coxdffc9ce2011-11-29 22:19:47 +0000327 return NULL;
Alan Cox4d8d0962011-11-03 18:21:20 +0000328}
329
330/**
331 * psbfb_create - create a framebuffer
332 * @fbdev: the framebuffer device
333 * @sizes: specification of the layout
334 *
335 * Create a framebuffer to the specifications provided
336 */
337static int psbfb_create(struct psb_fbdev *fbdev,
338 struct drm_fb_helper_surface_size *sizes)
339{
340 struct drm_device *dev = fbdev->psb_fb_helper.dev;
341 struct drm_psb_private *dev_priv = dev->dev_private;
342 struct fb_info *info;
343 struct drm_framebuffer *fb;
344 struct psb_framebuffer *psbfb = &fbdev->pfb;
Dave Airliea9a644a2011-11-28 14:08:46 +0000345 struct drm_mode_fb_cmd2 mode_cmd;
Alan Cox4d8d0962011-11-03 18:21:20 +0000346 struct device *device = &dev->pdev->dev;
347 int size;
348 int ret;
349 struct gtt_range *backing;
Dave Airliea9a644a2011-11-28 14:08:46 +0000350 u32 bpp, depth;
Alan Cox1b223c92011-11-29 22:27:34 +0000351 int gtt_roll = 0;
352 int pitch_lines = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000353
354 mode_cmd.width = sizes->surface_width;
355 mode_cmd.height = sizes->surface_height;
Dave Airliea9a644a2011-11-28 14:08:46 +0000356 bpp = sizes->surface_bpp;
Kirill A. Shutemov6aa1ead2012-03-08 16:02:36 +0000357 depth = sizes->surface_depth;
Alan Cox4d8d0962011-11-03 18:21:20 +0000358
359 /* No 24bit packed */
Dave Airliea9a644a2011-11-28 14:08:46 +0000360 if (bpp == 24)
361 bpp = 32;
Alan Cox4d8d0962011-11-03 18:21:20 +0000362
Alan Cox1b223c92011-11-29 22:27:34 +0000363 do {
364 /*
365 * Acceleration via the GTT requires pitch to be
366 * power of two aligned. Preferably page but less
367 * is ok with some fonts
368 */
369 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
Alan Cox4d8d0962011-11-03 18:21:20 +0000370
Alan Cox1b223c92011-11-29 22:27:34 +0000371 size = mode_cmd.pitches[0] * mode_cmd.height;
372 size = ALIGN(size, PAGE_SIZE);
Alan Cox4d8d0962011-11-03 18:21:20 +0000373
Alan Cox1b223c92011-11-29 22:27:34 +0000374 /* Allocate the fb in the GTT with stolen page backing */
375 backing = psbfb_alloc(dev, size);
376
377 if (pitch_lines)
378 pitch_lines *= 2;
379 else
380 pitch_lines = 1;
381 gtt_roll++;
382 } while (backing == NULL && pitch_lines <= 16);
383
384 /* The final pitch we accepted if we succeeded */
385 pitch_lines /= 2;
386
Alan Coxa6ba5822011-11-29 22:27:22 +0000387 if (backing == NULL) {
388 /*
389 * We couldn't get the space we wanted, fall back to the
390 * display engine requirement instead. The HW requires
391 * the pitch to be 64 byte aligned
Alan Coxa6ba5822011-11-29 22:27:22 +0000392 */
393
394 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
Alan Cox1b223c92011-11-29 22:27:34 +0000395 pitch_lines = 64;
Alan Coxa6ba5822011-11-29 22:27:22 +0000396
397 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
398
399 size = mode_cmd.pitches[0] * mode_cmd.height;
400 size = ALIGN(size, PAGE_SIZE);
401
402 /* Allocate the framebuffer in the GTT with stolen page backing */
403 backing = psbfb_alloc(dev, size);
404 if (backing == NULL)
405 return -ENOMEM;
406 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000407
Alan Coxbb849772012-05-03 15:05:53 +0100408 memset(dev_priv->vram_addr + backing->offset, 0, size);
409
Alan Cox4d8d0962011-11-03 18:21:20 +0000410 mutex_lock(&dev->struct_mutex);
411
412 info = framebuffer_alloc(0, device);
413 if (!info) {
414 ret = -ENOMEM;
415 goto out_err1;
416 }
417 info->par = fbdev;
418
Dave Airliea9a644a2011-11-28 14:08:46 +0000419 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
420
Alan Cox4d8d0962011-11-03 18:21:20 +0000421 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
422 if (ret)
423 goto out_unref;
424
425 fb = &psbfb->base;
426 psbfb->fbdev = info;
427
428 fbdev->psb_fb_helper.fb = fb;
429 fbdev->psb_fb_helper.fbdev = info;
430
Alan Cox45782402012-03-08 16:01:39 +0000431 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
Patrik Jakobssonf9d81492013-04-03 23:52:04 +0200432 strcpy(info->fix.id, "psbdrmfb");
Alan Cox4d8d0962011-11-03 18:21:20 +0000433
434 info->flags = FBINFO_DEFAULT;
Alan Cox1b223c92011-11-29 22:27:34 +0000435 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
436 info->fbops = &psbfb_ops;
437 else if (gtt_roll) { /* GTT rolling seems best */
Alan Coxa6ba5822011-11-29 22:27:22 +0000438 info->fbops = &psbfb_roll_ops;
439 info->flags |= FBINFO_HWACCEL_YPAN;
Alan Cox1b223c92011-11-29 22:27:34 +0000440 } else /* Software */
Alan Coxa6ba5822011-11-29 22:27:22 +0000441 info->fbops = &psbfb_unaccel_ops;
Alan Cox4d8d0962011-11-03 18:21:20 +0000442
443 ret = fb_alloc_cmap(&info->cmap, 256, 0);
444 if (ret) {
445 ret = -ENOMEM;
446 goto out_unref;
447 }
448
449 info->fix.smem_start = dev->mode_config.fb_base;
450 info->fix.smem_len = size;
Alan Coxa6ba5822011-11-29 22:27:22 +0000451 info->fix.ywrapstep = gtt_roll;
452 info->fix.ypanstep = 0;
Alan Cox4d8d0962011-11-03 18:21:20 +0000453
Alan Coxdffc9ce2011-11-29 22:19:47 +0000454 /* Accessed stolen memory directly */
Kirill A. Shutemov37214ca2012-05-03 15:08:26 +0100455 info->screen_base = dev_priv->vram_addr + backing->offset;
Alan Cox4d8d0962011-11-03 18:21:20 +0000456 info->screen_size = size;
457
458 if (dev_priv->gtt.stolen_size) {
459 info->apertures = alloc_apertures(1);
460 if (!info->apertures) {
461 ret = -ENOMEM;
462 goto out_unref;
463 }
464 info->apertures->ranges[0].base = dev->mode_config.fb_base;
465 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
466 }
467
Alan Cox4d8d0962011-11-03 18:21:20 +0000468 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
469 sizes->fb_width, sizes->fb_height);
470
471 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
472 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
473
Sascha Hauerfb2a99e2012-02-06 10:58:19 +0100474 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
Alan Cox4d8d0962011-11-03 18:21:20 +0000475
Alan Cox31a06852012-05-11 11:32:31 +0100476 dev_dbg(dev->dev, "allocated %dx%d fb\n",
Alan Cox4d8d0962011-11-03 18:21:20 +0000477 psbfb->base.width, psbfb->base.height);
478
479 mutex_unlock(&dev->struct_mutex);
480 return 0;
481out_unref:
482 if (backing->stolen)
483 psb_gtt_free_range(dev, backing);
Alan Coxdffc9ce2011-11-29 22:19:47 +0000484 else
Alan Cox4d8d0962011-11-03 18:21:20 +0000485 drm_gem_object_unreference(&backing->gem);
Alan Cox4d8d0962011-11-03 18:21:20 +0000486out_err1:
487 mutex_unlock(&dev->struct_mutex);
488 psb_gtt_free_range(dev, backing);
489 return ret;
490}
491
492/**
493 * psb_user_framebuffer_create - create framebuffer
494 * @dev: our DRM device
495 * @filp: client file
496 * @cmd: mode request
497 *
498 * Create a new framebuffer backed by a userspace GEM object
499 */
500static struct drm_framebuffer *psb_user_framebuffer_create
501 (struct drm_device *dev, struct drm_file *filp,
Dave Airliea9a644a2011-11-28 14:08:46 +0000502 struct drm_mode_fb_cmd2 *cmd)
Alan Cox4d8d0962011-11-03 18:21:20 +0000503{
504 struct gtt_range *r;
505 struct drm_gem_object *obj;
506
507 /*
508 * Find the GEM object and thus the gtt range object that is
509 * to back this space
510 */
Dave Airliea9a644a2011-11-28 14:08:46 +0000511 obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
Alan Cox4d8d0962011-11-03 18:21:20 +0000512 if (obj == NULL)
513 return ERR_PTR(-ENOENT);
514
515 /* Let the core code do all the work */
516 r = container_of(obj, struct gtt_range, gem);
517 return psb_framebuffer_create(dev, cmd, r);
518}
519
520static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
521 u16 blue, int regno)
522{
Patrik Jakobsson63068652013-07-22 01:31:23 +0200523 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
Alan Cox3df546b2012-03-08 16:00:00 +0000524
Patrik Jakobsson63068652013-07-22 01:31:23 +0200525 gma_crtc->lut_r[regno] = red >> 8;
526 gma_crtc->lut_g[regno] = green >> 8;
527 gma_crtc->lut_b[regno] = blue >> 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000528}
529
530static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
531 u16 *green, u16 *blue, int regno)
532{
Patrik Jakobsson63068652013-07-22 01:31:23 +0200533 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
Alan Cox3df546b2012-03-08 16:00:00 +0000534
Patrik Jakobsson63068652013-07-22 01:31:23 +0200535 *red = gma_crtc->lut_r[regno] << 8;
536 *green = gma_crtc->lut_g[regno] << 8;
537 *blue = gma_crtc->lut_b[regno] << 8;
Alan Cox4d8d0962011-11-03 18:21:20 +0000538}
539
540static int psbfb_probe(struct drm_fb_helper *helper,
541 struct drm_fb_helper_surface_size *sizes)
542{
543 struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper;
Alan Cox3aad16d2012-04-25 14:37:40 +0100544 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
545 struct drm_psb_private *dev_priv = dev->dev_private;
Alan Cox3aad16d2012-04-25 14:37:40 +0100546 int bytespp;
Alan Cox4d8d0962011-11-03 18:21:20 +0000547
Alan Cox3aad16d2012-04-25 14:37:40 +0100548 bytespp = sizes->surface_bpp / 8;
549 if (bytespp == 3) /* no 24bit packed */
550 bytespp = 4;
551
552 /* If the mode will not fit in 32bit then switch to 16bit to get
553 a console on full resolution. The X mode setting server will
554 allocate its own 32bit GEM framebuffer */
555 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
556 dev_priv->vram_stolen_size) {
557 sizes->surface_bpp = 16;
558 sizes->surface_depth = 16;
559 }
560
Daniel Vettercd5428a2013-01-21 23:42:49 +0100561 return psbfb_create(psb_fbdev, sizes);
Alan Cox4d8d0962011-11-03 18:21:20 +0000562}
563
Kirill A. Shutemov232e6682012-05-03 15:08:38 +0100564static struct drm_fb_helper_funcs psb_fb_helper_funcs = {
Alan Cox4d8d0962011-11-03 18:21:20 +0000565 .gamma_set = psbfb_gamma_set,
566 .gamma_get = psbfb_gamma_get,
567 .fb_probe = psbfb_probe,
568};
569
Kirill A. Shutemovbc7f2b02012-03-08 16:03:44 +0000570static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
Alan Cox4d8d0962011-11-03 18:21:20 +0000571{
572 struct fb_info *info;
573 struct psb_framebuffer *psbfb = &fbdev->pfb;
574
575 if (fbdev->psb_fb_helper.fbdev) {
576 info = fbdev->psb_fb_helper.fbdev;
Alan Cox4d8d0962011-11-03 18:21:20 +0000577 unregister_framebuffer(info);
578 if (info->cmap.len)
579 fb_dealloc_cmap(&info->cmap);
580 framebuffer_release(info);
581 }
582 drm_fb_helper_fini(&fbdev->psb_fb_helper);
Daniel Vetter36206362012-12-10 20:42:17 +0100583 drm_framebuffer_unregister_private(&psbfb->base);
Alan Cox4d8d0962011-11-03 18:21:20 +0000584 drm_framebuffer_cleanup(&psbfb->base);
585
586 if (psbfb->gtt)
587 drm_gem_object_unreference(&psbfb->gtt->gem);
588 return 0;
589}
590
591int psb_fbdev_init(struct drm_device *dev)
592{
593 struct psb_fbdev *fbdev;
594 struct drm_psb_private *dev_priv = dev->dev_private;
595
596 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
597 if (!fbdev) {
598 dev_err(dev->dev, "no memory\n");
599 return -ENOMEM;
600 }
601
602 dev_priv->fbdev = fbdev;
603 fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs;
604
605 drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs,
606 INTELFB_CONN_LIMIT);
607
608 drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
Daniel Vetter76a39db2013-01-20 23:12:54 +0100609
610 /* disable all the possible outputs/crtcs before entering KMS mode */
611 drm_helper_disable_unused_functions(dev);
612
Alan Cox4d8d0962011-11-03 18:21:20 +0000613 drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
614 return 0;
615}
616
Kirill A. Shutemovbc7f2b02012-03-08 16:03:44 +0000617static void psb_fbdev_fini(struct drm_device *dev)
Alan Cox4d8d0962011-11-03 18:21:20 +0000618{
619 struct drm_psb_private *dev_priv = dev->dev_private;
620
621 if (!dev_priv->fbdev)
622 return;
623
624 psb_fbdev_destroy(dev, dev_priv->fbdev);
625 kfree(dev_priv->fbdev);
626 dev_priv->fbdev = NULL;
627}
628
629static void psbfb_output_poll_changed(struct drm_device *dev)
630{
631 struct drm_psb_private *dev_priv = dev->dev_private;
632 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
633 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
634}
635
636/**
637 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
638 * @fb: framebuffer
639 * @file_priv: our DRM file
640 * @handle: returned handle
641 *
642 * Our framebuffer object is a GTT range which also contains a GEM
643 * object. We need to turn it into a handle for userspace. GEM will do
644 * the work for us
645 */
646static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
647 struct drm_file *file_priv,
648 unsigned int *handle)
649{
650 struct psb_framebuffer *psbfb = to_psb_fb(fb);
651 struct gtt_range *r = psbfb->gtt;
652 return drm_gem_handle_create(file_priv, &r->gem, handle);
653}
654
655/**
656 * psb_user_framebuffer_destroy - destruct user created fb
657 * @fb: framebuffer
658 *
659 * User framebuffers are backed by GEM objects so all we have to do is
660 * clean up a bit and drop the reference, GEM will handle the fallout
661 */
662static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
663{
664 struct psb_framebuffer *psbfb = to_psb_fb(fb);
665 struct gtt_range *r = psbfb->gtt;
Alan Cox4d8d0962011-11-03 18:21:20 +0000666
667 /* Let DRM do its clean up */
668 drm_framebuffer_cleanup(fb);
669 /* We are no longer using the resource in GEM */
670 drm_gem_object_unreference_unlocked(&r->gem);
671 kfree(fb);
672}
673
674static const struct drm_mode_config_funcs psb_mode_funcs = {
675 .fb_create = psb_user_framebuffer_create,
676 .output_poll_changed = psbfb_output_poll_changed,
677};
678
679static int psb_create_backlight_property(struct drm_device *dev)
680{
681 struct drm_psb_private *dev_priv = dev->dev_private;
682 struct drm_property *backlight;
683
684 if (dev_priv->backlight_property)
685 return 0;
686
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100687 backlight = drm_property_create_range(dev, 0, "backlight", 0, 100);
Alan Cox4d8d0962011-11-03 18:21:20 +0000688
689 dev_priv->backlight_property = backlight;
690
691 return 0;
692}
693
694static void psb_setup_outputs(struct drm_device *dev)
695{
696 struct drm_psb_private *dev_priv = dev->dev_private;
697 struct drm_connector *connector;
698
699 drm_mode_create_scaling_mode_property(dev);
700 psb_create_backlight_property(dev);
701
702 dev_priv->ops->output_init(dev);
703
704 list_for_each_entry(connector, &dev->mode_config.connector_list,
705 head) {
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200706 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
707 struct drm_encoder *encoder = &gma_encoder->base;
Alan Cox4d8d0962011-11-03 18:21:20 +0000708 int crtc_mask = 0, clone_mask = 0;
709
710 /* valid crtcs */
Patrik Jakobsson367e4402013-07-22 17:45:26 +0200711 switch (gma_encoder->type) {
Alan Cox4d8d0962011-11-03 18:21:20 +0000712 case INTEL_OUTPUT_ANALOG:
713 crtc_mask = (1 << 0);
714 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
715 break;
716 case INTEL_OUTPUT_SDVO:
Patrik Jakobssoncf8efd32013-09-16 17:54:54 +0200717 crtc_mask = dev_priv->ops->sdvo_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000718 clone_mask = (1 << INTEL_OUTPUT_SDVO);
719 break;
720 case INTEL_OUTPUT_LVDS:
Alan Coxd235e642012-04-25 14:38:07 +0100721 crtc_mask = dev_priv->ops->lvds_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000722 clone_mask = (1 << INTEL_OUTPUT_LVDS);
723 break;
724 case INTEL_OUTPUT_MIPI:
725 crtc_mask = (1 << 0);
726 clone_mask = (1 << INTEL_OUTPUT_MIPI);
727 break;
728 case INTEL_OUTPUT_MIPI2:
729 crtc_mask = (1 << 2);
730 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
731 break;
732 case INTEL_OUTPUT_HDMI:
Alan Coxd235e642012-04-25 14:38:07 +0100733 crtc_mask = dev_priv->ops->hdmi_mask;
Alan Cox4d8d0962011-11-03 18:21:20 +0000734 clone_mask = (1 << INTEL_OUTPUT_HDMI);
735 break;
Alan Cox220801b2012-08-08 13:54:41 +0000736 case INTEL_OUTPUT_DISPLAYPORT:
737 crtc_mask = (1 << 0) | (1 << 1);
738 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
739 break;
Zhao Yakuid112a812012-08-08 13:55:55 +0000740 case INTEL_OUTPUT_EDP:
741 crtc_mask = (1 << 1);
742 clone_mask = (1 << INTEL_OUTPUT_EDP);
Alan Cox4d8d0962011-11-03 18:21:20 +0000743 }
744 encoder->possible_crtcs = crtc_mask;
745 encoder->possible_clones =
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200746 gma_connector_clones(dev, clone_mask);
Alan Cox4d8d0962011-11-03 18:21:20 +0000747 }
748}
749
750void psb_modeset_init(struct drm_device *dev)
751{
752 struct drm_psb_private *dev_priv = dev->dev_private;
753 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
754 int i;
755
756 drm_mode_config_init(dev);
757
758 dev->mode_config.min_width = 0;
759 dev->mode_config.min_height = 0;
760
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200761 dev->mode_config.funcs = &psb_mode_funcs;
Alan Cox4d8d0962011-11-03 18:21:20 +0000762
763 /* set memory base */
Alan Coxdffc9ce2011-11-29 22:19:47 +0000764 /* Oaktrail and Poulsbo should use BAR 2*/
Alan Cox4d8d0962011-11-03 18:21:20 +0000765 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
766 &(dev->mode_config.fb_base));
767
768 /* num pipes is 2 for PSB but 1 for Mrst */
769 for (i = 0; i < dev_priv->num_pipe; i++)
770 psb_intel_crtc_init(dev, i, mode_dev);
771
Patrik Jakobssoncbbd3792013-04-25 22:23:36 +0200772 dev->mode_config.max_width = 4096;
773 dev->mode_config.max_height = 4096;
Alan Cox4d8d0962011-11-03 18:21:20 +0000774
775 psb_setup_outputs(dev);
Alan Coxd235e642012-04-25 14:38:07 +0100776
777 if (dev_priv->ops->errata)
778 dev_priv->ops->errata(dev);
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100779
780 dev_priv->modeset = true;
Alan Cox4d8d0962011-11-03 18:21:20 +0000781}
782
783void psb_modeset_cleanup(struct drm_device *dev)
784{
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100785 struct drm_psb_private *dev_priv = dev->dev_private;
786 if (dev_priv->modeset) {
787 mutex_lock(&dev->struct_mutex);
Alan Cox4d8d0962011-11-03 18:21:20 +0000788
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100789 drm_kms_helper_poll_fini(dev);
790 psb_fbdev_fini(dev);
791 drm_mode_config_cleanup(dev);
Alan Cox4d8d0962011-11-03 18:21:20 +0000792
Alan Cox4ab2c7f2012-05-14 12:04:00 +0100793 mutex_unlock(&dev->struct_mutex);
794 }
Alan Cox4d8d0962011-11-03 18:21:20 +0000795}