xref: /openbmc/linux/include/drm/drm_fb_helper.h (revision abade675e02e1b73da0c20ffaf08fbe309038298)
1 /*
2  * Copyright (c) 2006-2009 Red Hat Inc.
3  * Copyright (c) 2006-2008 Intel Corporation
4  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5  *
6  * DRM framebuffer helper functions
7  *
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.
17  *
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
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Dave Airlie <airlied@linux.ie>
28  *      Jesse Barnes <jesse.barnes@intel.com>
29  */
30 #ifndef DRM_FB_HELPER_H
31 #define DRM_FB_HELPER_H
32 
33 struct drm_fb_helper;
34 
35 #include <drm/drm_client.h>
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_device.h>
38 #include <linux/kgdb.h>
39 #include <linux/vgaarb.h>
40 
41 enum mode_set_atomic {
42 	LEAVE_ATOMIC_MODE_SET,
43 	ENTER_ATOMIC_MODE_SET,
44 };
45 
46 struct drm_fb_offset {
47 	int x, y;
48 };
49 
50 /**
51  * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size
52  * @fb_width: fbdev width
53  * @fb_height: fbdev height
54  * @surface_width: scanout buffer width
55  * @surface_height: scanout buffer height
56  * @surface_bpp: scanout buffer bpp
57  * @surface_depth: scanout buffer depth
58  *
59  * Note that the scanout surface width/height may be larger than the fbdev
60  * width/height.  In case of multiple displays, the scanout surface is sized
61  * according to the largest width/height (so it is large enough for all CRTCs
62  * to scanout).  But the fbdev width/height is sized to the minimum width/
63  * height of all the displays.  This ensures that fbcon fits on the smallest
64  * of the attached displays. fb_width/fb_height is used by
65  * drm_fb_helper_fill_info() to fill out the &fb_info.var structure.
66  */
67 struct drm_fb_helper_surface_size {
68 	u32 fb_width;
69 	u32 fb_height;
70 	u32 surface_width;
71 	u32 surface_height;
72 	u32 surface_bpp;
73 	u32 surface_depth;
74 };
75 
76 /**
77  * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library
78  *
79  * Driver callbacks used by the fbdev emulation helper library.
80  */
81 struct drm_fb_helper_funcs {
82 	/**
83 	 * @fb_probe:
84 	 *
85 	 * Driver callback to allocate and initialize the fbdev info structure.
86 	 * Furthermore it also needs to allocate the DRM framebuffer used to
87 	 * back the fbdev.
88 	 *
89 	 * This callback is mandatory.
90 	 *
91 	 * RETURNS:
92 	 *
93 	 * The driver should return 0 on success and a negative error code on
94 	 * failure.
95 	 */
96 	int (*fb_probe)(struct drm_fb_helper *helper,
97 			struct drm_fb_helper_surface_size *sizes);
98 };
99 
100 struct drm_fb_helper_connector {
101 	struct drm_connector *connector;
102 };
103 
104 /**
105  * struct drm_fb_helper - main structure to emulate fbdev on top of KMS
106  * @fb: Scanout framebuffer object
107  * @dev: DRM device
108  * @connector_count: number of connected connectors
109  * @connector_info_alloc_count: size of connector_info
110  * @funcs: driver callbacks for fb helper
111  * @fbdev: emulated fbdev device info struct
112  * @pseudo_palette: fake palette of 16 colors
113  * @dirty_clip: clip rectangle used with deferred_io to accumulate damage to
114  *              the screen buffer
115  * @dirty_lock: spinlock protecting @dirty_clip
116  * @dirty_work: worker used to flush the framebuffer
117  * @resume_work: worker used during resume if the console lock is already taken
118  *
119  * This is the main structure used by the fbdev helpers. Drivers supporting
120  * fbdev emulation should embedded this into their overall driver structure.
121  * Drivers must also fill out a &struct drm_fb_helper_funcs with a few
122  * operations.
123  */
124 struct drm_fb_helper {
125 	/**
126 	 * @client:
127 	 *
128 	 * DRM client used by the generic fbdev emulation.
129 	 */
130 	struct drm_client_dev client;
131 
132 	/**
133 	 * @buffer:
134 	 *
135 	 * Framebuffer used by the generic fbdev emulation.
136 	 */
137 	struct drm_client_buffer *buffer;
138 
139 	struct drm_framebuffer *fb;
140 	struct drm_device *dev;
141 	int connector_count;
142 	int connector_info_alloc_count;
143 	/**
144 	 * @connector_info:
145 	 *
146 	 * Array of per-connector information. Do not iterate directly, but use
147 	 * drm_fb_helper_for_each_connector.
148 	 */
149 	struct drm_fb_helper_connector **connector_info;
150 	const struct drm_fb_helper_funcs *funcs;
151 	struct fb_info *fbdev;
152 	u32 pseudo_palette[17];
153 	struct drm_clip_rect dirty_clip;
154 	spinlock_t dirty_lock;
155 	struct work_struct dirty_work;
156 	struct work_struct resume_work;
157 
158 	/**
159 	 * @lock:
160 	 *
161 	 * Top-level FBDEV helper lock. This protects all internal data
162 	 * structures and lists, such as @connector_info and @crtc_info.
163 	 *
164 	 * FIXME: fbdev emulation locking is a mess and long term we want to
165 	 * protect all helper internal state with this lock as well as reduce
166 	 * core KMS locking as much as possible.
167 	 */
168 	struct mutex lock;
169 
170 	/**
171 	 * @kernel_fb_list:
172 	 *
173 	 * Entry on the global kernel_fb_helper_list, used for kgdb entry/exit.
174 	 */
175 	struct list_head kernel_fb_list;
176 
177 	/**
178 	 * @delayed_hotplug:
179 	 *
180 	 * A hotplug was received while fbdev wasn't in control of the DRM
181 	 * device, i.e. another KMS master was active. The output configuration
182 	 * needs to be reprobe when fbdev is in control again.
183 	 */
184 	bool delayed_hotplug;
185 
186 	/**
187 	 * @deferred_setup:
188 	 *
189 	 * If no outputs are connected (disconnected or unknown) the FB helper
190 	 * code will defer setup until at least one of the outputs shows up.
191 	 * This field keeps track of the status so that setup can be retried
192 	 * at every hotplug event until it succeeds eventually.
193 	 *
194 	 * Protected by @lock.
195 	 */
196 	bool deferred_setup;
197 
198 	/**
199 	 * @preferred_bpp:
200 	 *
201 	 * Temporary storage for the driver's preferred BPP setting passed to
202 	 * FB helper initialization. This needs to be tracked so that deferred
203 	 * FB helper setup can pass this on.
204 	 *
205 	 * See also: @deferred_setup
206 	 */
207 	int preferred_bpp;
208 };
209 
210 static inline struct drm_fb_helper *
211 drm_fb_helper_from_client(struct drm_client_dev *client)
212 {
213 	return container_of(client, struct drm_fb_helper, client);
214 }
215 
216 /**
217  * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers
218  *
219  * Helper define to register default implementations of drm_fb_helper
220  * functions. To be used in struct fb_ops of drm drivers.
221  */
222 #define DRM_FB_HELPER_DEFAULT_OPS \
223 	.fb_check_var	= drm_fb_helper_check_var, \
224 	.fb_set_par	= drm_fb_helper_set_par, \
225 	.fb_setcmap	= drm_fb_helper_setcmap, \
226 	.fb_blank	= drm_fb_helper_blank, \
227 	.fb_pan_display	= drm_fb_helper_pan_display, \
228 	.fb_debug_enter = drm_fb_helper_debug_enter, \
229 	.fb_debug_leave = drm_fb_helper_debug_leave, \
230 	.fb_ioctl	= drm_fb_helper_ioctl
231 
232 #ifdef CONFIG_DRM_FBDEV_EMULATION
233 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
234 			   const struct drm_fb_helper_funcs *funcs);
235 int drm_fb_helper_init(struct drm_device *dev,
236 		       struct drm_fb_helper *helper, int max_conn);
237 void drm_fb_helper_fini(struct drm_fb_helper *helper);
238 int drm_fb_helper_blank(int blank, struct fb_info *info);
239 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
240 			      struct fb_info *info);
241 int drm_fb_helper_set_par(struct fb_info *info);
242 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
243 			    struct fb_info *info);
244 
245 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper);
246 
247 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper);
248 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper);
249 void drm_fb_helper_fill_info(struct fb_info *info,
250 			     struct drm_fb_helper *fb_helper,
251 			     struct drm_fb_helper_surface_size *sizes);
252 
253 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper);
254 
255 void drm_fb_helper_deferred_io(struct fb_info *info,
256 			       struct list_head *pagelist);
257 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper);
258 
259 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
260 			       size_t count, loff_t *ppos);
261 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
262 				size_t count, loff_t *ppos);
263 
264 void drm_fb_helper_sys_fillrect(struct fb_info *info,
265 				const struct fb_fillrect *rect);
266 void drm_fb_helper_sys_copyarea(struct fb_info *info,
267 				const struct fb_copyarea *area);
268 void drm_fb_helper_sys_imageblit(struct fb_info *info,
269 				 const struct fb_image *image);
270 
271 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
272 				const struct fb_fillrect *rect);
273 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
274 				const struct fb_copyarea *area);
275 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
276 				 const struct fb_image *image);
277 
278 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend);
279 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
280 					bool suspend);
281 
282 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info);
283 
284 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
285 			unsigned long arg);
286 
287 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper);
288 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel);
289 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper);
290 int drm_fb_helper_debug_enter(struct fb_info *info);
291 int drm_fb_helper_debug_leave(struct fb_info *info);
292 struct drm_display_mode *
293 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
294 			int width, int height);
295 struct drm_display_mode *
296 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn);
297 
298 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector);
299 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
300 				       struct drm_connector *connector);
301 
302 int drm_fb_helper_fbdev_setup(struct drm_device *dev,
303 			      struct drm_fb_helper *fb_helper,
304 			      const struct drm_fb_helper_funcs *funcs,
305 			      unsigned int preferred_bpp,
306 			      unsigned int max_conn_count);
307 void drm_fb_helper_fbdev_teardown(struct drm_device *dev);
308 
309 void drm_fb_helper_lastclose(struct drm_device *dev);
310 void drm_fb_helper_output_poll_changed(struct drm_device *dev);
311 
312 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
313 				struct drm_fb_helper_surface_size *sizes);
314 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp);
315 #else
316 static inline void drm_fb_helper_prepare(struct drm_device *dev,
317 					struct drm_fb_helper *helper,
318 					const struct drm_fb_helper_funcs *funcs)
319 {
320 }
321 
322 static inline int drm_fb_helper_init(struct drm_device *dev,
323 		       struct drm_fb_helper *helper,
324 		       int max_conn)
325 {
326 	/* So drivers can use it to free the struct */
327 	helper->dev = dev;
328 	dev->fb_helper = helper;
329 
330 	return 0;
331 }
332 
333 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper)
334 {
335 	if (helper && helper->dev)
336 		helper->dev->fb_helper = NULL;
337 }
338 
339 static inline int drm_fb_helper_blank(int blank, struct fb_info *info)
340 {
341 	return 0;
342 }
343 
344 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
345 					    struct fb_info *info)
346 {
347 	return 0;
348 }
349 
350 static inline int drm_fb_helper_set_par(struct fb_info *info)
351 {
352 	return 0;
353 }
354 
355 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
356 					  struct fb_info *info)
357 {
358 	return 0;
359 }
360 
361 static inline int
362 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
363 {
364 	return 0;
365 }
366 
367 static inline struct fb_info *
368 drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
369 {
370 	return NULL;
371 }
372 
373 static inline void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
374 {
375 }
376 
377 static inline void
378 drm_fb_helper_fill_info(struct fb_info *info,
379 			struct drm_fb_helper *fb_helper,
380 			struct drm_fb_helper_surface_size *sizes)
381 {
382 }
383 
384 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap,
385 					struct fb_info *info)
386 {
387 	return 0;
388 }
389 
390 static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
391 				      unsigned long arg)
392 {
393 	return 0;
394 }
395 
396 static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
397 {
398 }
399 
400 static inline void drm_fb_helper_deferred_io(struct fb_info *info,
401 					     struct list_head *pagelist)
402 {
403 }
404 
405 static inline int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper)
406 {
407 	return -ENODEV;
408 }
409 
410 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info,
411 					     char __user *buf, size_t count,
412 					     loff_t *ppos)
413 {
414 	return -ENODEV;
415 }
416 
417 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info,
418 					      const char __user *buf,
419 					      size_t count, loff_t *ppos)
420 {
421 	return -ENODEV;
422 }
423 
424 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info,
425 					      const struct fb_fillrect *rect)
426 {
427 }
428 
429 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info,
430 					      const struct fb_copyarea *area)
431 {
432 }
433 
434 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info,
435 					       const struct fb_image *image)
436 {
437 }
438 
439 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info,
440 					      const struct fb_fillrect *rect)
441 {
442 }
443 
444 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info,
445 					      const struct fb_copyarea *area)
446 {
447 }
448 
449 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info,
450 					       const struct fb_image *image)
451 {
452 }
453 
454 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper,
455 					     bool suspend)
456 {
457 }
458 
459 static inline void
460 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend)
461 {
462 }
463 
464 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
465 {
466 	return 0;
467 }
468 
469 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper,
470 					       int bpp_sel)
471 {
472 	return 0;
473 }
474 
475 static inline int
476 drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
477 {
478 	return 0;
479 }
480 
481 static inline int drm_fb_helper_debug_enter(struct fb_info *info)
482 {
483 	return 0;
484 }
485 
486 static inline int drm_fb_helper_debug_leave(struct fb_info *info)
487 {
488 	return 0;
489 }
490 
491 static inline struct drm_display_mode *
492 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
493 		       int width, int height)
494 {
495 	return NULL;
496 }
497 
498 static inline struct drm_display_mode *
499 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
500 		      int width, int height)
501 {
502 	return NULL;
503 }
504 
505 static inline int
506 drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
507 				struct drm_connector *connector)
508 {
509 	return 0;
510 }
511 
512 static inline int
513 drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
514 				   struct drm_connector *connector)
515 {
516 	return 0;
517 }
518 
519 static inline int
520 drm_fb_helper_fbdev_setup(struct drm_device *dev,
521 			  struct drm_fb_helper *fb_helper,
522 			  const struct drm_fb_helper_funcs *funcs,
523 			  unsigned int preferred_bpp,
524 			  unsigned int max_conn_count)
525 {
526 	/* So drivers can use it to free the struct */
527 	dev->fb_helper = fb_helper;
528 
529 	return 0;
530 }
531 
532 static inline void drm_fb_helper_fbdev_teardown(struct drm_device *dev)
533 {
534 	dev->fb_helper = NULL;
535 }
536 
537 static inline void drm_fb_helper_lastclose(struct drm_device *dev)
538 {
539 }
540 
541 static inline void drm_fb_helper_output_poll_changed(struct drm_device *dev)
542 {
543 }
544 
545 static inline int
546 drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
547 			    struct drm_fb_helper_surface_size *sizes)
548 {
549 	return 0;
550 }
551 
552 static inline int
553 drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
554 {
555 	return 0;
556 }
557 
558 #endif
559 
560 /**
561  * drm_fb_helper_remove_conflicting_framebuffers - remove firmware-configured framebuffers
562  * @a: memory range, users of which are to be removed
563  * @name: requesting driver name
564  * @primary: also kick vga16fb if present
565  *
566  * This function removes framebuffer devices (initialized by firmware/bootloader)
567  * which use memory range described by @a. If @a is NULL all such devices are
568  * removed.
569  */
570 static inline int
571 drm_fb_helper_remove_conflicting_framebuffers(struct apertures_struct *a,
572 					      const char *name, bool primary)
573 {
574 #if IS_REACHABLE(CONFIG_FB)
575 	return remove_conflicting_framebuffers(a, name, primary);
576 #else
577 	return 0;
578 #endif
579 }
580 
581 /**
582  * drm_fb_helper_remove_conflicting_pci_framebuffers - remove firmware-configured framebuffers for PCI devices
583  * @pdev: PCI device
584  * @resource_id: index of PCI BAR configuring framebuffer memory
585  * @name: requesting driver name
586  *
587  * This function removes framebuffer devices (eg. initialized by firmware)
588  * using memory range configured for @pdev's BAR @resource_id.
589  *
590  * The function assumes that PCI device with shadowed ROM drives a primary
591  * display and so kicks out vga16fb.
592  */
593 static inline int
594 drm_fb_helper_remove_conflicting_pci_framebuffers(struct pci_dev *pdev,
595 						  int resource_id,
596 						  const char *name)
597 {
598 	int ret = 0;
599 
600 	/*
601 	 * WARNING: Apparently we must kick fbdev drivers before vgacon,
602 	 * otherwise the vga fbdev driver falls over.
603 	 */
604 #if IS_REACHABLE(CONFIG_FB)
605 	ret = remove_conflicting_pci_framebuffers(pdev, resource_id, name);
606 #endif
607 	if (ret == 0)
608 		ret = vga_remove_vgacon(pdev);
609 	return ret;
610 }
611 
612 #endif
613