1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) COPYRIGHT 2016 ARM Limited. All rights reserved. 4 * Author: Brian Starkey <brian.starkey@arm.com> 5 * 6 * This program is free software and is provided to you under the terms of the 7 * GNU General Public License version 2 as published by the Free Software 8 * Foundation, and any use by you of this program is subject to the terms 9 * of such GNU licence. 10 */ 11 12 #include <linux/dma-fence.h> 13 14 #include <drm/drm_crtc.h> 15 #include <drm/drm_device.h> 16 #include <drm/drm_drv.h> 17 #include <drm/drm_framebuffer.h> 18 #include <drm/drm_managed.h> 19 #include <drm/drm_modeset_helper_vtables.h> 20 #include <drm/drm_property.h> 21 #include <drm/drm_writeback.h> 22 23 /** 24 * DOC: overview 25 * 26 * Writeback connectors are used to expose hardware which can write the output 27 * from a CRTC to a memory buffer. They are used and act similarly to other 28 * types of connectors, with some important differences: 29 * 30 * * Writeback connectors don't provide a way to output visually to the user. 31 * 32 * * Writeback connectors are visible to userspace only when the client sets 33 * DRM_CLIENT_CAP_WRITEBACK_CONNECTORS. 34 * 35 * * Writeback connectors don't have EDID. 36 * 37 * A framebuffer may only be attached to a writeback connector when the 38 * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the 39 * framebuffer applies only to a single commit (see below). A framebuffer may 40 * not be attached while the CRTC is off. 41 * 42 * Unlike with planes, when a writeback framebuffer is removed by userspace DRM 43 * makes no attempt to remove it from active use by the connector. This is 44 * because no method is provided to abort a writeback operation, and in any 45 * case making a new commit whilst a writeback is ongoing is undefined (see 46 * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished, 47 * the framebuffer will automatically no longer be in active use. As it will 48 * also have already been removed from the framebuffer list, there will be no 49 * way for any userspace application to retrieve a reference to it in the 50 * intervening period. 51 * 52 * Writeback connectors have some additional properties, which userspace 53 * can use to query and control them: 54 * 55 * "WRITEBACK_FB_ID": 56 * Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the 57 * framebuffer to be written by the writeback connector. This property is 58 * similar to the FB_ID property on planes, but will always read as zero 59 * and is not preserved across commits. 60 * Userspace must set this property to an output buffer every time it 61 * wishes the buffer to get filled. 62 * 63 * "WRITEBACK_PIXEL_FORMATS": 64 * Immutable blob property to store the supported pixel formats table. The 65 * data is an array of u32 DRM_FORMAT_* fourcc values. 66 * Userspace can use this blob to find out what pixel formats are supported 67 * by the connector's writeback engine. 68 * 69 * "WRITEBACK_OUT_FENCE_PTR": 70 * Userspace can use this property to provide a pointer for the kernel to 71 * fill with a sync_file file descriptor, which will signal once the 72 * writeback is finished. The value should be the address of a 32-bit 73 * signed integer, cast to a u64. 74 * Userspace should wait for this fence to signal before making another 75 * commit affecting any of the same CRTCs, Planes or Connectors. 76 * **Failure to do so will result in undefined behaviour.** 77 * For this reason it is strongly recommended that all userspace 78 * applications making use of writeback connectors *always* retrieve an 79 * out-fence for the commit and use it appropriately. 80 * From userspace, this property will always read as zero. 81 */ 82 83 #define fence_to_wb_connector(x) container_of(x->lock, \ 84 struct drm_writeback_connector, \ 85 fence_lock) 86 87 static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence) 88 { 89 struct drm_writeback_connector *wb_connector = 90 fence_to_wb_connector(fence); 91 92 return wb_connector->base.dev->driver->name; 93 } 94 95 static const char * 96 drm_writeback_fence_get_timeline_name(struct dma_fence *fence) 97 { 98 struct drm_writeback_connector *wb_connector = 99 fence_to_wb_connector(fence); 100 101 return wb_connector->timeline_name; 102 } 103 104 static const struct dma_fence_ops drm_writeback_fence_ops = { 105 .get_driver_name = drm_writeback_fence_get_driver_name, 106 .get_timeline_name = drm_writeback_fence_get_timeline_name, 107 }; 108 109 static int create_writeback_properties(struct drm_device *dev) 110 { 111 struct drm_property *prop; 112 113 if (!dev->mode_config.writeback_fb_id_property) { 114 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC, 115 "WRITEBACK_FB_ID", 116 DRM_MODE_OBJECT_FB); 117 if (!prop) 118 return -ENOMEM; 119 dev->mode_config.writeback_fb_id_property = prop; 120 } 121 122 if (!dev->mode_config.writeback_pixel_formats_property) { 123 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB | 124 DRM_MODE_PROP_ATOMIC | 125 DRM_MODE_PROP_IMMUTABLE, 126 "WRITEBACK_PIXEL_FORMATS", 0); 127 if (!prop) 128 return -ENOMEM; 129 dev->mode_config.writeback_pixel_formats_property = prop; 130 } 131 132 if (!dev->mode_config.writeback_out_fence_ptr_property) { 133 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, 134 "WRITEBACK_OUT_FENCE_PTR", 0, 135 U64_MAX); 136 if (!prop) 137 return -ENOMEM; 138 dev->mode_config.writeback_out_fence_ptr_property = prop; 139 } 140 141 return 0; 142 } 143 144 static const struct drm_encoder_funcs drm_writeback_encoder_funcs = { 145 .destroy = drm_encoder_cleanup, 146 }; 147 148 /** 149 * drm_writeback_connector_init - Initialize a writeback connector and its properties 150 * @dev: DRM device 151 * @wb_connector: Writeback connector to initialize 152 * @con_funcs: Connector funcs vtable 153 * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder 154 * @formats: Array of supported pixel formats for the writeback engine 155 * @n_formats: Length of the formats array 156 * @possible_crtcs: possible crtcs for the internal writeback encoder 157 * 158 * This function creates the writeback-connector-specific properties if they 159 * have not been already created, initializes the connector as 160 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property 161 * values. It will also create an internal encoder associated with the 162 * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for 163 * the encoder helper. 164 * 165 * Drivers should always use this function instead of drm_connector_init() to 166 * set up writeback connectors. 167 * 168 * Returns: 0 on success, or a negative error code 169 */ 170 int drm_writeback_connector_init(struct drm_device *dev, 171 struct drm_writeback_connector *wb_connector, 172 const struct drm_connector_funcs *con_funcs, 173 const struct drm_encoder_helper_funcs *enc_helper_funcs, 174 const u32 *formats, int n_formats, 175 u32 possible_crtcs) 176 { 177 int ret = 0; 178 179 drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs); 180 181 wb_connector->encoder.possible_crtcs = possible_crtcs; 182 183 ret = drm_encoder_init(dev, &wb_connector->encoder, 184 &drm_writeback_encoder_funcs, 185 DRM_MODE_ENCODER_VIRTUAL, NULL); 186 if (ret) 187 return ret; 188 189 ret = drm_writeback_connector_init_with_encoder(dev, wb_connector, &wb_connector->encoder, 190 con_funcs, formats, n_formats); 191 192 if (ret) 193 drm_encoder_cleanup(&wb_connector->encoder); 194 195 return ret; 196 } 197 EXPORT_SYMBOL(drm_writeback_connector_init); 198 199 static void delete_writeback_properties(struct drm_device *dev) 200 { 201 if (dev->mode_config.writeback_pixel_formats_property) { 202 drm_property_destroy(dev, dev->mode_config.writeback_pixel_formats_property); 203 dev->mode_config.writeback_pixel_formats_property = NULL; 204 } 205 if (dev->mode_config.writeback_out_fence_ptr_property) { 206 drm_property_destroy(dev, dev->mode_config.writeback_out_fence_ptr_property); 207 dev->mode_config.writeback_out_fence_ptr_property = NULL; 208 } 209 if (dev->mode_config.writeback_fb_id_property) { 210 drm_property_destroy(dev, dev->mode_config.writeback_fb_id_property); 211 dev->mode_config.writeback_fb_id_property = NULL; 212 } 213 } 214 215 /** 216 * __drm_writeback_connector_init - Initialize a writeback connector with 217 * a custom encoder 218 * 219 * @dev: DRM device 220 * @wb_connector: Writeback connector to initialize 221 * @enc: handle to the already initialized drm encoder 222 * @formats: Array of supported pixel formats for the writeback engine 223 * @n_formats: Length of the formats array 224 * 225 * This function creates the writeback-connector-specific properties if they 226 * have not been already created, initializes the connector as 227 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property 228 * values. 229 * 230 * This function assumes that the drm_writeback_connector's encoder has already been 231 * created and initialized before invoking this function. 232 * 233 * In addition, this function also assumes that callers of this API will manage 234 * assigning the encoder helper functions, possible_crtcs and any other encoder 235 * specific operation. 236 * 237 * Returns: 0 on success, or a negative error code 238 */ 239 static int __drm_writeback_connector_init(struct drm_device *dev, 240 struct drm_writeback_connector *wb_connector, 241 struct drm_encoder *enc, const u32 *formats, 242 int n_formats) 243 { 244 struct drm_connector *connector = &wb_connector->base; 245 struct drm_mode_config *config = &dev->mode_config; 246 struct drm_property_blob *blob; 247 int ret = create_writeback_properties(dev); 248 249 if (ret != 0) 250 goto failed_properties; 251 252 connector->interlace_allowed = 0; 253 254 ret = drm_connector_attach_encoder(connector, enc); 255 if (ret) 256 goto failed_properties; 257 258 blob = drm_property_create_blob(dev, n_formats * sizeof(*formats), 259 formats); 260 if (IS_ERR(blob)) { 261 ret = PTR_ERR(blob); 262 goto failed_properties; 263 } 264 265 INIT_LIST_HEAD(&wb_connector->job_queue); 266 spin_lock_init(&wb_connector->job_lock); 267 268 wb_connector->fence_context = dma_fence_context_alloc(1); 269 spin_lock_init(&wb_connector->fence_lock); 270 snprintf(wb_connector->timeline_name, 271 sizeof(wb_connector->timeline_name), 272 "CONNECTOR:%d-%s", connector->base.id, connector->name); 273 274 drm_object_attach_property(&connector->base, 275 config->writeback_out_fence_ptr_property, 0); 276 277 drm_object_attach_property(&connector->base, 278 config->writeback_fb_id_property, 0); 279 280 drm_object_attach_property(&connector->base, 281 config->writeback_pixel_formats_property, 282 blob->base.id); 283 wb_connector->pixel_formats_blob_ptr = blob; 284 285 return 0; 286 failed_properties: 287 delete_writeback_properties(dev); 288 return ret; 289 } 290 291 /** 292 * drm_writeback_connector_init_with_encoder - Initialize a writeback connector with 293 * a custom encoder 294 * 295 * @dev: DRM device 296 * @wb_connector: Writeback connector to initialize 297 * @enc: handle to the already initialized drm encoder 298 * @con_funcs: Connector funcs vtable 299 * @formats: Array of supported pixel formats for the writeback engine 300 * @n_formats: Length of the formats array 301 * 302 * This function creates the writeback-connector-specific properties if they 303 * have not been already created, initializes the connector as 304 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property 305 * values. 306 * 307 * This function assumes that the drm_writeback_connector's encoder has already been 308 * created and initialized before invoking this function. 309 * 310 * In addition, this function also assumes that callers of this API will manage 311 * assigning the encoder helper functions, possible_crtcs and any other encoder 312 * specific operation. 313 * 314 * Drivers should always use this function instead of drm_connector_init() to 315 * set up writeback connectors if they want to manage themselves the lifetime of the 316 * associated encoder. 317 * 318 * Returns: 0 on success, or a negative error code 319 */ 320 int drm_writeback_connector_init_with_encoder(struct drm_device *dev, 321 struct drm_writeback_connector *wb_connector, 322 struct drm_encoder *enc, 323 const struct drm_connector_funcs *con_funcs, 324 const u32 *formats, int n_formats) 325 { 326 struct drm_connector *connector = &wb_connector->base; 327 int ret; 328 329 ret = drm_connector_init(dev, connector, con_funcs, 330 DRM_MODE_CONNECTOR_WRITEBACK); 331 if (ret) 332 return ret; 333 334 ret = __drm_writeback_connector_init(dev, wb_connector, enc, formats, 335 n_formats); 336 if (ret) 337 drm_connector_cleanup(connector); 338 339 return ret; 340 } 341 EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder); 342 343 /** 344 * drm_writeback_connector_cleanup - Cleanup the writeback connector 345 * @dev: DRM device 346 * @data: Pointer to the writeback connector to clean up 347 * 348 * This will decrement the reference counter of blobs and destroy properties. It 349 * will also clean the remaining jobs in this writeback connector. Caution: This helper will not 350 * clean up the attached encoder and the drm_connector. 351 */ 352 static void drm_writeback_connector_cleanup(struct drm_device *dev, 353 void *data) 354 { 355 unsigned long flags; 356 struct drm_writeback_job *pos, *n; 357 struct drm_writeback_connector *wb_connector = data; 358 359 delete_writeback_properties(dev); 360 drm_property_blob_put(wb_connector->pixel_formats_blob_ptr); 361 362 spin_lock_irqsave(&wb_connector->job_lock, flags); 363 list_for_each_entry_safe(pos, n, &wb_connector->job_queue, list_entry) { 364 list_del(&pos->list_entry); 365 drm_writeback_cleanup_job(pos); 366 } 367 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 368 } 369 370 /** 371 * drmm_writeback_connector_init - Initialize a writeback connector with 372 * a custom encoder 373 * 374 * @dev: DRM device 375 * @wb_connector: Writeback connector to initialize 376 * @con_funcs: Connector funcs vtable 377 * @enc: Encoder to connect this writeback connector 378 * @formats: Array of supported pixel formats for the writeback engine 379 * @n_formats: Length of the formats array 380 * 381 * This function initialize a writeback connector and register its cleanup. 382 * 383 * This function creates the writeback-connector-specific properties if they 384 * have not been already created, initializes the connector as 385 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property 386 * values. 387 * 388 * Returns: 0 on success, or a negative error code 389 */ 390 int drmm_writeback_connector_init(struct drm_device *dev, 391 struct drm_writeback_connector *wb_connector, 392 const struct drm_connector_funcs *con_funcs, 393 struct drm_encoder *enc, 394 const u32 *formats, int n_formats) 395 { 396 struct drm_connector *connector = &wb_connector->base; 397 int ret; 398 399 ret = drmm_connector_init(dev, connector, con_funcs, 400 DRM_MODE_CONNECTOR_WRITEBACK, NULL); 401 if (ret) 402 return ret; 403 404 ret = __drm_writeback_connector_init(dev, wb_connector, enc, formats, 405 n_formats); 406 if (ret) 407 return ret; 408 409 ret = drmm_add_action_or_reset(dev, drm_writeback_connector_cleanup, 410 wb_connector); 411 if (ret) 412 return ret; 413 414 return 0; 415 } 416 EXPORT_SYMBOL(drmm_writeback_connector_init); 417 418 int drm_writeback_set_fb(struct drm_connector_state *conn_state, 419 struct drm_framebuffer *fb) 420 { 421 WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK); 422 423 if (!conn_state->writeback_job) { 424 conn_state->writeback_job = 425 kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL); 426 if (!conn_state->writeback_job) 427 return -ENOMEM; 428 429 conn_state->writeback_job->connector = 430 drm_connector_to_writeback(conn_state->connector); 431 } 432 433 drm_framebuffer_assign(&conn_state->writeback_job->fb, fb); 434 return 0; 435 } 436 437 int drm_writeback_prepare_job(struct drm_writeback_job *job) 438 { 439 struct drm_writeback_connector *connector = job->connector; 440 const struct drm_connector_helper_funcs *funcs = 441 connector->base.helper_private; 442 int ret; 443 444 if (funcs->prepare_writeback_job) { 445 ret = funcs->prepare_writeback_job(connector, job); 446 if (ret < 0) 447 return ret; 448 } 449 450 job->prepared = true; 451 return 0; 452 } 453 EXPORT_SYMBOL(drm_writeback_prepare_job); 454 455 /** 456 * drm_writeback_queue_job - Queue a writeback job for later signalling 457 * @wb_connector: The writeback connector to queue a job on 458 * @conn_state: The connector state containing the job to queue 459 * 460 * This function adds the job contained in @conn_state to the job_queue for a 461 * writeback connector. It takes ownership of the writeback job and sets the 462 * @conn_state->writeback_job to NULL, and so no access to the job may be 463 * performed by the caller after this function returns. 464 * 465 * Drivers must ensure that for a given writeback connector, jobs are queued in 466 * exactly the same order as they will be completed by the hardware (and 467 * signaled via drm_writeback_signal_completion). 468 * 469 * For every call to drm_writeback_queue_job() there must be exactly one call to 470 * drm_writeback_signal_completion() 471 * 472 * See also: drm_writeback_signal_completion() 473 */ 474 void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector, 475 struct drm_connector_state *conn_state) 476 { 477 struct drm_writeback_job *job; 478 unsigned long flags; 479 480 job = conn_state->writeback_job; 481 conn_state->writeback_job = NULL; 482 483 spin_lock_irqsave(&wb_connector->job_lock, flags); 484 list_add_tail(&job->list_entry, &wb_connector->job_queue); 485 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 486 } 487 EXPORT_SYMBOL(drm_writeback_queue_job); 488 489 void drm_writeback_cleanup_job(struct drm_writeback_job *job) 490 { 491 struct drm_writeback_connector *connector = job->connector; 492 const struct drm_connector_helper_funcs *funcs = 493 connector->base.helper_private; 494 495 if (job->prepared && funcs->cleanup_writeback_job) 496 funcs->cleanup_writeback_job(connector, job); 497 498 if (job->fb) 499 drm_framebuffer_put(job->fb); 500 501 if (job->out_fence) 502 dma_fence_put(job->out_fence); 503 504 kfree(job); 505 } 506 EXPORT_SYMBOL(drm_writeback_cleanup_job); 507 508 /* 509 * @cleanup_work: deferred cleanup of a writeback job 510 * 511 * The job cannot be cleaned up directly in drm_writeback_signal_completion, 512 * because it may be called in interrupt context. Dropping the framebuffer 513 * reference can sleep, and so the cleanup is deferred to a workqueue. 514 */ 515 static void cleanup_work(struct work_struct *work) 516 { 517 struct drm_writeback_job *job = container_of(work, 518 struct drm_writeback_job, 519 cleanup_work); 520 521 drm_writeback_cleanup_job(job); 522 } 523 524 /** 525 * drm_writeback_signal_completion - Signal the completion of a writeback job 526 * @wb_connector: The writeback connector whose job is complete 527 * @status: Status code to set in the writeback out_fence (0 for success) 528 * 529 * Drivers should call this to signal the completion of a previously queued 530 * writeback job. It should be called as soon as possible after the hardware 531 * has finished writing, and may be called from interrupt context. 532 * It is the driver's responsibility to ensure that for a given connector, the 533 * hardware completes writeback jobs in the same order as they are queued. 534 * 535 * Unless the driver is holding its own reference to the framebuffer, it must 536 * not be accessed after calling this function. 537 * 538 * See also: drm_writeback_queue_job() 539 */ 540 void 541 drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector, 542 int status) 543 { 544 unsigned long flags; 545 struct drm_writeback_job *job; 546 struct dma_fence *out_fence; 547 548 spin_lock_irqsave(&wb_connector->job_lock, flags); 549 job = list_first_entry_or_null(&wb_connector->job_queue, 550 struct drm_writeback_job, 551 list_entry); 552 if (job) 553 list_del(&job->list_entry); 554 555 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 556 557 if (WARN_ON(!job)) 558 return; 559 560 out_fence = job->out_fence; 561 if (out_fence) { 562 if (status) 563 dma_fence_set_error(out_fence, status); 564 dma_fence_signal(out_fence); 565 dma_fence_put(out_fence); 566 job->out_fence = NULL; 567 } 568 569 INIT_WORK(&job->cleanup_work, cleanup_work); 570 queue_work(system_long_wq, &job->cleanup_work); 571 } 572 EXPORT_SYMBOL(drm_writeback_signal_completion); 573 574 struct dma_fence * 575 drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector) 576 { 577 struct dma_fence *fence; 578 579 if (WARN_ON(wb_connector->base.connector_type != 580 DRM_MODE_CONNECTOR_WRITEBACK)) 581 return NULL; 582 583 fence = kzalloc(sizeof(*fence), GFP_KERNEL); 584 if (!fence) 585 return NULL; 586 587 dma_fence_init(fence, &drm_writeback_fence_ops, 588 &wb_connector->fence_lock, wb_connector->fence_context, 589 ++wb_connector->fence_seqno); 590 591 return fence; 592 } 593 EXPORT_SYMBOL(drm_writeback_get_out_fence); 594