#!/bin/bash

#### step 1
#PCIE_INTD_ENABLE_REG_SYS_0 [Bit 0-5]
#enables set for sys_en_pcie_downstream_0-5
echo "Enabling downstream interrupt - one time set up"
devmem2 0x2910100 w 0x3f > /dev/null


#### step 2
#PCIE_eoi_vector
#EOI vector for level interrupts.
#Writing the EOI value as specfied to this
#register will re-trigger a pending interrupt.
#0 - Downstream interrupt, 1 - FLR interrupt,
#2 - Legacy interrupt, 3 - Power state interrupt
#In software, at the end of interrupt, write to this register to signify end of interrupt
echo "Initializing PCIE_eoi_vector in case of first time run. Should be set at end of interrupt."
devmem2 0x29170c8 w 0x0 > /dev/null


#### step 3
#PCIE_CORE_PFn_I_VENDOR_SPECIFIC_CONTROL_REG
#Sets HTI bit that drives output pins Fx_VSEC_INTERRUPT_OUT
#where x is the function number
#Can be read and written by the host through a config transaction
#or via the local management interface
#For example, "setpci -s 0001:01:00.0 ECAP_VNDR+8.l=100" will set HTI bit of PCIE_CORE_PFn_I_VENDOR_SPECIFIC_CONTROL_REG 
#the register is part of the Extended Capability (ECAP) Vendor (VNDR) specific register.
echo "Triggering all 6 downstream interrupts."
devmem2 0x0d800408 w 0x100 > /dev/null
devmem2 0x0d801408 w 0x100 > /dev/null
devmem2 0x0d802408 w 0x100 > /dev/null
devmem2 0x0d803408 w 0x100 > /dev/null
devmem2 0x0d804408 w 0x100 > /dev/null
devmem2 0x0d805408 w 0x100 > /dev/null


#### step 4 (optional)
echo "Interrupt status is:"
#PCIE_status_reg_sys_x
#status for sys_en_pcie_downstream_0-5
devmem2 0x2910500 | sed 's/.*://' | sed '2d'


#### step 5
echo "Clearing all 6 downstream interrupts"
#clearing a PCIE_CORE_PFn_I_VENDOR_SPECIFIC_CONTROL_REG
devmem2 0x0d800408 w 0x0 > /dev/null
devmem2 0x0d801408 w 0x0 > /dev/null
devmem2 0x0d802408 w 0x0 > /dev/null
devmem2 0x0d803408 w 0x0 > /dev/null
devmem2 0x0d804408 w 0x0 > /dev/null
devmem2 0x0d805408 w 0x0 > /dev/null


#### step 6 (optional)
echo "Reprinting status:"
#re-printing status
devmem2 0x2910500 | sed 's/.*://' | sed '2d'


#### step 7
#Reset PCIE_eoi_vector 
echo "Writing to PCIE_eoi_vector again to simulate interrupt handler writing to EOI register at end of interrupt."
devmem2 0x29170c8 w 0x0 > /dev/null

