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_crtc.h>
36 #include <linux/kgdb.h>
37 
38 enum mode_set_atomic {
39 	LEAVE_ATOMIC_MODE_SET,
40 	ENTER_ATOMIC_MODE_SET,
41 };
42 
43 struct drm_fb_offset {
44 	int x, y;
45 };
46 
47 struct drm_fb_helper_crtc {
48 	struct drm_mode_set mode_set;
49 	struct drm_display_mode *desired_mode;
50 	int x, y;
51 };
52 
53 /**
54  * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size
55  * @fb_width: fbdev width
56  * @fb_height: fbdev height
57  * @surface_width: scanout buffer width
58  * @surface_height: scanout buffer height
59  * @surface_bpp: scanout buffer bpp
60  * @surface_depth: scanout buffer depth
61  *
62  * Note that the scanout surface width/height may be larger than the fbdev
63  * width/height.  In case of multiple displays, the scanout surface is sized
64  * according to the largest width/height (so it is large enough for all CRTCs
65  * to scanout).  But the fbdev width/height is sized to the minimum width/
66  * height of all the displays.  This ensures that fbcon fits on the smallest
67  * of the attached displays.
68  *
69  * So what is passed to drm_fb_helper_fill_var() should be fb_width/fb_height,
70  * rather than the surface size.
71  */
72 struct drm_fb_helper_surface_size {
73 	u32 fb_width;
74 	u32 fb_height;
75 	u32 surface_width;
76 	u32 surface_height;
77 	u32 surface_bpp;
78 	u32 surface_depth;
79 };
80 
81 /**
82  * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library
83  *
84  * Driver callbacks used by the fbdev emulation helper library.
85  */
86 struct drm_fb_helper_funcs {
87 	/**
88 	 * @fb_probe:
89 	 *
90 	 * Driver callback to allocate and initialize the fbdev info structure.
91 	 * Furthermore it also needs to allocate the DRM framebuffer used to
92 	 * back the fbdev.
93 	 *
94 	 * This callback is mandatory.
95 	 *
96 	 * RETURNS:
97 	 *
98 	 * The driver should return 0 on success and a negative error code on
99 	 * failure.
100 	 */
101 	int (*fb_probe)(struct drm_fb_helper *helper,
102 			struct drm_fb_helper_surface_size *sizes);
103 
104 	/**
105 	 * @initial_config:
106 	 *
107 	 * Driver callback to setup an initial fbdev display configuration.
108 	 * Drivers can use this callback to tell the fbdev emulation what the
109 	 * preferred initial configuration is. This is useful to implement
110 	 * smooth booting where the fbdev (and subsequently all userspace) never
111 	 * changes the mode, but always inherits the existing configuration.
112 	 *
113 	 * This callback is optional.
114 	 *
115 	 * RETURNS:
116 	 *
117 	 * The driver should return true if a suitable initial configuration has
118 	 * been filled out and false when the fbdev helper should fall back to
119 	 * the default probing logic.
120 	 */
121 	bool (*initial_config)(struct drm_fb_helper *fb_helper,
122 			       struct drm_fb_helper_crtc **crtcs,
123 			       struct drm_display_mode **modes,
124 			       struct drm_fb_offset *offsets,
125 			       bool *enabled, int width, int height);
126 };
127 
128 struct drm_fb_helper_connector {
129 	struct drm_connector *connector;
130 };
131 
132 /**
133  * struct drm_fb_helper - main structure to emulate fbdev on top of KMS
134  * @fb: Scanout framebuffer object
135  * @dev: DRM device
136  * @crtc_count: number of possible CRTCs
137  * @crtc_info: per-CRTC helper state (mode, x/y offset, etc)
138  * @connector_count: number of connected connectors
139  * @connector_info_alloc_count: size of connector_info
140  * @funcs: driver callbacks for fb helper
141  * @fbdev: emulated fbdev device info struct
142  * @pseudo_palette: fake palette of 16 colors
143  * @dirty_clip: clip rectangle used with deferred_io to accumulate damage to
144  *              the screen buffer
145  * @dirty_lock: spinlock protecting @dirty_clip
146  * @dirty_work: worker used to flush the framebuffer
147  * @resume_work: worker used during resume if the console lock is already taken
148  *
149  * This is the main structure used by the fbdev helpers. Drivers supporting
150  * fbdev emulation should embedded this into their overall driver structure.
151  * Drivers must also fill out a &struct drm_fb_helper_funcs with a few
152  * operations.
153  */
154 struct drm_fb_helper {
155 	struct drm_framebuffer *fb;
156 	struct drm_device *dev;
157 	int crtc_count;
158 	struct drm_fb_helper_crtc *crtc_info;
159 	int connector_count;
160 	int connector_info_alloc_count;
161 	/**
162 	 * @connector_info:
163 	 *
164 	 * Array of per-connector information. Do not iterate directly, but use
165 	 * drm_fb_helper_for_each_connector.
166 	 */
167 	struct drm_fb_helper_connector **connector_info;
168 	const struct drm_fb_helper_funcs *funcs;
169 	struct fb_info *fbdev;
170 	u32 pseudo_palette[17];
171 	struct drm_clip_rect dirty_clip;
172 	spinlock_t dirty_lock;
173 	struct work_struct dirty_work;
174 	struct work_struct resume_work;
175 
176 	/**
177 	 * @lock:
178 	 *
179 	 * Top-level FBDEV helper lock. This protects all internal data
180 	 * structures and lists, such as @connector_info and @crtc_info.
181 	 *
182 	 * FIXME: fbdev emulation locking is a mess and long term we want to
183 	 * protect all helper internal state with this lock as well as reduce
184 	 * core KMS locking as much as possible.
185 	 */
186 	struct lock lock;
187 
188 	/**
189 	 * @kernel_fb_list:
190 	 *
191 	 * Entry on the global kernel_fb_helper_list, used for kgdb entry/exit.
192 	 */
193 	struct list_head kernel_fb_list;
194 
195 	/**
196 	 * @delayed_hotplug:
197 	 *
198 	 * A hotplug was received while fbdev wasn't in control of the DRM
199 	 * device, i.e. another KMS master was active. The output configuration
200 	 * needs to be reprobe when fbdev is in control again.
201 	 */
202 	bool delayed_hotplug;
203 
204 	/**
205 	 * @deferred_setup:
206 	 *
207 	 * If no outputs are connected (disconnected or unknown) the FB helper
208 	 * code will defer setup until at least one of the outputs shows up.
209 	 * This field keeps track of the status so that setup can be retried
210 	 * at every hotplug event until it succeeds eventually.
211 	 *
212 	 * Protected by @lock.
213 	 */
214 	bool deferred_setup;
215 
216 	/**
217 	 * @preferred_bpp:
218 	 *
219 	 * Temporary storage for the driver's preferred BPP setting passed to
220 	 * FB helper initialization. This needs to be tracked so that deferred
221 	 * FB helper setup can pass this on.
222 	 *
223 	 * See also: @deferred_setup
224 	 */
225 	int preferred_bpp;
226 };
227 
228 /**
229  * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers
230  *
231  * Helper define to register default implementations of drm_fb_helper
232  * functions. To be used in struct fb_ops of drm drivers.
233  */
234 #define DRM_FB_HELPER_DEFAULT_OPS \
235 	.fb_check_var	= drm_fb_helper_check_var, \
236 	.fb_set_par	= drm_fb_helper_set_par, \
237 	.fb_setcmap	= drm_fb_helper_setcmap, \
238 	.fb_blank	= drm_fb_helper_blank, \
239 	.fb_pan_display	= drm_fb_helper_pan_display, \
240 	.fb_debug_enter = drm_fb_helper_debug_enter, \
241 	.fb_debug_leave = drm_fb_helper_debug_leave, \
242 	.fb_ioctl	= drm_fb_helper_ioctl
243 
244 #ifdef CONFIG_DRM_FBDEV_EMULATION
245 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
246 			   const struct drm_fb_helper_funcs *funcs);
247 int drm_fb_helper_init(struct drm_device *dev,
248 		       struct drm_fb_helper *helper, int max_conn);
249 void drm_fb_helper_fini(struct drm_fb_helper *helper);
250 int drm_fb_helper_blank(int blank, struct fb_info *info);
251 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
252 			      struct fb_info *info);
253 int drm_fb_helper_set_par(struct fb_info *info);
254 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
255 			    struct fb_info *info);
256 
257 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper);
258 
259 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper);
260 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper);
261 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
262 			    uint32_t fb_width, uint32_t fb_height);
263 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
264 			    uint32_t depth);
265 
266 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper);
267 
268 void drm_fb_helper_deferred_io(struct fb_info *info,
269 			       struct list_head *pagelist);
270 
271 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
272 			       size_t count, loff_t *ppos);
273 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
274 				size_t count, loff_t *ppos);
275 
276 void drm_fb_helper_sys_fillrect(struct fb_info *info,
277 				const struct fb_fillrect *rect);
278 void drm_fb_helper_sys_copyarea(struct fb_info *info,
279 				const struct fb_copyarea *area);
280 void drm_fb_helper_sys_imageblit(struct fb_info *info,
281 				 const struct fb_image *image);
282 
283 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
284 				const struct fb_fillrect *rect);
285 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
286 				const struct fb_copyarea *area);
287 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
288 				 const struct fb_image *image);
289 
290 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend);
291 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
292 					bool suspend);
293 
294 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info);
295 
296 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
297 			unsigned long arg);
298 
299 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper);
300 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel);
301 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper);
302 int drm_fb_helper_debug_enter(struct fb_info *info);
303 int drm_fb_helper_debug_leave(struct fb_info *info);
304 struct drm_display_mode *
305 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
306 			int width, int height);
307 struct drm_display_mode *
308 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn);
309 
310 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector);
311 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
312 				       struct drm_connector *connector);
313 #else
314 static inline void drm_fb_helper_prepare(struct drm_device *dev,
315 					struct drm_fb_helper *helper,
316 					const struct drm_fb_helper_funcs *funcs)
317 {
318 }
319 
320 static inline int drm_fb_helper_init(struct drm_device *dev,
321 		       struct drm_fb_helper *helper,
322 		       int max_conn)
323 {
324 	return 0;
325 }
326 
327 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper)
328 {
329 }
330 
331 static inline int drm_fb_helper_blank(int blank, struct fb_info *info)
332 {
333 	return 0;
334 }
335 
336 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
337 					    struct fb_info *info)
338 {
339 	return 0;
340 }
341 
342 static inline int drm_fb_helper_set_par(struct fb_info *info)
343 {
344 	return 0;
345 }
346 
347 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
348 					  struct fb_info *info)
349 {
350 	return 0;
351 }
352 
353 static inline int
354 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
355 {
356 	return 0;
357 }
358 
359 static inline struct fb_info *
360 drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
361 {
362 	return NULL;
363 }
364 
365 static inline void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
366 {
367 }
368 
369 static inline void drm_fb_helper_fill_var(struct fb_info *info,
370 					  struct drm_fb_helper *fb_helper,
371 					  uint32_t fb_width, uint32_t fb_height)
372 {
373 }
374 
375 static inline void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
376 					  uint32_t depth)
377 {
378 }
379 
380 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap,
381 					struct fb_info *info)
382 {
383 	return 0;
384 }
385 
386 static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
387 				      unsigned long arg)
388 {
389 	return 0;
390 }
391 
392 static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
393 {
394 }
395 
396 static inline void drm_fb_helper_deferred_io(struct fb_info *info,
397 					     struct list_head *pagelist)
398 {
399 }
400 
401 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info,
402 					     char __user *buf, size_t count,
403 					     loff_t *ppos)
404 {
405 	return -ENODEV;
406 }
407 
408 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info,
409 					      const char __user *buf,
410 					      size_t count, loff_t *ppos)
411 {
412 	return -ENODEV;
413 }
414 
415 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info,
416 					      const struct fb_fillrect *rect)
417 {
418 }
419 
420 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info,
421 					      const struct fb_copyarea *area)
422 {
423 }
424 
425 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info,
426 					       const struct fb_image *image)
427 {
428 }
429 
430 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info,
431 					      const struct fb_fillrect *rect)
432 {
433 }
434 
435 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info,
436 					      const struct fb_copyarea *area)
437 {
438 }
439 
440 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info,
441 					       const struct fb_image *image)
442 {
443 }
444 
445 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper,
446 					     bool suspend)
447 {
448 }
449 
450 static inline void
451 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend)
452 {
453 }
454 
455 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
456 {
457 	return 0;
458 }
459 
460 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper,
461 					       int bpp_sel)
462 {
463 	return 0;
464 }
465 
466 static inline int
467 drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
468 {
469 	return 0;
470 }
471 
472 static inline int drm_fb_helper_debug_enter(struct fb_info *info)
473 {
474 	return 0;
475 }
476 
477 static inline int drm_fb_helper_debug_leave(struct fb_info *info)
478 {
479 	return 0;
480 }
481 
482 static inline struct drm_display_mode *
483 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
484 		       int width, int height)
485 {
486 	return NULL;
487 }
488 
489 static inline struct drm_display_mode *
490 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
491 		      int width, int height)
492 {
493 	return NULL;
494 }
495 
496 static inline int
497 drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
498 				struct drm_connector *connector)
499 {
500 	return 0;
501 }
502 
503 static inline int
504 drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
505 				   struct drm_connector *connector)
506 {
507 	return 0;
508 }
509 
510 #endif
511 
512 static inline int
513 drm_fb_helper_remove_conflicting_framebuffers(struct apertures_struct *a,
514 					      const char *name, bool primary)
515 {
516 #if IS_ENABLED(CONFIG_FB)
517 	return remove_conflicting_framebuffers(a, name, primary);
518 #else
519 	return 0;
520 #endif
521 }
522 
523 #endif
524