How to access PCI / MCH bar in linux - c

The background is based on Intel EDS document (example_doc)
see Section 5 ~ 5.2
I was trying to access PCI configuration space to get the CPU vendor ID,
and then accessing the MCH Bar to read out some information under OS base, not developing the driver.
I had search for few days, but most of information are driver related, or using linux kernel library such as pci.h, which is not I prefer.
My Process:
get mcfg base address from /sys/firmware/acpi/tables/MCFG
calculate actual address with bus0, dev0, func0, reg0 (ie. lspci 0000:00:00.0)
read data from the address
Code in C:
BOOLEAN IoConfigRead(uint32_t bus, uint32_t dev, uint32_t func, uint32_t reg, void* Buffer, unsigned long length){
static uint32_t pcieBar = 0;
int fd,fd1;
int sz;
uint32_t buffer[12];
uint32_t addr;
uint32_t *p;
uint32_t value;
if(pcieBar == 0){
fd = open("/sys/firmware/acpi/tables/MCFG",0);
if(fd == -1){
printf("Error opening /sys/firmware/acpi/tables/MCFG");
exit(EXIT_FAILURE);
}
sz = read(fd, buffer, 48);
if (sz != 48) {
printf("couldn't read 48 bytes from MCFG, sz=%d\n", sz);
exit(EXIT_FAILURE);
}
if (close(fd)) {
perror("Error closing /sys/firmware/acpi/tables/MCFG");
exit(EXIT_FAILURE);
}
if (buffer[0] != 0x4746434d) {
printf("MCFG signature not found\n");
exit(EXIT_FAILURE);
}
pcieBar = buffer[11];
}
addr = pcieBar;
addr |= (((DWORD)(func & 0x7)) << 12);
addr |= (((DWORD)(dev & 0x1f)) << 15);
addr |= ((bus & 0xff) << 20);
fd1 = open("/dev/mem", O_RDWR|O_DSYNC);
if (fd1 == -1) {
perror("Error opening /dev/mem");
return EXIT_FAILURE;
}
p = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd1, addr);
if(p == MAP_FAILED){
printf("Failed to mmap address,0x%x\n",addr);
}
else{
int i = 0;
while (i < length/4){
((uint32_t*)Buffer)[i] = p[(reg/4)+i];
i++;
}
munmap(p,4096);
close(fd1);
}
return 0;
}
Error Msg:
Failed to mmap address,0xe0000000
Base on the acpi table the base address is
at byte 0xc(12) which is 0xe0000000 but I'm not able to use mmap to allocate it correctly.
This code seems to be workable in the past, but I'm not able to make it work.
I'm not sure what I had missed. It's already run under sudo, maybe it need some special permission?
I had tried it on kernel: 5.3.18 / 4.12.14-23 neither of them worked.
gcc version : 7.5 / 7.3

Related

mmap file not syncing

Hello I am trying to back up a vector by mmap.
However, I have tried msync then munmap but it doesn't work. After I write to the (char *) then munmap the file, the file has no content. The mmap file is also created with flag MAP_SHARED. Would really appreciate it if anyone can help.
//update file descriptor
if ((fd = open(filename.c_str(), O_RDWR | S_IRWXU)) < 0) { //| O_CREAT
printf("ERROR opening file %s for writing", filename.c_str());
exit(1);
}
//lseek create a file large enough
off_t i = lseek(fd, frontier_size * URL_MAX_SIZE, SEEK_SET);
if (i != frontier_size * URL_MAX_SIZE) {
cout << "failed to seek";
}
//reposition and write 3 bytes to the file else will failed to read
char buff[3] = "ta";
ssize_t kk = lseek(fd, 0, SEEK_SET);
if (kk < 0) {
cout << "failed to reposition";
}
ssize_t temp_write = write(fd, (void *)& buff, 2);
if (temp_write < 0) {
cout << "failed to write";
cout << temp_write;
}
//reposition to begining
ssize_t k = lseek(fd, 0, SEEK_SET);
if (k < 0) {
cout << "failed to reposition";
}
char * map = (char *)mmap(0, frontier_size * URL_MAX_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
printf("failed mmap");
exit(1);
}
mmap_frontier = map;
//write to frontier
for (int i = 0; i < frontier.size(); ++i) {
strcpy(mmap_frontier, frontier[i].c_str());
mmap_frontier += URL_MAX_SIZE;
}
mmap_frontier -= frontier.size() * URL_MAX_SIZE;
ssize_t k = lseek(fd, 0, SEEK_SET);
if (k < 0) {
cout << "failed to reposition";
}
int sync = msync((void *)0, frontier.size() * URL_MAX_SIZE, MS_ASYNC);
if (sync < 0 ) {
cout << "failed to sync";
}
int unmap = munmap((void *)0, frontier.size() * URL_MAX_SIZE);
if (unmap < 0) {
cout << "failed to unmap";
}
There are quite a few problems with your code, and with the question:
S_IRWXU is the 3rd argument to open(), not a flag for the 2nd parameter.
mmap() won't work correctly if the file is too small. You can use ftruncte() to set the file size correctly. You tried to seek past the total size of the mapping and write a couple of bytes ("ta"), but before doing that you issued the seek lseek(fd, 0, SEEK_SET) which means the file size was set to 3 rather than mapping_size+3.
You're not backing the vector with an mmapped file, the vector has nothing to do with it, the vector uses its own memory that isn't related in any way to this mapping (please edit your question...).
You called msync() with the address (void *)0, so the actual address which needs to be synced, map, is not being synced.
Likewise, you called munmap() with the address (void *)0, so the actual address which needs to be unmapped is not being unmapped.
You called msync() with MS_ASYNC, which means there's no guarantee that the sync happens before you read the file's contents.
Here's what's working for me (error handling omitted for brevity):
unsigned frontier_size = 2;
const unsigned URL_MAX_SIZE = 100;
int fd = open("data", O_RDWR);
loff_t size = frontier_size * URL_MAX_SIZE;
ftruncate(fd, size);
char *map = (char *)mmap(0, size, PROT_WRITE, MAP_SHARED, fd, 0);
strcpy(map, "hello there");
msync(map, size, MS_SYNC);
munmap(map, size);
close(fd);

