Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Brian Starkey <brian.starkey@arm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * This program is free software and is provided to you under the terms of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * GNU General Public License version 2 as published by the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Foundation, and any use by you of this program is subject to the terms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * of such GNU licence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/dma-fence.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <drm/drm_crtc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <drm/drm_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <drm/drm_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <drm/drm_modeset_helper_vtables.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <drm/drm_property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <drm/drm_writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * DOC: overview
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Writeback connectors are used to expose hardware which can write the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * from a CRTC to a memory buffer. They are used and act similarly to other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * types of connectors, with some important differences:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * * Writeback connectors don't provide a way to output visually to the user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * * Writeback connectors are visible to userspace only when the client sets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *   DRM_CLIENT_CAP_WRITEBACK_CONNECTORS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * * Writeback connectors don't have EDID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * A framebuffer may only be attached to a writeback connector when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * connector is attached to a CRTC. The WRITEBACK_FB_ID property which sets the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * framebuffer applies only to a single commit (see below). A framebuffer may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * not be attached while the CRTC is off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * Unlike with planes, when a writeback framebuffer is removed by userspace DRM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * makes no attempt to remove it from active use by the connector. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * because no method is provided to abort a writeback operation, and in any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * case making a new commit whilst a writeback is ongoing is undefined (see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * WRITEBACK_OUT_FENCE_PTR below). As soon as the current writeback is finished,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * the framebuffer will automatically no longer be in active use. As it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * also have already been removed from the framebuffer list, there will be no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * way for any userspace application to retrieve a reference to it in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * intervening period.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * Writeback connectors have some additional properties, which userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * can use to query and control them:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  *  "WRITEBACK_FB_ID":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *	Write-only object property storing a DRM_MODE_OBJECT_FB: it stores the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *	framebuffer to be written by the writeback connector. This property is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  *	similar to the FB_ID property on planes, but will always read as zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *	and is not preserved across commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  *	Userspace must set this property to an output buffer every time it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  *	wishes the buffer to get filled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *  "WRITEBACK_PIXEL_FORMATS":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *	Immutable blob property to store the supported pixel formats table. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  *	data is an array of u32 DRM_FORMAT_* fourcc values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *	Userspace can use this blob to find out what pixel formats are supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  *	by the connector's writeback engine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *  "WRITEBACK_OUT_FENCE_PTR":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *	Userspace can use this property to provide a pointer for the kernel to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  *	fill with a sync_file file descriptor, which will signal once the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *	writeback is finished. The value should be the address of a 32-bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *	signed integer, cast to a u64.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *	Userspace should wait for this fence to signal before making another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *	commit affecting any of the same CRTCs, Planes or Connectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *	**Failure to do so will result in undefined behaviour.**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *	For this reason it is strongly recommended that all userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  *	applications making use of writeback connectors *always* retrieve an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  *	out-fence for the commit and use it appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  *	From userspace, this property will always read as zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define fence_to_wb_connector(x) container_of(x->lock, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 					      struct drm_writeback_connector, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					      fence_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static const char *drm_writeback_fence_get_driver_name(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct drm_writeback_connector *wb_connector =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		fence_to_wb_connector(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return wb_connector->base.dev->driver->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct drm_writeback_connector *wb_connector =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		fence_to_wb_connector(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	return wb_connector->timeline_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static const struct dma_fence_ops drm_writeback_fence_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	.get_driver_name = drm_writeback_fence_get_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	.get_timeline_name = drm_writeback_fence_get_timeline_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	.enable_signaling = drm_writeback_fence_enable_signaling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int create_writeback_properties(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct drm_property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!dev->mode_config.writeback_fb_id_property) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 						  "WRITEBACK_FB_ID",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 						  DRM_MODE_OBJECT_FB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev->mode_config.writeback_fb_id_property = prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (!dev->mode_config.writeback_pixel_formats_property) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 					   DRM_MODE_PROP_ATOMIC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 					   DRM_MODE_PROP_IMMUTABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 					   "WRITEBACK_PIXEL_FORMATS", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		dev->mode_config.writeback_pixel_formats_property = prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!dev->mode_config.writeback_out_fence_ptr_property) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 						 "WRITEBACK_OUT_FENCE_PTR", 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 						 U64_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		dev->mode_config.writeback_out_fence_ptr_property = prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static const struct drm_encoder_funcs drm_writeback_encoder_funcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	.destroy = drm_encoder_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * drm_writeback_connector_init - Initialize a writeback connector and its properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * @wb_connector: Writeback connector to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * @con_funcs: Connector funcs vtable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * @enc_helper_funcs: Encoder helper funcs vtable to be used by the internal encoder
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * @formats: Array of supported pixel formats for the writeback engine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * @n_formats: Length of the formats array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * This function creates the writeback-connector-specific properties if they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * have not been already created, initializes the connector as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * type DRM_MODE_CONNECTOR_WRITEBACK, and correctly initializes the property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * values. It will also create an internal encoder associated with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * drm_writeback_connector and set it to use the @enc_helper_funcs vtable for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * the encoder helper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * Drivers should always use this function instead of drm_connector_init() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * set up writeback connectors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * Returns: 0 on success, or a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int drm_writeback_connector_init(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				 struct drm_writeback_connector *wb_connector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				 const struct drm_connector_funcs *con_funcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				 const struct drm_encoder_helper_funcs *enc_helper_funcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				 const u32 *formats, int n_formats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct drm_property_blob *blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct drm_connector *connector = &wb_connector->base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct drm_mode_config *config = &dev->mode_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	int ret = create_writeback_properties(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	blob = drm_property_create_blob(dev, n_formats * sizeof(*formats),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					formats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (IS_ERR(blob))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return PTR_ERR(blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	drm_encoder_helper_add(&wb_connector->encoder, enc_helper_funcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	ret = drm_encoder_init(dev, &wb_connector->encoder,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			       &drm_writeback_encoder_funcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	connector->interlace_allowed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	ret = drm_connector_init(dev, connector, con_funcs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 				 DRM_MODE_CONNECTOR_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		goto connector_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	ret = drm_connector_attach_encoder(connector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 						&wb_connector->encoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		goto attach_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	INIT_LIST_HEAD(&wb_connector->job_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	spin_lock_init(&wb_connector->job_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	wb_connector->fence_context = dma_fence_context_alloc(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	spin_lock_init(&wb_connector->fence_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	snprintf(wb_connector->timeline_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		 sizeof(wb_connector->timeline_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		 "CONNECTOR:%d-%s", connector->base.id, connector->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	drm_object_attach_property(&connector->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				   config->writeback_out_fence_ptr_property, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	drm_object_attach_property(&connector->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				   config->writeback_fb_id_property, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	drm_object_attach_property(&connector->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				   config->writeback_pixel_formats_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				   blob->base.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	wb_connector->pixel_formats_blob_ptr = blob;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) attach_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	drm_connector_cleanup(connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) connector_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	drm_encoder_cleanup(&wb_connector->encoder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	drm_property_blob_put(blob);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) EXPORT_SYMBOL(drm_writeback_connector_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int drm_writeback_set_fb(struct drm_connector_state *conn_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			 struct drm_framebuffer *fb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	WARN_ON(conn_state->connector->connector_type != DRM_MODE_CONNECTOR_WRITEBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (!conn_state->writeback_job) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		conn_state->writeback_job =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			kzalloc(sizeof(*conn_state->writeback_job), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (!conn_state->writeback_job)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		conn_state->writeback_job->connector =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			drm_connector_to_writeback(conn_state->connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	drm_framebuffer_assign(&conn_state->writeback_job->fb, fb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) int drm_writeback_prepare_job(struct drm_writeback_job *job)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	struct drm_writeback_connector *connector = job->connector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	const struct drm_connector_helper_funcs *funcs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		connector->base.helper_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (funcs->prepare_writeback_job) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		ret = funcs->prepare_writeback_job(connector, job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	job->prepared = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXPORT_SYMBOL(drm_writeback_prepare_job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  * drm_writeback_queue_job - Queue a writeback job for later signalling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * @wb_connector: The writeback connector to queue a job on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * @conn_state: The connector state containing the job to queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  * This function adds the job contained in @conn_state to the job_queue for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * writeback connector. It takes ownership of the writeback job and sets the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * @conn_state->writeback_job to NULL, and so no access to the job may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * performed by the caller after this function returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * Drivers must ensure that for a given writeback connector, jobs are queued in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  * exactly the same order as they will be completed by the hardware (and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * signaled via drm_writeback_signal_completion).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * For every call to drm_writeback_queue_job() there must be exactly one call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * drm_writeback_signal_completion()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * See also: drm_writeback_signal_completion()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void drm_writeback_queue_job(struct drm_writeback_connector *wb_connector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			     struct drm_connector_state *conn_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	struct drm_writeback_job *job;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	job = conn_state->writeback_job;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	conn_state->writeback_job = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	spin_lock_irqsave(&wb_connector->job_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	list_add_tail(&job->list_entry, &wb_connector->job_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) EXPORT_SYMBOL(drm_writeback_queue_job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) void drm_writeback_cleanup_job(struct drm_writeback_job *job)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct drm_writeback_connector *connector = job->connector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	const struct drm_connector_helper_funcs *funcs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		connector->base.helper_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	if (job->prepared && funcs->cleanup_writeback_job)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		funcs->cleanup_writeback_job(connector, job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (job->fb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		drm_framebuffer_put(job->fb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (job->out_fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		dma_fence_put(job->out_fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	kfree(job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) EXPORT_SYMBOL(drm_writeback_cleanup_job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  * @cleanup_work: deferred cleanup of a writeback job
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)  * The job cannot be cleaned up directly in drm_writeback_signal_completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)  * because it may be called in interrupt context. Dropping the framebuffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * reference can sleep, and so the cleanup is deferred to a workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static void cleanup_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	struct drm_writeback_job *job = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 						     struct drm_writeback_job,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 						     cleanup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	drm_writeback_cleanup_job(job);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * drm_writeback_signal_completion - Signal the completion of a writeback job
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  * @wb_connector: The writeback connector whose job is complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * @status: Status code to set in the writeback out_fence (0 for success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  * Drivers should call this to signal the completion of a previously queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * writeback job. It should be called as soon as possible after the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * has finished writing, and may be called from interrupt context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * It is the driver's responsibility to ensure that for a given connector, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * hardware completes writeback jobs in the same order as they are queued.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * Unless the driver is holding its own reference to the framebuffer, it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * not be accessed after calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  * See also: drm_writeback_queue_job()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) drm_writeback_signal_completion(struct drm_writeback_connector *wb_connector,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 				int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	struct drm_writeback_job *job;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	struct dma_fence *out_fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	spin_lock_irqsave(&wb_connector->job_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	job = list_first_entry_or_null(&wb_connector->job_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				       struct drm_writeback_job,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 				       list_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (job)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		list_del(&job->list_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	spin_unlock_irqrestore(&wb_connector->job_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (WARN_ON(!job))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	out_fence = job->out_fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (out_fence) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			dma_fence_set_error(out_fence, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		dma_fence_signal(out_fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		dma_fence_put(out_fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		job->out_fence = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	INIT_WORK(&job->cleanup_work, cleanup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	queue_work(system_long_wq, &job->cleanup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) EXPORT_SYMBOL(drm_writeback_signal_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct dma_fence *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) drm_writeback_get_out_fence(struct drm_writeback_connector *wb_connector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	struct dma_fence *fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (WARN_ON(wb_connector->base.connector_type !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		    DRM_MODE_CONNECTOR_WRITEBACK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	if (!fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	dma_fence_init(fence, &drm_writeback_fence_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		       &wb_connector->fence_lock, wb_connector->fence_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		       ++wb_connector->fence_seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return fence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) EXPORT_SYMBOL(drm_writeback_get_out_fence);