]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/gma500/framebuffer.c
Merge tag 'drm-misc-next-2017-03-06' of git://anongit.freedesktop.org/git/drm-misc...
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / gma500 / framebuffer.c
CommitLineData
4d8d096e
AC
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>
01c8f1c4 24#include <linux/pfn_t.h>
4d8d096e
AC
25#include <linux/mm.h>
26#include <linux/tty.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
4d8d096e
AC
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>
a9a644ac 35#include <drm/drm_fb_helper.h>
4d8d096e
AC
36
37#include "psb_drv.h"
38#include "psb_intel_reg.h"
39#include "psb_intel_drv.h"
40#include "framebuffer.h"
a6ba582d 41#include "gtt.h"
4d8d096e
AC
42
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) {
272725c7 80 switch (fb->format->cpp[0] * 8) {
4d8d096e
AC
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
a6ba582d
AC
94static 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}
4d8d096e 113
11bac800 114static int psbfb_vm_fault(struct vm_fault *vmf)
4d8d096e 115{
11bac800 116 struct vm_area_struct *vma = vmf->vma;
4d8d096e
AC
117 struct psb_framebuffer *psbfb = vma->vm_private_data;
118 struct drm_device *dev = psbfb->base.dev;
119 struct drm_psb_private *dev_priv = dev->dev_private;
120 int page_num;
121 int i;
122 unsigned long address;
123 int ret;
124 unsigned long pfn;
61bb3fea
PJ
125 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
126 psbfb->gtt->offset;
4d8d096e 127
024b6a63 128 page_num = vma_pages(vma);
1a29d85e 129 address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
4d8d096e
AC
130
131 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
132
133 for (i = 0; i < page_num; i++) {
134 pfn = (phys_addr >> PAGE_SHIFT);
135
01c8f1c4
DW
136 ret = vm_insert_mixed(vma, address,
137 __pfn_to_pfn_t(pfn, PFN_DEV));
4d8d096e
AC
138 if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
139 break;
140 else if (unlikely(ret != 0)) {
141 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
142 return ret;
143 }
144 address += PAGE_SIZE;
145 phys_addr += PAGE_SIZE;
146 }
147 return VM_FAULT_NOPAGE;
148}
149
150static void psbfb_vm_open(struct vm_area_struct *vma)
151{
152}
153
154static void psbfb_vm_close(struct vm_area_struct *vma)
155{
156}
157
78b68556 158static const struct vm_operations_struct psbfb_vm_ops = {
4d8d096e
AC
159 .fault = psbfb_vm_fault,
160 .open = psbfb_vm_open,
161 .close = psbfb_vm_close
162};
163
164static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
165{
166 struct psb_fbdev *fbdev = info->par;
167 struct psb_framebuffer *psbfb = &fbdev->pfb;
168
169 if (vma->vm_pgoff != 0)
170 return -EINVAL;
171 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
172 return -EINVAL;
173
174 if (!psbfb->addr_space)
175 psbfb->addr_space = vma->vm_file->f_mapping;
176 /*
177 * If this is a GEM object then info->screen_base is the virtual
178 * kernel remapping of the object. FIXME: Review if this is
179 * suitable for our mmap work
180 */
181 vma->vm_ops = &psbfb_vm_ops;
182 vma->vm_private_data = (void *)psbfb;
314e51b9 183 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
4d8d096e
AC
184 return 0;
185}
186
4d8d096e
AC
187static struct fb_ops psbfb_ops = {
188 .owner = THIS_MODULE,
3da6c2f3 189 DRM_FB_HELPER_DEFAULT_OPS,
4d8d096e 190 .fb_setcolreg = psbfb_setcolreg,
546187c8 191 .fb_fillrect = drm_fb_helper_cfb_fillrect,
4d8d096e 192 .fb_copyarea = psbfb_copyarea,
546187c8 193 .fb_imageblit = drm_fb_helper_cfb_imageblit,
4d8d096e
AC
194 .fb_mmap = psbfb_mmap,
195 .fb_sync = psbfb_sync,
4d8d096e
AC
196};
197
a6ba582d
AC
198static struct fb_ops psbfb_roll_ops = {
199 .owner = THIS_MODULE,
3da6c2f3 200 DRM_FB_HELPER_DEFAULT_OPS,
a6ba582d 201 .fb_setcolreg = psbfb_setcolreg,
546187c8
AT
202 .fb_fillrect = drm_fb_helper_cfb_fillrect,
203 .fb_copyarea = drm_fb_helper_cfb_copyarea,
204 .fb_imageblit = drm_fb_helper_cfb_imageblit,
a6ba582d
AC
205 .fb_pan_display = psbfb_pan,
206 .fb_mmap = psbfb_mmap,
a6ba582d
AC
207};
208
4d8d096e
AC
209static struct fb_ops psbfb_unaccel_ops = {
210 .owner = THIS_MODULE,
3da6c2f3 211 DRM_FB_HELPER_DEFAULT_OPS,
4d8d096e 212 .fb_setcolreg = psbfb_setcolreg,
546187c8
AT
213 .fb_fillrect = drm_fb_helper_cfb_fillrect,
214 .fb_copyarea = drm_fb_helper_cfb_copyarea,
215 .fb_imageblit = drm_fb_helper_cfb_imageblit,
4d8d096e 216 .fb_mmap = psbfb_mmap,
4d8d096e
AC
217};
218
219/**
220 * psb_framebuffer_init - initialize a framebuffer
221 * @dev: our DRM device
222 * @fb: framebuffer to set up
223 * @mode_cmd: mode description
224 * @gt: backing object
225 *
226 * Configure and fill in the boilerplate for our frame buffer. Return
227 * 0 on success or an error code if we fail.
228 */
229static int psb_framebuffer_init(struct drm_device *dev,
230 struct psb_framebuffer *fb,
1eb83451 231 const struct drm_mode_fb_cmd2 *mode_cmd,
4d8d096e
AC
232 struct gtt_range *gt)
233{
e0f9a4ab 234 const struct drm_format_info *info;
4d8d096e
AC
235 int ret;
236
e0f9a4ab
LP
237 /*
238 * Reject unknown formats, YUV formats, and formats with more than
239 * 4 bytes per pixel.
240 */
241 info = drm_format_info(mode_cmd->pixel_format);
242 if (!info || !info->depth || info->cpp[0] > 4)
243 return -EINVAL;
a9a644ac
DA
244
245 if (mode_cmd->pitches[0] & 63)
4d8d096e 246 return -EINVAL;
e0f9a4ab 247
a3f913ca 248 drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
c7d73f6a 249 fb->gtt = gt;
4d8d096e
AC
250 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
251 if (ret) {
252 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
253 return ret;
254 }
4d8d096e
AC
255 return 0;
256}
257
258/**
259 * psb_framebuffer_create - create a framebuffer backed by gt
260 * @dev: our DRM device
261 * @mode_cmd: the description of the requested mode
262 * @gt: the backing object
263 *
264 * Create a framebuffer object backed by the gt, and fill in the
265 * boilerplate required
266 *
267 * TODO: review object references
268 */
269
270static struct drm_framebuffer *psb_framebuffer_create
271 (struct drm_device *dev,
1eb83451 272 const struct drm_mode_fb_cmd2 *mode_cmd,
4d8d096e
AC
273 struct gtt_range *gt)
274{
275 struct psb_framebuffer *fb;
276 int ret;
277
278 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
279 if (!fb)
280 return ERR_PTR(-ENOMEM);
281
282 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
283 if (ret) {
284 kfree(fb);
285 return ERR_PTR(ret);
286 }
287 return &fb->base;
288}
289
290/**
291 * psbfb_alloc - allocate frame buffer memory
292 * @dev: the DRM device
293 * @aligned_size: space needed
294 *
295 * Allocate the frame buffer. In the usual case we get a GTT range that
296 * is stolen memory backed and life is simple. If there isn't sufficient
dffc9ceb
AC
297 * we fail as we don't have the virtual mapping space to really vmap it
298 * and the kernel console code can't handle non linear framebuffers.
4d8d096e 299 *
dffc9ceb 300 * Re-address this as and if the framebuffer layer grows this ability.
4d8d096e
AC
301 */
302static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
303{
304 struct gtt_range *backing;
305 /* Begin by trying to use stolen memory backing */
c269c685 306 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
4d8d096e 307 if (backing) {
89c8233f
DH
308 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
309 return backing;
4d8d096e 310 }
dffc9ceb 311 return NULL;
4d8d096e
AC
312}
313
314/**
315 * psbfb_create - create a framebuffer
316 * @fbdev: the framebuffer device
317 * @sizes: specification of the layout
318 *
319 * Create a framebuffer to the specifications provided
320 */
321static int psbfb_create(struct psb_fbdev *fbdev,
322 struct drm_fb_helper_surface_size *sizes)
323{
324 struct drm_device *dev = fbdev->psb_fb_helper.dev;
325 struct drm_psb_private *dev_priv = dev->dev_private;
326 struct fb_info *info;
327 struct drm_framebuffer *fb;
328 struct psb_framebuffer *psbfb = &fbdev->pfb;
a9a644ac 329 struct drm_mode_fb_cmd2 mode_cmd;
4d8d096e
AC
330 int size;
331 int ret;
332 struct gtt_range *backing;
a9a644ac 333 u32 bpp, depth;
1b223c9e
AC
334 int gtt_roll = 0;
335 int pitch_lines = 0;
4d8d096e
AC
336
337 mode_cmd.width = sizes->surface_width;
338 mode_cmd.height = sizes->surface_height;
a9a644ac 339 bpp = sizes->surface_bpp;
6aa1ead1 340 depth = sizes->surface_depth;
4d8d096e
AC
341
342 /* No 24bit packed */
a9a644ac
DA
343 if (bpp == 24)
344 bpp = 32;
4d8d096e 345
1b223c9e
AC
346 do {
347 /*
348 * Acceleration via the GTT requires pitch to be
349 * power of two aligned. Preferably page but less
350 * is ok with some fonts
351 */
352 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
1b223c9e
AC
353
354 size = mode_cmd.pitches[0] * mode_cmd.height;
355 size = ALIGN(size, PAGE_SIZE);
356
357 /* Allocate the fb in the GTT with stolen page backing */
358 backing = psbfb_alloc(dev, size);
359
360 if (pitch_lines)
361 pitch_lines *= 2;
362 else
363 pitch_lines = 1;
364 gtt_roll++;
365 } while (backing == NULL && pitch_lines <= 16);
4d8d096e 366
1b223c9e
AC
367 /* The final pitch we accepted if we succeeded */
368 pitch_lines /= 2;
4d8d096e 369
a6ba582d
AC
370 if (backing == NULL) {
371 /*
372 * We couldn't get the space we wanted, fall back to the
373 * display engine requirement instead. The HW requires
374 * the pitch to be 64 byte aligned
a6ba582d
AC
375 */
376
377 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
1b223c9e 378 pitch_lines = 64;
a6ba582d
AC
379
380 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
381
382 size = mode_cmd.pitches[0] * mode_cmd.height;
383 size = ALIGN(size, PAGE_SIZE);
384
385 /* Allocate the framebuffer in the GTT with stolen page backing */
386 backing = psbfb_alloc(dev, size);
387 if (backing == NULL)
388 return -ENOMEM;
389 }
4d8d096e 390
bb849779
AC
391 memset(dev_priv->vram_addr + backing->offset, 0, size);
392
546187c8
AT
393 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
394 if (IS_ERR(info)) {
395 ret = PTR_ERR(info);
da7bdda2 396 goto out;
4d8d096e
AC
397 }
398 info->par = fbdev;
399
a9a644ac
DA
400 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
401
4d8d096e
AC
402 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
403 if (ret)
da7bdda2 404 goto out;
4d8d096e
AC
405
406 fb = &psbfb->base;
407 psbfb->fbdev = info;
408
409 fbdev->psb_fb_helper.fb = fb;
4d8d096e 410
b00c600e 411 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
f9d8149a 412 strcpy(info->fix.id, "psbdrmfb");
4d8d096e
AC
413
414 info->flags = FBINFO_DEFAULT;
1b223c9e
AC
415 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
416 info->fbops = &psbfb_ops;
417 else if (gtt_roll) { /* GTT rolling seems best */
a6ba582d
AC
418 info->fbops = &psbfb_roll_ops;
419 info->flags |= FBINFO_HWACCEL_YPAN;
1b223c9e 420 } else /* Software */
a6ba582d 421 info->fbops = &psbfb_unaccel_ops;
4d8d096e 422
4d8d096e
AC
423 info->fix.smem_start = dev->mode_config.fb_base;
424 info->fix.smem_len = size;
a6ba582d
AC
425 info->fix.ywrapstep = gtt_roll;
426 info->fix.ypanstep = 0;
4d8d096e 427
dffc9ceb 428 /* Accessed stolen memory directly */
37214ca0 429 info->screen_base = dev_priv->vram_addr + backing->offset;
4d8d096e
AC
430 info->screen_size = size;
431
432 if (dev_priv->gtt.stolen_size) {
4d8d096e
AC
433 info->apertures->ranges[0].base = dev->mode_config.fb_base;
434 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
435 }
436
4d8d096e
AC
437 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
438 sizes->fb_width, sizes->fb_height);
439
440 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
441 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
442
fb2a99e1 443 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
4d8d096e 444
31a0685a 445 dev_dbg(dev->dev, "allocated %dx%d fb\n",
4d8d096e
AC
446 psbfb->base.width, psbfb->base.height);
447
4d8d096e 448 return 0;
da7bdda2 449out:
4d8d096e
AC
450 psb_gtt_free_range(dev, backing);
451 return ret;
452}
453
454/**
455 * psb_user_framebuffer_create - create framebuffer
456 * @dev: our DRM device
457 * @filp: client file
458 * @cmd: mode request
459 *
460 * Create a new framebuffer backed by a userspace GEM object
461 */
462static struct drm_framebuffer *psb_user_framebuffer_create
463 (struct drm_device *dev, struct drm_file *filp,
1eb83451 464 const struct drm_mode_fb_cmd2 *cmd)
4d8d096e
AC
465{
466 struct gtt_range *r;
467 struct drm_gem_object *obj;
468
469 /*
470 * Find the GEM object and thus the gtt range object that is
471 * to back this space
472 */
a8ad0bd8 473 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
4d8d096e
AC
474 if (obj == NULL)
475 return ERR_PTR(-ENOENT);
476
477 /* Let the core code do all the work */
478 r = container_of(obj, struct gtt_range, gem);
479 return psb_framebuffer_create(dev, cmd, r);
480}
481
482static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
483 u16 blue, int regno)
484{
6306865d 485 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
3df546be 486
6306865d
PJ
487 gma_crtc->lut_r[regno] = red >> 8;
488 gma_crtc->lut_g[regno] = green >> 8;
489 gma_crtc->lut_b[regno] = blue >> 8;
4d8d096e
AC
490}
491
492static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
493 u16 *green, u16 *blue, int regno)
494{
6306865d 495 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
3df546be 496
6306865d
PJ
497 *red = gma_crtc->lut_r[regno] << 8;
498 *green = gma_crtc->lut_g[regno] << 8;
499 *blue = gma_crtc->lut_b[regno] << 8;
4d8d096e
AC
500}
501
502static int psbfb_probe(struct drm_fb_helper *helper,
503 struct drm_fb_helper_surface_size *sizes)
504{
c39aa6a1
FF
505 struct psb_fbdev *psb_fbdev =
506 container_of(helper, struct psb_fbdev, psb_fb_helper);
3aad16d2
AC
507 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
508 struct drm_psb_private *dev_priv = dev->dev_private;
3aad16d2 509 int bytespp;
4d8d096e 510
3aad16d2
AC
511 bytespp = sizes->surface_bpp / 8;
512 if (bytespp == 3) /* no 24bit packed */
513 bytespp = 4;
514
515 /* If the mode will not fit in 32bit then switch to 16bit to get
516 a console on full resolution. The X mode setting server will
517 allocate its own 32bit GEM framebuffer */
518 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
519 dev_priv->vram_stolen_size) {
520 sizes->surface_bpp = 16;
521 sizes->surface_depth = 16;
522 }
523
cd5428a5 524 return psbfb_create(psb_fbdev, sizes);
4d8d096e
AC
525}
526
3a493879 527static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
4d8d096e
AC
528 .gamma_set = psbfb_gamma_set,
529 .gamma_get = psbfb_gamma_get,
530 .fb_probe = psbfb_probe,
531};
532
bc7f2b08 533static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
4d8d096e 534{
4d8d096e
AC
535 struct psb_framebuffer *psbfb = &fbdev->pfb;
536
546187c8 537 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
546187c8 538
4d8d096e 539 drm_fb_helper_fini(&fbdev->psb_fb_helper);
36206361 540 drm_framebuffer_unregister_private(&psbfb->base);
4d8d096e
AC
541 drm_framebuffer_cleanup(&psbfb->base);
542
543 if (psbfb->gtt)
46a0f223 544 drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
4d8d096e
AC
545 return 0;
546}
547
548int psb_fbdev_init(struct drm_device *dev)
549{
550 struct psb_fbdev *fbdev;
551 struct drm_psb_private *dev_priv = dev->dev_private;
01934c2a 552 int ret;
4d8d096e
AC
553
554 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
555 if (!fbdev) {
556 dev_err(dev->dev, "no memory\n");
557 return -ENOMEM;
558 }
559
560 dev_priv->fbdev = fbdev;
10a23102
TR
561
562 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
4d8d096e 563
01934c2a 564 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
e4563f6b 565 INTELFB_CONN_LIMIT);
01934c2a
TR
566 if (ret)
567 goto free;
4d8d096e 568
01934c2a
TR
569 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
570 if (ret)
571 goto fini;
76a39dbf
DV
572
573 /* disable all the possible outputs/crtcs before entering KMS mode */
574 drm_helper_disable_unused_functions(dev);
575
01934c2a
TR
576 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
577 if (ret)
578 goto fini;
579
4d8d096e 580 return 0;
01934c2a
TR
581
582fini:
583 drm_fb_helper_fini(&fbdev->psb_fb_helper);
584free:
585 kfree(fbdev);
586 return ret;
4d8d096e
AC
587}
588
bc7f2b08 589static void psb_fbdev_fini(struct drm_device *dev)
4d8d096e
AC
590{
591 struct drm_psb_private *dev_priv = dev->dev_private;
592
593 if (!dev_priv->fbdev)
594 return;
595
596 psb_fbdev_destroy(dev, dev_priv->fbdev);
597 kfree(dev_priv->fbdev);
598 dev_priv->fbdev = NULL;
599}
600
601static void psbfb_output_poll_changed(struct drm_device *dev)
602{
603 struct drm_psb_private *dev_priv = dev->dev_private;
604 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
605 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
606}
607
608/**
609 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
610 * @fb: framebuffer
611 * @file_priv: our DRM file
612 * @handle: returned handle
613 *
614 * Our framebuffer object is a GTT range which also contains a GEM
615 * object. We need to turn it into a handle for userspace. GEM will do
616 * the work for us
617 */
618static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
619 struct drm_file *file_priv,
620 unsigned int *handle)
621{
622 struct psb_framebuffer *psbfb = to_psb_fb(fb);
623 struct gtt_range *r = psbfb->gtt;
624 return drm_gem_handle_create(file_priv, &r->gem, handle);
625}
626
627/**
628 * psb_user_framebuffer_destroy - destruct user created fb
629 * @fb: framebuffer
630 *
631 * User framebuffers are backed by GEM objects so all we have to do is
632 * clean up a bit and drop the reference, GEM will handle the fallout
633 */
634static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
635{
636 struct psb_framebuffer *psbfb = to_psb_fb(fb);
637 struct gtt_range *r = psbfb->gtt;
4d8d096e
AC
638
639 /* Let DRM do its clean up */
640 drm_framebuffer_cleanup(fb);
641 /* We are no longer using the resource in GEM */
642 drm_gem_object_unreference_unlocked(&r->gem);
643 kfree(fb);
644}
645
646static const struct drm_mode_config_funcs psb_mode_funcs = {
647 .fb_create = psb_user_framebuffer_create,
648 .output_poll_changed = psbfb_output_poll_changed,
649};
650
4d8d096e
AC
651static void psb_setup_outputs(struct drm_device *dev)
652{
653 struct drm_psb_private *dev_priv = dev->dev_private;
654 struct drm_connector *connector;
655
656 drm_mode_create_scaling_mode_property(dev);
4d8d096e 657
13619ce5
AC
658 /* It is ok for this to fail - we just don't get backlight control */
659 if (!dev_priv->backlight_property)
660 dev_priv->backlight_property = drm_property_create_range(dev, 0,
661 "backlight", 0, 100);
4d8d096e
AC
662 dev_priv->ops->output_init(dev);
663
664 list_for_each_entry(connector, &dev->mode_config.connector_list,
665 head) {
367e4408
PJ
666 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
667 struct drm_encoder *encoder = &gma_encoder->base;
4d8d096e
AC
668 int crtc_mask = 0, clone_mask = 0;
669
670 /* valid crtcs */
367e4408 671 switch (gma_encoder->type) {
4d8d096e
AC
672 case INTEL_OUTPUT_ANALOG:
673 crtc_mask = (1 << 0);
674 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
675 break;
676 case INTEL_OUTPUT_SDVO:
cf8efd3a 677 crtc_mask = dev_priv->ops->sdvo_mask;
4d8d096e
AC
678 clone_mask = (1 << INTEL_OUTPUT_SDVO);
679 break;
680 case INTEL_OUTPUT_LVDS:
d235e64a 681 crtc_mask = dev_priv->ops->lvds_mask;
4d8d096e
AC
682 clone_mask = (1 << INTEL_OUTPUT_LVDS);
683 break;
684 case INTEL_OUTPUT_MIPI:
685 crtc_mask = (1 << 0);
686 clone_mask = (1 << INTEL_OUTPUT_MIPI);
687 break;
688 case INTEL_OUTPUT_MIPI2:
689 crtc_mask = (1 << 2);
690 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
691 break;
692 case INTEL_OUTPUT_HDMI:
d235e64a 693 crtc_mask = dev_priv->ops->hdmi_mask;
4d8d096e
AC
694 clone_mask = (1 << INTEL_OUTPUT_HDMI);
695 break;
220801bd
AC
696 case INTEL_OUTPUT_DISPLAYPORT:
697 crtc_mask = (1 << 0) | (1 << 1);
698 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
699 break;
d112a816
ZY
700 case INTEL_OUTPUT_EDP:
701 crtc_mask = (1 << 1);
702 clone_mask = (1 << INTEL_OUTPUT_EDP);
4d8d096e
AC
703 }
704 encoder->possible_crtcs = crtc_mask;
705 encoder->possible_clones =
a3d5d75f 706 gma_connector_clones(dev, clone_mask);
4d8d096e
AC
707 }
708}
709
710void psb_modeset_init(struct drm_device *dev)
711{
712 struct drm_psb_private *dev_priv = dev->dev_private;
713 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
714 int i;
715
716 drm_mode_config_init(dev);
717
718 dev->mode_config.min_width = 0;
719 dev->mode_config.min_height = 0;
720
e6ecefaa 721 dev->mode_config.funcs = &psb_mode_funcs;
4d8d096e
AC
722
723 /* set memory base */
dffc9ceb 724 /* Oaktrail and Poulsbo should use BAR 2*/
4d8d096e
AC
725 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
726 &(dev->mode_config.fb_base));
727
728 /* num pipes is 2 for PSB but 1 for Mrst */
729 for (i = 0; i < dev_priv->num_pipe; i++)
730 psb_intel_crtc_init(dev, i, mode_dev);
731
cbbd379a
PJ
732 dev->mode_config.max_width = 4096;
733 dev->mode_config.max_height = 4096;
4d8d096e
AC
734
735 psb_setup_outputs(dev);
d235e64a
AC
736
737 if (dev_priv->ops->errata)
738 dev_priv->ops->errata(dev);
4ab2c7f1
AC
739
740 dev_priv->modeset = true;
4d8d096e
AC
741}
742
743void psb_modeset_cleanup(struct drm_device *dev)
744{
4ab2c7f1
AC
745 struct drm_psb_private *dev_priv = dev->dev_private;
746 if (dev_priv->modeset) {
4ab2c7f1
AC
747 drm_kms_helper_poll_fini(dev);
748 psb_fbdev_fini(dev);
749 drm_mode_config_cleanup(dev);
4ab2c7f1 750 }
4d8d096e 751}