1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 3 /* 4 * Xen para-virtual DRM device 5 * 6 * Copyright (C) 2016-2018 EPAM Systems Inc. 7 * 8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com> 9 */ 10 11 #include <drm/drm_atomic.h> 12 #include <drm/drm_atomic_helper.h> 13 #include <drm/drm_drv.h> 14 #include <drm/drm_fourcc.h> 15 #include <drm/drm_gem.h> 16 #include <drm/drm_gem_framebuffer_helper.h> 17 #include <drm/drm_probe_helper.h> 18 #include <drm/drm_vblank.h> 19 20 #include "xen_drm_front.h" 21 #include "xen_drm_front_conn.h" 22 #include "xen_drm_front_kms.h" 23 24 /* 25 * Timeout in ms to wait for frame done event from the backend: 26 * must be a bit more than IO time-out 27 */ 28 #define FRAME_DONE_TO_MS (XEN_DRM_FRONT_WAIT_BACK_MS + 100) 29 30 static struct xen_drm_front_drm_pipeline * 31 to_xen_drm_pipeline(struct drm_simple_display_pipe *pipe) 32 { 33 return container_of(pipe, struct xen_drm_front_drm_pipeline, pipe); 34 } 35 36 static void fb_destroy(struct drm_framebuffer *fb) 37 { 38 struct xen_drm_front_drm_info *drm_info = fb->dev->dev_private; 39 int idx; 40 41 if (drm_dev_enter(fb->dev, &idx)) { 42 xen_drm_front_fb_detach(drm_info->front_info, 43 xen_drm_front_fb_to_cookie(fb)); 44 drm_dev_exit(idx); 45 } 46 drm_gem_fb_destroy(fb); 47 } 48 49 static const struct drm_framebuffer_funcs fb_funcs = { 50 .destroy = fb_destroy, 51 }; 52 53 static struct drm_framebuffer * 54 fb_create(struct drm_device *dev, struct drm_file *filp, 55 const struct drm_mode_fb_cmd2 *mode_cmd) 56 { 57 struct xen_drm_front_drm_info *drm_info = dev->dev_private; 58 struct drm_framebuffer *fb; 59 struct drm_gem_object *gem_obj; 60 int ret; 61 62 fb = drm_gem_fb_create_with_funcs(dev, filp, mode_cmd, &fb_funcs); 63 if (IS_ERR_OR_NULL(fb)) 64 return fb; 65 66 gem_obj = fb->obj[0]; 67 68 ret = xen_drm_front_fb_attach(drm_info->front_info, 69 xen_drm_front_dbuf_to_cookie(gem_obj), 70 xen_drm_front_fb_to_cookie(fb), 71 fb->width, fb->height, 72 fb->format->format); 73 if (ret < 0) { 74 DRM_ERROR("Back failed to attach FB %p: %d\n", fb, ret); 75 goto fail; 76 } 77 78 return fb; 79 80 fail: 81 drm_gem_fb_destroy(fb); 82 return ERR_PTR(ret); 83 } 84 85 static const struct drm_mode_config_funcs mode_config_funcs = { 86 .fb_create = fb_create, 87 .atomic_check = drm_atomic_helper_check, 88 .atomic_commit = drm_atomic_helper_commit, 89 }; 90 91 static void send_pending_event(struct xen_drm_front_drm_pipeline *pipeline) 92 { 93 struct drm_crtc *crtc = &pipeline->pipe.crtc; 94 struct drm_device *dev = crtc->dev; 95 unsigned long flags; 96 97 spin_lock_irqsave(&dev->event_lock, flags); 98 if (pipeline->pending_event) 99 drm_crtc_send_vblank_event(crtc, pipeline->pending_event); 100 pipeline->pending_event = NULL; 101 spin_unlock_irqrestore(&dev->event_lock, flags); 102 } 103 104 static void display_enable(struct drm_simple_display_pipe *pipe, 105 struct drm_crtc_state *crtc_state, 106 struct drm_plane_state *plane_state) 107 { 108 struct xen_drm_front_drm_pipeline *pipeline = 109 to_xen_drm_pipeline(pipe); 110 struct drm_crtc *crtc = &pipe->crtc; 111 struct drm_framebuffer *fb = plane_state->fb; 112 int ret, idx; 113 114 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 115 return; 116 117 ret = xen_drm_front_mode_set(pipeline, crtc->x, crtc->y, 118 fb->width, fb->height, 119 fb->format->cpp[0] * 8, 120 xen_drm_front_fb_to_cookie(fb)); 121 122 if (ret) { 123 DRM_ERROR("Failed to enable display: %d\n", ret); 124 pipeline->conn_connected = false; 125 } 126 127 drm_dev_exit(idx); 128 } 129 130 static void display_disable(struct drm_simple_display_pipe *pipe) 131 { 132 struct xen_drm_front_drm_pipeline *pipeline = 133 to_xen_drm_pipeline(pipe); 134 int ret = 0, idx; 135 136 if (drm_dev_enter(pipe->crtc.dev, &idx)) { 137 ret = xen_drm_front_mode_set(pipeline, 0, 0, 0, 0, 0, 138 xen_drm_front_fb_to_cookie(NULL)); 139 drm_dev_exit(idx); 140 } 141 if (ret) 142 DRM_ERROR("Failed to disable display: %d\n", ret); 143 144 /* Make sure we can restart with enabled connector next time */ 145 pipeline->conn_connected = true; 146 147 /* release stalled event if any */ 148 send_pending_event(pipeline); 149 } 150 151 void xen_drm_front_kms_on_frame_done(struct xen_drm_front_drm_pipeline *pipeline, 152 u64 fb_cookie) 153 { 154 /* 155 * This runs in interrupt context, e.g. under 156 * drm_info->front_info->io_lock, so we cannot call _sync version 157 * to cancel the work 158 */ 159 cancel_delayed_work(&pipeline->pflip_to_worker); 160 161 send_pending_event(pipeline); 162 } 163 164 static void pflip_to_worker(struct work_struct *work) 165 { 166 struct delayed_work *delayed_work = to_delayed_work(work); 167 struct xen_drm_front_drm_pipeline *pipeline = 168 container_of(delayed_work, 169 struct xen_drm_front_drm_pipeline, 170 pflip_to_worker); 171 172 DRM_ERROR("Frame done timed-out, releasing"); 173 send_pending_event(pipeline); 174 } 175 176 static bool display_send_page_flip(struct drm_simple_display_pipe *pipe, 177 struct drm_plane_state *old_plane_state) 178 { 179 struct drm_plane_state *plane_state = 180 drm_atomic_get_new_plane_state(old_plane_state->state, 181 &pipe->plane); 182 183 /* 184 * If old_plane_state->fb is NULL and plane_state->fb is not, 185 * then this is an atomic commit which will enable display. 186 * If old_plane_state->fb is not NULL and plane_state->fb is, 187 * then this is an atomic commit which will disable display. 188 * Ignore these and do not send page flip as this framebuffer will be 189 * sent to the backend as a part of display_set_config call. 190 */ 191 if (old_plane_state->fb && plane_state->fb) { 192 struct xen_drm_front_drm_pipeline *pipeline = 193 to_xen_drm_pipeline(pipe); 194 struct xen_drm_front_drm_info *drm_info = pipeline->drm_info; 195 int ret; 196 197 schedule_delayed_work(&pipeline->pflip_to_worker, 198 msecs_to_jiffies(FRAME_DONE_TO_MS)); 199 200 ret = xen_drm_front_page_flip(drm_info->front_info, 201 pipeline->index, 202 xen_drm_front_fb_to_cookie(plane_state->fb)); 203 if (ret) { 204 DRM_ERROR("Failed to send page flip request to backend: %d\n", ret); 205 206 pipeline->conn_connected = false; 207 /* 208 * Report the flip not handled, so pending event is 209 * sent, unblocking user-space. 210 */ 211 return false; 212 } 213 /* 214 * Signal that page flip was handled, pending event will be sent 215 * on frame done event from the backend. 216 */ 217 return true; 218 } 219 220 return false; 221 } 222 223 static void display_update(struct drm_simple_display_pipe *pipe, 224 struct drm_plane_state *old_plane_state) 225 { 226 struct xen_drm_front_drm_pipeline *pipeline = 227 to_xen_drm_pipeline(pipe); 228 struct drm_crtc *crtc = &pipe->crtc; 229 struct drm_pending_vblank_event *event; 230 int idx; 231 232 event = crtc->state->event; 233 if (event) { 234 struct drm_device *dev = crtc->dev; 235 unsigned long flags; 236 237 WARN_ON(pipeline->pending_event); 238 239 spin_lock_irqsave(&dev->event_lock, flags); 240 crtc->state->event = NULL; 241 242 pipeline->pending_event = event; 243 spin_unlock_irqrestore(&dev->event_lock, flags); 244 } 245 246 if (!drm_dev_enter(pipe->crtc.dev, &idx)) { 247 send_pending_event(pipeline); 248 return; 249 } 250 251 /* 252 * Send page flip request to the backend *after* we have event cached 253 * above, so on page flip done event from the backend we can 254 * deliver it and there is no race condition between this code and 255 * event from the backend. 256 * If this is not a page flip, e.g. no flip done event from the backend 257 * is expected, then send now. 258 */ 259 if (!display_send_page_flip(pipe, old_plane_state)) 260 send_pending_event(pipeline); 261 262 drm_dev_exit(idx); 263 } 264 265 static enum drm_mode_status 266 display_mode_valid(struct drm_simple_display_pipe *pipe, 267 const struct drm_display_mode *mode) 268 { 269 struct xen_drm_front_drm_pipeline *pipeline = 270 container_of(pipe, struct xen_drm_front_drm_pipeline, 271 pipe); 272 273 if (mode->hdisplay != pipeline->width) 274 return MODE_ERROR; 275 276 if (mode->vdisplay != pipeline->height) 277 return MODE_ERROR; 278 279 return MODE_OK; 280 } 281 282 static const struct drm_simple_display_pipe_funcs display_funcs = { 283 .mode_valid = display_mode_valid, 284 .enable = display_enable, 285 .disable = display_disable, 286 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb, 287 .update = display_update, 288 }; 289 290 static int display_pipe_init(struct xen_drm_front_drm_info *drm_info, 291 int index, struct xen_drm_front_cfg_connector *cfg, 292 struct xen_drm_front_drm_pipeline *pipeline) 293 { 294 struct drm_device *dev = drm_info->drm_dev; 295 const u32 *formats; 296 int format_count; 297 int ret; 298 299 pipeline->drm_info = drm_info; 300 pipeline->index = index; 301 pipeline->height = cfg->height; 302 pipeline->width = cfg->width; 303 304 INIT_DELAYED_WORK(&pipeline->pflip_to_worker, pflip_to_worker); 305 306 ret = xen_drm_front_conn_init(drm_info, &pipeline->conn); 307 if (ret) 308 return ret; 309 310 formats = xen_drm_front_conn_get_formats(&format_count); 311 312 return drm_simple_display_pipe_init(dev, &pipeline->pipe, 313 &display_funcs, formats, 314 format_count, NULL, 315 &pipeline->conn); 316 } 317 318 int xen_drm_front_kms_init(struct xen_drm_front_drm_info *drm_info) 319 { 320 struct drm_device *dev = drm_info->drm_dev; 321 int i, ret; 322 323 drm_mode_config_init(dev); 324 325 dev->mode_config.min_width = 0; 326 dev->mode_config.min_height = 0; 327 dev->mode_config.max_width = 4095; 328 dev->mode_config.max_height = 2047; 329 dev->mode_config.funcs = &mode_config_funcs; 330 331 for (i = 0; i < drm_info->front_info->cfg.num_connectors; i++) { 332 struct xen_drm_front_cfg_connector *cfg = 333 &drm_info->front_info->cfg.connectors[i]; 334 struct xen_drm_front_drm_pipeline *pipeline = 335 &drm_info->pipeline[i]; 336 337 ret = display_pipe_init(drm_info, i, cfg, pipeline); 338 if (ret) { 339 drm_mode_config_cleanup(dev); 340 return ret; 341 } 342 } 343 344 drm_mode_config_reset(dev); 345 drm_kms_helper_poll_init(dev); 346 return 0; 347 } 348 349 void xen_drm_front_kms_fini(struct xen_drm_front_drm_info *drm_info) 350 { 351 int i; 352 353 for (i = 0; i < drm_info->front_info->cfg.num_connectors; i++) { 354 struct xen_drm_front_drm_pipeline *pipeline = 355 &drm_info->pipeline[i]; 356 357 cancel_delayed_work_sync(&pipeline->pflip_to_worker); 358 359 send_pending_event(pipeline); 360 } 361 } 362