When the firmware leaves a video port running, the planes feeding it have to be read out too, otherwise the first commit sees them as disabled and has to reprogram the whole pipeline.
Implement the atomic_sro_readout_state hook for the tidss planes. A plane is read out as disabled if its VID pipeline isn't enabled, if it isn't routed to an overlay layer, or if its CRTC is inactive. The latter two matter because tidss_crtc_atomic_disable() and a disabled video port leave the VID pipeline of their planes enabled. Otherwise, the CRTC, source and destination rectangles, zpos, alpha and blend mode are read from the registers. The zpos is the index of the overlay layer the plane is routed to, which is the normalized zpos it was committed with. The alpha is the 8-bit global alpha replicated in both bytes of the 16-bit property, the only inverse that maps full opacity back to 0xffff. The framebuffer is reconstructed from the format, picture size and increments. It can only be rebuilt when the planes aren't YUV, aren't multiplanar and don't use horizontal decimation, and the readout fails otherwise so that the core falls back to the regular initialization. The offset into the buffer, and thus the source origin, isn't recovered yet. Since the firmware allocated the buffer, its GEM object is created with drm_atomic_helper_sro_create_gem_dma_object() from the DMA address the plane fetches from. The buffer is thus never accessible from the CPU, but the framebuffer can go through the regular framebuffer helpers, and dispc_plane_setup() keeps programming the address that drm_fb_dma_get_gem_addr() returns. Use drm_atomic_helper_plane_compare_state() to compare the readout state with the committed one. Signed-off-by: Maxime Ripard <[email protected]> --- drivers/gpu/drm/tidss/tidss_plane.c | 245 ++++++++++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) diff --git a/drivers/gpu/drm/tidss/tidss_plane.c b/drivers/gpu/drm/tidss/tidss_plane.c index b0d5d6959b88..a9b4005d1d60 100644 --- a/drivers/gpu/drm/tidss/tidss_plane.c +++ b/drivers/gpu/drm/tidss/tidss_plane.c @@ -4,16 +4,20 @@ * Author: Tomi Valkeinen <[email protected]> */ #include <drm/drm_atomic.h> #include <drm/drm_atomic_helper.h> +#include <drm/drm_atomic_state_readout.h> +#include <drm/drm_atomic_state_readout_helper.h> #include <drm/drm_blend.h> #include <drm/drm_crtc.h> #include <drm/drm_fb_dma_helper.h> #include <drm/drm_fourcc.h> #include <drm/drm_framebuffer.h> #include <drm/drm_gem_atomic_helper.h> +#include <drm/drm_gem_dma_helper.h> +#include <drm/drm_gem_framebuffer_helper.h> #include <drm/drm_panic_helper.h> #include "tidss_crtc.h" #include "tidss_dispc.h" #include "tidss_drv.h" @@ -174,15 +178,256 @@ static const struct drm_plane_helper_funcs tidss_primary_plane_helper_funcs = { .atomic_enable = tidss_plane_atomic_enable, .atomic_disable = tidss_plane_atomic_disable, .get_scanout_buffer = drm_fb_dma_get_scanout_buffer, }; +static const struct drm_framebuffer_funcs tidss_plane_readout_fb_funcs = { + .destroy = drm_gem_fb_destroy, +}; + +static struct drm_framebuffer *tidss_plane_readout_fb(struct drm_plane *plane) +{ + struct drm_device *ddev = plane->dev; + struct tidss_device *tidss = to_tidss(ddev); + struct dispc_device *dispc = tidss->dispc; + struct tidss_plane *tplane = to_tidss_plane(plane); + const struct drm_format_info *info; + struct drm_gem_dma_object *dma_obj; + struct drm_framebuffer *fb; + dma_addr_t dma_addr; + size_t size; + int ret; + + fb = kzalloc_obj(*fb); + if (!fb) + return ERR_PTR(-ENOMEM); + + fb->dev = plane->dev; + + info = dispc_plane_get_current_format(dispc, tplane->hw_plane_id); + if (IS_ERR(info)) { + ret = PTR_ERR(info); + goto err_free_fb; + } + + // TODO: Figure out YUV and multiplanar formats, along with + // the color encoding and range + if (info->is_yuv) { + ret = -EINVAL; + goto err_free_fb; + } + + fb->format = info; + + dispc_plane_get_picture_size(dispc, tplane->hw_plane_id, &fb->width, + &fb->height); + + /* + * A pixel increment of 1 means that the DMA engine fetches every + * pixel of the source buffer. Any other value means that + * horizontal decimation is enabled, and the buffer geometry + * cannot be reconstructed from the registers. + */ + if (dispc_plane_get_pixel_inc(dispc, tplane->hw_plane_id) != 1) { + ret = -EINVAL; + goto err_free_fb; + } + + /* + * The row increment register holds the extra bytes between the + * end of a row of the picture and the beginning of the next one, + * which includes any padding the firmware might have programmed. + */ + fb->pitches[0] = fb->width * (drm_format_info_bpp(info, 0) / 8) + + dispc_plane_get_row_inc(dispc, tplane->hw_plane_id) - 1; + + /* + * TODO: The DMA base address has the offsets baked in, and there + * is no backing buffer to recover them from. + */ + fb->offsets[0] = 0; + + /* + * The firmware allocated the buffer, so the best we can do is + * to wrap the region the DMA engine fetches from, starting at + * its base address. + */ + dma_addr = dispc_plane_get_dma_addr(dispc, tplane->hw_plane_id); + size = (size_t)fb->pitches[0] * (fb->height - 1) + + drm_format_info_min_pitch(info, 0, fb->width); + dma_obj = drm_atomic_helper_sro_create_gem_dma_object(ddev, dma_addr, + size); + if (IS_ERR(dma_obj)) { + ret = PTR_ERR(dma_obj); + goto err_free_fb; + } + + fb->obj[0] = &dma_obj->base; + + ret = drm_framebuffer_init(plane->dev, fb, &tidss_plane_readout_fb_funcs); + if (ret) + goto err_put_obj; + + return fb; + +err_put_obj: + drm_gem_object_put(fb->obj[0]); +err_free_fb: + kfree(fb); + return ERR_PTR(ret); +} + +static struct drm_crtc *tidss_plane_readout_crtc(struct drm_plane *plane, + int *crtc_x, int *crtc_y, + u32 *normalized_zpos) +{ + struct drm_device *dev = plane->dev; + struct tidss_device *tidss = to_tidss(dev); + struct tidss_plane *tplane = to_tidss_plane(plane); + struct drm_crtc *crtc; + u32 hw_videoport, layer; + int ret; + + ret = dispc_ovr_get_plane(tidss->dispc, tplane->hw_plane_id, + &hw_videoport, crtc_x, crtc_y, &layer); + if (ret) + return ERR_PTR(ret); + + *normalized_zpos = layer; + + drm_for_each_crtc(crtc, dev) { + if (to_tidss_crtc(crtc)->hw_videoport == hw_videoport) + return crtc; + } + + return ERR_PTR(-ENODEV); +} + +static int tidss_plane_atomic_readout_state(struct drm_plane *plane, + struct drm_atomic_sro_state *state, + struct drm_plane_state *plane_state) +{ + struct drm_device *ddev = plane->dev; + struct tidss_device *tidss = to_tidss(ddev); + struct dispc_device *dispc = tidss->dispc; + struct tidss_plane *tplane = to_tidss_plane(plane); + struct drm_crtc_state *crtc_state; + struct drm_framebuffer *fb; + struct drm_crtc *crtc; + unsigned int in_w, in_h; + int crtc_x, crtc_y; + u32 normalized_zpos; + int ret; + + ret = tidss_runtime_get(tidss); + if (ret < 0) + return ret; + + if (!dispc_plane_is_enabled(dispc, tplane->hw_plane_id)) + goto out; + + crtc = tidss_plane_readout_crtc(plane, &crtc_x, &crtc_y, + &normalized_zpos); + if (IS_ERR(crtc)) { + ret = PTR_ERR(crtc); + + /* + * tidss_crtc_atomic_disable() disables the overlay layers of + * the video port, but the planes on a CRTC being disabled + * aren't disabled in the VID pipeline. A plane that isn't + * routed to any layer is thus disabled. + */ + if (ret == -ENODEV) + goto out; + + goto err_runtime_pm; + } + + crtc_state = drm_atomic_sro_get_crtc_state(state, crtc); + if (!crtc_state) { + ret = -ENODEV; + goto err_runtime_pm; + } + + /* + * K2G has no overlay manager to tell us whether the plane is + * routed, and the planes of a disabled video port are left + * enabled: a plane on an inactive CRTC isn't scanned out. + */ + if (!crtc_state->active) + goto out; + + fb = tidss_plane_readout_fb(plane); + if (IS_ERR(fb)) { + ret = PTR_ERR(fb); + goto err_runtime_pm; + } + + plane_state->fb = fb; + plane_state->crtc = crtc; + plane_state->visible = true; + + dispc_plane_get_picture_size(dispc, tplane->hw_plane_id, &in_w, &in_h); + plane_state->src_w = in_w << 16; + plane_state->src_h = in_h << 16; + + dispc_plane_get_size(dispc, tplane->hw_plane_id, &plane_state->crtc_w, + &plane_state->crtc_h); + + plane_state->crtc_x = crtc_x; + plane_state->crtc_y = crtc_y; + + /* + * The overlay layer index a plane is routed to is its + * normalized zpos, as that is what the atomic update uses as + * the layer index. The raw zpos property userspace set + * cannot be recovered from the hardware. + */ + plane_state->normalized_zpos = normalized_zpos; + plane_state->zpos = normalized_zpos; + + // TODO: Handle src_x/src_y, see DISPC_VID_BA_0 + + plane_state->src.x1 = 0; + plane_state->src.x2 = plane_state->src_w; + plane_state->src.y1 = 0; + plane_state->src.y2 = plane_state->src_h; + plane_state->dst.x1 = crtc_x; + plane_state->dst.x2 = crtc_x + plane_state->crtc_w; + plane_state->dst.y1 = crtc_y; + plane_state->dst.y2 = crtc_y + plane_state->crtc_h; + + /* + * The global alpha register is 8 bits wide and is programmed + * from the high byte of the 16 bits alpha property. + * Replicating the byte is the only inverse that preserves the + * fraction of full opacity (0x00 -> 0x0000, 0xff -> 0xffff) + * and round-trips through a program-then-readout cycle. + */ + plane_state->alpha = + dispc_plane_get_global_alpha(dispc, tplane->hw_plane_id) * 0x0101; + plane_state->pixel_blend_mode = + dispc_plane_get_blend_mode(dispc, tplane->hw_plane_id); + + crtc_state->plane_mask |= drm_plane_mask(plane); + +out: + tidss_runtime_put(tidss); + return 0; + +err_runtime_pm: + tidss_runtime_put(tidss); + return ret; +} + static const struct drm_plane_funcs tidss_plane_funcs = { .update_plane = drm_atomic_helper_update_plane, .disable_plane = drm_atomic_helper_disable_plane, .destroy = drm_plane_destroy, .atomic_create_state = drm_atomic_helper_plane_create_state, + .atomic_sro_readout_state = tidss_plane_atomic_readout_state, + .atomic_sro_compare_state = drm_atomic_helper_plane_compare_state, .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, DRM_PANIC_PLANE_FUNCS, }; -- 2.56.1
