diff --git a/drivers/gpu/drm/vc4/vc4_crtc.c b/drivers/gpu/drm/vc4/vc4_crtc.c index 6bb52539a5a66b..6ac60e18701542 100644 --- a/drivers/gpu/drm/vc4/vc4_crtc.c +++ b/drivers/gpu/drm/vc4/vc4_crtc.c @@ -781,6 +781,7 @@ vc4_async_page_flip_complete(struct vc4_seqno_cb *cb) container_of(cb, struct vc4_async_flip_state, cb); struct drm_crtc *crtc = flip_state->crtc; struct drm_device *dev = crtc->dev; + struct vc4_dev *vc4 = to_vc4_dev(dev); struct drm_plane *plane = crtc->primary; vc4_plane_async_set_fb(plane, flip_state->fb); @@ -812,6 +813,8 @@ vc4_async_page_flip_complete(struct vc4_seqno_cb *cb) } kfree(flip_state); + + up(&vc4->async_modeset); } /* Implements async (non-vblank-synced) page flips. @@ -826,6 +829,7 @@ static int vc4_async_page_flip(struct drm_crtc *crtc, uint32_t flags) { struct drm_device *dev = crtc->dev; + struct vc4_dev *vc4 = to_vc4_dev(dev); struct drm_plane *plane = crtc->primary; int ret = 0; struct vc4_async_flip_state *flip_state; @@ -854,6 +858,15 @@ static int vc4_async_page_flip(struct drm_crtc *crtc, flip_state->crtc = crtc; flip_state->event = event; + /* Make sure all other async modesetes have landed. */ + ret = down_interruptible(&vc4->async_modeset); + if (ret) { + drm_framebuffer_put(fb); + vc4_bo_dec_usecnt(bo); + kfree(flip_state); + return ret; + } + /* Save the current FB before it's replaced by the new one in * drm_atomic_set_fb_for_plane(). We'll need the old FB in * vc4_async_page_flip_complete() to decrement the BO usecnt and keep diff --git a/drivers/gpu/drm/vc4/vc4_drv.h b/drivers/gpu/drm/vc4/vc4_drv.h index 7387244af483f2..7c749e08cb0c44 100644 --- a/drivers/gpu/drm/vc4/vc4_drv.h +++ b/drivers/gpu/drm/vc4/vc4_drv.h @@ -216,6 +216,8 @@ struct vc4_dev { struct work_struct reset_work; } hangcheck; + struct semaphore async_modeset; + struct drm_modeset_lock ctm_state_lock; struct drm_private_obj ctm_manager; struct drm_private_obj hvs_channels; diff --git a/drivers/gpu/drm/vc4/vc4_kms.c b/drivers/gpu/drm/vc4/vc4_kms.c index a2a03b52b27514..fb2465964d4631 100644 --- a/drivers/gpu/drm/vc4/vc4_kms.c +++ b/drivers/gpu/drm/vc4/vc4_kms.c @@ -39,13 +39,10 @@ static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv) struct vc4_hvs_state { struct drm_private_state base; + unsigned int unassigned_channels; + unsigned int num_outputs; + unsigned long fifo_load; unsigned long core_clock_rate; - - struct { - unsigned in_use: 1; - unsigned long fifo_load; - struct drm_crtc_commit *pending_commit; - } fifo_state[HVS_NUM_CHANNELS]; }; static struct vc4_hvs_state * @@ -204,19 +201,6 @@ vc4_hvs_get_new_global_state(struct drm_atomic_state *state) return to_vc4_hvs_state(priv_state); } -static struct vc4_hvs_state * -vc4_hvs_get_old_global_state(struct drm_atomic_state *state) -{ - struct vc4_dev *vc4 = to_vc4_dev(state->dev); - struct drm_private_state *priv_state; - - priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels); - if (IS_ERR(priv_state)) - return ERR_CAST(priv_state); - - return to_vc4_hvs_state(priv_state); -} - static struct vc4_hvs_state * vc4_hvs_get_global_state(struct drm_atomic_state *state) { @@ -337,25 +321,20 @@ static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4, } } -static void vc4_atomic_commit_tail(struct drm_atomic_state *state) +static void +vc4_atomic_complete_commit(struct drm_atomic_state *state) { struct drm_device *dev = state->dev; struct vc4_dev *vc4 = to_vc4_dev(dev); struct vc4_hvs *hvs = vc4->hvs; - struct drm_crtc_state *old_crtc_state; struct drm_crtc_state *new_crtc_state; - struct vc4_hvs_state *new_hvs_state; + struct vc4_hvs_state *hvs_state; struct drm_crtc *crtc; - struct vc4_hvs_state *old_hvs_state; struct clk_request *core_req; int i; - old_hvs_state = vc4_hvs_get_old_global_state(state); - if (WARN_ON(!old_hvs_state)) - return; - - new_hvs_state = vc4_hvs_get_new_global_state(state); - if (WARN_ON(!new_hvs_state)) + hvs_state = vc4_hvs_get_new_global_state(state); + if (WARN_ON(!hvs_state)) return; for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { @@ -371,7 +350,7 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state) if (vc4->hvs && vc4->hvs->hvs5) { unsigned long core_rate = max_t(unsigned long, 500000000, - new_hvs_state->core_clock_rate); + hvs_state->core_clock_rate); drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate); @@ -388,31 +367,9 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state) clk_request_done(hvs->core_req); } - for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) { - struct vc4_crtc_state *vc4_crtc_state = - to_vc4_crtc_state(old_crtc_state); - struct drm_crtc_commit *commit; - unsigned int channel = vc4_crtc_state->assigned_channel; - unsigned long done; - - if (channel == VC4_HVS_CHANNEL_DISABLED) - continue; + drm_atomic_helper_wait_for_fences(dev, state, false); - if (!old_hvs_state->fifo_state[channel].in_use) - continue; - - commit = old_hvs_state->fifo_state[i].pending_commit; - if (!commit) - continue; - - done = wait_for_completion_timeout(&commit->hw_done, 10 * HZ); - if (!done) - drm_err(dev, "Timed out waiting for hw_done\n"); - - done = wait_for_completion_timeout(&commit->flip_done, 10 * HZ); - if (!done) - drm_err(dev, "Timed out waiting for flip_done\n"); - } + drm_atomic_helper_wait_for_dependencies(state); drm_atomic_helper_commit_modeset_disables(dev, state); @@ -437,30 +394,75 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state) drm_atomic_helper_cleanup_planes(dev, state); + drm_atomic_helper_commit_cleanup_done(state); + if (vc4->hvs && vc4->hvs->hvs5) { drm_dbg(dev, "Running the core clock at %lu Hz\n", - new_hvs_state->core_clock_rate); + hvs_state->core_clock_rate); /* * Request a clock rate based on the current HVS * requirements. */ hvs->core_req = clk_request_start(hvs->core_clk, - new_hvs_state->core_clock_rate); + hvs_state->core_clock_rate); /* And drop the temporary request */ clk_request_done(core_req); } + + drm_atomic_state_put(state); + + up(&vc4->async_modeset); } -static int vc4_atomic_commit_setup(struct drm_atomic_state *state) +static void commit_work(struct work_struct *work) +{ + struct drm_atomic_state *state = container_of(work, + struct drm_atomic_state, + commit_work); + vc4_atomic_complete_commit(state); +} + +/** + * vc4_atomic_commit - commit validated state object + * @dev: DRM device + * @state: the driver state object + * @nonblock: nonblocking commit + * + * This function commits a with drm_atomic_helper_check() pre-validated state + * object. This can still fail when e.g. the framebuffer reservation fails. For + * now this doesn't implement asynchronous commits. + * + * RETURNS + * Zero for success or -errno. + */ +static int vc4_atomic_commit(struct drm_device *dev, + struct drm_atomic_state *state, + bool nonblock) { - struct drm_device *dev = state->dev; struct vc4_dev *vc4 = to_vc4_dev(dev); - struct drm_crtc_state *crtc_state; - struct vc4_hvs_state *hvs_state; - struct drm_crtc *crtc; - unsigned int i; + int ret; + + if (state->async_update) { + ret = down_interruptible(&vc4->async_modeset); + if (ret) + return ret; + + ret = drm_atomic_helper_prepare_planes(dev, state); + if (ret) { + up(&vc4->async_modeset); + return ret; + } + + drm_atomic_helper_async_commit(dev, state); + + drm_atomic_helper_cleanup_planes(dev, state); + + up(&vc4->async_modeset); + + return 0; + } /* We know for sure we don't want an async update here. Set * state->legacy_cursor_update to false to prevent @@ -469,27 +471,61 @@ static int vc4_atomic_commit_setup(struct drm_atomic_state *state) */ if (!vc4->firmware_kms) state->legacy_cursor_update = false; + ret = drm_atomic_helper_setup_commit(state, nonblock); + if (ret) + return ret; - hvs_state = vc4_hvs_get_new_global_state(state); - if (!hvs_state) - return -EINVAL; - - for_each_new_crtc_in_state(state, crtc, crtc_state, i) { - struct vc4_crtc_state *vc4_crtc_state = - to_vc4_crtc_state(crtc_state); - unsigned int channel = - vc4_crtc_state->assigned_channel; + INIT_WORK(&state->commit_work, commit_work); - if (channel == VC4_HVS_CHANNEL_DISABLED) - continue; + ret = down_interruptible(&vc4->async_modeset); + if (ret) + return ret; - if (!hvs_state->fifo_state[channel].in_use) - continue; + ret = drm_atomic_helper_prepare_planes(dev, state); + if (ret) { + up(&vc4->async_modeset); + return ret; + } - hvs_state->fifo_state[channel].pending_commit = - drm_crtc_commit_get(crtc_state->commit); + if (!nonblock) { + ret = drm_atomic_helper_wait_for_fences(dev, state, true); + if (ret) { + drm_atomic_helper_cleanup_planes(dev, state); + up(&vc4->async_modeset); + return ret; + } } + /* + * This is the point of no return - everything below never fails except + * when the hw goes bonghits. Which means we can commit the new state on + * the software side now. + */ + + BUG_ON(drm_atomic_helper_swap_state(state, false) < 0); + + /* + * Everything below can be run asynchronously without the need to grab + * any modeset locks at all under one condition: It must be guaranteed + * that the asynchronous work has either been cancelled (if the driver + * supports it, which at least requires that the framebuffers get + * cleaned up with drm_atomic_helper_cleanup_planes()) or completed + * before the new state gets committed on the software side with + * drm_atomic_helper_swap_state(). + * + * This scheme allows new atomic state updates to be prepared and + * checked in parallel to the asynchronous completion of the previous + * update. Which is important since compositors need to figure out the + * composition of the next frame right after having submitted the + * current layout. + */ + + drm_atomic_state_get(state); + if (nonblock) + queue_work(system_unbound_wq, &state->commit_work); + else + vc4_atomic_complete_commit(state); + return 0; } @@ -709,7 +745,6 @@ vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) { struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state); struct vc4_hvs_state *state; - unsigned int i; state = kzalloc(sizeof(*state), GFP_KERNEL); if (!state) @@ -717,20 +752,11 @@ vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); - for (i = 0; i < HVS_NUM_CHANNELS; i++) { - state->fifo_state[i].in_use = old_state->fifo_state[i].in_use; - state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load; - - if (!old_state->fifo_state[i].pending_commit) - continue; - - state->fifo_state[i].pending_commit = - drm_crtc_commit_get(old_state->fifo_state[i].pending_commit); - } - + state->unassigned_channels = old_state->unassigned_channels; + state->fifo_load = old_state->fifo_load; + state->num_outputs = old_state->num_outputs; state->core_clock_rate = old_state->core_clock_rate; - return &state->base; } @@ -738,14 +764,6 @@ static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj, struct drm_private_state *state) { struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); - unsigned int i; - - for (i = 0; i < HVS_NUM_CHANNELS; i++) { - if (!hvs_state->fifo_state[i].pending_commit) - continue; - - drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit); - } kfree(hvs_state); } @@ -770,6 +788,7 @@ static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4) if (!state) return -ENOMEM; + state->unassigned_channels = GENMASK(HVS_NUM_CHANNELS - 1, 0); drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels, &state->base, &vc4_hvs_state_funcs); @@ -814,17 +833,12 @@ static int vc4_pv_muxing_atomic_check(struct drm_device *dev, struct vc4_hvs_state *hvs_new_state; struct drm_crtc_state *old_crtc_state, *new_crtc_state; struct drm_crtc *crtc; - unsigned int unassigned_channels = 0; unsigned int i; hvs_new_state = vc4_hvs_get_global_state(state); if (!hvs_new_state) return -EINVAL; - for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++) - if (!hvs_new_state->fifo_state[i].in_use) - unassigned_channels |= BIT(i); - for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { struct vc4_crtc_state *old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state); @@ -846,8 +860,7 @@ static int vc4_pv_muxing_atomic_check(struct drm_device *dev, /* If we're disabling our CRTC, we put back our channel */ if (!new_crtc_state->enable) { - channel = old_vc4_crtc_state->assigned_channel; - hvs_new_state->fifo_state[channel].in_use = false; + hvs_new_state->unassigned_channels |= BIT(old_vc4_crtc_state->assigned_channel); new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; continue; } @@ -876,14 +889,13 @@ static int vc4_pv_muxing_atomic_check(struct drm_device *dev, * the future, we will need to have something smarter, * but it works so far. */ - matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels; + matching_channels = hvs_new_state->unassigned_channels & vc4_crtc->data->hvs_available_channels; if (!matching_channels) return -EINVAL; channel = ffs(matching_channels) - 1; new_vc4_crtc_state->assigned_channel = channel; - unassigned_channels &= ~BIT(channel); - hvs_new_state->fifo_state[channel].in_use = true; + hvs_new_state->unassigned_channels &= ~BIT(channel); } return 0; @@ -898,7 +910,6 @@ vc4_core_clock_atomic_check(struct drm_atomic_state *state) struct vc4_load_tracker_state *load_state; struct drm_crtc_state *old_crtc_state, *new_crtc_state; struct drm_crtc *crtc; - unsigned int num_outputs; unsigned long pixel_rate; unsigned long cob_rate; unsigned int i; @@ -921,33 +932,23 @@ vc4_core_clock_atomic_check(struct drm_atomic_state *state) if (old_crtc_state->active) { struct vc4_crtc_state *old_vc4_state = to_vc4_crtc_state(old_crtc_state); - unsigned int channel = old_vc4_state->assigned_channel; - hvs_new_state->fifo_state[channel].fifo_load = 0; + hvs_new_state->num_outputs -= 1; + hvs_new_state->fifo_load -= old_vc4_state->hvs_load; } if (new_crtc_state->active) { struct vc4_crtc_state *new_vc4_state = to_vc4_crtc_state(new_crtc_state); - unsigned int channel = new_vc4_state->assigned_channel; - hvs_new_state->fifo_state[channel].fifo_load = - new_vc4_state->hvs_load; + hvs_new_state->num_outputs += 1; + hvs_new_state->fifo_load += new_vc4_state->hvs_load; } } - cob_rate = 0; - num_outputs = 0; - for (i = 0; i < HVS_NUM_CHANNELS; i++) { - if (!hvs_new_state->fifo_state[i].in_use) - continue; - - num_outputs++; - cob_rate += hvs_new_state->fifo_state[i].fifo_load; - } - + cob_rate = hvs_new_state->fifo_load; pixel_rate = load_state->hvs_load; - if (num_outputs > 1) { + if (hvs_new_state->num_outputs > 1) { pixel_rate = (pixel_rate * 40) / 100; } else { pixel_rate = (pixel_rate * 60) / 100; @@ -983,14 +984,9 @@ vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) return vc4_core_clock_atomic_check(state); } -static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = { - .atomic_commit_setup = vc4_atomic_commit_setup, - .atomic_commit_tail = vc4_atomic_commit_tail, -}; - static const struct drm_mode_config_funcs vc4_mode_funcs = { .atomic_check = vc4_atomic_check, - .atomic_commit = drm_atomic_helper_commit, + .atomic_commit = vc4_atomic_commit, .fb_create = vc4_fb_create, }; @@ -1013,6 +1009,8 @@ int vc4_kms_load(struct drm_device *dev) vc4->load_tracker_enabled = true; } + sema_init(&vc4->async_modeset, 1); + /* Set support for vblank irq fast disable, before drm_vblank_init() */ dev->vblank_disable_immediate = true; @@ -1032,7 +1030,6 @@ int vc4_kms_load(struct drm_device *dev) } dev->mode_config.funcs = &vc4_mode_funcs; - dev->mode_config.helper_private = &vc4_mode_config_helpers; dev->mode_config.preferred_depth = 24; dev->mode_config.async_page_flip = true; dev->mode_config.allow_fb_modifiers = true;