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 * @wb_connector: 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 struct drm_writeback_connector *wb_connector) 354 { 355 unsigned long flags; 356 struct drm_writeback_job *pos, *n; 357 358 delete_writeback_properties(dev); 359 drm_property_blob_put(wb_connector->pixel_formats_blob_ptr); 360 361 spin_lock_irqsave(&wb_connector->job_lock, flags); 362 list_for_each_entry_safe(pos, n, &wb_connector->job_queue, list_entry) { 363 list_del(&pos->list_entry); 364 drm_writeback_cleanup_job(pos); 365 } 366 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 367 } 368 369 /** 370 * drmm_writeback_connector_init - Initialize a writeback connector with 371 * a custom encoder 372 * 373 * @dev: DRM device 374 * @wb_connector: Writeback connector to initialize 375 * @con_funcs: Connector funcs vtable 376 * @enc: Encoder to connect this writeback connector 377 * @formats: Array of supported pixel formats for the writeback engine 378 * @n_formats: Length of the formats array 379 * 380 * This function initialize a writeback connector and register its cleanup. 381 * 382 * This function creates the writeback-connector-specific properties if they 383 * have not been already created, initializes the connector as 384 * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property 385 * values. 386 * 387 * Returns: 0 on success, or a negative error code 388 */ 389 int drmm_writeback_connector_init(struct drm_device *dev, 390 struct drm_writeback_connector *wb_connector, 391 const struct drm_connector_funcs *con_funcs, 392 struct drm_encoder *enc, 393 const u32 *formats, int n_formats) 394 { 395 struct drm_connector *connector = &wb_connector->base; 396 int ret; 397 398 ret = drmm_connector_init(dev, connector, con_funcs, 399 DRM_MODE_CONNECTOR_WRITEBACK, NULL); 400 if (ret) 401 return ret; 402 403 ret = __drm_writeback_connector_init(dev, wb_connector, enc, formats, 404 n_formats); 405 if (ret) 406 return ret; 407 408 ret = drmm_add_action_or_reset(dev, (void *)drm_writeback_connector_cleanup, 409 wb_connector); 410 if (ret) 411 return ret; 412 413 return 0; 414 } 415 EXPORT_SYMBOL(drmm_writeback_connector_init); 416 417 int drm_writeback_set_fb(struct drm_connector_state *conn_state, 418 struct drm_framebuffer *fb) 419 { 420 WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK); 421 422 if (!conn_state->writeback_job) { 423 conn_state->writeback_job = 424 kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL); 425 if (!conn_state->writeback_job) 426 return -ENOMEM; 427 428 conn_state->writeback_job->connector = 429 drm_connector_to_writeback(conn_state->connector); 430 } 431 432 drm_framebuffer_assign(&conn_state->writeback_job->fb, fb); 433 return 0; 434 } 435 436 int drm_writeback_prepare_job(struct drm_writeback_job *job) 437 { 438 struct drm_writeback_connector *connector = job->connector; 439 const struct drm_connector_helper_funcs *funcs = 440 connector->base.helper_private; 441 int ret; 442 443 if (funcs->prepare_writeback_job) { 444 ret = funcs->prepare_writeback_job(connector, job); 445 if (ret < 0) 446 return ret; 447 } 448 449 job->prepared = true; 450 return 0; 451 } 452 EXPORT_SYMBOL(drm_writeback_prepare_job); 453 454 /** 455 * drm_writeback_queue_job - Queue a writeback job for later signalling 456 * @wb_connector: The writeback connector to queue a job on 457 * @conn_state: The connector state containing the job to queue 458 * 459 * This function adds the job contained in @conn_state to the job_queue for a 460 * writeback connector. It takes ownership of the writeback job and sets the 461 * @conn_state->writeback_job to NULL, and so no access to the job may be 462 * performed by the caller after this function returns. 463 * 464 * Drivers must ensure that for a given writeback connector, jobs are queued in 465 * exactly the same order as they will be completed by the hardware (and 466 * signaled via drm_writeback_signal_completion). 467 * 468 * For every call to drm_writeback_queue_job() there must be exactly one call to 469 * drm_writeback_signal_completion() 470 * 471 * See also: drm_writeback_signal_completion() 472 */ 473 void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector, 474 struct drm_connector_state *conn_state) 475 { 476 struct drm_writeback_job *job; 477 unsigned long flags; 478 479 job = conn_state->writeback_job; 480 conn_state->writeback_job = NULL; 481 482 spin_lock_irqsave(&wb_connector->job_lock, flags); 483 list_add_tail(&job->list_entry, &wb_connector->job_queue); 484 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 485 } 486 EXPORT_SYMBOL(drm_writeback_queue_job); 487 488 void drm_writeback_cleanup_job(struct drm_writeback_job *job) 489 { 490 struct drm_writeback_connector *connector = job->connector; 491 const struct drm_connector_helper_funcs *funcs = 492 connector->base.helper_private; 493 494 if (job->prepared && funcs->cleanup_writeback_job) 495 funcs->cleanup_writeback_job(connector, job); 496 497 if (job->fb) 498 drm_framebuffer_put(job->fb); 499 500 if (job->out_fence) 501 dma_fence_put(job->out_fence); 502 503 kfree(job); 504 } 505 EXPORT_SYMBOL(drm_writeback_cleanup_job); 506 507 /* 508 * @cleanup_work: deferred cleanup of a writeback job 509 * 510 * The job cannot be cleaned up directly in drm_writeback_signal_completion, 511 * because it may be called in interrupt context. Dropping the framebuffer 512 * reference can sleep, and so the cleanup is deferred to a workqueue. 513 */ 514 static void cleanup_work(struct work_struct *work) 515 { 516 struct drm_writeback_job *job = container_of(work, 517 struct drm_writeback_job, 518 cleanup_work); 519 520 drm_writeback_cleanup_job(job); 521 } 522 523 /** 524 * drm_writeback_signal_completion - Signal the completion of a writeback job 525 * @wb_connector: The writeback connector whose job is complete 526 * @status: Status code to set in the writeback out_fence (0 for success) 527 * 528 * Drivers should call this to signal the completion of a previously queued 529 * writeback job. It should be called as soon as possible after the hardware 530 * has finished writing, and may be called from interrupt context. 531 * It is the driver's responsibility to ensure that for a given connector, the 532 * hardware completes writeback jobs in the same order as they are queued. 533 * 534 * Unless the driver is holding its own reference to the framebuffer, it must 535 * not be accessed after calling this function. 536 * 537 * See also: drm_writeback_queue_job() 538 */ 539 void 540 drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector, 541 int status) 542 { 543 unsigned long flags; 544 struct drm_writeback_job *job; 545 struct dma_fence *out_fence; 546 547 spin_lock_irqsave(&wb_connector->job_lock, flags); 548 job = list_first_entry_or_null(&wb_connector->job_queue, 549 struct drm_writeback_job, 550 list_entry); 551 if (job) 552 list_del(&job->list_entry); 553 554 spin_unlock_irqrestore(&wb_connector->job_lock, flags); 555 556 if (WARN_ON(!job)) 557 return; 558 559 out_fence = job->out_fence; 560 if (out_fence) { 561 if (status) 562 dma_fence_set_error(out_fence, status); 563 dma_fence_signal(out_fence); 564 dma_fence_put(out_fence); 565 job->out_fence = NULL; 566 } 567 568 INIT_WORK(&job->cleanup_work, cleanup_work); 569 queue_work(system_long_wq, &job->cleanup_work); 570 } 571 EXPORT_SYMBOL(drm_writeback_signal_completion); 572 573 struct dma_fence * 574 drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector) 575 { 576 struct dma_fence *fence; 577 578 if (WARN_ON(wb_connector->base.connector_type != 579 DRM_MODE_CONNECTOR_WRITEBACK)) 580 return NULL; 581 582 fence = kzalloc(sizeof(*fence), GFP_KERNEL); 583 if (!fence) 584 return NULL; 585 586 dma_fence_init(fence, &drm_writeback_fence_ops, 587 &wb_connector->fence_lock, wb_connector->fence_context, 588 ++wb_connector->fence_seqno); 589 590 return fence; 591 } 592 EXPORT_SYMBOL(drm_writeback_get_out_fence); 593