1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2006-2009 Red Hat Inc.
3*4882a593Smuzhiyun * Copyright (c) 2006-2008 Intel Corporation
4*4882a593Smuzhiyun * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * DRM framebuffer helper functions
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Permission to use, copy, modify, distribute, and sell this software and its
9*4882a593Smuzhiyun * documentation for any purpose is hereby granted without fee, provided that
10*4882a593Smuzhiyun * the above copyright notice appear in all copies and that both that copyright
11*4882a593Smuzhiyun * notice and this permission notice appear in supporting documentation, and
12*4882a593Smuzhiyun * that the name of the copyright holders not be used in advertising or
13*4882a593Smuzhiyun * publicity pertaining to distribution of the software without specific,
14*4882a593Smuzhiyun * written prior permission. The copyright holders make no representations
15*4882a593Smuzhiyun * about the suitability of this software for any purpose. It is provided "as
16*4882a593Smuzhiyun * is" without express or implied warranty.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19*4882a593Smuzhiyun * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20*4882a593Smuzhiyun * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21*4882a593Smuzhiyun * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22*4882a593Smuzhiyun * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23*4882a593Smuzhiyun * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24*4882a593Smuzhiyun * OF THIS SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Authors:
27*4882a593Smuzhiyun * Dave Airlie <airlied@linux.ie>
28*4882a593Smuzhiyun * Jesse Barnes <jesse.barnes@intel.com>
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include <linux/console.h>
33*4882a593Smuzhiyun #include <linux/dma-buf.h>
34*4882a593Smuzhiyun #include <linux/kernel.h>
35*4882a593Smuzhiyun #include <linux/module.h>
36*4882a593Smuzhiyun #include <linux/slab.h>
37*4882a593Smuzhiyun #include <linux/sysrq.h>
38*4882a593Smuzhiyun #include <linux/vmalloc.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <drm/drm_atomic.h>
41*4882a593Smuzhiyun #include <drm/drm_crtc.h>
42*4882a593Smuzhiyun #include <drm/drm_crtc_helper.h>
43*4882a593Smuzhiyun #include <drm/drm_drv.h>
44*4882a593Smuzhiyun #include <drm/drm_fb_helper.h>
45*4882a593Smuzhiyun #include <drm/drm_fourcc.h>
46*4882a593Smuzhiyun #include <drm/drm_print.h>
47*4882a593Smuzhiyun #include <drm/drm_vblank.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun #include "drm_crtc_helper_internal.h"
50*4882a593Smuzhiyun #include "drm_internal.h"
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun static bool drm_fbdev_emulation = true;
53*4882a593Smuzhiyun module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
54*4882a593Smuzhiyun MODULE_PARM_DESC(fbdev_emulation,
55*4882a593Smuzhiyun "Enable legacy fbdev emulation [default=true]");
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
58*4882a593Smuzhiyun module_param(drm_fbdev_overalloc, int, 0444);
59*4882a593Smuzhiyun MODULE_PARM_DESC(drm_fbdev_overalloc,
60*4882a593Smuzhiyun "Overallocation of the fbdev buffer (%) [default="
61*4882a593Smuzhiyun __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun * In order to keep user-space compatibility, we want in certain use-cases
65*4882a593Smuzhiyun * to keep leaking the fbdev physical address to the user-space program
66*4882a593Smuzhiyun * handling the fbdev buffer.
67*4882a593Smuzhiyun * This is a bad habit essentially kept into closed source opengl driver
68*4882a593Smuzhiyun * that should really be moved into open-source upstream projects instead
69*4882a593Smuzhiyun * of using legacy physical addresses in user space to communicate with
70*4882a593Smuzhiyun * other out-of-tree kernel modules.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * This module_param *should* be removed as soon as possible and be
73*4882a593Smuzhiyun * considered as a broken and legacy behaviour from a modern fbdev device.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
76*4882a593Smuzhiyun static bool drm_leak_fbdev_smem = false;
77*4882a593Smuzhiyun module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
78*4882a593Smuzhiyun MODULE_PARM_DESC(drm_leak_fbdev_smem,
79*4882a593Smuzhiyun "Allow unsafe leaking fbdev physical smem address [default=false]");
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun static LIST_HEAD(kernel_fb_helper_list);
83*4882a593Smuzhiyun static DEFINE_MUTEX(kernel_fb_helper_lock);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun * DOC: fbdev helpers
87*4882a593Smuzhiyun *
88*4882a593Smuzhiyun * The fb helper functions are useful to provide an fbdev on top of a drm kernel
89*4882a593Smuzhiyun * mode setting driver. They can be used mostly independently from the crtc
90*4882a593Smuzhiyun * helper functions used by many drivers to implement the kernel mode setting
91*4882a593Smuzhiyun * interfaces.
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Drivers that support a dumb buffer with a virtual address and mmap support,
94*4882a593Smuzhiyun * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
95*4882a593Smuzhiyun * It will automatically set up deferred I/O if the driver requires a shadow
96*4882a593Smuzhiyun * buffer.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * At runtime drivers should restore the fbdev console by using
99*4882a593Smuzhiyun * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
100*4882a593Smuzhiyun * They should also notify the fb helper code from updates to the output
101*4882a593Smuzhiyun * configuration by using drm_fb_helper_output_poll_changed() as their
102*4882a593Smuzhiyun * &drm_mode_config_funcs.output_poll_changed callback.
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * For suspend/resume consider using drm_mode_config_helper_suspend() and
105*4882a593Smuzhiyun * drm_mode_config_helper_resume() which takes care of fbdev as well.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * All other functions exported by the fb helper library can be used to
108*4882a593Smuzhiyun * implement the fbdev driver interface by the driver.
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * It is possible, though perhaps somewhat tricky, to implement race-free
111*4882a593Smuzhiyun * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
112*4882a593Smuzhiyun * helper must be called first to initialize the minimum required to make
113*4882a593Smuzhiyun * hotplug detection work. Drivers also need to make sure to properly set up
114*4882a593Smuzhiyun * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
115*4882a593Smuzhiyun * it is safe to enable interrupts and start processing hotplug events. At the
116*4882a593Smuzhiyun * same time, drivers should initialize all modeset objects such as CRTCs,
117*4882a593Smuzhiyun * encoders and connectors. To finish up the fbdev helper initialization, the
118*4882a593Smuzhiyun * drm_fb_helper_init() function is called. To probe for all attached displays
119*4882a593Smuzhiyun * and set up an initial configuration using the detected hardware, drivers
120*4882a593Smuzhiyun * should call drm_fb_helper_initial_config().
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * If &drm_framebuffer_funcs.dirty is set, the
123*4882a593Smuzhiyun * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
124*4882a593Smuzhiyun * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
125*4882a593Smuzhiyun * away. This worker then calls the dirty() function ensuring that it will
126*4882a593Smuzhiyun * always run in process context since the fb_*() function could be running in
127*4882a593Smuzhiyun * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
128*4882a593Smuzhiyun * callback it will also schedule dirty_work with the damage collected from the
129*4882a593Smuzhiyun * mmap page writes.
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * Deferred I/O is not compatible with SHMEM. Such drivers should request an
132*4882a593Smuzhiyun * fbdev shadow buffer and call drm_fbdev_generic_setup() instead.
133*4882a593Smuzhiyun */
134*4882a593Smuzhiyun
drm_fb_helper_restore_lut_atomic(struct drm_crtc * crtc)135*4882a593Smuzhiyun static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun uint16_t *r_base, *g_base, *b_base;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun if (crtc->funcs->gamma_set == NULL)
140*4882a593Smuzhiyun return;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun r_base = crtc->gamma_store;
143*4882a593Smuzhiyun g_base = r_base + crtc->gamma_size;
144*4882a593Smuzhiyun b_base = g_base + crtc->gamma_size;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
147*4882a593Smuzhiyun crtc->gamma_size, NULL);
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
152*4882a593Smuzhiyun * @info: fbdev registered by the helper
153*4882a593Smuzhiyun */
drm_fb_helper_debug_enter(struct fb_info * info)154*4882a593Smuzhiyun int drm_fb_helper_debug_enter(struct fb_info *info)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun struct drm_fb_helper *helper = info->par;
157*4882a593Smuzhiyun const struct drm_crtc_helper_funcs *funcs;
158*4882a593Smuzhiyun struct drm_mode_set *mode_set;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
161*4882a593Smuzhiyun mutex_lock(&helper->client.modeset_mutex);
162*4882a593Smuzhiyun drm_client_for_each_modeset(mode_set, &helper->client) {
163*4882a593Smuzhiyun if (!mode_set->crtc->enabled)
164*4882a593Smuzhiyun continue;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun funcs = mode_set->crtc->helper_private;
167*4882a593Smuzhiyun if (funcs->mode_set_base_atomic == NULL)
168*4882a593Smuzhiyun continue;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
171*4882a593Smuzhiyun continue;
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun funcs->mode_set_base_atomic(mode_set->crtc,
174*4882a593Smuzhiyun mode_set->fb,
175*4882a593Smuzhiyun mode_set->x,
176*4882a593Smuzhiyun mode_set->y,
177*4882a593Smuzhiyun ENTER_ATOMIC_MODE_SET);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun mutex_unlock(&helper->client.modeset_mutex);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun return 0;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_debug_enter);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
188*4882a593Smuzhiyun * @info: fbdev registered by the helper
189*4882a593Smuzhiyun */
drm_fb_helper_debug_leave(struct fb_info * info)190*4882a593Smuzhiyun int drm_fb_helper_debug_leave(struct fb_info *info)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun struct drm_fb_helper *helper = info->par;
193*4882a593Smuzhiyun struct drm_client_dev *client = &helper->client;
194*4882a593Smuzhiyun struct drm_device *dev = helper->dev;
195*4882a593Smuzhiyun struct drm_crtc *crtc;
196*4882a593Smuzhiyun const struct drm_crtc_helper_funcs *funcs;
197*4882a593Smuzhiyun struct drm_mode_set *mode_set;
198*4882a593Smuzhiyun struct drm_framebuffer *fb;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun mutex_lock(&client->modeset_mutex);
201*4882a593Smuzhiyun drm_client_for_each_modeset(mode_set, client) {
202*4882a593Smuzhiyun crtc = mode_set->crtc;
203*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(crtc->dev))
204*4882a593Smuzhiyun continue;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun funcs = crtc->helper_private;
207*4882a593Smuzhiyun fb = crtc->primary->fb;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!crtc->enabled)
210*4882a593Smuzhiyun continue;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (!fb) {
213*4882a593Smuzhiyun drm_err(dev, "no fb to restore?\n");
214*4882a593Smuzhiyun continue;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (funcs->mode_set_base_atomic == NULL)
218*4882a593Smuzhiyun continue;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun drm_fb_helper_restore_lut_atomic(mode_set->crtc);
221*4882a593Smuzhiyun funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
222*4882a593Smuzhiyun crtc->y, LEAVE_ATOMIC_MODE_SET);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun mutex_unlock(&client->modeset_mutex);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_debug_leave);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun static int
__drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper * fb_helper,bool force)231*4882a593Smuzhiyun __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper,
232*4882a593Smuzhiyun bool force)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun bool do_delayed;
235*4882a593Smuzhiyun int ret;
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (!drm_fbdev_emulation || !fb_helper)
238*4882a593Smuzhiyun return -ENODEV;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun if (READ_ONCE(fb_helper->deferred_setup))
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
244*4882a593Smuzhiyun if (force) {
245*4882a593Smuzhiyun /*
246*4882a593Smuzhiyun * Yes this is the _locked version which expects the master lock
247*4882a593Smuzhiyun * to be held. But for forced restores we're intentionally
248*4882a593Smuzhiyun * racing here, see drm_fb_helper_set_par().
249*4882a593Smuzhiyun */
250*4882a593Smuzhiyun ret = drm_client_modeset_commit_locked(&fb_helper->client);
251*4882a593Smuzhiyun } else {
252*4882a593Smuzhiyun ret = drm_client_modeset_commit(&fb_helper->client);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun do_delayed = fb_helper->delayed_hotplug;
256*4882a593Smuzhiyun if (do_delayed)
257*4882a593Smuzhiyun fb_helper->delayed_hotplug = false;
258*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun if (do_delayed)
261*4882a593Smuzhiyun drm_fb_helper_hotplug_event(fb_helper);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return ret;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun /**
267*4882a593Smuzhiyun * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
268*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
269*4882a593Smuzhiyun *
270*4882a593Smuzhiyun * This should be called from driver's drm &drm_driver.lastclose callback
271*4882a593Smuzhiyun * when implementing an fbcon on top of kms using this helper. This ensures that
272*4882a593Smuzhiyun * the user isn't greeted with a black screen when e.g. X dies.
273*4882a593Smuzhiyun *
274*4882a593Smuzhiyun * RETURNS:
275*4882a593Smuzhiyun * Zero if everything went ok, negative error code otherwise.
276*4882a593Smuzhiyun */
drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper * fb_helper)277*4882a593Smuzhiyun int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, false);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun #ifdef CONFIG_MAGIC_SYSRQ
284*4882a593Smuzhiyun /*
285*4882a593Smuzhiyun * restore fbcon display for all kms driver's using this helper, used for sysrq
286*4882a593Smuzhiyun * and panic handling.
287*4882a593Smuzhiyun */
drm_fb_helper_force_kernel_mode(void)288*4882a593Smuzhiyun static bool drm_fb_helper_force_kernel_mode(void)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun bool ret, error = false;
291*4882a593Smuzhiyun struct drm_fb_helper *helper;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun if (list_empty(&kernel_fb_helper_list))
294*4882a593Smuzhiyun return false;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
297*4882a593Smuzhiyun struct drm_device *dev = helper->dev;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
300*4882a593Smuzhiyun continue;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun mutex_lock(&helper->lock);
303*4882a593Smuzhiyun ret = drm_client_modeset_commit_locked(&helper->client);
304*4882a593Smuzhiyun if (ret)
305*4882a593Smuzhiyun error = true;
306*4882a593Smuzhiyun mutex_unlock(&helper->lock);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun return error;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
drm_fb_helper_restore_work_fn(struct work_struct * ignored)311*4882a593Smuzhiyun static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun bool ret;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun ret = drm_fb_helper_force_kernel_mode();
316*4882a593Smuzhiyun if (ret == true)
317*4882a593Smuzhiyun DRM_ERROR("Failed to restore crtc configuration\n");
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
320*4882a593Smuzhiyun
drm_fb_helper_sysrq(int dummy1)321*4882a593Smuzhiyun static void drm_fb_helper_sysrq(int dummy1)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun schedule_work(&drm_fb_helper_restore_work);
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
327*4882a593Smuzhiyun .handler = drm_fb_helper_sysrq,
328*4882a593Smuzhiyun .help_msg = "force-fb(v)",
329*4882a593Smuzhiyun .action_msg = "Restore framebuffer console",
330*4882a593Smuzhiyun };
331*4882a593Smuzhiyun #else
332*4882a593Smuzhiyun static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
333*4882a593Smuzhiyun #endif
334*4882a593Smuzhiyun
drm_fb_helper_dpms(struct fb_info * info,int dpms_mode)335*4882a593Smuzhiyun static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
340*4882a593Smuzhiyun drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
341*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
346*4882a593Smuzhiyun * @blank: desired blanking state
347*4882a593Smuzhiyun * @info: fbdev registered by the helper
348*4882a593Smuzhiyun */
drm_fb_helper_blank(int blank,struct fb_info * info)349*4882a593Smuzhiyun int drm_fb_helper_blank(int blank, struct fb_info *info)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun if (oops_in_progress)
352*4882a593Smuzhiyun return -EBUSY;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun switch (blank) {
355*4882a593Smuzhiyun /* Display: On; HSync: On, VSync: On */
356*4882a593Smuzhiyun case FB_BLANK_UNBLANK:
357*4882a593Smuzhiyun drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
358*4882a593Smuzhiyun break;
359*4882a593Smuzhiyun /* Display: Off; HSync: On, VSync: On */
360*4882a593Smuzhiyun case FB_BLANK_NORMAL:
361*4882a593Smuzhiyun drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
362*4882a593Smuzhiyun break;
363*4882a593Smuzhiyun /* Display: Off; HSync: Off, VSync: On */
364*4882a593Smuzhiyun case FB_BLANK_HSYNC_SUSPEND:
365*4882a593Smuzhiyun drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
366*4882a593Smuzhiyun break;
367*4882a593Smuzhiyun /* Display: Off; HSync: On, VSync: Off */
368*4882a593Smuzhiyun case FB_BLANK_VSYNC_SUSPEND:
369*4882a593Smuzhiyun drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
370*4882a593Smuzhiyun break;
371*4882a593Smuzhiyun /* Display: Off; HSync: Off, VSync: Off */
372*4882a593Smuzhiyun case FB_BLANK_POWERDOWN:
373*4882a593Smuzhiyun drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
374*4882a593Smuzhiyun break;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun return 0;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_blank);
379*4882a593Smuzhiyun
drm_fb_helper_resume_worker(struct work_struct * work)380*4882a593Smuzhiyun static void drm_fb_helper_resume_worker(struct work_struct *work)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
383*4882a593Smuzhiyun resume_work);
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun console_lock();
386*4882a593Smuzhiyun fb_set_suspend(helper->fbdev, 0);
387*4882a593Smuzhiyun console_unlock();
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
drm_fb_helper_dirty_blit_real(struct drm_fb_helper * fb_helper,struct drm_clip_rect * clip)390*4882a593Smuzhiyun static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
391*4882a593Smuzhiyun struct drm_clip_rect *clip)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct drm_framebuffer *fb = fb_helper->fb;
394*4882a593Smuzhiyun unsigned int cpp = fb->format->cpp[0];
395*4882a593Smuzhiyun size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
396*4882a593Smuzhiyun void *src = fb_helper->fbdev->screen_buffer + offset;
397*4882a593Smuzhiyun void *dst = fb_helper->buffer->vaddr + offset;
398*4882a593Smuzhiyun size_t len = (clip->x2 - clip->x1) * cpp;
399*4882a593Smuzhiyun unsigned int y;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun for (y = clip->y1; y < clip->y2; y++) {
402*4882a593Smuzhiyun if (!fb_helper->dev->mode_config.fbdev_use_iomem)
403*4882a593Smuzhiyun memcpy(dst, src, len);
404*4882a593Smuzhiyun else
405*4882a593Smuzhiyun memcpy_toio((void __iomem *)dst, src, len);
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun src += fb->pitches[0];
408*4882a593Smuzhiyun dst += fb->pitches[0];
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun
drm_fb_helper_dirty_work(struct work_struct * work)412*4882a593Smuzhiyun static void drm_fb_helper_dirty_work(struct work_struct *work)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
415*4882a593Smuzhiyun dirty_work);
416*4882a593Smuzhiyun struct drm_clip_rect *clip = &helper->dirty_clip;
417*4882a593Smuzhiyun struct drm_clip_rect clip_copy;
418*4882a593Smuzhiyun unsigned long flags;
419*4882a593Smuzhiyun void *vaddr;
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun spin_lock_irqsave(&helper->dirty_lock, flags);
422*4882a593Smuzhiyun clip_copy = *clip;
423*4882a593Smuzhiyun clip->x1 = clip->y1 = ~0;
424*4882a593Smuzhiyun clip->x2 = clip->y2 = 0;
425*4882a593Smuzhiyun spin_unlock_irqrestore(&helper->dirty_lock, flags);
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* call dirty callback only when it has been really touched */
428*4882a593Smuzhiyun if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) {
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun /* Generic fbdev uses a shadow buffer */
431*4882a593Smuzhiyun if (helper->buffer) {
432*4882a593Smuzhiyun vaddr = drm_client_buffer_vmap(helper->buffer);
433*4882a593Smuzhiyun if (IS_ERR(vaddr))
434*4882a593Smuzhiyun return;
435*4882a593Smuzhiyun drm_fb_helper_dirty_blit_real(helper, &clip_copy);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun if (helper->fb->funcs->dirty)
438*4882a593Smuzhiyun helper->fb->funcs->dirty(helper->fb, NULL, 0, 0,
439*4882a593Smuzhiyun &clip_copy, 1);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun if (helper->buffer)
442*4882a593Smuzhiyun drm_client_buffer_vunmap(helper->buffer);
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun /**
447*4882a593Smuzhiyun * drm_fb_helper_prepare - setup a drm_fb_helper structure
448*4882a593Smuzhiyun * @dev: DRM device
449*4882a593Smuzhiyun * @helper: driver-allocated fbdev helper structure to set up
450*4882a593Smuzhiyun * @funcs: pointer to structure of functions associate with this helper
451*4882a593Smuzhiyun *
452*4882a593Smuzhiyun * Sets up the bare minimum to make the framebuffer helper usable. This is
453*4882a593Smuzhiyun * useful to implement race-free initialization of the polling helpers.
454*4882a593Smuzhiyun */
drm_fb_helper_prepare(struct drm_device * dev,struct drm_fb_helper * helper,const struct drm_fb_helper_funcs * funcs)455*4882a593Smuzhiyun void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
456*4882a593Smuzhiyun const struct drm_fb_helper_funcs *funcs)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun INIT_LIST_HEAD(&helper->kernel_fb_list);
459*4882a593Smuzhiyun spin_lock_init(&helper->dirty_lock);
460*4882a593Smuzhiyun INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
461*4882a593Smuzhiyun INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
462*4882a593Smuzhiyun helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
463*4882a593Smuzhiyun mutex_init(&helper->lock);
464*4882a593Smuzhiyun helper->funcs = funcs;
465*4882a593Smuzhiyun helper->dev = dev;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_prepare);
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /**
470*4882a593Smuzhiyun * drm_fb_helper_init - initialize a &struct drm_fb_helper
471*4882a593Smuzhiyun * @dev: drm device
472*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper structure to initialize
473*4882a593Smuzhiyun *
474*4882a593Smuzhiyun * This allocates the structures for the fbdev helper with the given limits.
475*4882a593Smuzhiyun * Note that this won't yet touch the hardware (through the driver interfaces)
476*4882a593Smuzhiyun * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
477*4882a593Smuzhiyun * to allow driver writes more control over the exact init sequence.
478*4882a593Smuzhiyun *
479*4882a593Smuzhiyun * Drivers must call drm_fb_helper_prepare() before calling this function.
480*4882a593Smuzhiyun *
481*4882a593Smuzhiyun * RETURNS:
482*4882a593Smuzhiyun * Zero if everything went ok, nonzero otherwise.
483*4882a593Smuzhiyun */
drm_fb_helper_init(struct drm_device * dev,struct drm_fb_helper * fb_helper)484*4882a593Smuzhiyun int drm_fb_helper_init(struct drm_device *dev,
485*4882a593Smuzhiyun struct drm_fb_helper *fb_helper)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun int ret;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun if (!drm_fbdev_emulation) {
490*4882a593Smuzhiyun dev->fb_helper = fb_helper;
491*4882a593Smuzhiyun return 0;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /*
495*4882a593Smuzhiyun * If this is not the generic fbdev client, initialize a drm_client
496*4882a593Smuzhiyun * without callbacks so we can use the modesets.
497*4882a593Smuzhiyun */
498*4882a593Smuzhiyun if (!fb_helper->client.funcs) {
499*4882a593Smuzhiyun ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL);
500*4882a593Smuzhiyun if (ret)
501*4882a593Smuzhiyun return ret;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun dev->fb_helper = fb_helper;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun return 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_init);
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
512*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * A helper to alloc fb_info and the members cmap and apertures. Called
515*4882a593Smuzhiyun * by the driver within the fb_probe fb_helper callback function. Drivers do not
516*4882a593Smuzhiyun * need to release the allocated fb_info structure themselves, this is
517*4882a593Smuzhiyun * automatically done when calling drm_fb_helper_fini().
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * RETURNS:
520*4882a593Smuzhiyun * fb_info pointer if things went okay, pointer containing error code
521*4882a593Smuzhiyun * otherwise
522*4882a593Smuzhiyun */
drm_fb_helper_alloc_fbi(struct drm_fb_helper * fb_helper)523*4882a593Smuzhiyun struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun struct device *dev = fb_helper->dev->dev;
526*4882a593Smuzhiyun struct fb_info *info;
527*4882a593Smuzhiyun int ret;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun info = framebuffer_alloc(0, dev);
530*4882a593Smuzhiyun if (!info)
531*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun ret = fb_alloc_cmap(&info->cmap, 256, 0);
534*4882a593Smuzhiyun if (ret)
535*4882a593Smuzhiyun goto err_release;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /*
538*4882a593Smuzhiyun * TODO: We really should be smarter here and alloc an apperture
539*4882a593Smuzhiyun * for each IORESOURCE_MEM resource helper->dev->dev has and also
540*4882a593Smuzhiyun * init the ranges of the appertures based on the resources.
541*4882a593Smuzhiyun * Note some drivers currently count on there being only 1 empty
542*4882a593Smuzhiyun * aperture and fill this themselves, these will need to be dealt
543*4882a593Smuzhiyun * with somehow when fixing this.
544*4882a593Smuzhiyun */
545*4882a593Smuzhiyun info->apertures = alloc_apertures(1);
546*4882a593Smuzhiyun if (!info->apertures) {
547*4882a593Smuzhiyun ret = -ENOMEM;
548*4882a593Smuzhiyun goto err_free_cmap;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun fb_helper->fbdev = info;
552*4882a593Smuzhiyun info->skip_vt_switch = true;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun return info;
555*4882a593Smuzhiyun
556*4882a593Smuzhiyun err_free_cmap:
557*4882a593Smuzhiyun fb_dealloc_cmap(&info->cmap);
558*4882a593Smuzhiyun err_release:
559*4882a593Smuzhiyun framebuffer_release(info);
560*4882a593Smuzhiyun return ERR_PTR(ret);
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun /**
565*4882a593Smuzhiyun * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
566*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
567*4882a593Smuzhiyun *
568*4882a593Smuzhiyun * A wrapper around unregister_framebuffer, to release the fb_info
569*4882a593Smuzhiyun * framebuffer device. This must be called before releasing all resources for
570*4882a593Smuzhiyun * @fb_helper by calling drm_fb_helper_fini().
571*4882a593Smuzhiyun */
drm_fb_helper_unregister_fbi(struct drm_fb_helper * fb_helper)572*4882a593Smuzhiyun void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun if (fb_helper && fb_helper->fbdev)
575*4882a593Smuzhiyun unregister_framebuffer(fb_helper->fbdev);
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
578*4882a593Smuzhiyun
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun * drm_fb_helper_fini - finialize a &struct drm_fb_helper
581*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
582*4882a593Smuzhiyun *
583*4882a593Smuzhiyun * This cleans up all remaining resources associated with @fb_helper.
584*4882a593Smuzhiyun */
drm_fb_helper_fini(struct drm_fb_helper * fb_helper)585*4882a593Smuzhiyun void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun struct fb_info *info;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun if (!fb_helper)
590*4882a593Smuzhiyun return;
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun fb_helper->dev->fb_helper = NULL;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun if (!drm_fbdev_emulation)
595*4882a593Smuzhiyun return;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun cancel_work_sync(&fb_helper->resume_work);
598*4882a593Smuzhiyun cancel_work_sync(&fb_helper->dirty_work);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun info = fb_helper->fbdev;
601*4882a593Smuzhiyun if (info) {
602*4882a593Smuzhiyun if (info->cmap.len)
603*4882a593Smuzhiyun fb_dealloc_cmap(&info->cmap);
604*4882a593Smuzhiyun framebuffer_release(info);
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun fb_helper->fbdev = NULL;
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun mutex_lock(&kernel_fb_helper_lock);
609*4882a593Smuzhiyun if (!list_empty(&fb_helper->kernel_fb_list)) {
610*4882a593Smuzhiyun list_del(&fb_helper->kernel_fb_list);
611*4882a593Smuzhiyun if (list_empty(&kernel_fb_helper_list))
612*4882a593Smuzhiyun unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun mutex_unlock(&kernel_fb_helper_lock);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun mutex_destroy(&fb_helper->lock);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun if (!fb_helper->client.funcs)
619*4882a593Smuzhiyun drm_client_release(&fb_helper->client);
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_fini);
622*4882a593Smuzhiyun
drm_fbdev_use_shadow_fb(struct drm_fb_helper * fb_helper)623*4882a593Smuzhiyun static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
626*4882a593Smuzhiyun struct drm_framebuffer *fb = fb_helper->fb;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun return dev->mode_config.prefer_shadow_fbdev ||
629*4882a593Smuzhiyun dev->mode_config.prefer_shadow ||
630*4882a593Smuzhiyun fb->funcs->dirty;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
drm_fb_helper_dirty(struct fb_info * info,u32 x,u32 y,u32 width,u32 height)633*4882a593Smuzhiyun static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
634*4882a593Smuzhiyun u32 width, u32 height)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun struct drm_fb_helper *helper = info->par;
637*4882a593Smuzhiyun struct drm_clip_rect *clip = &helper->dirty_clip;
638*4882a593Smuzhiyun unsigned long flags;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun if (!drm_fbdev_use_shadow_fb(helper))
641*4882a593Smuzhiyun return;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun spin_lock_irqsave(&helper->dirty_lock, flags);
644*4882a593Smuzhiyun clip->x1 = min_t(u32, clip->x1, x);
645*4882a593Smuzhiyun clip->y1 = min_t(u32, clip->y1, y);
646*4882a593Smuzhiyun clip->x2 = max_t(u32, clip->x2, x + width);
647*4882a593Smuzhiyun clip->y2 = max_t(u32, clip->y2, y + height);
648*4882a593Smuzhiyun spin_unlock_irqrestore(&helper->dirty_lock, flags);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun schedule_work(&helper->dirty_work);
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /**
654*4882a593Smuzhiyun * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
655*4882a593Smuzhiyun * @info: fb_info struct pointer
656*4882a593Smuzhiyun * @pagelist: list of dirty mmap framebuffer pages
657*4882a593Smuzhiyun *
658*4882a593Smuzhiyun * This function is used as the &fb_deferred_io.deferred_io
659*4882a593Smuzhiyun * callback function for flushing the fbdev mmap writes.
660*4882a593Smuzhiyun */
drm_fb_helper_deferred_io(struct fb_info * info,struct list_head * pagelist)661*4882a593Smuzhiyun void drm_fb_helper_deferred_io(struct fb_info *info,
662*4882a593Smuzhiyun struct list_head *pagelist)
663*4882a593Smuzhiyun {
664*4882a593Smuzhiyun unsigned long start, end, min, max;
665*4882a593Smuzhiyun struct page *page;
666*4882a593Smuzhiyun u32 y1, y2;
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun min = ULONG_MAX;
669*4882a593Smuzhiyun max = 0;
670*4882a593Smuzhiyun list_for_each_entry(page, pagelist, lru) {
671*4882a593Smuzhiyun start = page->index << PAGE_SHIFT;
672*4882a593Smuzhiyun end = start + PAGE_SIZE - 1;
673*4882a593Smuzhiyun min = min(min, start);
674*4882a593Smuzhiyun max = max(max, end);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun if (min < max) {
678*4882a593Smuzhiyun y1 = min / info->fix.line_length;
679*4882a593Smuzhiyun y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
680*4882a593Smuzhiyun info->var.yres);
681*4882a593Smuzhiyun drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_deferred_io);
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun /**
687*4882a593Smuzhiyun * drm_fb_helper_sys_read - wrapper around fb_sys_read
688*4882a593Smuzhiyun * @info: fb_info struct pointer
689*4882a593Smuzhiyun * @buf: userspace buffer to read from framebuffer memory
690*4882a593Smuzhiyun * @count: number of bytes to read from framebuffer memory
691*4882a593Smuzhiyun * @ppos: read offset within framebuffer memory
692*4882a593Smuzhiyun *
693*4882a593Smuzhiyun * A wrapper around fb_sys_read implemented by fbdev core
694*4882a593Smuzhiyun */
drm_fb_helper_sys_read(struct fb_info * info,char __user * buf,size_t count,loff_t * ppos)695*4882a593Smuzhiyun ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
696*4882a593Smuzhiyun size_t count, loff_t *ppos)
697*4882a593Smuzhiyun {
698*4882a593Smuzhiyun return fb_sys_read(info, buf, count, ppos);
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_sys_read);
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /**
703*4882a593Smuzhiyun * drm_fb_helper_sys_write - wrapper around fb_sys_write
704*4882a593Smuzhiyun * @info: fb_info struct pointer
705*4882a593Smuzhiyun * @buf: userspace buffer to write to framebuffer memory
706*4882a593Smuzhiyun * @count: number of bytes to write to framebuffer memory
707*4882a593Smuzhiyun * @ppos: write offset within framebuffer memory
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * A wrapper around fb_sys_write implemented by fbdev core
710*4882a593Smuzhiyun */
drm_fb_helper_sys_write(struct fb_info * info,const char __user * buf,size_t count,loff_t * ppos)711*4882a593Smuzhiyun ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
712*4882a593Smuzhiyun size_t count, loff_t *ppos)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun ssize_t ret;
715*4882a593Smuzhiyun
716*4882a593Smuzhiyun ret = fb_sys_write(info, buf, count, ppos);
717*4882a593Smuzhiyun if (ret > 0)
718*4882a593Smuzhiyun drm_fb_helper_dirty(info, 0, 0, info->var.xres,
719*4882a593Smuzhiyun info->var.yres);
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun return ret;
722*4882a593Smuzhiyun }
723*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_sys_write);
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /**
726*4882a593Smuzhiyun * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
727*4882a593Smuzhiyun * @info: fbdev registered by the helper
728*4882a593Smuzhiyun * @rect: info about rectangle to fill
729*4882a593Smuzhiyun *
730*4882a593Smuzhiyun * A wrapper around sys_fillrect implemented by fbdev core
731*4882a593Smuzhiyun */
drm_fb_helper_sys_fillrect(struct fb_info * info,const struct fb_fillrect * rect)732*4882a593Smuzhiyun void drm_fb_helper_sys_fillrect(struct fb_info *info,
733*4882a593Smuzhiyun const struct fb_fillrect *rect)
734*4882a593Smuzhiyun {
735*4882a593Smuzhiyun sys_fillrect(info, rect);
736*4882a593Smuzhiyun drm_fb_helper_dirty(info, rect->dx, rect->dy,
737*4882a593Smuzhiyun rect->width, rect->height);
738*4882a593Smuzhiyun }
739*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
740*4882a593Smuzhiyun
741*4882a593Smuzhiyun /**
742*4882a593Smuzhiyun * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
743*4882a593Smuzhiyun * @info: fbdev registered by the helper
744*4882a593Smuzhiyun * @area: info about area to copy
745*4882a593Smuzhiyun *
746*4882a593Smuzhiyun * A wrapper around sys_copyarea implemented by fbdev core
747*4882a593Smuzhiyun */
drm_fb_helper_sys_copyarea(struct fb_info * info,const struct fb_copyarea * area)748*4882a593Smuzhiyun void drm_fb_helper_sys_copyarea(struct fb_info *info,
749*4882a593Smuzhiyun const struct fb_copyarea *area)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun sys_copyarea(info, area);
752*4882a593Smuzhiyun drm_fb_helper_dirty(info, area->dx, area->dy,
753*4882a593Smuzhiyun area->width, area->height);
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /**
758*4882a593Smuzhiyun * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
759*4882a593Smuzhiyun * @info: fbdev registered by the helper
760*4882a593Smuzhiyun * @image: info about image to blit
761*4882a593Smuzhiyun *
762*4882a593Smuzhiyun * A wrapper around sys_imageblit implemented by fbdev core
763*4882a593Smuzhiyun */
drm_fb_helper_sys_imageblit(struct fb_info * info,const struct fb_image * image)764*4882a593Smuzhiyun void drm_fb_helper_sys_imageblit(struct fb_info *info,
765*4882a593Smuzhiyun const struct fb_image *image)
766*4882a593Smuzhiyun {
767*4882a593Smuzhiyun sys_imageblit(info, image);
768*4882a593Smuzhiyun drm_fb_helper_dirty(info, image->dx, image->dy,
769*4882a593Smuzhiyun image->width, image->height);
770*4882a593Smuzhiyun }
771*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /**
774*4882a593Smuzhiyun * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
775*4882a593Smuzhiyun * @info: fbdev registered by the helper
776*4882a593Smuzhiyun * @rect: info about rectangle to fill
777*4882a593Smuzhiyun *
778*4882a593Smuzhiyun * A wrapper around cfb_fillrect implemented by fbdev core
779*4882a593Smuzhiyun */
drm_fb_helper_cfb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)780*4882a593Smuzhiyun void drm_fb_helper_cfb_fillrect(struct fb_info *info,
781*4882a593Smuzhiyun const struct fb_fillrect *rect)
782*4882a593Smuzhiyun {
783*4882a593Smuzhiyun cfb_fillrect(info, rect);
784*4882a593Smuzhiyun drm_fb_helper_dirty(info, rect->dx, rect->dy,
785*4882a593Smuzhiyun rect->width, rect->height);
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /**
790*4882a593Smuzhiyun * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
791*4882a593Smuzhiyun * @info: fbdev registered by the helper
792*4882a593Smuzhiyun * @area: info about area to copy
793*4882a593Smuzhiyun *
794*4882a593Smuzhiyun * A wrapper around cfb_copyarea implemented by fbdev core
795*4882a593Smuzhiyun */
drm_fb_helper_cfb_copyarea(struct fb_info * info,const struct fb_copyarea * area)796*4882a593Smuzhiyun void drm_fb_helper_cfb_copyarea(struct fb_info *info,
797*4882a593Smuzhiyun const struct fb_copyarea *area)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun cfb_copyarea(info, area);
800*4882a593Smuzhiyun drm_fb_helper_dirty(info, area->dx, area->dy,
801*4882a593Smuzhiyun area->width, area->height);
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun /**
806*4882a593Smuzhiyun * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
807*4882a593Smuzhiyun * @info: fbdev registered by the helper
808*4882a593Smuzhiyun * @image: info about image to blit
809*4882a593Smuzhiyun *
810*4882a593Smuzhiyun * A wrapper around cfb_imageblit implemented by fbdev core
811*4882a593Smuzhiyun */
drm_fb_helper_cfb_imageblit(struct fb_info * info,const struct fb_image * image)812*4882a593Smuzhiyun void drm_fb_helper_cfb_imageblit(struct fb_info *info,
813*4882a593Smuzhiyun const struct fb_image *image)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun cfb_imageblit(info, image);
816*4882a593Smuzhiyun drm_fb_helper_dirty(info, image->dx, image->dy,
817*4882a593Smuzhiyun image->width, image->height);
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
820*4882a593Smuzhiyun
821*4882a593Smuzhiyun /**
822*4882a593Smuzhiyun * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
823*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
824*4882a593Smuzhiyun * @suspend: whether to suspend or resume
825*4882a593Smuzhiyun *
826*4882a593Smuzhiyun * A wrapper around fb_set_suspend implemented by fbdev core.
827*4882a593Smuzhiyun * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
828*4882a593Smuzhiyun * the lock yourself
829*4882a593Smuzhiyun */
drm_fb_helper_set_suspend(struct drm_fb_helper * fb_helper,bool suspend)830*4882a593Smuzhiyun void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun if (fb_helper && fb_helper->fbdev)
833*4882a593Smuzhiyun fb_set_suspend(fb_helper->fbdev, suspend);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_set_suspend);
836*4882a593Smuzhiyun
837*4882a593Smuzhiyun /**
838*4882a593Smuzhiyun * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
839*4882a593Smuzhiyun * takes the console lock
840*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
841*4882a593Smuzhiyun * @suspend: whether to suspend or resume
842*4882a593Smuzhiyun *
843*4882a593Smuzhiyun * A wrapper around fb_set_suspend() that takes the console lock. If the lock
844*4882a593Smuzhiyun * isn't available on resume, a worker is tasked with waiting for the lock
845*4882a593Smuzhiyun * to become available. The console lock can be pretty contented on resume
846*4882a593Smuzhiyun * due to all the printk activity.
847*4882a593Smuzhiyun *
848*4882a593Smuzhiyun * This function can be called multiple times with the same state since
849*4882a593Smuzhiyun * &fb_info.state is checked to see if fbdev is running or not before locking.
850*4882a593Smuzhiyun *
851*4882a593Smuzhiyun * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
852*4882a593Smuzhiyun */
drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper * fb_helper,bool suspend)853*4882a593Smuzhiyun void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
854*4882a593Smuzhiyun bool suspend)
855*4882a593Smuzhiyun {
856*4882a593Smuzhiyun if (!fb_helper || !fb_helper->fbdev)
857*4882a593Smuzhiyun return;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun /* make sure there's no pending/ongoing resume */
860*4882a593Smuzhiyun flush_work(&fb_helper->resume_work);
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun if (suspend) {
863*4882a593Smuzhiyun if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
864*4882a593Smuzhiyun return;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun console_lock();
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun } else {
869*4882a593Smuzhiyun if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
870*4882a593Smuzhiyun return;
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun if (!console_trylock()) {
873*4882a593Smuzhiyun schedule_work(&fb_helper->resume_work);
874*4882a593Smuzhiyun return;
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun fb_set_suspend(fb_helper->fbdev, suspend);
879*4882a593Smuzhiyun console_unlock();
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
882*4882a593Smuzhiyun
setcmap_pseudo_palette(struct fb_cmap * cmap,struct fb_info * info)883*4882a593Smuzhiyun static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun u32 *palette = (u32 *)info->pseudo_palette;
886*4882a593Smuzhiyun int i;
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun if (cmap->start + cmap->len > 16)
889*4882a593Smuzhiyun return -EINVAL;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun for (i = 0; i < cmap->len; ++i) {
892*4882a593Smuzhiyun u16 red = cmap->red[i];
893*4882a593Smuzhiyun u16 green = cmap->green[i];
894*4882a593Smuzhiyun u16 blue = cmap->blue[i];
895*4882a593Smuzhiyun u32 value;
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun red >>= 16 - info->var.red.length;
898*4882a593Smuzhiyun green >>= 16 - info->var.green.length;
899*4882a593Smuzhiyun blue >>= 16 - info->var.blue.length;
900*4882a593Smuzhiyun value = (red << info->var.red.offset) |
901*4882a593Smuzhiyun (green << info->var.green.offset) |
902*4882a593Smuzhiyun (blue << info->var.blue.offset);
903*4882a593Smuzhiyun if (info->var.transp.length > 0) {
904*4882a593Smuzhiyun u32 mask = (1 << info->var.transp.length) - 1;
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun mask <<= info->var.transp.offset;
907*4882a593Smuzhiyun value |= mask;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun palette[cmap->start + i] = value;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun return 0;
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun
setcmap_legacy(struct fb_cmap * cmap,struct fb_info * info)915*4882a593Smuzhiyun static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
918*4882a593Smuzhiyun struct drm_mode_set *modeset;
919*4882a593Smuzhiyun struct drm_crtc *crtc;
920*4882a593Smuzhiyun u16 *r, *g, *b;
921*4882a593Smuzhiyun int ret = 0;
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun drm_modeset_lock_all(fb_helper->dev);
924*4882a593Smuzhiyun drm_client_for_each_modeset(modeset, &fb_helper->client) {
925*4882a593Smuzhiyun crtc = modeset->crtc;
926*4882a593Smuzhiyun if (!crtc->funcs->gamma_set || !crtc->gamma_size) {
927*4882a593Smuzhiyun ret = -EINVAL;
928*4882a593Smuzhiyun goto out;
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun if (cmap->start + cmap->len > crtc->gamma_size) {
932*4882a593Smuzhiyun ret = -EINVAL;
933*4882a593Smuzhiyun goto out;
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun r = crtc->gamma_store;
937*4882a593Smuzhiyun g = r + crtc->gamma_size;
938*4882a593Smuzhiyun b = g + crtc->gamma_size;
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
941*4882a593Smuzhiyun memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
942*4882a593Smuzhiyun memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun ret = crtc->funcs->gamma_set(crtc, r, g, b,
945*4882a593Smuzhiyun crtc->gamma_size, NULL);
946*4882a593Smuzhiyun if (ret)
947*4882a593Smuzhiyun goto out;
948*4882a593Smuzhiyun }
949*4882a593Smuzhiyun out:
950*4882a593Smuzhiyun drm_modeset_unlock_all(fb_helper->dev);
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun return ret;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
setcmap_new_gamma_lut(struct drm_crtc * crtc,struct fb_cmap * cmap)955*4882a593Smuzhiyun static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
956*4882a593Smuzhiyun struct fb_cmap *cmap)
957*4882a593Smuzhiyun {
958*4882a593Smuzhiyun struct drm_device *dev = crtc->dev;
959*4882a593Smuzhiyun struct drm_property_blob *gamma_lut;
960*4882a593Smuzhiyun struct drm_color_lut *lut;
961*4882a593Smuzhiyun int size = crtc->gamma_size;
962*4882a593Smuzhiyun int i;
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun if (!size || cmap->start + cmap->len > size)
965*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
966*4882a593Smuzhiyun
967*4882a593Smuzhiyun gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
968*4882a593Smuzhiyun if (IS_ERR(gamma_lut))
969*4882a593Smuzhiyun return gamma_lut;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun lut = gamma_lut->data;
972*4882a593Smuzhiyun if (cmap->start || cmap->len != size) {
973*4882a593Smuzhiyun u16 *r = crtc->gamma_store;
974*4882a593Smuzhiyun u16 *g = r + crtc->gamma_size;
975*4882a593Smuzhiyun u16 *b = g + crtc->gamma_size;
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun for (i = 0; i < cmap->start; i++) {
978*4882a593Smuzhiyun lut[i].red = r[i];
979*4882a593Smuzhiyun lut[i].green = g[i];
980*4882a593Smuzhiyun lut[i].blue = b[i];
981*4882a593Smuzhiyun }
982*4882a593Smuzhiyun for (i = cmap->start + cmap->len; i < size; i++) {
983*4882a593Smuzhiyun lut[i].red = r[i];
984*4882a593Smuzhiyun lut[i].green = g[i];
985*4882a593Smuzhiyun lut[i].blue = b[i];
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun for (i = 0; i < cmap->len; i++) {
990*4882a593Smuzhiyun lut[cmap->start + i].red = cmap->red[i];
991*4882a593Smuzhiyun lut[cmap->start + i].green = cmap->green[i];
992*4882a593Smuzhiyun lut[cmap->start + i].blue = cmap->blue[i];
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun return gamma_lut;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
setcmap_atomic(struct fb_cmap * cmap,struct fb_info * info)998*4882a593Smuzhiyun static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
999*4882a593Smuzhiyun {
1000*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1001*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1002*4882a593Smuzhiyun struct drm_property_blob *gamma_lut = NULL;
1003*4882a593Smuzhiyun struct drm_modeset_acquire_ctx ctx;
1004*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
1005*4882a593Smuzhiyun struct drm_atomic_state *state;
1006*4882a593Smuzhiyun struct drm_mode_set *modeset;
1007*4882a593Smuzhiyun struct drm_crtc *crtc;
1008*4882a593Smuzhiyun u16 *r, *g, *b;
1009*4882a593Smuzhiyun bool replaced;
1010*4882a593Smuzhiyun int ret = 0;
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun drm_modeset_acquire_init(&ctx, 0);
1013*4882a593Smuzhiyun
1014*4882a593Smuzhiyun state = drm_atomic_state_alloc(dev);
1015*4882a593Smuzhiyun if (!state) {
1016*4882a593Smuzhiyun ret = -ENOMEM;
1017*4882a593Smuzhiyun goto out_ctx;
1018*4882a593Smuzhiyun }
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun state->acquire_ctx = &ctx;
1021*4882a593Smuzhiyun retry:
1022*4882a593Smuzhiyun drm_client_for_each_modeset(modeset, &fb_helper->client) {
1023*4882a593Smuzhiyun crtc = modeset->crtc;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun if (!gamma_lut)
1026*4882a593Smuzhiyun gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
1027*4882a593Smuzhiyun if (IS_ERR(gamma_lut)) {
1028*4882a593Smuzhiyun ret = PTR_ERR(gamma_lut);
1029*4882a593Smuzhiyun gamma_lut = NULL;
1030*4882a593Smuzhiyun goto out_state;
1031*4882a593Smuzhiyun }
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(state, crtc);
1034*4882a593Smuzhiyun if (IS_ERR(crtc_state)) {
1035*4882a593Smuzhiyun ret = PTR_ERR(crtc_state);
1036*4882a593Smuzhiyun goto out_state;
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
1040*4882a593Smuzhiyun NULL);
1041*4882a593Smuzhiyun replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
1042*4882a593Smuzhiyun replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
1043*4882a593Smuzhiyun gamma_lut);
1044*4882a593Smuzhiyun crtc_state->color_mgmt_changed |= replaced;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun ret = drm_atomic_commit(state);
1048*4882a593Smuzhiyun if (ret)
1049*4882a593Smuzhiyun goto out_state;
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun drm_client_for_each_modeset(modeset, &fb_helper->client) {
1052*4882a593Smuzhiyun crtc = modeset->crtc;
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun r = crtc->gamma_store;
1055*4882a593Smuzhiyun g = r + crtc->gamma_size;
1056*4882a593Smuzhiyun b = g + crtc->gamma_size;
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
1059*4882a593Smuzhiyun memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
1060*4882a593Smuzhiyun memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun out_state:
1064*4882a593Smuzhiyun if (ret == -EDEADLK)
1065*4882a593Smuzhiyun goto backoff;
1066*4882a593Smuzhiyun
1067*4882a593Smuzhiyun drm_property_blob_put(gamma_lut);
1068*4882a593Smuzhiyun drm_atomic_state_put(state);
1069*4882a593Smuzhiyun out_ctx:
1070*4882a593Smuzhiyun drm_modeset_drop_locks(&ctx);
1071*4882a593Smuzhiyun drm_modeset_acquire_fini(&ctx);
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun return ret;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun backoff:
1076*4882a593Smuzhiyun drm_atomic_state_clear(state);
1077*4882a593Smuzhiyun drm_modeset_backoff(&ctx);
1078*4882a593Smuzhiyun goto retry;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun /**
1082*4882a593Smuzhiyun * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
1083*4882a593Smuzhiyun * @cmap: cmap to set
1084*4882a593Smuzhiyun * @info: fbdev registered by the helper
1085*4882a593Smuzhiyun */
drm_fb_helper_setcmap(struct fb_cmap * cmap,struct fb_info * info)1086*4882a593Smuzhiyun int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1089*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1090*4882a593Smuzhiyun int ret;
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun if (oops_in_progress)
1093*4882a593Smuzhiyun return -EBUSY;
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun if (!drm_master_internal_acquire(dev)) {
1098*4882a593Smuzhiyun ret = -EBUSY;
1099*4882a593Smuzhiyun goto unlock;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun mutex_lock(&fb_helper->client.modeset_mutex);
1103*4882a593Smuzhiyun if (info->fix.visual == FB_VISUAL_TRUECOLOR)
1104*4882a593Smuzhiyun ret = setcmap_pseudo_palette(cmap, info);
1105*4882a593Smuzhiyun else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
1106*4882a593Smuzhiyun ret = setcmap_atomic(cmap, info);
1107*4882a593Smuzhiyun else
1108*4882a593Smuzhiyun ret = setcmap_legacy(cmap, info);
1109*4882a593Smuzhiyun mutex_unlock(&fb_helper->client.modeset_mutex);
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun drm_master_internal_release(dev);
1112*4882a593Smuzhiyun unlock:
1113*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun return ret;
1116*4882a593Smuzhiyun }
1117*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_setcmap);
1118*4882a593Smuzhiyun
1119*4882a593Smuzhiyun /**
1120*4882a593Smuzhiyun * drm_fb_helper_ioctl - legacy ioctl implementation
1121*4882a593Smuzhiyun * @info: fbdev registered by the helper
1122*4882a593Smuzhiyun * @cmd: ioctl command
1123*4882a593Smuzhiyun * @arg: ioctl argument
1124*4882a593Smuzhiyun *
1125*4882a593Smuzhiyun * A helper to implement the standard fbdev ioctl. Only
1126*4882a593Smuzhiyun * FBIO_WAITFORVSYNC is implemented for now.
1127*4882a593Smuzhiyun */
drm_fb_helper_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)1128*4882a593Smuzhiyun int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
1129*4882a593Smuzhiyun unsigned long arg)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1132*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1133*4882a593Smuzhiyun struct drm_crtc *crtc;
1134*4882a593Smuzhiyun int ret = 0;
1135*4882a593Smuzhiyun
1136*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
1137*4882a593Smuzhiyun if (!drm_master_internal_acquire(dev)) {
1138*4882a593Smuzhiyun ret = -EBUSY;
1139*4882a593Smuzhiyun goto unlock;
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun switch (cmd) {
1143*4882a593Smuzhiyun case FBIO_WAITFORVSYNC:
1144*4882a593Smuzhiyun /*
1145*4882a593Smuzhiyun * Only consider the first CRTC.
1146*4882a593Smuzhiyun *
1147*4882a593Smuzhiyun * This ioctl is supposed to take the CRTC number as
1148*4882a593Smuzhiyun * an argument, but in fbdev times, what that number
1149*4882a593Smuzhiyun * was supposed to be was quite unclear, different
1150*4882a593Smuzhiyun * drivers were passing that argument differently
1151*4882a593Smuzhiyun * (some by reference, some by value), and most of the
1152*4882a593Smuzhiyun * userspace applications were just hardcoding 0 as an
1153*4882a593Smuzhiyun * argument.
1154*4882a593Smuzhiyun *
1155*4882a593Smuzhiyun * The first CRTC should be the integrated panel on
1156*4882a593Smuzhiyun * most drivers, so this is the best choice we can
1157*4882a593Smuzhiyun * make. If we're not smart enough here, one should
1158*4882a593Smuzhiyun * just consider switch the userspace to KMS.
1159*4882a593Smuzhiyun */
1160*4882a593Smuzhiyun crtc = fb_helper->client.modesets[0].crtc;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun /*
1163*4882a593Smuzhiyun * Only wait for a vblank event if the CRTC is
1164*4882a593Smuzhiyun * enabled, otherwise just don't do anythintg,
1165*4882a593Smuzhiyun * not even report an error.
1166*4882a593Smuzhiyun */
1167*4882a593Smuzhiyun ret = drm_crtc_vblank_get(crtc);
1168*4882a593Smuzhiyun if (!ret) {
1169*4882a593Smuzhiyun drm_crtc_wait_one_vblank(crtc);
1170*4882a593Smuzhiyun drm_crtc_vblank_put(crtc);
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun
1173*4882a593Smuzhiyun ret = 0;
1174*4882a593Smuzhiyun break;
1175*4882a593Smuzhiyun default:
1176*4882a593Smuzhiyun ret = -ENOTTY;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun drm_master_internal_release(dev);
1180*4882a593Smuzhiyun unlock:
1181*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1182*4882a593Smuzhiyun return ret;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_ioctl);
1185*4882a593Smuzhiyun
drm_fb_pixel_format_equal(const struct fb_var_screeninfo * var_1,const struct fb_var_screeninfo * var_2)1186*4882a593Smuzhiyun static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
1187*4882a593Smuzhiyun const struct fb_var_screeninfo *var_2)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun return var_1->bits_per_pixel == var_2->bits_per_pixel &&
1190*4882a593Smuzhiyun var_1->grayscale == var_2->grayscale &&
1191*4882a593Smuzhiyun var_1->red.offset == var_2->red.offset &&
1192*4882a593Smuzhiyun var_1->red.length == var_2->red.length &&
1193*4882a593Smuzhiyun var_1->red.msb_right == var_2->red.msb_right &&
1194*4882a593Smuzhiyun var_1->green.offset == var_2->green.offset &&
1195*4882a593Smuzhiyun var_1->green.length == var_2->green.length &&
1196*4882a593Smuzhiyun var_1->green.msb_right == var_2->green.msb_right &&
1197*4882a593Smuzhiyun var_1->blue.offset == var_2->blue.offset &&
1198*4882a593Smuzhiyun var_1->blue.length == var_2->blue.length &&
1199*4882a593Smuzhiyun var_1->blue.msb_right == var_2->blue.msb_right &&
1200*4882a593Smuzhiyun var_1->transp.offset == var_2->transp.offset &&
1201*4882a593Smuzhiyun var_1->transp.length == var_2->transp.length &&
1202*4882a593Smuzhiyun var_1->transp.msb_right == var_2->transp.msb_right;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo * var,u8 depth)1205*4882a593Smuzhiyun static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
1206*4882a593Smuzhiyun u8 depth)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun switch (depth) {
1209*4882a593Smuzhiyun case 8:
1210*4882a593Smuzhiyun var->red.offset = 0;
1211*4882a593Smuzhiyun var->green.offset = 0;
1212*4882a593Smuzhiyun var->blue.offset = 0;
1213*4882a593Smuzhiyun var->red.length = 8; /* 8bit DAC */
1214*4882a593Smuzhiyun var->green.length = 8;
1215*4882a593Smuzhiyun var->blue.length = 8;
1216*4882a593Smuzhiyun var->transp.offset = 0;
1217*4882a593Smuzhiyun var->transp.length = 0;
1218*4882a593Smuzhiyun break;
1219*4882a593Smuzhiyun case 15:
1220*4882a593Smuzhiyun var->red.offset = 10;
1221*4882a593Smuzhiyun var->green.offset = 5;
1222*4882a593Smuzhiyun var->blue.offset = 0;
1223*4882a593Smuzhiyun var->red.length = 5;
1224*4882a593Smuzhiyun var->green.length = 5;
1225*4882a593Smuzhiyun var->blue.length = 5;
1226*4882a593Smuzhiyun var->transp.offset = 15;
1227*4882a593Smuzhiyun var->transp.length = 1;
1228*4882a593Smuzhiyun break;
1229*4882a593Smuzhiyun case 16:
1230*4882a593Smuzhiyun var->red.offset = 11;
1231*4882a593Smuzhiyun var->green.offset = 5;
1232*4882a593Smuzhiyun var->blue.offset = 0;
1233*4882a593Smuzhiyun var->red.length = 5;
1234*4882a593Smuzhiyun var->green.length = 6;
1235*4882a593Smuzhiyun var->blue.length = 5;
1236*4882a593Smuzhiyun var->transp.offset = 0;
1237*4882a593Smuzhiyun break;
1238*4882a593Smuzhiyun case 24:
1239*4882a593Smuzhiyun var->red.offset = 16;
1240*4882a593Smuzhiyun var->green.offset = 8;
1241*4882a593Smuzhiyun var->blue.offset = 0;
1242*4882a593Smuzhiyun var->red.length = 8;
1243*4882a593Smuzhiyun var->green.length = 8;
1244*4882a593Smuzhiyun var->blue.length = 8;
1245*4882a593Smuzhiyun var->transp.offset = 0;
1246*4882a593Smuzhiyun var->transp.length = 0;
1247*4882a593Smuzhiyun break;
1248*4882a593Smuzhiyun case 32:
1249*4882a593Smuzhiyun var->red.offset = 16;
1250*4882a593Smuzhiyun var->green.offset = 8;
1251*4882a593Smuzhiyun var->blue.offset = 0;
1252*4882a593Smuzhiyun var->red.length = 8;
1253*4882a593Smuzhiyun var->green.length = 8;
1254*4882a593Smuzhiyun var->blue.length = 8;
1255*4882a593Smuzhiyun var->transp.offset = 24;
1256*4882a593Smuzhiyun var->transp.length = 8;
1257*4882a593Smuzhiyun break;
1258*4882a593Smuzhiyun default:
1259*4882a593Smuzhiyun break;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun }
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun /**
1264*4882a593Smuzhiyun * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
1265*4882a593Smuzhiyun * @var: screeninfo to check
1266*4882a593Smuzhiyun * @info: fbdev registered by the helper
1267*4882a593Smuzhiyun */
drm_fb_helper_check_var(struct fb_var_screeninfo * var,struct fb_info * info)1268*4882a593Smuzhiyun int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
1269*4882a593Smuzhiyun struct fb_info *info)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1272*4882a593Smuzhiyun struct drm_framebuffer *fb = fb_helper->fb;
1273*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun if (in_dbg_master())
1276*4882a593Smuzhiyun return -EINVAL;
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun if (var->pixclock != 0) {
1279*4882a593Smuzhiyun drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
1280*4882a593Smuzhiyun var->pixclock = 0;
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun if ((drm_format_info_block_width(fb->format, 0) > 1) ||
1284*4882a593Smuzhiyun (drm_format_info_block_height(fb->format, 0) > 1))
1285*4882a593Smuzhiyun return -EINVAL;
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun /*
1288*4882a593Smuzhiyun * Changes struct fb_var_screeninfo are currently not pushed back
1289*4882a593Smuzhiyun * to KMS, hence fail if different settings are requested.
1290*4882a593Smuzhiyun */
1291*4882a593Smuzhiyun if (var->bits_per_pixel > fb->format->cpp[0] * 8 ||
1292*4882a593Smuzhiyun var->xres > fb->width || var->yres > fb->height ||
1293*4882a593Smuzhiyun var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
1294*4882a593Smuzhiyun drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb "
1295*4882a593Smuzhiyun "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
1296*4882a593Smuzhiyun var->xres, var->yres, var->bits_per_pixel,
1297*4882a593Smuzhiyun var->xres_virtual, var->yres_virtual,
1298*4882a593Smuzhiyun fb->width, fb->height, fb->format->cpp[0] * 8);
1299*4882a593Smuzhiyun return -EINVAL;
1300*4882a593Smuzhiyun }
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun /*
1303*4882a593Smuzhiyun * Workaround for SDL 1.2, which is known to be setting all pixel format
1304*4882a593Smuzhiyun * fields values to zero in some cases. We treat this situation as a
1305*4882a593Smuzhiyun * kind of "use some reasonable autodetected values".
1306*4882a593Smuzhiyun */
1307*4882a593Smuzhiyun if (!var->red.offset && !var->green.offset &&
1308*4882a593Smuzhiyun !var->blue.offset && !var->transp.offset &&
1309*4882a593Smuzhiyun !var->red.length && !var->green.length &&
1310*4882a593Smuzhiyun !var->blue.length && !var->transp.length &&
1311*4882a593Smuzhiyun !var->red.msb_right && !var->green.msb_right &&
1312*4882a593Smuzhiyun !var->blue.msb_right && !var->transp.msb_right) {
1313*4882a593Smuzhiyun drm_fb_helper_fill_pixel_fmt(var, fb->format->depth);
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun
1316*4882a593Smuzhiyun /*
1317*4882a593Smuzhiyun * Likewise, bits_per_pixel should be rounded up to a supported value.
1318*4882a593Smuzhiyun */
1319*4882a593Smuzhiyun var->bits_per_pixel = fb->format->cpp[0] * 8;
1320*4882a593Smuzhiyun
1321*4882a593Smuzhiyun /*
1322*4882a593Smuzhiyun * drm fbdev emulation doesn't support changing the pixel format at all,
1323*4882a593Smuzhiyun * so reject all pixel format changing requests.
1324*4882a593Smuzhiyun */
1325*4882a593Smuzhiyun if (!drm_fb_pixel_format_equal(var, &info->var)) {
1326*4882a593Smuzhiyun drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n");
1327*4882a593Smuzhiyun return -EINVAL;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun
1330*4882a593Smuzhiyun return 0;
1331*4882a593Smuzhiyun }
1332*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_check_var);
1333*4882a593Smuzhiyun
1334*4882a593Smuzhiyun /**
1335*4882a593Smuzhiyun * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
1336*4882a593Smuzhiyun * @info: fbdev registered by the helper
1337*4882a593Smuzhiyun *
1338*4882a593Smuzhiyun * This will let fbcon do the mode init and is called at initialization time by
1339*4882a593Smuzhiyun * the fbdev core when registering the driver, and later on through the hotplug
1340*4882a593Smuzhiyun * callback.
1341*4882a593Smuzhiyun */
drm_fb_helper_set_par(struct fb_info * info)1342*4882a593Smuzhiyun int drm_fb_helper_set_par(struct fb_info *info)
1343*4882a593Smuzhiyun {
1344*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1345*4882a593Smuzhiyun struct fb_var_screeninfo *var = &info->var;
1346*4882a593Smuzhiyun bool force;
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun if (oops_in_progress)
1349*4882a593Smuzhiyun return -EBUSY;
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun if (var->pixclock != 0) {
1352*4882a593Smuzhiyun drm_err(fb_helper->dev, "PIXEL CLOCK SET\n");
1353*4882a593Smuzhiyun return -EINVAL;
1354*4882a593Smuzhiyun }
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun /*
1357*4882a593Smuzhiyun * Normally we want to make sure that a kms master takes precedence over
1358*4882a593Smuzhiyun * fbdev, to avoid fbdev flickering and occasionally stealing the
1359*4882a593Smuzhiyun * display status. But Xorg first sets the vt back to text mode using
1360*4882a593Smuzhiyun * the KDSET IOCTL with KD_TEXT, and only after that drops the master
1361*4882a593Smuzhiyun * status when exiting.
1362*4882a593Smuzhiyun *
1363*4882a593Smuzhiyun * In the past this was caught by drm_fb_helper_lastclose(), but on
1364*4882a593Smuzhiyun * modern systems where logind always keeps a drm fd open to orchestrate
1365*4882a593Smuzhiyun * the vt switching, this doesn't work.
1366*4882a593Smuzhiyun *
1367*4882a593Smuzhiyun * To not break the userspace ABI we have this special case here, which
1368*4882a593Smuzhiyun * is only used for the above case. Everything else uses the normal
1369*4882a593Smuzhiyun * commit function, which ensures that we never steal the display from
1370*4882a593Smuzhiyun * an active drm master.
1371*4882a593Smuzhiyun */
1372*4882a593Smuzhiyun force = var->activate & FB_ACTIVATE_KD_TEXT;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force);
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun return 0;
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_set_par);
1379*4882a593Smuzhiyun
pan_set(struct drm_fb_helper * fb_helper,int x,int y)1380*4882a593Smuzhiyun static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
1381*4882a593Smuzhiyun {
1382*4882a593Smuzhiyun struct drm_mode_set *mode_set;
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun mutex_lock(&fb_helper->client.modeset_mutex);
1385*4882a593Smuzhiyun drm_client_for_each_modeset(mode_set, &fb_helper->client) {
1386*4882a593Smuzhiyun mode_set->x = x;
1387*4882a593Smuzhiyun mode_set->y = y;
1388*4882a593Smuzhiyun }
1389*4882a593Smuzhiyun mutex_unlock(&fb_helper->client.modeset_mutex);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun
pan_display_atomic(struct fb_var_screeninfo * var,struct fb_info * info)1392*4882a593Smuzhiyun static int pan_display_atomic(struct fb_var_screeninfo *var,
1393*4882a593Smuzhiyun struct fb_info *info)
1394*4882a593Smuzhiyun {
1395*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1396*4882a593Smuzhiyun int ret;
1397*4882a593Smuzhiyun
1398*4882a593Smuzhiyun pan_set(fb_helper, var->xoffset, var->yoffset);
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun ret = drm_client_modeset_commit_locked(&fb_helper->client);
1401*4882a593Smuzhiyun if (!ret) {
1402*4882a593Smuzhiyun info->var.xoffset = var->xoffset;
1403*4882a593Smuzhiyun info->var.yoffset = var->yoffset;
1404*4882a593Smuzhiyun } else
1405*4882a593Smuzhiyun pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun return ret;
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun
pan_display_legacy(struct fb_var_screeninfo * var,struct fb_info * info)1410*4882a593Smuzhiyun static int pan_display_legacy(struct fb_var_screeninfo *var,
1411*4882a593Smuzhiyun struct fb_info *info)
1412*4882a593Smuzhiyun {
1413*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1414*4882a593Smuzhiyun struct drm_client_dev *client = &fb_helper->client;
1415*4882a593Smuzhiyun struct drm_mode_set *modeset;
1416*4882a593Smuzhiyun int ret = 0;
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun mutex_lock(&client->modeset_mutex);
1419*4882a593Smuzhiyun drm_modeset_lock_all(fb_helper->dev);
1420*4882a593Smuzhiyun drm_client_for_each_modeset(modeset, client) {
1421*4882a593Smuzhiyun modeset->x = var->xoffset;
1422*4882a593Smuzhiyun modeset->y = var->yoffset;
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun if (modeset->num_connectors) {
1425*4882a593Smuzhiyun ret = drm_mode_set_config_internal(modeset);
1426*4882a593Smuzhiyun if (!ret) {
1427*4882a593Smuzhiyun info->var.xoffset = var->xoffset;
1428*4882a593Smuzhiyun info->var.yoffset = var->yoffset;
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun }
1432*4882a593Smuzhiyun drm_modeset_unlock_all(fb_helper->dev);
1433*4882a593Smuzhiyun mutex_unlock(&client->modeset_mutex);
1434*4882a593Smuzhiyun
1435*4882a593Smuzhiyun return ret;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun
1438*4882a593Smuzhiyun /**
1439*4882a593Smuzhiyun * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
1440*4882a593Smuzhiyun * @var: updated screen information
1441*4882a593Smuzhiyun * @info: fbdev registered by the helper
1442*4882a593Smuzhiyun */
drm_fb_helper_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)1443*4882a593Smuzhiyun int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
1444*4882a593Smuzhiyun struct fb_info *info)
1445*4882a593Smuzhiyun {
1446*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1447*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1448*4882a593Smuzhiyun int ret;
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun if (oops_in_progress)
1451*4882a593Smuzhiyun return -EBUSY;
1452*4882a593Smuzhiyun
1453*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
1454*4882a593Smuzhiyun if (!drm_master_internal_acquire(dev)) {
1455*4882a593Smuzhiyun ret = -EBUSY;
1456*4882a593Smuzhiyun goto unlock;
1457*4882a593Smuzhiyun }
1458*4882a593Smuzhiyun
1459*4882a593Smuzhiyun if (drm_drv_uses_atomic_modeset(dev))
1460*4882a593Smuzhiyun ret = pan_display_atomic(var, info);
1461*4882a593Smuzhiyun else
1462*4882a593Smuzhiyun ret = pan_display_legacy(var, info);
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun drm_master_internal_release(dev);
1465*4882a593Smuzhiyun unlock:
1466*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun return ret;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_pan_display);
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun /*
1473*4882a593Smuzhiyun * Allocates the backing storage and sets up the fbdev info structure through
1474*4882a593Smuzhiyun * the ->fb_probe callback.
1475*4882a593Smuzhiyun */
drm_fb_helper_single_fb_probe(struct drm_fb_helper * fb_helper,int preferred_bpp)1476*4882a593Smuzhiyun static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1477*4882a593Smuzhiyun int preferred_bpp)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun struct drm_client_dev *client = &fb_helper->client;
1480*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1481*4882a593Smuzhiyun int ret = 0;
1482*4882a593Smuzhiyun int crtc_count = 0;
1483*4882a593Smuzhiyun struct drm_connector_list_iter conn_iter;
1484*4882a593Smuzhiyun struct drm_fb_helper_surface_size sizes;
1485*4882a593Smuzhiyun struct drm_connector *connector;
1486*4882a593Smuzhiyun struct drm_mode_set *mode_set;
1487*4882a593Smuzhiyun int best_depth = 0;
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1490*4882a593Smuzhiyun sizes.surface_depth = 24;
1491*4882a593Smuzhiyun sizes.surface_bpp = 32;
1492*4882a593Smuzhiyun sizes.fb_width = (u32)-1;
1493*4882a593Smuzhiyun sizes.fb_height = (u32)-1;
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun /*
1496*4882a593Smuzhiyun * If driver picks 8 or 16 by default use that for both depth/bpp
1497*4882a593Smuzhiyun * to begin with
1498*4882a593Smuzhiyun */
1499*4882a593Smuzhiyun if (preferred_bpp != sizes.surface_bpp)
1500*4882a593Smuzhiyun sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1503*4882a593Smuzhiyun drm_client_for_each_connector_iter(connector, &conn_iter) {
1504*4882a593Smuzhiyun struct drm_cmdline_mode *cmdline_mode;
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun cmdline_mode = &connector->cmdline_mode;
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun if (cmdline_mode->bpp_specified) {
1509*4882a593Smuzhiyun switch (cmdline_mode->bpp) {
1510*4882a593Smuzhiyun case 8:
1511*4882a593Smuzhiyun sizes.surface_depth = sizes.surface_bpp = 8;
1512*4882a593Smuzhiyun break;
1513*4882a593Smuzhiyun case 15:
1514*4882a593Smuzhiyun sizes.surface_depth = 15;
1515*4882a593Smuzhiyun sizes.surface_bpp = 16;
1516*4882a593Smuzhiyun break;
1517*4882a593Smuzhiyun case 16:
1518*4882a593Smuzhiyun sizes.surface_depth = sizes.surface_bpp = 16;
1519*4882a593Smuzhiyun break;
1520*4882a593Smuzhiyun case 24:
1521*4882a593Smuzhiyun sizes.surface_depth = sizes.surface_bpp = 24;
1522*4882a593Smuzhiyun break;
1523*4882a593Smuzhiyun case 32:
1524*4882a593Smuzhiyun sizes.surface_depth = 24;
1525*4882a593Smuzhiyun sizes.surface_bpp = 32;
1526*4882a593Smuzhiyun break;
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun break;
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun }
1531*4882a593Smuzhiyun drm_connector_list_iter_end(&conn_iter);
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun /*
1534*4882a593Smuzhiyun * If we run into a situation where, for example, the primary plane
1535*4882a593Smuzhiyun * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
1536*4882a593Smuzhiyun * 16) we need to scale down the depth of the sizes we request.
1537*4882a593Smuzhiyun */
1538*4882a593Smuzhiyun mutex_lock(&client->modeset_mutex);
1539*4882a593Smuzhiyun drm_client_for_each_modeset(mode_set, client) {
1540*4882a593Smuzhiyun struct drm_crtc *crtc = mode_set->crtc;
1541*4882a593Smuzhiyun struct drm_plane *plane = crtc->primary;
1542*4882a593Smuzhiyun int j;
1543*4882a593Smuzhiyun
1544*4882a593Smuzhiyun drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc));
1545*4882a593Smuzhiyun
1546*4882a593Smuzhiyun for (j = 0; j < plane->format_count; j++) {
1547*4882a593Smuzhiyun const struct drm_format_info *fmt;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun fmt = drm_format_info(plane->format_types[j]);
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun /*
1552*4882a593Smuzhiyun * Do not consider YUV or other complicated formats
1553*4882a593Smuzhiyun * for framebuffers. This means only legacy formats
1554*4882a593Smuzhiyun * are supported (fmt->depth is a legacy field) but
1555*4882a593Smuzhiyun * the framebuffer emulation can only deal with such
1556*4882a593Smuzhiyun * formats, specifically RGB/BGA formats.
1557*4882a593Smuzhiyun */
1558*4882a593Smuzhiyun if (fmt->depth == 0)
1559*4882a593Smuzhiyun continue;
1560*4882a593Smuzhiyun
1561*4882a593Smuzhiyun /* We found a perfect fit, great */
1562*4882a593Smuzhiyun if (fmt->depth == sizes.surface_depth) {
1563*4882a593Smuzhiyun best_depth = fmt->depth;
1564*4882a593Smuzhiyun break;
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun
1567*4882a593Smuzhiyun /* Skip depths above what we're looking for */
1568*4882a593Smuzhiyun if (fmt->depth > sizes.surface_depth)
1569*4882a593Smuzhiyun continue;
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun /* Best depth found so far */
1572*4882a593Smuzhiyun if (fmt->depth > best_depth)
1573*4882a593Smuzhiyun best_depth = fmt->depth;
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun }
1576*4882a593Smuzhiyun if (sizes.surface_depth != best_depth && best_depth) {
1577*4882a593Smuzhiyun drm_info(dev, "requested bpp %d, scaled depth down to %d",
1578*4882a593Smuzhiyun sizes.surface_bpp, best_depth);
1579*4882a593Smuzhiyun sizes.surface_depth = best_depth;
1580*4882a593Smuzhiyun }
1581*4882a593Smuzhiyun
1582*4882a593Smuzhiyun /* first up get a count of crtcs now in use and new min/maxes width/heights */
1583*4882a593Smuzhiyun crtc_count = 0;
1584*4882a593Smuzhiyun drm_client_for_each_modeset(mode_set, client) {
1585*4882a593Smuzhiyun struct drm_display_mode *desired_mode;
1586*4882a593Smuzhiyun int x, y, j;
1587*4882a593Smuzhiyun /* in case of tile group, are we the last tile vert or horiz?
1588*4882a593Smuzhiyun * If no tile group you are always the last one both vertically
1589*4882a593Smuzhiyun * and horizontally
1590*4882a593Smuzhiyun */
1591*4882a593Smuzhiyun bool lastv = true, lasth = true;
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun desired_mode = mode_set->mode;
1594*4882a593Smuzhiyun
1595*4882a593Smuzhiyun if (!desired_mode)
1596*4882a593Smuzhiyun continue;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun crtc_count++;
1599*4882a593Smuzhiyun
1600*4882a593Smuzhiyun x = mode_set->x;
1601*4882a593Smuzhiyun y = mode_set->y;
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
1604*4882a593Smuzhiyun sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun for (j = 0; j < mode_set->num_connectors; j++) {
1607*4882a593Smuzhiyun struct drm_connector *connector = mode_set->connectors[j];
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun if (connector->has_tile &&
1610*4882a593Smuzhiyun desired_mode->hdisplay == connector->tile_h_size &&
1611*4882a593Smuzhiyun desired_mode->vdisplay == connector->tile_v_size) {
1612*4882a593Smuzhiyun lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
1613*4882a593Smuzhiyun lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
1614*4882a593Smuzhiyun /* cloning to multiple tiles is just crazy-talk, so: */
1615*4882a593Smuzhiyun break;
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun }
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun if (lasth)
1620*4882a593Smuzhiyun sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
1621*4882a593Smuzhiyun if (lastv)
1622*4882a593Smuzhiyun sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun mutex_unlock(&client->modeset_mutex);
1625*4882a593Smuzhiyun
1626*4882a593Smuzhiyun if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
1627*4882a593Smuzhiyun drm_info(dev, "Cannot find any crtc or sizes\n");
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun /* First time: disable all crtc's.. */
1630*4882a593Smuzhiyun if (!fb_helper->deferred_setup)
1631*4882a593Smuzhiyun drm_client_modeset_commit(client);
1632*4882a593Smuzhiyun return -EAGAIN;
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun /* Handle our overallocation */
1636*4882a593Smuzhiyun sizes.surface_height *= drm_fbdev_overalloc;
1637*4882a593Smuzhiyun sizes.surface_height /= 100;
1638*4882a593Smuzhiyun
1639*4882a593Smuzhiyun /* push down into drivers */
1640*4882a593Smuzhiyun ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1641*4882a593Smuzhiyun if (ret < 0)
1642*4882a593Smuzhiyun return ret;
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun strcpy(fb_helper->fb->comm, "[fbcon]");
1645*4882a593Smuzhiyun return 0;
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun
drm_fb_helper_fill_fix(struct fb_info * info,uint32_t pitch,uint32_t depth)1648*4882a593Smuzhiyun static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
1649*4882a593Smuzhiyun uint32_t depth)
1650*4882a593Smuzhiyun {
1651*4882a593Smuzhiyun info->fix.type = FB_TYPE_PACKED_PIXELS;
1652*4882a593Smuzhiyun info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
1653*4882a593Smuzhiyun FB_VISUAL_TRUECOLOR;
1654*4882a593Smuzhiyun info->fix.mmio_start = 0;
1655*4882a593Smuzhiyun info->fix.mmio_len = 0;
1656*4882a593Smuzhiyun info->fix.type_aux = 0;
1657*4882a593Smuzhiyun info->fix.xpanstep = 1; /* doing it in hw */
1658*4882a593Smuzhiyun info->fix.ypanstep = 1; /* doing it in hw */
1659*4882a593Smuzhiyun info->fix.ywrapstep = 0;
1660*4882a593Smuzhiyun info->fix.accel = FB_ACCEL_NONE;
1661*4882a593Smuzhiyun
1662*4882a593Smuzhiyun info->fix.line_length = pitch;
1663*4882a593Smuzhiyun }
1664*4882a593Smuzhiyun
drm_fb_helper_fill_var(struct fb_info * info,struct drm_fb_helper * fb_helper,uint32_t fb_width,uint32_t fb_height)1665*4882a593Smuzhiyun static void drm_fb_helper_fill_var(struct fb_info *info,
1666*4882a593Smuzhiyun struct drm_fb_helper *fb_helper,
1667*4882a593Smuzhiyun uint32_t fb_width, uint32_t fb_height)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun struct drm_framebuffer *fb = fb_helper->fb;
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) ||
1672*4882a593Smuzhiyun (drm_format_info_block_height(fb->format, 0) > 1));
1673*4882a593Smuzhiyun info->pseudo_palette = fb_helper->pseudo_palette;
1674*4882a593Smuzhiyun info->var.xres_virtual = fb->width;
1675*4882a593Smuzhiyun info->var.yres_virtual = fb->height;
1676*4882a593Smuzhiyun info->var.bits_per_pixel = fb->format->cpp[0] * 8;
1677*4882a593Smuzhiyun info->var.accel_flags = FB_ACCELF_TEXT;
1678*4882a593Smuzhiyun info->var.xoffset = 0;
1679*4882a593Smuzhiyun info->var.yoffset = 0;
1680*4882a593Smuzhiyun info->var.activate = FB_ACTIVATE_NOW;
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth);
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun info->var.xres = fb_width;
1685*4882a593Smuzhiyun info->var.yres = fb_height;
1686*4882a593Smuzhiyun }
1687*4882a593Smuzhiyun
1688*4882a593Smuzhiyun /**
1689*4882a593Smuzhiyun * drm_fb_helper_fill_info - initializes fbdev information
1690*4882a593Smuzhiyun * @info: fbdev instance to set up
1691*4882a593Smuzhiyun * @fb_helper: fb helper instance to use as template
1692*4882a593Smuzhiyun * @sizes: describes fbdev size and scanout surface size
1693*4882a593Smuzhiyun *
1694*4882a593Smuzhiyun * Sets up the variable and fixed fbdev metainformation from the given fb helper
1695*4882a593Smuzhiyun * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
1696*4882a593Smuzhiyun *
1697*4882a593Smuzhiyun * Drivers should call this (or their equivalent setup code) from their
1698*4882a593Smuzhiyun * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
1699*4882a593Smuzhiyun * backing storage framebuffer.
1700*4882a593Smuzhiyun */
drm_fb_helper_fill_info(struct fb_info * info,struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)1701*4882a593Smuzhiyun void drm_fb_helper_fill_info(struct fb_info *info,
1702*4882a593Smuzhiyun struct drm_fb_helper *fb_helper,
1703*4882a593Smuzhiyun struct drm_fb_helper_surface_size *sizes)
1704*4882a593Smuzhiyun {
1705*4882a593Smuzhiyun struct drm_framebuffer *fb = fb_helper->fb;
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
1708*4882a593Smuzhiyun drm_fb_helper_fill_var(info, fb_helper,
1709*4882a593Smuzhiyun sizes->fb_width, sizes->fb_height);
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun info->par = fb_helper;
1712*4882a593Smuzhiyun snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
1713*4882a593Smuzhiyun fb_helper->dev->driver->name);
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun }
1716*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_fill_info);
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun /*
1719*4882a593Smuzhiyun * This is a continuation of drm_setup_crtcs() that sets up anything related
1720*4882a593Smuzhiyun * to the framebuffer. During initialization, drm_setup_crtcs() is called before
1721*4882a593Smuzhiyun * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
1722*4882a593Smuzhiyun * So, any setup that touches those fields needs to be done here instead of in
1723*4882a593Smuzhiyun * drm_setup_crtcs().
1724*4882a593Smuzhiyun */
drm_setup_crtcs_fb(struct drm_fb_helper * fb_helper)1725*4882a593Smuzhiyun static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
1726*4882a593Smuzhiyun {
1727*4882a593Smuzhiyun struct drm_client_dev *client = &fb_helper->client;
1728*4882a593Smuzhiyun struct drm_connector_list_iter conn_iter;
1729*4882a593Smuzhiyun struct fb_info *info = fb_helper->fbdev;
1730*4882a593Smuzhiyun unsigned int rotation, sw_rotations = 0;
1731*4882a593Smuzhiyun struct drm_connector *connector;
1732*4882a593Smuzhiyun struct drm_mode_set *modeset;
1733*4882a593Smuzhiyun
1734*4882a593Smuzhiyun mutex_lock(&client->modeset_mutex);
1735*4882a593Smuzhiyun drm_client_for_each_modeset(modeset, client) {
1736*4882a593Smuzhiyun if (!modeset->num_connectors)
1737*4882a593Smuzhiyun continue;
1738*4882a593Smuzhiyun
1739*4882a593Smuzhiyun modeset->fb = fb_helper->fb;
1740*4882a593Smuzhiyun
1741*4882a593Smuzhiyun if (drm_client_rotation(modeset, &rotation))
1742*4882a593Smuzhiyun /* Rotating in hardware, fbcon should not rotate */
1743*4882a593Smuzhiyun sw_rotations |= DRM_MODE_ROTATE_0;
1744*4882a593Smuzhiyun else
1745*4882a593Smuzhiyun sw_rotations |= rotation;
1746*4882a593Smuzhiyun }
1747*4882a593Smuzhiyun mutex_unlock(&client->modeset_mutex);
1748*4882a593Smuzhiyun
1749*4882a593Smuzhiyun drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
1750*4882a593Smuzhiyun drm_client_for_each_connector_iter(connector, &conn_iter) {
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun /* use first connected connector for the physical dimensions */
1753*4882a593Smuzhiyun if (connector->status == connector_status_connected) {
1754*4882a593Smuzhiyun info->var.width = connector->display_info.width_mm;
1755*4882a593Smuzhiyun info->var.height = connector->display_info.height_mm;
1756*4882a593Smuzhiyun break;
1757*4882a593Smuzhiyun }
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun drm_connector_list_iter_end(&conn_iter);
1760*4882a593Smuzhiyun
1761*4882a593Smuzhiyun switch (sw_rotations) {
1762*4882a593Smuzhiyun case DRM_MODE_ROTATE_0:
1763*4882a593Smuzhiyun info->fbcon_rotate_hint = FB_ROTATE_UR;
1764*4882a593Smuzhiyun break;
1765*4882a593Smuzhiyun case DRM_MODE_ROTATE_90:
1766*4882a593Smuzhiyun info->fbcon_rotate_hint = FB_ROTATE_CCW;
1767*4882a593Smuzhiyun break;
1768*4882a593Smuzhiyun case DRM_MODE_ROTATE_180:
1769*4882a593Smuzhiyun info->fbcon_rotate_hint = FB_ROTATE_UD;
1770*4882a593Smuzhiyun break;
1771*4882a593Smuzhiyun case DRM_MODE_ROTATE_270:
1772*4882a593Smuzhiyun info->fbcon_rotate_hint = FB_ROTATE_CW;
1773*4882a593Smuzhiyun break;
1774*4882a593Smuzhiyun default:
1775*4882a593Smuzhiyun /*
1776*4882a593Smuzhiyun * Multiple bits are set / multiple rotations requested
1777*4882a593Smuzhiyun * fbcon cannot handle separate rotation settings per
1778*4882a593Smuzhiyun * output, so fallback to unrotated.
1779*4882a593Smuzhiyun */
1780*4882a593Smuzhiyun info->fbcon_rotate_hint = FB_ROTATE_UR;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun
1784*4882a593Smuzhiyun /* Note: Drops fb_helper->lock before returning. */
1785*4882a593Smuzhiyun static int
__drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper * fb_helper,int bpp_sel)1786*4882a593Smuzhiyun __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
1787*4882a593Smuzhiyun int bpp_sel)
1788*4882a593Smuzhiyun {
1789*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
1790*4882a593Smuzhiyun struct fb_info *info;
1791*4882a593Smuzhiyun unsigned int width, height;
1792*4882a593Smuzhiyun int ret;
1793*4882a593Smuzhiyun
1794*4882a593Smuzhiyun width = dev->mode_config.max_width;
1795*4882a593Smuzhiyun height = dev->mode_config.max_height;
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun drm_client_modeset_probe(&fb_helper->client, width, height);
1798*4882a593Smuzhiyun ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1799*4882a593Smuzhiyun if (ret < 0) {
1800*4882a593Smuzhiyun if (ret == -EAGAIN) {
1801*4882a593Smuzhiyun fb_helper->preferred_bpp = bpp_sel;
1802*4882a593Smuzhiyun fb_helper->deferred_setup = true;
1803*4882a593Smuzhiyun ret = 0;
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1806*4882a593Smuzhiyun
1807*4882a593Smuzhiyun return ret;
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun drm_setup_crtcs_fb(fb_helper);
1810*4882a593Smuzhiyun
1811*4882a593Smuzhiyun fb_helper->deferred_setup = false;
1812*4882a593Smuzhiyun
1813*4882a593Smuzhiyun info = fb_helper->fbdev;
1814*4882a593Smuzhiyun info->var.pixclock = 0;
1815*4882a593Smuzhiyun /* Shamelessly allow physical address leaking to userspace */
1816*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
1817*4882a593Smuzhiyun if (!drm_leak_fbdev_smem)
1818*4882a593Smuzhiyun #endif
1819*4882a593Smuzhiyun /* don't leak any physical addresses to userspace */
1820*4882a593Smuzhiyun info->flags |= FBINFO_HIDE_SMEM_START;
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun /* Need to drop locks to avoid recursive deadlock in
1823*4882a593Smuzhiyun * register_framebuffer. This is ok because the only thing left to do is
1824*4882a593Smuzhiyun * register the fbdev emulation instance in kernel_fb_helper_list. */
1825*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun ret = register_framebuffer(info);
1828*4882a593Smuzhiyun if (ret < 0)
1829*4882a593Smuzhiyun return ret;
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun drm_info(dev, "fb%d: %s frame buffer device\n",
1832*4882a593Smuzhiyun info->node, info->fix.id);
1833*4882a593Smuzhiyun
1834*4882a593Smuzhiyun mutex_lock(&kernel_fb_helper_lock);
1835*4882a593Smuzhiyun if (list_empty(&kernel_fb_helper_list))
1836*4882a593Smuzhiyun register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
1837*4882a593Smuzhiyun
1838*4882a593Smuzhiyun list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
1839*4882a593Smuzhiyun mutex_unlock(&kernel_fb_helper_lock);
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun return 0;
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun /**
1845*4882a593Smuzhiyun * drm_fb_helper_initial_config - setup a sane initial connector configuration
1846*4882a593Smuzhiyun * @fb_helper: fb_helper device struct
1847*4882a593Smuzhiyun * @bpp_sel: bpp value to use for the framebuffer configuration
1848*4882a593Smuzhiyun *
1849*4882a593Smuzhiyun * Scans the CRTCs and connectors and tries to put together an initial setup.
1850*4882a593Smuzhiyun * At the moment, this is a cloned configuration across all heads with
1851*4882a593Smuzhiyun * a new framebuffer object as the backing store.
1852*4882a593Smuzhiyun *
1853*4882a593Smuzhiyun * Note that this also registers the fbdev and so allows userspace to call into
1854*4882a593Smuzhiyun * the driver through the fbdev interfaces.
1855*4882a593Smuzhiyun *
1856*4882a593Smuzhiyun * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
1857*4882a593Smuzhiyun * to let the driver allocate and initialize the fbdev info structure and the
1858*4882a593Smuzhiyun * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
1859*4882a593Smuzhiyun * as a helper to setup simple default values for the fbdev info structure.
1860*4882a593Smuzhiyun *
1861*4882a593Smuzhiyun * HANG DEBUGGING:
1862*4882a593Smuzhiyun *
1863*4882a593Smuzhiyun * When you have fbcon support built-in or already loaded, this function will do
1864*4882a593Smuzhiyun * a full modeset to setup the fbdev console. Due to locking misdesign in the
1865*4882a593Smuzhiyun * VT/fbdev subsystem that entire modeset sequence has to be done while holding
1866*4882a593Smuzhiyun * console_lock. Until console_unlock is called no dmesg lines will be sent out
1867*4882a593Smuzhiyun * to consoles, not even serial console. This means when your driver crashes,
1868*4882a593Smuzhiyun * you will see absolutely nothing else but a system stuck in this function,
1869*4882a593Smuzhiyun * with no further output. Any kind of printk() you place within your own driver
1870*4882a593Smuzhiyun * or in the drm core modeset code will also never show up.
1871*4882a593Smuzhiyun *
1872*4882a593Smuzhiyun * Standard debug practice is to run the fbcon setup without taking the
1873*4882a593Smuzhiyun * console_lock as a hack, to be able to see backtraces and crashes on the
1874*4882a593Smuzhiyun * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
1875*4882a593Smuzhiyun * cmdline option.
1876*4882a593Smuzhiyun *
1877*4882a593Smuzhiyun * The other option is to just disable fbdev emulation since very likely the
1878*4882a593Smuzhiyun * first modeset from userspace will crash in the same way, and is even easier
1879*4882a593Smuzhiyun * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
1880*4882a593Smuzhiyun * kernel cmdline option.
1881*4882a593Smuzhiyun *
1882*4882a593Smuzhiyun * RETURNS:
1883*4882a593Smuzhiyun * Zero if everything went ok, nonzero otherwise.
1884*4882a593Smuzhiyun */
drm_fb_helper_initial_config(struct drm_fb_helper * fb_helper,int bpp_sel)1885*4882a593Smuzhiyun int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1886*4882a593Smuzhiyun {
1887*4882a593Smuzhiyun int ret;
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun if (!drm_fbdev_emulation)
1890*4882a593Smuzhiyun return 0;
1891*4882a593Smuzhiyun
1892*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
1893*4882a593Smuzhiyun ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
1894*4882a593Smuzhiyun
1895*4882a593Smuzhiyun return ret;
1896*4882a593Smuzhiyun }
1897*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_initial_config);
1898*4882a593Smuzhiyun
1899*4882a593Smuzhiyun /**
1900*4882a593Smuzhiyun * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1901*4882a593Smuzhiyun * probing all the outputs attached to the fb
1902*4882a593Smuzhiyun * @fb_helper: driver-allocated fbdev helper, can be NULL
1903*4882a593Smuzhiyun *
1904*4882a593Smuzhiyun * Scan the connectors attached to the fb_helper and try to put together a
1905*4882a593Smuzhiyun * setup after notification of a change in output configuration.
1906*4882a593Smuzhiyun *
1907*4882a593Smuzhiyun * Called at runtime, takes the mode config locks to be able to check/change the
1908*4882a593Smuzhiyun * modeset configuration. Must be run from process context (which usually means
1909*4882a593Smuzhiyun * either the output polling work or a work item launched from the driver's
1910*4882a593Smuzhiyun * hotplug interrupt).
1911*4882a593Smuzhiyun *
1912*4882a593Smuzhiyun * Note that drivers may call this even before calling
1913*4882a593Smuzhiyun * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
1914*4882a593Smuzhiyun * for a race-free fbcon setup and will make sure that the fbdev emulation will
1915*4882a593Smuzhiyun * not miss any hotplug events.
1916*4882a593Smuzhiyun *
1917*4882a593Smuzhiyun * RETURNS:
1918*4882a593Smuzhiyun * 0 on success and a non-zero error code otherwise.
1919*4882a593Smuzhiyun */
drm_fb_helper_hotplug_event(struct drm_fb_helper * fb_helper)1920*4882a593Smuzhiyun int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1921*4882a593Smuzhiyun {
1922*4882a593Smuzhiyun int err = 0;
1923*4882a593Smuzhiyun
1924*4882a593Smuzhiyun if (!drm_fbdev_emulation || !fb_helper)
1925*4882a593Smuzhiyun return 0;
1926*4882a593Smuzhiyun
1927*4882a593Smuzhiyun mutex_lock(&fb_helper->lock);
1928*4882a593Smuzhiyun if (fb_helper->deferred_setup) {
1929*4882a593Smuzhiyun err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
1930*4882a593Smuzhiyun fb_helper->preferred_bpp);
1931*4882a593Smuzhiyun return err;
1932*4882a593Smuzhiyun }
1933*4882a593Smuzhiyun
1934*4882a593Smuzhiyun if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
1935*4882a593Smuzhiyun fb_helper->delayed_hotplug = true;
1936*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1937*4882a593Smuzhiyun return err;
1938*4882a593Smuzhiyun }
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun drm_master_internal_release(fb_helper->dev);
1941*4882a593Smuzhiyun
1942*4882a593Smuzhiyun drm_dbg_kms(fb_helper->dev, "\n");
1943*4882a593Smuzhiyun
1944*4882a593Smuzhiyun drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
1945*4882a593Smuzhiyun drm_setup_crtcs_fb(fb_helper);
1946*4882a593Smuzhiyun mutex_unlock(&fb_helper->lock);
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun drm_fb_helper_set_par(fb_helper->fbdev);
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun return 0;
1951*4882a593Smuzhiyun }
1952*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
1953*4882a593Smuzhiyun
1954*4882a593Smuzhiyun /**
1955*4882a593Smuzhiyun * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
1956*4882a593Smuzhiyun * @dev: DRM device
1957*4882a593Smuzhiyun *
1958*4882a593Smuzhiyun * This function can be used as the &drm_driver->lastclose callback for drivers
1959*4882a593Smuzhiyun * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
1960*4882a593Smuzhiyun */
drm_fb_helper_lastclose(struct drm_device * dev)1961*4882a593Smuzhiyun void drm_fb_helper_lastclose(struct drm_device *dev)
1962*4882a593Smuzhiyun {
1963*4882a593Smuzhiyun drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_lastclose);
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun /**
1968*4882a593Smuzhiyun * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
1969*4882a593Smuzhiyun * helper for fbdev emulation
1970*4882a593Smuzhiyun * @dev: DRM device
1971*4882a593Smuzhiyun *
1972*4882a593Smuzhiyun * This function can be used as the
1973*4882a593Smuzhiyun * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
1974*4882a593Smuzhiyun * need to call drm_fb_helper_hotplug_event().
1975*4882a593Smuzhiyun */
drm_fb_helper_output_poll_changed(struct drm_device * dev)1976*4882a593Smuzhiyun void drm_fb_helper_output_poll_changed(struct drm_device *dev)
1977*4882a593Smuzhiyun {
1978*4882a593Smuzhiyun drm_fb_helper_hotplug_event(dev->fb_helper);
1979*4882a593Smuzhiyun }
1980*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
1981*4882a593Smuzhiyun
1982*4882a593Smuzhiyun /* @user: 1=userspace, 0=fbcon */
drm_fbdev_fb_open(struct fb_info * info,int user)1983*4882a593Smuzhiyun static int drm_fbdev_fb_open(struct fb_info *info, int user)
1984*4882a593Smuzhiyun {
1985*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun /* No need to take a ref for fbcon because it unbinds on unregister */
1988*4882a593Smuzhiyun if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
1989*4882a593Smuzhiyun return -ENODEV;
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun return 0;
1992*4882a593Smuzhiyun }
1993*4882a593Smuzhiyun
drm_fbdev_fb_release(struct fb_info * info,int user)1994*4882a593Smuzhiyun static int drm_fbdev_fb_release(struct fb_info *info, int user)
1995*4882a593Smuzhiyun {
1996*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun if (user)
1999*4882a593Smuzhiyun module_put(fb_helper->dev->driver->fops->owner);
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun return 0;
2002*4882a593Smuzhiyun }
2003*4882a593Smuzhiyun
drm_fbdev_cleanup(struct drm_fb_helper * fb_helper)2004*4882a593Smuzhiyun static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
2005*4882a593Smuzhiyun {
2006*4882a593Smuzhiyun struct fb_info *fbi = fb_helper->fbdev;
2007*4882a593Smuzhiyun void *shadow = NULL;
2008*4882a593Smuzhiyun
2009*4882a593Smuzhiyun if (!fb_helper->dev)
2010*4882a593Smuzhiyun return;
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun if (fbi && fbi->fbdefio) {
2013*4882a593Smuzhiyun fb_deferred_io_cleanup(fbi);
2014*4882a593Smuzhiyun shadow = fbi->screen_buffer;
2015*4882a593Smuzhiyun }
2016*4882a593Smuzhiyun
2017*4882a593Smuzhiyun drm_fb_helper_fini(fb_helper);
2018*4882a593Smuzhiyun
2019*4882a593Smuzhiyun vfree(shadow);
2020*4882a593Smuzhiyun
2021*4882a593Smuzhiyun drm_client_framebuffer_delete(fb_helper->buffer);
2022*4882a593Smuzhiyun }
2023*4882a593Smuzhiyun
drm_fbdev_release(struct drm_fb_helper * fb_helper)2024*4882a593Smuzhiyun static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
2025*4882a593Smuzhiyun {
2026*4882a593Smuzhiyun drm_fbdev_cleanup(fb_helper);
2027*4882a593Smuzhiyun drm_client_release(&fb_helper->client);
2028*4882a593Smuzhiyun kfree(fb_helper);
2029*4882a593Smuzhiyun }
2030*4882a593Smuzhiyun
2031*4882a593Smuzhiyun /*
2032*4882a593Smuzhiyun * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
2033*4882a593Smuzhiyun * unregister_framebuffer() or fb_release().
2034*4882a593Smuzhiyun */
drm_fbdev_fb_destroy(struct fb_info * info)2035*4882a593Smuzhiyun static void drm_fbdev_fb_destroy(struct fb_info *info)
2036*4882a593Smuzhiyun {
2037*4882a593Smuzhiyun drm_fbdev_release(info->par);
2038*4882a593Smuzhiyun }
2039*4882a593Smuzhiyun
drm_fbdev_fb_mmap(struct fb_info * info,struct vm_area_struct * vma)2040*4882a593Smuzhiyun static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
2041*4882a593Smuzhiyun {
2042*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = info->par;
2043*4882a593Smuzhiyun
2044*4882a593Smuzhiyun if (fb_helper->dev->driver->gem_prime_mmap)
2045*4882a593Smuzhiyun return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
2046*4882a593Smuzhiyun else
2047*4882a593Smuzhiyun return -ENODEV;
2048*4882a593Smuzhiyun }
2049*4882a593Smuzhiyun
2050*4882a593Smuzhiyun static const struct fb_ops drm_fbdev_fb_ops = {
2051*4882a593Smuzhiyun .owner = THIS_MODULE,
2052*4882a593Smuzhiyun DRM_FB_HELPER_DEFAULT_OPS,
2053*4882a593Smuzhiyun .fb_open = drm_fbdev_fb_open,
2054*4882a593Smuzhiyun .fb_release = drm_fbdev_fb_release,
2055*4882a593Smuzhiyun .fb_destroy = drm_fbdev_fb_destroy,
2056*4882a593Smuzhiyun .fb_mmap = drm_fbdev_fb_mmap,
2057*4882a593Smuzhiyun .fb_read = drm_fb_helper_sys_read,
2058*4882a593Smuzhiyun .fb_write = drm_fb_helper_sys_write,
2059*4882a593Smuzhiyun .fb_fillrect = drm_fb_helper_sys_fillrect,
2060*4882a593Smuzhiyun .fb_copyarea = drm_fb_helper_sys_copyarea,
2061*4882a593Smuzhiyun .fb_imageblit = drm_fb_helper_sys_imageblit,
2062*4882a593Smuzhiyun };
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun static struct fb_deferred_io drm_fbdev_defio = {
2065*4882a593Smuzhiyun .delay = HZ / 20,
2066*4882a593Smuzhiyun .deferred_io = drm_fb_helper_deferred_io,
2067*4882a593Smuzhiyun };
2068*4882a593Smuzhiyun
2069*4882a593Smuzhiyun /*
2070*4882a593Smuzhiyun * This function uses the client API to create a framebuffer backed by a dumb buffer.
2071*4882a593Smuzhiyun *
2072*4882a593Smuzhiyun * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
2073*4882a593Smuzhiyun * fb_copyarea, fb_imageblit.
2074*4882a593Smuzhiyun */
drm_fb_helper_generic_probe(struct drm_fb_helper * fb_helper,struct drm_fb_helper_surface_size * sizes)2075*4882a593Smuzhiyun static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
2076*4882a593Smuzhiyun struct drm_fb_helper_surface_size *sizes)
2077*4882a593Smuzhiyun {
2078*4882a593Smuzhiyun struct drm_client_dev *client = &fb_helper->client;
2079*4882a593Smuzhiyun struct drm_device *dev = fb_helper->dev;
2080*4882a593Smuzhiyun struct drm_client_buffer *buffer;
2081*4882a593Smuzhiyun struct drm_framebuffer *fb;
2082*4882a593Smuzhiyun struct fb_info *fbi;
2083*4882a593Smuzhiyun u32 format;
2084*4882a593Smuzhiyun void *vaddr;
2085*4882a593Smuzhiyun
2086*4882a593Smuzhiyun drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
2087*4882a593Smuzhiyun sizes->surface_width, sizes->surface_height,
2088*4882a593Smuzhiyun sizes->surface_bpp);
2089*4882a593Smuzhiyun
2090*4882a593Smuzhiyun format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
2091*4882a593Smuzhiyun buffer = drm_client_framebuffer_create(client, sizes->surface_width,
2092*4882a593Smuzhiyun sizes->surface_height, format);
2093*4882a593Smuzhiyun if (IS_ERR(buffer))
2094*4882a593Smuzhiyun return PTR_ERR(buffer);
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun fb_helper->buffer = buffer;
2097*4882a593Smuzhiyun fb_helper->fb = buffer->fb;
2098*4882a593Smuzhiyun fb = buffer->fb;
2099*4882a593Smuzhiyun
2100*4882a593Smuzhiyun fbi = drm_fb_helper_alloc_fbi(fb_helper);
2101*4882a593Smuzhiyun if (IS_ERR(fbi))
2102*4882a593Smuzhiyun return PTR_ERR(fbi);
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun fbi->fbops = &drm_fbdev_fb_ops;
2105*4882a593Smuzhiyun fbi->screen_size = fb->height * fb->pitches[0];
2106*4882a593Smuzhiyun fbi->fix.smem_len = fbi->screen_size;
2107*4882a593Smuzhiyun
2108*4882a593Smuzhiyun drm_fb_helper_fill_info(fbi, fb_helper, sizes);
2109*4882a593Smuzhiyun
2110*4882a593Smuzhiyun if (drm_fbdev_use_shadow_fb(fb_helper)) {
2111*4882a593Smuzhiyun fbi->screen_buffer = vzalloc(fbi->screen_size);
2112*4882a593Smuzhiyun if (!fbi->screen_buffer)
2113*4882a593Smuzhiyun return -ENOMEM;
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun fbi->fbdefio = &drm_fbdev_defio;
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun fb_deferred_io_init(fbi);
2118*4882a593Smuzhiyun } else {
2119*4882a593Smuzhiyun /* buffer is mapped for HW framebuffer */
2120*4882a593Smuzhiyun vaddr = drm_client_buffer_vmap(fb_helper->buffer);
2121*4882a593Smuzhiyun if (IS_ERR(vaddr))
2122*4882a593Smuzhiyun return PTR_ERR(vaddr);
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun fbi->screen_buffer = vaddr;
2125*4882a593Smuzhiyun /* Shamelessly leak the physical address to user-space */
2126*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
2127*4882a593Smuzhiyun if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0)
2128*4882a593Smuzhiyun fbi->fix.smem_start =
2129*4882a593Smuzhiyun page_to_phys(virt_to_page(fbi->screen_buffer));
2130*4882a593Smuzhiyun #endif
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun return 0;
2134*4882a593Smuzhiyun }
2135*4882a593Smuzhiyun
2136*4882a593Smuzhiyun static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
2137*4882a593Smuzhiyun .fb_probe = drm_fb_helper_generic_probe,
2138*4882a593Smuzhiyun };
2139*4882a593Smuzhiyun
drm_fbdev_client_unregister(struct drm_client_dev * client)2140*4882a593Smuzhiyun static void drm_fbdev_client_unregister(struct drm_client_dev *client)
2141*4882a593Smuzhiyun {
2142*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2143*4882a593Smuzhiyun
2144*4882a593Smuzhiyun if (fb_helper->fbdev)
2145*4882a593Smuzhiyun /* drm_fbdev_fb_destroy() takes care of cleanup */
2146*4882a593Smuzhiyun drm_fb_helper_unregister_fbi(fb_helper);
2147*4882a593Smuzhiyun else
2148*4882a593Smuzhiyun drm_fbdev_release(fb_helper);
2149*4882a593Smuzhiyun }
2150*4882a593Smuzhiyun
drm_fbdev_client_restore(struct drm_client_dev * client)2151*4882a593Smuzhiyun static int drm_fbdev_client_restore(struct drm_client_dev *client)
2152*4882a593Smuzhiyun {
2153*4882a593Smuzhiyun drm_fb_helper_lastclose(client->dev);
2154*4882a593Smuzhiyun
2155*4882a593Smuzhiyun return 0;
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun
drm_fbdev_client_hotplug(struct drm_client_dev * client)2158*4882a593Smuzhiyun static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
2159*4882a593Smuzhiyun {
2160*4882a593Smuzhiyun struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
2161*4882a593Smuzhiyun struct drm_device *dev = client->dev;
2162*4882a593Smuzhiyun int ret;
2163*4882a593Smuzhiyun
2164*4882a593Smuzhiyun /* Setup is not retried if it has failed */
2165*4882a593Smuzhiyun if (!fb_helper->dev && fb_helper->funcs)
2166*4882a593Smuzhiyun return 0;
2167*4882a593Smuzhiyun
2168*4882a593Smuzhiyun if (dev->fb_helper)
2169*4882a593Smuzhiyun return drm_fb_helper_hotplug_event(dev->fb_helper);
2170*4882a593Smuzhiyun
2171*4882a593Smuzhiyun if (!dev->mode_config.num_connector) {
2172*4882a593Smuzhiyun drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n");
2173*4882a593Smuzhiyun return 0;
2174*4882a593Smuzhiyun }
2175*4882a593Smuzhiyun
2176*4882a593Smuzhiyun drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
2177*4882a593Smuzhiyun
2178*4882a593Smuzhiyun ret = drm_fb_helper_init(dev, fb_helper);
2179*4882a593Smuzhiyun if (ret)
2180*4882a593Smuzhiyun goto err;
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun if (!drm_drv_uses_atomic_modeset(dev))
2183*4882a593Smuzhiyun drm_helper_disable_unused_functions(dev);
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
2186*4882a593Smuzhiyun if (ret)
2187*4882a593Smuzhiyun goto err_cleanup;
2188*4882a593Smuzhiyun
2189*4882a593Smuzhiyun return 0;
2190*4882a593Smuzhiyun
2191*4882a593Smuzhiyun err_cleanup:
2192*4882a593Smuzhiyun drm_fbdev_cleanup(fb_helper);
2193*4882a593Smuzhiyun err:
2194*4882a593Smuzhiyun fb_helper->dev = NULL;
2195*4882a593Smuzhiyun fb_helper->fbdev = NULL;
2196*4882a593Smuzhiyun
2197*4882a593Smuzhiyun drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
2198*4882a593Smuzhiyun
2199*4882a593Smuzhiyun return ret;
2200*4882a593Smuzhiyun }
2201*4882a593Smuzhiyun
2202*4882a593Smuzhiyun static const struct drm_client_funcs drm_fbdev_client_funcs = {
2203*4882a593Smuzhiyun .owner = THIS_MODULE,
2204*4882a593Smuzhiyun .unregister = drm_fbdev_client_unregister,
2205*4882a593Smuzhiyun .restore = drm_fbdev_client_restore,
2206*4882a593Smuzhiyun .hotplug = drm_fbdev_client_hotplug,
2207*4882a593Smuzhiyun };
2208*4882a593Smuzhiyun
2209*4882a593Smuzhiyun /**
2210*4882a593Smuzhiyun * drm_fbdev_generic_setup() - Setup generic fbdev emulation
2211*4882a593Smuzhiyun * @dev: DRM device
2212*4882a593Smuzhiyun * @preferred_bpp: Preferred bits per pixel for the device.
2213*4882a593Smuzhiyun * @dev->mode_config.preferred_depth is used if this is zero.
2214*4882a593Smuzhiyun *
2215*4882a593Smuzhiyun * This function sets up generic fbdev emulation for drivers that supports
2216*4882a593Smuzhiyun * dumb buffers with a virtual address and that can be mmap'ed.
2217*4882a593Smuzhiyun * drm_fbdev_generic_setup() shall be called after the DRM driver registered
2218*4882a593Smuzhiyun * the new DRM device with drm_dev_register().
2219*4882a593Smuzhiyun *
2220*4882a593Smuzhiyun * Restore, hotplug events and teardown are all taken care of. Drivers that do
2221*4882a593Smuzhiyun * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
2222*4882a593Smuzhiyun * Simple drivers might use drm_mode_config_helper_suspend().
2223*4882a593Smuzhiyun *
2224*4882a593Smuzhiyun * Drivers that set the dirty callback on their framebuffer will get a shadow
2225*4882a593Smuzhiyun * fbdev buffer that is blitted onto the real buffer. This is done in order to
2226*4882a593Smuzhiyun * make deferred I/O work with all kinds of buffers. A shadow buffer can be
2227*4882a593Smuzhiyun * requested explicitly by setting struct drm_mode_config.prefer_shadow or
2228*4882a593Smuzhiyun * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
2229*4882a593Smuzhiyun * required to use generic fbdev emulation with SHMEM helpers.
2230*4882a593Smuzhiyun *
2231*4882a593Smuzhiyun * This function is safe to call even when there are no connectors present.
2232*4882a593Smuzhiyun * Setup will be retried on the next hotplug event.
2233*4882a593Smuzhiyun *
2234*4882a593Smuzhiyun * The fbdev is destroyed by drm_dev_unregister().
2235*4882a593Smuzhiyun */
drm_fbdev_generic_setup(struct drm_device * dev,unsigned int preferred_bpp)2236*4882a593Smuzhiyun void drm_fbdev_generic_setup(struct drm_device *dev,
2237*4882a593Smuzhiyun unsigned int preferred_bpp)
2238*4882a593Smuzhiyun {
2239*4882a593Smuzhiyun struct drm_fb_helper *fb_helper;
2240*4882a593Smuzhiyun int ret;
2241*4882a593Smuzhiyun
2242*4882a593Smuzhiyun drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
2243*4882a593Smuzhiyun drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
2244*4882a593Smuzhiyun
2245*4882a593Smuzhiyun if (!drm_fbdev_emulation)
2246*4882a593Smuzhiyun return;
2247*4882a593Smuzhiyun
2248*4882a593Smuzhiyun fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
2249*4882a593Smuzhiyun if (!fb_helper) {
2250*4882a593Smuzhiyun drm_err(dev, "Failed to allocate fb_helper\n");
2251*4882a593Smuzhiyun return;
2252*4882a593Smuzhiyun }
2253*4882a593Smuzhiyun
2254*4882a593Smuzhiyun ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
2255*4882a593Smuzhiyun if (ret) {
2256*4882a593Smuzhiyun kfree(fb_helper);
2257*4882a593Smuzhiyun drm_err(dev, "Failed to register client: %d\n", ret);
2258*4882a593Smuzhiyun return;
2259*4882a593Smuzhiyun }
2260*4882a593Smuzhiyun
2261*4882a593Smuzhiyun if (!preferred_bpp)
2262*4882a593Smuzhiyun preferred_bpp = dev->mode_config.preferred_depth;
2263*4882a593Smuzhiyun if (!preferred_bpp)
2264*4882a593Smuzhiyun preferred_bpp = 32;
2265*4882a593Smuzhiyun fb_helper->preferred_bpp = preferred_bpp;
2266*4882a593Smuzhiyun
2267*4882a593Smuzhiyun ret = drm_fbdev_client_hotplug(&fb_helper->client);
2268*4882a593Smuzhiyun if (ret)
2269*4882a593Smuzhiyun drm_dbg_kms(dev, "client hotplug ret=%d\n", ret);
2270*4882a593Smuzhiyun
2271*4882a593Smuzhiyun drm_client_register(&fb_helper->client);
2272*4882a593Smuzhiyun }
2273*4882a593Smuzhiyun EXPORT_SYMBOL(drm_fbdev_generic_setup);
2274