Re: [PATCH v6] drm/dp: clamp PWM bit count to advertised MIN and MAX capabilities

From: Johan Hovold
Date: Fri Apr 04 2025 - 04:55:03 EST


On Fri, Apr 04, 2025 at 08:54:29AM +0100, Christopher Obbard wrote:
> On Mon, 31 Mar 2025 at 09:33, Johan Hovold <johan@xxxxxxxxxx> wrote:

> > > @@ -4035,6 +4036,32 @@ drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_inf
> > > }
> > >
> > > pn &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
> > > +
> > > + ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);
> > > + if (ret < 0) {
> > > + drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",
> > > + aux->name, ret);
> > > + return -ENODEV;
> > > + }
> > > + pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
> > > +
> > > + ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);
> > > + if (ret < 0) {
> > > + drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",
> > > + aux->name, ret);
> > > + return -ENODEV;
> > > + }
> > > + pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;
> > > +
> > > + /*
> > > + * Per VESA eDP Spec v1.4b, section 3.3.10.2:
> > > + * If DP_EDP_PWMGEN_BIT_COUNT is less than DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN,
> > > + * the sink must use the MIN value as the effective PWM bit count.
> > > + * Clamp the reported value to the [MIN, MAX] capability range to ensure
> > > + * correct brightness scaling on compliant eDP panels.
> > > + */
> > > + pn = clamp(pn, pn_min, pn_max);
> >
> > You never make sure that pn_min <= pn_max so you could end up with
> > pn < pn_min on broken hardware here. Not sure if it's something you need
> > to worry about at this point.
>
> I am honestly not sure. I would hope that devices follow the spec and
> there is no need to be too paranoid, but then again we do live in the
> real world where things are... not so simple ;-).
> I will wait for further feedback from someone who has more experience
> with eDP panels than I have.

There's always going to be buggy devices and input should always be
sanitised so I suggest adding that check before calling clamp() (which
expects min <= max) so that the result here is well-defined.

Johan