BUS_ALLOC_RESOURCE(9)
π» BUS_ALLOC_RESOURCE(9) β the kernel dev manual, bestie edition π
okay so this is BUS_ALLOC_RESOURCE(9) from the Kernel Developer's Manual, and it's genuinely one of those pages that makes kernel life so much easier. sit down, grab a boba, we're allocating resources π
β¨ 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 π¦
This is the easy, low-effort, high-reward interface to the resource-management functions, and the way it slays is that it hides all the indirection through the parent's method table. like, you don't have to think about it, it just works β¨
timing matters, bestie: this function generally should be called in attach, but (except in some rare cases) never earlier. fr fr, don't jump the gun π
The bus_alloc_resource_any() and bus_alloc_resource_anywhere() functions are convenience wrappers for bus_alloc_resource() β the lite versions, the ready-to-wear versions π
bus_alloc_resource_any()β setsstart,end, andcountto the default resource (see the description ofstartbelow).bus_alloc_resource_anywhere()β setsstartandendto the default resource, and uses thecountargument you provide.
π The arguments, one by one π
devβ the device that requests ownership of the resource. Before allocation, the resource is owned by the parent bus. it's giving "handing over the aux cord" energy πΆtypeβ the type of resource you want to allocate, and 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β a bus specific handle that identifies the resource being allocated. and ngl, whatridmeans depends entirely on the bus, so:- 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.
startandendβ the start/end addresses of the resource. If you specify values of0ulforstartand~0ulforendand1forcount, the default values for the bus are calculated. iconic default behavior β¨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.flagsβ sets the flags for the resource. You can set zero or more of these flags: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. sharing is caring, literally π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. that's it, that's the whole vibe check π«Ά
π₯ EXAMPLES π₯
This is some example code that allocates a 32 byte I/O port range and an IRQ. look at her go ππ
struct resource *portres, *irqres;
portres = bus_alloc_resource(dev, SYS_RES_IOPORT, 0,
0ul, ~0ul, 32, RF_ACTIVE);
irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, 0,
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>. legends, both of them π
and that's the whole page, bestie β go forth and allocate responsibly π§π
this Debian rendition of BUS_ALLOC_RESOURCE(9) is dated March 6, 2026 β mwah, see u in the next manpage πβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2000 Alexander Langer
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms