Skip to content

Commit

Permalink
Handle CRS during enumeration
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Forencich <alex@alexforencich.com>
  • Loading branch information
alexforencich committed Jun 24, 2023
1 parent 249970c commit 004b618
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cocotbext/pcie/core/pci.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import struct

from cocotb.triggers import Timer

from .caps import PciCapId, PciExtCapId
from .utils import PcieId, align

Expand Down Expand Up @@ -119,6 +121,27 @@ def only_one_child(self):
return True
return False

async def wait_crs(self, dev_id, timeout=1000, timeout_unit='ns'):
delay = 10
val = 0xffff0001
while val == 0xffff0001:
if delay > 10000:
self.rc.log.warning("pci %s: not ready after %d us; giving up", dev_id, delay)
return False

if delay > 1000:
self.rc.log.info("pci %s: not ready after %d us; waiting", dev_id, delay)

await Timer(delay, 'us')
delay *= 2

val = await self.rc.config_read_dword(dev_id, 0x000, 'little', timeout, timeout_unit)

if delay > 1000:
self.rc.log.info("pci %s: ready after %d us", dev_id, delay)

return True

async def scan(self, available_buses=0, timeout=1000, timeout_unit='ns'):
first_bus = self.bus_num
last_bus = first_bus
Expand All @@ -138,6 +161,10 @@ async def scan(self, available_buses=0, timeout=1000, timeout_unit='ns'):
if val in {0, 0xffffffff, 0xffff0000, 0x0000ffff}:
continue

if val == 0xffff0001:
if not await self.wait_crs(dev_id, timeout, timeout_unit):
continue

# valid vendor ID
self.rc.log.info("Found device at %s", dev_id)

Expand Down
7 changes: 7 additions & 0 deletions tests/pcie_us/test_pcie_us.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,13 @@ async def run_test_crs(dut, idle_inserter=None, backpressure_inserter=None):
await FallingEdge(dut.user_reset)
await Timer(100, 'ns')

async def toggle_config_space_enable(dut):
dut.cfg_config_space_enable.setimmediatevalue(0)
await Timer(100, 'us')
dut.cfg_config_space_enable.setimmediatevalue(1)

cocotb.start_soon(toggle_config_space_enable(dut))

await tb.rc.enumerate()

dev = tb.rc.find_device(tb.dev.functions[0].pcie_id)
Expand Down
7 changes: 7 additions & 0 deletions tests/pcie_usp/test_pcie_usp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,13 @@ async def run_test_crs(dut, idle_inserter=None, backpressure_inserter=None):
await FallingEdge(dut.user_reset)
await Timer(100, 'ns')

async def toggle_config_space_enable(dut):
dut.cfg_config_space_enable.setimmediatevalue(0)
await Timer(100, 'us')
dut.cfg_config_space_enable.setimmediatevalue(1)

cocotb.start_soon(toggle_config_space_enable(dut))

await tb.rc.enumerate()

dev = tb.rc.find_device(tb.dev.functions[0].pcie_id)
Expand Down

0 comments on commit 004b618

Please sign in to comment.