Skip to content

[pcimem]: Add pcimem Utility #12125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions rules/pcimem.dep
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SPATH := $($(SONIC_PCIMEM)_SRC_PATH)
DEP_FILES := $(SONIC_COMMON_FILES_LIST) rules/pcimem.mk rules/pcimem.dep
DEP_FILES += $(SONIC_COMMON_BASE_FILES_LIST)
DEP_FILES += $(shell git ls-files $(SPATH))

$(SONIC_PCIMEM)_CACHE_MODE := GIT_CONTENT_SHA
$(SONIC_PCIMEM)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST)
$(SONIC_PCIMEM)_DEP_FILES := $(DEP_FILES)

8 changes: 8 additions & 0 deletions rules/pcimem.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# pcimem package

SONIC_PCIMEM_VERSION = 1.0.0
SONIC_PCIMEM_PKG_NAME = pcimem

SONIC_PCIMEM = $(SONIC_PCIMEM_PKG_NAME)_$(SONIC_PCIMEM_VERSION)_$(CONFIGURED_ARCH).deb
$(SONIC_PCIMEM)_SRC_PATH = $(SRC_PATH)/$(SONIC_PCIMEM_PKG_NAME)
SONIC_DPKG_DEBS += $(SONIC_PCIMEM)
32 changes: 32 additions & 0 deletions src/pcimem/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
RM := rm -rf
PCIMEM_TARGET := pcimem
CP := cp
MKDIR := mkdir
CC := gcc
MV := mv

-include src/subdir.mk

ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif

all: pcimem-build

pcimem-build: $(OBJS)
$(CC) -o "$(PCIMEM_TARGET)" $(OBJS)

install:
$(MKDIR) -p $(DESTDIR)/usr/sbin
$(MV) $(PCIMEM_TARGET) $(DESTDIR)/usr/sbin

deinstall:
$(RM) $(DESTDIR)/usr/sbin/$(PCIMEM_TARGET)
$(RM) -rf $(DESTDIR)/usr/sbin

clean:
-$(RM) $(EXECUTABLES)$(OBJS)$(C_DEPS) $(PCIMEM_TARGET)

.PHONY: all clean dependents
6 changes: 6 additions & 0 deletions src/pcimem/debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pcimem (1.0.0) stable; urgency=medium

* Initial release.

-- Ashwin H <[email protected]> Tue, 20 Sep 2022 13:55:00 +0530

1 change: 1 addition & 0 deletions src/pcimem/debian/compat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
13 changes: 13 additions & 0 deletions src/pcimem/debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Source: pcimem
Section: devel
Priority: optional
Maintainer: Ashwin H <[email protected]>
Build-Depends: debhelper (>= 8.0.0),
Standards-Version: 3.9.3
Homepage: https://github.com/sonic-net/sonic-buildimage
XS-Go-Import-Path: github.com/sonic-net/sonic-buildimage

Package: pcimem
Architecture: any
Built-Using: ${misc:Built-Using}
description: program to read/write from/to a pci device.
3 changes: 3 additions & 0 deletions src/pcimem/debian/rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/make -f
%:
dh $@
339 changes: 339 additions & 0 deletions src/pcimem/src/LICENSE.txt

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions src/pcimem/src/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
== Overview ==

The pcimem application provides a simple method of reading and writing
to memory registers on a PCI card.

Usage: ./pcimem { sys file } { offset } [ type [ data ] ]
sys file: sysfs file for the pci resource to act on
offset : offset into pci memory region to act upon
type : access operation type : [b]yte, [h]alfword, [w]ord, [d]ouble-word
data : data to be written

== Platform Support ==

WARNING !! This method is platform dependent and may not work on your
particular target architecture. Refer to the PowerPC section below.

== Example ==

bash# ./pcimem /sys/devices/pci0001\:00/0001\:00\:07.0/resource0 0 w
/sys/devices/pci0001:00/0001:00:07.0/resource0 opened.
Target offset is 0x0, page size is 4096
mmap(0, 4096, 0x3, 0x1, 3, 0x0)
PCI Memory mapped to address 0x4801f000.
Value at offset 0x0 (0x4801f000): 0xC0BE0100


== Why do this at all ? ==

When I start working on a new PCI device driver I generally go through a
discovery phase of reading and writing to certain registers on the PCI card.
Over the years I have written lots of small kernel modules to probe addresses
within the PCI memory space, constantly iterating: modify code, recompile, scp
to target, load module, unload module, dmesg.

