1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3 * Copyright (c) 2007-2011, Intel Corporation.
6 **************************************************************************/
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>
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>
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>
26 #include "framebuffer.h"
30 #include "psb_intel_drv.h"
31 #include "psb_intel_reg.h"
33 static const struct drm_framebuffer_funcs psb_fb_funcs
= {
34 .destroy
= drm_gem_fb_destroy
,
35 .create_handle
= drm_gem_fb_create_handle
,
38 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
40 static int psbfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
41 unsigned blue
, unsigned transp
,
44 struct drm_fb_helper
*fb_helper
= info
->par
;
45 struct drm_framebuffer
*fb
= fb_helper
->fb
;
54 red
= CMAP_TOHW(red
, info
->var
.red
.length
);
55 blue
= CMAP_TOHW(blue
, info
->var
.blue
.length
);
56 green
= CMAP_TOHW(green
, info
->var
.green
.length
);
57 transp
= CMAP_TOHW(transp
, info
->var
.transp
.length
);
59 v
= (red
<< info
->var
.red
.offset
) |
60 (green
<< info
->var
.green
.offset
) |
61 (blue
<< info
->var
.blue
.offset
) |
62 (transp
<< info
->var
.transp
.offset
);
65 switch (fb
->format
->cpp
[0] * 8) {
67 ((uint32_t *) info
->pseudo_palette
)[regno
] = v
;
71 ((uint32_t *) info
->pseudo_palette
)[regno
] = v
;
79 static vm_fault_t
psbfb_vm_fault(struct vm_fault
*vmf
)
81 struct vm_area_struct
*vma
= vmf
->vma
;
82 struct drm_framebuffer
*fb
= vma
->vm_private_data
;
83 struct drm_device
*dev
= fb
->dev
;
84 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
85 struct gtt_range
*gtt
= to_gtt_range(fb
->obj
[0]);
88 unsigned long address
;
89 vm_fault_t ret
= VM_FAULT_SIGBUS
;
91 unsigned long phys_addr
= (unsigned long)dev_priv
->stolen_base
+
94 page_num
= vma_pages(vma
);
95 address
= vmf
->address
- (vmf
->pgoff
<< PAGE_SHIFT
);
97 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
99 for (i
= 0; i
< page_num
; i
++) {
100 pfn
= (phys_addr
>> PAGE_SHIFT
);
102 ret
= vmf_insert_mixed(vma
, address
,
103 __pfn_to_pfn_t(pfn
, PFN_DEV
));
104 if (unlikely(ret
& VM_FAULT_ERROR
))
106 address
+= PAGE_SIZE
;
107 phys_addr
+= PAGE_SIZE
;
112 static void psbfb_vm_open(struct vm_area_struct
*vma
)
116 static void psbfb_vm_close(struct vm_area_struct
*vma
)
120 static const struct vm_operations_struct psbfb_vm_ops
= {
121 .fault
= psbfb_vm_fault
,
122 .open
= psbfb_vm_open
,
123 .close
= psbfb_vm_close
126 static int psbfb_mmap(struct fb_info
*info
, struct vm_area_struct
*vma
)
128 struct drm_fb_helper
*fb_helper
= info
->par
;
129 struct drm_framebuffer
*fb
= fb_helper
->fb
;
131 if (vma
->vm_pgoff
!= 0)
133 if (vma
->vm_pgoff
> (~0UL >> PAGE_SHIFT
))
137 * If this is a GEM object then info->screen_base is the virtual
138 * kernel remapping of the object. FIXME: Review if this is
139 * suitable for our mmap work
141 vma
->vm_ops
= &psbfb_vm_ops
;
142 vma
->vm_private_data
= (void *)fb
;
143 vma
->vm_flags
|= VM_IO
| VM_MIXEDMAP
| VM_DONTEXPAND
| VM_DONTDUMP
;
147 static const struct fb_ops psbfb_unaccel_ops
= {
148 .owner
= THIS_MODULE
,
149 DRM_FB_HELPER_DEFAULT_OPS
,
150 .fb_setcolreg
= psbfb_setcolreg
,
151 .fb_fillrect
= drm_fb_helper_cfb_fillrect
,
152 .fb_copyarea
= drm_fb_helper_cfb_copyarea
,
153 .fb_imageblit
= drm_fb_helper_cfb_imageblit
,
154 .fb_mmap
= psbfb_mmap
,
158 * psb_framebuffer_init - initialize a framebuffer
159 * @dev: our DRM device
160 * @fb: framebuffer to set up
161 * @mode_cmd: mode description
162 * @gt: backing object
164 * Configure and fill in the boilerplate for our frame buffer. Return
165 * 0 on success or an error code if we fail.
167 static int psb_framebuffer_init(struct drm_device
*dev
,
168 struct drm_framebuffer
*fb
,
169 const struct drm_mode_fb_cmd2
*mode_cmd
,
170 struct drm_gem_object
*obj
)
172 const struct drm_format_info
*info
;
176 * Reject unknown formats, YUV formats, and formats with more than
179 info
= drm_get_format_info(dev
, mode_cmd
);
180 if (!info
|| !info
->depth
|| info
->cpp
[0] > 4)
183 if (mode_cmd
->pitches
[0] & 63)
186 drm_helper_mode_fill_fb_struct(dev
, fb
, mode_cmd
);
188 ret
= drm_framebuffer_init(dev
, fb
, &psb_fb_funcs
);
190 dev_err(dev
->dev
, "framebuffer init failed: %d\n", ret
);
197 * psb_framebuffer_create - create a framebuffer backed by gt
198 * @dev: our DRM device
199 * @mode_cmd: the description of the requested mode
200 * @gt: the backing object
202 * Create a framebuffer object backed by the gt, and fill in the
203 * boilerplate required
205 * TODO: review object references
208 static struct drm_framebuffer
*psb_framebuffer_create
209 (struct drm_device
*dev
,
210 const struct drm_mode_fb_cmd2
*mode_cmd
,
211 struct drm_gem_object
*obj
)
213 struct drm_framebuffer
*fb
;
216 fb
= kzalloc(sizeof(*fb
), GFP_KERNEL
);
218 return ERR_PTR(-ENOMEM
);
220 ret
= psb_framebuffer_init(dev
, fb
, mode_cmd
, obj
);
229 * psbfb_alloc - allocate frame buffer memory
230 * @dev: the DRM device
231 * @aligned_size: space needed
233 * Allocate the frame buffer. In the usual case we get a GTT range that
234 * is stolen memory backed and life is simple. If there isn't sufficient
235 * we fail as we don't have the virtual mapping space to really vmap it
236 * and the kernel console code can't handle non linear framebuffers.
238 * Re-address this as and if the framebuffer layer grows this ability.
240 static struct gtt_range
*psbfb_alloc(struct drm_device
*dev
, int aligned_size
)
242 struct gtt_range
*backing
;
243 /* Begin by trying to use stolen memory backing */
244 backing
= psb_gtt_alloc_range(dev
, aligned_size
, "fb", 1, PAGE_SIZE
);
246 backing
->gem
.funcs
= &psb_gem_object_funcs
;
247 drm_gem_private_object_init(dev
, &backing
->gem
, aligned_size
);
254 * psbfb_create - create a framebuffer
255 * @fbdev: the framebuffer device
256 * @sizes: specification of the layout
258 * Create a framebuffer to the specifications provided
260 static int psbfb_create(struct drm_fb_helper
*fb_helper
,
261 struct drm_fb_helper_surface_size
*sizes
)
263 struct drm_device
*dev
= fb_helper
->dev
;
264 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
265 struct fb_info
*info
;
266 struct drm_framebuffer
*fb
;
267 struct drm_mode_fb_cmd2 mode_cmd
;
270 struct gtt_range
*backing
;
273 mode_cmd
.width
= sizes
->surface_width
;
274 mode_cmd
.height
= sizes
->surface_height
;
275 bpp
= sizes
->surface_bpp
;
276 depth
= sizes
->surface_depth
;
278 /* No 24bit packed */
282 mode_cmd
.pitches
[0] = ALIGN(mode_cmd
.width
* DIV_ROUND_UP(bpp
, 8), 64);
284 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
285 size
= ALIGN(size
, PAGE_SIZE
);
287 /* Allocate the framebuffer in the GTT with stolen page backing */
288 backing
= psbfb_alloc(dev
, size
);
292 memset(dev_priv
->vram_addr
+ backing
->offset
, 0, size
);
294 info
= drm_fb_helper_alloc_fbi(fb_helper
);
300 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(bpp
, depth
);
302 fb
= psb_framebuffer_create(dev
, &mode_cmd
, &backing
->gem
);
310 info
->fbops
= &psbfb_unaccel_ops
;
312 info
->fix
.smem_start
= dev
->mode_config
.fb_base
;
313 info
->fix
.smem_len
= size
;
314 info
->fix
.ywrapstep
= 0;
315 info
->fix
.ypanstep
= 0;
317 /* Accessed stolen memory directly */
318 info
->screen_base
= dev_priv
->vram_addr
+ backing
->offset
;
319 info
->screen_size
= size
;
321 if (dev_priv
->gtt
.stolen_size
) {
322 info
->apertures
->ranges
[0].base
= dev
->mode_config
.fb_base
;
323 info
->apertures
->ranges
[0].size
= dev_priv
->gtt
.stolen_size
;
326 drm_fb_helper_fill_info(info
, fb_helper
, sizes
);
328 info
->fix
.mmio_start
= pci_resource_start(dev
->pdev
, 0);
329 info
->fix
.mmio_len
= pci_resource_len(dev
->pdev
, 0);
331 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
333 dev_dbg(dev
->dev
, "allocated %dx%d fb\n", fb
->width
, fb
->height
);
337 psb_gtt_free_range(dev
, backing
);
342 * psb_user_framebuffer_create - create framebuffer
343 * @dev: our DRM device
347 * Create a new framebuffer backed by a userspace GEM object
349 static struct drm_framebuffer
*psb_user_framebuffer_create
350 (struct drm_device
*dev
, struct drm_file
*filp
,
351 const struct drm_mode_fb_cmd2
*cmd
)
353 struct drm_gem_object
*obj
;
356 * Find the GEM object and thus the gtt range object that is
359 obj
= drm_gem_object_lookup(filp
, cmd
->handles
[0]);
361 return ERR_PTR(-ENOENT
);
363 /* Let the core code do all the work */
364 return psb_framebuffer_create(dev
, cmd
, obj
);
367 static int psbfb_probe(struct drm_fb_helper
*fb_helper
,
368 struct drm_fb_helper_surface_size
*sizes
)
370 struct drm_device
*dev
= fb_helper
->dev
;
371 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
372 unsigned int fb_size
;
375 bytespp
= sizes
->surface_bpp
/ 8;
376 if (bytespp
== 3) /* no 24bit packed */
379 /* If the mode will not fit in 32bit then switch to 16bit to get
380 a console on full resolution. The X mode setting server will
381 allocate its own 32bit GEM framebuffer */
382 fb_size
= ALIGN(sizes
->surface_width
* bytespp
, 64) *
383 sizes
->surface_height
;
384 fb_size
= ALIGN(fb_size
, PAGE_SIZE
);
386 if (fb_size
> dev_priv
->vram_stolen_size
) {
387 sizes
->surface_bpp
= 16;
388 sizes
->surface_depth
= 16;
391 return psbfb_create(fb_helper
, sizes
);
394 static const struct drm_fb_helper_funcs psb_fb_helper_funcs
= {
395 .fb_probe
= psbfb_probe
,
398 static int psb_fbdev_destroy(struct drm_device
*dev
,
399 struct drm_fb_helper
*fb_helper
)
401 struct drm_framebuffer
*fb
= fb_helper
->fb
;
403 drm_fb_helper_unregister_fbi(fb_helper
);
405 drm_fb_helper_fini(fb_helper
);
406 drm_framebuffer_unregister_private(fb
);
407 drm_framebuffer_cleanup(fb
);
410 drm_gem_object_put(fb
->obj
[0]);
416 int psb_fbdev_init(struct drm_device
*dev
)
418 struct drm_fb_helper
*fb_helper
;
419 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
422 fb_helper
= kzalloc(sizeof(*fb_helper
), GFP_KERNEL
);
424 dev_err(dev
->dev
, "no memory\n");
428 dev_priv
->fb_helper
= fb_helper
;
430 drm_fb_helper_prepare(dev
, fb_helper
, &psb_fb_helper_funcs
);
432 ret
= drm_fb_helper_init(dev
, fb_helper
);
436 /* disable all the possible outputs/crtcs before entering KMS mode */
437 drm_helper_disable_unused_functions(dev
);
439 ret
= drm_fb_helper_initial_config(fb_helper
, 32);
446 drm_fb_helper_fini(fb_helper
);
452 static void psb_fbdev_fini(struct drm_device
*dev
)
454 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
456 if (!dev_priv
->fb_helper
)
459 psb_fbdev_destroy(dev
, dev_priv
->fb_helper
);
460 kfree(dev_priv
->fb_helper
);
461 dev_priv
->fb_helper
= NULL
;
464 static const struct drm_mode_config_funcs psb_mode_funcs
= {
465 .fb_create
= psb_user_framebuffer_create
,
466 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
469 static void psb_setup_outputs(struct drm_device
*dev
)
471 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
472 struct drm_connector
*connector
;
474 drm_mode_create_scaling_mode_property(dev
);
476 /* It is ok for this to fail - we just don't get backlight control */
477 if (!dev_priv
->backlight_property
)
478 dev_priv
->backlight_property
= drm_property_create_range(dev
, 0,
479 "backlight", 0, 100);
480 dev_priv
->ops
->output_init(dev
);
482 list_for_each_entry(connector
, &dev
->mode_config
.connector_list
,
484 struct gma_encoder
*gma_encoder
= gma_attached_encoder(connector
);
485 struct drm_encoder
*encoder
= &gma_encoder
->base
;
486 int crtc_mask
= 0, clone_mask
= 0;
489 switch (gma_encoder
->type
) {
490 case INTEL_OUTPUT_ANALOG
:
491 crtc_mask
= (1 << 0);
492 clone_mask
= (1 << INTEL_OUTPUT_ANALOG
);
494 case INTEL_OUTPUT_SDVO
:
495 crtc_mask
= dev_priv
->ops
->sdvo_mask
;
498 case INTEL_OUTPUT_LVDS
:
499 crtc_mask
= dev_priv
->ops
->lvds_mask
;
502 case INTEL_OUTPUT_MIPI
:
503 crtc_mask
= (1 << 0);
506 case INTEL_OUTPUT_MIPI2
:
507 crtc_mask
= (1 << 2);
510 case INTEL_OUTPUT_HDMI
:
511 crtc_mask
= dev_priv
->ops
->hdmi_mask
;
512 clone_mask
= (1 << INTEL_OUTPUT_HDMI
);
514 case INTEL_OUTPUT_DISPLAYPORT
:
515 crtc_mask
= (1 << 0) | (1 << 1);
518 case INTEL_OUTPUT_EDP
:
519 crtc_mask
= (1 << 1);
522 encoder
->possible_crtcs
= crtc_mask
;
523 encoder
->possible_clones
=
524 gma_connector_clones(dev
, clone_mask
);
528 void psb_modeset_init(struct drm_device
*dev
)
530 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
531 struct psb_intel_mode_device
*mode_dev
= &dev_priv
->mode_dev
;
534 drm_mode_config_init(dev
);
536 dev
->mode_config
.min_width
= 0;
537 dev
->mode_config
.min_height
= 0;
539 dev
->mode_config
.funcs
= &psb_mode_funcs
;
541 /* set memory base */
542 /* Oaktrail and Poulsbo should use BAR 2*/
543 pci_read_config_dword(dev
->pdev
, PSB_BSM
, (u32
*)
544 &(dev
->mode_config
.fb_base
));
546 /* num pipes is 2 for PSB but 1 for Mrst */
547 for (i
= 0; i
< dev_priv
->num_pipe
; i
++)
548 psb_intel_crtc_init(dev
, i
, mode_dev
);
550 dev
->mode_config
.max_width
= 4096;
551 dev
->mode_config
.max_height
= 4096;
553 psb_setup_outputs(dev
);
555 if (dev_priv
->ops
->errata
)
556 dev_priv
->ops
->errata(dev
);
558 dev_priv
->modeset
= true;
561 void psb_modeset_cleanup(struct drm_device
*dev
)
563 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
564 if (dev_priv
->modeset
) {
565 drm_kms_helper_poll_fini(dev
);
567 drm_mode_config_cleanup(dev
);