Index: share/man/man9/Makefile =================================================================== --- share/man/man9/Makefile +++ share/man/man9/Makefile @@ -44,6 +44,7 @@ bus_generic_shutdown.9 \ BUS_GET_CPUS.9 \ bus_get_resource.9 \ + bus_map_resource.9 \ BUS_NEW_PASS.9 \ BUS_PRINT_CHILD.9 \ BUS_READ_IVAR.9 \ @@ -504,6 +505,8 @@ bus_dma.9 bus_dma_tag_destroy.9 MLINKS+=bus_generic_read_ivar.9 bus_generic_write_ivar.9 MLINKS+=BUS_GET_CPUS.9 bus_get_cpus.9 +MLINKS+=bus_map_resource.9 bus_unmap_resource.9 \ + bus_map_resource.9 resource_init_map_request.9 MLINKS+=BUS_READ_IVAR.9 BUS_WRITE_IVAR.9 MLINKS+=BUS_SETUP_INTR.9 bus_setup_intr.9 \ BUS_SETUP_INTR.9 BUS_TEARDOWN_INTR.9 \ @@ -1380,6 +1383,7 @@ rman.9 rman_get_device.9 \ rman.9 rman_get_end.9 \ rman.9 rman_get_flags.9 \ + rman.9 rman_get_mapping.9 \ rman.9 rman_get_rid.9 \ rman.9 rman_get_size.9 \ rman.9 rman_get_start.9 \ @@ -1395,6 +1399,7 @@ rman.9 rman_reserve_resource_bound.9 \ rman.9 rman_set_bushandle.9 \ rman.9 rman_set_bustag.9 \ + rman.9 rman_set_mapping.9 \ rman.9 rman_set_rid.9 \ rman.9 rman_set_virtual.9 MLINKS+=rmlock.9 rm_assert.9 \ Index: share/man/man9/bus_activate_resource.9 =================================================================== --- share/man/man9/bus_activate_resource.9 +++ share/man/man9/bus_activate_resource.9 @@ -106,6 +106,14 @@ the virtual address can be retrieved via .Xr rman_get_virtual 9 . .Pp +This implicit mapping can be disabled by passing the +.Dv RF_UNMAPPED +flag to +.Xr bus_alloc_resource 9 . +A driver may use this if it wishes to allocate its own mappings of a resource +using +.Xr bus_map_resource 9 . +.Pp A wrapper API for .Xr bus_space 9 is also provided that accepts the associated resource as the first argument @@ -133,6 +141,7 @@ Zero is returned on success, otherwise an error is returned. .Sh SEE ALSO .Xr bus_alloc_resource 9 , +.Xr bus_map_resource 9 , .Xr bus_space 9 , .Xr device 9 , .Xr driver 9 Index: share/man/man9/bus_alloc_resource.9 =================================================================== --- share/man/man9/bus_alloc_resource.9 +++ share/man/man9/bus_alloc_resource.9 @@ -166,6 +166,9 @@ cannot share IRQs while .Xr cardbus 4 can. +.It Dv RF_UNMAPPED +do not establish implicit mapping when activated via +.Xr bus_activate_resource 9 . .El .El .Sh RETURN VALUES @@ -193,6 +196,7 @@ .Sh SEE ALSO .Xr bus_activate_resource 9 , .Xr bus_adjust_resource 9 , +.Xr bus_map_resource 9 , .Xr bus_release_resource 9 , .Xr device 9 , .Xr driver 9 Index: share/man/man9/bus_map_resource.9 =================================================================== --- /dev/null +++ share/man/man9/bus_map_resource.9 @@ -0,0 +1,167 @@ +.\" -*- nroff -*- +.\" +.\" Copyright (c) 2016 John H. Baldwin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd March 28, 2003 +.Dt BUS_MAP_RESOURCE 9 +.Os +.Sh NAME +.Nm bus_map_resource , bus_unmap_resource , resource_init_map_request +.Nd map or unmap an active resource +.Sh SYNOPSIS +.In sys/param.h +.In sys/bus.h +.Pp +.In machine/bus.h +.In sys/rman.h +.In machine/resource.h +.Ft int +.Fo bus_map_resource +.Fa "device_t dev" "int type" "struct resource *r" +.Fa "struct resource_map_request *args" "struct resource_map *map" +.Fc +.Ft int +.Fo bus_unmap_resource +.Fa "device_t dev" "int type" "struct resource *r" "struct resource_map *map" +.Fc +.Ft void +.Fn resource_init_map_request "struct resource_map_request *args" +.Sh DESCRIPTION +These functions create or destroy a mapping of a previously activated +resource. +Mappings permit CPU access to the resource via the +.Xr bus_space 9 +API. +.Pp +The arguments are as follows: +.Bl -tag -width indent +.It Fa dev +The device that owns the resource. +.It Fa type +The type of resource to map. +It is one of: +.Pp +.Bl -tag -width ".Dv SYS_RES_MEMORY" -compact +.It Dv SYS_RES_IOPORT +for I/O ports +.It Dv SYS_RES_MEMORY +for I/O memory +.El +.It Fa r +A pointer to the +.Vt "struct resource" +returned by +.Xr bus_alloc_resource 9 . +.It Fa args +A set of optional properties to apply when creating a mapping. +This argument can be set to +.Dv NULL +to request a mapping of the entire resource with the default properties. +.It Fa map +The resource mapping to create or destroy. +.El +.Ss Resource Mappings +Resource mappings are described by a +.Vt "struct resource_map" +object. +This structure contains a +.Xr bus_space 9 +tag and handle in the +.Va r_bustag +and +.Va r_bushandle +members that can be used for CPU access to the mapping. +The structure also contains a +.Va r_vaddr +member which contains the virtual address of the mapping if one exists. +.Pp +The wrapper API for +.Vt "struct resource" +objects described in +.Xr bus_activate_resource 9 +can also be used with +.Vt "struct resource_map" . +For example, +a pointer to a mapping object can be passed as the first argument to +.Fn bus_read_4 . +This wrapper API is preferred over using the +.Va r_bustag +and +.Va r_bushandle +members directly. +.Ss Optional Mapping Properties +The +.Vt "struct resource_map_request" +object passed in +.Fa args +can be used to specify optional properties of a mapping. +The structure must be initialized by invoking +.Fn resource_init_map_request . +Properties are then specified by setting one or more of these members: +.Bl -tag -width indent +.It Va offset , length +These two members specify a region of the resource to map. +By default a mapping is created for the entire resource. +The +.Va offset +is relative to the start of the resource. +.It Va memattr +Specifies a memory attribute to use when mapping the resource. +By default memory mappings use the +.Dv VM_MEMATTR_UNCACHEABLE +attribute. +.El +.Sh EXAMPLES +This maps a PCI memory BAR with the write-combining memory attribute and +reads the first 32-bit word: +.Bd -literal + struct resource *r; + struct resource_map map; + struct resource_map_args args; + uint32_t val; + int rid; + + rid = PCIR_BAR(0); + r = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE | + RF_UNMAPPED); + resource_init_map_request(&args); + args.memattr = VM_MEMATTR_WRITE_COMBINING; + bus_map_resource(dev, SYS_RES_MEMORY, r, &args, &map); + val = bus_read_4(&map, 0); +.Ed +.Pp +.Sh RETURN VALUES +Zero is returned on success, otherwise an error is returned. +.Sh SEE ALSO +.Xr bus_activate_resource 9 , +.Xr bus_alloc_resource 9 , +.Xr bus_space 9 , +.Xr device 9 , +.Xr driver 9 +.Sh AUTHORS +This manual page was written by +.An John Baldwin Aq Mt jhb@FreeBSD.org . Index: share/man/man9/rman.9 =================================================================== --- share/man/man9/rman.9 +++ share/man/man9/rman.9 @@ -50,6 +50,8 @@ .Nm rman_get_device , .Nm rman_get_size , .Nm rman_get_flags , +.Nm rman_set_mapping , +.Nm rman_get_mapping , .Nm rman_set_virtual , .Nm rman_get_virtual , .Nm rman_set_bustag , @@ -109,6 +111,10 @@ .Ft u_int .Fn rman_get_flags "struct resource *r" .Ft void +.Fn rman_set_mapping "struct resource *r" "struct resource_map *map" +.Ft void +.Fn rman_get_mapping "struct resource *r" "struct resource_map *map" +.Ft void .Fn rman_set_virtual "struct resource *r" "void *v" .Ft "void *" .Fn rman_get_virtual "struct resource *r" @@ -143,6 +149,7 @@ #define RF_SHAREABLE 0x0004 /* resource permits contemporaneous sharing */ #define RF_FIRSTSHARE 0x0020 /* first in sharing list */ #define RF_PREFETCHABLE 0x0040 /* resource is prefetchable */ +#define RF_UNMAPPED 0x0100 /* don't map resource when activating */ .Ed .Pp Bits 15:10 of the flag register are used to represent the desired alignment @@ -444,6 +451,26 @@ function can be used to retrieve the KVA once set. .Pp The +.Fn rman_set_mapping +function is used to associate a resource mapping with a resource +.Fa r . +The mapping must cover the entire resource. +Setting a mapping sets the associated +.Xr bus_space 9 +handle and tag for +.Fa r +as well as the kernel virtual address if the mapping contains one. +These individual values can be retrieved via +.Fn rman_get_bushandle , +.Fn rman_get_bustag , +and +.Fn rman_get_virtual . +.Pp +The +.Fn rman_get_mapping +function can be used to retrieve the associated resource mapping once set. +.Pp +The .Fn rman_set_rid function associates a resource identifier with a resource .Fa r . @@ -459,7 +486,9 @@ .Xr bus_activate_resource 9 , .Xr bus_adjust_resource 9 , .Xr bus_alloc_resource 9 , +.Xr bus_map_resource 9 , .Xr bus_release_resource 9 , +.Xr bus_space 9 , .Xr bus_set_resource 9 , .Xr mutex 9 .Sh AUTHORS Index: sys/kern/bus_if.m =================================================================== --- sys/kern/bus_if.m +++ sys/kern/bus_if.m @@ -287,8 +287,9 @@ * @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 @@ 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 Index: sys/kern/subr_bus.c =================================================================== --- sys/kern/subr_bus.c +++ sys/kern/subr_bus.c @@ -63,6 +63,7 @@ #include #include +#include SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL); SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW, NULL, NULL); @@ -3050,6 +3051,15 @@ * 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 @@ } /** + * @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 @@ } /** + * @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 Index: sys/kern/subr_rman.c =================================================================== --- sys/kern/subr_rman.c +++ sys/kern/subr_rman.c @@ -897,6 +897,27 @@ } 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) { Index: sys/sparc64/include/vm.h =================================================================== --- sys/sparc64/include/vm.h +++ sys/sparc64/include/vm.h @@ -31,5 +31,6 @@ /* Memory attribute configuration is not (yet) implemented. */ #define VM_MEMATTR_DEFAULT 0 +#define VM_MEMATTR_UNCACHEABLE 0 #endif /* !_MACHINE_VM_H_ */ Index: sys/sys/bus.h =================================================================== --- sys/sys/bus.h +++ sys/sys/bus.h @@ -294,6 +294,31 @@ KOBJ_CLASS_FIELDS; }; +/** + * @brief A resource mapping. + */ +struct resource_map { + bus_space_tag_t r_bustag; + bus_space_handle_t r_bushandle; + bus_size_t r_size; + void *r_vaddr; +}; + +/** + * @brief Optional properties of a resource mapping request. + */ +struct resource_map_request { + size_t size; + rman_res_t offset; + rman_res_t length; + vm_memattr_t memattr; +}; + +void resource_init_map_request_impl(struct resource_map_request *_args, + size_t _sz); +#define resource_init_map_request(rmr) \ + resource_init_map_request_impl((rmr), sizeof(*(rmr))) + /* * Definitions for drivers which need to keep simple lists of resources * for their child devices. @@ -407,6 +432,10 @@ int bus_generic_get_domain(device_t dev, device_t child, int *domain); struct resource_list * bus_generic_get_resource_list (device_t, device_t); +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); void bus_generic_new_pass(device_t dev); int bus_print_child_header(device_t dev, device_t child); int bus_print_child_domain(device_t dev, device_t child); @@ -440,6 +469,9 @@ int bus_generic_suspend_child(device_t dev, device_t child); int bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq, void *cookie); +int bus_generic_unmap_resource(device_t dev, device_t child, int type, + struct resource *r, + struct resource_map *map); int bus_generic_write_ivar(device_t dev, device_t child, int which, uintptr_t value); int bus_null_rescan(device_t dev); @@ -469,6 +501,11 @@ struct resource *r); int bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r); +int bus_map_resource(device_t dev, int type, struct resource *r, + struct resource_map_request *args, + struct resource_map *map); +int bus_unmap_resource(device_t dev, int type, struct resource *r, + struct resource_map *map); int bus_get_cpus(device_t dev, enum cpu_sets op, size_t setsize, struct _cpuset *cpuset); bus_dma_tag_t bus_get_dma_tag(device_t dev); Index: sys/sys/rman.h =================================================================== --- sys/sys/rman.h +++ sys/sys/rman.h @@ -47,6 +47,7 @@ #define RF_FIRSTSHARE 0x0020 /* first in sharing list */ #define RF_PREFETCHABLE 0x0040 /* resource is prefetchable */ #define RF_OPTIONAL 0x0080 /* for bus_alloc_resources() */ +#define RF_UNMAPPED 0x0100 /* don't map resource when activating */ #define RF_ALIGNMENT_SHIFT 10 /* alignment size bit starts bit 10 */ #define RF_ALIGNMENT_MASK (0x003F << RF_ALIGNMENT_SHIFT) @@ -105,6 +106,7 @@ }; struct resource_i; +struct resource_map; TAILQ_HEAD(resource_head, resource_i); @@ -128,6 +130,7 @@ rman_res_t rman_get_end(struct resource *); struct device *rman_get_device(struct resource *); u_int rman_get_flags(struct resource *); +void rman_get_mapping(struct resource *, struct resource_map *); int rman_get_rid(struct resource *); rman_res_t rman_get_size(struct resource *); rman_res_t rman_get_start(struct resource *); @@ -151,6 +154,7 @@ void rman_set_bustag(struct resource *_r, bus_space_tag_t _t); void rman_set_device(struct resource *_r, struct device *_dev); void rman_set_end(struct resource *_r, rman_res_t _end); +void rman_set_mapping(struct resource *, struct resource_map *); void rman_set_rid(struct resource *_r, int _rid); void rman_set_start(struct resource *_r, rman_res_t _start); void rman_set_virtual(struct resource *_r, void *_v); Index: sys/vm/vm.h =================================================================== --- sys/vm/vm.h +++ sys/vm/vm.h @@ -109,8 +109,9 @@ typedef int boolean_t; /* - * The exact set of memory attributes is machine dependent. However, every - * machine is required to define VM_MEMATTR_DEFAULT. + * The exact set of memory attributes is machine dependent. However, + * every machine is required to define VM_MEMATTR_DEFAULT and + * VM_MEMATTR_UNCACHEABLE. */ typedef char vm_memattr_t; /* memory attribute codes */ Index: sys/x86/x86/nexus.c =================================================================== --- sys/x86/x86/nexus.c +++ sys/x86/x86/nexus.c @@ -114,6 +114,12 @@ struct resource *); static int nexus_deactivate_resource(device_t, device_t, int, int, struct resource *); +static int nexus_map_resource(device_t bus, device_t child, int type, + struct resource *r, + struct resource_map_request *argsp, + struct resource_map *map); +static int nexus_unmap_resource(device_t bus, device_t child, int type, + struct resource *r, struct resource_map *map); static int nexus_release_resource(device_t, device_t, int, int, struct resource *); static int nexus_setup_intr(device_t, device_t, struct resource *, int flags, @@ -154,6 +160,8 @@ DEVMETHOD(bus_release_resource, nexus_release_resource), DEVMETHOD(bus_activate_resource, nexus_activate_resource), DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), + DEVMETHOD(bus_map_resource, nexus_map_resource), + DEVMETHOD(bus_unmap_resource, nexus_unmap_resource), DEVMETHOD(bus_setup_intr, nexus_setup_intr), DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), #ifdef SMP @@ -432,11 +440,81 @@ nexus_activate_resource(device_t bus, device_t child, int type, int rid, struct resource *r) { + struct resource_map map; + int error; + + error = rman_activate_resource(r); + if (error != 0) + return (error); + + if (!(rman_get_flags(r) & RF_UNMAPPED) && + (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { + error = nexus_map_resource(bus, child, type, r, NULL, &map); + if (error) { + rman_deactivate_resource(r); + return (error); + } + + rman_set_mapping(r,&map); + } + return (0); +} + +static int +nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, + struct resource *r) +{ + struct resource_map map; + int error; + + error = rman_deactivate_resource(r); + if (error) + return (error); + + if (!(rman_get_flags(r) & RF_UNMAPPED) && + (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { + rman_get_mapping(r, &map); + nexus_unmap_resource(bus, child, type, r, &map); + } + return (0); +} + +static int +nexus_map_resource(device_t bus, device_t child, int type, struct resource *r, + struct resource_map_request *argsp, struct resource_map *map) +{ + struct resource_map_request args; + rman_res_t end, length, start; #ifdef PC98 - bus_space_handle_t bh; int error; #endif - void *vaddr; + + /* Resources must be active to be mapped. */ + if (!(rman_get_flags(r) & RF_ACTIVE)) + return (ENXIO); + + /* Mappings are only supported on I/O and memory resources. */ + switch (type) { + case SYS_RES_IOPORT: + case SYS_RES_MEMORY: + break; + default: + return (EINVAL); + } + + resource_init_map_request(&args); + if (argsp != NULL) + bcopy(argsp, &args, imin(argsp->size, args.size)); + start = rman_get_start(r) + args.offset; + if (args.length == 0) + length = rman_get_size(r); + else + length = args.length; + end = start + length - 1; + if (start > rman_get_end(r) || start < rman_get_start(r)) + return (EINVAL); + if (end > rman_get_end(r) || end < start) + return (EINVAL); /* * If this is a memory resource, map it into the kernel. @@ -445,58 +523,64 @@ case SYS_RES_IOPORT: #ifdef PC98 error = i386_bus_space_handle_alloc(X86_BUS_SPACE_IO, - rman_get_start(r), rman_get_size(r), &bh); + start, length, &map->r_bushandle); if (error) return (error); - rman_set_bushandle(r, bh); #else - rman_set_bushandle(r, rman_get_start(r)); + map->r_bushandle = start; #endif - rman_set_bustag(r, X86_BUS_SPACE_IO); + map->r_bustag = X86_BUS_SPACE_IO; + map->r_size = length; + map->r_vaddr = NULL; break; case SYS_RES_MEMORY: #ifdef PC98 error = i386_bus_space_handle_alloc(X86_BUS_SPACE_MEM, - rman_get_start(r), rman_get_size(r), &bh); + start, length, &map->r_bushandle); if (error) return (error); #endif - vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r)); - rman_set_virtual(r, vaddr); - rman_set_bustag(r, X86_BUS_SPACE_MEM); + map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr); + map->r_bustag = X86_BUS_SPACE_MEM; + map->r_size = length; + + /* + * PC-98 stores the virtual address as a member of the + * structure in the handle. On plain x86, the handle is + * the virtual address. + */ #ifdef PC98 - /* PC-98: the type of bus_space_handle_t is the structure. */ - bh->bsh_base = (bus_addr_t) vaddr; - rman_set_bushandle(r, bh); + map->r_bushandle->bsh_base = (bus_addr_t)map->r_vaddr; #else - /* IBM-PC: the type of bus_space_handle_t is u_int */ - rman_set_bushandle(r, (bus_space_handle_t) vaddr); + map->r_bushandle = (bus_space_handle_t)map->r_vaddr; #endif + break; } - return (rman_activate_resource(r)); + return (0); } static int -nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, - struct resource *r) +nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r, + struct resource_map *map) { - + /* * If this is a memory resource, unmap it. */ - if (type == SYS_RES_MEMORY) { - pmap_unmapdev((vm_offset_t)rman_get_virtual(r), - rman_get_size(r)); - } + switch (type) { + case SYS_RES_MEMORY: + pmap_unmapdev((vm_offset_t)map->r_vaddr, map->r_size); + /* FALLTHROUGH */ + case SYS_RES_IOPORT: #ifdef PC98 - if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { - bus_space_handle_t bh; - - bh = rman_get_bushandle(r); - i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz); - } + i386_bus_space_handle_free(map->r_bustag, map->r_bushandle, + map->r_bushandle->bsh_sz); #endif - return (rman_deactivate_resource(r)); + break; + default: + return (EINVAL); + } + return (0); } static int