2 .\" Copyright (c) 2000, Sun Microsystems, Inc., All Rights Reserved.
3 .\" Copyright 1989 AT&T
4 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
5 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
6 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
7 .TH DEVMAP 9E "Jan 15, 1997"
9 devmap \- validate and translate virtual mapping for memory mapped device
14 #include <sys/sunddi.h>
18 \fBint prefix\fR\fBdevmap\fR(\fBdev_t\fR \fIdev\fR, \fBdevmap_cookie_t\fR \fIdhp\fR, \fBoffset_t\fR \fIoff\fR,
19 \fBsize_t\fR \fIlen\fR, \fBsize_t *\fR\fImaplen\fR, \fBuint_t\fR \fImodel\fR);
25 Solaris DDI specific (Solaris DDI).
33 Device whose memory is to be mapped.
42 An opaque mapping handle that the system uses to describe the mapping.
51 User offset within the logical device memory at which the mapping begins.
60 Length (in bytes) of the mapping to be mapped.
69 Pointer to length (in bytes) of mapping that has been validated. \fImaplen\fR
70 is less than or equal to \fIlen\fR.
79 The data model type of the current thread.
85 \fBdevmap()\fR is a required entry point for character drivers supporting
86 memory-mapped devices if the drivers use the devmap framework to set up the
87 mapping. A memory mapped device has memory that can be mapped into a process's
88 address space. The \fBmmap\fR(2) system call, when applied to a character
89 special file, allows this device memory to be mapped into user space for direct
90 access by the user applications.
93 As a result of a \fBmmap\fR(2) system call, the system calls the \fBdevmap()\fR
94 entry point during the mapping setup when \fBD_DEVMAP\fR is set in the
95 \fBcb_flag\fR field of the \fBcb_ops\fR(9S) structure, and any of the following
101 \fBddi_devmap_segmap\fR(9F) is used as the \fBsegmap\fR(9E) entry point.
107 \fBsegmap\fR(9E) entry point is set to \fINULL.\fR
113 \fBmmap\fR(9E) entry point is set to \fINULL.\fR
117 Otherwise \fBEINVAL\fR will be returned to \fBmmap\fR(2).
120 Device drivers should use \fBdevmap()\fR to validate the user mappings to the
121 device, to translate the logical offset, \fIoff\fR, to the corresponding
122 physical offset within the device address space, and to pass the mapping
123 information to the system for setting up the mapping.
126 \fIdhp\fR is a device mapping handle that the system uses to describe a mapping
127 to a memory that is either contiguous in physical address space or in kernel
128 virtual address space. The system may create multiple mapping handles in one
129 \fBmmap\fR(2) system call (for example, if the mapping contains multiple
130 physically discontiguous memory regions).
133 \fImodel\fR returns the C Language Type Model which the current thread expects.
134 It is set to \fBDDI_MODEL_ILP32\fR if the current thread expects 32-bit (
135 \fIILP32\fR) semantics, or \fBDDI_MODEL_LP64\fR if the current thread expects
136 64-bit ( \fILP64\fR) semantics. \fImodel\fR is used in combination with
137 \fBddi_model_convert_from\fR(9F) to determine whether there is a data model
138 mismatch between the current thread and the device driver. The device driver
139 might have to adjust the shape of data structures before exporting them to a
140 user thread which supports a different data model.
143 \fBdevmap()\fR should return \fBEINVAL\fR if the logical offset, \fIoff\fR, is
144 out of the range of memory exported by the device to user space. If \fIoff\fR +
145 \fIlen\fR exceeds the range of the contiguous memory, \fBdevmap()\fR should
146 return the length from \fIoff\fR to the end of the contiguous memory region.
147 The system will repeatedly call \fBdevmap()\fR until the original mapping
148 length is satisfied. The driver sets \fI*maplen\fR to the validated length
149 which must be either less than or equal to \fIlen\fR.
152 The \fBdevmap()\fR entry point must initialize the mapping parameters before
153 passing them to the system through either \fBdevmap_devmem_setup\fR(9F) (if the
154 memory being mapped is device memory) or \fBdevmap_umem_setup\fR(9F) (if the
155 memory being mapped is kernel memory). The \fBdevmap()\fR entry point
156 initializes the mapping parameters by mapping the control callback structure
157 (see \fBdevmap_callback_ctl\fR(9S)), the device access attributes, mapping
158 length, maximum protection possible for the mapping, and optional mapping
159 flags. See \fBdevmap_devmem_setup\fR(9F) and \fBdevmap_umem_setup\fR(9F) for
160 further information on initializing the mapping parameters.
163 The system will copy the driver's \fBdevmap_callback_ctl\fR(9S) data into its
164 private memory so the drivers do not need to keep the data structure after the
165 return from either \fBdevmap_devmem_setup\fR(9F) or
166 \fBdevmap_umem_setup\fR(9F).
169 For device mappings, the system establishes the mapping to the physical address
170 that corresponds to \fIoff\fR by passing the register number and the offset
171 within the register address space to \fBdevmap_devmem_setup\fR(9F).
174 For kernel memory mapping, the system selects a user virtual address that is
175 aligned with the kernel address being mapped for cache coherence.
183 Successful completion.
197 \fBExample 1 \fRImplementing the \fBdevmap()\fR Entry Point
200 The following is an example of the implementation for the \fBdevmap()\fR entry
201 point. For mapping device memory, \fBdevmap()\fR calls
202 \fBdevmap_devmem_setup\fR(9F) with the register number, \fIrnumber\fR, and the
203 offset within the register, \fIroff\fR. For mapping kernel memory, the driver
204 must first allocate the kernel memory using \fBddi_umem_alloc\fR(9F). For
205 example, \fBddi_umem_alloc\fR(9F) can be called in the \fBattach\fR(9E)
206 routine. The resulting kernel memory cookie is stored in the driver soft state
207 structure, which is accessible from the \fBdevmap()\fR entry point. See
208 \fBddi_soft_state\fR(9F). \fBdevmap()\fR passes the cookie obtained from
209 \fBddi_umem_alloc\fR(9F) and the offset within the allocated kernel memory to
210 \fBdevmap_umem_setup\fR(9F). The corresponding \fBddi_umem_free\fR(9F) can be
211 made in the \fBdetach\fR(9E) routine to free up the kernel memory.
217 #define MAPPING_SIZE 0x2000 /* size of the mapping */
218 #define MAPPING_START 0x70000000 /* logical offset at beginning
221 struct devmap_callback_ctl xxmap_ops = {
222 DEVMAP_OPS_REV, /* devmap_ops version number */
223 xxmap_map, /* devmap_ops map routine */
224 xxmap_access, /* devmap_ops access routine */
225 xxmap_dup, /* devmap_ops dup routine */
226 xxmap_unmap, /* devmap_ops unmap routine */
231 xxdevmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
232 size_t *maplen, uint_t model)
236 struct ddi_device_acc_attr *endian_attr;
237 struct devmap_callback_ctl *callbackops = NULL;
238 ddi_umem_cookie_t cookie;
248 /* get device soft state */
249 instance = getminor(dev);
250 xsp = ddi_get_soft_state(statep, instance);
255 /* check for a valid offset */
256 if ( \fIoff is invalid\fR )
258 /* check if len is within the range of contiguous memory */
259 if ( \fB(\fR\fIoff\fR\fB + \fR\fIlen\fR\fB)\fR \fIis contiguous\fR\fB\&.)\fR
262 length = MAPPING_START + MAPPING_SIZE - off;
264 /* device access attributes */
265 endian_attr = xsp->endian_attr;
267 if ( \fI off is referring to a device memory. \fR ) {
268 /* assign register related parameters */
269 rnumber = XXX; /* index to register set at off */
270 roff = XXX; /* offset of rnumber at local bus */
271 callbackops = &xxmap_ops; /* do all callbacks for this mapping */
272 maxprot = PROT_ALL; /* allowing all access */
273 if ((err = devmap_devmem_setup(dhp, dip, callbackops, rnumber, roff,
274 length, maxprot, flags, endian_attr)) < 0)
279 } else if (\fI off is referring to a kernel memory.\fR) {
280 cookie = xsp->cookie; /* cookie is obtained from
281 ddi_umem_alloc(9F) */
282 koff = XXX; /* offset within the kernel memory. */
283 callbackops = NULL; /* don't do callback for this mapping */
284 maxprot = PROT_ALL; /* allowing all access */
285 if ((err = devmap_umem_setup(dhp, dip, callbackops, cookie, koff,
286 length, maxprot, flags, endian_attr)) < 0)
299 \fBmmap\fR(2), \fBattach\fR(9E), \fBdetach\fR(9E), \fBmmap\fR(9E),
300 \fBsegmap\fR(9E), \fBddi_devmap_segmap\fR(9F),
301 \fBddi_model_convert_from\fR(9F), \fBddi_soft_state\fR(9F),
302 \fBddi_umem_alloc\fR(9F), \fBddi_umem_free\fR(9F),
303 \fBdevmap_devmem_setup\fR(9F), \fBdevmap_setup\fR(9F),
304 \fBdevmap_umem_setup\fR(9F), \fBcb_ops\fR(9S), \fBdevmap_callback_ctl\fR(9S)
307 \fIWriting Device Drivers\fR