Fork for kernel 5.18 API change.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.3 KiB

4 years ago
#include "SonoPcieDevice.h"
#include <sys/mman.h>
#include <fcntl.h>
SonoPcieDevice::SonoPcieDevice()
{
bars[0].size = 32 MB;
bars[1].size = 16 MB;
bars[2].size = 16 KB;
for(auto i = 0; i < TOTAL_DMA_BUFFER_NUM; i++)
{
dmaBuffers[i].size = DMA_SIZE;
}
}
void SonoPcieDevice::init()
{
initBars();
initDmaBuffers();
}
void SonoPcieDevice::initBars()
{
int fd = open("/proc/sono_bars", O_RDWR | O_SYNC);
if(fd == -1)
{
SonoException exp("Could not open bar proc file");
throw exp;
}
for(auto i = 0; i < TOTAL_BAR_NUM; i++)
{
bars[i].ptr = mmap(0, bars[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, i << 12);
}
}
void SonoPcieDevice::initDmaBuffers()
{
int fd = open("/proc/sono_buffers", O_RDWR | O_SYNC);
if(fd == -1)
{
SonoException exp("Could not open buffers proc file");
throw exp;
}
for(auto i = 0; i < TOTAL_DMA_BUFFER_NUM; i++)
{
dmaBuffers[i].ptr = mmap(0, dmaBuffers[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, i << 12);
}
}
void SonoPcieDevice::writeByte(uint32_t bar, uint8_t data, uint32_t offset)
{
uint8_t* temp = (uint8_t*)bars[bar].ptr;
temp[offset] = data;
}
void SonoPcieDevice::writeShort(uint32_t bar, uint16_t data, uint32_t offset)
{
uint16_t* temp = (uint16_t*)bars[bar].ptr;
temp[offset / 2] = data;
}
void SonoPcieDevice::writeWord(uint32_t bar, uint32_t data, uint32_t offset)
{
uint32_t* temp = (uint32_t*)bars[bar].ptr;
temp[offset / 4] = data;
}
void SonoPcieDevice::writeLong(uint32_t bar, uint64_t data, uint32_t offset)
{
uint64_t* temp = (uint64_t*)bars[bar].ptr;
temp[offset / 8] = data;
}
uint8_t SonoPcieDevice::readByte(uint32_t bar, uint32_t offset)
{
uint8_t* temp = (uint8_t*)bars[bar].ptr;
return temp[offset];
}
uint16_t SonoPcieDevice::readShort(uint32_t bar, uint32_t offset)
{
uint16_t* temp = (uint16_t*)bars[bar].ptr;
return temp[offset / 2];
}
uint32_t SonoPcieDevice::readWord(uint32_t bar, uint32_t offset)
{
uint32_t* temp = (uint32_t*)bars[bar].ptr;
return temp[offset / 4];
}
uint64_t SonoPcieDevice::readLong(uint32_t bar, uint32_t offset)
{
uint64_t* temp = (uint64_t*)bars[bar].ptr;
return temp[offset / 8];
}
uint8_t* SonoPcieDevice::getBufferPtr(uint32_t buffer)
{
return (uint8_t*)dmaBuffers[buffer].ptr;
}