Files
externals/linux/include/celoxica/lldt.h
T
2017-10-04 14:16:02 -04:00

757 lines
27 KiB
C

/****************************************************************
* *
* Copyright (C) 1991-2007 Celoxica Ltd. All rights reserved. *
* *
*****************************************************************
* *
* Project : Low-Latency Data Transfer library *
* Date : 01 FEB 2008 *
* File : lldt.h *
* Author : Daniel Sulik (DS) *
* Contributors: Will Benfold (WB) *
* *
* Description: *
* Low-latency HT data capture test declarations. *
* *
* Date Version Author Reason for change *
* *
* 01 FEB 2008 1.00 DS Created *
* *
****************************************************************/
#ifndef __LLDT_H__
#define __LLDT_H__
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include "rc.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _MSC_VER
#ifdef LLDT_EXPORTS
#define LLDT_API __declspec(dllexport)
#else
#define LLDT_API __declspec(dllimport)
#endif
#else
#define LLDT_API
#endif
/****************************************************************************
*
* LLDTPacket
* LLDTControlWord
* LLDTMessage
*
* Summary:
* The LLDTPacket type represents the smallest transfer unit used by
* the LLDT API. An LLDTPacket may be invalidated by subsequent
* packets; it is the caller's responsibility to deal with the
* consequences.
*
* An LLDTMessage represents a string of LLDTPackets of variable
* length. Once passed to the caller, an LLDTMessage cannot be
* invalidated.
*
* Description:
* Each packet contains fifteen words of data, and one control word,
* described by the LLDTControlWord type. The fields of the control
* word are as follows:
*
* Type : Indicates which source generated this packet
* EOM : If set, indicates that this is the last packet in the
* source's current message
* Err : The source signalled an error on this message. For
* some sources (e.g. ethernet), this implies that
* previous packets within the same message should be
* discarded.
* SubType: Type-specific information; sources may use this field
* to add extra status/information
* Len : Indicates how many valid data bytes the packet contains
* Data : An array containing the received data
*
* The LLDTMessage structure contains a subset of these fields:
*
* typedef struct {
* void *Data;
* unsigned long Len;
* unsigned int Type;
* unsigned int SubType;
* } LLDTMessage;
*
****************************************************************************/
#define LLDT64_PACKET_LEN 64
#define LLDT128_PACKET_LEN 128
#define LLDT_PAYLOAD_LENGTH_MAX 120
#pragma pack(push,1)
typedef enum {
LLDTTextModeLLDTPackets,
LLDTTextModeProtoPackets,
LLDTTextModeGMACDumpPackets,
LLDTTextModeMax /* keep at bottom */
} LLDTTextMode;
/* 64 bytes packets */
struct _LLDT64ControlWord
{
unsigned EOM : 1;
unsigned Err : 1;
unsigned Len : 6;
unsigned Reserved: 7;
unsigned Colour : 1;
unsigned SubType : 8;
unsigned Type : 8;
};
typedef struct _LLDT64ControlWord LLDT64ControlWord;
struct _LLDT64Packet
{
char Data[LLDT64_PACKET_LEN - sizeof(LLDT64ControlWord)];
LLDT64ControlWord Ctrl;
};
typedef struct _LLDT64Packet LLDT64Packet;
/* 128 byte packets */
struct _LLDT128ControlWord
{
unsigned EOM : 1;
unsigned Err : 1; // removed Len and Reserved, pushed below
unsigned Colour : 1; // moved by 13 bits
unsigned Reserved: 1;
unsigned SubTypeB: 4;
unsigned Len : 8; // moved
unsigned SubType : 8; // staying
unsigned Type : 8; // staying
};
typedef struct _LLDT128ControlWord LLDT128ControlWord;
struct _LLDT128Packet
{
char Data[LLDT128_PACKET_LEN - sizeof(LLDT128ControlWord)];
LLDT128ControlWord Ctrl;
};
typedef struct _LLDT128Packet LLDT128Packet;
/* Union packet */
typedef struct
{
union
{
LLDT64Packet Packet64;
LLDT128Packet Packet128;
};
}
LLDTUPacket;
#pragma pack(pop)
#define LLDT64_CONTROL_WORD_LENGTH sizeof(LLDT64ControlWord)
#define LLDT64_PAYLOAD_LENGTH (LLDT64_PACKET_LEN - LLDT64_CONTROL_WORD_LENGTH)
#define LLDT128_CONTROL_WORD_LENGTH sizeof(LLDT128ControlWord)
#define LLDT128_PAYLOAD_LENGTH (LLDT128_PACKET_LEN - LLDT128_CONTROL_WORD_LENGTH)
struct _LLDTMessage;
typedef struct _LLDTMessage LLDTMessage;
/***************************************************************************
*
* LLDTStatus
*
* Summary:
* The type of LLDT error codes
*
* Description:
* Unless specified otherwise, all LLDT functions return one of the
* following status codes:
*
* LLDT_STATUS_OK No error occurred
* LLDT_STATUS_MALLOC_FAILED A call to malloc() failed
* LLDT_STATUS_NOT_OPEN Use of uninitialised LLDTInstance
* LLDT_STATUS_TIMEOUT A timeout expired
* LLDT_STATUS_STOPPED LLDTStop() was called
* LLDT_STATUS_BUFFER_FULL A message buffer ran out of space
* (or no space was allocated)
* LLDT_STATUS_FRAGMENT_OK A valid message fragment was received
* but more fragments follow
* LLDT_STATUS_FRAGMENT_ERROR An invalid message fragment was
* received
* LLDT_STATUS_INVALID_INFOMESSAGE An invalid info message was
* received
* LLDT_STATUS_NO_PLATFORM No LLDT implementation is
* available for the specified
* board
* LLDT_STATUS_INVALID_ADDRESS The specified address falls
* outside the register space
* LLDT_STATUS_HARDWARE_FAILURE The hardware did not respond
* as expected
* LLDT_STATUS_INVALID_CPU_AFFINITY The current CPU affinity mask
* permits migration
* LLDT_STATUS_NO_INSTANCE The specified hardware
* instance does not exist
*
***************************************************************************/
typedef enum
{
LLDT_STATUS_OK = 0,
LLDT_STATUS_MALLOC_FAILED,
LLDT_STATUS_NOT_OPEN,
LLDT_STATUS_TIMEOUT,
LLDT_STATUS_STOPPED,
LLDT_STATUS_BUFFER_FULL,
LLDT_STATUS_FRAGMENT_OK,
LLDT_STATUS_FRAGMENT_ERROR,
LLDT_STATUS_INVALID_INFOMESSAGE,
LLDT_STATUS_NO_PLATFORM,
LLDT_STATUS_INVALID_ADDRESS,
LLDT_STATUS_HARDWARE_FAILURE,
LLDT_STATUS_INVALID_CPU_AFFINITY,
LLDT_STATUS_NO_INSTANCE,
LLDT_STATUS_BAD_STATUS_CODE /* must be last */
}
LLDTStatus;
/***************************************************************************
*
* Low-Latency Data Transfer API
*
***************************************************************************
*
* LLDTStatus LLDTOpen (RCBoard Board, int Index, LLDTInstance **LLDTPtr, uint32_t DMAQueue);
*
* Arguments:
* Board: A handle previously obtained using RCBoardOpen()
* Index: An integer (see below)
* LLDTPtr: Address of a pointer to an LLDTInstance
* DMAQueue: Set 'Q' or 'NOT Q' that LLDT instance uses
*
* Description:
* Creates an LLDT handle and initialises hardware. The Index
* parameter is used to determine which hardware LLDT instance
* should be used.
*
* This function must be invoked with CPU migration disabled, i.e.
* with a CPU affinity mask which has exactly one bit set. All
* future calls (using the same handle) to the functions listed
* below must use the same affinity mask.
*
* LLDTBegin
* LLDTEnd
* LLDTClose
*
* LLDTNextMessage
* LLDTNextMessageFragment
* LLDTNextPacket
* LLDTReleasePacket
*
* This condition will be checked for LLDTOpen() only. It is the
* responsibility of the caller to ensure that the affinity is
* preserved. Undefined behaviour will result if these restrictions
* are not observed.
*
***************************************************************************
*
* LLDTStatus LLDTClose (LLDTInstance *LLDT);
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
*
* Description:
* Release hardware.
*
***************************************************************************
*
* LLDTStatus LLDTBegin (LLDTInstance *LLDT);
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
*
* Description:
* Enable hardware message sources and begin DMA transfer to the host
*
***************************************************************************
*
* LLDTStatus LLDTEnd (LLDTInstance *LLDT);
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
*
* Description:
* End DMA transfer and disable message sources
*
***************************************************************************
*
* LLDTPacket *LLDTNextPacket (LLDTInstance *LLDT, LLDTTimeout Timeout);
*
* Arguments:
* LLDT : A handle obtained from LLDTOpen()
* Timeout: How long to wait for data before returning NULL.
* If LLDT_TIMEOUT_INFINITE is used, the function will
* not return early unless LLDTStop() is called. If
* this parameter is zero, only one polling cycle will
* be performed, and the function will return almost
* immediately.
*
* Any other value specifies a timeout, measured in
* polling iterations (TODO: use TSC instead).
*
* Returns: Pointer to the next LLDTPacket, NULL on timeout
*
* Description:
* Waits for the next packet to arrive, then returns a pointer to it.
*
* After a successful call, LLDTReleasePacket MUST be used to release
* the captured packet.
*
***************************************************************************
*
* void LLDTReleasePacket (LLDTInstance *LLDT, volatile LLDTPacket *Packet)
*
* Arguments:
* LLDT : A handle obtained from LLDTOpen()
* Packet : A packet obtained from LLDTNextPacket()
*
* Description:
* Releases the specified packet, so that it may be overwritten by
* the hardware.
*
***************************************************************************
*
* LLDTStatus LLDTNextMessage (LLDTInstance *LLDT,
* const LLDTMessage **MessagePtr,
* LLDTTimeout Timeout);
*
* void LLDTMessageBufferReset (const LLDTMessage *Message);
* void LLDTMessageBufferAdvance (const LLDTMessage *Message);
* unsigned long LLDTMessageBufferSpaceRemaining (const LLDTMessage *Message);
*
* void LLDTMessageBufferTranslationSet (LLDTInstance *LLDT,
* int FromType, int ToType);
*
* Arguments:
* LLDT : A handle obtained from LLDTOpen()
* Message: An LLDTMessage pointer
* MessagePtr: Pointer to an LLDTMessage pointer
* Timeout: How long to wait for data before returning
* LLDT_STATUS_TIMEOUT; see LLDTNextPacket().
* FromType: A message type to change
* ToType: The message type to change to
*
* Description:
* LLDTNextMessage waits for the next complete message. The type,
* subtype, length and payload are available in an LLDTMessage
* structure, returned through the supplied pointer.
*
* After receiving a message, the caller MUST indicate what should
* be done with the associated storage, as follows:
*
* To have the current message overwritten by the next:
*
* LLDTMessageBufferReset (Message);
*
* To have the next message stored just after the current one:
*
* LLDTMessageBufferAdvance (Message);
*
* To have future messages stored in a new buffer:
*
* LLDTMessageBufferSet (LLDT, Message->Type
*
* As a convenience, the space remaining in the current buffer can be
* determined using LLDTMessageBufferSpaceRemaining().
*
* LLDTMessageBufferTranslationSet may be used to allow one message
* type to be reported as if it were another. FromType always refers
* to the "real" message type, while ToType specifies the alias.
*
***************************************************************************
*
* LLDTStatus LLDTNextMessageFragment (LLDTInstance *LLDT,
* const LLDTMessage **MessagePtr,
* LLDTTimeout Timeout);
*
* Description:
* As for LLDTNextMessage(), but returns every time some data is
* acquired. Still returns LLDT_STATUS_OK for end-of-message, but
* can also return LLDT_FRAGMENT_OK for mid-message fragment, and
* LLDT_FRAGMENT_ERROR for an error fragment (caller should discard
* data since last end-of-message.
*
***************************************************************************
*
* LLDTStatus LLDTInfoMessageRequestSend (LLDTInstance *LLDT,
* int InfoRegistersGroup);
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* InfoRegistersGroup: Register group number.
*
* Description:
* Request a group of registers from the hardware. The register data
* will be returned as a message of type (TODO: type); the group
* number will be visible in the SubType field.
*
* TODO: Either describe the contents of these register dumps, or
* provide routines for extracting the useful information.
* Also document the message type used.
*
***************************************************************************
*
* LLDTStatus LLDTInfoMessageProcess (LLDTInstance *LLDT, void *Data,
* unsigned int Len)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* Data: A pointer to an InfoMessage payload
* Len : The length of the InfoMessage payload
*
* Description:
* Request that LLDT process the specified group 0 info message. This
* register group contains hardware-specific information which can be
* used by LLDT to improve the statistics reported through
* LLDTStatsGet(). Results are undefined if the supplied data is not
* a group 0 info message/packet payload (SubType should be 0).
*
***************************************************************************
*
* LLDTStatus LLDTStopSet (LLDTInstance *LLDT, int Stop)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* Stop: Boolean stop state (0=Normal, 1=Stop)
*
* Description:
* Setting the stop state to 1 causes all current and subsequent
* calls to LLDTNextPacket() or LLDTNextMessage() should return
* immediately. The effect persists until the stop state is set to
* zero.
*
***************************************************************************
*
* int LLDTStopGet (LLDTInstance *LLDT)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
*
* Returns:
* Nonzero if calls to LLDTNextMessage() and LLDTNextPacket() will
* return immediately (i.e. LLDTStop() has been called). Otherwise
* returns zero.
*
***************************************************************************
*
* LLDTStatus LLDTMaxMessageLengthSet (LLDTInstance *LLDT, int Value,
* int Port)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* Value: Maximal message length value.
* Port: LLDT port index.
*
* Description:
* Writes maximal message length allowed for a specific LLDT port.
* If the hw FIFO on the LLDT port has less free space than the Value
* the FIFO will refuse to recevie the whole message
* (truncate bit for the corresponding port is cleared) or truncate
* the message (truncate bit for the corresponding port is set).
*
***************************************************************************
*
* LLDTStatus LLDTTruncateModeSet (LLDTInstance *LLDT, int Mask)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* Mask: Bit mask for LLDT ports. Bit 0 corresponds to
* LLDT port 0, bit 1 to LLDT port 1, etc.
* Description:
* When corresponding bit is set the truncation is on otherwise
* off (the whole message discarding is on).
*
***************************************************************************
*
* size_t LLDTRegisterOffset (LLDTInstance *LLDT)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
*
* Description:
* Returns the offset of the application-specific registers within
* the register space. When writing to these registers, the address
* passed to RCWrite should be adjusted by this value
*
***************************************************************************
*
* LLDTStatus LLDTRegisterWrite (LLDTInstance *LLDT, size_t Offset,
* uint32_t Value)
*
* Arguments:
* LLDT: A handle obtained from LLDTOpen()
* Offset: Register address with application register space
* Value: Value to write
*
* Description:
* Writes the specified value to the application-specific registers.
*
***************************************************************************/
struct _LLDTInstance;
typedef struct _LLDTInstance LLDTInstance;
typedef uint64_t LLDTTimeout;
#define LLDT_TIMEOUT_INFINITE ((LLDTTimeout) -1)
#define LLDT_BLOCKING ((LLDTTimeout) -1)
#define LLDT_NONBLOCKING ((LLDTTimeout) 1)
LLDT_API LLDTStatus LLDTOpen (RCBoard Board, int Index,
LLDTInstance **LLDT, uint32_t DMAQueue);
LLDT_API int LLDTGetQueueDescriptor (LLDTInstance *LLDT);
LLDT_API LLDTStatus LLDTClose (LLDTInstance *LLDT);
LLDT_API LLDTStatus LLDTBegin (LLDTInstance *LLDT);
LLDT_API LLDTStatus LLDTEnd (LLDTInstance *LLDT);
LLDT_API LLDTUPacket *LLDTNextPacket (LLDTInstance *LLDT,
LLDTTimeout Timeout);
LLDT_API void LLDTReleasePacket (LLDTInstance *LLDT,
volatile LLDTUPacket *Packet);
LLDT_API LLDTStatus LLDTEnsureRemainingSpace (LLDTInstance *I, unsigned int Packets);
LLDT_API void LLDTMessageBufferResetAll (LLDTInstance *LLDT);
LLDT_API void LLDTMessageBufferSet (LLDTInstance *LLDT, unsigned int Type,
void *Data, unsigned int Len);
LLDT_API void LLDTMessageBufferGet (LLDTInstance *LLDT, unsigned int Type,
void **DataPtr, unsigned int *LenPtr);
/* LLDT SIM ONLY ***********************************/
/* LLDT_API compromise for use by lldtsim only */
typedef enum
{
LLDT_STAT_RX_MESSAGES,
LLDT_STAT_ERROR_MESSAGES,
LLDT_STAT_REJECTED_MESSAGES,
LLDT_STAT_RX_BYTES,
LLDT_STAT_RX_PACKETS,
LLDT_STAT_INVALID_PACKETS,
LLDT_STAT_ERROR_PACKETS
}
LLDTStatType;
LLDTMessage* LLDTSimMessageGet (LLDTInstance *I, unsigned int Type);
void LLDTSimStatIncT (LLDTInstance *I, unsigned int Type, LLDTStatType Stat, unsigned int Count);
/* LLDT SIM ONLY ***********************************/
static inline
unsigned long LLDTMessageBufferSpaceRemaining (const LLDTMessage *Message);
static inline
void LLDTMessageBufferReset (const LLDTMessage *Message);
static inline
void LLDTMessageBufferAdvance (const LLDTMessage *Message);
LLDT_API void LLDTMessageBufferTranslationSet (LLDTInstance *LLDT, int From, int To);
LLDT_API LLDTStatus LLDTNextMessage (LLDTInstance *LLDT,
const LLDTMessage **Message,
LLDTTimeout Timeout);
LLDT_API LLDTStatus LLDTNextMessageFragment (LLDTInstance *LLDT,
const LLDTMessage **Message,
LLDTTimeout Timeout);
LLDT_API LLDTStatus LLDTInfoMessageRequestSend (LLDTInstance *LLDT,
int InfoRegistersGroup);
LLDT_API LLDTStatus LLDTInfoMessageProcess (LLDTInstance *LLDT, void *Data,
unsigned int Len);
LLDT_API LLDTStatus LLDTStopSet (LLDTInstance *Instance, int Stop);
LLDT_API int LLDTStopGet (LLDTInstance *Instance);
LLDT_API LLDTStatus LLDTMaxMessageLengthSet (LLDTInstance *LLDT, int Value,
int Port);
LLDT_API LLDTStatus LLDTTruncateModeSet (LLDTInstance *LLDT, int Mask);
LLDT_API size_t LLDTRegisterOffset (LLDTInstance *LLDT);
LLDT_API size_t LLDTRegisterLength (LLDTInstance *LLDT);
LLDT_API LLDTStatus LLDTRegisterWrite (LLDTInstance *LLDT, size_t Offset, uint32_t Value);
LLDT_API const char *LLDTStatusToString (LLDTStatus Status);
LLDT_API uint32_t LLDTGetCredit (LLDTInstance *LLDT, uint32_t *TotalLengthPtr);
typedef struct {
uint64_t RxBytes;
uint64_t RxPackets;
uint64_t RxMessages;
uint64_t ErrorMessages;
uint64_t RejectedMessages;
uint64_t ErrorPackets;
uint64_t CancelledPackets;
uint64_t InvalidPackets;
} LLDTStreamStats;
const static uint64_t LLDT_STAT_UNAVAILABLE = ((uint64_t) (-1));
#define LLDT_MAX_DROPPED_COUNTERS 8
typedef struct {
/* Protocol stats */
LLDTStreamStats Total, ByType[0x100];
uint64_t UnreleasedPackets;
uint64_t BufferIndex;
/* Hardware stats */
uint64_t DMABufferSize;
uint64_t InfoMessagesRequested;
uint64_t InfoMessagesProcessed;
/* From parsing infomessages */
uint64_t HWProtocolVersionMajor;
uint64_t HWProtocolVersionMinor;
uint64_t HWBuildTime;
uint64_t HWClockRate;
uint64_t HWPorts;
uint64_t HWDropped[LLDT_MAX_DROPPED_COUNTERS];
uint64_t HWDroppedTotal;
/* Compile-time */
uint64_t SWBuildTime;
} LLDTStats;
LLDT_API LLDTStats *LLDTStatsGet (LLDTInstance *Instance);
LLDT_API char *LLDTStatsToString (const LLDTStats *Stats);
LLDT_API void LLDTStatsStringFree (char *Str);
static inline void LLDTStatsDescribe (const LLDTStats *Stats, FILE *f)
{
char *Str = LLDTStatsToString (Stats);
fputs (Str, f);
free (Str);
}
typedef const int *LLDTVersion;
const static int _LLDTVersionCurrent = 4; /* mixed 64 and 128 packet length */
const static LLDTVersion LLDTVersionCurrent = &_LLDTVersionCurrent;
LLDT_API int LLDTVersionCheck (LLDTVersion Expected, int Verbosity);
/* Obsolete and debugging functions */
//LLDTStatus LLDTSourceEnable (LLDTInstance *LLDT);
//LLDTStatus LLDTSourceDisable (LLDTInstance *LLDT);
LLDT_API size_t LLDTDebugBufferOffset (LLDTInstance *I, const LLDTUPacket *P);
LLDT_API void LLDTDebugSaveBuffer (LLDTInstance *I, const char *Filename);
// void LLDTDumpState (LLDTInstance *I);
LLDT_API void LLDTDebugShowPlatformState (LLDTInstance *I, FILE *f);
LLDT_API void LLDTProfileReset (void);
LLDT_API void LLDTProfilePrint (FILE *f);
LLDT_API int LLDTMessagePassToEmulation(LLDTInstance *I, unsigned int Port, void *Data, int Length);
/**
* \brief Write the contents of the LLDT buffer to a file pointer
*
* @param I The LLDT instance pointer
* @param max_packets The maximum number of UDP packets to dump. 0 for all.
* @param f File pointer to which the data will be written.
*
* @return 0 on success, or -1 on error
*/
LLDT_API int LLDTWriteDump(LLDTInstance *I, unsigned int max_packets, FILE *f);
/**
* \brief Convert a binary dump file to text
*
* @param[in] in The binary file
* @param[out] out The text file.
* @param[in] force Force conversion even if the versions do not match
*
* @return 0 on success, or -1 on error
*/
LLDT_API int LLDTDumpToText(FILE *in, FILE *out, int force, LLDTTextMode mode);
/* Implementation details */
typedef struct
{
void *WritePtr;
void *EndPtr;
} LLDTMessageBuffer;
struct _LLDTMessage {
void *Data;
unsigned long Len;
unsigned int Type;
unsigned int SubType;
LLDTMessageBuffer Buffer;
};
static inline
unsigned long LLDTMessageBufferSpaceRemaining (const LLDTMessage *Message)
{
const LLDTMessageBuffer *B = &Message->Buffer;
return (char *) B->EndPtr - (char *) B->WritePtr;
}
static inline
void LLDTMessageBufferReset (const LLDTMessage *Message)
{
LLDTMessageBuffer *B = (LLDTMessageBuffer *) &Message->Buffer;
B->WritePtr = Message->Data;
}
static inline
void LLDTMessageBufferAdvance (const LLDTMessage *Message)
{
((LLDTMessage *) Message)->Data = Message->Buffer.WritePtr;
}
#ifdef __cplusplus
}
#endif
#endif /* __LLDT_H__ */