1.\" 2.\" Copyright (c) 2003 Bruce M Simpson <bms@spc.org> 3.\" All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.\" $FreeBSD$ 27.\" 28.Dd May 12, 2003 29.Dt RMAN 9 30.Os 31.Sh NAME 32.Nm rman , 33.Nm rman_activate_resource , 34.Nm rman_await_resource , 35.Nm rman_deactivate_resource , 36.Nm rman_fini , 37.Nm rman_init , 38.Nm rman_manage_region , 39.Nm rman_release_resource , 40.Nm rman_reserve_resource , 41.Nm rman_reserve_resource_bound , 42.Nm rman_make_alignment_flags , 43.Nm rman_get_start , 44.Nm rman_get_end , 45.Nm rman_get_size , 46.Nm rman_get_flags , 47.Nm rman_set_virtual , 48.Nm rman_get_virtual , 49.Nm rman_set_bustag , 50.Nm rman_get_bustag , 51.Nm rman_set_bushandle , 52.Nm rman_get_bushandle , 53.Nm rman_set_rid , 54.Nm rman_get_rid 55.Nd resource management functions 56.Sh SYNOPSIS 57.In sys/rman.h 58.Ft int 59.Fn rman_activate_resource "struct resource *r" 60.Ft int 61.Fn rman_await_resource "struct resource *r" "int pri2" "int timo" 62.Ft int 63.Fn rman_deactivate_resource "struct resource *r" 64.Ft int 65.Fn rman_fini "struct rman *rm" 66.Ft int 67.Fn rman_init "struct rman *rm" 68.Ft int 69.Fn rman_manage_region "struct rman *rm" "u_long start" "u_long end" 70.Ft int 71.Fn rman_release_resource "struct resource *r" 72.Ft "struct resource *" 73.Fo rman_reserve_resource 74.Fa "struct rman *rm" "u_long start" "u_long end" "u_long count" 75.Fa "u_int flags" "struct device *dev" 76.Fc 77.Ft "struct resource *" 78.Fo rman_reserve_resource_bound 79.Fa "struct rman *rm" "u_long start" "u_long end" "u_long count" 80.Fa "u_long bound" "u_int flags" "struct device *dev" 81.Fc 82.Ft uint32_t 83.Fn rman_make_alignment_flags "uint32_t size" 84.Ft u_long 85.Fn rman_get_start "struct resource *r" 86.Ft u_long 87.Fn rman_get_end "struct resource *r" 88.Ft u_long 89.Fn rman_get_size "struct resource *r" 90.Ft u_int 91.Fn rman_get_flags "struct resource *r" 92.Ft void 93.Fn rman_set_virtual "struct resource *r" "void *v" 94.Ft "void *" 95.Fn rman_get_virtual "struct resource *r" 96.Ft void 97.Fn rman_set_bustag "struct resource *r" "bus_space_tag_t t" 98.Ft bus_space_tag_t 99.Fn rman_get_bustag "struct resource *r" 100.Ft void 101.Fn rman_set_bushandle "struct resource *r" "bus_space_handle_t h" 102.Ft bus_space_handle_t 103.Fn rman_get_bushandle "struct resource *r" 104.Ft void 105.Fn rman_set_rid "struct resource *r" "int rid" 106.Ft int 107.Fn rman_get_rid "struct resource *r" 108.Sh DESCRIPTION 109The 110.Nm 111set of functions provides a flexible resource management abstraction. 112It is used extensively by the bus management code. 113It implements the abstractions of region and resource. 114A region descriptor is used to manage a region; this could be memory or 115some other form of bus space. 116.Pp 117Each region has a set of bounds. 118Within these bounds, allocated segments may reside. 119Each segment, termed a resource, has several properties which are 120represented by a 16-bit flag register, as follows. 121.Bd -literal 122#define RF_ALLOCATED 0x0001 /* resource has been reserved */ 123#define RF_ACTIVE 0x0002 /* resource allocation has been activated */ 124#define RF_SHAREABLE 0x0004 /* resource permits contemporaneous sharing */ 125#define RF_TIMESHARE 0x0008 /* resource permits time-division sharing */ 126#define RF_WANTED 0x0010 /* somebody is waiting for this resource */ 127#define RF_FIRSTSHARE 0x0020 /* first in sharing list */ 128#define RF_PREFETCHABLE 0x0040 /* resource is prefetchable */ 129.Ed 130.Pp 131The remainder of the flag bits are used to represent the desired alignment 132of the resource within the region. 133.Pp 134The 135.Fn rman_init 136function initializes the region descriptor, pointed to by the 137.Fa rm 138argument, for use with the resource management functions. 139It also initializes any mutexes associated with the structure. 140.Pp 141The 142.Fn rman_fini 143function frees any structures associated with the structure 144pointed to by the 145.Fa rm 146argument. 147If any of the resources within the managed region have the 148.Dv RF_ALLOCATED 149flag set, it will return 150.Er EBUSY ; 151otherwise, any mutexes associated with the structure will be released 152and destroyed, and the function will return 0. 153.Pp 154The 155.Fn rman_manage_region 156function establishes the concept of a region which is under 157.Nm 158control. 159The 160.Fa rman 161argument points to the region descriptor. 162The 163.Fa start 164and 165.Fa end 166arguments specify the bounds of the region. 167.Pp 168.Em NOTE : 169This interface is not robust against programming errors which 170add multiple copies of the same region. 171.Pp 172The 173.Fn rman_reserve_resource_bound 174function is where the bulk of the 175.Nm 176logic is located. 177It attempts to reserve a contiguous range in the specified region 178.Fa rm 179for the use of the device 180.Fa dev . 181The caller can specify the 182.Fa start 183and 184.Fa end 185of an acceptable range, as well as 186alignment, and the code will attempt to find a free segment which fits. 187The default behavior is to allocate an exclusive segment, unless the 188.Dv RF_SHAREABLE 189or 190.Dv RF_TIMESHARE 191flags are set, in which case a shared 192segment will be allocated. 193If this shared segment already exists, the caller has its device 194added to the list of consumers. 195.Pp 196The 197.Fn rman_reserve_resource 198function is used to reserve resources within a previously established region. 199It is a simplified interface to 200.Fn rman_reserve_resource_bound 201which passes 0 for the 202.Fa flags 203argument. 204.Pp 205The 206.Fn rman_make_alignment_flags 207function returns the flag mask corresponding to the desired alignment 208.Fa size . 209This should be used when calling 210.Fn rman_reserve_resource_bound . 211.Pp 212The 213.Fn rman_release_resource 214function releases the reserved resource 215.Fa r . 216It may attempt to merge adjacent free resources. 217.Pp 218The 219.Fn rman_activate_resource 220function marks a resource as active, by setting the 221.Dv RF_ACTIVE 222flag. 223If this is a time shared resource, and the caller has not yet acquired 224the resource, the function returns 225.Er EBUSY . 226.Pp 227The 228.Fn rman_deactivate_resource 229function marks a resource 230.Fa r 231as inactive, by clearing the 232.Dv RF_ACTIVE 233flag. 234If other consumers are waiting for this range, it will wakeup their threads. 235.Pp 236The 237.Fn rman_await_resource 238function performs an asynchronous wait for a resource 239.Fa r 240to become inactive, that is, for the 241.Dv RF_ACTIVE 242flag to be cleared. 243It is used to enable cooperative sharing of a resource 244which can only be safely used by one thread at a time. 245The arguments 246.Fa pri 247and 248.Fa timo 249are passed to the 250.Fn rman_await_resource 251function. 252.Pp 253The 254.Fn rman_get_start , 255.Fn rman_get_end , 256.Fn rman_get_size , 257and 258.Fn rman_get_flags 259functions return the bounds, size and flags of the previously reserved 260resource 261.Fa r . 262.Pp 263The 264.Fn rman_set_bustag 265function associates a 266.Vt bus_space_tag_t 267.Fa t 268with the resource 269.Fa r . 270The 271.Fn rman_get_bustag 272function is used to retrieve this tag once set. 273.Pp 274The 275.Fn rman_set_bushandle 276function associates a 277.Vt bus_space_handle_t 278.Fa h 279with the resource 280.Fa r . 281The 282.Fn rman_get_bushandle 283function is used to retrieve this handle once set. 284.Pp 285The 286.Fn rman_set_virtual 287function is used to associate a kernel virtual address with a resource 288.Fa r . 289The 290.Fn rman_get_virtual 291function can be used to retrieve the KVA once set. 292.Pp 293The 294.Fn rman_set_rid 295function associates a resource identifier with a resource 296.Fa r . 297The 298.Fn rman_get_rid 299function retrieves this RID. 300.Pp 301The 302.Fn rman_get_device 303function returns a pointer to the device which reserved the resource 304.Fa r . 305.Pp 306.Sh SEE ALSO 307.Xr bus_activate_resource 9 , 308.Xr bus_alloc_resource 9 , 309.Xr bus_release_resource 9 , 310.Xr bus_set_resource 9 , 311.Xr mutex 9 312.Sh AUTHORS 313This man page was written by 314.An Bruce M Simpson Aq bms@spc.org . 315