Urk! There has to be a better way - sysfs and mmap() to the rescue.


== Sysfs ==

Let's start at with the PCI files under sysfs:

bash# ls -l /sys/devices/pci0001\:00/0001\:00\:07.0/
total 0
-rw-r--r-- 1 root root     4096 Jul  2 20:13 broken_parity_status
lrwxrwxrwx 1 root root        0 Jul  2 20:13 bus -> ../../../bus/pci
-r--r--r-- 1 root root     4096 Jul  2 20:13 class
-rw-r--r-- 1 root root      256 Jul  2 20:13 config
-r--r--r-- 1 root root     4096 Jul  2 20:13 device
-r--r--r-- 1 root root     4096 Jul  2 20:13 devspec
-rw------- 1 root root     4096 Jul  2 20:13 enable
-r--r--r-- 1 root root     4096 Jul  2 20:13 irq
-r--r--r-- 1 root root     4096 Jul  2 20:13 local_cpus
-r--r--r-- 1 root root     4096 Jul  2 20:13 modalias
-rw-r--r-- 1 root root     4096 Jul  2 20:13 msi_bus
-r--r--r-- 1 root root     4096 Jul  2 20:13 resource
-rw------- 1 root root     4096 Jul  2 20:13 resource0
-rw------- 1 root root    65536 Jul  2 20:13 resource1
-rw------- 1 root root 16777216 Jul  2 20:13 resource2
lrwxrwxrwx 1 root root        0 Jul  2 20:13 subsystem -> ../../../bus/pci
-r--r--r-- 1 root root     4096 Jul  2 20:13 subsystem_device
-r--r--r-- 1 root root     4096 Jul  2 20:13 subsystem_vendor
-rw-r--r-- 1 root root     4096 Jul  2 20:13 uevent
-r--r--r-- 1 root root     4096 Jul  2 20:13 vendor

The vendor and device files report the PCI vendor ID and device ID:

bash# cat device
0x0001

This info is also available from lspci

bash# lspci -v
0001:00:07.0 Class 0680: Unknown device bec0:0001 (rev 01)
    Flags: bus master, 66MHz, medium devsel, latency 128, IRQ 31
    Memory at 8d010000 (32-bit, non-prefetchable) [size=4K]
    Memory at 8d000000 (32-bit, non-prefetchable) [size=64K]
    Memory at 8c000000 (32-bit, non-prefetchable) [size=16M]

This PCI card makes 3 seperate regions of memory available to the host
computer. The sysfs resource0 file corresponds to the first memory region. The
PCI card lets the host computer know about these memory regions using the BAR
registers in the PCI config.

== mmap() ==

These sysfs resource can be used with mmap() to map the PCI memory into a
userspace applications memory space.  The application then has a pointer to the
start of the PCI memory region and can read and write values directly. (There
is a bit more going on here with respect to memory pointers, but that is all
taken care of by the kernel).

