How Do I Allocate Less Than the Entire BAR for a PCI Device?

M. Warner Losh imp at bsdimp.com
Sat Apr 8 01:31:26 UTC 2006


In message: <20060408005155.GF72485 at funkthat.com>
            John-Mark Gurney <gurney_j at resnet.uoregon.edu> writes:
: David Christensen wrote this message on Fri, Apr 07, 2006 at 16:43 -0700:
: > The Ethernet driver I'm working on has a very large PCI BAR mapping 
: > for memory (32MB), but I only need 128KB of that BAR memory for use
: > by the driver.  How can I map only the amount of memory I need with
: > bus_alloc_resource()?  It looks like I can specify the size if I
: > also know the BAR starting address, but I'm not sure how to find that.
: > Has anyone done this or know of any existing code that does this?  All
: > of the examples I looked at used all of BAR mapped memory.
: 
: start and end should be relative to the resource.. so if you need to map
: 128kb at 1meg of the resource, you would do:
: 	rid = PCIR_BAR(0); /* or what ever bar the memory map is at */
: 	bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 1024*1024,
: 	    1024*1024+128*1024 - 1, 128*1024, RF_ACTIVE);
: 
: I believe this will work, I haven't tried it.. though my tracing of
: the code seems to imply it might not...

0 and ~0UL should be used instead.  However, I'm pretty sure that we
ignore the size in the pci code.

: also, most systems provide complete direct access to the pci address
: space.. and on platforms like i386 w/ direct mapped pci space, even
: if you want to only map 128k, the resource will still consume 32megs,
: since you cannot map less than the resource lets you... (on archs like
: sparc64, w/ 64bit address space, you don't have to worry about consuming
: address space)..

The problem is that in the phyiscal address space (on i386 at least),
the BAR does take up 32MB.  The card decodes that entire range, so no
other cards can be in that range.

: so, if you were only trying to map 128k to save kernel memory, etc,
: don't bother, map the whole thing and use only the parts you need..

Agreed.

Warner


More information about the freebsd-drivers mailing list