linux-stable/sound/hda
Thomas Gleixner 9de9858d7b ALSA: hda: Make proper use of timecounter
[ Upstream commit 6dd21ad81b ]

HDA uses a timecounter to read a hardware clock running at 24 MHz. The
conversion factor is set with a mult value of 125 and a shift value of 0,
which is not converting the hardware clock to nanoseconds, it is converting
to 1/3 nanoseconds because the conversion factor from 24Mhz to nanoseconds
is 125/3. The usage sites divide the "nanoseconds" value returned by
timecounter_read() by 3 to get a real nanoseconds value.

There is a lengthy comment in azx_timecounter_init() explaining this
choice. That comment makes blatantly wrong assumptions about how
timecounters work and what can overflow.

The comment says:

     * Applying the 1/3 factor as part of the multiplication
     * requires at least 20 bits for a decent precision, however
     * overflows occur after about 4 hours or less, not a option.

timecounters operate on time deltas between two readouts of a clock and use
the mult/shift pair to calculate a precise nanoseconds value:

    delta_nsec = (delta_clock * mult) >> shift;

The fractional part is also taken into account and preserved to prevent
accumulated rounding errors. For details see cyclecounter_cyc2ns().

The mult/shift pair has to be chosen so that the multiplication of the
maximum expected delta value does not result in a 64bit overflow. As the
counter wraps around on 32bit, the maximum observable delta between two
reads is (1 << 32) - 1 which is about 178.9 seconds.

That in turn means the maximum multiplication factor which fits into an u32
will not cause a 64bit overflow ever because it's guaranteed that:

     ((1 << 32) - 1) ^ 2 < (1 << 64)

The resulting correct multiplication factor is 2796202667 and the shift
value is 26, i.e. 26 bit precision. The overflow of the multiplication
would happen exactly at a clock readout delta of 6597069765 which is way
after the wrap around of the hardware clock at around 274.8 seconds which
is off from the claimed 4 hours by more than an order of magnitude.

If the counter ever wraps around the last read value then the calculation
is off by the number of wrap arounds times 178.9 seconds because the
overflow cannot be observed.

Use clocks_calc_mult_shift(), which calculates the most accurate mult/shift
pair based on the given clock frequency, and remove the bogus comment along
with the divisions at the readout sites.

Fixes: 5d890f591d ("ALSA: hda: support for wallclock timestamps")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Link: https://lore.kernel.org/r/871r35kwji.ffs@tglx
Signed-off-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2022-01-27 11:04:07 +01:00
..
ext ALSA: hda: hdac_ext_stream: fix potential locking issues 2021-11-25 09:49:08 +01:00
array.c treewide: Add SPDX license identifier for missed files 2019-05-21 10:50:45 +02:00
hda_bus_type.c treewide: Add SPDX license identifier for more missed files 2019-05-21 10:50:45 +02:00
hdac_bus.c ALSA: hda: add link_power op to hdac_bus_ops 2021-02-08 15:56:35 +01:00
hdac_component.c ALSA: hda/i915 - fix list corruption with concurrent probes 2020-10-09 16:46:04 +02:00
hdac_controller.c ALSA: hda: avoid write to STATESTS if controller is in reset 2021-10-12 18:05:28 +02:00
hdac_device.c ALSA: hda: fix a runtime pm issue in SOF when integrated GPU is disabled 2020-09-02 19:26:39 +02:00
hdac_i915.c ALSA: hda/i915 - fix list corruption with concurrent probes 2020-10-09 16:46:04 +02:00
hdac_regmap.c ALSA: hda: fix kernel-doc warnings 2021-03-02 10:21:36 +01:00
hdac_stream.c ALSA: hda: Make proper use of timecounter 2022-01-27 11:04:07 +01:00
hdac_sysfs.c ALSA: hda: Constify static attribute_group 2021-01-31 09:50:11 +01:00
hdmi_chmap.c ALSA: hda: Use scnprintf() for printing texts for sysfs/procfs 2020-02-18 10:15:41 +01:00
intel-dsp-config.c ALSA: intel-dsp-config: add quirk for CML devices based on ES8336 codec 2021-12-08 09:04:49 +01:00
intel-nhlt.c ALSA: hda: ignore invalid NHLT table 2021-03-04 09:14:36 +01:00
intel-sdw-acpi.c ALSA: hda: intel-sdw-acpi: go through HDAS ACPI at max depth of 2 2022-01-05 12:42:33 +01:00
Kconfig ALSA: hda: Revert "ALSA: hda: Allow setting preallocation again for x86" 2021-03-19 16:55:11 +01:00
local.h ALSA: hda: Unexport some local helper functions 2020-05-16 08:29:49 +02:00
Makefile ALSA: hda: move Intel SoundWire ACPI scan to dedicated module 2021-03-02 15:33:00 +01:00
trace.c
trace.h License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00