BUS_ALLOC_RESOURCE(9)

πŸ“¦
version 15.1 Β· 3 versions
, in freebsd 15.1

πŸ’» 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() β€” sets start, end, and count to the default resource (see the description of start below).
  • bus_alloc_resource_anywhere() β€” sets start and end to the default resource, and uses the count argument 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 numbers
    • SYS_RES_IRQ β€” for IRQs
    • SYS_RES_DRQ β€” for ISA DMA lines
    • SYS_RES_IOPORT β€” for I/O ports
    • SYS_RES_MEMORY β€” for I/O memory
  • rid β€” a bus specific handle that identifies the resource being allocated. and ngl, what rid means 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.
  • start and end β€” the start/end addresses of the resource. If you specify values of 0ul for start and ~0ul for end and 1 for count, 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 for start and end, then the default value of the bus is used if count is smaller than the default value, and count is 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 via bus_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 🌈

πŸ“ 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms