access pci from user space
Wim Vanderbauwhede
wim at motherearth.org
Tue Mar 8 02:49:37 MST 2005
Hi,
Your mail was not lost.
I'm not an expert, but I was wondering if this could help you:
On embedded systems, you would access the memory mapped IO without
using /dev/mem, but by directly accessing the memory locations. I don't
think you need mmap, as you can allocate memory in your code in the
usual fashion (statically or with malloc).
Following snippet are some convenience functions:
<snip
src="http://www.koders.com/c/fidF8BACE7ED1FDC791C586365DF3A7700993B294A7.aspx">
/*
* readX/writeX() are used to access memory mapped devices. On some
* architectures the memory mapped IO stuff needs to be accessed
* differently. On the x86 architecture, we just read/write the
* memory location directly.
*/
#define readb(addr) (*(volatile unsigned char *) (addr))
#define readw(addr) (*(volatile unsigned short *) (addr))
#define readl(addr) (*(volatile unsigned int *) (addr))
#define writeb(b,addr) ((*(volatile unsigned char *) (addr)) = (b))
#define writew(b,addr) ((*(volatile unsigned short *) (addr)) = (b))
#define writel(b,addr) ((*(volatile unsigned int *) (addr)) = (b))
#define memset_io(a,b,c) memset((void *)(a),(b),(c))
#define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
#define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
</snip>
The "volatile" keyword is important, as it tells the compiler not to try anything fancy for that memory location, no optmisations etc.
If you have an OS with a filesystem, you can try to use the /dev block devices.
You can find the actual memory values for pci from /proc/pci (cat /proc/pci)
HTH,
Wim
On Sun, 2005-02-27 at 19:41 +0100, Dominik Meyer wrote:
> Hello,
>
> i'm currently trying to do something like pciproxy
> (http://scaramanga.co.uk/stuff/qemu-pciproxy/) for mol to let the
> Airport driver from OsX access the real hardware.
>
> Now i've come to the point where i have to access the pci device.
> I thought it should be possible, to do that from user space and i wrote
> a little application to test this, but it didn't work:
>
> base = 0xa0006000; // the address from /proc/bus/pci/devices like
> // described on
> // http://www.linuxjournal.com/article/5442
> length = 0x2000;
>
> fd = open("/dev/mem", O_RDWR|O_SYNC);
>
> map = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
> if (map == MAP_FAILED) {
> printf("map failed\n");
> exit(1);
> }
>
> memcpy(&buf, map, 0x10);
>
> and then it crashes with a "Bus Error". I have no idea why?
> Anybody can help me out?
>
> Dominik
>
> (Full source: http://mitglied.lycos.de/phpspacer/blog/pcitest.c)
>
> _______________________________________________
> mol-general mailing list
> mol-general at lists.maconlinux.org
> http://lists.maconlinux.org/mailman/listinfo/mol-general
--
Wim Vanderbauwhede <wim at motherearth.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.terrasoftsolutions.com/pipermail/mol-general/attachments/20050308/053e5c9f/attachment.bin
More information about the mol-general
mailing list