drm/i915/display: add generic to_intel_display() macro
authorJani Nikula <jani.nikula@intel.com>
Wed, 17 Apr 2024 13:02:40 +0000 (16:02 +0300)
committerJani Nikula <jani.nikula@intel.com>
Thu, 18 Apr 2024 18:16:30 +0000 (21:16 +0300)
Convert various pointers to struct intel_display * using _Generic().

Add some macro magic to make adding new conversions easier, and somewhat
abstract the need to cast each generic association. The cast is required
because all associations needs to compile, regardless of the type and
the generic selection.

The use of *p in the generic selection assignment expression removes the
need to add separate associations for const pointers.

Note: This intentionally does *not* cover struct drm_i915_private or
struct xe_device. They are not to be used in the long run, so avoid
using this macro for them.

Reviewed-by: Rodrigo Vivi <rodrigo.vivi@intel.com>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/02cf407961200db4379370856c779ea62b3eaa90.1713358679.git.jani.nikula@intel.com
drivers/gpu/drm/i915/display/intel_display_types.h

index 387119c0254c89a5213ea87b4b448840bb68f897..62f7a30c37dcfc8b5266f266373ccf8dfc86ad6a 100644 (file)
@@ -2156,4 +2156,41 @@ static inline int to_bpp_x16(int bpp)
        return bpp << 4;
 }
 
+/*
+ * Conversion functions/macros from various pointer types to struct
+ * intel_display pointer.
+ */
+#define __drm_device_to_intel_display(p) \
+       (&to_i915(p)->display)
+#define __intel_connector_to_intel_display(p)          \
+       __drm_device_to_intel_display((p)->base.dev)
+#define __intel_crtc_to_intel_display(p)               \
+       __drm_device_to_intel_display((p)->base.dev)
+#define __intel_crtc_state_to_intel_display(p)                 \
+       __drm_device_to_intel_display((p)->uapi.crtc->dev)
+#define __intel_digital_port_to_intel_display(p)               \
+       __drm_device_to_intel_display((p)->base.base.dev)
+#define __intel_dp_to_intel_display(p) \
+       __drm_device_to_intel_display(dp_to_dig_port(p)->base.base.dev)
+#define __intel_encoder_to_intel_display(p)            \
+       __drm_device_to_intel_display((p)->base.dev)
+#define __intel_hdmi_to_intel_display(p)       \
+       __drm_device_to_intel_display(hdmi_to_dig_port(p)->base.base.dev)
+
+/* Helper for generic association. Map types to conversion functions/macros. */
+#define __assoc(type, p) \
+       struct type: __##type##_to_intel_display((struct type *)(p))
+
+/* Convert various pointer types to struct intel_display pointer. */
+#define to_intel_display(p)                            \
+       _Generic(*p,                                    \
+                __assoc(drm_device, p),                \
+                __assoc(intel_connector, p),           \
+                __assoc(intel_crtc, p),                \
+                __assoc(intel_crtc_state, p),          \
+                __assoc(intel_digital_port, p),        \
+                __assoc(intel_dp, p),                  \
+                __assoc(intel_encoder, p),             \
+                __assoc(intel_hdmi, p))
+
 #endif /*  __INTEL_DISPLAY_TYPES_H__ */