Expose GUD display brightness through the connector LUMINANCE property when DRM_LUMINANCE is enabled.
The device receives brightness as part of its display state and the protocol has no dedicated brightness request. The backlight class device works around that with a full modeset, which userspace can't rely on for a connector property since commits without DRM_MODE_ATOMIC_ALLOW_MODESET would be rejected. Send the committed luminance with the state check during a modeset, and for a luminance-only commit resend the current configuration with the new value and apply it with a state commit. Signed-off-by: Mario Limonciello <[email protected]> --- v12: * Resend the display state with the new luminance from .set_luminance and apply it, instead of mirroring LUMINANCE into tv.brightness in atomic check which forced a modeset and rejected commits without DRM_MODE_ATOMIC_ALLOW_MODESET (Sashiko) v11: * Apply LUMINANCE in the atomic commit instead of a worker, fixing an endless commit loop, a use-after-free on disconnect and brightness being clobbered on DPMS off (Sashiko) * Attach the property at connector init (Sashiko) * Keep selecting BACKLIGHT_CLASS_DEVICE (Sashiko) --- drivers/gpu/drm/gud/gud_connector.c | 61 ++++++++++++++-- drivers/gpu/drm/gud/gud_internal.h | 5 +- drivers/gpu/drm/gud/gud_pipe.c | 136 ++++++++++++++++++++---------------- 3 files changed, 134 insertions(+), 68 deletions(-) diff --git a/drivers/gpu/drm/gud/gud_connector.c b/drivers/gpu/drm/gud/gud_connector.c index ddbf3b3ebbca8..f00272f030c1c 100644 --- a/drivers/gpu/drm/gud/gud_connector.c +++ b/drivers/gpu/drm/gud/gud_connector.c @@ -13,6 +13,7 @@ #include <drm/drm_edid.h> #include <drm/drm_encoder.h> #include <drm/drm_file.h> +#include <drm/drm_framebuffer.h> #include <drm/drm_modeset_helper_vtables.h> #include <drm/drm_print.h> #include <drm/drm_probe_helper.h> @@ -314,17 +315,52 @@ static int gud_connector_atomic_check(struct drm_connector *connector, return 0; } +/* + * The device receives brightness as part of its display state, there is no + * dedicated request. Resend the current configuration with the new luminance + * and apply it, so that a luminance change doesn't need a modeset. + */ +static void gud_connector_set_luminance(const struct drm_connector_state *conn_state) +{ + struct drm_connector *connector = conn_state->connector; + struct gud_device *gdrm = to_gud_device(connector->dev); + struct drm_crtc_state *crtc_state = conn_state->crtc->state; + struct drm_plane_state *plane_state = gdrm->plane.state; + int idx, ret; + + /* + * gud_plane_atomic_check() sent the committed luminance with + * GUD_REQ_SET_STATE_CHECK for these changes and gud_crtc_atomic_enable() + * applied it. A modeset that only changed the active state didn't. + */ + if (crtc_state->mode_changed || crtc_state->connectors_changed) + return; + + if (drm_WARN_ON_ONCE(connector->dev, !plane_state->fb)) + return; + + ret = gud_state_check(gdrm, &crtc_state->mode, plane_state->fb->format, + plane_state->rotation, conn_state); + if (!ret && drm_dev_enter(connector->dev, &idx)) { + ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_COMMIT, 0, NULL, 0); + drm_dev_exit(idx); + } + if (ret) + gud_conn_err(connector, "Failed to set luminance", ret); +} + static const struct drm_connector_helper_funcs gud_connector_helper_funcs = { .detect_ctx = gud_connector_detect, .get_modes = gud_connector_get_modes, .atomic_check = gud_connector_atomic_check, + .set_luminance = gud_connector_set_luminance, }; static int gud_connector_late_register(struct drm_connector *connector) { struct gud_connector *gconn = to_gud_connector(connector); - if (gconn->initial_brightness < 0) + if (IS_ENABLED(CONFIG_DRM_LUMINANCE) || gconn->initial_brightness < 0) return 0; return gud_connector_backlight_register(gconn); @@ -593,10 +629,12 @@ static int gud_connector_add_properties(struct gud_device *gdrm, struct gud_conn return ret; } -int gud_connector_fill_properties(struct drm_connector_state *connector_state, +int gud_connector_fill_properties(const struct drm_connector_state *connector_state, struct gud_property_req *properties) { - struct gud_connector *gconn = to_gud_connector(connector_state->connector); + struct drm_connector *connector = connector_state->connector; + struct gud_connector *gconn = to_gud_connector(connector); + struct drm_tv_connector_state tv = connector_state->tv; unsigned int i; for (i = 0; i < gconn->num_properties; i++) { @@ -604,12 +642,16 @@ int gud_connector_fill_properties(struct drm_connector_state *connector_state, u64 val; if (prop == GUD_PROPERTY_BACKLIGHT_BRIGHTNESS) { - val = connector_state->tv.brightness; + /* The backlight device reuses tv.brightness to avoid subclassing */ + if (connector->luminance_property) + val = connector_state->luminance; + else + val = tv.brightness; } else { unsigned int *state_val; - state_val = gud_connector_tv_state_val(prop, &connector_state->tv); - if (drm_WARN_ON_ONCE(connector_state->connector->dev, IS_ERR(state_val))) + state_val = gud_connector_tv_state_val(prop, &tv); + if (drm_WARN_ON_ONCE(connector->dev, IS_ERR(state_val))) return PTR_ERR(state_val); val = *state_val; @@ -699,6 +741,13 @@ static int gud_connector_create(struct gud_device *gdrm, unsigned int index, return ret; } + if (IS_ENABLED(CONFIG_DRM_LUMINANCE) && gconn->initial_brightness >= 0) { + ret = drm_connector_attach_luminance_property(connector, 0, 100, + gconn->initial_brightness); + if (ret) + return ret; + } + gconn->encoder.possible_crtcs = drm_crtc_mask(&gdrm->crtc); ret = drm_encoder_init(drm, &gconn->encoder, &gud_drm_simple_encoder_funcs_cleanup, DRM_MODE_ENCODER_NONE, NULL); diff --git a/drivers/gpu/drm/gud/gud_internal.h b/drivers/gpu/drm/gud/gud_internal.h index 95744b0b6c727..5b1430f93f555 100644 --- a/drivers/gpu/drm/gud/gud_internal.h +++ b/drivers/gpu/drm/gud/gud_internal.h @@ -70,7 +70,10 @@ int gud_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_commit *state); void gud_plane_atomic_update(struct drm_plane *plane, struct drm_atomic_commit *atomic_state); -int gud_connector_fill_properties(struct drm_connector_state *connector_state, +int gud_state_check(struct gud_device *gdrm, const struct drm_display_mode *mode, + const struct drm_format_info *format, unsigned int rotation, + const struct drm_connector_state *connector_state); +int gud_connector_fill_properties(const struct drm_connector_state *connector_state, struct gud_property_req *properties); int gud_get_connectors(struct gud_device *gdrm); diff --git a/drivers/gpu/drm/gud/gud_pipe.c b/drivers/gpu/drm/gud/gud_pipe.c index 5aa5ac2f37274..638b94f55733b 100644 --- a/drivers/gpu/drm/gud/gud_pipe.c +++ b/drivers/gpu/drm/gud/gud_pipe.c @@ -450,6 +450,77 @@ static void gud_fb_handle_damage(struct gud_device *gdrm, struct drm_framebuffer gud_flush_damage(gdrm, fb, src, !drm_gem_is_imported(fb->obj[0]), damage); } +/* + * Send the display configuration to the device with GUD_REQ_SET_STATE_CHECK, + * a following GUD_REQ_SET_STATE_COMMIT applies it. + */ +int gud_state_check(struct gud_device *gdrm, const struct drm_display_mode *mode, + const struct drm_format_info *format, unsigned int rotation, + const struct drm_connector_state *connector_state) +{ + struct drm_device *drm = &gdrm->drm; + unsigned int i, num_properties; + struct gud_state_req *req; + int idx, ret; + size_t len; + + if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format) + format = gdrm->xrgb8888_emulation_format; + + len = struct_size(req, properties, + size_add(GUD_PROPERTIES_MAX_NUM, GUD_CONNECTOR_PROPERTIES_MAX_NUM)); + req = kzalloc(len, GFP_KERNEL); + if (!req) + return -ENOMEM; + + gud_from_display_mode(&req->mode, mode); + + req->format = gud_from_fourcc(format->format); + if (drm_WARN_ON_ONCE(drm, !req->format)) { + ret = -EINVAL; + goto out; + } + + req->connector = drm_connector_index(connector_state->connector); + + ret = gud_connector_fill_properties(connector_state, req->properties); + if (ret < 0) + goto out; + + num_properties = ret; + for (i = 0; i < gdrm->num_properties; i++) { + u16 prop = gdrm->properties[i]; + u64 val; + + switch (prop) { + case GUD_PROPERTY_ROTATION: + /* DRM UAPI matches the protocol so use value directly */ + val = rotation; + break; + default: + drm_WARN_ON_ONCE(drm, 1); + ret = -EINVAL; + goto out; + } + + req->properties[num_properties].prop = cpu_to_le16(prop); + req->properties[num_properties].val = cpu_to_le64(val); + num_properties++; + } + + if (drm_dev_enter(drm, &idx)) { + len = struct_size(req, properties, num_properties); + ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_CHECK, 0, req, len); + drm_dev_exit(idx); + } else { + ret = -ENODEV; + } +out: + kfree(req); + + return ret; +} + int gud_plane_atomic_check(struct drm_plane *plane, struct drm_atomic_commit *state) { @@ -458,16 +529,13 @@ int gud_plane_atomic_check(struct drm_plane *plane, struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); struct drm_crtc *crtc = new_plane_state->crtc; struct drm_crtc_state *crtc_state = NULL; - const struct drm_display_mode *mode; struct drm_framebuffer *old_fb = old_plane_state->fb; struct drm_connector_state *connector_state = NULL; struct drm_framebuffer *fb = new_plane_state->fb; const struct drm_format_info *format; struct drm_connector *connector; - unsigned int i, num_properties; - struct gud_state_req *req; - int idx, ret; - size_t len; + unsigned int i; + int ret; if (crtc) crtc_state = drm_atomic_get_new_crtc_state(state, crtc); @@ -488,7 +556,6 @@ int gud_plane_atomic_check(struct drm_plane *plane, if (old_plane_state->rotation != new_plane_state->rotation) crtc_state->mode_changed = true; - mode = &crtc_state->mode; format = fb->format; if (old_fb && old_fb->format != format) @@ -501,9 +568,6 @@ int gud_plane_atomic_check(struct drm_plane *plane, if (hweight32(crtc_state->connector_mask) != 1) return -EINVAL; - if (format->format == DRM_FORMAT_XRGB8888 && gdrm->xrgb8888_emulation_format) - format = gdrm->xrgb8888_emulation_format; - for_each_new_connector_in_state(state, connector, connector_state, i) { if (connector_state->crtc) break; @@ -530,58 +594,8 @@ int gud_plane_atomic_check(struct drm_plane *plane, if (drm_WARN_ON_ONCE(plane->dev, !connector_state)) return -ENOENT; - len = struct_size(req, properties, - size_add(GUD_PROPERTIES_MAX_NUM, GUD_CONNECTOR_PROPERTIES_MAX_NUM)); - req = kzalloc(len, GFP_KERNEL); - if (!req) - return -ENOMEM; - - gud_from_display_mode(&req->mode, mode); - - req->format = gud_from_fourcc(format->format); - if (drm_WARN_ON_ONCE(plane->dev, !req->format)) { - ret = -EINVAL; - goto out; - } - - req->connector = drm_connector_index(connector_state->connector); - - ret = gud_connector_fill_properties(connector_state, req->properties); - if (ret < 0) - goto out; - - num_properties = ret; - for (i = 0; i < gdrm->num_properties; i++) { - u16 prop = gdrm->properties[i]; - u64 val; - - switch (prop) { - case GUD_PROPERTY_ROTATION: - /* DRM UAPI matches the protocol so use value directly */ - val = new_plane_state->rotation; - break; - default: - drm_WARN_ON_ONCE(plane->dev, 1); - ret = -EINVAL; - goto out; - } - - req->properties[num_properties].prop = cpu_to_le16(prop); - req->properties[num_properties].val = cpu_to_le64(val); - num_properties++; - } - - if (drm_dev_enter(fb->dev, &idx)) { - len = struct_size(req, properties, num_properties); - ret = gud_usb_set(gdrm, GUD_REQ_SET_STATE_CHECK, 0, req, len); - drm_dev_exit(idx); - } else { - ret = -ENODEV; - } -out: - kfree(req); - - return ret; + return gud_state_check(gdrm, &crtc_state->mode, format, new_plane_state->rotation, + connector_state); } void gud_crtc_atomic_enable(struct drm_crtc *crtc, -- 2.43.0
