Skip to content

Instantly share code, notes, and snippets.

@patrickschur
Last active August 28, 2023 17:25
Show Gist options
  • Save patrickschur/4645f81601fe6f8ff16c7a90e848d7cb to your computer and use it in GitHub Desktop.
Save patrickschur/4645f81601fe6f8ff16c7a90e848d7cb to your computer and use it in GitHub Desktop.
Reads the PMTable on Cezanne
import os
import struct
import sys
SMN_INDEX_REG = 0x60
SMN_DATA_REG = 0x64
SMN_MSG_REG = 0x3b10a20
SMN_RSP_REG = 0x3b10a80
SMN_ARG_REG = 0x3b10a88
SMN_STATUS_OK = 1
SMN_CMD_TRANSFER_TABLE = 0x65
SMN_CMD_GET_TABLE_BASE_ADDRESS = 0x66
SMN_CMD_GET_TABLE_VERSION = 0x6
config = os.open('/sys/devices/pci0000:00/0000:00:00.0/config', os.O_RDWR)
def smn_read(address):
os.pwrite(config, struct.pack('I', address), SMN_INDEX_REG)
return struct.unpack('I', os.pread(config, 4, SMN_DATA_REG))[0]
def smn_write(address, data):
os.pwrite(config, struct.pack('I', address), SMN_INDEX_REG)
os.pwrite(config, struct.pack('I', data), SMN_DATA_REG)
def smn_send_msg(msg, *args):
status = 0
smn_write(SMN_RSP_REG, status)
for j, arg in enumerate(args):
smn_write(SMN_ARG_REG + (j << 2), arg)
smn_write(SMN_MSG_REG, msg)
while (status := smn_read(SMN_RSP_REG)) == 0:
pass
if status != SMN_STATUS_OK:
return []
return [smn_read(SMN_ARG_REG + (k << 2)) for k in range(5)]
base_address = smn_send_msg(SMN_CMD_GET_TABLE_BASE_ADDRESS, 1, 1)
table_base_address = base_address[1] << 32 | base_address[0]
print(f'base address (high): {hex(base_address[1])}, (low): {hex(base_address[0])}')
table_version = smn_send_msg(SMN_CMD_GET_TABLE_VERSION)[0]
print(f'table version: {hex(table_version)}')
print(f'table base address: {hex(table_base_address)}')
print(f'transfer table: {smn_send_msg(SMN_CMD_TRANSFER_TABLE)}')
devmem = os.open('/dev/mem', os.O_RDONLY)
os.lseek(devmem, table_base_address, os.SEEK_SET)
for i in range(0, 0xAA4, 4):
print(f"{i:04X}: {struct.unpack('f', os.read(devmem, 4))[0]}")
os.close(devmem)
os.close(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment