BUS_ALLOC_RESOURCE(9)
π BUS_ALLOC_RESOURCE π
okay bestie, welcome to BUS_ALLOC_RESOURCE(9), straight out of the Kernel Developer's Manual β¨ this is the page about grabbing resources off your parent bus, and it's genuinely so useful. let's get into it π«Ά
πΈ NAME πΈ
bus_alloc_resource, bus_alloc_resource_any, bus_alloc_resource_anywhere β allocate resources from a parent bus π
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <machine/resource.h>
struct resource *
bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start,
rman_res_t end, rman_res_t count, u_int flags);
struct resource *
bus_alloc_resource_any(device_t dev, int type, int *rid, u_int flags);
struct resource *
bus_alloc_resource_anywhere(device_t dev, int type, int *rid,
rman_res_t count, u_int flags);
π DESCRIPTION π
so this is the easy-peasy interface to the resource-management functions β it hides all the indirection through the parent's method table (the way it just... skips the middleman? iconic π
). generally you should call this in attach, but (except in some rare cases) never earlier. fr fr, timing matters.
the bus_alloc_resource_any() and bus_alloc_resource_anywhere() functions are convenience wrappers for bus_alloc_resource():
bus_alloc_resource_any()setsstart,end, andcountto the default resource (peep the description ofstartbelow π).bus_alloc_resource_anywhere()setsstartandendto the default resource and uses the providedcountargument.
the arguments are as follows, bestie, listen up π:
devβ the device that requests ownership of the resource. before allocation, the resource is owned by the parent bus. not you. the bus. πtypeβ the type of resource you want to allocate. it is one of:PCI_RES_BUSβ for PCI bus numbersSYS_RES_IRQβ for IRQsSYS_RES_DRQβ for ISA DMA linesSYS_RES_IOPORTβ for I/O portsSYS_RES_MEMORYβ for I/O memory
ridβ points to a bus specific handle that identifies the resource being allocated. for ISA this is an index into an array of resources that have been setup for this device by either the PnP mechanism, or via the hints mechanism. for PCCARD, this is an index into the array of resources described by the PC Card's CIS entry. for PCI, the offset into PCI config space which has the BAR to use to access the resource. π«Ά
β οΈ the bus methods are free to change the RIDs that they are given as a parameter. you must not depend on the value you gave it earlier. it's giving "moving on without you" energy, and you just have to accept it π
startandendβ the start/end addresses of the resource. if you specify values of0ulforstartand~0ulforendand1forcount, the default values for the bus are calculated. β¨countβ the size of the resource. for example, the size of an I/O port is usually 1 byte (but some devices override this). if you specified the default values forstartandend, then the default value of the bus is used ifcountis smaller than the default value andcountis used, if it is bigger than the default value. the way it just picks the bigger one?? lowkey fair πflagsβ sets the flags for the resource. you can set one or more of these flags:RF_ALLOCATEDβ resource has been reserved. the resource still needs to be activated withbus_activate_resource(9). not done yet, babes, one more step π§RF_ACTIVEβ activate resource atomically. β‘RF_PREFETCHABLEβ resource is prefetchable.RF_SHAREABLEβ resource permits contemporaneous sharing. it should always be set unless you know that the resource cannot be shared. it is the bus driver's task to filter out the flag if the bus does not support sharing. for example,pccard(4)cannot share IRQs whilecardbus(4)can. sharing is caring but only sometimes π«ΆRF_UNMAPPEDβ do not establish implicit mapping when activated viabus_activate_resource(9).
π RETURN VALUES π
a pointer to struct resource is returned on success, a null pointer otherwise. so like... check it. please. for me. π₯Ί
π EXAMPLES π
here's some example code that allocates a 32 byte I/O port range and an IRQ. the values of portid and irqid should be saved in the softc of the device after these calls. save them, bestie, they're precious π
struct resource *portres, *irqres;
int portid, irqid;
portid = 0;
irqid = 0;
portres = bus_alloc_resource(dev, SYS_RES_IOPORT, &portid,
0ul, ~0ul, 32, RF_ACTIVE);
irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &irqid,
RF_ACTIVE | RF_SHAREABLE);
π SEE ALSO π
bus_activate_resource(9), bus_adjust_resource(9), bus_map_resource(9), bus_release_resource(9), device(9), driver(9)
π AUTHORS π
this manual page was written by Alexander Langer <alex@big.endian.de> with parts by Warner Losh <imp@FreeBSD.org>. absolute legends, we stan π
okay that's the whole page, you ate it, periodt π
this was BUS_ALLOC_RESOURCE(9) from the Debian build, dated May 20, 2016 β go forth and allocate responsibly, no cap π¦π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2000 Alexander Langer
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms