Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
0004  * Author: Brian Starkey <brian.starkey@arm.com>
0005  *
0006  * This program is free software and is provided to you under the terms of the
0007  * GNU General Public License version 2 as published by the Free Software
0008  * Foundation, and any use by you of this program is subject to the terms
0009  * of such GNU licence.
0010  */
0011 
0012 #include <linux/dma-fence.h>
0013 
0014 #include <drm/drm_crtc.h>
0015 #include <drm/drm_device.h>
0016 #include <drm/drm_drv.h>
0017 #include <drm/drm_framebuffer.h>
0018 #include <drm/drm_modeset_helper_vtables.h>
0019 #include <drm/drm_property.h>
0020 #include <drm/drm_writeback.h>
0021 
0022 /**
0023  * DOC: overview
0024  *
0025  * Writeback connectors are used to expose hardware which can write the output
0026  * from a CRTC to a memory buffer. They are used and act similarly to other
0027  * types of connectors, with some important differences:
0028  *
0029  * * Writeback connectors don't provide a way to output visually to the user.
0030  *
0031  * * Writeback connectors are visible to userspace only when the client sets
0032  *   DRM_CLIENT_CAP_WRITEBACK_CONNECTORS.
0033  *
0034  * * Writeback connectors don't have EDID.
0035  *
0036  * A framebuffer may only be attached to a writeback connector when the
0037  * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the
0038  * framebuffer applies only to a single commit (see below). A framebuffer may
0039  * not be attached while the CRTC is off.
0040  *
0041  * Unlike with planes, when a writeback framebuffer is removed by userspace DRM
0042  * makes no attempt to remove it from active use by the connector. This is
0043  * because no method is provided to abort a writeback operation, and in any
0044  * case making a new commit whilst a writeback is ongoing is undefined (see
0045  * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished,
0046  * the framebuffer will automatically no longer be in active use. As it will
0047  * also have already been removed from the framebuffer list, there will be no
0048  * way for any userspace application to retrieve a reference to it in the
0049  * intervening period.
0050  *
0051  * Writeback connectors have some additional properties, which userspace
0052  * can use to query and control them:
0053  *
0054  *  "WRITEBACK_FB_ID":
0055  *  Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the
0056  *  framebuffer to be written by the writeback connector. This property is
0057  *  similar to the FB_ID property on planes, but will always read as zero
0058  *  and is not preserved across commits.
0059  *  Userspace must set this property to an output buffer every time it
0060  *  wishes the buffer to get filled.
0061  *
0062  *  "WRITEBACK_PIXEL_FORMATS":
0063  *  Immutable blob property to store the supported pixel formats table. The
0064  *  data is an array of u32 DRM_FORMAT_* fourcc values.
0065  *  Userspace can use this blob to find out what pixel formats are supported
0066  *  by the connector's writeback engine.
0067  *
0068  *  "WRITEBACK_OUT_FENCE_PTR":
0069  *  Userspace can use this property to provide a pointer for the kernel to
0070  *  fill with a sync_file file descriptor, which will signal once the
0071  *  writeback is finished. The value should be the address of a 32-bit
0072  *  signed integer, cast to a u64.
0073  *  Userspace should wait for this fence to signal before making another
0074  *  commit affecting any of the same CRTCs, Planes or Connectors.
0075  *  **Failure to do so will result in undefined behaviour.**
0076  *  For this reason it is strongly recommended that all userspace
0077  *  applications making use of writeback connectors *always* retrieve an
0078  *  out-fence for the commit and use it appropriately.
0079  *  From userspace, this property will always read as zero.
0080  */
0081 
0082 #define fence_to_wb_connector(x) container_of(x->lock, \
0083                           struct drm_writeback_connector, \
0084                           fence_lock)
0085 
0086 static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
0087 {
0088     struct drm_writeback_connector *wb_connector =
0089         fence_to_wb_connector(fence);
0090 
0091     return wb_connector->base.dev->driver->name;
0092 }
0093 
0094 static const char *
0095 drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
0096 {
0097     struct drm_writeback_connector *wb_connector =
0098         fence_to_wb_connector(fence);
0099 
0100     return wb_connector->timeline_name;
0101 }
0102 
0103 static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
0104 {
0105     return true;
0106 }
0107 
0108 static const struct dma_fence_ops drm_writeback_fence_ops = {
0109     .get_driver_name = drm_writeback_fence_get_driver_name,
0110     .get_timeline_name = drm_writeback_fence_get_timeline_name,
0111     .enable_signaling = drm_writeback_fence_enable_signaling,
0112 };
0113 
0114 static int create_writeback_properties(struct drm_device *dev)
0115 {
0116     struct drm_property *prop;
0117 
0118     if (!dev->mode_config.writeback_fb_id_property) {
0119         prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
0120                           "WRITEBACK_FB_ID",
0121                           DRM_MODE_OBJECT_FB);
0122         if (!prop)
0123             return -ENOMEM;
0124         dev->mode_config.writeback_fb_id_property = prop;
0125     }
0126 
0127     if (!dev->mode_config.writeback_pixel_formats_property) {
0128         prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
0129                        DRM_MODE_PROP_ATOMIC |
0130                        DRM_MODE_PROP_IMMUTABLE,
0131                        "WRITEBACK_PIXEL_FORMATS", 0);
0132         if (!prop)
0133             return -ENOMEM;
0134         dev->mode_config.writeback_pixel_formats_property = prop;
0135     }
0136 
0137     if (!dev->mode_config.writeback_out_fence_ptr_property) {
0138         prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
0139                          "WRITEBACK_OUT_FENCE_PTR", 0,
0140                          U64_MAX);
0141         if (!prop)
0142             return -ENOMEM;
0143         dev->mode_config.writeback_out_fence_ptr_property = prop;
0144     }
0145 
0146     return 0;
0147 }
0148 
0149 static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
0150     .destroy = drm_encoder_cleanup,
0151 };
0152 
0153 /**
0154  * drm_writeback_connector_init - Initialize a writeback connector and its properties
0155  * @dev: DRM device
0156  * @wb_connector: Writeback connector to initialize
0157  * @con_funcs: Connector funcs vtable
0158  * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
0159  * @formats: Array of supported pixel formats for the writeback engine
0160  * @n_formats: Length of the formats array
0161  * @possible_crtcs: possible crtcs for the internal writeback encoder
0162  *
0163  * This function creates the writeback-connector-specific properties if they
0164  * have not been already created, initializes the connector as
0165  * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
0166  * values. It will also create an internal encoder associated with the
0167  * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for
0168  * the encoder helper.
0169  *
0170  * Drivers should always use this function instead of drm_connector_init() to
0171  * set up writeback connectors.
0172  *
0173  * Returns: 0 on success, or a negative error code
0174  */
0175 int drm_writeback_connector_init(struct drm_device *dev,
0176                  struct drm_writeback_connector *wb_connector,
0177                  const struct drm_connector_funcs *con_funcs,
0178                  const struct drm_encoder_helper_funcs *enc_helper_funcs,
0179                  const u32 *formats, int n_formats,
0180                  u32 possible_crtcs)
0181 {
0182     int ret = 0;
0183 
0184     drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
0185 
0186     wb_connector->encoder.possible_crtcs = possible_crtcs;
0187 
0188     ret = drm_encoder_init(dev, &wb_connector->encoder,
0189                    &drm_writeback_encoder_funcs,
0190                    DRM_MODE_ENCODER_VIRTUAL, NULL);
0191     if (ret)
0192         return ret;
0193 
0194     ret = drm_writeback_connector_init_with_encoder(dev, wb_connector, &wb_connector->encoder,
0195             con_funcs, formats, n_formats);
0196 
0197     if (ret)
0198         drm_encoder_cleanup(&wb_connector->encoder);
0199 
0200     return ret;
0201 }
0202 EXPORT_SYMBOL(drm_writeback_connector_init);
0203 
0204 /**
0205  * drm_writeback_connector_init_with_encoder - Initialize a writeback connector with
0206  * a custom encoder
0207  *
0208  * @dev: DRM device
0209  * @wb_connector: Writeback connector to initialize
0210  * @enc: handle to the already initialized drm encoder
0211  * @con_funcs: Connector funcs vtable
0212  * @formats: Array of supported pixel formats for the writeback engine
0213  * @n_formats: Length of the formats array
0214  *
0215  * This function creates the writeback-connector-specific properties if they
0216  * have not been already created, initializes the connector as
0217  * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
0218  * values.
0219  *
0220  * This function assumes that the drm_writeback_connector's encoder has already been
0221  * created and initialized before invoking this function.
0222  *
0223  * In addition, this function also assumes that callers of this API will manage
0224  * assigning the encoder helper functions, possible_crtcs and any other encoder
0225  * specific operation.
0226  *
0227  * Drivers should always use this function instead of drm_connector_init() to
0228  * set up writeback connectors if they want to manage themselves the lifetime of the
0229  * associated encoder.
0230  *
0231  * Returns: 0 on success, or a negative error code
0232  */
0233 int drm_writeback_connector_init_with_encoder(struct drm_device *dev,
0234         struct drm_writeback_connector *wb_connector, struct drm_encoder *enc,
0235         const struct drm_connector_funcs *con_funcs, const u32 *formats,
0236         int n_formats)
0237 {
0238     struct drm_property_blob *blob;
0239     struct drm_connector *connector = &wb_connector->base;
0240     struct drm_mode_config *config = &dev->mode_config;
0241     int ret = create_writeback_properties(dev);
0242 
0243     if (ret != 0)
0244         return ret;
0245 
0246     blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
0247                     formats);
0248     if (IS_ERR(blob))
0249         return PTR_ERR(blob);
0250 
0251 
0252     connector->interlace_allowed = 0;
0253 
0254     ret = drm_connector_init(dev, connector, con_funcs,
0255                  DRM_MODE_CONNECTOR_WRITEBACK);
0256     if (ret)
0257         goto connector_fail;
0258 
0259     ret = drm_connector_attach_encoder(connector, enc);
0260     if (ret)
0261         goto attach_fail;
0262 
0263     INIT_LIST_HEAD(&wb_connector->job_queue);
0264     spin_lock_init(&wb_connector->job_lock);
0265 
0266     wb_connector->fence_context = dma_fence_context_alloc(1);
0267     spin_lock_init(&wb_connector->fence_lock);
0268     snprintf(wb_connector->timeline_name,
0269          sizeof(wb_connector->timeline_name),
0270          "CONNECTOR:%d-%s", connector->base.id, connector->name);
0271 
0272     drm_object_attach_property(&connector->base,
0273                    config->writeback_out_fence_ptr_property, 0);
0274 
0275     drm_object_attach_property(&connector->base,
0276                    config->writeback_fb_id_property, 0);
0277 
0278     drm_object_attach_property(&connector->base,
0279                    config->writeback_pixel_formats_property,
0280                    blob->base.id);
0281     wb_connector->pixel_formats_blob_ptr = blob;
0282 
0283     return 0;
0284 
0285 attach_fail:
0286     drm_connector_cleanup(connector);
0287 connector_fail:
0288     drm_property_blob_put(blob);
0289     return ret;
0290 }
0291 EXPORT_SYMBOL(drm_writeback_connector_init_with_encoder);
0292 
0293 int drm_writeback_set_fb(struct drm_connector_state *conn_state,
0294              struct drm_framebuffer *fb)
0295 {
0296     WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
0297 
0298     if (!conn_state->writeback_job) {
0299         conn_state->writeback_job =
0300             kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
0301         if (!conn_state->writeback_job)
0302             return -ENOMEM;
0303 
0304         conn_state->writeback_job->connector =
0305             drm_connector_to_writeback(conn_state->connector);
0306     }
0307 
0308     drm_framebuffer_assign(&conn_state->writeback_job->fb, fb);
0309     return 0;
0310 }
0311 
0312 int drm_writeback_prepare_job(struct drm_writeback_job *job)
0313 {
0314     struct drm_writeback_connector *connector = job->connector;
0315     const struct drm_connector_helper_funcs *funcs =
0316         connector->base.helper_private;
0317     int ret;
0318 
0319     if (funcs->prepare_writeback_job) {
0320         ret = funcs->prepare_writeback_job(connector, job);
0321         if (ret < 0)
0322             return ret;
0323     }
0324 
0325     job->prepared = true;
0326     return 0;
0327 }
0328 EXPORT_SYMBOL(drm_writeback_prepare_job);
0329 
0330 /**
0331  * drm_writeback_queue_job - Queue a writeback job for later signalling
0332  * @wb_connector: The writeback connector to queue a job on
0333  * @conn_state: The connector state containing the job to queue
0334  *
0335  * This function adds the job contained in @conn_state to the job_queue for a
0336  * writeback connector. It takes ownership of the writeback job and sets the
0337  * @conn_state->writeback_job to NULL, and so no access to the job may be
0338  * performed by the caller after this function returns.
0339  *
0340  * Drivers must ensure that for a given writeback connector, jobs are queued in
0341  * exactly the same order as they will be completed by the hardware (and
0342  * signaled via drm_writeback_signal_completion).
0343  *
0344  * For every call to drm_writeback_queue_job() there must be exactly one call to
0345  * drm_writeback_signal_completion()
0346  *
0347  * See also: drm_writeback_signal_completion()
0348  */
0349 void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
0350                  struct drm_connector_state *conn_state)
0351 {
0352     struct drm_writeback_job *job;
0353     unsigned long flags;
0354 
0355     job = conn_state->writeback_job;
0356     conn_state->writeback_job = NULL;
0357 
0358     spin_lock_irqsave(&wb_connector->job_lock, flags);
0359     list_add_tail(&job->list_entry, &wb_connector->job_queue);
0360     spin_unlock_irqrestore(&wb_connector->job_lock, flags);
0361 }
0362 EXPORT_SYMBOL(drm_writeback_queue_job);
0363 
0364 void drm_writeback_cleanup_job(struct drm_writeback_job *job)
0365 {
0366     struct drm_writeback_connector *connector = job->connector;
0367     const struct drm_connector_helper_funcs *funcs =
0368         connector->base.helper_private;
0369 
0370     if (job->prepared && funcs->cleanup_writeback_job)
0371         funcs->cleanup_writeback_job(connector, job);
0372 
0373     if (job->fb)
0374         drm_framebuffer_put(job->fb);
0375 
0376     if (job->out_fence)
0377         dma_fence_put(job->out_fence);
0378 
0379     kfree(job);
0380 }
0381 EXPORT_SYMBOL(drm_writeback_cleanup_job);
0382 
0383 /*
0384  * @cleanup_work: deferred cleanup of a writeback job
0385  *
0386  * The job cannot be cleaned up directly in drm_writeback_signal_completion,
0387  * because it may be called in interrupt context. Dropping the framebuffer
0388  * reference can sleep, and so the cleanup is deferred to a workqueue.
0389  */
0390 static void cleanup_work(struct work_struct *work)
0391 {
0392     struct drm_writeback_job *job = container_of(work,
0393                              struct drm_writeback_job,
0394                              cleanup_work);
0395 
0396     drm_writeback_cleanup_job(job);
0397 }
0398 
0399 /**
0400  * drm_writeback_signal_completion - Signal the completion of a writeback job
0401  * @wb_connector: The writeback connector whose job is complete
0402  * @status: Status code to set in the writeback out_fence (0 for success)
0403  *
0404  * Drivers should call this to signal the completion of a previously queued
0405  * writeback job. It should be called as soon as possible after the hardware
0406  * has finished writing, and may be called from interrupt context.
0407  * It is the driver's responsibility to ensure that for a given connector, the
0408  * hardware completes writeback jobs in the same order as they are queued.
0409  *
0410  * Unless the driver is holding its own reference to the framebuffer, it must
0411  * not be accessed after calling this function.
0412  *
0413  * See also: drm_writeback_queue_job()
0414  */
0415 void
0416 drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
0417                 int status)
0418 {
0419     unsigned long flags;
0420     struct drm_writeback_job *job;
0421     struct dma_fence *out_fence;
0422 
0423     spin_lock_irqsave(&wb_connector->job_lock, flags);
0424     job = list_first_entry_or_null(&wb_connector->job_queue,
0425                        struct drm_writeback_job,
0426                        list_entry);
0427     if (job)
0428         list_del(&job->list_entry);
0429 
0430     spin_unlock_irqrestore(&wb_connector->job_lock, flags);
0431 
0432     if (WARN_ON(!job))
0433         return;
0434 
0435     out_fence = job->out_fence;
0436     if (out_fence) {
0437         if (status)
0438             dma_fence_set_error(out_fence, status);
0439         dma_fence_signal(out_fence);
0440         dma_fence_put(out_fence);
0441         job->out_fence = NULL;
0442     }
0443 
0444     INIT_WORK(&job->cleanup_work, cleanup_work);
0445     queue_work(system_long_wq, &job->cleanup_work);
0446 }
0447 EXPORT_SYMBOL(drm_writeback_signal_completion);
0448 
0449 struct dma_fence *
0450 drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
0451 {
0452     struct dma_fence *fence;
0453 
0454     if (WARN_ON(wb_connector->base.connector_type !=
0455             DRM_MODE_CONNECTOR_WRITEBACK))
0456         return NULL;
0457 
0458     fence = kzalloc(sizeof(*fence), GFP_KERNEL);
0459     if (!fence)
0460         return NULL;
0461 
0462     dma_fence_init(fence, &drm_writeback_fence_ops,
0463                &wb_connector->fence_lock, wb_connector->fence_context,
0464                ++wb_connector->fence_seqno);
0465 
0466     return fence;
0467 }
0468 EXPORT_SYMBOL(drm_writeback_get_out_fence);