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 \ 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: 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 @@ -3050,6 +3050,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 +4069,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 +4464,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/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) 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,12 +440,79 @@ nexus_activate_resource(device_t bus, device_t child, int type, int rid, struct resource *r) { + struct resource_map map; + int error; + void *vaddr; + + error = rman_activate_resource(r); + if (error != 0) + return (error); + + if (rman_get_flags(r) & RF_UNMAPPED) + return (0); + + error = nexus_map_resource(bus, child, type, r, NULL, &map); + if (error) { + rman_deactivate_resource(r); + return (error); + } + + rman_set_bustag(r, map->r_tag); + rman_set_bushandle(r, map->r_handle); + rman_set_virtual(r, map->r_vaddr); + 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) + return (0); + + map.r_tag = rman_get_bustag(r); + map.r_handle = rman_get_bushandle(r); + map.r_size = rman_get_size(r); + map.r_vaddr = rman_get_virtual(r); + 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 start, size; #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); + + 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.size == 0) + size = rman_get_size(r); + else + size = args.size; + if (start > rman_get_end(r) || start < rman_get_start(r)) + return (EINVAL); + if (start + size > rman_get_end(r) || start + size < start) + return (EINVAL); + /* * If this is a memory resource, map it into the kernel. */ @@ -445,58 +520,59 @@ 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, size, &map->r_handle); if (error) return (error); - rman_set_bushandle(r, bh); #else - rman_set_bushandle(r, rman_get_start(r)); + map->r_handle = start; #endif - rman_set_bustag(r, X86_BUS_SPACE_IO); + map->r_tag = X86_BUS_SPACE_IO; + map->r_size = size; + 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, size, &map->r_handle); 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, size, args.memattr); + map->r_tag = X86_BUS_SPACE_MEM; + map->r_size = size; + + /* + * 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_handle->bsh_base = (bus_addr_t)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_handle = (bus_space_handle_t)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)); - } + if (type == SYS_RES_MEMORY) + pmap_unmapdev((vm_offset_t)map->r_vaddr, map->r_size); #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_tag, map->r_handle, + map->r_handle->bsh_sz); } #endif - return (rman_deactivate_resource(r)); + return (0); } static int