On Thu, 19 Sep 2024, Jani Nikula <jani.nikula@xxxxxxxxx> wrote: > The macros for iterating crtcs have become unweildy. Add a more generic > solution. > > This is extensible via new initialization functions, more pipe_masks, > and dedicated .get_next() functions. > > Cc: Ankit Nautiyal <ankit.k.nautiyal@xxxxxxxxx> > Cc: Ville Syrjälä <ville.syrjala@xxxxxxxxxxxxxxx> > Signed-off-by: Jani Nikula <jani.nikula@xxxxxxxxx> > --- > drivers/gpu/drm/i915/Makefile | 1 + > .../gpu/drm/i915/display/intel_crtc_iter.c | 94 +++++++++++++++++++ > .../gpu/drm/i915/display/intel_crtc_iter.h | 87 +++++++++++++++++ > drivers/gpu/drm/xe/Makefile | 1 + > 4 files changed, 183 insertions(+) > create mode 100644 drivers/gpu/drm/i915/display/intel_crtc_iter.c > create mode 100644 drivers/gpu/drm/i915/display/intel_crtc_iter.h > > diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile > index bb67bad839ea..7a370cc91dcb 100644 > --- a/drivers/gpu/drm/i915/Makefile > +++ b/drivers/gpu/drm/i915/Makefile > @@ -232,6 +232,7 @@ i915-y += \ > display/intel_combo_phy.o \ > display/intel_connector.o \ > display/intel_crtc.o \ > + display/intel_crtc_iter.o \ > display/intel_crtc_state_dump.o \ > display/intel_cursor.o \ > display/intel_display.o \ > diff --git a/drivers/gpu/drm/i915/display/intel_crtc_iter.c b/drivers/gpu/drm/i915/display/intel_crtc_iter.c > new file mode 100644 > index 000000000000..27f16b565a67 > --- /dev/null > +++ b/drivers/gpu/drm/i915/display/intel_crtc_iter.c > @@ -0,0 +1,94 @@ > +// SPDX-License-Identifier: MIT > +/* Copyright © 2024 Intel Corporation */ > + > +#include <linux/list.h> > +#include <linux/kernel.h> > + > +#include "intel_crtc_iter.h" > +#include "intel_display.h" > +#include "intel_display_core.h" > +#include "intel_display_types.h" > + > +static void reset_pos(struct intel_crtc_iter *iter) > +{ > + if (iter->reverse) > + iter->pos = list_last_entry(iter->crtc_list, typeof(*iter->pos), base.head); > + else > + iter->pos = list_first_entry(iter->crtc_list, typeof(*iter->pos), base.head); > +} > + > +static struct intel_crtc *get_next(struct intel_crtc_iter *iter) > +{ > + if (iter->reverse) { > + list_for_each_entry_from(iter->pos, iter->crtc_list, base.head) { > + if (iter->pipe_mask[iter->pipe_mask_index] & BIT(iter->pos->pipe)) > + return iter->pos; > + } > + } else { > + list_for_each_entry_from_reverse(iter->pos, iter->crtc_list, base.head) { > + if (iter->pipe_mask[iter->pipe_mask_index] & BIT(iter->pos->pipe)) > + return iter->pos; > + } > + } It's getting late, the blocks for the above are the wrong way around. > + > + /* List exhausted, start over with the next pipe mask, if any. */ > + iter->pipe_mask_index++; > + if (iter->pipe_mask_index < ARRAY_SIZE(iter->pipe_mask) && > + iter->pipe_mask[iter->pipe_mask_index]) { > + reset_pos(iter); > + return get_next(iter); > + } > + > + return NULL; > +} > + > +/* > + * Iterate all CRTCs in forward or reverse CRTC initialization order, depending > + * on reverse parameter, first matching pipes in pipe_mask0, then the pipes in > + * pipe_mask1. > + */ > +void __intel_crtc_iter_begin(struct intel_display *display, > + struct intel_crtc_iter *iter, > + unsigned long pipe_mask0, > + unsigned long pipe_mask1, > + bool reverse) > +{ > + iter->crtc_list = &display->drm->mode_config.crtc_list; > + iter->reverse = reverse; > + reset_pos(iter); > + iter->get_next = get_next; > + iter->pipe_mask_index = 0; > + iter->pipe_mask[0] = pipe_mask0; > + iter->pipe_mask[1] = pipe_mask1; > + > + /* It's an error to match the same pipe twice. */ > + drm_WARN_ON(display->drm, pipe_mask0 & pipe_mask1); > +} > + > + > +void intel_crtc_iter_end(struct intel_crtc_iter *iter) > +{ > + memset(iter, 0, sizeof(*iter)); > +} > + > +void intel_crtc_iter_begin_modeset_enable(struct intel_display *display, > + struct intel_crtc_iter *iter, > + const struct intel_crtc_state *crtc_state) > +{ > + /* Enable secondary pipes first, then the primary pipes. */ > + __intel_crtc_iter_begin(display, iter, > + _intel_modeset_secondary_pipes(crtc_state), > + _intel_modeset_primary_pipes(crtc_state), > + false); > +} > + > +void intel_crtc_iter_begin_modeset_disable(struct intel_display *display, > + struct intel_crtc_iter *iter, > + const struct intel_crtc_state *crtc_state) > +{ > + /* Disable primary pipes first, then the secondary pipes. */ > + __intel_crtc_iter_begin(display, iter, > + _intel_modeset_primary_pipes(crtc_state), > + _intel_modeset_secondary_pipes(crtc_state), > + true); > +} > diff --git a/drivers/gpu/drm/i915/display/intel_crtc_iter.h b/drivers/gpu/drm/i915/display/intel_crtc_iter.h > new file mode 100644 > index 000000000000..ec8d7ea3d595 > --- /dev/null > +++ b/drivers/gpu/drm/i915/display/intel_crtc_iter.h > @@ -0,0 +1,87 @@ > +/* SPDX-License-Identifier: MIT */ > +/* Copyright © 2024 Intel Corporation */ > + > +/* > + * struct intel_crtc iterators for various needs. > + * > + * Usage: > + * > + * struct intel_crtc *crtc; > + * struct intel_crtc_iter iter; > + * > + * intel_crtc_iter_begin(display, &iter); > + * intel_crtc_iter_for_each(crtc, &iter) { > + * ... > + * } > + * intel_crtc_iter_end(&iter); > + * > + * There are various alternatives to intel_crtc_iter_begin() that change the > + * iteration behaviour, but the rest remains the same in all cases. > + */ > + > +#ifndef __INTEL_CRTC_ITER_H__ > +#define __INTEL_CRTC_ITER_H__ > + > +#include <linux/types.h> > + > +struct intel_crtc; > +struct intel_crtc_state; > +struct intel_display; > +struct list_head; > + > +/* This is private. Do not look insde. */ > +struct intel_crtc_iter { > + struct list_head *crtc_list; > + struct intel_crtc *pos; > + struct intel_crtc *(*get_next)(struct intel_crtc_iter *iter); > + int pipe_mask_index; > + u32 pipe_mask[2]; > + bool reverse; > +}; > + > +void __intel_crtc_iter_begin(struct intel_display *display, > + struct intel_crtc_iter *iter, > + unsigned long pipe_mask0, > + unsigned long pipe_mask1, > + bool reverse); > + > +static inline void intel_crtc_iter_begin(struct intel_display *display, > + struct intel_crtc_iter *iter) > +{ > + __intel_crtc_iter_begin(display, iter, ~0UL, 0, false); > +} > + > +static inline void intel_crtc_iter_begin_reverse(struct intel_display *display, > + struct intel_crtc_iter *iter) > +{ > + __intel_crtc_iter_begin(display, iter, ~0UL, 0, true); > +} > + > +static inline void intel_crtc_iter_begin_pipe_mask(struct intel_display *display, > + struct intel_crtc_iter *iter, > + unsigned long pipe_mask) > +{ > + __intel_crtc_iter_begin(display, iter, pipe_mask, 0, false); > +} > + > +static inline void intel_crtc_iter_begin_pipe_mask_reverse(struct intel_display *display, > + struct intel_crtc_iter *iter, > + unsigned long pipe_mask) > +{ > + __intel_crtc_iter_begin(display, iter, pipe_mask, 0, true); > +} > + > +void intel_crtc_iter_begin_modeset_enable(struct intel_display *display, > + struct intel_crtc_iter *iter, > + const struct intel_crtc_state *crtc_state); > + > +void intel_crtc_iter_begin_modeset_disable(struct intel_display *display, > + struct intel_crtc_iter *iter, > + const struct intel_crtc_state *crtc_state); > + > +#define intel_crtc_iter_for_each(crtc, iter) \ > + while (((crtc) = (iter)->get_next(iter))) > + > +void intel_crtc_iter_end(struct intel_crtc_iter *iter); > + > +#endif /* __INTEL_CRTC_ITER_H__ */ > diff --git a/drivers/gpu/drm/xe/Makefile b/drivers/gpu/drm/xe/Makefile > index ae245fbd91ee..e1c8111b2528 100644 > --- a/drivers/gpu/drm/xe/Makefile > +++ b/drivers/gpu/drm/xe/Makefile > @@ -198,6 +198,7 @@ xe-$(CONFIG_DRM_XE_DISPLAY) += \ > i915-display/intel_combo_phy.o \ > i915-display/intel_connector.o \ > i915-display/intel_crtc.o \ > + i915-display/intel_crtc_iter.o \ > i915-display/intel_crtc_state_dump.o \ > i915-display/intel_cursor.o \ > i915-display/intel_cx0_phy.o \ -- Jani Nikula, Intel