1 1.6 riastrad /* $NetBSD: drm_plane_helper.c,v 1.6 2021/12/19 09:49:56 riastradh Exp $ */ 2 1.3 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright (C) 2014 Intel Corporation 5 1.1 riastrad * 6 1.1 riastrad * DRM universal plane helper functions 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice (including the next 16 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 17 1.1 riastrad * Software. 18 1.1 riastrad * 19 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 1.1 riastrad * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 1.1 riastrad * SOFTWARE. 26 1.1 riastrad */ 27 1.1 riastrad 28 1.3 riastrad #include <sys/cdefs.h> 29 1.6 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_plane_helper.c,v 1.6 2021/12/19 09:49:56 riastradh Exp $"); 30 1.3 riastrad 31 1.1 riastrad #include <linux/list.h> 32 1.5 riastrad 33 1.3 riastrad #include <drm/drm_atomic.h> 34 1.5 riastrad #include <drm/drm_atomic_helper.h> 35 1.5 riastrad #include <drm/drm_atomic_uapi.h> 36 1.3 riastrad #include <drm/drm_crtc_helper.h> 37 1.5 riastrad #include <drm/drm_device.h> 38 1.5 riastrad #include <drm/drm_encoder.h> 39 1.2 riastrad #include <drm/drm_plane_helper.h> 40 1.5 riastrad #include <drm/drm_rect.h> 41 1.1 riastrad 42 1.3 riastrad /** 43 1.3 riastrad * DOC: overview 44 1.3 riastrad * 45 1.3 riastrad * This helper library has two parts. The first part has support to implement 46 1.3 riastrad * primary plane support on top of the normal CRTC configuration interface. 47 1.5 riastrad * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary 48 1.5 riastrad * plane together with the CRTC state this does not allow userspace to disable 49 1.5 riastrad * the primary plane itself. The default primary plane only expose XRBG8888 and 50 1.5 riastrad * ARGB8888 as valid pixel formats for the attached framebuffer. 51 1.3 riastrad * 52 1.3 riastrad * Drivers are highly recommended to implement proper support for primary 53 1.3 riastrad * planes, and newly merged drivers must not rely upon these transitional 54 1.3 riastrad * helpers. 55 1.3 riastrad * 56 1.3 riastrad * The second part also implements transitional helpers which allow drivers to 57 1.3 riastrad * gradually switch to the atomic helper infrastructure for plane updates. Once 58 1.3 riastrad * that switch is complete drivers shouldn't use these any longer, instead using 59 1.3 riastrad * the proper legacy implementations for update and disable plane hooks provided 60 1.3 riastrad * by the atomic helpers. 61 1.3 riastrad * 62 1.3 riastrad * Again drivers are strongly urged to switch to the new interfaces. 63 1.5 riastrad * 64 1.5 riastrad * The plane helpers share the function table structures with other helpers, 65 1.5 riastrad * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for 66 1.5 riastrad * the details. 67 1.3 riastrad */ 68 1.3 riastrad 69 1.1 riastrad /* 70 1.1 riastrad * Returns the connectors currently associated with a CRTC. This function 71 1.1 riastrad * should be called twice: once with a NULL connector list to retrieve 72 1.1 riastrad * the list size, and once with the properly allocated list to be filled in. 73 1.1 riastrad */ 74 1.1 riastrad static int get_connectors_for_crtc(struct drm_crtc *crtc, 75 1.1 riastrad struct drm_connector **connector_list, 76 1.1 riastrad int num_connectors) 77 1.1 riastrad { 78 1.1 riastrad struct drm_device *dev = crtc->dev; 79 1.1 riastrad struct drm_connector *connector; 80 1.5 riastrad struct drm_connector_list_iter conn_iter; 81 1.1 riastrad int count = 0; 82 1.1 riastrad 83 1.3 riastrad /* 84 1.3 riastrad * Note: Once we change the plane hooks to more fine-grained locking we 85 1.3 riastrad * need to grab the connection_mutex here to be able to make these 86 1.3 riastrad * checks. 87 1.3 riastrad */ 88 1.3 riastrad WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex)); 89 1.3 riastrad 90 1.5 riastrad drm_connector_list_iter_begin(dev, &conn_iter); 91 1.5 riastrad drm_for_each_connector_iter(connector, &conn_iter) { 92 1.1 riastrad if (connector->encoder && connector->encoder->crtc == crtc) { 93 1.1 riastrad if (connector_list != NULL && count < num_connectors) 94 1.1 riastrad *(connector_list++) = connector; 95 1.1 riastrad 96 1.1 riastrad count++; 97 1.1 riastrad } 98 1.3 riastrad } 99 1.5 riastrad drm_connector_list_iter_end(&conn_iter); 100 1.1 riastrad 101 1.1 riastrad return count; 102 1.1 riastrad } 103 1.1 riastrad 104 1.5 riastrad static int drm_plane_helper_check_update(struct drm_plane *plane, 105 1.5 riastrad struct drm_crtc *crtc, 106 1.5 riastrad struct drm_framebuffer *fb, 107 1.5 riastrad struct drm_rect *src, 108 1.5 riastrad struct drm_rect *dst, 109 1.5 riastrad unsigned int rotation, 110 1.5 riastrad int min_scale, 111 1.5 riastrad int max_scale, 112 1.5 riastrad bool can_position, 113 1.5 riastrad bool can_update_disabled, 114 1.5 riastrad bool *visible) 115 1.3 riastrad { 116 1.5 riastrad struct drm_plane_state plane_state = { 117 1.5 riastrad .plane = plane, 118 1.5 riastrad .crtc = crtc, 119 1.5 riastrad .fb = fb, 120 1.5 riastrad .src_x = src->x1, 121 1.5 riastrad .src_y = src->y1, 122 1.5 riastrad .src_w = drm_rect_width(src), 123 1.5 riastrad .src_h = drm_rect_height(src), 124 1.5 riastrad .crtc_x = dst->x1, 125 1.5 riastrad .crtc_y = dst->y1, 126 1.5 riastrad .crtc_w = drm_rect_width(dst), 127 1.5 riastrad .crtc_h = drm_rect_height(dst), 128 1.5 riastrad .rotation = rotation, 129 1.5 riastrad .visible = *visible, 130 1.5 riastrad }; 131 1.5 riastrad struct drm_crtc_state crtc_state = { 132 1.5 riastrad .crtc = crtc, 133 1.5 riastrad .enable = crtc->enabled, 134 1.5 riastrad .mode = crtc->mode, 135 1.5 riastrad }; 136 1.5 riastrad int ret; 137 1.3 riastrad 138 1.5 riastrad ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state, 139 1.5 riastrad min_scale, max_scale, 140 1.5 riastrad can_position, 141 1.5 riastrad can_update_disabled); 142 1.5 riastrad if (ret) 143 1.5 riastrad return ret; 144 1.3 riastrad 145 1.5 riastrad *src = plane_state.src; 146 1.5 riastrad *dst = plane_state.dst; 147 1.5 riastrad *visible = plane_state.visible; 148 1.3 riastrad 149 1.3 riastrad return 0; 150 1.3 riastrad } 151 1.3 riastrad 152 1.5 riastrad static int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc, 153 1.5 riastrad struct drm_framebuffer *fb, 154 1.5 riastrad int crtc_x, int crtc_y, 155 1.5 riastrad unsigned int crtc_w, unsigned int crtc_h, 156 1.5 riastrad uint32_t src_x, uint32_t src_y, 157 1.5 riastrad uint32_t src_w, uint32_t src_h, 158 1.5 riastrad struct drm_modeset_acquire_ctx *ctx) 159 1.1 riastrad { 160 1.1 riastrad struct drm_mode_set set = { 161 1.1 riastrad .crtc = crtc, 162 1.1 riastrad .fb = fb, 163 1.1 riastrad .mode = &crtc->mode, 164 1.1 riastrad .x = src_x >> 16, 165 1.1 riastrad .y = src_y >> 16, 166 1.1 riastrad }; 167 1.3 riastrad struct drm_rect src = { 168 1.3 riastrad .x1 = src_x, 169 1.3 riastrad .y1 = src_y, 170 1.3 riastrad .x2 = src_x + src_w, 171 1.3 riastrad .y2 = src_y + src_h, 172 1.3 riastrad }; 173 1.1 riastrad struct drm_rect dest = { 174 1.1 riastrad .x1 = crtc_x, 175 1.1 riastrad .y1 = crtc_y, 176 1.1 riastrad .x2 = crtc_x + crtc_w, 177 1.1 riastrad .y2 = crtc_y + crtc_h, 178 1.1 riastrad }; 179 1.1 riastrad struct drm_connector **connector_list; 180 1.1 riastrad int num_connectors, ret; 181 1.6 riastrad bool visible = false; /* XXX wasn't initialized normally. Looks like bug. */ 182 1.1 riastrad 183 1.3 riastrad ret = drm_plane_helper_check_update(plane, crtc, fb, 184 1.5 riastrad &src, &dest, 185 1.5 riastrad DRM_MODE_ROTATE_0, 186 1.3 riastrad DRM_PLANE_HELPER_NO_SCALING, 187 1.3 riastrad DRM_PLANE_HELPER_NO_SCALING, 188 1.3 riastrad false, false, &visible); 189 1.1 riastrad if (ret) 190 1.1 riastrad return ret; 191 1.1 riastrad 192 1.3 riastrad if (!visible) 193 1.3 riastrad /* 194 1.3 riastrad * Primary plane isn't visible. Note that unless a driver 195 1.3 riastrad * provides their own disable function, this will just 196 1.3 riastrad * wind up returning -EINVAL to userspace. 197 1.3 riastrad */ 198 1.5 riastrad return plane->funcs->disable_plane(plane, ctx); 199 1.3 riastrad 200 1.1 riastrad /* Find current connectors for CRTC */ 201 1.1 riastrad num_connectors = get_connectors_for_crtc(crtc, NULL, 0); 202 1.1 riastrad BUG_ON(num_connectors == 0); 203 1.5 riastrad connector_list = kcalloc(num_connectors, sizeof(*connector_list), 204 1.1 riastrad GFP_KERNEL); 205 1.1 riastrad if (!connector_list) 206 1.1 riastrad return -ENOMEM; 207 1.1 riastrad get_connectors_for_crtc(crtc, connector_list, num_connectors); 208 1.1 riastrad 209 1.1 riastrad set.connectors = connector_list; 210 1.1 riastrad set.num_connectors = num_connectors; 211 1.1 riastrad 212 1.1 riastrad /* 213 1.3 riastrad * We call set_config() directly here rather than using 214 1.1 riastrad * drm_mode_set_config_internal. We're reprogramming the same 215 1.1 riastrad * connectors that were already in use, so we shouldn't need the extra 216 1.1 riastrad * cross-CRTC fb refcounting to accomodate stealing connectors. 217 1.1 riastrad * drm_mode_setplane() already handles the basic refcounting for the 218 1.1 riastrad * framebuffers involved in this operation. 219 1.1 riastrad */ 220 1.5 riastrad ret = crtc->funcs->set_config(&set, ctx); 221 1.1 riastrad 222 1.1 riastrad kfree(connector_list); 223 1.1 riastrad return ret; 224 1.1 riastrad } 225 1.1 riastrad 226 1.5 riastrad static int drm_primary_helper_disable(struct drm_plane *plane, 227 1.5 riastrad struct drm_modeset_acquire_ctx *ctx) 228 1.1 riastrad { 229 1.1 riastrad return -EINVAL; 230 1.1 riastrad } 231 1.1 riastrad 232 1.1 riastrad /** 233 1.1 riastrad * drm_primary_helper_destroy() - Helper for primary plane destruction 234 1.1 riastrad * @plane: plane to destroy 235 1.1 riastrad * 236 1.1 riastrad * Provides a default plane destroy handler for primary planes. This handler 237 1.1 riastrad * is called during CRTC destruction. We disable the primary plane, remove 238 1.1 riastrad * it from the DRM plane list, and deallocate the plane structure. 239 1.1 riastrad */ 240 1.1 riastrad void drm_primary_helper_destroy(struct drm_plane *plane) 241 1.1 riastrad { 242 1.1 riastrad drm_plane_cleanup(plane); 243 1.1 riastrad kfree(plane); 244 1.1 riastrad } 245 1.1 riastrad EXPORT_SYMBOL(drm_primary_helper_destroy); 246 1.1 riastrad 247 1.1 riastrad const struct drm_plane_funcs drm_primary_helper_funcs = { 248 1.1 riastrad .update_plane = drm_primary_helper_update, 249 1.1 riastrad .disable_plane = drm_primary_helper_disable, 250 1.1 riastrad .destroy = drm_primary_helper_destroy, 251 1.1 riastrad }; 252 1.1 riastrad EXPORT_SYMBOL(drm_primary_helper_funcs); 253