13253c27bSkettenis /*
23253c27bSkettenis * Copyright (C) 2014 Intel Corporation
33253c27bSkettenis *
43253c27bSkettenis * DRM universal plane helper functions
53253c27bSkettenis *
63253c27bSkettenis * Permission is hereby granted, free of charge, to any person obtaining a
73253c27bSkettenis * copy of this software and associated documentation files (the "Software"),
83253c27bSkettenis * to deal in the Software without restriction, including without limitation
93253c27bSkettenis * the rights to use, copy, modify, merge, publish, distribute, sublicense,
103253c27bSkettenis * and/or sell copies of the Software, and to permit persons to whom the
113253c27bSkettenis * Software is furnished to do so, subject to the following conditions:
123253c27bSkettenis *
133253c27bSkettenis * The above copyright notice and this permission notice (including the next
143253c27bSkettenis * paragraph) shall be included in all copies or substantial portions of the
153253c27bSkettenis * Software.
163253c27bSkettenis *
173253c27bSkettenis * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
183253c27bSkettenis * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193253c27bSkettenis * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
203253c27bSkettenis * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
213253c27bSkettenis * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
223253c27bSkettenis * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
233253c27bSkettenis * SOFTWARE.
243253c27bSkettenis */
253253c27bSkettenis
263253c27bSkettenis #include <linux/list.h>
27c349dbc7Sjsg
28c349dbc7Sjsg #include <drm/drm_atomic.h>
29c349dbc7Sjsg #include <drm/drm_atomic_helper.h>
30c349dbc7Sjsg #include <drm/drm_atomic_uapi.h>
31c349dbc7Sjsg #include <drm/drm_device.h>
321bb76ff1Sjsg #include <drm/drm_drv.h>
33c349dbc7Sjsg #include <drm/drm_encoder.h>
347f4dd379Sjsg #include <drm/drm_plane_helper.h>
351bb76ff1Sjsg #include <drm/drm_print.h>
367f4dd379Sjsg #include <drm/drm_rect.h>
373253c27bSkettenis
383253c27bSkettenis #define SUBPIXEL_MASK 0xffff
393253c27bSkettenis
403253c27bSkettenis /**
413253c27bSkettenis * DOC: overview
423253c27bSkettenis *
43*f005ef32Sjsg * This helper library contains helpers to implement primary plane support on
44*f005ef32Sjsg * top of the normal CRTC configuration interface.
457f4dd379Sjsg * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
467f4dd379Sjsg * plane together with the CRTC state this does not allow userspace to disable
47c349dbc7Sjsg * the primary plane itself. The default primary plane only expose XRBG8888 and
48c349dbc7Sjsg * ARGB8888 as valid pixel formats for the attached framebuffer.
493253c27bSkettenis *
503253c27bSkettenis * Drivers are highly recommended to implement proper support for primary
513253c27bSkettenis * planes, and newly merged drivers must not rely upon these transitional
523253c27bSkettenis * helpers.
533253c27bSkettenis *
547f4dd379Sjsg * The plane helpers share the function table structures with other helpers,
557f4dd379Sjsg * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
567f4dd379Sjsg * the details.
573253c27bSkettenis */
583253c27bSkettenis
593253c27bSkettenis /*
603253c27bSkettenis * Returns the connectors currently associated with a CRTC. This function
613253c27bSkettenis * should be called twice: once with a NULL connector list to retrieve
623253c27bSkettenis * the list size, and once with the properly allocated list to be filled in.
633253c27bSkettenis */
get_connectors_for_crtc(struct drm_crtc * crtc,struct drm_connector ** connector_list,int num_connectors)643253c27bSkettenis static int get_connectors_for_crtc(struct drm_crtc *crtc,
653253c27bSkettenis struct drm_connector **connector_list,
663253c27bSkettenis int num_connectors)
673253c27bSkettenis {
683253c27bSkettenis struct drm_device *dev = crtc->dev;
693253c27bSkettenis struct drm_connector *connector;
707f4dd379Sjsg struct drm_connector_list_iter conn_iter;
713253c27bSkettenis int count = 0;
723253c27bSkettenis
733253c27bSkettenis /*
743253c27bSkettenis * Note: Once we change the plane hooks to more fine-grained locking we
753253c27bSkettenis * need to grab the connection_mutex here to be able to make these
763253c27bSkettenis * checks.
773253c27bSkettenis */
783253c27bSkettenis WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
793253c27bSkettenis
807f4dd379Sjsg drm_connector_list_iter_begin(dev, &conn_iter);
817f4dd379Sjsg drm_for_each_connector_iter(connector, &conn_iter) {
823253c27bSkettenis if (connector->encoder && connector->encoder->crtc == crtc) {
833253c27bSkettenis if (connector_list != NULL && count < num_connectors)
843253c27bSkettenis *(connector_list++) = connector;
853253c27bSkettenis
863253c27bSkettenis count++;
873253c27bSkettenis }
883253c27bSkettenis }
897f4dd379Sjsg drm_connector_list_iter_end(&conn_iter);
903253c27bSkettenis
913253c27bSkettenis return count;
923253c27bSkettenis }
933253c27bSkettenis
drm_plane_helper_check_update(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_rect * src,struct drm_rect * dst,unsigned int rotation,int min_scale,int max_scale,bool can_position,bool can_update_disabled,bool * visible)94c349dbc7Sjsg static int drm_plane_helper_check_update(struct drm_plane *plane,
953253c27bSkettenis struct drm_crtc *crtc,
963253c27bSkettenis struct drm_framebuffer *fb,
973253c27bSkettenis struct drm_rect *src,
987f4dd379Sjsg struct drm_rect *dst,
997f4dd379Sjsg unsigned int rotation,
1003253c27bSkettenis int min_scale,
1013253c27bSkettenis int max_scale,
1023253c27bSkettenis bool can_position,
1033253c27bSkettenis bool can_update_disabled,
1043253c27bSkettenis bool *visible)
1053253c27bSkettenis {
1067f4dd379Sjsg struct drm_plane_state plane_state = {
1077f4dd379Sjsg .plane = plane,
1087f4dd379Sjsg .crtc = crtc,
1097f4dd379Sjsg .fb = fb,
1107f4dd379Sjsg .src_x = src->x1,
1117f4dd379Sjsg .src_y = src->y1,
1127f4dd379Sjsg .src_w = drm_rect_width(src),
1137f4dd379Sjsg .src_h = drm_rect_height(src),
1147f4dd379Sjsg .crtc_x = dst->x1,
1157f4dd379Sjsg .crtc_y = dst->y1,
1167f4dd379Sjsg .crtc_w = drm_rect_width(dst),
1177f4dd379Sjsg .crtc_h = drm_rect_height(dst),
1187f4dd379Sjsg .rotation = rotation,
1197f4dd379Sjsg };
1207f4dd379Sjsg struct drm_crtc_state crtc_state = {
1217f4dd379Sjsg .crtc = crtc,
1227f4dd379Sjsg .enable = crtc->enabled,
1237f4dd379Sjsg .mode = crtc->mode,
1247f4dd379Sjsg };
1257f4dd379Sjsg int ret;
1263253c27bSkettenis
1277f4dd379Sjsg ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
1287f4dd379Sjsg min_scale, max_scale,
1297f4dd379Sjsg can_position,
1307f4dd379Sjsg can_update_disabled);
1317f4dd379Sjsg if (ret)
1327f4dd379Sjsg return ret;
1333253c27bSkettenis
1347f4dd379Sjsg *src = plane_state.src;
1357f4dd379Sjsg *dst = plane_state.dst;
1367f4dd379Sjsg *visible = plane_state.visible;
1373253c27bSkettenis
1383253c27bSkettenis return 0;
1393253c27bSkettenis }
1403253c27bSkettenis
1411bb76ff1Sjsg /**
1421bb76ff1Sjsg * drm_plane_helper_update_primary - Helper for updating primary planes
1431bb76ff1Sjsg * @plane: plane to update
1441bb76ff1Sjsg * @crtc: the plane's new CRTC
1451bb76ff1Sjsg * @fb: the plane's new framebuffer
1461bb76ff1Sjsg * @crtc_x: x coordinate within CRTC
1471bb76ff1Sjsg * @crtc_y: y coordinate within CRTC
1481bb76ff1Sjsg * @crtc_w: width coordinate within CRTC
1491bb76ff1Sjsg * @crtc_h: height coordinate within CRTC
1501bb76ff1Sjsg * @src_x: x coordinate within source
1511bb76ff1Sjsg * @src_y: y coordinate within source
1521bb76ff1Sjsg * @src_w: width coordinate within source
1531bb76ff1Sjsg * @src_h: height coordinate within source
1541bb76ff1Sjsg * @ctx: modeset locking context
1551bb76ff1Sjsg *
1561bb76ff1Sjsg * This helper validates the given parameters and updates the primary plane.
1571bb76ff1Sjsg *
1581bb76ff1Sjsg * This function is only useful for non-atomic modesetting. Don't use
1591bb76ff1Sjsg * it in new drivers.
1601bb76ff1Sjsg *
1611bb76ff1Sjsg * Returns:
1621bb76ff1Sjsg * Zero on success, or an errno code otherwise.
1631bb76ff1Sjsg */
drm_plane_helper_update_primary(struct drm_plane * plane,struct drm_crtc * crtc,struct drm_framebuffer * fb,int crtc_x,int crtc_y,unsigned int crtc_w,unsigned int crtc_h,uint32_t src_x,uint32_t src_y,uint32_t src_w,uint32_t src_h,struct drm_modeset_acquire_ctx * ctx)1641bb76ff1Sjsg int drm_plane_helper_update_primary(struct drm_plane *plane, struct drm_crtc *crtc,
1653253c27bSkettenis struct drm_framebuffer *fb,
1663253c27bSkettenis int crtc_x, int crtc_y,
1673253c27bSkettenis unsigned int crtc_w, unsigned int crtc_h,
1683253c27bSkettenis uint32_t src_x, uint32_t src_y,
1697f4dd379Sjsg uint32_t src_w, uint32_t src_h,
1707f4dd379Sjsg struct drm_modeset_acquire_ctx *ctx)
1713253c27bSkettenis {
1723253c27bSkettenis struct drm_mode_set set = {
1733253c27bSkettenis .crtc = crtc,
1743253c27bSkettenis .fb = fb,
1753253c27bSkettenis .mode = &crtc->mode,
1763253c27bSkettenis .x = src_x >> 16,
1773253c27bSkettenis .y = src_y >> 16,
1783253c27bSkettenis };
1793253c27bSkettenis struct drm_rect src = {
1803253c27bSkettenis .x1 = src_x,
1813253c27bSkettenis .y1 = src_y,
1823253c27bSkettenis .x2 = src_x + src_w,
1833253c27bSkettenis .y2 = src_y + src_h,
1843253c27bSkettenis };
1853253c27bSkettenis struct drm_rect dest = {
1863253c27bSkettenis .x1 = crtc_x,
1873253c27bSkettenis .y1 = crtc_y,
1883253c27bSkettenis .x2 = crtc_x + crtc_w,
1893253c27bSkettenis .y2 = crtc_y + crtc_h,
1903253c27bSkettenis };
1911bb76ff1Sjsg struct drm_device *dev = plane->dev;
1923253c27bSkettenis struct drm_connector **connector_list;
1933253c27bSkettenis int num_connectors, ret;
1943253c27bSkettenis bool visible;
1953253c27bSkettenis
1961bb76ff1Sjsg if (drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev)))
1971bb76ff1Sjsg return -EINVAL;
1981bb76ff1Sjsg
1993253c27bSkettenis ret = drm_plane_helper_check_update(plane, crtc, fb,
2007f4dd379Sjsg &src, &dest,
2017f4dd379Sjsg DRM_MODE_ROTATE_0,
2021bb76ff1Sjsg DRM_PLANE_NO_SCALING,
2031bb76ff1Sjsg DRM_PLANE_NO_SCALING,
2043253c27bSkettenis false, false, &visible);
2053253c27bSkettenis if (ret)
2063253c27bSkettenis return ret;
2073253c27bSkettenis
2083253c27bSkettenis if (!visible)
2093253c27bSkettenis /*
2103253c27bSkettenis * Primary plane isn't visible. Note that unless a driver
2113253c27bSkettenis * provides their own disable function, this will just
2123253c27bSkettenis * wind up returning -EINVAL to userspace.
2133253c27bSkettenis */
2147f4dd379Sjsg return plane->funcs->disable_plane(plane, ctx);
2153253c27bSkettenis
2163253c27bSkettenis /* Find current connectors for CRTC */
2173253c27bSkettenis num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
2183253c27bSkettenis BUG_ON(num_connectors == 0);
2197f4dd379Sjsg connector_list = kcalloc(num_connectors, sizeof(*connector_list),
2203253c27bSkettenis GFP_KERNEL);
2213253c27bSkettenis if (!connector_list)
2223253c27bSkettenis return -ENOMEM;
2233253c27bSkettenis get_connectors_for_crtc(crtc, connector_list, num_connectors);
2243253c27bSkettenis
2253253c27bSkettenis set.connectors = connector_list;
2263253c27bSkettenis set.num_connectors = num_connectors;
2273253c27bSkettenis
2283253c27bSkettenis /*
2293253c27bSkettenis * We call set_config() directly here rather than using
2303253c27bSkettenis * drm_mode_set_config_internal. We're reprogramming the same
2313253c27bSkettenis * connectors that were already in use, so we shouldn't need the extra
2325ca02815Sjsg * cross-CRTC fb refcounting to accommodate stealing connectors.
2333253c27bSkettenis * drm_mode_setplane() already handles the basic refcounting for the
2343253c27bSkettenis * framebuffers involved in this operation.
2353253c27bSkettenis */
2367f4dd379Sjsg ret = crtc->funcs->set_config(&set, ctx);
2373253c27bSkettenis
2383253c27bSkettenis kfree(connector_list);
2393253c27bSkettenis return ret;
2403253c27bSkettenis }
2411bb76ff1Sjsg EXPORT_SYMBOL(drm_plane_helper_update_primary);
2423253c27bSkettenis
2433253c27bSkettenis /**
2441bb76ff1Sjsg * drm_plane_helper_disable_primary - Helper for disabling primary planes
2451bb76ff1Sjsg * @plane: plane to disable
2461bb76ff1Sjsg * @ctx: modeset locking context
2471bb76ff1Sjsg *
2481bb76ff1Sjsg * This helper returns an error when trying to disable the primary
2491bb76ff1Sjsg * plane.
2501bb76ff1Sjsg *
2511bb76ff1Sjsg * This function is only useful for non-atomic modesetting. Don't use
2521bb76ff1Sjsg * it in new drivers.
2531bb76ff1Sjsg *
2541bb76ff1Sjsg * Returns:
2551bb76ff1Sjsg * An errno code.
2561bb76ff1Sjsg */
drm_plane_helper_disable_primary(struct drm_plane * plane,struct drm_modeset_acquire_ctx * ctx)2571bb76ff1Sjsg int drm_plane_helper_disable_primary(struct drm_plane *plane,
2581bb76ff1Sjsg struct drm_modeset_acquire_ctx *ctx)
2591bb76ff1Sjsg {
2601bb76ff1Sjsg struct drm_device *dev = plane->dev;
2611bb76ff1Sjsg
2621bb76ff1Sjsg drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
2631bb76ff1Sjsg
2641bb76ff1Sjsg return -EINVAL;
2651bb76ff1Sjsg }
2661bb76ff1Sjsg EXPORT_SYMBOL(drm_plane_helper_disable_primary);
2671bb76ff1Sjsg
2681bb76ff1Sjsg /**
2691bb76ff1Sjsg * drm_plane_helper_destroy() - Helper for primary plane destruction
2703253c27bSkettenis * @plane: plane to destroy
2713253c27bSkettenis *
2723253c27bSkettenis * Provides a default plane destroy handler for primary planes. This handler
2733253c27bSkettenis * is called during CRTC destruction. We disable the primary plane, remove
2743253c27bSkettenis * it from the DRM plane list, and deallocate the plane structure.
2753253c27bSkettenis */
drm_plane_helper_destroy(struct drm_plane * plane)2761bb76ff1Sjsg void drm_plane_helper_destroy(struct drm_plane *plane)
2773253c27bSkettenis {
2783253c27bSkettenis drm_plane_cleanup(plane);
2793253c27bSkettenis kfree(plane);
2803253c27bSkettenis }
2811bb76ff1Sjsg EXPORT_SYMBOL(drm_plane_helper_destroy);
2823253c27bSkettenis
2831bb76ff1Sjsg /**
2841bb76ff1Sjsg * drm_plane_helper_atomic_check() - Helper to check plane atomic-state
2851bb76ff1Sjsg * @plane: plane to check
2861bb76ff1Sjsg * @state: atomic state object
2871bb76ff1Sjsg *
2881bb76ff1Sjsg * Provides a default plane-state check handler for planes whose atomic-state
2891bb76ff1Sjsg * scale and positioning are not expected to change since the plane is always
2901bb76ff1Sjsg * a fullscreen scanout buffer.
2911bb76ff1Sjsg *
292*f005ef32Sjsg * This is often the case for the primary plane of simple framebuffers. See
293*f005ef32Sjsg * also drm_crtc_helper_atomic_check() for the respective CRTC-state check
294*f005ef32Sjsg * helper function.
2951bb76ff1Sjsg *
2961bb76ff1Sjsg * RETURNS:
2971bb76ff1Sjsg * Zero on success, or an errno code otherwise.
2981bb76ff1Sjsg */
drm_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)2991bb76ff1Sjsg int drm_plane_helper_atomic_check(struct drm_plane *plane, struct drm_atomic_state *state)
3001bb76ff1Sjsg {
3011bb76ff1Sjsg struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
3021bb76ff1Sjsg struct drm_crtc *new_crtc = new_plane_state->crtc;
3031bb76ff1Sjsg struct drm_crtc_state *new_crtc_state = NULL;
3041bb76ff1Sjsg
3051bb76ff1Sjsg if (new_crtc)
3061bb76ff1Sjsg new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
3071bb76ff1Sjsg
3081bb76ff1Sjsg return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
3091bb76ff1Sjsg DRM_PLANE_NO_SCALING,
3101bb76ff1Sjsg DRM_PLANE_NO_SCALING,
3111bb76ff1Sjsg false, false);
3121bb76ff1Sjsg }
3131bb76ff1Sjsg EXPORT_SYMBOL(drm_plane_helper_atomic_check);
314