diff options
author | jhb <jhb@FreeBSD.org> | 2016-05-20 17:57:47 +0000 |
---|---|---|
committer | jhb <jhb@FreeBSD.org> | 2016-05-20 17:57:47 +0000 |
commit | b4b2ae565200af6f37fec26de490a48783880ce4 (patch) | |
tree | 3aa16fa17a986984dd375052ea627ea43a7d21dd /sys/kern | |
parent | 6f2769857a743d51ed1025cabba5e6b4c769fcfe (diff) | |
download | FreeBSD-src-b4b2ae565200af6f37fec26de490a48783880ce4.zip FreeBSD-src-b4b2ae565200af6f37fec26de490a48783880ce4.tar.gz |
Add new bus methods for mapping resources.
Add a pair of bus methods that can be used to "map" resources for direct
CPU access using bus_space(9). bus_map_resource() creates a mapping and
bus_unmap_resource() releases a previously created mapping. Mappings are
described by 'struct resource_map' object. Pointers to these objects can
be passed as the first argument to the bus_space wrapper API used for bus
resources.
Drivers that wish to map all of a resource using default settings
(for example, using uncacheable memory attributes) do not need to change.
However, drivers that wish to use non-default settings can now do so
without jumping through hoops.
First, an RF_UNMAPPED flag is added to request that a resource is not
implicitly mapped with the default settings when it is activated. This
permits other activation steps (such as enabling I/O or memory decoding
in a device's PCI command register) to be taken without creating a
mapping. Right now the AGP drivers don't set RF_ACTIVE to avoid using
up a large amount of KVA to map the AGP aperture on 32-bit platforms.
Once RF_UNMAPPED is supported on all platforms that support AGP this
can be changed to using RF_UNMAPPED with RF_ACTIVE instead.
Second, bus_map_resource accepts an optional structure that defines
additional settings for a given mapping.
For example, a driver can now request to map only a subset of a resource
instead of the entire range. The AGP driver could also use this to only
map the first page of the aperture (IIRC, it calls pmap_mapdev() directly
to map the first page currently). I will also eventually change the
PCI-PCI bridge driver to request mappings of the subset of the I/O window
resource on its parent side to create mappings for child devices rather
than passing child resources directly up to nexus to be mapped. This
also permits bridges that do address translation to request suitable
mappings from a resource on the "upper" side of the bus when mapping
resources on the "lower" side of the bus.
Another attribute that can be specified is an alternate memory attribute
for memory-mapped resources. This can be used to request a
Write-Combining mapping of a PCI BAR in an MI fashion. (Currently the
drivers that do this call pmap_change_attr() directly for x86 only.)
Note that this commit only adds the MI framework. Each platform needs
to add support for handling RF_UNMAPPED and thew new
bus_map/unmap_resource methods. Generally speaking, any drivers that
are calling rman_set_bustag() and rman_set_bushandle() need to be
updated.
Discussed on: arch
Reviewed by: cem
Differential Revision: https://reviews.freebsd.org/D5237
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/bus_if.m | 55 | ||||
-rw-r--r-- | sys/kern/subr_bus.c | 74 | ||||
-rw-r--r-- | sys/kern/subr_rman.c | 21 |
3 files changed, 146 insertions, 4 deletions
diff --git a/sys/kern/bus_if.m b/sys/kern/bus_if.m index 8592e9f..2b75438 100644 --- a/sys/kern/bus_if.m +++ b/sys/kern/bus_if.m @@ -287,8 +287,9 @@ METHOD struct resource * alloc_resource { * @brief Activate a resource * * Activate a resource previously allocated with - * BUS_ALLOC_RESOURCE(). This may for instance map a memory region - * into the kernel's virtual address space. + * BUS_ALLOC_RESOURCE(). This may enable decoding of this resource in a + * device for instance. It will also establish a mapping for the resource + * unless RF_UNMAPPED was set when allocating the resource. * * @param _dev the parent device of @p _child * @param _child the device which allocated the resource @@ -304,12 +305,58 @@ METHOD int activate_resource { struct resource *_r; }; + +/** + * @brief Map a resource + * + * Allocate a mapping for a range of an active resource. The mapping + * is described by a struct resource_map object. This may for instance + * map a memory region into the kernel's virtual address space. + * + * @param _dev the parent device of @p _child + * @param _child the device which allocated the resource + * @param _type the type of resource + * @param _r the resource to map + * @param _args optional attributes of the mapping + * @param _map the mapping + */ +METHOD int map_resource { + device_t _dev; + device_t _child; + int _type; + struct resource *_r; + struct resource_map_request *_args; + struct resource_map *_map; +} DEFAULT bus_generic_map_resource; + + +/** + * @brief Unmap a resource + * + * Release a mapping previously allocated with + * BUS_MAP_RESOURCE(). This may for instance unmap a memory region + * from the kernel's virtual address space. + * + * @param _dev the parent device of @p _child + * @param _child the device which allocated the resource + * @param _type the type of resource + * @param _r the resource + * @param _map the mapping to release + */ +METHOD int unmap_resource { + device_t _dev; + device_t _child; + int _type; + struct resource *_r; + struct resource_map *_map; +} DEFAULT bus_generic_unmap_resource; + + /** * @brief Deactivate a resource * * Deactivate a resource previously allocated with - * BUS_ALLOC_RESOURCE(). This may for instance unmap a memory region - * from the kernel's virtual address space. + * BUS_ALLOC_RESOURCE(). * * @param _dev the parent device of @p _child * @param _child the device which allocated the resource diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c index e882553..f491643 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -63,6 +63,7 @@ __FBSDID("$FreeBSD$"); #include <machine/stdarg.h> #include <vm/uma.h> +#include <vm/vm.h> SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW, NULL, NULL); @@ -3050,6 +3051,15 @@ device_set_unit(device_t dev, int unit) * Some useful method implementations to make life easier for bus drivers. */ +void +resource_init_map_request_impl(struct resource_map_request *args, size_t sz) +{ + + bzero(args, sz); + args->size = sz; + args->memattr = VM_MEMATTR_UNCACHEABLE; +} + /** * @brief Initialise a resource list. * @@ -4060,6 +4070,40 @@ bus_generic_deactivate_resource(device_t dev, device_t child, int type, } /** + * @brief Helper function for implementing BUS_MAP_RESOURCE(). + * + * This simple implementation of BUS_MAP_RESOURCE() simply calls the + * BUS_MAP_RESOURCE() method of the parent of @p dev. + */ +int +bus_generic_map_resource(device_t dev, device_t child, int type, + struct resource *r, struct resource_map_request *args, + struct resource_map *map) +{ + /* Propagate up the bus hierarchy until someone handles it. */ + if (dev->parent) + return (BUS_MAP_RESOURCE(dev->parent, child, type, r, args, + map)); + return (EINVAL); +} + +/** + * @brief Helper function for implementing BUS_UNMAP_RESOURCE(). + * + * This simple implementation of BUS_UNMAP_RESOURCE() simply calls the + * BUS_UNMAP_RESOURCE() method of the parent of @p dev. + */ +int +bus_generic_unmap_resource(device_t dev, device_t child, int type, + struct resource *r, struct resource_map *map) +{ + /* Propagate up the bus hierarchy until someone handles it. */ + if (dev->parent) + return (BUS_UNMAP_RESOURCE(dev->parent, child, type, r, map)); + return (EINVAL); +} + +/** * @brief Helper function for implementing BUS_BIND_INTR(). * * This simple implementation of BUS_BIND_INTR() simply calls the @@ -4421,6 +4465,36 @@ bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r) } /** + * @brief Wrapper function for BUS_MAP_RESOURCE(). + * + * This function simply calls the BUS_MAP_RESOURCE() method of the + * parent of @p dev. + */ +int +bus_map_resource(device_t dev, int type, struct resource *r, + struct resource_map_request *args, struct resource_map *map) +{ + if (dev->parent == NULL) + return (EINVAL); + return (BUS_MAP_RESOURCE(dev->parent, dev, type, r, args, map)); +} + +/** + * @brief Wrapper function for BUS_UNMAP_RESOURCE(). + * + * This function simply calls the BUS_UNMAP_RESOURCE() method of the + * parent of @p dev. + */ +int +bus_unmap_resource(device_t dev, int type, struct resource *r, + struct resource_map *map) +{ + if (dev->parent == NULL) + return (EINVAL); + return (BUS_UNMAP_RESOURCE(dev->parent, dev, type, r, map)); +} + +/** * @brief Wrapper function for BUS_RELEASE_RESOURCE(). * * This function simply calls the BUS_RELEASE_RESOURCE() method of the diff --git a/sys/kern/subr_rman.c b/sys/kern/subr_rman.c index d1c68b45..cc0bcdc 100644 --- a/sys/kern/subr_rman.c +++ b/sys/kern/subr_rman.c @@ -897,6 +897,27 @@ rman_get_bushandle(struct resource *r) } void +rman_set_mapping(struct resource *r, struct resource_map *map) +{ + + KASSERT(rman_get_size(r) == map->r_size, + ("rman_set_mapping: size mismatch")); + rman_set_bustag(r, map->r_bustag); + rman_set_bushandle(r, map->r_bushandle); + rman_set_virtual(r, map->r_vaddr); +} + +void +rman_get_mapping(struct resource *r, struct resource_map *map) +{ + + map->r_bustag = rman_get_bustag(r); + map->r_bushandle = rman_get_bushandle(r); + map->r_size = rman_get_size(r); + map->r_vaddr = rman_get_virtual(r); +} + +void rman_set_rid(struct resource *r, int rid) { |