Why am I receiving 0 bytes of data from usb output from libusb bulk transfer?

I pulse a USB camera with a 5v pulse and it takes a picture. Pulsing the camera then sends a USB signal back to a raspberry pi. I'm writing a program to collect the images sent over USB. Below is my code for the function to begin taking input from the camera after it is triggered.
void opendevice()
{
libusb_device_handle* dev;
struct libusb_device_descriptor* desc;
int r;
int err;
int transfered;
libusb_init(NULL);
dev = libusb_open_device_with_vid_pid( NULL, 0x2a0b, 0x00f8);
if (dev == NULL)
{
printf("device not found\n");
}
if(libusb_kernel_driver_active(dev, 0) == 1)
{
printf("Driver active\n");
if(libusb_detach_kernel_driver(dev, 0) == 0)
{
printf("Kernel Driver Detached\n");
}
}
libusb_set_configuration(dev,1);
err = libusb_claim_interface(dev, 0);
if(err != 0)
{
printf("cannot claim interface\n");
}
unsigned char usb_data[4];
int size = sizeof(unsigned int) *1280 *960;
unsigned *data;
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data, sizeof(data), &transfered, 0);
if(r == 0)
{
printf("data has been transfered\n");
}
else{
printf("error code %d\n", r);
printf("bytes transfered %d\n", transfered);
}
}
I locate the device detach check to see if the kernel is using it then if it is I detach it. After detaching it I claim the interface then wait for a transfer to happen inside of bulk transfer. However I never receive data from the transfer. Even when unplugging the camera from the usb port r returns value -1 and my transfer is of size 0. Can anyone tell me what I am missing here?
Let me know if you need any more information.
Your code has a buffer overflow:
unsigned char usb_data[4]; // <---
int size = sizeof(unsigned int) *1280 *960;
unsigned *data; // <-- not used!
data = (unsigned int *)malloc(size);
r = libusb_bulk_transfer(dev, LIBUSB_ENDPOINT_IN | 0x83, usb_data //<
,sizeof(data), &transfered, 0)
You probably wanted to use "data" as receiving buffer in the libusb_bulk_transfer() call, but you actually used usb_data which is only 4 bytes long.

Issues mmaping the same file twice

I'm using a Raspberry Pi B+, and I'm trying to mmap two different sections of /dev/mem - the first to be able to set two pins' functions from location 0x2020 0004 (0x04 bytes long), the other to manipulate the BSC Slave functions on the BCM2835 chip on the Pi from location 0x2021 4000 (0x1C bytes long).
static uint32_t * initMapMem(int fd, uint32_t addr, uint32_t len)
{
return (uint32_t *) mmap((void*)0x0, len,
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_SHARED|MAP_LOCKED,
fd, addr);
}
int initialise(void) {
int fd;
fd = open("/dev/mem", O_RDWR | O_SYNC) ;
if (fd < 0)
{
fprintf(stderr, "This program needs root privileges. Try using sudo.\n");
return 1;
}
pinReg = initMapMem(fd, 0x20200004, 0x4);
bscReg = initMapMem(fd, 0x20214000, 0x1C);
close(fd);
if (bscReg == MAP_FAILED)
{
fprintf(stderr, "Bad, mmap failed.\n");
return 1;
}
if (pinReg == MAP_FAILED)
{
fprintf(stderr, "Bad, mmap failed.\n");
return 1;
}
return 0;
}
initialise() is called out of main(). Stepping through the program with gdb I find that bscReg gets positioned right, but pinReg returns as MAP_FAILED (aka 0xFFFFFFFF) with errno set to EINVAL. Doesn't matter which way it's done, either - pinReg always finds itself as MAP_FAILED when mmaped first or second.
How do I get pinReg to a valid value?
The first mmap() is failing because the offset you're trying to map (0x20200004) isn't page-aligned. Create a mapping at 0x20200000 with a size of at least 8, then write to it at an offset of 0x4.

