Loading...
1/**************************************************************************
2 * Copyright (c) 2007-2011, Intel Corporation.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 **************************************************************************/
19
20#include <linux/module.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/pfn_t.h>
25#include <linux/mm.h>
26#include <linux/tty.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
29#include <linux/fb.h>
30#include <linux/init.h>
31#include <linux/console.h>
32
33#include <drm/drmP.h>
34#include <drm/drm.h>
35#include <drm/drm_crtc.h>
36#include <drm/drm_fb_helper.h>
37
38#include "psb_drv.h"
39#include "psb_intel_reg.h"
40#include "psb_intel_drv.h"
41#include "framebuffer.h"
42#include "gtt.h"
43
44static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb);
45static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
46 struct drm_file *file_priv,
47 unsigned int *handle);
48
49static const struct drm_framebuffer_funcs psb_fb_funcs = {
50 .destroy = psb_user_framebuffer_destroy,
51 .create_handle = psb_user_framebuffer_create_handle,
52};
53
54#define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
55
56static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
57 unsigned blue, unsigned transp,
58 struct fb_info *info)
59{
60 struct psb_fbdev *fbdev = info->par;
61 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
62 uint32_t v;
63
64 if (!fb)
65 return -ENOMEM;
66
67 if (regno > 255)
68 return 1;
69
70 red = CMAP_TOHW(red, info->var.red.length);
71 blue = CMAP_TOHW(blue, info->var.blue.length);
72 green = CMAP_TOHW(green, info->var.green.length);
73 transp = CMAP_TOHW(transp, info->var.transp.length);
74
75 v = (red << info->var.red.offset) |
76 (green << info->var.green.offset) |
77 (blue << info->var.blue.offset) |
78 (transp << info->var.transp.offset);
79
80 if (regno < 16) {
81 switch (fb->bits_per_pixel) {
82 case 16:
83 ((uint32_t *) info->pseudo_palette)[regno] = v;
84 break;
85 case 24:
86 case 32:
87 ((uint32_t *) info->pseudo_palette)[regno] = v;
88 break;
89 }
90 }
91
92 return 0;
93}
94
95static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
96{
97 struct psb_fbdev *fbdev = info->par;
98 struct psb_framebuffer *psbfb = &fbdev->pfb;
99 struct drm_device *dev = psbfb->base.dev;
100
101 /*
102 * We have to poke our nose in here. The core fb code assumes
103 * panning is part of the hardware that can be invoked before
104 * the actual fb is mapped. In our case that isn't quite true.
105 */
106 if (psbfb->gtt->npage) {
107 /* GTT roll shifts in 4K pages, we need to shift the right
108 number of pages */
109 int pages = info->fix.line_length >> 12;
110 psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages);
111 }
112 return 0;
113}
114
115static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
116{
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;
125 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
126 psbfb->gtt->offset;
127
128 page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
129 address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT);
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
136 ret = vm_insert_mixed(vma, address,
137 __pfn_to_pfn_t(pfn, PFN_DEV));
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
158static const struct vm_operations_struct psbfb_vm_ops = {
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;
183 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
184 return 0;
185}
186
187static int psbfb_ioctl(struct fb_info *info, unsigned int cmd,
188 unsigned long arg)
189{
190 return -ENOTTY;
191}
192
193static struct fb_ops psbfb_ops = {
194 .owner = THIS_MODULE,
195 .fb_check_var = drm_fb_helper_check_var,
196 .fb_set_par = drm_fb_helper_set_par,
197 .fb_blank = drm_fb_helper_blank,
198 .fb_setcolreg = psbfb_setcolreg,
199 .fb_fillrect = drm_fb_helper_cfb_fillrect,
200 .fb_copyarea = psbfb_copyarea,
201 .fb_imageblit = drm_fb_helper_cfb_imageblit,
202 .fb_mmap = psbfb_mmap,
203 .fb_sync = psbfb_sync,
204 .fb_ioctl = psbfb_ioctl,
205};
206
207static struct fb_ops psbfb_roll_ops = {
208 .owner = THIS_MODULE,
209 .fb_check_var = drm_fb_helper_check_var,
210 .fb_set_par = drm_fb_helper_set_par,
211 .fb_blank = drm_fb_helper_blank,
212 .fb_setcolreg = psbfb_setcolreg,
213 .fb_fillrect = drm_fb_helper_cfb_fillrect,
214 .fb_copyarea = drm_fb_helper_cfb_copyarea,
215 .fb_imageblit = drm_fb_helper_cfb_imageblit,
216 .fb_pan_display = psbfb_pan,
217 .fb_mmap = psbfb_mmap,
218 .fb_ioctl = psbfb_ioctl,
219};
220
221static 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 = drm_fb_helper_cfb_fillrect,
228 .fb_copyarea = drm_fb_helper_cfb_copyarea,
229 .fb_imageblit = drm_fb_helper_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,
246 const struct drm_mode_fb_cmd2 *mode_cmd,
247 struct gtt_range *gt)
248{
249 u32 bpp, depth;
250 int ret;
251
252 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
253
254 if (mode_cmd->pitches[0] & 63)
255 return -EINVAL;
256 switch (bpp) {
257 case 8:
258 case 16:
259 case 24:
260 case 32:
261 break;
262 default:
263 return -EINVAL;
264 }
265 drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
266 fb->gtt = gt;
267 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
268 if (ret) {
269 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
270 return ret;
271 }
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,
289 const struct drm_mode_fb_cmd2 *mode_cmd,
290 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
311 * @force: fall back to GEM buffers if need be
312 *
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
315 * 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.
317 *
318 * Re-address this as and if the framebuffer layer grows this ability.
319 */
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, PAGE_SIZE);
325 if (backing) {
326 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
327 return backing;
328 }
329 return NULL;
330}
331
332/**
333 * psbfb_create - create a framebuffer
334 * @fbdev: the framebuffer device
335 * @sizes: specification of the layout
336 *
337 * Create a framebuffer to the specifications provided
338 */
339static int psbfb_create(struct psb_fbdev *fbdev,
340 struct drm_fb_helper_surface_size *sizes)
341{
342 struct drm_device *dev = fbdev->psb_fb_helper.dev;
343 struct drm_psb_private *dev_priv = dev->dev_private;
344 struct fb_info *info;
345 struct drm_framebuffer *fb;
346 struct psb_framebuffer *psbfb = &fbdev->pfb;
347 struct drm_mode_fb_cmd2 mode_cmd;
348 int size;
349 int ret;
350 struct gtt_range *backing;
351 u32 bpp, depth;
352 int gtt_roll = 0;
353 int pitch_lines = 0;
354
355 mode_cmd.width = sizes->surface_width;
356 mode_cmd.height = sizes->surface_height;
357 bpp = sizes->surface_bpp;
358 depth = sizes->surface_depth;
359
360 /* No 24bit packed */
361 if (bpp == 24)
362 bpp = 32;
363
364 do {
365 /*
366 * Acceleration via the GTT requires pitch to be
367 * power of two aligned. Preferably page but less
368 * is ok with some fonts
369 */
370 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
371
372 size = mode_cmd.pitches[0] * mode_cmd.height;
373 size = ALIGN(size, PAGE_SIZE);
374
375 /* Allocate the fb in the GTT with stolen page backing */
376 backing = psbfb_alloc(dev, size);
377
378 if (pitch_lines)
379 pitch_lines *= 2;
380 else
381 pitch_lines = 1;
382 gtt_roll++;
383 } while (backing == NULL && pitch_lines <= 16);
384
385 /* The final pitch we accepted if we succeeded */
386 pitch_lines /= 2;
387
388 if (backing == NULL) {
389 /*
390 * We couldn't get the space we wanted, fall back to the
391 * display engine requirement instead. The HW requires
392 * the pitch to be 64 byte aligned
393 */
394
395 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
396 pitch_lines = 64;
397
398 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
399
400 size = mode_cmd.pitches[0] * mode_cmd.height;
401 size = ALIGN(size, PAGE_SIZE);
402
403 /* Allocate the framebuffer in the GTT with stolen page backing */
404 backing = psbfb_alloc(dev, size);
405 if (backing == NULL)
406 return -ENOMEM;
407 }
408
409 memset(dev_priv->vram_addr + backing->offset, 0, size);
410
411 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
412 if (IS_ERR(info)) {
413 ret = PTR_ERR(info);
414 goto out_err1;
415 }
416 info->par = fbdev;
417
418 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
419
420 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
421 if (ret)
422 goto out_unref;
423
424 fb = &psbfb->base;
425 psbfb->fbdev = info;
426
427 fbdev->psb_fb_helper.fb = fb;
428
429 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
430 strcpy(info->fix.id, "psbdrmfb");
431
432 info->flags = FBINFO_DEFAULT;
433 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
434 info->fbops = &psbfb_ops;
435 else if (gtt_roll) { /* GTT rolling seems best */
436 info->fbops = &psbfb_roll_ops;
437 info->flags |= FBINFO_HWACCEL_YPAN;
438 } else /* Software */
439 info->fbops = &psbfb_unaccel_ops;
440
441 info->fix.smem_start = dev->mode_config.fb_base;
442 info->fix.smem_len = size;
443 info->fix.ywrapstep = gtt_roll;
444 info->fix.ypanstep = 0;
445
446 /* Accessed stolen memory directly */
447 info->screen_base = dev_priv->vram_addr + backing->offset;
448 info->screen_size = size;
449
450 if (dev_priv->gtt.stolen_size) {
451 info->apertures->ranges[0].base = dev->mode_config.fb_base;
452 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
453 }
454
455 drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper,
456 sizes->fb_width, sizes->fb_height);
457
458 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
459 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
460
461 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
462
463 dev_dbg(dev->dev, "allocated %dx%d fb\n",
464 psbfb->base.width, psbfb->base.height);
465
466 return 0;
467out_unref:
468 if (backing->stolen)
469 psb_gtt_free_range(dev, backing);
470 else
471 drm_gem_object_unreference_unlocked(&backing->gem);
472
473 drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
474out_err1:
475 psb_gtt_free_range(dev, backing);
476 return ret;
477}
478
479/**
480 * psb_user_framebuffer_create - create framebuffer
481 * @dev: our DRM device
482 * @filp: client file
483 * @cmd: mode request
484 *
485 * Create a new framebuffer backed by a userspace GEM object
486 */
487static struct drm_framebuffer *psb_user_framebuffer_create
488 (struct drm_device *dev, struct drm_file *filp,
489 const struct drm_mode_fb_cmd2 *cmd)
490{
491 struct gtt_range *r;
492 struct drm_gem_object *obj;
493
494 /*
495 * Find the GEM object and thus the gtt range object that is
496 * to back this space
497 */
498 obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]);
499 if (obj == NULL)
500 return ERR_PTR(-ENOENT);
501
502 /* Let the core code do all the work */
503 r = container_of(obj, struct gtt_range, gem);
504 return psb_framebuffer_create(dev, cmd, r);
505}
506
507static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
508 u16 blue, int regno)
509{
510 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
511
512 gma_crtc->lut_r[regno] = red >> 8;
513 gma_crtc->lut_g[regno] = green >> 8;
514 gma_crtc->lut_b[regno] = blue >> 8;
515}
516
517static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red,
518 u16 *green, u16 *blue, int regno)
519{
520 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
521
522 *red = gma_crtc->lut_r[regno] << 8;
523 *green = gma_crtc->lut_g[regno] << 8;
524 *blue = gma_crtc->lut_b[regno] << 8;
525}
526
527static int psbfb_probe(struct drm_fb_helper *helper,
528 struct drm_fb_helper_surface_size *sizes)
529{
530 struct psb_fbdev *psb_fbdev =
531 container_of(helper, struct psb_fbdev, psb_fb_helper);
532 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
533 struct drm_psb_private *dev_priv = dev->dev_private;
534 int bytespp;
535
536 bytespp = sizes->surface_bpp / 8;
537 if (bytespp == 3) /* no 24bit packed */
538 bytespp = 4;
539
540 /* If the mode will not fit in 32bit then switch to 16bit to get
541 a console on full resolution. The X mode setting server will
542 allocate its own 32bit GEM framebuffer */
543 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
544 dev_priv->vram_stolen_size) {
545 sizes->surface_bpp = 16;
546 sizes->surface_depth = 16;
547 }
548
549 return psbfb_create(psb_fbdev, sizes);
550}
551
552static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
553 .gamma_set = psbfb_gamma_set,
554 .gamma_get = psbfb_gamma_get,
555 .fb_probe = psbfb_probe,
556};
557
558static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
559{
560 struct psb_framebuffer *psbfb = &fbdev->pfb;
561
562 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
563 drm_fb_helper_release_fbi(&fbdev->psb_fb_helper);
564
565 drm_fb_helper_fini(&fbdev->psb_fb_helper);
566 drm_framebuffer_unregister_private(&psbfb->base);
567 drm_framebuffer_cleanup(&psbfb->base);
568
569 if (psbfb->gtt)
570 drm_gem_object_unreference_unlocked(&psbfb->gtt->gem);
571 return 0;
572}
573
574int psb_fbdev_init(struct drm_device *dev)
575{
576 struct psb_fbdev *fbdev;
577 struct drm_psb_private *dev_priv = dev->dev_private;
578 int ret;
579
580 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
581 if (!fbdev) {
582 dev_err(dev->dev, "no memory\n");
583 return -ENOMEM;
584 }
585
586 dev_priv->fbdev = fbdev;
587
588 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
589
590 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
591 dev_priv->ops->crtcs, INTELFB_CONN_LIMIT);
592 if (ret)
593 goto free;
594
595 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
596 if (ret)
597 goto fini;
598
599 /* disable all the possible outputs/crtcs before entering KMS mode */
600 drm_helper_disable_unused_functions(dev);
601
602 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
603 if (ret)
604 goto fini;
605
606 return 0;
607
608fini:
609 drm_fb_helper_fini(&fbdev->psb_fb_helper);
610free:
611 kfree(fbdev);
612 return ret;
613}
614
615static void psb_fbdev_fini(struct drm_device *dev)
616{
617 struct drm_psb_private *dev_priv = dev->dev_private;
618
619 if (!dev_priv->fbdev)
620 return;
621
622 psb_fbdev_destroy(dev, dev_priv->fbdev);
623 kfree(dev_priv->fbdev);
624 dev_priv->fbdev = NULL;
625}
626
627static void psbfb_output_poll_changed(struct drm_device *dev)
628{
629 struct drm_psb_private *dev_priv = dev->dev_private;
630 struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev;
631 drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper);
632}
633
634/**
635 * psb_user_framebuffer_create_handle - add hamdle to a framebuffer
636 * @fb: framebuffer
637 * @file_priv: our DRM file
638 * @handle: returned handle
639 *
640 * Our framebuffer object is a GTT range which also contains a GEM
641 * object. We need to turn it into a handle for userspace. GEM will do
642 * the work for us
643 */
644static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb,
645 struct drm_file *file_priv,
646 unsigned int *handle)
647{
648 struct psb_framebuffer *psbfb = to_psb_fb(fb);
649 struct gtt_range *r = psbfb->gtt;
650 return drm_gem_handle_create(file_priv, &r->gem, handle);
651}
652
653/**
654 * psb_user_framebuffer_destroy - destruct user created fb
655 * @fb: framebuffer
656 *
657 * User framebuffers are backed by GEM objects so all we have to do is
658 * clean up a bit and drop the reference, GEM will handle the fallout
659 */
660static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb)
661{
662 struct psb_framebuffer *psbfb = to_psb_fb(fb);
663 struct gtt_range *r = psbfb->gtt;
664
665 /* Let DRM do its clean up */
666 drm_framebuffer_cleanup(fb);
667 /* We are no longer using the resource in GEM */
668 drm_gem_object_unreference_unlocked(&r->gem);
669 kfree(fb);
670}
671
672static const struct drm_mode_config_funcs psb_mode_funcs = {
673 .fb_create = psb_user_framebuffer_create,
674 .output_poll_changed = psbfb_output_poll_changed,
675};
676
677static void psb_setup_outputs(struct drm_device *dev)
678{
679 struct drm_psb_private *dev_priv = dev->dev_private;
680 struct drm_connector *connector;
681
682 drm_mode_create_scaling_mode_property(dev);
683
684 /* It is ok for this to fail - we just don't get backlight control */
685 if (!dev_priv->backlight_property)
686 dev_priv->backlight_property = drm_property_create_range(dev, 0,
687 "backlight", 0, 100);
688 dev_priv->ops->output_init(dev);
689
690 list_for_each_entry(connector, &dev->mode_config.connector_list,
691 head) {
692 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
693 struct drm_encoder *encoder = &gma_encoder->base;
694 int crtc_mask = 0, clone_mask = 0;
695
696 /* valid crtcs */
697 switch (gma_encoder->type) {
698 case INTEL_OUTPUT_ANALOG:
699 crtc_mask = (1 << 0);
700 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
701 break;
702 case INTEL_OUTPUT_SDVO:
703 crtc_mask = dev_priv->ops->sdvo_mask;
704 clone_mask = (1 << INTEL_OUTPUT_SDVO);
705 break;
706 case INTEL_OUTPUT_LVDS:
707 crtc_mask = dev_priv->ops->lvds_mask;
708 clone_mask = (1 << INTEL_OUTPUT_LVDS);
709 break;
710 case INTEL_OUTPUT_MIPI:
711 crtc_mask = (1 << 0);
712 clone_mask = (1 << INTEL_OUTPUT_MIPI);
713 break;
714 case INTEL_OUTPUT_MIPI2:
715 crtc_mask = (1 << 2);
716 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
717 break;
718 case INTEL_OUTPUT_HDMI:
719 crtc_mask = dev_priv->ops->hdmi_mask;
720 clone_mask = (1 << INTEL_OUTPUT_HDMI);
721 break;
722 case INTEL_OUTPUT_DISPLAYPORT:
723 crtc_mask = (1 << 0) | (1 << 1);
724 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
725 break;
726 case INTEL_OUTPUT_EDP:
727 crtc_mask = (1 << 1);
728 clone_mask = (1 << INTEL_OUTPUT_EDP);
729 }
730 encoder->possible_crtcs = crtc_mask;
731 encoder->possible_clones =
732 gma_connector_clones(dev, clone_mask);
733 }
734}
735
736void psb_modeset_init(struct drm_device *dev)
737{
738 struct drm_psb_private *dev_priv = dev->dev_private;
739 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
740 int i;
741
742 drm_mode_config_init(dev);
743
744 dev->mode_config.min_width = 0;
745 dev->mode_config.min_height = 0;
746
747 dev->mode_config.funcs = &psb_mode_funcs;
748
749 /* set memory base */
750 /* Oaktrail and Poulsbo should use BAR 2*/
751 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
752 &(dev->mode_config.fb_base));
753
754 /* num pipes is 2 for PSB but 1 for Mrst */
755 for (i = 0; i < dev_priv->num_pipe; i++)
756 psb_intel_crtc_init(dev, i, mode_dev);
757
758 dev->mode_config.max_width = 4096;
759 dev->mode_config.max_height = 4096;
760
761 psb_setup_outputs(dev);
762
763 if (dev_priv->ops->errata)
764 dev_priv->ops->errata(dev);
765
766 dev_priv->modeset = true;
767}
768
769void psb_modeset_cleanup(struct drm_device *dev)
770{
771 struct drm_psb_private *dev_priv = dev->dev_private;
772 if (dev_priv->modeset) {
773 drm_kms_helper_poll_fini(dev);
774 psb_fbdev_fini(dev);
775 drm_mode_config_cleanup(dev);
776 }
777}
1// SPDX-License-Identifier: GPL-2.0-only
2/**************************************************************************
3 * Copyright (c) 2007-2011, Intel Corporation.
4 * All Rights Reserved.
5 *
6 **************************************************************************/
7
8#include <linux/console.h>
9#include <linux/delay.h>
10#include <linux/errno.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/mm.h>
14#include <linux/module.h>
15#include <linux/pfn_t.h>
16#include <linux/slab.h>
17#include <linux/string.h>
18#include <linux/tty.h>
19
20#include <drm/drm.h>
21#include <drm/drm_crtc.h>
22#include <drm/drm_fb_helper.h>
23#include <drm/drm_fourcc.h>
24#include <drm/drm_gem_framebuffer_helper.h>
25
26#include "framebuffer.h"
27#include "gtt.h"
28#include "psb_drv.h"
29#include "psb_intel_drv.h"
30#include "psb_intel_reg.h"
31
32static const struct drm_framebuffer_funcs psb_fb_funcs = {
33 .destroy = drm_gem_fb_destroy,
34 .create_handle = drm_gem_fb_create_handle,
35};
36
37#define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
38
39static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
40 unsigned blue, unsigned transp,
41 struct fb_info *info)
42{
43 struct psb_fbdev *fbdev = info->par;
44 struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb;
45 uint32_t v;
46
47 if (!fb)
48 return -ENOMEM;
49
50 if (regno > 255)
51 return 1;
52
53 red = CMAP_TOHW(red, info->var.red.length);
54 blue = CMAP_TOHW(blue, info->var.blue.length);
55 green = CMAP_TOHW(green, info->var.green.length);
56 transp = CMAP_TOHW(transp, info->var.transp.length);
57
58 v = (red << info->var.red.offset) |
59 (green << info->var.green.offset) |
60 (blue << info->var.blue.offset) |
61 (transp << info->var.transp.offset);
62
63 if (regno < 16) {
64 switch (fb->format->cpp[0] * 8) {
65 case 16:
66 ((uint32_t *) info->pseudo_palette)[regno] = v;
67 break;
68 case 24:
69 case 32:
70 ((uint32_t *) info->pseudo_palette)[regno] = v;
71 break;
72 }
73 }
74
75 return 0;
76}
77
78static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info)
79{
80 struct psb_fbdev *fbdev = info->par;
81 struct psb_framebuffer *psbfb = &fbdev->pfb;
82 struct drm_device *dev = psbfb->base.dev;
83 struct gtt_range *gtt = to_gtt_range(psbfb->base.obj[0]);
84
85 /*
86 * We have to poke our nose in here. The core fb code assumes
87 * panning is part of the hardware that can be invoked before
88 * the actual fb is mapped. In our case that isn't quite true.
89 */
90 if (gtt->npage) {
91 /* GTT roll shifts in 4K pages, we need to shift the right
92 number of pages */
93 int pages = info->fix.line_length >> 12;
94 psb_gtt_roll(dev, gtt, var->yoffset * pages);
95 }
96 return 0;
97}
98
99static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
100{
101 struct vm_area_struct *vma = vmf->vma;
102 struct psb_framebuffer *psbfb = vma->vm_private_data;
103 struct drm_device *dev = psbfb->base.dev;
104 struct drm_psb_private *dev_priv = dev->dev_private;
105 struct gtt_range *gtt = to_gtt_range(psbfb->base.obj[0]);
106 int page_num;
107 int i;
108 unsigned long address;
109 vm_fault_t ret = VM_FAULT_SIGBUS;
110 unsigned long pfn;
111 unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
112 gtt->offset;
113
114 page_num = vma_pages(vma);
115 address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
116
117 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
118
119 for (i = 0; i < page_num; i++) {
120 pfn = (phys_addr >> PAGE_SHIFT);
121
122 ret = vmf_insert_mixed(vma, address,
123 __pfn_to_pfn_t(pfn, PFN_DEV));
124 if (unlikely(ret & VM_FAULT_ERROR))
125 break;
126 address += PAGE_SIZE;
127 phys_addr += PAGE_SIZE;
128 }
129 return ret;
130}
131
132static void psbfb_vm_open(struct vm_area_struct *vma)
133{
134}
135
136static void psbfb_vm_close(struct vm_area_struct *vma)
137{
138}
139
140static const struct vm_operations_struct psbfb_vm_ops = {
141 .fault = psbfb_vm_fault,
142 .open = psbfb_vm_open,
143 .close = psbfb_vm_close
144};
145
146static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
147{
148 struct psb_fbdev *fbdev = info->par;
149 struct psb_framebuffer *psbfb = &fbdev->pfb;
150
151 if (vma->vm_pgoff != 0)
152 return -EINVAL;
153 if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
154 return -EINVAL;
155
156 if (!psbfb->addr_space)
157 psbfb->addr_space = vma->vm_file->f_mapping;
158 /*
159 * If this is a GEM object then info->screen_base is the virtual
160 * kernel remapping of the object. FIXME: Review if this is
161 * suitable for our mmap work
162 */
163 vma->vm_ops = &psbfb_vm_ops;
164 vma->vm_private_data = (void *)psbfb;
165 vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
166 return 0;
167}
168
169static struct fb_ops psbfb_ops = {
170 .owner = THIS_MODULE,
171 DRM_FB_HELPER_DEFAULT_OPS,
172 .fb_setcolreg = psbfb_setcolreg,
173 .fb_fillrect = drm_fb_helper_cfb_fillrect,
174 .fb_copyarea = psbfb_copyarea,
175 .fb_imageblit = drm_fb_helper_cfb_imageblit,
176 .fb_mmap = psbfb_mmap,
177 .fb_sync = psbfb_sync,
178};
179
180static struct fb_ops psbfb_roll_ops = {
181 .owner = THIS_MODULE,
182 DRM_FB_HELPER_DEFAULT_OPS,
183 .fb_setcolreg = psbfb_setcolreg,
184 .fb_fillrect = drm_fb_helper_cfb_fillrect,
185 .fb_copyarea = drm_fb_helper_cfb_copyarea,
186 .fb_imageblit = drm_fb_helper_cfb_imageblit,
187 .fb_pan_display = psbfb_pan,
188 .fb_mmap = psbfb_mmap,
189};
190
191static struct fb_ops psbfb_unaccel_ops = {
192 .owner = THIS_MODULE,
193 DRM_FB_HELPER_DEFAULT_OPS,
194 .fb_setcolreg = psbfb_setcolreg,
195 .fb_fillrect = drm_fb_helper_cfb_fillrect,
196 .fb_copyarea = drm_fb_helper_cfb_copyarea,
197 .fb_imageblit = drm_fb_helper_cfb_imageblit,
198 .fb_mmap = psbfb_mmap,
199};
200
201/**
202 * psb_framebuffer_init - initialize a framebuffer
203 * @dev: our DRM device
204 * @fb: framebuffer to set up
205 * @mode_cmd: mode description
206 * @gt: backing object
207 *
208 * Configure and fill in the boilerplate for our frame buffer. Return
209 * 0 on success or an error code if we fail.
210 */
211static int psb_framebuffer_init(struct drm_device *dev,
212 struct psb_framebuffer *fb,
213 const struct drm_mode_fb_cmd2 *mode_cmd,
214 struct gtt_range *gt)
215{
216 const struct drm_format_info *info;
217 int ret;
218
219 /*
220 * Reject unknown formats, YUV formats, and formats with more than
221 * 4 bytes per pixel.
222 */
223 info = drm_get_format_info(dev, mode_cmd);
224 if (!info || !info->depth || info->cpp[0] > 4)
225 return -EINVAL;
226
227 if (mode_cmd->pitches[0] & 63)
228 return -EINVAL;
229
230 drm_helper_mode_fill_fb_struct(dev, &fb->base, mode_cmd);
231 fb->base.obj[0] = >->gem;
232 ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs);
233 if (ret) {
234 dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
235 return ret;
236 }
237 return 0;
238}
239
240/**
241 * psb_framebuffer_create - create a framebuffer backed by gt
242 * @dev: our DRM device
243 * @mode_cmd: the description of the requested mode
244 * @gt: the backing object
245 *
246 * Create a framebuffer object backed by the gt, and fill in the
247 * boilerplate required
248 *
249 * TODO: review object references
250 */
251
252static struct drm_framebuffer *psb_framebuffer_create
253 (struct drm_device *dev,
254 const struct drm_mode_fb_cmd2 *mode_cmd,
255 struct gtt_range *gt)
256{
257 struct psb_framebuffer *fb;
258 int ret;
259
260 fb = kzalloc(sizeof(*fb), GFP_KERNEL);
261 if (!fb)
262 return ERR_PTR(-ENOMEM);
263
264 ret = psb_framebuffer_init(dev, fb, mode_cmd, gt);
265 if (ret) {
266 kfree(fb);
267 return ERR_PTR(ret);
268 }
269 return &fb->base;
270}
271
272/**
273 * psbfb_alloc - allocate frame buffer memory
274 * @dev: the DRM device
275 * @aligned_size: space needed
276 *
277 * Allocate the frame buffer. In the usual case we get a GTT range that
278 * is stolen memory backed and life is simple. If there isn't sufficient
279 * we fail as we don't have the virtual mapping space to really vmap it
280 * and the kernel console code can't handle non linear framebuffers.
281 *
282 * Re-address this as and if the framebuffer layer grows this ability.
283 */
284static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
285{
286 struct gtt_range *backing;
287 /* Begin by trying to use stolen memory backing */
288 backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
289 if (backing) {
290 drm_gem_private_object_init(dev, &backing->gem, aligned_size);
291 return backing;
292 }
293 return NULL;
294}
295
296/**
297 * psbfb_create - create a framebuffer
298 * @fbdev: the framebuffer device
299 * @sizes: specification of the layout
300 *
301 * Create a framebuffer to the specifications provided
302 */
303static int psbfb_create(struct psb_fbdev *fbdev,
304 struct drm_fb_helper_surface_size *sizes)
305{
306 struct drm_device *dev = fbdev->psb_fb_helper.dev;
307 struct drm_psb_private *dev_priv = dev->dev_private;
308 struct fb_info *info;
309 struct drm_framebuffer *fb;
310 struct psb_framebuffer *psbfb = &fbdev->pfb;
311 struct drm_mode_fb_cmd2 mode_cmd;
312 int size;
313 int ret;
314 struct gtt_range *backing;
315 u32 bpp, depth;
316 int gtt_roll = 0;
317 int pitch_lines = 0;
318
319 mode_cmd.width = sizes->surface_width;
320 mode_cmd.height = sizes->surface_height;
321 bpp = sizes->surface_bpp;
322 depth = sizes->surface_depth;
323
324 /* No 24bit packed */
325 if (bpp == 24)
326 bpp = 32;
327
328 do {
329 /*
330 * Acceleration via the GTT requires pitch to be
331 * power of two aligned. Preferably page but less
332 * is ok with some fonts
333 */
334 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines);
335
336 size = mode_cmd.pitches[0] * mode_cmd.height;
337 size = ALIGN(size, PAGE_SIZE);
338
339 /* Allocate the fb in the GTT with stolen page backing */
340 backing = psbfb_alloc(dev, size);
341
342 if (pitch_lines)
343 pitch_lines *= 2;
344 else
345 pitch_lines = 1;
346 gtt_roll++;
347 } while (backing == NULL && pitch_lines <= 16);
348
349 /* The final pitch we accepted if we succeeded */
350 pitch_lines /= 2;
351
352 if (backing == NULL) {
353 /*
354 * We couldn't get the space we wanted, fall back to the
355 * display engine requirement instead. The HW requires
356 * the pitch to be 64 byte aligned
357 */
358
359 gtt_roll = 0; /* Don't use GTT accelerated scrolling */
360 pitch_lines = 64;
361
362 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64);
363
364 size = mode_cmd.pitches[0] * mode_cmd.height;
365 size = ALIGN(size, PAGE_SIZE);
366
367 /* Allocate the framebuffer in the GTT with stolen page backing */
368 backing = psbfb_alloc(dev, size);
369 if (backing == NULL)
370 return -ENOMEM;
371 }
372
373 memset(dev_priv->vram_addr + backing->offset, 0, size);
374
375 info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper);
376 if (IS_ERR(info)) {
377 ret = PTR_ERR(info);
378 goto out;
379 }
380
381 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
382
383 ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing);
384 if (ret)
385 goto out;
386
387 fb = &psbfb->base;
388 psbfb->fbdev = info;
389
390 fbdev->psb_fb_helper.fb = fb;
391
392 if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */
393 info->fbops = &psbfb_ops;
394 else if (gtt_roll) { /* GTT rolling seems best */
395 info->fbops = &psbfb_roll_ops;
396 info->flags |= FBINFO_HWACCEL_YPAN;
397 } else /* Software */
398 info->fbops = &psbfb_unaccel_ops;
399
400 info->fix.smem_start = dev->mode_config.fb_base;
401 info->fix.smem_len = size;
402 info->fix.ywrapstep = gtt_roll;
403 info->fix.ypanstep = 0;
404
405 /* Accessed stolen memory directly */
406 info->screen_base = dev_priv->vram_addr + backing->offset;
407 info->screen_size = size;
408
409 if (dev_priv->gtt.stolen_size) {
410 info->apertures->ranges[0].base = dev->mode_config.fb_base;
411 info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
412 }
413
414 drm_fb_helper_fill_info(info, &fbdev->psb_fb_helper, sizes);
415
416 info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
417 info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
418
419 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
420
421 dev_dbg(dev->dev, "allocated %dx%d fb\n",
422 psbfb->base.width, psbfb->base.height);
423
424 return 0;
425out:
426 psb_gtt_free_range(dev, backing);
427 return ret;
428}
429
430/**
431 * psb_user_framebuffer_create - create framebuffer
432 * @dev: our DRM device
433 * @filp: client file
434 * @cmd: mode request
435 *
436 * Create a new framebuffer backed by a userspace GEM object
437 */
438static struct drm_framebuffer *psb_user_framebuffer_create
439 (struct drm_device *dev, struct drm_file *filp,
440 const struct drm_mode_fb_cmd2 *cmd)
441{
442 struct gtt_range *r;
443 struct drm_gem_object *obj;
444
445 /*
446 * Find the GEM object and thus the gtt range object that is
447 * to back this space
448 */
449 obj = drm_gem_object_lookup(filp, cmd->handles[0]);
450 if (obj == NULL)
451 return ERR_PTR(-ENOENT);
452
453 /* Let the core code do all the work */
454 r = container_of(obj, struct gtt_range, gem);
455 return psb_framebuffer_create(dev, cmd, r);
456}
457
458static int psbfb_probe(struct drm_fb_helper *helper,
459 struct drm_fb_helper_surface_size *sizes)
460{
461 struct psb_fbdev *psb_fbdev =
462 container_of(helper, struct psb_fbdev, psb_fb_helper);
463 struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
464 struct drm_psb_private *dev_priv = dev->dev_private;
465 int bytespp;
466
467 bytespp = sizes->surface_bpp / 8;
468 if (bytespp == 3) /* no 24bit packed */
469 bytespp = 4;
470
471 /* If the mode will not fit in 32bit then switch to 16bit to get
472 a console on full resolution. The X mode setting server will
473 allocate its own 32bit GEM framebuffer */
474 if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
475 dev_priv->vram_stolen_size) {
476 sizes->surface_bpp = 16;
477 sizes->surface_depth = 16;
478 }
479
480 return psbfb_create(psb_fbdev, sizes);
481}
482
483static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
484 .fb_probe = psbfb_probe,
485};
486
487static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev)
488{
489 struct psb_framebuffer *psbfb = &fbdev->pfb;
490
491 drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper);
492
493 drm_fb_helper_fini(&fbdev->psb_fb_helper);
494 drm_framebuffer_unregister_private(&psbfb->base);
495 drm_framebuffer_cleanup(&psbfb->base);
496
497 if (psbfb->base.obj[0])
498 drm_gem_object_put_unlocked(psbfb->base.obj[0]);
499 return 0;
500}
501
502int psb_fbdev_init(struct drm_device *dev)
503{
504 struct psb_fbdev *fbdev;
505 struct drm_psb_private *dev_priv = dev->dev_private;
506 int ret;
507
508 fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL);
509 if (!fbdev) {
510 dev_err(dev->dev, "no memory\n");
511 return -ENOMEM;
512 }
513
514 dev_priv->fbdev = fbdev;
515
516 drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs);
517
518 ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper,
519 INTELFB_CONN_LIMIT);
520 if (ret)
521 goto free;
522
523 ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper);
524 if (ret)
525 goto fini;
526
527 /* disable all the possible outputs/crtcs before entering KMS mode */
528 drm_helper_disable_unused_functions(dev);
529
530 ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32);
531 if (ret)
532 goto fini;
533
534 return 0;
535
536fini:
537 drm_fb_helper_fini(&fbdev->psb_fb_helper);
538free:
539 kfree(fbdev);
540 return ret;
541}
542
543static void psb_fbdev_fini(struct drm_device *dev)
544{
545 struct drm_psb_private *dev_priv = dev->dev_private;
546
547 if (!dev_priv->fbdev)
548 return;
549
550 psb_fbdev_destroy(dev, dev_priv->fbdev);
551 kfree(dev_priv->fbdev);
552 dev_priv->fbdev = NULL;
553}
554
555static const struct drm_mode_config_funcs psb_mode_funcs = {
556 .fb_create = psb_user_framebuffer_create,
557 .output_poll_changed = drm_fb_helper_output_poll_changed,
558};
559
560static void psb_setup_outputs(struct drm_device *dev)
561{
562 struct drm_psb_private *dev_priv = dev->dev_private;
563 struct drm_connector *connector;
564
565 drm_mode_create_scaling_mode_property(dev);
566
567 /* It is ok for this to fail - we just don't get backlight control */
568 if (!dev_priv->backlight_property)
569 dev_priv->backlight_property = drm_property_create_range(dev, 0,
570 "backlight", 0, 100);
571 dev_priv->ops->output_init(dev);
572
573 list_for_each_entry(connector, &dev->mode_config.connector_list,
574 head) {
575 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
576 struct drm_encoder *encoder = &gma_encoder->base;
577 int crtc_mask = 0, clone_mask = 0;
578
579 /* valid crtcs */
580 switch (gma_encoder->type) {
581 case INTEL_OUTPUT_ANALOG:
582 crtc_mask = (1 << 0);
583 clone_mask = (1 << INTEL_OUTPUT_ANALOG);
584 break;
585 case INTEL_OUTPUT_SDVO:
586 crtc_mask = dev_priv->ops->sdvo_mask;
587 clone_mask = (1 << INTEL_OUTPUT_SDVO);
588 break;
589 case INTEL_OUTPUT_LVDS:
590 crtc_mask = dev_priv->ops->lvds_mask;
591 clone_mask = (1 << INTEL_OUTPUT_LVDS);
592 break;
593 case INTEL_OUTPUT_MIPI:
594 crtc_mask = (1 << 0);
595 clone_mask = (1 << INTEL_OUTPUT_MIPI);
596 break;
597 case INTEL_OUTPUT_MIPI2:
598 crtc_mask = (1 << 2);
599 clone_mask = (1 << INTEL_OUTPUT_MIPI2);
600 break;
601 case INTEL_OUTPUT_HDMI:
602 crtc_mask = dev_priv->ops->hdmi_mask;
603 clone_mask = (1 << INTEL_OUTPUT_HDMI);
604 break;
605 case INTEL_OUTPUT_DISPLAYPORT:
606 crtc_mask = (1 << 0) | (1 << 1);
607 clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT);
608 break;
609 case INTEL_OUTPUT_EDP:
610 crtc_mask = (1 << 1);
611 clone_mask = (1 << INTEL_OUTPUT_EDP);
612 }
613 encoder->possible_crtcs = crtc_mask;
614 encoder->possible_clones =
615 gma_connector_clones(dev, clone_mask);
616 }
617}
618
619void psb_modeset_init(struct drm_device *dev)
620{
621 struct drm_psb_private *dev_priv = dev->dev_private;
622 struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
623 int i;
624
625 drm_mode_config_init(dev);
626
627 dev->mode_config.min_width = 0;
628 dev->mode_config.min_height = 0;
629
630 dev->mode_config.funcs = &psb_mode_funcs;
631
632 /* set memory base */
633 /* Oaktrail and Poulsbo should use BAR 2*/
634 pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
635 &(dev->mode_config.fb_base));
636
637 /* num pipes is 2 for PSB but 1 for Mrst */
638 for (i = 0; i < dev_priv->num_pipe; i++)
639 psb_intel_crtc_init(dev, i, mode_dev);
640
641 dev->mode_config.max_width = 4096;
642 dev->mode_config.max_height = 4096;
643
644 psb_setup_outputs(dev);
645
646 if (dev_priv->ops->errata)
647 dev_priv->ops->errata(dev);
648
649 dev_priv->modeset = true;
650}
651
652void psb_modeset_cleanup(struct drm_device *dev)
653{
654 struct drm_psb_private *dev_priv = dev->dev_private;
655 if (dev_priv->modeset) {
656 drm_kms_helper_poll_fini(dev);
657 psb_fbdev_fini(dev);
658 drm_mode_config_cleanup(dev);
659 }
660}