xref: /onnv-gate/usr/src/uts/common/sys/ddidevmap.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	_SYS_DDIDEVMAP_H
28*0Sstevel@tonic-gate #define	_SYS_DDIDEVMAP_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #ifdef	__cplusplus
33*0Sstevel@tonic-gate extern "C" {
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #ifdef	_KERNEL
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <sys/mman.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate struct devmap_info {
41*0Sstevel@tonic-gate 	size_t	length;		/* and this length */
42*0Sstevel@tonic-gate 	size_t	page_size;	/* pte page size selected by framework */
43*0Sstevel@tonic-gate 	size_t	offset;		/* optimal page size based on this offset */
44*0Sstevel@tonic-gate 	ushort_t valid_flag;	/* flag to indicate the validity of data */
45*0Sstevel@tonic-gate 	uchar_t	byte_order;	/* the  endian characteristics of the mapping */
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate 	/*
48*0Sstevel@tonic-gate 	 * describes  order in which the CPU will reference data.
49*0Sstevel@tonic-gate 	 */
50*0Sstevel@tonic-gate 	uchar_t	data_order;
51*0Sstevel@tonic-gate };
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate typedef void * ddi_umem_cookie_t;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * umem callback function vector for drivers
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  * NOTE: IMPORTANT!  When umem_lockmemory is called with a valid
59*0Sstevel@tonic-gate  * umem_callback_ops and DDI_UMEMLOCK_LONGTERM set, the 'cleanup'
60*0Sstevel@tonic-gate  * callback function may be called AFTER a call to ddi_umem_lock.
61*0Sstevel@tonic-gate  * It is the users responsibility to make sure that ddi_umem_lock is
62*0Sstevel@tonic-gate  * called ONLY once for each ddi_umem_lock/umem_lockmemory cookie.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate #define	UMEM_CALLBACK_VERSION 1
65*0Sstevel@tonic-gate struct umem_callback_ops {
66*0Sstevel@tonic-gate 	int	cbo_umem_callback_version;	/* version number */
67*0Sstevel@tonic-gate 	void (*cbo_umem_lock_cleanup)(ddi_umem_cookie_t *);
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate struct ddi_umem_cookie {
71*0Sstevel@tonic-gate 	size_t	size;	/* size of allocation */
72*0Sstevel@tonic-gate 	caddr_t	cvaddr;	/* cookie virtual address. */
73*0Sstevel@tonic-gate 			/* KMEM - kvaddr returned from ddi_umem_alloc() */
74*0Sstevel@tonic-gate 			/* For LOCKEDUMEM - user address of backing store */
75*0Sstevel@tonic-gate 			/* For TRASH_UMEM - unused */
76*0Sstevel@tonic-gate 	kmutex_t	lock;
77*0Sstevel@tonic-gate 	uint_t		type;	/* see below for umem_cookie types */
78*0Sstevel@tonic-gate 	/*
79*0Sstevel@tonic-gate 	 * Following 4 members are used for UMEM_LOCKED cookie type
80*0Sstevel@tonic-gate 	 */
81*0Sstevel@tonic-gate 	page_t		**pparray;	/* shadow list from as_pagelock */
82*0Sstevel@tonic-gate 	void		*procp;		/* user process owning backing store */
83*0Sstevel@tonic-gate 	struct as	*asp;		/* as ptr for use by ddi_umem_unlock */
84*0Sstevel@tonic-gate 	enum seg_rw	s_flags;	/* flags used during pagelock/fault */
85*0Sstevel@tonic-gate 	/*
86*0Sstevel@tonic-gate 	 * locked indicates underlying memory locked for KMEM_PAGEABLE
87*0Sstevel@tonic-gate 	 * locked is a count of for how many pages this has been locked
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	uint_t		locked;
90*0Sstevel@tonic-gate 	struct umem_callback_ops callbacks;
91*0Sstevel@tonic-gate 	/*
92*0Sstevel@tonic-gate 	 * cook_refcnt used in UMEM_LOCKED type
93*0Sstevel@tonic-gate 	 */
94*0Sstevel@tonic-gate 	ulong_t		cook_refcnt;	/* cookie reference count */
95*0Sstevel@tonic-gate 	struct ddi_umem_cookie *unl_forw;   /* list ptr for unlock cookies */
96*0Sstevel@tonic-gate 	void		*lockmem_proj;	/* project ptr for resource mgmt */
97*0Sstevel@tonic-gate };
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate typedef struct as *ddi_as_handle_t;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * type of umem_cookie:
104*0Sstevel@tonic-gate  *    pageable memory allocated from segkp segment driver
105*0Sstevel@tonic-gate  *    non-pageable memory allocated from kmem_getpages()
106*0Sstevel@tonic-gate  *    locked umem allocated from ddi_umem_lock
107*0Sstevel@tonic-gate  *    trash umem maps all user virtual addresses to a common trash page
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate #define	KMEM_PAGEABLE		0x100	/* un-locked kernel memory */
110*0Sstevel@tonic-gate #define	KMEM_NON_PAGEABLE	0x200	/* locked kernel memeory */
111*0Sstevel@tonic-gate #define	UMEM_LOCKED		0x400	/* locked user process memeory */
112*0Sstevel@tonic-gate #define	UMEM_TRASH		0x800	/* trash page mapping */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate typedef struct __devmap_pmem_cookie *devmap_pmem_cookie_t;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate typedef void *devmap_cookie_t;
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate struct devmap_callback_ctl {
119*0Sstevel@tonic-gate 	int	devmap_rev;		/* devmap_callback_ctl version number */
120*0Sstevel@tonic-gate 	int	(*devmap_map)(devmap_cookie_t dhp, dev_t dev, uint_t flags,
121*0Sstevel@tonic-gate 				offset_t off, size_t len, void **pvtp);
122*0Sstevel@tonic-gate 	int	(*devmap_access)(devmap_cookie_t dhp, void *pvtp, offset_t off,
123*0Sstevel@tonic-gate 				size_t len, uint_t type, uint_t rw);
124*0Sstevel@tonic-gate 	int	(*devmap_dup)(devmap_cookie_t dhp, void *pvtp,
125*0Sstevel@tonic-gate 				devmap_cookie_t new_dhp, void **new_pvtp);
126*0Sstevel@tonic-gate 	void	(*devmap_unmap)(devmap_cookie_t dhp, void *pvtp, offset_t off,
127*0Sstevel@tonic-gate 				size_t len, devmap_cookie_t new_dhp1,
128*0Sstevel@tonic-gate 				void **new_pvtp1, devmap_cookie_t new_dhp2,
129*0Sstevel@tonic-gate 				void **new_pvtp2);
130*0Sstevel@tonic-gate };
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate struct devmap_softlock {
133*0Sstevel@tonic-gate 	ulong_t		id;	/* handle grouping id */
134*0Sstevel@tonic-gate 	dev_t		dev; /* Device to which we are mapping */
135*0Sstevel@tonic-gate 	struct		devmap_softlock	*next;
136*0Sstevel@tonic-gate 	kmutex_t	lock;
137*0Sstevel@tonic-gate 	kcondvar_t	cv;
138*0Sstevel@tonic-gate 	int		refcnt;	/* Number of threads with mappings */
139*0Sstevel@tonic-gate 	ssize_t		softlocked;
140*0Sstevel@tonic-gate };
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate struct devmap_ctx {
143*0Sstevel@tonic-gate 	ulong_t		id; /* handle grouping id */
144*0Sstevel@tonic-gate 	dev_info_t	*dip; /* Device info struct for tracing context */
145*0Sstevel@tonic-gate 	struct devmap_ctx *next;
146*0Sstevel@tonic-gate 	kmutex_t	lock;
147*0Sstevel@tonic-gate 	kcondvar_t	cv;
148*0Sstevel@tonic-gate 	int		refcnt; /* Number of threads with mappings */
149*0Sstevel@tonic-gate 	uint_t		oncpu; /* this context is running on a cpu */
150*0Sstevel@tonic-gate 	timeout_id_t	timeout; /* Timeout ID */
151*0Sstevel@tonic-gate };
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate  * Fault information passed to the driver fault handling routine.
155*0Sstevel@tonic-gate  * The DEVMAP_LOCK and DEVMAP_UNLOCK are used by software
156*0Sstevel@tonic-gate  * to lock and unlock pages for physical I/O.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate enum devmap_fault_type {
159*0Sstevel@tonic-gate 	DEVMAP_ACCESS,		/* invalid page */
160*0Sstevel@tonic-gate 	DEVMAP_PROT,		/* protection fault */
161*0Sstevel@tonic-gate 	DEVMAP_LOCK,		/* software requested locking */
162*0Sstevel@tonic-gate 	DEVMAP_UNLOCK		/* software requested unlocking */
163*0Sstevel@tonic-gate };
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate  * seg_rw gives the access type for a fault operation
167*0Sstevel@tonic-gate  */
168*0Sstevel@tonic-gate enum devmap_rw {
169*0Sstevel@tonic-gate 	DEVMAP_OTHER,		/* unknown or not touched */
170*0Sstevel@tonic-gate 	DEVMAP_READ,		/* read access attempted */
171*0Sstevel@tonic-gate 	DEVMAP_WRITE,		/* write access attempted */
172*0Sstevel@tonic-gate 	DEVMAP_EXEC,		/* execution access attempted */
173*0Sstevel@tonic-gate 	DEVMAP_CREATE		/* create if page doesn't exist */
174*0Sstevel@tonic-gate };
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate typedef struct devmap_handle {
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	/*
179*0Sstevel@tonic-gate 	 * physical offset at the beginning of mapping.
180*0Sstevel@tonic-gate 	 */
181*0Sstevel@tonic-gate 	offset_t	dh_roff;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/*
184*0Sstevel@tonic-gate 	 * user offset at the beginning of mapping.
185*0Sstevel@tonic-gate 	 */
186*0Sstevel@tonic-gate 	offset_t	dh_uoff;
187*0Sstevel@tonic-gate 	size_t		dh_len;		/* length of mapping */
188*0Sstevel@tonic-gate 	dev_t		dh_dev;		/* dev_t for this mapping */
189*0Sstevel@tonic-gate 	caddr_t		dh_cvaddr;  /* cookie virtual address */
190*0Sstevel@tonic-gate 	caddr_t		dh_uvaddr;  /* user address within dh_seg */
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/*
193*0Sstevel@tonic-gate 	 * Lock protects fields that can change during remap
194*0Sstevel@tonic-gate 	 * dh_roff, dh_cookie, dh_flags, dh_mmulevel, dh_maxprot,
195*0Sstevel@tonic-gate 	 * dh_pfn, dh_hat_attr
196*0Sstevel@tonic-gate 	 */
197*0Sstevel@tonic-gate 	kmutex_t	dh_lock;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	/*
200*0Sstevel@tonic-gate 	 * to sync. faults for remap and unlocked kvaddr.
201*0Sstevel@tonic-gate 	 */
202*0Sstevel@tonic-gate 	struct seg		*dh_seg; /* segment created for this mapping */
203*0Sstevel@tonic-gate 	void			*dh_pvtp; /* device mapping private data */
204*0Sstevel@tonic-gate 	struct devmap_handle	*dh_next;
205*0Sstevel@tonic-gate 	struct devmap_softlock	*dh_softlock;
206*0Sstevel@tonic-gate 	struct devmap_ctx	*dh_ctx;
207*0Sstevel@tonic-gate 	ddi_umem_cookie_t	dh_cookie;	/* kmem cookie */
208*0Sstevel@tonic-gate 	devmap_pmem_cookie_t	dh_pcookie;	/* pmem cookie */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	/*
211*0Sstevel@tonic-gate 	 * protection flag possible for attempted mapping.
212*0Sstevel@tonic-gate 	 */
213*0Sstevel@tonic-gate 	uint_t		dh_prot;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	/*
216*0Sstevel@tonic-gate 	 * Current maximum protection flag for attempted mapping.
217*0Sstevel@tonic-gate 	 * This controls how dh_prot can be changed in segdev_setprot
218*0Sstevel@tonic-gate 	 * See dh_orig_maxprot below also
219*0Sstevel@tonic-gate 	 */
220*0Sstevel@tonic-gate 	uint_t		dh_maxprot;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	/*
223*0Sstevel@tonic-gate 	 * mmu level corresponds to the Max page size can be use for
224*0Sstevel@tonic-gate 	 * the mapping.
225*0Sstevel@tonic-gate 	 */
226*0Sstevel@tonic-gate 	uint_t		dh_mmulevel;
227*0Sstevel@tonic-gate 	uint_t		dh_flags;   /* see defines below */
228*0Sstevel@tonic-gate 	pfn_t		dh_pfn;		/* pfn corresponds to dh_reg_off */
229*0Sstevel@tonic-gate 	uint_t		dh_hat_attr;
230*0Sstevel@tonic-gate 	clock_t		dh_timeout_length;
231*0Sstevel@tonic-gate 	struct devmap_callback_ctl dh_callbackops;
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate 	/*
234*0Sstevel@tonic-gate 	 * orig_maxprot is what the original mmap set maxprot to.
235*0Sstevel@tonic-gate 	 * This is never modified once it is setup during mmap(2)
236*0Sstevel@tonic-gate 	 * This is different from the current dh_maxprot which can
237*0Sstevel@tonic-gate 	 * be changed in devmap_*_setup/remap
238*0Sstevel@tonic-gate 	 */
239*0Sstevel@tonic-gate 	uint_t		dh_orig_maxprot;
240*0Sstevel@tonic-gate } devmap_handle_t;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate #endif	/* _KERNEL */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * define for devmap_rev
246*0Sstevel@tonic-gate  */
247*0Sstevel@tonic-gate #define	DEVMAP_OPS_REV 1
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate /*
250*0Sstevel@tonic-gate  * defines for devmap_*_setup flag, called by drivers
251*0Sstevel@tonic-gate  */
252*0Sstevel@tonic-gate #define	DEVMAP_DEFAULTS			0x00
253*0Sstevel@tonic-gate #define	DEVMAP_MAPPING_INVALID		0x01 	/* mapping is invalid */
254*0Sstevel@tonic-gate #define	DEVMAP_ALLOW_REMAP		0x02	/* allow remap */
255*0Sstevel@tonic-gate #define	DEVMAP_USE_PAGESIZE		0x04	/* use pagesize for mmu load */
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate /* flags used by drivers */
258*0Sstevel@tonic-gate #define	DEVMAP_SETUP_FLAGS	\
259*0Sstevel@tonic-gate 	(DEVMAP_MAPPING_INVALID | DEVMAP_ALLOW_REMAP | DEVMAP_USE_PAGESIZE)
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate /*
262*0Sstevel@tonic-gate  * defines for dh_flags, these are used internally in devmap
263*0Sstevel@tonic-gate  */
264*0Sstevel@tonic-gate #define	DEVMAP_SETUP_DONE		0x100	/* mapping setup is done */
265*0Sstevel@tonic-gate #define	DEVMAP_LOCK_INITED		0x200	/* locks are initailized */
266*0Sstevel@tonic-gate #define	DEVMAP_LOCKED			0x800	/* dhp is locked. */
267*0Sstevel@tonic-gate #define	DEVMAP_FLAG_LARGE		0x1000  /* cal. optimal pgsize */
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate /*
270*0Sstevel@tonic-gate  * Flags to pass to ddi_umem_alloc and ddi_umem_iosetup
271*0Sstevel@tonic-gate  */
272*0Sstevel@tonic-gate #define	DDI_UMEM_SLEEP		0x0
273*0Sstevel@tonic-gate #define	DDI_UMEM_NOSLEEP	0x01
274*0Sstevel@tonic-gate #define	DDI_UMEM_PAGEABLE	0x02
275*0Sstevel@tonic-gate #define	DDI_UMEM_TRASH		0x04
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /*
278*0Sstevel@tonic-gate  * Flags to pass to ddi_umem_lock to indicate expected access pattern
279*0Sstevel@tonic-gate  * DDI_UMEMLOCK_READ implies the memory being locked will be read
280*0Sstevel@tonic-gate  * (e.g., data read from memory is written out to the disk or network)
281*0Sstevel@tonic-gate  * DDI_UMEMLOCK_WRITE implies the memory being locked will be written
282*0Sstevel@tonic-gate  * (e.g., data from the disk or network is written to memory)
283*0Sstevel@tonic-gate  * Both flags may be set in the call to ddi_umem_lock,
284*0Sstevel@tonic-gate  * Note that this corresponds to the VM subsystem definition of read/write
285*0Sstevel@tonic-gate  * and also correspond to the prots set in devmap
286*0Sstevel@tonic-gate  * When doing I/O, B_READ/B_WRITE are used which have exactly the opposite
287*0Sstevel@tonic-gate  * meaning. Be careful when using it both for I/O and devmap
288*0Sstevel@tonic-gate  *
289*0Sstevel@tonic-gate  *
290*0Sstevel@tonic-gate  */
291*0Sstevel@tonic-gate #define	DDI_UMEMLOCK_READ	0x01
292*0Sstevel@tonic-gate #define	DDI_UMEMLOCK_WRITE	0x02
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate #ifdef	__cplusplus
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate #endif
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate #endif	/* _SYS_DDIDEVMAP_H */
299