2 * Copyright (c) 2006-2009 Red Hat Inc.
3 * Copyright (c) 2006-2008 Intel Corporation
4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
6 * DRM framebuffer helper functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
27 * Dave Airlie <airlied@linux.ie>
28 * Jesse Barnes <jesse.barnes@intel.com>
30 #ifndef DRM_FB_HELPER_H
31 #define DRM_FB_HELPER_H
35 #include <drm/drm_client.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_device.h>
38 #include <linux/kgdb.h>
40 enum mode_set_atomic
{
41 LEAVE_ATOMIC_MODE_SET
,
42 ENTER_ATOMIC_MODE_SET
,
45 struct drm_fb_offset
{
49 struct drm_fb_helper_crtc
{
50 struct drm_mode_set mode_set
;
51 struct drm_display_mode
*desired_mode
;
57 * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size
58 * @fb_width: fbdev width
59 * @fb_height: fbdev height
60 * @surface_width: scanout buffer width
61 * @surface_height: scanout buffer height
62 * @surface_bpp: scanout buffer bpp
63 * @surface_depth: scanout buffer depth
65 * Note that the scanout surface width/height may be larger than the fbdev
66 * width/height. In case of multiple displays, the scanout surface is sized
67 * according to the largest width/height (so it is large enough for all CRTCs
68 * to scanout). But the fbdev width/height is sized to the minimum width/
69 * height of all the displays. This ensures that fbcon fits on the smallest
70 * of the attached displays. fb_width/fb_height is used by
71 * drm_fb_helper_fill_info() to fill out the &fb_info.var structure.
73 struct drm_fb_helper_surface_size
{
83 * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library
85 * Driver callbacks used by the fbdev emulation helper library.
87 struct drm_fb_helper_funcs
{
91 * Driver callback to allocate and initialize the fbdev info structure.
92 * Furthermore it also needs to allocate the DRM framebuffer used to
95 * This callback is mandatory.
99 * The driver should return 0 on success and a negative error code on
102 int (*fb_probe
)(struct drm_fb_helper
*helper
,
103 struct drm_fb_helper_surface_size
*sizes
);
108 * Driver callback to setup an initial fbdev display configuration.
109 * Drivers can use this callback to tell the fbdev emulation what the
110 * preferred initial configuration is. This is useful to implement
111 * smooth booting where the fbdev (and subsequently all userspace) never
112 * changes the mode, but always inherits the existing configuration.
114 * This callback is optional.
118 * The driver should return true if a suitable initial configuration has
119 * been filled out and false when the fbdev helper should fall back to
120 * the default probing logic.
122 bool (*initial_config
)(struct drm_fb_helper
*fb_helper
,
123 struct drm_fb_helper_crtc
**crtcs
,
124 struct drm_display_mode
**modes
,
125 struct drm_fb_offset
*offsets
,
126 bool *enabled
, int width
, int height
);
129 struct drm_fb_helper_connector
{
130 struct drm_connector
*connector
;
134 * struct drm_fb_helper - main structure to emulate fbdev on top of KMS
135 * @fb: Scanout framebuffer object
137 * @crtc_count: number of possible CRTCs
138 * @crtc_info: per-CRTC helper state (mode, x/y offset, etc)
139 * @connector_count: number of connected connectors
140 * @connector_info_alloc_count: size of connector_info
141 * @funcs: driver callbacks for fb helper
142 * @fbdev: emulated fbdev device info struct
143 * @pseudo_palette: fake palette of 16 colors
144 * @dirty_clip: clip rectangle used with deferred_io to accumulate damage to
146 * @dirty_lock: spinlock protecting @dirty_clip
147 * @dirty_work: worker used to flush the framebuffer
148 * @resume_work: worker used during resume if the console lock is already taken
150 * This is the main structure used by the fbdev helpers. Drivers supporting
151 * fbdev emulation should embedded this into their overall driver structure.
152 * Drivers must also fill out a &struct drm_fb_helper_funcs with a few
155 struct drm_fb_helper
{
159 * DRM client used by the generic fbdev emulation.
161 struct drm_client_dev client
;
166 * Framebuffer used by the generic fbdev emulation.
168 struct drm_client_buffer
*buffer
;
170 struct drm_framebuffer
*fb
;
171 struct drm_device
*dev
;
173 struct drm_fb_helper_crtc
*crtc_info
;
175 int connector_info_alloc_count
;
178 * Bitmask of all rotations requested for panel-orientation which
179 * could not be handled in hardware. If only one bit is set
180 * fbdev->fbcon_rotate_hint gets set to the requested rotation.
186 * Array of per-connector information. Do not iterate directly, but use
187 * drm_fb_helper_for_each_connector.
189 struct drm_fb_helper_connector
**connector_info
;
190 const struct drm_fb_helper_funcs
*funcs
;
191 struct fb_info
*fbdev
;
192 u32 pseudo_palette
[17];
193 struct drm_clip_rect dirty_clip
;
194 spinlock_t dirty_lock
;
195 struct work_struct dirty_work
;
196 struct work_struct resume_work
;
201 * Top-level FBDEV helper lock. This protects all internal data
202 * structures and lists, such as @connector_info and @crtc_info.
204 * FIXME: fbdev emulation locking is a mess and long term we want to
205 * protect all helper internal state with this lock as well as reduce
206 * core KMS locking as much as possible.
213 * Entry on the global kernel_fb_helper_list, used for kgdb entry/exit.
215 struct list_head kernel_fb_list
;
220 * A hotplug was received while fbdev wasn't in control of the DRM
221 * device, i.e. another KMS master was active. The output configuration
222 * needs to be reprobe when fbdev is in control again.
224 bool delayed_hotplug
;
229 * If no outputs are connected (disconnected or unknown) the FB helper
230 * code will defer setup until at least one of the outputs shows up.
231 * This field keeps track of the status so that setup can be retried
232 * at every hotplug event until it succeeds eventually.
234 * Protected by @lock.
241 * Temporary storage for the driver's preferred BPP setting passed to
242 * FB helper initialization. This needs to be tracked so that deferred
243 * FB helper setup can pass this on.
245 * See also: @deferred_setup
250 static inline struct drm_fb_helper
*
251 drm_fb_helper_from_client(struct drm_client_dev
*client
)
253 return container_of(client
, struct drm_fb_helper
, client
);
257 * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers
259 * Helper define to register default implementations of drm_fb_helper
260 * functions. To be used in struct fb_ops of drm drivers.
262 #define DRM_FB_HELPER_DEFAULT_OPS \
263 .fb_check_var = drm_fb_helper_check_var, \
264 .fb_set_par = drm_fb_helper_set_par, \
265 .fb_setcmap = drm_fb_helper_setcmap, \
266 .fb_blank = drm_fb_helper_blank, \
267 .fb_pan_display = drm_fb_helper_pan_display, \
268 .fb_debug_enter = drm_fb_helper_debug_enter, \
269 .fb_debug_leave = drm_fb_helper_debug_leave, \
270 .fb_ioctl = drm_fb_helper_ioctl
272 #ifdef CONFIG_DRM_FBDEV_EMULATION
273 void drm_fb_helper_prepare(struct drm_device
*dev
, struct drm_fb_helper
*helper
,
274 const struct drm_fb_helper_funcs
*funcs
);
275 int drm_fb_helper_init(struct drm_device
*dev
,
276 struct drm_fb_helper
*helper
, int max_conn
);
277 void drm_fb_helper_fini(struct drm_fb_helper
*helper
);
278 int drm_fb_helper_blank(int blank
, struct fb_info
*info
);
279 int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
280 struct fb_info
*info
);
281 int drm_fb_helper_set_par(struct fb_info
*info
);
282 int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
283 struct fb_info
*info
);
285 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
);
287 struct fb_info
*drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
);
288 void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
);
289 void drm_fb_helper_fill_info(struct fb_info
*info
,
290 struct drm_fb_helper
*fb_helper
,
291 struct drm_fb_helper_surface_size
*sizes
);
293 void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
);
295 void drm_fb_helper_deferred_io(struct fb_info
*info
,
296 struct list_head
*pagelist
);
297 int drm_fb_helper_defio_init(struct drm_fb_helper
*fb_helper
);
299 ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
, char __user
*buf
,
300 size_t count
, loff_t
*ppos
);
301 ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
, const char __user
*buf
,
302 size_t count
, loff_t
*ppos
);
304 void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
305 const struct fb_fillrect
*rect
);
306 void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
307 const struct fb_copyarea
*area
);
308 void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
309 const struct fb_image
*image
);
311 void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
312 const struct fb_fillrect
*rect
);
313 void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
314 const struct fb_copyarea
*area
);
315 void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
316 const struct fb_image
*image
);
318 void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
, bool suspend
);
319 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
,
322 int drm_fb_helper_setcmap(struct fb_cmap
*cmap
, struct fb_info
*info
);
324 int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
327 int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
);
328 int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
, int bpp_sel
);
329 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
);
330 int drm_fb_helper_debug_enter(struct fb_info
*info
);
331 int drm_fb_helper_debug_leave(struct fb_info
*info
);
332 struct drm_display_mode
*
333 drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
,
334 int width
, int height
);
335 struct drm_display_mode
*
336 drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
);
338 int drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
, struct drm_connector
*connector
);
339 int drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
340 struct drm_connector
*connector
);
342 int drm_fb_helper_fbdev_setup(struct drm_device
*dev
,
343 struct drm_fb_helper
*fb_helper
,
344 const struct drm_fb_helper_funcs
*funcs
,
345 unsigned int preferred_bpp
,
346 unsigned int max_conn_count
);
347 void drm_fb_helper_fbdev_teardown(struct drm_device
*dev
);
349 void drm_fb_helper_lastclose(struct drm_device
*dev
);
350 void drm_fb_helper_output_poll_changed(struct drm_device
*dev
);
352 int drm_fb_helper_generic_probe(struct drm_fb_helper
*fb_helper
,
353 struct drm_fb_helper_surface_size
*sizes
);
354 int drm_fbdev_generic_setup(struct drm_device
*dev
, unsigned int preferred_bpp
);
356 static inline void drm_fb_helper_prepare(struct drm_device
*dev
,
357 struct drm_fb_helper
*helper
,
358 const struct drm_fb_helper_funcs
*funcs
)
362 static inline int drm_fb_helper_init(struct drm_device
*dev
,
363 struct drm_fb_helper
*helper
,
366 /* So drivers can use it to free the struct */
368 dev
->fb_helper
= helper
;
373 static inline void drm_fb_helper_fini(struct drm_fb_helper
*helper
)
375 if (helper
&& helper
->dev
)
376 helper
->dev
->fb_helper
= NULL
;
379 static inline int drm_fb_helper_blank(int blank
, struct fb_info
*info
)
384 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo
*var
,
385 struct fb_info
*info
)
390 static inline int drm_fb_helper_set_par(struct fb_info
*info
)
395 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo
*var
,
396 struct fb_info
*info
)
402 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper
*fb_helper
)
407 static inline struct fb_info
*
408 drm_fb_helper_alloc_fbi(struct drm_fb_helper
*fb_helper
)
413 static inline void drm_fb_helper_unregister_fbi(struct drm_fb_helper
*fb_helper
)
418 drm_fb_helper_fill_info(struct fb_info
*info
,
419 struct drm_fb_helper
*fb_helper
,
420 struct drm_fb_helper_surface_size
*sizes
)
424 static inline int drm_fb_helper_setcmap(struct fb_cmap
*cmap
,
425 struct fb_info
*info
)
430 static inline int drm_fb_helper_ioctl(struct fb_info
*info
, unsigned int cmd
,
436 static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper
*fb_helper
)
440 static inline void drm_fb_helper_deferred_io(struct fb_info
*info
,
441 struct list_head
*pagelist
)
445 static inline int drm_fb_helper_defio_init(struct drm_fb_helper
*fb_helper
)
450 static inline ssize_t
drm_fb_helper_sys_read(struct fb_info
*info
,
451 char __user
*buf
, size_t count
,
457 static inline ssize_t
drm_fb_helper_sys_write(struct fb_info
*info
,
458 const char __user
*buf
,
459 size_t count
, loff_t
*ppos
)
464 static inline void drm_fb_helper_sys_fillrect(struct fb_info
*info
,
465 const struct fb_fillrect
*rect
)
469 static inline void drm_fb_helper_sys_copyarea(struct fb_info
*info
,
470 const struct fb_copyarea
*area
)
474 static inline void drm_fb_helper_sys_imageblit(struct fb_info
*info
,
475 const struct fb_image
*image
)
479 static inline void drm_fb_helper_cfb_fillrect(struct fb_info
*info
,
480 const struct fb_fillrect
*rect
)
484 static inline void drm_fb_helper_cfb_copyarea(struct fb_info
*info
,
485 const struct fb_copyarea
*area
)
489 static inline void drm_fb_helper_cfb_imageblit(struct fb_info
*info
,
490 const struct fb_image
*image
)
494 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper
*fb_helper
,
500 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper
*fb_helper
, bool suspend
)
504 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper
*fb_helper
)
509 static inline int drm_fb_helper_initial_config(struct drm_fb_helper
*fb_helper
,
516 drm_fb_helper_single_add_all_connectors(struct drm_fb_helper
*fb_helper
)
521 static inline int drm_fb_helper_debug_enter(struct fb_info
*info
)
526 static inline int drm_fb_helper_debug_leave(struct fb_info
*info
)
531 static inline struct drm_display_mode
*
532 drm_has_preferred_mode(struct drm_fb_helper_connector
*fb_connector
,
533 int width
, int height
)
538 static inline struct drm_display_mode
*
539 drm_pick_cmdline_mode(struct drm_fb_helper_connector
*fb_helper_conn
,
540 int width
, int height
)
546 drm_fb_helper_add_one_connector(struct drm_fb_helper
*fb_helper
,
547 struct drm_connector
*connector
)
553 drm_fb_helper_remove_one_connector(struct drm_fb_helper
*fb_helper
,
554 struct drm_connector
*connector
)
560 drm_fb_helper_fbdev_setup(struct drm_device
*dev
,
561 struct drm_fb_helper
*fb_helper
,
562 const struct drm_fb_helper_funcs
*funcs
,
563 unsigned int preferred_bpp
,
564 unsigned int max_conn_count
)
566 /* So drivers can use it to free the struct */
567 dev
->fb_helper
= fb_helper
;
572 static inline void drm_fb_helper_fbdev_teardown(struct drm_device
*dev
)
574 dev
->fb_helper
= NULL
;
577 static inline void drm_fb_helper_lastclose(struct drm_device
*dev
)
581 static inline void drm_fb_helper_output_poll_changed(struct drm_device
*dev
)
586 drm_fb_helper_generic_probe(struct drm_fb_helper
*fb_helper
,
587 struct drm_fb_helper_surface_size
*sizes
)
593 drm_fbdev_generic_setup(struct drm_device
*dev
, unsigned int preferred_bpp
)
601 * drm_fb_helper_remove_conflicting_framebuffers - remove firmware-configured framebuffers
602 * @a: memory range, users of which are to be removed
603 * @name: requesting driver name
604 * @primary: also kick vga16fb if present
606 * This function removes framebuffer devices (initialized by firmware/bootloader)
607 * which use memory range described by @a. If @a is NULL all such devices are
611 drm_fb_helper_remove_conflicting_framebuffers(struct apertures_struct
*a
,
612 const char *name
, bool primary
)
614 #if IS_REACHABLE(CONFIG_FB)
615 return remove_conflicting_framebuffers(a
, name
, primary
);
622 * drm_fb_helper_remove_conflicting_pci_framebuffers - remove firmware-configured framebuffers for PCI devices
624 * @resource_id: index of PCI BAR configuring framebuffer memory
625 * @name: requesting driver name
627 * This function removes framebuffer devices (eg. initialized by firmware)
628 * using memory range configured for @pdev's BAR @resource_id.
630 * The function assumes that PCI device with shadowed ROM drives a primary
631 * display and so kicks out vga16fb.
634 drm_fb_helper_remove_conflicting_pci_framebuffers(struct pci_dev
*pdev
,
638 #if IS_REACHABLE(CONFIG_FB)
639 return remove_conflicting_pci_framebuffers(pdev
, resource_id
, name
);