• Vladimir Oltean's avatar
    spi: Add a PTP system timestamp to the transfer structure · 79591b7d
    Vladimir Oltean authored
    SPI is one of the interfaces used to access devices which have a POSIX
    clock driver (real time clocks, 1588 timers etc). The fact that the SPI
    bus is slow is not what the main problem is, but rather the fact that
    drivers don't take a constant amount of time in transferring data over
    SPI. When there is a high delay in the readout of time, there will be
    uncertainty in the value that has been read out of the peripheral.
    When that delay is constant, the uncertainty can at least be
    approximated with a certain accuracy which is fine more often than not.
    
    Timing jitter occurs all over in the kernel code, and is mainly caused
    by having to let go of the CPU for various reasons such as preemption,
    servicing interrupts, going to sleep, etc. Another major reason is CPU
    dynamic frequency scaling.
    
    It turns out that the problem of retrieving time from a SPI peripheral
    with high accuracy can be solved by the use of "PTP system
    timestamping" - a mechanism to correlate the time when the device has
    snapshotted its internal time counter with the Linux system time at that
    same moment. This is sufficient for having a precise time measurement -
    it is not necessary for the whole SPI transfer to be transmitted "as
    fast as possible", or "as low-jitter as possible". The system has to be
    low-jitter for a very short amount of time to be effective.
    
    This patch introduces a PTP system timestamping mechanism in struct
    spi_transfer. This is to be used by SPI device drivers when they need to
    know the exact time at which the underlying device's time was
    snapshotted. More often than not, SPI peripherals have a very exact
    timing for when their SPI-to-interconnect bridge issues a transaction
    for snapshotting and reading the time register, and that will be
    dependent on when the SPI-to-interconnect bridge figures out that this
    is what it should do, aka as soon as it sees byte N of the SPI transfer.
    Since spi_device drivers are the ones who'd know best how the peripheral
    behaves in this regard, expose a mechanism in spi_transfer which allows
    them to specify which word (or word range) from the transfer should be
    timestamped.
    
    Add a default implementation of the PTP system timestamping in the SPI
    core. This is not going to be satisfactory performance-wise, but should
    at least increase the likelihood that SPI device drivers will use PTP
    system timestamping in the future.
    There are 3 entry points from the core towards the SPI controller
    drivers:
    
    - transfer_one: The driver is passed individual spi_transfers to
      execute. This is the easiest to timestamp.
    
    - transfer_one_message: The core passes the driver an entire spi_message
      (a potential batch of spi_transfers). The core puts the same pre and
      post timestamp to all transfers within a message. This is not ideal,
      but nothing better can be done by default anyway, since the core has
      no insight into how the driver batches the transfers.
    
    - transfer: Like transfer_one_message, but for unqueued drivers (i.e.
      the driver implements its own queue scheduling).
    Signed-off-by: default avatarVladimir Oltean <olteanv@gmail.com>
    Link: https://lore.kernel.org/r/20190905010114.26718-3-olteanv@gmail.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
    79591b7d
spi.c 107 KB