-
-
Save corsix/cdc676f08bb26ddb858d45bd8a2062fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/ioctl.h> | |
#include <sys/mman.h> | |
#define FATAL(fmt, ...) do {fprintf(stderr, fmt " (%s:%d)\n",##__VA_ARGS__,__FILE__,__LINE__); exit(1);} while(0) | |
#define ASSERT(cond) if (cond) {} else FATAL("Assertion failed: %s", #cond) | |
#define TENSTORRENT_IOCTL_QUERY_MAPPINGS 0xFA02 | |
struct tenstorrent_mapping { | |
uint32_t mapping_id; | |
uint32_t reserved; | |
uint64_t mapping_base; | |
uint64_t mapping_size; | |
}; | |
#define TENSTORRENT_MAPPING_RESOURCE0_UC 1 | |
#define TENSTORRENT_MAPPING_RESOURCE0_WC 2 | |
#define TENSTORRENT_MAPPING_RESOURCE2_UC 5 | |
#define BAR0_WC_SIZE (464 << 20) | |
#define BAR0_SIZE (496 << 20) | |
#define MMAP_SIZE (512 << 20) | |
#define BAR4_SOC_TARGET_ADDRESS 0x1E000000 | |
#define TLB_CONFIG_ADDR 0x1FC00000 | |
#define TLB_IDX_UC0 184 | |
#define TLB_CFG_UNICAST(x, y) (((y) << 6) + (x)) | |
#define TLB_CFG_MULTICAST(x_start, y_start, x_end, y_end) ((1 << 25) + ((y_start) << 18) + ((x_start) << 12) + ((y_end) << 6) + (x_end)) | |
#define TLB_CFG_NOC1 (1 << 24) | |
static char* set_tlb(char* dev, uint32_t idx, uint64_t cfg, uint32_t suitable_for_addr) { | |
char* result = dev; | |
uint32_t abits; | |
if (idx < 156) { | |
abits = 20; | |
result += (idx << 20); | |
} else if (idx < 166) { | |
abits = 21; | |
result += (156 << 20) + ((idx - 156) << 21); | |
} else { | |
abits = 24; | |
result += (156 << 20) + (10 << 21) + ((idx - 166) << 24); | |
} | |
cfg = (cfg << (36 - abits)) + (suitable_for_addr >>= abits); | |
((volatile uint64_t*)(dev + TLB_CONFIG_ADDR))[idx] = cfg; | |
return result - (suitable_for_addr << abits); | |
} | |
#define RV_ADDR_NOC0_MC_DISABLE_ROW 0xFFB20108 | |
#define RV_ADDR_NOC0_MC_DISABLE_COL 0xFFB20110 | |
int main() { | |
int fd = open("/dev/tenstorrent/0", O_RDWR | O_CLOEXEC); | |
ASSERT(fd >= 0); | |
unsigned char resource_to_mapping[8] = {0}; | |
struct tenstorrent_mapping mappings[sizeof(resource_to_mapping) + 1]; | |
mappings[0].mapping_size = sizeof(resource_to_mapping); | |
ASSERT(ioctl(fd, TENSTORRENT_IOCTL_QUERY_MAPPINGS, &mappings[0].mapping_size) >= 0); | |
mappings[0].mapping_size = 0; | |
for (unsigned i = 1; i <= sizeof(resource_to_mapping); ++i) { | |
uint32_t resource = mappings[i].mapping_id; | |
if (resource < sizeof(resource_to_mapping)) { | |
resource_to_mapping[resource] = i; | |
} | |
} | |
struct tenstorrent_mapping* bar0uc = mappings + resource_to_mapping[TENSTORRENT_MAPPING_RESOURCE0_UC]; | |
struct tenstorrent_mapping* bar0wc = mappings + resource_to_mapping[TENSTORRENT_MAPPING_RESOURCE0_WC]; | |
struct tenstorrent_mapping* bar4uc = mappings + resource_to_mapping[TENSTORRENT_MAPPING_RESOURCE2_UC]; | |
ASSERT(bar0uc->mapping_size >= BAR0_SIZE); | |
ASSERT(bar4uc->mapping_size >= MMAP_SIZE - BAR4_SOC_TARGET_ADDRESS); | |
char* dev = mmap(NULL, MMAP_SIZE, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
ASSERT(dev != MAP_FAILED); | |
uint32_t wc_size = bar0wc->mapping_size; | |
if (wc_size) { | |
if (wc_size > BAR0_WC_SIZE) { | |
wc_size = BAR0_WC_SIZE; | |
} | |
if (mmap(dev, wc_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, bar0wc->mapping_base) == MAP_FAILED) { | |
wc_size = 0; | |
} | |
} | |
ASSERT(mmap(dev + wc_size, BAR0_SIZE - wc_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, bar0uc->mapping_base + wc_size) != MAP_FAILED); | |
ASSERT(mmap(dev + BAR0_SIZE, MMAP_SIZE - BAR0_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, bar4uc->mapping_base + (BAR0_SIZE - BAR4_SOC_TARGET_ADDRESS)) != MAP_FAILED); | |
char* reg_tlb = set_tlb(dev, TLB_IDX_UC0, TLB_CFG_UNICAST(1, 0), RV_ADDR_NOC0_MC_DISABLE_ROW); | |
printf("%u, %u\n", | |
*(volatile uint32_t*)(reg_tlb + RV_ADDR_NOC0_MC_DISABLE_ROW), | |
*(volatile uint32_t*)(reg_tlb + RV_ADDR_NOC0_MC_DISABLE_COL)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment