Commit 0ef1905e authored by Ville Syrjälä's avatar Ville Syrjälä

drm/i915: Introduce better global state handling

Our current global state handling is pretty ad-hoc. Let's try to
make it better by imitating the standard drm core private object
approach.

The reason why we don't want to directly use the private objects
is locking; Each private object has its own lock so if we
introduce any global private objects we get serialized by that
single lock across all pipes. The global state apporoach instead
uses a read/write lock type of approach where each individual
crtc lock counts as a read lock, and grabbing all the crtc locks
allows one write access.
Signed-off-by: default avatarVille Syrjälä <ville.syrjala@linux.intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20200120174728.21095-15-ville.syrjala@linux.intel.comReviewed-by: default avatarImre Deak <imre.deak@intel.com>
parent 5f342993
...@@ -190,6 +190,7 @@ i915-y += \ ...@@ -190,6 +190,7 @@ i915-y += \
display/intel_fbc.o \ display/intel_fbc.o \
display/intel_fifo_underrun.o \ display/intel_fifo_underrun.o \
display/intel_frontbuffer.o \ display/intel_frontbuffer.o \
display/intel_global_state.o \
display/intel_hdcp.o \ display/intel_hdcp.o \
display/intel_hotplug.o \ display/intel_hotplug.o \
display/intel_lpe_audio.o \ display/intel_lpe_audio.o \
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "intel_atomic.h" #include "intel_atomic.h"
#include "intel_cdclk.h" #include "intel_cdclk.h"
#include "intel_display_types.h" #include "intel_display_types.h"
#include "intel_global_state.h"
#include "intel_hdcp.h" #include "intel_hdcp.h"
#include "intel_psr.h" #include "intel_psr.h"
#include "intel_sprite.h" #include "intel_sprite.h"
...@@ -502,6 +503,7 @@ void intel_atomic_state_free(struct drm_atomic_state *_state) ...@@ -502,6 +503,7 @@ void intel_atomic_state_free(struct drm_atomic_state *_state)
struct intel_atomic_state *state = to_intel_atomic_state(_state); struct intel_atomic_state *state = to_intel_atomic_state(_state);
drm_atomic_state_default_release(&state->base); drm_atomic_state_default_release(&state->base);
kfree(state->global_objs);
i915_sw_fence_fini(&state->commit_ready); i915_sw_fence_fini(&state->commit_ready);
...@@ -513,6 +515,7 @@ void intel_atomic_state_clear(struct drm_atomic_state *s) ...@@ -513,6 +515,7 @@ void intel_atomic_state_clear(struct drm_atomic_state *s)
struct intel_atomic_state *state = to_intel_atomic_state(s); struct intel_atomic_state *state = to_intel_atomic_state(s);
drm_atomic_state_default_clear(&state->base); drm_atomic_state_default_clear(&state->base);
intel_atomic_clear_global_state(state);
state->dpll_set = state->modeset = false; state->dpll_set = state->modeset = false;
state->global_state_changed = false; state->global_state_changed = false;
...@@ -532,7 +535,7 @@ intel_atomic_get_crtc_state(struct drm_atomic_state *state, ...@@ -532,7 +535,7 @@ intel_atomic_get_crtc_state(struct drm_atomic_state *state,
return to_intel_crtc_state(crtc_state); return to_intel_crtc_state(crtc_state);
} }
int intel_atomic_lock_global_state(struct intel_atomic_state *state) int _intel_atomic_lock_global_state(struct intel_atomic_state *state)
{ {
struct drm_i915_private *dev_priv = to_i915(state->base.dev); struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_crtc *crtc; struct intel_crtc *crtc;
...@@ -551,7 +554,7 @@ int intel_atomic_lock_global_state(struct intel_atomic_state *state) ...@@ -551,7 +554,7 @@ int intel_atomic_lock_global_state(struct intel_atomic_state *state)
return 0; return 0;
} }
int intel_atomic_serialize_global_state(struct intel_atomic_state *state) int _intel_atomic_serialize_global_state(struct intel_atomic_state *state)
{ {
struct drm_i915_private *dev_priv = to_i915(state->base.dev); struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_crtc *crtc; struct intel_crtc *crtc;
......
...@@ -56,8 +56,8 @@ int intel_atomic_setup_scalers(struct drm_i915_private *dev_priv, ...@@ -56,8 +56,8 @@ int intel_atomic_setup_scalers(struct drm_i915_private *dev_priv,
struct intel_crtc *intel_crtc, struct intel_crtc *intel_crtc,
struct intel_crtc_state *crtc_state); struct intel_crtc_state *crtc_state);
int intel_atomic_lock_global_state(struct intel_atomic_state *state); int _intel_atomic_lock_global_state(struct intel_atomic_state *state);
int intel_atomic_serialize_global_state(struct intel_atomic_state *state); int _intel_atomic_serialize_global_state(struct intel_atomic_state *state);
#endif /* __INTEL_ATOMIC_H__ */ #endif /* __INTEL_ATOMIC_H__ */
...@@ -829,7 +829,7 @@ static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv, ...@@ -829,7 +829,7 @@ static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
enable ? 2 * 96000 : 0; enable ? 2 * 96000 : 0;
/* Protects dev_priv->cdclk.force_min_cdclk */ /* Protects dev_priv->cdclk.force_min_cdclk */
ret = intel_atomic_lock_global_state(to_intel_atomic_state(state)); ret = _intel_atomic_lock_global_state(to_intel_atomic_state(state));
if (!ret) if (!ret)
ret = drm_atomic_commit(state); ret = drm_atomic_commit(state);
......
...@@ -2080,7 +2080,7 @@ static int intel_compute_min_cdclk(struct intel_atomic_state *state) ...@@ -2080,7 +2080,7 @@ static int intel_compute_min_cdclk(struct intel_atomic_state *state)
cdclk_state->min_cdclk[i] = min_cdclk; cdclk_state->min_cdclk[i] = min_cdclk;
ret = intel_atomic_lock_global_state(state); ret = _intel_atomic_lock_global_state(state);
if (ret) if (ret)
return ret; return ret;
} }
...@@ -2128,7 +2128,7 @@ static int bxt_compute_min_voltage_level(struct intel_atomic_state *state) ...@@ -2128,7 +2128,7 @@ static int bxt_compute_min_voltage_level(struct intel_atomic_state *state)
cdclk_state->min_voltage_level[i] = min_voltage_level; cdclk_state->min_voltage_level[i] = min_voltage_level;
ret = intel_atomic_lock_global_state(state); ret = _intel_atomic_lock_global_state(state);
if (ret) if (ret)
return ret; return ret;
} }
...@@ -2403,12 +2403,12 @@ int intel_modeset_calc_cdclk(struct intel_atomic_state *state) ...@@ -2403,12 +2403,12 @@ int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
* Also serialize commits across all crtcs * Also serialize commits across all crtcs
* if the actual hw needs to be poked. * if the actual hw needs to be poked.
*/ */
ret = intel_atomic_serialize_global_state(state); ret = _intel_atomic_serialize_global_state(state);
if (ret) if (ret)
return ret; return ret;
} else if (intel_cdclk_changed(&old_cdclk_state->logical, } else if (intel_cdclk_changed(&old_cdclk_state->logical,
&new_cdclk_state->logical)) { &new_cdclk_state->logical)) {
ret = intel_atomic_lock_global_state(state); ret = _intel_atomic_lock_global_state(state);
if (ret) if (ret)
return ret; return ret;
} else { } else {
......
...@@ -14562,7 +14562,7 @@ static int intel_modeset_checks(struct intel_atomic_state *state) ...@@ -14562,7 +14562,7 @@ static int intel_modeset_checks(struct intel_atomic_state *state)
} }
if (state->active_pipe_changes) { if (state->active_pipe_changes) {
ret = intel_atomic_lock_global_state(state); ret = _intel_atomic_lock_global_state(state);
if (ret) if (ret)
return ret; return ret;
} }
...@@ -15842,6 +15842,8 @@ static int intel_atomic_commit(struct drm_device *dev, ...@@ -15842,6 +15842,8 @@ static int intel_atomic_commit(struct drm_device *dev,
ret = drm_atomic_helper_setup_commit(&state->base, nonblock); ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
if (!ret) if (!ret)
ret = drm_atomic_helper_swap_state(&state->base, true); ret = drm_atomic_helper_swap_state(&state->base, true);
if (!ret)
intel_atomic_swap_global_state(state);
if (ret) { if (ret) {
i915_sw_fence_commit(&state->commit_ready); i915_sw_fence_commit(&state->commit_ready);
...@@ -17759,6 +17761,7 @@ static void intel_mode_config_init(struct drm_i915_private *i915) ...@@ -17759,6 +17761,7 @@ static void intel_mode_config_init(struct drm_i915_private *i915)
struct drm_mode_config *mode_config = &i915->drm.mode_config; struct drm_mode_config *mode_config = &i915->drm.mode_config;
drm_mode_config_init(&i915->drm); drm_mode_config_init(&i915->drm);
INIT_LIST_HEAD(&i915->global_obj_list);
mode_config->min_width = 0; mode_config->min_width = 0;
mode_config->min_height = 0; mode_config->min_height = 0;
...@@ -17800,6 +17803,12 @@ static void intel_mode_config_init(struct drm_i915_private *i915) ...@@ -17800,6 +17803,12 @@ static void intel_mode_config_init(struct drm_i915_private *i915)
} }
} }
static void intel_mode_config_cleanup(struct drm_i915_private *i915)
{
intel_atomic_global_obj_cleanup(i915);
drm_mode_config_cleanup(&i915->drm);
}
int intel_modeset_init(struct drm_i915_private *i915) int intel_modeset_init(struct drm_i915_private *i915)
{ {
struct drm_device *dev = &i915->drm; struct drm_device *dev = &i915->drm;
...@@ -17839,7 +17848,7 @@ int intel_modeset_init(struct drm_i915_private *i915) ...@@ -17839,7 +17848,7 @@ int intel_modeset_init(struct drm_i915_private *i915)
for_each_pipe(i915, pipe) { for_each_pipe(i915, pipe) {
ret = intel_crtc_init(i915, pipe); ret = intel_crtc_init(i915, pipe);
if (ret) { if (ret) {
drm_mode_config_cleanup(dev); intel_mode_config_cleanup(i915);
return ret; return ret;
} }
} }
...@@ -18807,7 +18816,7 @@ void intel_modeset_driver_remove(struct drm_i915_private *i915) ...@@ -18807,7 +18816,7 @@ void intel_modeset_driver_remove(struct drm_i915_private *i915)
intel_hdcp_component_fini(i915); intel_hdcp_component_fini(i915);
drm_mode_config_cleanup(&i915->drm); intel_mode_config_cleanup(i915);
intel_overlay_cleanup(i915); intel_overlay_cleanup(i915);
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include "intel_de.h" #include "intel_de.h"
struct drm_printer; struct drm_printer;
struct __intel_global_objs_state;
/* /*
* Display related stuff * Display related stuff
...@@ -462,6 +463,9 @@ struct intel_atomic_state { ...@@ -462,6 +463,9 @@ struct intel_atomic_state {
intel_wakeref_t wakeref; intel_wakeref_t wakeref;
struct __intel_global_objs_state *global_objs;
int num_global_objs;
struct intel_cdclk_state cdclk_state; struct intel_cdclk_state cdclk_state;
bool dpll_set, modeset; bool dpll_set, modeset;
......
// SPDX-License-Identifier: MIT
/*
* Copyright © 2020 Intel Corporation
*/
#include <linux/string.h>
#include "i915_drv.h"
#include "intel_atomic.h"
#include "intel_display_types.h"
#include "intel_global_state.h"
void intel_atomic_global_obj_init(struct drm_i915_private *dev_priv,
struct intel_global_obj *obj,
struct intel_global_state *state,
const struct intel_global_state_funcs *funcs)
{
memset(obj, 0, sizeof(*obj));
obj->state = state;
obj->funcs = funcs;
list_add_tail(&obj->head, &dev_priv->global_obj_list);
}
void intel_atomic_global_obj_cleanup(struct drm_i915_private *dev_priv)
{
struct intel_global_obj *obj, *next;
list_for_each_entry_safe(obj, next, &dev_priv->global_obj_list, head) {
list_del(&obj->head);
obj->funcs->atomic_destroy_state(obj, obj->state);
}
}
static void assert_global_state_write_locked(struct drm_i915_private *dev_priv)
{
struct intel_crtc *crtc;
for_each_intel_crtc(&dev_priv->drm, crtc)
drm_modeset_lock_assert_held(&crtc->base.mutex);
}
static bool modeset_lock_is_held(struct drm_modeset_acquire_ctx *ctx,
struct drm_modeset_lock *lock)
{
struct drm_modeset_lock *l;
list_for_each_entry(l, &ctx->locked, head) {
if (lock == l)
return true;
}
return false;
}
static void assert_global_state_read_locked(struct intel_atomic_state *state)
{
struct drm_modeset_acquire_ctx *ctx = state->base.acquire_ctx;
struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_crtc *crtc;
for_each_intel_crtc(&dev_priv->drm, crtc) {
if (modeset_lock_is_held(ctx, &crtc->base.mutex))
return;
}
WARN(1, "Global state not read locked\n");
}
struct intel_global_state *
intel_atomic_get_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj)
{
int index, num_objs, i;
size_t size;
struct __intel_global_objs_state *arr;
struct intel_global_state *obj_state;
for (i = 0; i < state->num_global_objs; i++)
if (obj == state->global_objs[i].ptr)
return state->global_objs[i].state;
assert_global_state_read_locked(state);
num_objs = state->num_global_objs + 1;
size = sizeof(*state->global_objs) * num_objs;
arr = krealloc(state->global_objs, size, GFP_KERNEL);
if (!arr)
return ERR_PTR(-ENOMEM);
state->global_objs = arr;
index = state->num_global_objs;
memset(&state->global_objs[index], 0, sizeof(*state->global_objs));
obj_state = obj->funcs->atomic_duplicate_state(obj);
if (!obj_state)
return ERR_PTR(-ENOMEM);
obj_state->changed = false;
state->global_objs[index].state = obj_state;
state->global_objs[index].old_state = obj->state;
state->global_objs[index].new_state = obj_state;
state->global_objs[index].ptr = obj;
obj_state->state = state;
state->num_global_objs = num_objs;
DRM_DEBUG_ATOMIC("Added new global object %p state %p to %p\n",
obj, obj_state, state);
return obj_state;
}
struct intel_global_state *
intel_atomic_get_old_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj)
{
int i;
for (i = 0; i < state->num_global_objs; i++)
if (obj == state->global_objs[i].ptr)
return state->global_objs[i].old_state;
return NULL;
}
struct intel_global_state *
intel_atomic_get_new_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj)
{
int i;
for (i = 0; i < state->num_global_objs; i++)
if (obj == state->global_objs[i].ptr)
return state->global_objs[i].new_state;
return NULL;
}
void intel_atomic_swap_global_state(struct intel_atomic_state *state)
{
struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_global_state *old_obj_state, *new_obj_state;
struct intel_global_obj *obj;
int i;
for_each_oldnew_global_obj_in_state(state, obj, old_obj_state,
new_obj_state, i) {
WARN_ON(obj->state != old_obj_state);
/*
* If the new state wasn't modified (and properly
* locked for write access) we throw it away.
*/
if (!new_obj_state->changed)
continue;
assert_global_state_write_locked(dev_priv);
old_obj_state->state = state;
new_obj_state->state = NULL;
state->global_objs[i].state = old_obj_state;
obj->state = new_obj_state;
}
}
void intel_atomic_clear_global_state(struct intel_atomic_state *state)
{
int i;
for (i = 0; i < state->num_global_objs; i++) {
struct intel_global_obj *obj = state->global_objs[i].ptr;
obj->funcs->atomic_destroy_state(obj,
state->global_objs[i].state);
state->global_objs[i].ptr = NULL;
state->global_objs[i].state = NULL;
state->global_objs[i].old_state = NULL;
state->global_objs[i].new_state = NULL;
}
state->num_global_objs = 0;
}
int intel_atomic_lock_global_state(struct intel_global_state *obj_state)
{
struct intel_atomic_state *state = obj_state->state;
struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_crtc *crtc;
for_each_intel_crtc(&dev_priv->drm, crtc) {
int ret;
ret = drm_modeset_lock(&crtc->base.mutex,
state->base.acquire_ctx);
if (ret)
return ret;
}
obj_state->changed = true;
return 0;
}
int intel_atomic_serialize_global_state(struct intel_global_state *obj_state)
{
struct intel_atomic_state *state = obj_state->state;
struct drm_i915_private *dev_priv = to_i915(state->base.dev);
struct intel_crtc *crtc;
for_each_intel_crtc(&dev_priv->drm, crtc) {
struct intel_crtc_state *crtc_state;
crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
if (IS_ERR(crtc_state))
return PTR_ERR(crtc_state);
}
obj_state->changed = true;
return 0;
}
/* SPDX-License-Identifier: MIT */
/*
* Copyright © 2020 Intel Corporation
*/
#ifndef __INTEL_GLOBAL_STATE_H__
#define __INTEL_GLOBAL_STATE_H__
#include <linux/list.h>
struct drm_i915_private;
struct intel_atomic_state;
struct intel_global_obj;
struct intel_global_state;
struct intel_global_state_funcs {
struct intel_global_state *(*atomic_duplicate_state)(struct intel_global_obj *obj);
void (*atomic_destroy_state)(struct intel_global_obj *obj,
struct intel_global_state *state);
};
struct intel_global_obj {
struct list_head head;
struct intel_global_state *state;
const struct intel_global_state_funcs *funcs;
};
#define intel_for_each_global_obj(obj, dev_priv) \
list_for_each_entry(obj, &(dev_priv)->global_obj_list, head)
#define for_each_new_global_obj_in_state(__state, obj, new_obj_state, __i) \
for ((__i) = 0; \
(__i) < (__state)->num_global_objs && \
((obj) = (__state)->global_objs[__i].ptr, \
(new_obj_state) = (__state)->global_objs[__i].new_state, 1); \
(__i)++) \
for_each_if(obj)
#define for_each_old_global_obj_in_state(__state, obj, new_obj_state, __i) \
for ((__i) = 0; \
(__i) < (__state)->num_global_objs && \
((obj) = (__state)->global_objs[__i].ptr, \
(new_obj_state) = (__state)->global_objs[__i].old_state, 1); \
(__i)++) \
for_each_if(obj)
#define for_each_oldnew_global_obj_in_state(__state, obj, old_obj_state, new_obj_state, __i) \
for ((__i) = 0; \
(__i) < (__state)->num_global_objs && \
((obj) = (__state)->global_objs[__i].ptr, \
(old_obj_state) = (__state)->global_objs[__i].old_state, \
(new_obj_state) = (__state)->global_objs[__i].new_state, 1); \
(__i)++) \
for_each_if(obj)
struct intel_global_state {
struct intel_atomic_state *state;
bool changed;
};
struct __intel_global_objs_state {
struct intel_global_obj *ptr;
struct intel_global_state *state, *old_state, *new_state;
};
void intel_atomic_global_obj_init(struct drm_i915_private *dev_priv,
struct intel_global_obj *obj,
struct intel_global_state *state,
const struct intel_global_state_funcs *funcs);
void intel_atomic_global_obj_cleanup(struct drm_i915_private *dev_priv);
struct intel_global_state *
intel_atomic_get_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj);
struct intel_global_state *
intel_atomic_get_old_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj);
struct intel_global_state *
intel_atomic_get_new_global_obj_state(struct intel_atomic_state *state,
struct intel_global_obj *obj);
void intel_atomic_swap_global_state(struct intel_atomic_state *state);
void intel_atomic_clear_global_state(struct intel_atomic_state *state);
int intel_atomic_lock_global_state(struct intel_global_state *obj_state);
int intel_atomic_serialize_global_state(struct intel_global_state *obj_state);
#endif
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
#include "display/intel_dpll_mgr.h" #include "display/intel_dpll_mgr.h"
#include "display/intel_dsb.h" #include "display/intel_dsb.h"
#include "display/intel_frontbuffer.h" #include "display/intel_frontbuffer.h"
#include "display/intel_global_state.h"
#include "display/intel_gmbus.h" #include "display/intel_gmbus.h"
#include "display/intel_opregion.h" #include "display/intel_opregion.h"
...@@ -1098,6 +1099,8 @@ struct drm_i915_private { ...@@ -1098,6 +1099,8 @@ struct drm_i915_private {
*/ */
struct mutex dpll_lock; struct mutex dpll_lock;
struct list_head global_obj_list;
/* /*
* For reading active_pipes, cdclk_state holding any crtc * For reading active_pipes, cdclk_state holding any crtc
* lock is sufficient, for writing must hold all of them. * lock is sufficient, for writing must hold all of them.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment