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 
36 struct drm_fb_offset {
37 	int x, y;
38 };
39 
40 struct drm_fb_helper_crtc {
41 	struct drm_mode_set mode_set;
42 	struct drm_display_mode *desired_mode;
43 	int x, y;
44 };
45 
46 /**
47  * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size
48  * @fb_width: fbdev width
49  * @fb_height: fbdev height
50  * @surface_width: scanout buffer width
51  * @surface_height: scanout buffer height
52  * @surface_bpp: scanout buffer bpp
53  * @surface_depth: scanout buffer depth
54  *
55  * Note that the scanout surface width/height may be larger than the fbdev
56  * width/height.  In case of multiple displays, the scanout surface is sized
57  * according to the largest width/height (so it is large enough for all CRTCs
58  * to scanout).  But the fbdev width/height is sized to the minimum width/
59  * height of all the displays.  This ensures that fbcon fits on the smallest
60  * of the attached displays.
61  *
62  * So what is passed to drm_fb_helper_fill_var() should be fb_width/fb_height,
63  * rather than the surface size.
64  */
65 struct drm_fb_helper_surface_size {
66 	u32 fb_width;
67 	u32 fb_height;
68 	u32 surface_width;
69 	u32 surface_height;
70 	u32 surface_bpp;
71 	u32 surface_depth;
72 };
73 
74 /**
75  * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library
76  * @gamma_set: Set the given gamma lut register on the given crtc.
77  * @gamma_get: Read the given gamma lut register on the given crtc, used to
78  *             save the current lut when force-restoring the fbdev for e.g.
79  *             kdbg.
80  * @fb_probe: Driver callback to allocate and initialize the fbdev info
81  *            structure. Furthermore it also needs to allocate the drm
82  *            framebuffer used to back the fbdev.
83  * @initial_config: Setup an initial fbdev display configuration
84  *
85  * Driver callbacks used by the fbdev emulation helper library.
86  */
87 struct drm_fb_helper_funcs {
88 	void (*gamma_set)(struct drm_crtc *crtc, u16 red, u16 green,
89 			  u16 blue, int regno);
90 	void (*gamma_get)(struct drm_crtc *crtc, u16 *red, u16 *green,
91 			  u16 *blue, int regno);
92 
93 	int (*fb_probe)(struct drm_fb_helper *helper,
94 			struct drm_fb_helper_surface_size *sizes);
95 	bool (*initial_config)(struct drm_fb_helper *fb_helper,
96 			       struct drm_fb_helper_crtc **crtcs,
97 			       struct drm_display_mode **modes,
98 			       struct drm_fb_offset *offsets,
99 			       bool *enabled, int width, int height);
100 };
101 
102 struct drm_fb_helper_connector {
103 	struct drm_connector *connector;
104 };
105 
106 /**
107  * struct drm_fb_helper - helper to emulate fbdev on top of kms
108  * @fb:  Scanout framebuffer object
109  * @dev:  DRM device
110  * @crtc_count: number of possible CRTCs
111  * @crtc_info: per-CRTC helper state (mode, x/y offset, etc)
112  * @connector_count: number of connected connectors
113  * @connector_info_alloc_count: size of connector_info
114  * @funcs: driver callbacks for fb helper
115  * @fbdev: emulated fbdev device info struct
116  * @pseudo_palette: fake palette of 16 colors
117  * @kernel_fb_list: list_head in kernel_fb_helper_list
118  * @delayed_hotplug: was there a hotplug while kms master active?
119  */
120 struct drm_fb_helper {
121 	struct drm_framebuffer *fb;
122 	struct drm_device *dev;
123 	int crtc_count;
124 	struct drm_fb_helper_crtc *crtc_info;
125 	int connector_count;
126 	int connector_info_alloc_count;
127 	struct drm_fb_helper_connector **connector_info;
128 	const struct drm_fb_helper_funcs *funcs;
129 	struct fb_info *fbdev;
130 	u32 pseudo_palette[17];
131 	struct list_head kernel_fb_list;
132 
133 	/* we got a hotplug but fbdev wasn't running the console
134 	   delay until next set_par */
135 	bool delayed_hotplug;
136 
137 	/**
138 	 * @atomic:
139 	 *
140 	 * Use atomic updates for restore_fbdev_mode(), etc.  This defaults to
141 	 * true if driver has DRIVER_ATOMIC feature flag, but drivers can
142 	 * override it to true after drm_fb_helper_init() if they support atomic
143 	 * modeset but do not yet advertise DRIVER_ATOMIC (note that fb-helper
144 	 * does not require ASYNC commits).
145 	 */
146 	bool atomic;
147 };
148 
149 #define CONFIG_DRM_FBDEV_EMULATION 1
150 #ifdef CONFIG_DRM_FBDEV_EMULATION
151 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
152 			   const struct drm_fb_helper_funcs *funcs);
153 int drm_fb_helper_init(struct drm_device *dev,
154 		       struct drm_fb_helper *helper, int crtc_count,
155 		       int max_conn);
156 void drm_fb_helper_fini(struct drm_fb_helper *helper);
157 int drm_fb_helper_blank(int blank, struct fb_info *info);
158 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
159 			      struct fb_info *info);
160 int drm_fb_helper_set_par(struct fb_info *info);
161 int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
162 			    struct fb_info *info);
163 
164 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper);
165 
166 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper);
167 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper);
168 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper);
169 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
170 			    uint32_t fb_width, uint32_t fb_height);
171 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
172 			    uint32_t depth);
173 
174 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper);
175 
176 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
177 			       size_t count, loff_t *ppos);
178 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
179 				size_t count, loff_t *ppos);
180 
181 void drm_fb_helper_sys_fillrect(struct fb_info *info,
182 				const struct fb_fillrect *rect);
183 void drm_fb_helper_sys_copyarea(struct fb_info *info,
184 				const struct fb_copyarea *area);
185 void drm_fb_helper_sys_imageblit(struct fb_info *info,
186 				 const struct fb_image *image);
187 
188 void drm_fb_helper_cfb_fillrect(struct fb_info *info,
189 				const struct fb_fillrect *rect);
190 void drm_fb_helper_cfb_copyarea(struct fb_info *info,
191 				const struct fb_copyarea *area);
192 void drm_fb_helper_cfb_imageblit(struct fb_info *info,
193 				 const struct fb_image *image);
194 
195 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state);
196 
197 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info);
198 
199 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper);
200 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel);
201 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper);
202 int drm_fb_helper_debug_enter(struct fb_info *info);
203 int drm_fb_helper_debug_leave(struct fb_info *info);
204 struct drm_display_mode *
205 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
206 			int width, int height);
207 struct drm_display_mode *
208 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
209 		      int width, int height);
210 
211 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector);
212 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
213 				       struct drm_connector *connector);
214 #else
215 static inline void drm_fb_helper_prepare(struct drm_device *dev,
216 					struct drm_fb_helper *helper,
217 					const struct drm_fb_helper_funcs *funcs)
218 {
219 }
220 
221 static inline int drm_fb_helper_init(struct drm_device *dev,
222 		       struct drm_fb_helper *helper, int crtc_count,
223 		       int max_conn)
224 {
225 	return 0;
226 }
227 
228 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper)
229 {
230 }
231 
232 static inline int drm_fb_helper_blank(int blank, struct fb_info *info)
233 {
234 	return 0;
235 }
236 
237 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
238 					    struct fb_info *info)
239 {
240 	return 0;
241 }
242 
243 static inline int drm_fb_helper_set_par(struct fb_info *info)
244 {
245 	return 0;
246 }
247 
248 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
249 					  struct fb_info *info)
250 {
251 	return 0;
252 }
253 
254 static inline int
255 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
256 {
257 	return 0;
258 }
259 
260 static inline struct fb_info *
261 drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
262 {
263 	return NULL;
264 }
265 
266 static inline void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
267 {
268 }
269 static inline void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper)
270 {
271 }
272 
273 static inline void drm_fb_helper_fill_var(struct fb_info *info,
274 					  struct drm_fb_helper *fb_helper,
275 					  uint32_t fb_width, uint32_t fb_height)
276 {
277 }
278 
279 static inline void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
280 					  uint32_t depth)
281 {
282 }
283 
284 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap,
285 					struct fb_info *info)
286 {
287 	return 0;
288 }
289 
290 static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
291 {
292 }
293 
294 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info,
295 					     char __user *buf, size_t count,
296 					     loff_t *ppos)
297 {
298 	return -ENODEV;
299 }
300 
301 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info,
302 					      const char __user *buf,
303 					      size_t count, loff_t *ppos)
304 {
305 	return -ENODEV;
306 }
307 
308 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info,
309 					      const struct fb_fillrect *rect)
310 {
311 }
312 
313 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info,
314 					      const struct fb_copyarea *area)
315 {
316 }
317 
318 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info,
319 					       const struct fb_image *image)
320 {
321 }
322 
323 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info,
324 					      const struct fb_fillrect *rect)
325 {
326 }
327 
328 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info,
329 					      const struct fb_copyarea *area)
330 {
331 }
332 
333 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info,
334 					       const struct fb_image *image)
335 {
336 }
337 
338 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper,
339 					     int state)
340 {
341 }
342 
343 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
344 {
345 	return 0;
346 }
347 
348 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper,
349 					       int bpp_sel)
350 {
351 	return 0;
352 }
353 
354 static inline int
355 drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
356 {
357 	return 0;
358 }
359 
360 static inline int drm_fb_helper_debug_enter(struct fb_info *info)
361 {
362 	return 0;
363 }
364 
365 static inline int drm_fb_helper_debug_leave(struct fb_info *info)
366 {
367 	return 0;
368 }
369 
370 static inline struct drm_display_mode *
371 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector,
372 		       int width, int height)
373 {
374 	return NULL;
375 }
376 
377 static inline struct drm_display_mode *
378 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
379 		      int width, int height)
380 {
381 	return NULL;
382 }
383 
384 static inline int
385 drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
386 				struct drm_connector *connector)
387 {
388 	return 0;
389 }
390 
391 static inline int
392 drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
393 				   struct drm_connector *connector)
394 {
395 	return 0;
396 }
397 #endif
398 #endif
399