1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2014 Red Hat
3*4882a593Smuzhiyun * Copyright (C) 2014 Intel Corp.
4*4882a593Smuzhiyun * Copyright (C) 2018 Intel Corp.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
7*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
8*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
9*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
11*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
14*4882a593Smuzhiyun * all copies or substantial portions of the Software.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*4882a593Smuzhiyun * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Authors:
25*4882a593Smuzhiyun * Rob Clark <robdclark@gmail.com>
26*4882a593Smuzhiyun * Daniel Vetter <daniel.vetter@ffwll.ch>
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <drm/drm_atomic_uapi.h>
30*4882a593Smuzhiyun #include <drm/drm_atomic.h>
31*4882a593Smuzhiyun #include <drm/drm_print.h>
32*4882a593Smuzhiyun #include <drm/drm_drv.h>
33*4882a593Smuzhiyun #include <drm/drm_writeback.h>
34*4882a593Smuzhiyun #include <drm/drm_vblank.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include <linux/dma-fence.h>
37*4882a593Smuzhiyun #include <linux/uaccess.h>
38*4882a593Smuzhiyun #include <linux/sync_file.h>
39*4882a593Smuzhiyun #include <linux/file.h>
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include "drm_crtc_internal.h"
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun * DOC: overview
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * This file contains the marshalling and demarshalling glue for the atomic UAPI
47*4882a593Smuzhiyun * in all its forms: The monster ATOMIC IOCTL itself, code for GET_PROPERTY and
48*4882a593Smuzhiyun * SET_PROPERTY IOCTLs. Plus interface functions for compatibility helpers and
49*4882a593Smuzhiyun * drivers which have special needs to construct their own atomic updates, e.g.
50*4882a593Smuzhiyun * for load detect or similiar.
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun * drm_atomic_set_mode_for_crtc - set mode for CRTC
55*4882a593Smuzhiyun * @state: the CRTC whose incoming state to update
56*4882a593Smuzhiyun * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * Set a mode (originating from the kernel) on the desired CRTC state and update
59*4882a593Smuzhiyun * the enable property.
60*4882a593Smuzhiyun *
61*4882a593Smuzhiyun * RETURNS:
62*4882a593Smuzhiyun * Zero on success, error code on failure. Cannot return -EDEADLK.
63*4882a593Smuzhiyun */
drm_atomic_set_mode_for_crtc(struct drm_crtc_state * state,const struct drm_display_mode * mode)64*4882a593Smuzhiyun int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
65*4882a593Smuzhiyun const struct drm_display_mode *mode)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct drm_crtc *crtc = state->crtc;
68*4882a593Smuzhiyun struct drm_mode_modeinfo umode;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Early return for no change. */
71*4882a593Smuzhiyun if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
72*4882a593Smuzhiyun return 0;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun drm_property_blob_put(state->mode_blob);
75*4882a593Smuzhiyun state->mode_blob = NULL;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (mode) {
78*4882a593Smuzhiyun drm_mode_convert_to_umode(&umode, mode);
79*4882a593Smuzhiyun state->mode_blob =
80*4882a593Smuzhiyun drm_property_create_blob(state->crtc->dev,
81*4882a593Smuzhiyun sizeof(umode),
82*4882a593Smuzhiyun &umode);
83*4882a593Smuzhiyun if (IS_ERR(state->mode_blob))
84*4882a593Smuzhiyun return PTR_ERR(state->mode_blob);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun drm_mode_copy(&state->mode, mode);
87*4882a593Smuzhiyun state->enable = true;
88*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
89*4882a593Smuzhiyun mode->name, crtc->base.id, crtc->name, state);
90*4882a593Smuzhiyun } else {
91*4882a593Smuzhiyun memset(&state->mode, 0, sizeof(state->mode));
92*4882a593Smuzhiyun state->enable = false;
93*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
94*4882a593Smuzhiyun crtc->base.id, crtc->name, state);
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun return 0;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
103*4882a593Smuzhiyun * @state: the CRTC whose incoming state to update
104*4882a593Smuzhiyun * @blob: pointer to blob property to use for mode
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * Set a mode (originating from a blob property) on the desired CRTC state.
107*4882a593Smuzhiyun * This function will take a reference on the blob property for the CRTC state,
108*4882a593Smuzhiyun * and release the reference held on the state's existing mode property, if any
109*4882a593Smuzhiyun * was set.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * RETURNS:
112*4882a593Smuzhiyun * Zero on success, error code on failure. Cannot return -EDEADLK.
113*4882a593Smuzhiyun */
drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state * state,struct drm_property_blob * blob)114*4882a593Smuzhiyun int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
115*4882a593Smuzhiyun struct drm_property_blob *blob)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun struct drm_crtc *crtc = state->crtc;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (blob == state->mode_blob)
120*4882a593Smuzhiyun return 0;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun drm_property_blob_put(state->mode_blob);
123*4882a593Smuzhiyun state->mode_blob = NULL;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun memset(&state->mode, 0, sizeof(state->mode));
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (blob) {
128*4882a593Smuzhiyun int ret;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun if (blob->length != sizeof(struct drm_mode_modeinfo)) {
131*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[CRTC:%d:%s] bad mode blob length: %zu\n",
132*4882a593Smuzhiyun crtc->base.id, crtc->name,
133*4882a593Smuzhiyun blob->length);
134*4882a593Smuzhiyun return -EINVAL;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun ret = drm_mode_convert_umode(crtc->dev,
138*4882a593Smuzhiyun &state->mode, blob->data);
139*4882a593Smuzhiyun if (ret) {
140*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[CRTC:%d:%s] invalid mode (ret=%d, status=%s):\n",
141*4882a593Smuzhiyun crtc->base.id, crtc->name,
142*4882a593Smuzhiyun ret, drm_get_mode_status_name(state->mode.status));
143*4882a593Smuzhiyun drm_mode_debug_printmodeline(&state->mode);
144*4882a593Smuzhiyun return -EINVAL;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun state->mode_blob = drm_property_blob_get(blob);
148*4882a593Smuzhiyun state->enable = true;
149*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [MODE:%s] for [CRTC:%d:%s] state %p\n",
150*4882a593Smuzhiyun state->mode.name, crtc->base.id, crtc->name,
151*4882a593Smuzhiyun state);
152*4882a593Smuzhiyun } else {
153*4882a593Smuzhiyun state->enable = false;
154*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [NOMODE] for [CRTC:%d:%s] state %p\n",
155*4882a593Smuzhiyun crtc->base.id, crtc->name, state);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * drm_atomic_set_crtc_for_plane - set CRTC for plane
164*4882a593Smuzhiyun * @plane_state: the plane whose incoming state to update
165*4882a593Smuzhiyun * @crtc: CRTC to use for the plane
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * Changing the assigned CRTC for a plane requires us to grab the lock and state
168*4882a593Smuzhiyun * for the new CRTC, as needed. This function takes care of all these details
169*4882a593Smuzhiyun * besides updating the pointer in the state object itself.
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Returns:
172*4882a593Smuzhiyun * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
173*4882a593Smuzhiyun * then the w/w mutex code has detected a deadlock and the entire atomic
174*4882a593Smuzhiyun * sequence must be restarted. All other errors are fatal.
175*4882a593Smuzhiyun */
176*4882a593Smuzhiyun int
drm_atomic_set_crtc_for_plane(struct drm_plane_state * plane_state,struct drm_crtc * crtc)177*4882a593Smuzhiyun drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
178*4882a593Smuzhiyun struct drm_crtc *crtc)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct drm_plane *plane = plane_state->plane;
181*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
182*4882a593Smuzhiyun /* Nothing to do for same crtc*/
183*4882a593Smuzhiyun if (plane_state->crtc == crtc)
184*4882a593Smuzhiyun return 0;
185*4882a593Smuzhiyun if (plane_state->crtc) {
186*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(plane_state->state,
187*4882a593Smuzhiyun plane_state->crtc);
188*4882a593Smuzhiyun if (WARN_ON(IS_ERR(crtc_state)))
189*4882a593Smuzhiyun return PTR_ERR(crtc_state);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun crtc_state->plane_mask &= ~drm_plane_mask(plane);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun plane_state->crtc = crtc;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (crtc) {
197*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(plane_state->state,
198*4882a593Smuzhiyun crtc);
199*4882a593Smuzhiyun if (IS_ERR(crtc_state))
200*4882a593Smuzhiyun return PTR_ERR(crtc_state);
201*4882a593Smuzhiyun crtc_state->plane_mask |= drm_plane_mask(plane);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (crtc)
205*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [CRTC:%d:%s]\n",
206*4882a593Smuzhiyun plane->base.id, plane->name, plane_state,
207*4882a593Smuzhiyun crtc->base.id, crtc->name);
208*4882a593Smuzhiyun else
209*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Link [PLANE:%d:%s] state %p to [NOCRTC]\n",
210*4882a593Smuzhiyun plane->base.id, plane->name, plane_state);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return 0;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun * drm_atomic_set_fb_for_plane - set framebuffer for plane
218*4882a593Smuzhiyun * @plane_state: atomic state object for the plane
219*4882a593Smuzhiyun * @fb: fb to use for the plane
220*4882a593Smuzhiyun *
221*4882a593Smuzhiyun * Changing the assigned framebuffer for a plane requires us to grab a reference
222*4882a593Smuzhiyun * to the new fb and drop the reference to the old fb, if there is one. This
223*4882a593Smuzhiyun * function takes care of all these details besides updating the pointer in the
224*4882a593Smuzhiyun * state object itself.
225*4882a593Smuzhiyun */
226*4882a593Smuzhiyun void
drm_atomic_set_fb_for_plane(struct drm_plane_state * plane_state,struct drm_framebuffer * fb)227*4882a593Smuzhiyun drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
228*4882a593Smuzhiyun struct drm_framebuffer *fb)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun struct drm_plane *plane = plane_state->plane;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if (fb)
233*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [FB:%d] for [PLANE:%d:%s] state %p\n",
234*4882a593Smuzhiyun fb->base.id, plane->base.id, plane->name,
235*4882a593Smuzhiyun plane_state);
236*4882a593Smuzhiyun else
237*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [NOFB] for [PLANE:%d:%s] state %p\n",
238*4882a593Smuzhiyun plane->base.id, plane->name, plane_state);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun drm_framebuffer_assign(&plane_state->fb, fb);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /**
245*4882a593Smuzhiyun * drm_atomic_set_fence_for_plane - set fence for plane
246*4882a593Smuzhiyun * @plane_state: atomic state object for the plane
247*4882a593Smuzhiyun * @fence: dma_fence to use for the plane
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * Helper to setup the plane_state fence in case it is not set yet.
250*4882a593Smuzhiyun * By using this drivers doesn't need to worry if the user choose
251*4882a593Smuzhiyun * implicit or explicit fencing.
252*4882a593Smuzhiyun *
253*4882a593Smuzhiyun * This function will not set the fence to the state if it was set
254*4882a593Smuzhiyun * via explicit fencing interfaces on the atomic ioctl. In that case it will
255*4882a593Smuzhiyun * drop the reference to the fence as we are not storing it anywhere.
256*4882a593Smuzhiyun * Otherwise, if &drm_plane_state.fence is not set this function we just set it
257*4882a593Smuzhiyun * with the received implicit fence. In both cases this function consumes a
258*4882a593Smuzhiyun * reference for @fence.
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * This way explicit fencing can be used to overrule implicit fencing, which is
261*4882a593Smuzhiyun * important to make explicit fencing use-cases work: One example is using one
262*4882a593Smuzhiyun * buffer for 2 screens with different refresh rates. Implicit fencing will
263*4882a593Smuzhiyun * clamp rendering to the refresh rate of the slower screen, whereas explicit
264*4882a593Smuzhiyun * fence allows 2 independent render and display loops on a single buffer. If a
265*4882a593Smuzhiyun * driver allows obeys both implicit and explicit fences for plane updates, then
266*4882a593Smuzhiyun * it will break all the benefits of explicit fencing.
267*4882a593Smuzhiyun */
268*4882a593Smuzhiyun void
drm_atomic_set_fence_for_plane(struct drm_plane_state * plane_state,struct dma_fence * fence)269*4882a593Smuzhiyun drm_atomic_set_fence_for_plane(struct drm_plane_state *plane_state,
270*4882a593Smuzhiyun struct dma_fence *fence)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun if (plane_state->fence) {
273*4882a593Smuzhiyun dma_fence_put(fence);
274*4882a593Smuzhiyun return;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun plane_state->fence = fence;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_fence_for_plane);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun * drm_atomic_set_crtc_for_connector - set CRTC for connector
283*4882a593Smuzhiyun * @conn_state: atomic state object for the connector
284*4882a593Smuzhiyun * @crtc: CRTC to use for the connector
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * Changing the assigned CRTC for a connector requires us to grab the lock and
287*4882a593Smuzhiyun * state for the new CRTC, as needed. This function takes care of all these
288*4882a593Smuzhiyun * details besides updating the pointer in the state object itself.
289*4882a593Smuzhiyun *
290*4882a593Smuzhiyun * Returns:
291*4882a593Smuzhiyun * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
292*4882a593Smuzhiyun * then the w/w mutex code has detected a deadlock and the entire atomic
293*4882a593Smuzhiyun * sequence must be restarted. All other errors are fatal.
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun int
drm_atomic_set_crtc_for_connector(struct drm_connector_state * conn_state,struct drm_crtc * crtc)296*4882a593Smuzhiyun drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
297*4882a593Smuzhiyun struct drm_crtc *crtc)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct drm_connector *connector = conn_state->connector;
300*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (conn_state->crtc == crtc)
303*4882a593Smuzhiyun return 0;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (conn_state->crtc) {
306*4882a593Smuzhiyun crtc_state = drm_atomic_get_new_crtc_state(conn_state->state,
307*4882a593Smuzhiyun conn_state->crtc);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun crtc_state->connector_mask &=
310*4882a593Smuzhiyun ~drm_connector_mask(conn_state->connector);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun drm_connector_put(conn_state->connector);
313*4882a593Smuzhiyun conn_state->crtc = NULL;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun if (crtc) {
317*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
318*4882a593Smuzhiyun if (IS_ERR(crtc_state))
319*4882a593Smuzhiyun return PTR_ERR(crtc_state);
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun crtc_state->connector_mask |=
322*4882a593Smuzhiyun drm_connector_mask(conn_state->connector);
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun drm_connector_get(conn_state->connector);
325*4882a593Smuzhiyun conn_state->crtc = crtc;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [CRTC:%d:%s]\n",
328*4882a593Smuzhiyun connector->base.id, connector->name,
329*4882a593Smuzhiyun conn_state, crtc->base.id, crtc->name);
330*4882a593Smuzhiyun } else {
331*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Link [CONNECTOR:%d:%s] state %p to [NOCRTC]\n",
332*4882a593Smuzhiyun connector->base.id, connector->name,
333*4882a593Smuzhiyun conn_state);
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun return 0;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
339*4882a593Smuzhiyun
set_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc,s32 __user * fence_ptr)340*4882a593Smuzhiyun static void set_out_fence_for_crtc(struct drm_atomic_state *state,
341*4882a593Smuzhiyun struct drm_crtc *crtc, s32 __user *fence_ptr)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = fence_ptr;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
get_out_fence_for_crtc(struct drm_atomic_state * state,struct drm_crtc * crtc)346*4882a593Smuzhiyun static s32 __user *get_out_fence_for_crtc(struct drm_atomic_state *state,
347*4882a593Smuzhiyun struct drm_crtc *crtc)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun s32 __user *fence_ptr;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun fence_ptr = state->crtcs[drm_crtc_index(crtc)].out_fence_ptr;
352*4882a593Smuzhiyun state->crtcs[drm_crtc_index(crtc)].out_fence_ptr = NULL;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun return fence_ptr;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
set_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector,s32 __user * fence_ptr)357*4882a593Smuzhiyun static int set_out_fence_for_connector(struct drm_atomic_state *state,
358*4882a593Smuzhiyun struct drm_connector *connector,
359*4882a593Smuzhiyun s32 __user *fence_ptr)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun unsigned int index = drm_connector_index(connector);
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (!fence_ptr)
364*4882a593Smuzhiyun return 0;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun if (put_user(-1, fence_ptr))
367*4882a593Smuzhiyun return -EFAULT;
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun state->connectors[index].out_fence_ptr = fence_ptr;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
get_out_fence_for_connector(struct drm_atomic_state * state,struct drm_connector * connector)374*4882a593Smuzhiyun static s32 __user *get_out_fence_for_connector(struct drm_atomic_state *state,
375*4882a593Smuzhiyun struct drm_connector *connector)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun unsigned int index = drm_connector_index(connector);
378*4882a593Smuzhiyun s32 __user *fence_ptr;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun fence_ptr = state->connectors[index].out_fence_ptr;
381*4882a593Smuzhiyun state->connectors[index].out_fence_ptr = NULL;
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun return fence_ptr;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun static int
drm_atomic_replace_property_blob_from_id(struct drm_device * dev,struct drm_property_blob ** blob,uint64_t blob_id,ssize_t expected_size,ssize_t expected_elem_size,bool * replaced)387*4882a593Smuzhiyun drm_atomic_replace_property_blob_from_id(struct drm_device *dev,
388*4882a593Smuzhiyun struct drm_property_blob **blob,
389*4882a593Smuzhiyun uint64_t blob_id,
390*4882a593Smuzhiyun ssize_t expected_size,
391*4882a593Smuzhiyun ssize_t expected_elem_size,
392*4882a593Smuzhiyun bool *replaced)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun struct drm_property_blob *new_blob = NULL;
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun if (blob_id != 0) {
397*4882a593Smuzhiyun new_blob = drm_property_lookup_blob(dev, blob_id);
398*4882a593Smuzhiyun if (new_blob == NULL)
399*4882a593Smuzhiyun return -EINVAL;
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun if (expected_size > 0 &&
402*4882a593Smuzhiyun new_blob->length != expected_size) {
403*4882a593Smuzhiyun drm_property_blob_put(new_blob);
404*4882a593Smuzhiyun return -EINVAL;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun if (expected_elem_size > 0 &&
407*4882a593Smuzhiyun new_blob->length % expected_elem_size != 0) {
408*4882a593Smuzhiyun drm_property_blob_put(new_blob);
409*4882a593Smuzhiyun return -EINVAL;
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun *replaced |= drm_property_replace_blob(blob, new_blob);
414*4882a593Smuzhiyun drm_property_blob_put(new_blob);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun return 0;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
drm_atomic_crtc_set_property(struct drm_crtc * crtc,struct drm_crtc_state * state,struct drm_property * property,uint64_t val)419*4882a593Smuzhiyun static int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
420*4882a593Smuzhiyun struct drm_crtc_state *state, struct drm_property *property,
421*4882a593Smuzhiyun uint64_t val)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun struct drm_device *dev = crtc->dev;
424*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
425*4882a593Smuzhiyun bool replaced = false;
426*4882a593Smuzhiyun int ret;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (property == config->prop_active)
429*4882a593Smuzhiyun state->active = val;
430*4882a593Smuzhiyun else if (property == config->prop_mode_id) {
431*4882a593Smuzhiyun struct drm_property_blob *mode =
432*4882a593Smuzhiyun drm_property_lookup_blob(dev, val);
433*4882a593Smuzhiyun ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
434*4882a593Smuzhiyun drm_property_blob_put(mode);
435*4882a593Smuzhiyun return ret;
436*4882a593Smuzhiyun } else if (property == config->prop_vrr_enabled) {
437*4882a593Smuzhiyun state->vrr_enabled = val;
438*4882a593Smuzhiyun } else if (property == config->degamma_lut_property) {
439*4882a593Smuzhiyun ret = drm_atomic_replace_property_blob_from_id(dev,
440*4882a593Smuzhiyun &state->degamma_lut,
441*4882a593Smuzhiyun val,
442*4882a593Smuzhiyun -1, sizeof(struct drm_color_lut),
443*4882a593Smuzhiyun &replaced);
444*4882a593Smuzhiyun state->color_mgmt_changed |= replaced;
445*4882a593Smuzhiyun return ret;
446*4882a593Smuzhiyun } else if (property == config->ctm_property) {
447*4882a593Smuzhiyun ret = drm_atomic_replace_property_blob_from_id(dev,
448*4882a593Smuzhiyun &state->ctm,
449*4882a593Smuzhiyun val,
450*4882a593Smuzhiyun sizeof(struct drm_color_ctm), -1,
451*4882a593Smuzhiyun &replaced);
452*4882a593Smuzhiyun state->color_mgmt_changed |= replaced;
453*4882a593Smuzhiyun return ret;
454*4882a593Smuzhiyun } else if (property == config->gamma_lut_property) {
455*4882a593Smuzhiyun ret = drm_atomic_replace_property_blob_from_id(dev,
456*4882a593Smuzhiyun &state->gamma_lut,
457*4882a593Smuzhiyun val,
458*4882a593Smuzhiyun -1, sizeof(struct drm_color_lut),
459*4882a593Smuzhiyun &replaced);
460*4882a593Smuzhiyun state->color_mgmt_changed |= replaced;
461*4882a593Smuzhiyun return ret;
462*4882a593Smuzhiyun } else if (property == config->prop_out_fence_ptr) {
463*4882a593Smuzhiyun s32 __user *fence_ptr = u64_to_user_ptr(val);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun if (!fence_ptr)
466*4882a593Smuzhiyun return 0;
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (put_user(-1, fence_ptr))
469*4882a593Smuzhiyun return -EFAULT;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun set_out_fence_for_crtc(state->state, crtc, fence_ptr);
472*4882a593Smuzhiyun } else if (crtc->funcs->atomic_set_property) {
473*4882a593Smuzhiyun return crtc->funcs->atomic_set_property(crtc, state, property, val);
474*4882a593Smuzhiyun } else {
475*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[CRTC:%d:%s] unknown property [PROP:%d:%s]]\n",
476*4882a593Smuzhiyun crtc->base.id, crtc->name,
477*4882a593Smuzhiyun property->base.id, property->name);
478*4882a593Smuzhiyun return -EINVAL;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun static int
drm_atomic_crtc_get_property(struct drm_crtc * crtc,const struct drm_crtc_state * state,struct drm_property * property,uint64_t * val)485*4882a593Smuzhiyun drm_atomic_crtc_get_property(struct drm_crtc *crtc,
486*4882a593Smuzhiyun const struct drm_crtc_state *state,
487*4882a593Smuzhiyun struct drm_property *property, uint64_t *val)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun struct drm_device *dev = crtc->dev;
490*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun if (property == config->prop_active)
493*4882a593Smuzhiyun *val = drm_atomic_crtc_effectively_active(state);
494*4882a593Smuzhiyun else if (property == config->prop_mode_id)
495*4882a593Smuzhiyun *val = (state->mode_blob) ? state->mode_blob->base.id : 0;
496*4882a593Smuzhiyun else if (property == config->prop_vrr_enabled)
497*4882a593Smuzhiyun *val = state->vrr_enabled;
498*4882a593Smuzhiyun else if (property == config->degamma_lut_property)
499*4882a593Smuzhiyun *val = (state->degamma_lut) ? state->degamma_lut->base.id : 0;
500*4882a593Smuzhiyun else if (property == config->ctm_property)
501*4882a593Smuzhiyun *val = (state->ctm) ? state->ctm->base.id : 0;
502*4882a593Smuzhiyun else if (property == config->gamma_lut_property)
503*4882a593Smuzhiyun *val = (state->gamma_lut) ? state->gamma_lut->base.id : 0;
504*4882a593Smuzhiyun else if (property == config->prop_out_fence_ptr)
505*4882a593Smuzhiyun *val = 0;
506*4882a593Smuzhiyun else if (crtc->funcs->atomic_get_property)
507*4882a593Smuzhiyun return crtc->funcs->atomic_get_property(crtc, state, property, val);
508*4882a593Smuzhiyun else
509*4882a593Smuzhiyun return -EINVAL;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun return 0;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
drm_atomic_plane_set_property(struct drm_plane * plane,struct drm_plane_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val)514*4882a593Smuzhiyun static int drm_atomic_plane_set_property(struct drm_plane *plane,
515*4882a593Smuzhiyun struct drm_plane_state *state, struct drm_file *file_priv,
516*4882a593Smuzhiyun struct drm_property *property, uint64_t val)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun struct drm_device *dev = plane->dev;
519*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
520*4882a593Smuzhiyun bool replaced = false;
521*4882a593Smuzhiyun int ret;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (property == config->prop_fb_id) {
524*4882a593Smuzhiyun struct drm_framebuffer *fb;
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun fb = drm_framebuffer_lookup(dev, file_priv, val);
527*4882a593Smuzhiyun drm_atomic_set_fb_for_plane(state, fb);
528*4882a593Smuzhiyun if (fb)
529*4882a593Smuzhiyun drm_framebuffer_put(fb);
530*4882a593Smuzhiyun } else if (property == config->prop_in_fence_fd) {
531*4882a593Smuzhiyun if (state->fence)
532*4882a593Smuzhiyun return -EINVAL;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun if (U642I64(val) == -1)
535*4882a593Smuzhiyun return 0;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun state->fence = sync_file_get_fence(val);
538*4882a593Smuzhiyun if (!state->fence)
539*4882a593Smuzhiyun return -EINVAL;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun } else if (property == config->prop_crtc_id) {
542*4882a593Smuzhiyun struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun if (val && !crtc)
545*4882a593Smuzhiyun return -EACCES;
546*4882a593Smuzhiyun return drm_atomic_set_crtc_for_plane(state, crtc);
547*4882a593Smuzhiyun } else if (property == config->prop_crtc_x) {
548*4882a593Smuzhiyun state->crtc_x = U642I64(val);
549*4882a593Smuzhiyun } else if (property == config->prop_crtc_y) {
550*4882a593Smuzhiyun state->crtc_y = U642I64(val);
551*4882a593Smuzhiyun } else if (property == config->prop_crtc_w) {
552*4882a593Smuzhiyun state->crtc_w = val;
553*4882a593Smuzhiyun } else if (property == config->prop_crtc_h) {
554*4882a593Smuzhiyun state->crtc_h = val;
555*4882a593Smuzhiyun } else if (property == config->prop_src_x) {
556*4882a593Smuzhiyun state->src_x = val;
557*4882a593Smuzhiyun } else if (property == config->prop_src_y) {
558*4882a593Smuzhiyun state->src_y = val;
559*4882a593Smuzhiyun } else if (property == config->prop_src_w) {
560*4882a593Smuzhiyun state->src_w = val;
561*4882a593Smuzhiyun } else if (property == config->prop_src_h) {
562*4882a593Smuzhiyun state->src_h = val;
563*4882a593Smuzhiyun } else if (property == plane->alpha_property) {
564*4882a593Smuzhiyun state->alpha = val;
565*4882a593Smuzhiyun } else if (property == plane->blend_mode_property) {
566*4882a593Smuzhiyun state->pixel_blend_mode = val;
567*4882a593Smuzhiyun } else if (property == plane->rotation_property) {
568*4882a593Smuzhiyun if (!is_power_of_2(val & DRM_MODE_ROTATE_MASK)) {
569*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[PLANE:%d:%s] bad rotation bitmask: 0x%llx\n",
570*4882a593Smuzhiyun plane->base.id, plane->name, val);
571*4882a593Smuzhiyun return -EINVAL;
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun state->rotation = val;
574*4882a593Smuzhiyun } else if (property == plane->zpos_property) {
575*4882a593Smuzhiyun state->zpos = val;
576*4882a593Smuzhiyun } else if (property == plane->color_encoding_property) {
577*4882a593Smuzhiyun state->color_encoding = val;
578*4882a593Smuzhiyun } else if (property == plane->color_range_property) {
579*4882a593Smuzhiyun state->color_range = val;
580*4882a593Smuzhiyun } else if (property == config->prop_fb_damage_clips) {
581*4882a593Smuzhiyun ret = drm_atomic_replace_property_blob_from_id(dev,
582*4882a593Smuzhiyun &state->fb_damage_clips,
583*4882a593Smuzhiyun val,
584*4882a593Smuzhiyun -1,
585*4882a593Smuzhiyun sizeof(struct drm_rect),
586*4882a593Smuzhiyun &replaced);
587*4882a593Smuzhiyun return ret;
588*4882a593Smuzhiyun } else if (plane->funcs->atomic_set_property) {
589*4882a593Smuzhiyun return plane->funcs->atomic_set_property(plane, state,
590*4882a593Smuzhiyun property, val);
591*4882a593Smuzhiyun } else {
592*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[PLANE:%d:%s] unknown property [PROP:%d:%s]]\n",
593*4882a593Smuzhiyun plane->base.id, plane->name,
594*4882a593Smuzhiyun property->base.id, property->name);
595*4882a593Smuzhiyun return -EINVAL;
596*4882a593Smuzhiyun }
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun return 0;
599*4882a593Smuzhiyun }
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun static int
drm_atomic_plane_get_property(struct drm_plane * plane,const struct drm_plane_state * state,struct drm_property * property,uint64_t * val)602*4882a593Smuzhiyun drm_atomic_plane_get_property(struct drm_plane *plane,
603*4882a593Smuzhiyun const struct drm_plane_state *state,
604*4882a593Smuzhiyun struct drm_property *property, uint64_t *val)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun struct drm_device *dev = plane->dev;
607*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun if (property == config->prop_fb_id) {
610*4882a593Smuzhiyun *val = (state->fb) ? state->fb->base.id : 0;
611*4882a593Smuzhiyun } else if (property == config->prop_in_fence_fd) {
612*4882a593Smuzhiyun *val = -1;
613*4882a593Smuzhiyun } else if (property == config->prop_crtc_id) {
614*4882a593Smuzhiyun *val = (state->crtc) ? state->crtc->base.id : 0;
615*4882a593Smuzhiyun } else if (property == config->prop_crtc_x) {
616*4882a593Smuzhiyun *val = I642U64(state->crtc_x);
617*4882a593Smuzhiyun } else if (property == config->prop_crtc_y) {
618*4882a593Smuzhiyun *val = I642U64(state->crtc_y);
619*4882a593Smuzhiyun } else if (property == config->prop_crtc_w) {
620*4882a593Smuzhiyun *val = state->crtc_w;
621*4882a593Smuzhiyun } else if (property == config->prop_crtc_h) {
622*4882a593Smuzhiyun *val = state->crtc_h;
623*4882a593Smuzhiyun } else if (property == config->prop_src_x) {
624*4882a593Smuzhiyun *val = state->src_x;
625*4882a593Smuzhiyun } else if (property == config->prop_src_y) {
626*4882a593Smuzhiyun *val = state->src_y;
627*4882a593Smuzhiyun } else if (property == config->prop_src_w) {
628*4882a593Smuzhiyun *val = state->src_w;
629*4882a593Smuzhiyun } else if (property == config->prop_src_h) {
630*4882a593Smuzhiyun *val = state->src_h;
631*4882a593Smuzhiyun } else if (property == plane->alpha_property) {
632*4882a593Smuzhiyun *val = state->alpha;
633*4882a593Smuzhiyun } else if (property == plane->blend_mode_property) {
634*4882a593Smuzhiyun *val = state->pixel_blend_mode;
635*4882a593Smuzhiyun } else if (property == plane->rotation_property) {
636*4882a593Smuzhiyun *val = state->rotation;
637*4882a593Smuzhiyun } else if (property == plane->zpos_property) {
638*4882a593Smuzhiyun *val = state->zpos;
639*4882a593Smuzhiyun } else if (property == plane->color_encoding_property) {
640*4882a593Smuzhiyun *val = state->color_encoding;
641*4882a593Smuzhiyun } else if (property == plane->color_range_property) {
642*4882a593Smuzhiyun *val = state->color_range;
643*4882a593Smuzhiyun } else if (property == config->prop_fb_damage_clips) {
644*4882a593Smuzhiyun *val = (state->fb_damage_clips) ?
645*4882a593Smuzhiyun state->fb_damage_clips->base.id : 0;
646*4882a593Smuzhiyun } else if (plane->funcs->atomic_get_property) {
647*4882a593Smuzhiyun return plane->funcs->atomic_get_property(plane, state, property, val);
648*4882a593Smuzhiyun } else {
649*4882a593Smuzhiyun return -EINVAL;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun return 0;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
drm_atomic_set_writeback_fb_for_connector(struct drm_connector_state * conn_state,struct drm_framebuffer * fb)655*4882a593Smuzhiyun static int drm_atomic_set_writeback_fb_for_connector(
656*4882a593Smuzhiyun struct drm_connector_state *conn_state,
657*4882a593Smuzhiyun struct drm_framebuffer *fb)
658*4882a593Smuzhiyun {
659*4882a593Smuzhiyun int ret;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun ret = drm_writeback_set_fb(conn_state, fb);
662*4882a593Smuzhiyun if (ret < 0)
663*4882a593Smuzhiyun return ret;
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun if (fb)
666*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [FB:%d] for connector state %p\n",
667*4882a593Smuzhiyun fb->base.id, conn_state);
668*4882a593Smuzhiyun else
669*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Set [NOFB] for connector state %p\n",
670*4882a593Smuzhiyun conn_state);
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun return 0;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun
drm_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_file * file_priv,struct drm_property * property,uint64_t val)675*4882a593Smuzhiyun static int drm_atomic_connector_set_property(struct drm_connector *connector,
676*4882a593Smuzhiyun struct drm_connector_state *state, struct drm_file *file_priv,
677*4882a593Smuzhiyun struct drm_property *property, uint64_t val)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun struct drm_device *dev = connector->dev;
680*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
681*4882a593Smuzhiyun bool replaced = false;
682*4882a593Smuzhiyun int ret;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun if (property == config->prop_crtc_id) {
685*4882a593Smuzhiyun struct drm_crtc *crtc = drm_crtc_find(dev, file_priv, val);
686*4882a593Smuzhiyun
687*4882a593Smuzhiyun if (val && !crtc)
688*4882a593Smuzhiyun return -EACCES;
689*4882a593Smuzhiyun return drm_atomic_set_crtc_for_connector(state, crtc);
690*4882a593Smuzhiyun } else if (property == config->dpms_property) {
691*4882a593Smuzhiyun /* setting DPMS property requires special handling, which
692*4882a593Smuzhiyun * is done in legacy setprop path for us. Disallow (for
693*4882a593Smuzhiyun * now?) atomic writes to DPMS property:
694*4882a593Smuzhiyun */
695*4882a593Smuzhiyun return -EINVAL;
696*4882a593Smuzhiyun } else if (property == config->tv_select_subconnector_property) {
697*4882a593Smuzhiyun state->tv.subconnector = val;
698*4882a593Smuzhiyun } else if (property == config->tv_left_margin_property) {
699*4882a593Smuzhiyun state->tv.margins.left = val;
700*4882a593Smuzhiyun } else if (property == config->tv_right_margin_property) {
701*4882a593Smuzhiyun state->tv.margins.right = val;
702*4882a593Smuzhiyun } else if (property == config->tv_top_margin_property) {
703*4882a593Smuzhiyun state->tv.margins.top = val;
704*4882a593Smuzhiyun } else if (property == config->tv_bottom_margin_property) {
705*4882a593Smuzhiyun state->tv.margins.bottom = val;
706*4882a593Smuzhiyun } else if (property == config->tv_mode_property) {
707*4882a593Smuzhiyun state->tv.mode = val;
708*4882a593Smuzhiyun } else if (property == config->tv_brightness_property) {
709*4882a593Smuzhiyun state->tv.brightness = val;
710*4882a593Smuzhiyun } else if (property == config->tv_contrast_property) {
711*4882a593Smuzhiyun state->tv.contrast = val;
712*4882a593Smuzhiyun } else if (property == config->tv_flicker_reduction_property) {
713*4882a593Smuzhiyun state->tv.flicker_reduction = val;
714*4882a593Smuzhiyun } else if (property == config->tv_overscan_property) {
715*4882a593Smuzhiyun state->tv.overscan = val;
716*4882a593Smuzhiyun } else if (property == config->tv_saturation_property) {
717*4882a593Smuzhiyun state->tv.saturation = val;
718*4882a593Smuzhiyun } else if (property == config->tv_hue_property) {
719*4882a593Smuzhiyun state->tv.hue = val;
720*4882a593Smuzhiyun } else if (property == config->link_status_property) {
721*4882a593Smuzhiyun /* Never downgrade from GOOD to BAD on userspace's request here,
722*4882a593Smuzhiyun * only hw issues can do that.
723*4882a593Smuzhiyun *
724*4882a593Smuzhiyun * For an atomic property the userspace doesn't need to be able
725*4882a593Smuzhiyun * to understand all the properties, but needs to be able to
726*4882a593Smuzhiyun * restore the state it wants on VT switch. So if the userspace
727*4882a593Smuzhiyun * tries to change the link_status from GOOD to BAD, driver
728*4882a593Smuzhiyun * silently rejects it and returns a 0. This prevents userspace
729*4882a593Smuzhiyun * from accidently breaking the display when it restores the
730*4882a593Smuzhiyun * state.
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun if (state->link_status != DRM_LINK_STATUS_GOOD)
733*4882a593Smuzhiyun state->link_status = val;
734*4882a593Smuzhiyun } else if (property == config->hdr_output_metadata_property) {
735*4882a593Smuzhiyun ret = drm_atomic_replace_property_blob_from_id(dev,
736*4882a593Smuzhiyun &state->hdr_output_metadata,
737*4882a593Smuzhiyun val,
738*4882a593Smuzhiyun sizeof(struct hdr_output_metadata), -1,
739*4882a593Smuzhiyun &replaced);
740*4882a593Smuzhiyun return ret;
741*4882a593Smuzhiyun } else if (property == config->aspect_ratio_property) {
742*4882a593Smuzhiyun state->picture_aspect_ratio = val;
743*4882a593Smuzhiyun } else if (property == config->content_type_property) {
744*4882a593Smuzhiyun state->content_type = val;
745*4882a593Smuzhiyun } else if (property == connector->scaling_mode_property) {
746*4882a593Smuzhiyun state->scaling_mode = val;
747*4882a593Smuzhiyun } else if (property == config->content_protection_property) {
748*4882a593Smuzhiyun if (val == DRM_MODE_CONTENT_PROTECTION_ENABLED) {
749*4882a593Smuzhiyun DRM_DEBUG_KMS("only drivers can set CP Enabled\n");
750*4882a593Smuzhiyun return -EINVAL;
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun state->content_protection = val;
753*4882a593Smuzhiyun } else if (property == config->hdcp_content_type_property) {
754*4882a593Smuzhiyun state->hdcp_content_type = val;
755*4882a593Smuzhiyun } else if (property == connector->colorspace_property) {
756*4882a593Smuzhiyun state->colorspace = val;
757*4882a593Smuzhiyun } else if (property == config->writeback_fb_id_property) {
758*4882a593Smuzhiyun struct drm_framebuffer *fb;
759*4882a593Smuzhiyun int ret;
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun fb = drm_framebuffer_lookup(dev, file_priv, val);
762*4882a593Smuzhiyun ret = drm_atomic_set_writeback_fb_for_connector(state, fb);
763*4882a593Smuzhiyun if (fb)
764*4882a593Smuzhiyun drm_framebuffer_put(fb);
765*4882a593Smuzhiyun return ret;
766*4882a593Smuzhiyun } else if (property == config->writeback_out_fence_ptr_property) {
767*4882a593Smuzhiyun s32 __user *fence_ptr = u64_to_user_ptr(val);
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun return set_out_fence_for_connector(state->state, connector,
770*4882a593Smuzhiyun fence_ptr);
771*4882a593Smuzhiyun } else if (property == connector->max_bpc_property) {
772*4882a593Smuzhiyun state->max_requested_bpc = val;
773*4882a593Smuzhiyun } else if (connector->funcs->atomic_set_property) {
774*4882a593Smuzhiyun return connector->funcs->atomic_set_property(connector,
775*4882a593Smuzhiyun state, property, val);
776*4882a593Smuzhiyun } else {
777*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("[CONNECTOR:%d:%s] unknown property [PROP:%d:%s]]\n",
778*4882a593Smuzhiyun connector->base.id, connector->name,
779*4882a593Smuzhiyun property->base.id, property->name);
780*4882a593Smuzhiyun return -EINVAL;
781*4882a593Smuzhiyun }
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun return 0;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun static int
drm_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)787*4882a593Smuzhiyun drm_atomic_connector_get_property(struct drm_connector *connector,
788*4882a593Smuzhiyun const struct drm_connector_state *state,
789*4882a593Smuzhiyun struct drm_property *property, uint64_t *val)
790*4882a593Smuzhiyun {
791*4882a593Smuzhiyun struct drm_device *dev = connector->dev;
792*4882a593Smuzhiyun struct drm_mode_config *config = &dev->mode_config;
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun if (property == config->prop_crtc_id) {
795*4882a593Smuzhiyun *val = (state->crtc) ? state->crtc->base.id : 0;
796*4882a593Smuzhiyun } else if (property == config->dpms_property) {
797*4882a593Smuzhiyun if (state->crtc && state->crtc->state->self_refresh_active)
798*4882a593Smuzhiyun *val = DRM_MODE_DPMS_ON;
799*4882a593Smuzhiyun else
800*4882a593Smuzhiyun *val = connector->dpms;
801*4882a593Smuzhiyun } else if (property == config->tv_select_subconnector_property) {
802*4882a593Smuzhiyun *val = state->tv.subconnector;
803*4882a593Smuzhiyun } else if (property == config->tv_left_margin_property) {
804*4882a593Smuzhiyun *val = state->tv.margins.left;
805*4882a593Smuzhiyun } else if (property == config->tv_right_margin_property) {
806*4882a593Smuzhiyun *val = state->tv.margins.right;
807*4882a593Smuzhiyun } else if (property == config->tv_top_margin_property) {
808*4882a593Smuzhiyun *val = state->tv.margins.top;
809*4882a593Smuzhiyun } else if (property == config->tv_bottom_margin_property) {
810*4882a593Smuzhiyun *val = state->tv.margins.bottom;
811*4882a593Smuzhiyun } else if (property == config->tv_mode_property) {
812*4882a593Smuzhiyun *val = state->tv.mode;
813*4882a593Smuzhiyun } else if (property == config->tv_brightness_property) {
814*4882a593Smuzhiyun *val = state->tv.brightness;
815*4882a593Smuzhiyun } else if (property == config->tv_contrast_property) {
816*4882a593Smuzhiyun *val = state->tv.contrast;
817*4882a593Smuzhiyun } else if (property == config->tv_flicker_reduction_property) {
818*4882a593Smuzhiyun *val = state->tv.flicker_reduction;
819*4882a593Smuzhiyun } else if (property == config->tv_overscan_property) {
820*4882a593Smuzhiyun *val = state->tv.overscan;
821*4882a593Smuzhiyun } else if (property == config->tv_saturation_property) {
822*4882a593Smuzhiyun *val = state->tv.saturation;
823*4882a593Smuzhiyun } else if (property == config->tv_hue_property) {
824*4882a593Smuzhiyun *val = state->tv.hue;
825*4882a593Smuzhiyun } else if (property == config->link_status_property) {
826*4882a593Smuzhiyun *val = state->link_status;
827*4882a593Smuzhiyun } else if (property == config->aspect_ratio_property) {
828*4882a593Smuzhiyun *val = state->picture_aspect_ratio;
829*4882a593Smuzhiyun } else if (property == config->content_type_property) {
830*4882a593Smuzhiyun *val = state->content_type;
831*4882a593Smuzhiyun } else if (property == connector->colorspace_property) {
832*4882a593Smuzhiyun *val = state->colorspace;
833*4882a593Smuzhiyun } else if (property == connector->scaling_mode_property) {
834*4882a593Smuzhiyun *val = state->scaling_mode;
835*4882a593Smuzhiyun } else if (property == config->hdr_output_metadata_property) {
836*4882a593Smuzhiyun *val = state->hdr_output_metadata ?
837*4882a593Smuzhiyun state->hdr_output_metadata->base.id : 0;
838*4882a593Smuzhiyun } else if (property == config->content_protection_property) {
839*4882a593Smuzhiyun *val = state->content_protection;
840*4882a593Smuzhiyun } else if (property == config->hdcp_content_type_property) {
841*4882a593Smuzhiyun *val = state->hdcp_content_type;
842*4882a593Smuzhiyun } else if (property == config->writeback_fb_id_property) {
843*4882a593Smuzhiyun /* Writeback framebuffer is one-shot, write and forget */
844*4882a593Smuzhiyun *val = 0;
845*4882a593Smuzhiyun } else if (property == config->writeback_out_fence_ptr_property) {
846*4882a593Smuzhiyun *val = 0;
847*4882a593Smuzhiyun } else if (property == connector->max_bpc_property) {
848*4882a593Smuzhiyun *val = state->max_requested_bpc;
849*4882a593Smuzhiyun } else if (connector->funcs->atomic_get_property) {
850*4882a593Smuzhiyun return connector->funcs->atomic_get_property(connector,
851*4882a593Smuzhiyun state, property, val);
852*4882a593Smuzhiyun } else {
853*4882a593Smuzhiyun return -EINVAL;
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun return 0;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun
drm_atomic_get_property(struct drm_mode_object * obj,struct drm_property * property,uint64_t * val)859*4882a593Smuzhiyun int drm_atomic_get_property(struct drm_mode_object *obj,
860*4882a593Smuzhiyun struct drm_property *property, uint64_t *val)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun struct drm_device *dev = property->dev;
863*4882a593Smuzhiyun int ret;
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun switch (obj->type) {
866*4882a593Smuzhiyun case DRM_MODE_OBJECT_CONNECTOR: {
867*4882a593Smuzhiyun struct drm_connector *connector = obj_to_connector(obj);
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
870*4882a593Smuzhiyun ret = drm_atomic_connector_get_property(connector,
871*4882a593Smuzhiyun connector->state, property, val);
872*4882a593Smuzhiyun break;
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun case DRM_MODE_OBJECT_CRTC: {
875*4882a593Smuzhiyun struct drm_crtc *crtc = obj_to_crtc(obj);
876*4882a593Smuzhiyun
877*4882a593Smuzhiyun WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
878*4882a593Smuzhiyun ret = drm_atomic_crtc_get_property(crtc,
879*4882a593Smuzhiyun crtc->state, property, val);
880*4882a593Smuzhiyun break;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun case DRM_MODE_OBJECT_PLANE: {
883*4882a593Smuzhiyun struct drm_plane *plane = obj_to_plane(obj);
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun WARN_ON(!drm_modeset_is_locked(&plane->mutex));
886*4882a593Smuzhiyun ret = drm_atomic_plane_get_property(plane,
887*4882a593Smuzhiyun plane->state, property, val);
888*4882a593Smuzhiyun break;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun default:
891*4882a593Smuzhiyun ret = -EINVAL;
892*4882a593Smuzhiyun break;
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun return ret;
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun /*
899*4882a593Smuzhiyun * The big monster ioctl
900*4882a593Smuzhiyun */
901*4882a593Smuzhiyun
create_vblank_event(struct drm_crtc * crtc,uint64_t user_data)902*4882a593Smuzhiyun static struct drm_pending_vblank_event *create_vblank_event(
903*4882a593Smuzhiyun struct drm_crtc *crtc, uint64_t user_data)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun struct drm_pending_vblank_event *e = NULL;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun e = kzalloc(sizeof *e, GFP_KERNEL);
908*4882a593Smuzhiyun if (!e)
909*4882a593Smuzhiyun return NULL;
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
912*4882a593Smuzhiyun e->event.base.length = sizeof(e->event);
913*4882a593Smuzhiyun e->event.vbl.crtc_id = crtc->base.id;
914*4882a593Smuzhiyun e->event.vbl.user_data = user_data;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun return e;
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun
drm_atomic_connector_commit_dpms(struct drm_atomic_state * state,struct drm_connector * connector,int mode)919*4882a593Smuzhiyun int drm_atomic_connector_commit_dpms(struct drm_atomic_state *state,
920*4882a593Smuzhiyun struct drm_connector *connector,
921*4882a593Smuzhiyun int mode)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun struct drm_connector *tmp_connector;
924*4882a593Smuzhiyun struct drm_connector_state *new_conn_state;
925*4882a593Smuzhiyun struct drm_crtc *crtc;
926*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
927*4882a593Smuzhiyun int i, ret, old_mode = connector->dpms;
928*4882a593Smuzhiyun bool active = false;
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
931*4882a593Smuzhiyun state->acquire_ctx);
932*4882a593Smuzhiyun if (ret)
933*4882a593Smuzhiyun return ret;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun if (mode != DRM_MODE_DPMS_ON)
936*4882a593Smuzhiyun mode = DRM_MODE_DPMS_OFF;
937*4882a593Smuzhiyun connector->dpms = mode;
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun crtc = connector->state->crtc;
940*4882a593Smuzhiyun if (!crtc)
941*4882a593Smuzhiyun goto out;
942*4882a593Smuzhiyun ret = drm_atomic_add_affected_connectors(state, crtc);
943*4882a593Smuzhiyun if (ret)
944*4882a593Smuzhiyun goto out;
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(state, crtc);
947*4882a593Smuzhiyun if (IS_ERR(crtc_state)) {
948*4882a593Smuzhiyun ret = PTR_ERR(crtc_state);
949*4882a593Smuzhiyun goto out;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun for_each_new_connector_in_state(state, tmp_connector, new_conn_state, i) {
953*4882a593Smuzhiyun if (new_conn_state->crtc != crtc)
954*4882a593Smuzhiyun continue;
955*4882a593Smuzhiyun if (tmp_connector->dpms == DRM_MODE_DPMS_ON) {
956*4882a593Smuzhiyun active = true;
957*4882a593Smuzhiyun break;
958*4882a593Smuzhiyun }
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun crtc_state->active = active;
962*4882a593Smuzhiyun ret = drm_atomic_commit(state);
963*4882a593Smuzhiyun out:
964*4882a593Smuzhiyun if (ret != 0)
965*4882a593Smuzhiyun connector->dpms = old_mode;
966*4882a593Smuzhiyun return ret;
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun
drm_atomic_set_property(struct drm_atomic_state * state,struct drm_file * file_priv,struct drm_mode_object * obj,struct drm_property * prop,uint64_t prop_value)969*4882a593Smuzhiyun int drm_atomic_set_property(struct drm_atomic_state *state,
970*4882a593Smuzhiyun struct drm_file *file_priv,
971*4882a593Smuzhiyun struct drm_mode_object *obj,
972*4882a593Smuzhiyun struct drm_property *prop,
973*4882a593Smuzhiyun uint64_t prop_value)
974*4882a593Smuzhiyun {
975*4882a593Smuzhiyun struct drm_mode_object *ref;
976*4882a593Smuzhiyun int ret;
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun if (!drm_property_change_valid_get(prop, prop_value, &ref))
979*4882a593Smuzhiyun return -EINVAL;
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun switch (obj->type) {
982*4882a593Smuzhiyun case DRM_MODE_OBJECT_CONNECTOR: {
983*4882a593Smuzhiyun struct drm_connector *connector = obj_to_connector(obj);
984*4882a593Smuzhiyun struct drm_connector_state *connector_state;
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun connector_state = drm_atomic_get_connector_state(state, connector);
987*4882a593Smuzhiyun if (IS_ERR(connector_state)) {
988*4882a593Smuzhiyun ret = PTR_ERR(connector_state);
989*4882a593Smuzhiyun break;
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun ret = drm_atomic_connector_set_property(connector,
993*4882a593Smuzhiyun connector_state, file_priv,
994*4882a593Smuzhiyun prop, prop_value);
995*4882a593Smuzhiyun break;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun case DRM_MODE_OBJECT_CRTC: {
998*4882a593Smuzhiyun struct drm_crtc *crtc = obj_to_crtc(obj);
999*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun crtc_state = drm_atomic_get_crtc_state(state, crtc);
1002*4882a593Smuzhiyun if (IS_ERR(crtc_state)) {
1003*4882a593Smuzhiyun ret = PTR_ERR(crtc_state);
1004*4882a593Smuzhiyun break;
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun ret = drm_atomic_crtc_set_property(crtc,
1008*4882a593Smuzhiyun crtc_state, prop, prop_value);
1009*4882a593Smuzhiyun break;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun case DRM_MODE_OBJECT_PLANE: {
1012*4882a593Smuzhiyun struct drm_plane *plane = obj_to_plane(obj);
1013*4882a593Smuzhiyun struct drm_plane_state *plane_state;
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun plane_state = drm_atomic_get_plane_state(state, plane);
1016*4882a593Smuzhiyun if (IS_ERR(plane_state)) {
1017*4882a593Smuzhiyun ret = PTR_ERR(plane_state);
1018*4882a593Smuzhiyun break;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun ret = drm_atomic_plane_set_property(plane,
1022*4882a593Smuzhiyun plane_state, file_priv,
1023*4882a593Smuzhiyun prop, prop_value);
1024*4882a593Smuzhiyun break;
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun default:
1027*4882a593Smuzhiyun ret = -EINVAL;
1028*4882a593Smuzhiyun break;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun drm_property_change_valid_put(prop, ref);
1032*4882a593Smuzhiyun return ret;
1033*4882a593Smuzhiyun }
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun /**
1036*4882a593Smuzhiyun * DOC: explicit fencing properties
1037*4882a593Smuzhiyun *
1038*4882a593Smuzhiyun * Explicit fencing allows userspace to control the buffer synchronization
1039*4882a593Smuzhiyun * between devices. A Fence or a group of fences are transfered to/from
1040*4882a593Smuzhiyun * userspace using Sync File fds and there are two DRM properties for that.
1041*4882a593Smuzhiyun * IN_FENCE_FD on each DRM Plane to send fences to the kernel and
1042*4882a593Smuzhiyun * OUT_FENCE_PTR on each DRM CRTC to receive fences from the kernel.
1043*4882a593Smuzhiyun *
1044*4882a593Smuzhiyun * As a contrast, with implicit fencing the kernel keeps track of any
1045*4882a593Smuzhiyun * ongoing rendering, and automatically ensures that the atomic update waits
1046*4882a593Smuzhiyun * for any pending rendering to complete. For shared buffers represented with
1047*4882a593Smuzhiyun * a &struct dma_buf this is tracked in &struct dma_resv.
1048*4882a593Smuzhiyun * Implicit syncing is how Linux traditionally worked (e.g. DRI2/3 on X.org),
1049*4882a593Smuzhiyun * whereas explicit fencing is what Android wants.
1050*4882a593Smuzhiyun *
1051*4882a593Smuzhiyun * "IN_FENCE_FD”:
1052*4882a593Smuzhiyun * Use this property to pass a fence that DRM should wait on before
1053*4882a593Smuzhiyun * proceeding with the Atomic Commit request and show the framebuffer for
1054*4882a593Smuzhiyun * the plane on the screen. The fence can be either a normal fence or a
1055*4882a593Smuzhiyun * merged one, the sync_file framework will handle both cases and use a
1056*4882a593Smuzhiyun * fence_array if a merged fence is received. Passing -1 here means no
1057*4882a593Smuzhiyun * fences to wait on.
1058*4882a593Smuzhiyun *
1059*4882a593Smuzhiyun * If the Atomic Commit request has the DRM_MODE_ATOMIC_TEST_ONLY flag
1060*4882a593Smuzhiyun * it will only check if the Sync File is a valid one.
1061*4882a593Smuzhiyun *
1062*4882a593Smuzhiyun * On the driver side the fence is stored on the @fence parameter of
1063*4882a593Smuzhiyun * &struct drm_plane_state. Drivers which also support implicit fencing
1064*4882a593Smuzhiyun * should set the implicit fence using drm_atomic_set_fence_for_plane(),
1065*4882a593Smuzhiyun * to make sure there's consistent behaviour between drivers in precedence
1066*4882a593Smuzhiyun * of implicit vs. explicit fencing.
1067*4882a593Smuzhiyun *
1068*4882a593Smuzhiyun * "OUT_FENCE_PTR”:
1069*4882a593Smuzhiyun * Use this property to pass a file descriptor pointer to DRM. Once the
1070*4882a593Smuzhiyun * Atomic Commit request call returns OUT_FENCE_PTR will be filled with
1071*4882a593Smuzhiyun * the file descriptor number of a Sync File. This Sync File contains the
1072*4882a593Smuzhiyun * CRTC fence that will be signaled when all framebuffers present on the
1073*4882a593Smuzhiyun * Atomic Commit * request for that given CRTC are scanned out on the
1074*4882a593Smuzhiyun * screen.
1075*4882a593Smuzhiyun *
1076*4882a593Smuzhiyun * The Atomic Commit request fails if a invalid pointer is passed. If the
1077*4882a593Smuzhiyun * Atomic Commit request fails for any other reason the out fence fd
1078*4882a593Smuzhiyun * returned will be -1. On a Atomic Commit with the
1079*4882a593Smuzhiyun * DRM_MODE_ATOMIC_TEST_ONLY flag the out fence will also be set to -1.
1080*4882a593Smuzhiyun *
1081*4882a593Smuzhiyun * Note that out-fences don't have a special interface to drivers and are
1082*4882a593Smuzhiyun * internally represented by a &struct drm_pending_vblank_event in struct
1083*4882a593Smuzhiyun * &drm_crtc_state, which is also used by the nonblocking atomic commit
1084*4882a593Smuzhiyun * helpers and for the DRM event handling for existing userspace.
1085*4882a593Smuzhiyun */
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun struct drm_out_fence_state {
1088*4882a593Smuzhiyun s32 __user *out_fence_ptr;
1089*4882a593Smuzhiyun struct sync_file *sync_file;
1090*4882a593Smuzhiyun int fd;
1091*4882a593Smuzhiyun };
1092*4882a593Smuzhiyun
setup_out_fence(struct drm_out_fence_state * fence_state,struct dma_fence * fence)1093*4882a593Smuzhiyun static int setup_out_fence(struct drm_out_fence_state *fence_state,
1094*4882a593Smuzhiyun struct dma_fence *fence)
1095*4882a593Smuzhiyun {
1096*4882a593Smuzhiyun fence_state->fd = get_unused_fd_flags(O_CLOEXEC);
1097*4882a593Smuzhiyun if (fence_state->fd < 0)
1098*4882a593Smuzhiyun return fence_state->fd;
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun if (put_user(fence_state->fd, fence_state->out_fence_ptr))
1101*4882a593Smuzhiyun return -EFAULT;
1102*4882a593Smuzhiyun
1103*4882a593Smuzhiyun fence_state->sync_file = sync_file_create(fence);
1104*4882a593Smuzhiyun if (!fence_state->sync_file)
1105*4882a593Smuzhiyun return -ENOMEM;
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun return 0;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun
prepare_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_mode_atomic * arg,struct drm_file * file_priv,struct drm_out_fence_state ** fence_state,unsigned int * num_fences)1110*4882a593Smuzhiyun static int prepare_signaling(struct drm_device *dev,
1111*4882a593Smuzhiyun struct drm_atomic_state *state,
1112*4882a593Smuzhiyun struct drm_mode_atomic *arg,
1113*4882a593Smuzhiyun struct drm_file *file_priv,
1114*4882a593Smuzhiyun struct drm_out_fence_state **fence_state,
1115*4882a593Smuzhiyun unsigned int *num_fences)
1116*4882a593Smuzhiyun {
1117*4882a593Smuzhiyun struct drm_crtc *crtc;
1118*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
1119*4882a593Smuzhiyun struct drm_connector *conn;
1120*4882a593Smuzhiyun struct drm_connector_state *conn_state;
1121*4882a593Smuzhiyun int i, c = 0, ret;
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY)
1124*4882a593Smuzhiyun return 0;
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1127*4882a593Smuzhiyun s32 __user *fence_ptr;
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun fence_ptr = get_out_fence_for_crtc(crtc_state->state, crtc);
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT || fence_ptr) {
1132*4882a593Smuzhiyun struct drm_pending_vblank_event *e;
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun e = create_vblank_event(crtc, arg->user_data);
1135*4882a593Smuzhiyun if (!e)
1136*4882a593Smuzhiyun return -ENOMEM;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun crtc_state->event = e;
1139*4882a593Smuzhiyun }
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun if (arg->flags & DRM_MODE_PAGE_FLIP_EVENT) {
1142*4882a593Smuzhiyun struct drm_pending_vblank_event *e = crtc_state->event;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun if (!file_priv)
1145*4882a593Smuzhiyun continue;
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun ret = drm_event_reserve_init(dev, file_priv, &e->base,
1148*4882a593Smuzhiyun &e->event.base);
1149*4882a593Smuzhiyun if (ret) {
1150*4882a593Smuzhiyun kfree(e);
1151*4882a593Smuzhiyun crtc_state->event = NULL;
1152*4882a593Smuzhiyun return ret;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun if (fence_ptr) {
1157*4882a593Smuzhiyun struct dma_fence *fence;
1158*4882a593Smuzhiyun struct drm_out_fence_state *f;
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun f = krealloc(*fence_state, sizeof(**fence_state) *
1161*4882a593Smuzhiyun (*num_fences + 1), GFP_KERNEL);
1162*4882a593Smuzhiyun if (!f)
1163*4882a593Smuzhiyun return -ENOMEM;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun memset(&f[*num_fences], 0, sizeof(*f));
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun f[*num_fences].out_fence_ptr = fence_ptr;
1168*4882a593Smuzhiyun *fence_state = f;
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun fence = drm_crtc_create_fence(crtc);
1171*4882a593Smuzhiyun if (!fence)
1172*4882a593Smuzhiyun return -ENOMEM;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun ret = setup_out_fence(&f[(*num_fences)++], fence);
1175*4882a593Smuzhiyun if (ret) {
1176*4882a593Smuzhiyun dma_fence_put(fence);
1177*4882a593Smuzhiyun return ret;
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun crtc_state->event->base.fence = fence;
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun c++;
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun for_each_new_connector_in_state(state, conn, conn_state, i) {
1187*4882a593Smuzhiyun struct drm_writeback_connector *wb_conn;
1188*4882a593Smuzhiyun struct drm_out_fence_state *f;
1189*4882a593Smuzhiyun struct dma_fence *fence;
1190*4882a593Smuzhiyun s32 __user *fence_ptr;
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun if (!conn_state->writeback_job)
1193*4882a593Smuzhiyun continue;
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun fence_ptr = get_out_fence_for_connector(state, conn);
1196*4882a593Smuzhiyun if (!fence_ptr)
1197*4882a593Smuzhiyun continue;
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun f = krealloc(*fence_state, sizeof(**fence_state) *
1200*4882a593Smuzhiyun (*num_fences + 1), GFP_KERNEL);
1201*4882a593Smuzhiyun if (!f)
1202*4882a593Smuzhiyun return -ENOMEM;
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun memset(&f[*num_fences], 0, sizeof(*f));
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun f[*num_fences].out_fence_ptr = fence_ptr;
1207*4882a593Smuzhiyun *fence_state = f;
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun wb_conn = drm_connector_to_writeback(conn);
1210*4882a593Smuzhiyun fence = drm_writeback_get_out_fence(wb_conn);
1211*4882a593Smuzhiyun if (!fence)
1212*4882a593Smuzhiyun return -ENOMEM;
1213*4882a593Smuzhiyun
1214*4882a593Smuzhiyun ret = setup_out_fence(&f[(*num_fences)++], fence);
1215*4882a593Smuzhiyun if (ret) {
1216*4882a593Smuzhiyun dma_fence_put(fence);
1217*4882a593Smuzhiyun return ret;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun conn_state->writeback_job->out_fence = fence;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun /*
1224*4882a593Smuzhiyun * Having this flag means user mode pends on event which will never
1225*4882a593Smuzhiyun * reach due to lack of at least one CRTC for signaling
1226*4882a593Smuzhiyun */
1227*4882a593Smuzhiyun if (c == 0 && (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1228*4882a593Smuzhiyun return -EINVAL;
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun return 0;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
complete_signaling(struct drm_device * dev,struct drm_atomic_state * state,struct drm_out_fence_state * fence_state,unsigned int num_fences,bool install_fds)1233*4882a593Smuzhiyun static void complete_signaling(struct drm_device *dev,
1234*4882a593Smuzhiyun struct drm_atomic_state *state,
1235*4882a593Smuzhiyun struct drm_out_fence_state *fence_state,
1236*4882a593Smuzhiyun unsigned int num_fences,
1237*4882a593Smuzhiyun bool install_fds)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun struct drm_crtc *crtc;
1240*4882a593Smuzhiyun struct drm_crtc_state *crtc_state;
1241*4882a593Smuzhiyun int i;
1242*4882a593Smuzhiyun
1243*4882a593Smuzhiyun if (install_fds) {
1244*4882a593Smuzhiyun for (i = 0; i < num_fences; i++)
1245*4882a593Smuzhiyun fd_install(fence_state[i].fd,
1246*4882a593Smuzhiyun fence_state[i].sync_file->file);
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun kfree(fence_state);
1249*4882a593Smuzhiyun return;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1253*4882a593Smuzhiyun struct drm_pending_vblank_event *event = crtc_state->event;
1254*4882a593Smuzhiyun /*
1255*4882a593Smuzhiyun * Free the allocated event. drm_atomic_helper_setup_commit
1256*4882a593Smuzhiyun * can allocate an event too, so only free it if it's ours
1257*4882a593Smuzhiyun * to prevent a double free in drm_atomic_state_clear.
1258*4882a593Smuzhiyun */
1259*4882a593Smuzhiyun if (event && (event->base.fence || event->base.file_priv)) {
1260*4882a593Smuzhiyun drm_event_cancel_free(dev, &event->base);
1261*4882a593Smuzhiyun crtc_state->event = NULL;
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun
1265*4882a593Smuzhiyun if (!fence_state)
1266*4882a593Smuzhiyun return;
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun for (i = 0; i < num_fences; i++) {
1269*4882a593Smuzhiyun if (fence_state[i].sync_file)
1270*4882a593Smuzhiyun fput(fence_state[i].sync_file->file);
1271*4882a593Smuzhiyun if (fence_state[i].fd >= 0)
1272*4882a593Smuzhiyun put_unused_fd(fence_state[i].fd);
1273*4882a593Smuzhiyun
1274*4882a593Smuzhiyun /* If this fails log error to the user */
1275*4882a593Smuzhiyun if (fence_state[i].out_fence_ptr &&
1276*4882a593Smuzhiyun put_user(-1, fence_state[i].out_fence_ptr))
1277*4882a593Smuzhiyun DRM_DEBUG_ATOMIC("Couldn't clear out_fence_ptr\n");
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun kfree(fence_state);
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun
drm_mode_atomic_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1283*4882a593Smuzhiyun int drm_mode_atomic_ioctl(struct drm_device *dev,
1284*4882a593Smuzhiyun void *data, struct drm_file *file_priv)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun struct drm_mode_atomic *arg = data;
1287*4882a593Smuzhiyun uint32_t __user *objs_ptr = (uint32_t __user *)(unsigned long)(arg->objs_ptr);
1288*4882a593Smuzhiyun uint32_t __user *count_props_ptr = (uint32_t __user *)(unsigned long)(arg->count_props_ptr);
1289*4882a593Smuzhiyun uint32_t __user *props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
1290*4882a593Smuzhiyun uint64_t __user *prop_values_ptr = (uint64_t __user *)(unsigned long)(arg->prop_values_ptr);
1291*4882a593Smuzhiyun unsigned int copied_objs, copied_props;
1292*4882a593Smuzhiyun struct drm_atomic_state *state;
1293*4882a593Smuzhiyun struct drm_modeset_acquire_ctx ctx;
1294*4882a593Smuzhiyun struct drm_out_fence_state *fence_state;
1295*4882a593Smuzhiyun int ret = 0;
1296*4882a593Smuzhiyun unsigned int i, j, num_fences;
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun /* disallow for drivers not supporting atomic: */
1299*4882a593Smuzhiyun if (!drm_core_check_feature(dev, DRIVER_ATOMIC))
1300*4882a593Smuzhiyun return -EOPNOTSUPP;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun /* disallow for userspace that has not enabled atomic cap (even
1303*4882a593Smuzhiyun * though this may be a bit overkill, since legacy userspace
1304*4882a593Smuzhiyun * wouldn't know how to call this ioctl)
1305*4882a593Smuzhiyun */
1306*4882a593Smuzhiyun if (!file_priv->atomic)
1307*4882a593Smuzhiyun return -EINVAL;
1308*4882a593Smuzhiyun
1309*4882a593Smuzhiyun if (arg->flags & ~DRM_MODE_ATOMIC_FLAGS)
1310*4882a593Smuzhiyun return -EINVAL;
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun if (arg->reserved)
1313*4882a593Smuzhiyun return -EINVAL;
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun if (arg->flags & DRM_MODE_PAGE_FLIP_ASYNC)
1316*4882a593Smuzhiyun return -EINVAL;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun /* can't test and expect an event at the same time. */
1319*4882a593Smuzhiyun if ((arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) &&
1320*4882a593Smuzhiyun (arg->flags & DRM_MODE_PAGE_FLIP_EVENT))
1321*4882a593Smuzhiyun return -EINVAL;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun state = drm_atomic_state_alloc(dev);
1324*4882a593Smuzhiyun if (!state)
1325*4882a593Smuzhiyun return -ENOMEM;
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1328*4882a593Smuzhiyun state->acquire_ctx = &ctx;
1329*4882a593Smuzhiyun state->allow_modeset = !!(arg->flags & DRM_MODE_ATOMIC_ALLOW_MODESET);
1330*4882a593Smuzhiyun
1331*4882a593Smuzhiyun retry:
1332*4882a593Smuzhiyun copied_objs = 0;
1333*4882a593Smuzhiyun copied_props = 0;
1334*4882a593Smuzhiyun fence_state = NULL;
1335*4882a593Smuzhiyun num_fences = 0;
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun for (i = 0; i < arg->count_objs; i++) {
1338*4882a593Smuzhiyun uint32_t obj_id, count_props;
1339*4882a593Smuzhiyun struct drm_mode_object *obj;
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun if (get_user(obj_id, objs_ptr + copied_objs)) {
1342*4882a593Smuzhiyun ret = -EFAULT;
1343*4882a593Smuzhiyun goto out;
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun obj = drm_mode_object_find(dev, file_priv, obj_id, DRM_MODE_OBJECT_ANY);
1347*4882a593Smuzhiyun if (!obj) {
1348*4882a593Smuzhiyun ret = -ENOENT;
1349*4882a593Smuzhiyun goto out;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun
1352*4882a593Smuzhiyun if (!obj->properties) {
1353*4882a593Smuzhiyun drm_mode_object_put(obj);
1354*4882a593Smuzhiyun ret = -ENOENT;
1355*4882a593Smuzhiyun goto out;
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun if (get_user(count_props, count_props_ptr + copied_objs)) {
1359*4882a593Smuzhiyun drm_mode_object_put(obj);
1360*4882a593Smuzhiyun ret = -EFAULT;
1361*4882a593Smuzhiyun goto out;
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun copied_objs++;
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun for (j = 0; j < count_props; j++) {
1367*4882a593Smuzhiyun uint32_t prop_id;
1368*4882a593Smuzhiyun uint64_t prop_value;
1369*4882a593Smuzhiyun struct drm_property *prop;
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun if (get_user(prop_id, props_ptr + copied_props)) {
1372*4882a593Smuzhiyun drm_mode_object_put(obj);
1373*4882a593Smuzhiyun ret = -EFAULT;
1374*4882a593Smuzhiyun goto out;
1375*4882a593Smuzhiyun }
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun prop = drm_mode_obj_find_prop_id(obj, prop_id);
1378*4882a593Smuzhiyun if (!prop) {
1379*4882a593Smuzhiyun drm_mode_object_put(obj);
1380*4882a593Smuzhiyun ret = -ENOENT;
1381*4882a593Smuzhiyun goto out;
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun if (copy_from_user(&prop_value,
1385*4882a593Smuzhiyun prop_values_ptr + copied_props,
1386*4882a593Smuzhiyun sizeof(prop_value))) {
1387*4882a593Smuzhiyun drm_mode_object_put(obj);
1388*4882a593Smuzhiyun ret = -EFAULT;
1389*4882a593Smuzhiyun goto out;
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun
1392*4882a593Smuzhiyun ret = drm_atomic_set_property(state, file_priv,
1393*4882a593Smuzhiyun obj, prop, prop_value);
1394*4882a593Smuzhiyun if (ret) {
1395*4882a593Smuzhiyun drm_mode_object_put(obj);
1396*4882a593Smuzhiyun goto out;
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun copied_props++;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun drm_mode_object_put(obj);
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun
1405*4882a593Smuzhiyun ret = prepare_signaling(dev, state, arg, file_priv, &fence_state,
1406*4882a593Smuzhiyun &num_fences);
1407*4882a593Smuzhiyun if (ret)
1408*4882a593Smuzhiyun goto out;
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun if (arg->flags & DRM_MODE_ATOMIC_TEST_ONLY) {
1411*4882a593Smuzhiyun ret = drm_atomic_check_only(state);
1412*4882a593Smuzhiyun } else if (arg->flags & DRM_MODE_ATOMIC_NONBLOCK) {
1413*4882a593Smuzhiyun ret = drm_atomic_nonblocking_commit(state);
1414*4882a593Smuzhiyun } else {
1415*4882a593Smuzhiyun if (drm_debug_enabled(DRM_UT_STATE))
1416*4882a593Smuzhiyun drm_atomic_print_state(state);
1417*4882a593Smuzhiyun
1418*4882a593Smuzhiyun ret = drm_atomic_commit(state);
1419*4882a593Smuzhiyun }
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun out:
1422*4882a593Smuzhiyun complete_signaling(dev, state, fence_state, num_fences, !ret);
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun if (ret == -EDEADLK) {
1425*4882a593Smuzhiyun drm_atomic_state_clear(state);
1426*4882a593Smuzhiyun ret = drm_modeset_backoff(&ctx);
1427*4882a593Smuzhiyun if (!ret)
1428*4882a593Smuzhiyun goto retry;
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun
1431*4882a593Smuzhiyun drm_atomic_state_put(state);
1432*4882a593Smuzhiyun
1433*4882a593Smuzhiyun drm_modeset_drop_locks(&ctx);
1434*4882a593Smuzhiyun drm_modeset_acquire_fini(&ctx);
1435*4882a593Smuzhiyun
1436*4882a593Smuzhiyun return ret;
1437*4882a593Smuzhiyun }
1438