// Copyright (C) 2023-2025 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
include "structure.fbs";
include "memoryManagement.fbs";

namespace MVCNN;

enum ActKernelType : byte {
    KERNEL = 0,
    DEBUG = 1,
    DXIL_KERNEL = 2,
    /// Used for describing L2Cache operations an ActShave might need to do before or after
    /// executing the ActKernel with Type == KERNEL. Handled by the ActRuntimeRuntime internally
    /// Add these as a separate ActKernel entry with 1 ActKernelInvocation for barrier information
    CACHE_OP_FLUSH = 3,
    CACHE_OP_INVALIDATE = 4,
    CACHE_OP_FLUSHINV = 5,
    NOP = 127
}

enum ActKernelPerfCounter : byte {
    /// Stall cycle
    STALL_CYCLE_CNT_EN = 0,
    /// Executed instruction
    EXEC_INST_CNT_EN = 1,
    /// Clock cycle
    CLK_CYCLE_CNT_EN = 2,
    /// Branch taken
    BRANCH_TAKEN_CNT_EN = 3,
    /// Instruction breakpoint 0
    INST_BRKP0_CNT_EN = 4,
    /// Instruction breakpoint 1
    INST_BRKP1_CNT_EN = 5,
    /// Data breakpoint 0
    DATA_BRKP0_CNT_EN = 6,
    /// Data breakpoint 1
    DATA_BRKP1_CNT_EN = 7,
    /// Go counter of Operation Control Register
    GO_COUNT_EN = 8,
    /// LSU0 read transaction byte
    LSU0_RBYTE_CNT_EN = 9,
    /// LSU0 write transaction byte
    LSU0_WBYTE_CNT_EN = 10,
    /// LSU1 read transaction byte
    LSU1_RBYTE_CNT_EN = 11,
    /// LSU1 write transaction byte
    LSU1_WBYTE_CNT_EN = 12
}

enum ActKernelPerfStallFilter : byte {
    /// SW Interrupt Handler
    SWIH = 16,
    /// Other interrupts
    OTHER_INTR = 17,
    /// LSU0 Stall (waiting for data)
    LSU0_DATA_WAIT = 18,
    /// LSU1 Stall (waiting for data)
    LSU1_DATA_WAIT = 19,
    /// LSU0 Access Stall
    LSU0_ACCESS = 20,
    /// LSU1 Access Stall
    LSU1_ACCESS = 21,
    /// Instruction buffer Low Stall
    INST_BUFF_LOW = 22,
    /// Discontinuity Fetch Stall
    DISC_FETCH = 23,
    /// Discontinuity Decode Stall (too much data in instruction buffer at end of delay slots)
    DISC_DEC = 24,
    /// Discontinuity Starve Stall
    DISC_STARVE = 25,
    /// Instruction buffer Low during discontinuity
    INST_BUFF_LOW_DISC = 26
}
table PerfPayloadConfig {
  /// Use the Free Running Counter
  /// 64b
  frcTimestamp: bool = false;
  /// Duration measured by FRC
  /// 32b
  frcDuration: bool = false;
  counters: [ActKernelPerfCounter];

  /// Only applicable if STALL_CYCLE_CNT_EN is set as one of the counter targets in `counters`.
  /// This setting is ignored otherwise.
  stallFilters : [ActKernelPerfStallFilter];
}

table ActKernel {
  /// This object describes the activation kernel itself. It specifies the size and entry
  /// of the kernel binary, the size of the `.data` section of all associated invocations,
  /// and a type specifier intended for future extensibility.
  /// A type identifier allowing for the FW to do special handling of work items
  type: ActKernelType;

  /// the a monolithic kernel's .text section
  kernelText: KernelDataReference;

  /// the entry point of the kernel given as an offset from the start of the
  /// kernelText's address
  kernelEntry: uint32;
  globalArgs: KernelDataReference;

  // Fields for Uniquely identifying a Kernel (for version tracking)
  kernelIdentifier: string;
  kernelVersionMajor: uint16;
  kernelVersionMinor: uint16;
}

table ActKernelInvocation{
  /// This object exists to differentiate invocations of the kernel range.
  /// Unlike DPU Tasks, the ActKernelTask and associated Kernel are not bound by barriers
  associatedBarriers: BarrierReference;

  /// the monolithic kernel's .data section. There is a unique data section per-invocation
  dataSection: KernelDataReference;

  /// This buffer represents the generic input arguments that all Activation SHAVE kernels share.
  invocationArgs: KernelDataReference;
  /// In short; this parameter determines _if_ performance data is captured this Invocation
  perfPayload: KernelDataReference;
  tile: uint32;
  /// Adding fields to the schema such that we can use the tensor dimensions for for performance predictions.
  inputTensors: [TensorReference];
  outputTensors: [TensorReference];
}

table ActKernelTask {
  /// This object describes an entire Activation SHAVE Kernel ND Range and
  /// the configurations for the kernel invocations within that range.
  kernel: ActKernel;
  invocations: [ActKernelInvocation];
  layerType: string;
}

table ActKernelRuntime {
  /// This object exist to accommodate the firmware to execute the Activation SHAVE Runtime

  /// The schedule allocated space used for the runtime stack(s).
  shaveStacks: [KernelDataReference];
  kernel: ActKernel;
  perfConfig: PerfPayloadConfig;
  codeScratchBuffer: KernelDataReference;
}