read()/ioctl disturbs GPIO signal?

I connect a Linux embedded board(based on imx233) and a MSP430 MCU. They are connected via 4 pin SPI, but I use a GPIO for the chip select purpose on the Linux board. What I do is to use poll to detect falling edge of the GPIO(nr 52) then perform SPI reading either ioctl or read()
int main(void)
{
/********************************LINUX SCHEDULING**********************************/
sp.sched_priority = sched_get_priority_max(SCHED_FIFO); //scheduling
sched_setscheduler(0, SCHED_FIFO, &sp); //scheduling
/********************************LINUX SCHEDULING_END******************************/
struct pollfd fdset[2]; //declare the poll to be used in interrupt catching
int nfds = 2;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF]; //max=64byte
int len;
initialize(); //gpio's are set to SPI_SLAVE
// spi_init();
gpio_fd = gpio_fd_open(CHIP_SELECT_PIN); //the CS(SS) pin is opened
timeout = POLL_TIMEOUT; //timeout 3 sec is set
// uint8_t voidFirstDetection = 1;
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = NULL;
fdset[0].events = POLLIN;
fdset[1].fd = gpio_fd;
fdset[1].events = POLLPRI;
/*** POLL starts to detect chipselects****/
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) {
printf(".");
}
if (fdset[1].revents & POLLPRI ) { //HERE I need to run SPI_read
len = read(fdset[1].fd, buf, MAX_BUF);
/* if(voidFirstDetection){
voidFirstDetection = 0;
}else{*/
printf("\npoll() GPIO %d interrupt occurred\n", CHIP_SELECT_PIN);
int fd = open(device, O_RDWR);
if (fd < 0){
// snprintf(systemlogmsg, sizeof(systemlogmsg), "[1181]: errno:%s Cannot open /dev/spidev ", strerror(errno));
// error_logging(systemlogmsg, LOGLEVEL_ERROR);
printf("error spi recive\n");
}
//spi_transfer(fd);
do_read(fd);
close(fd);
// }
}
}
gpio_fd_close(gpio_fd);
return 0;
}
Above code works fine that it generates an interrupt only at the falling edge of the signal. I use the either of the below code when the interrupt is detected to read the /dev/spidev1-0
static void do_read(int fd)
{
unsigned char buf[1], *bp;
int status;
int len = 1;
/* read at least 2 bytes, no more than 32 */
memset(buf, 0, sizeof buf);
status = read(fd, buf, len);
if (status < 0) {
perror("read");
return;
}
if (status != len) {
fprintf(stderr, "short read\n");
return;
}
printf("read(%2d, %2d): %02x %02x,", len, status,
buf[0], buf[1]);
status -= 2;
bp = buf + 2;
while (status-- > 0)
printf(" %02x", *bp++);
printf("\n");
}
static void spi_transfer(int fd)
{
int ret;
uint8_t tx[2];
uint8_t rx[3] = {0 };
struct spi_ioc_transfer tr = {
.tx_buf = 0,
.rx_buf = (unsigned long)rx,
.len = ARRAY_SIZE(tx),
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1){
printf("can't send spi message");
exit(1);
}
for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
if (!(ret % 6))
puts("");
printf("%.2X ", rx[ret]);
}
puts("");
}
Whenever the either ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); line on spi_transfer() or status = read(fd, buf, len); on do_read() is executed, I see an infinite loop that detects an interrupt on the GPIO52 (chipselect). I try the observe the GPIO via oscilloscope but I could not see any signal change (it might be a spike that my oscilloscope cannot detect), however, when I connect the chipselect to the Vcc, it does not get the infinite loop. As I am on the early stage, I set one of GPIO of the MCU as an output and a constant logic high. I use GPIO52 (Chip select) as an input because my aim is to transfer data from MCU to the linux board.
I guess, the read() and ioctl somehow effects the GPIO to sink more current than the GPIO can provide. If it is the problem, what can I do that ioctl or read() would not disturb GPIO. Or do you think something else could be a problem?
I was lucky that I found the problem quick. I tied the grounds of both boards and now it works fine. I will keep the post as someone else might have the same problem. But I am still curious how ioctl or read disturbs the GPIO signal level

Accessing system resources from ruby

I'm writing a ruby wrapper for Hitachi-44780 library for Raspberry Pi. The library itself is written in C.
I have the following code that tries to open /dev/mem
void setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit (-1);
}
// Allocate MAP block
if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
printf("allocation error \n");
exit (-1);
}
// Make sure pointer is on 4K boundary
if ((unsigned long)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
// Now map it
gpio_map = (unsigned char *)mmap(
(caddr_t)gpio_mem,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
mem_fd,
GPIO_BASE
);
if ((long)gpio_map < 0) {
printf("mmap error %d\n", (int)gpio_map);
exit (-1);
}
gpio = (volatile unsigned *)gpio_map;
}
The question is how can i use the gem without being a superuser? Now it fails when the memory is opened.

Resources