fd = open("/sys/devices/pci0001\:00/0001\:00\:07.0/resource0", O_RDWR | O_SYNC);
ptr = mmap(0, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
printf("PCI BAR0 0x0000 = 0x%4x\n",  *((unsigned short *) ptr);

== PowerPC ==

To make this work on a PowerPC architecture you also need to make a small
change to the pci core. My example is from kernel 2.6.34, and hopefully this
will be fixed for us in a later kernel version.

bash# vi arch/powerpc/kernel/pci-common.c
    /* If memory, add on the PCI bridge address offset */
     if (mmap_state == pci_mmap_mem) {
-#if 0 /* See comment in pci_resource_to_user() for why this is disabled */
+#if 1 /* See comment in pci_resource_to_user() for why this is disabled */
         *offset += hose->pci_mem_offset;
 #endif
         res_bit = IORESOURCE_MEM;

         /* We pass a fully fixed up address to userland for MMIO instead of
         * a BAR value because X is lame and expects to be able to use that
         * to pass to /dev/mem !
         *
         * That means that we'll have potentially 64 bits values where some
         * userland apps only expect 32 (like X itself since it thinks only
         * Sparc has 64 bits MMIO) but if we don't do that, we break it on
         * 32 bits CHRPs :-(
         *
         * Hopefully, the sysfs insterface is immune to that gunk. Once X
         * has been fixed (and the fix spread enough), we can re-enable the
         * 2 lines below and pass down a BAR value to userland. In that case
         * we'll also have to re-enable the matching code in
         * __pci_mmap_make_offset().
         *
         * BenH.
         */
-#if 0
+#if 1
        else if (rsrc->flags &amp; IORESOURCE_MEM)
                offset = hose->pci_mem_offset;
#endif

180 changes: 180 additions & 0 deletions src/pcimem/src/pcimem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* pcimem.c: Simple program to read/write from/to a pci device from userspace.
*
* Copyright (C) 2010, Bill Farrow ([email protected])
*
* Based on the devmem2.c code
* Copyright (C) 2000, Jan-Derk Bakker ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>

#define PRINT_ERROR \
do { \
fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); \
} while(0)


int main(int argc, char **argv) {
int fd;
void *map_base, *virt_addr;
uint64_t read_result, writeval, prev_read_result = 0;
char *filename;
off_t target, target_base;
int access_type = 'w';
int items_count = 1;
int verbose = 0;
int read_result_dupped = 0;
int type_width;
int i;
int map_size = 4096UL;

if(argc < 3) {
// pcimem /sys/bus/pci/devices/0001\:00\:07.0/resource0 0x100 w 0x00
// argv[0] [1] [2] [3] [4]
fprintf(stderr, "\nUsage:\t%s { sysfile } { offset } [ type*count [ data ] ]\n"
"\tsys file: sysfs file for the pci resource to act on\n"
"\toffset : offset into pci memory region to act upon\n"
"\ttype : access operation type : [b]yte, [h]alfword, [w]ord, [d]ouble-word\n"
"\t*count : number of items to read: w*100 will dump 100 words\n"
"\tdata : data to be written\n\n",
argv[0]);
exit(1);
}
filename = argv[1];
target = strtoul(argv[2], 0, 0);

if(argc > 3) {
access_type = tolower(argv[3][0]);
if (argv[3][1] == '*')
items_count = strtoul(argv[3]+2, 0, 0);
}

switch(access_type) {
case 'b':
type_width = 1;
break;
case 'h':
type_width = 2;
break;
case 'w':
type_width = 4;
break;
case 'd':
type_width = 8;
break;
default:
fprintf(stderr, "Illegal data type '%c'.\n", access_type);
exit(2);
}

if((fd = open(filename, O_RDWR | O_SYNC)) == -1) PRINT_ERROR;
printf("%s opened.\n", filename);
printf("Target offset is 0x%x, page size is %ld\n", (int) target, sysconf(_SC_PAGE_SIZE));
fflush(stdout);

target_base = target & ~(sysconf(_SC_PAGE_SIZE)-1);
if (target + items_count*type_width - target_base > map_size)
map_size = target + items_count*type_width - target_base;

/* Map one page */
printf("mmap(%d, %d, 0x%x, 0x%x, %d, 0x%x)\n", 0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (int) target);

map_base = mmap(0, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target_base);
if(map_base == (void *) -1) PRINT_ERROR;
printf("PCI Memory mapped to address 0x%08lx.\n", (unsigned long) map_base);
fflush(stdout);

for (i = 0; i < items_count; i++) {

virt_addr = map_base + target + i*type_width - target_base;
switch(access_type) {
case 'b':
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
read_result = *((uint32_t *) virt_addr);
break;
case 'd':
read_result = *((uint64_t *) virt_addr);
break;
}

if (verbose)
printf("Value at offset 0x%X (%p): 0x%0*lX\n", (int) target + i*type_width, virt_addr, type_width*2, read_result);
else {
if (read_result != prev_read_result || i == 0) {
printf("0x%04X: 0x%0*lX\n", (int)(target + i*type_width), type_width*2, read_result);
read_result_dupped = 0;
} else {
if (!read_result_dupped)
printf("...\n");
read_result_dupped = 1;
}
}

prev_read_result = read_result;

}

fflush(stdout);

if(argc > 4) {
writeval = strtoull(argv[4], NULL, 0);
switch(access_type) {
case 'b':
*((uint8_t *) virt_addr) = writeval;
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
*((uint16_t *) virt_addr) = writeval;
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
*((uint32_t *) virt_addr) = writeval;
read_result = *((uint32_t *) virt_addr);
break;
case 'd':
*((uint64_t *) virt_addr) = writeval;
read_result = *((uint64_t *) virt_addr);
break;
}
printf("Written 0x%0*lX; readback 0x%*lX\n", type_width,
writeval, type_width, read_result);
fflush(stdout);
}

if(munmap(map_base, map_size) == -1) PRINT_ERROR;
close(fd);
return 0;
}
Loading