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