xref: /titanic_50/usr/src/uts/common/sys/ddi_intr_impl.h (revision 2ef9abdc6ea9bad985430325b12b90938a8cd18f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_DDI_INTR_IMPL_H
27 #define	_SYS_DDI_INTR_IMPL_H
28 
29 /*
30  * Sun DDI interrupt implementation specific definitions
31  */
32 
33 #include <sys/list.h>
34 #include <sys/ksynch.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef _KERNEL
41 
42 /*
43  * Typedef for interrupt ops
44  */
45 typedef enum {
46 	DDI_INTROP_SUPPORTED_TYPES = 1,	/* 1 get supported interrupts types */
47 	DDI_INTROP_NINTRS,		/* 2 get num of interrupts supported */
48 	DDI_INTROP_ALLOC,		/* 3 allocate interrupt handle */
49 	DDI_INTROP_GETPRI,		/* 4 get priority */
50 	DDI_INTROP_SETPRI,		/* 5 set priority */
51 	DDI_INTROP_ADDISR,		/* 6 add interrupt handler */
52 	DDI_INTROP_DUPVEC,		/* 7 duplicate interrupt handler */
53 	DDI_INTROP_ENABLE,		/* 8 enable interrupt */
54 	DDI_INTROP_BLOCKENABLE,		/* 9 block enable interrupts */
55 	DDI_INTROP_BLOCKDISABLE,	/* 10 block disable interrupts */
56 	DDI_INTROP_DISABLE,		/* 11 disable interrupt */
57 	DDI_INTROP_REMISR,		/* 12 remove interrupt handler */
58 	DDI_INTROP_FREE,		/* 13 free interrupt handle */
59 	DDI_INTROP_GETCAP,		/* 14 get capacity */
60 	DDI_INTROP_SETCAP,		/* 15 set capacity */
61 	DDI_INTROP_SETMASK,		/* 16 set mask */
62 	DDI_INTROP_CLRMASK,		/* 17 clear mask */
63 	DDI_INTROP_GETPENDING,		/* 18 get pending interrupt */
64 	DDI_INTROP_NAVAIL,		/* 19 get num of available interrupts */
65 	DDI_INTROP_GETPOOL		/* 20 get resource management pool */
66 } ddi_intr_op_t;
67 
68 /* Version number used in the handles */
69 #define	DDI_INTR_VERSION_1	1
70 #define	DDI_INTR_VERSION	DDI_INTR_VERSION_1
71 
72 /*
73  * One such data structure is allocated per ddi_intr_handle_t
74  * This is the incore copy of the regular interrupt info.
75  */
76 typedef struct ddi_intr_handle_impl {
77 	dev_info_t		*ih_dip;	/* dip associated with handle */
78 	uint16_t		ih_type;	/* interrupt type being used */
79 	ushort_t		ih_inum;	/* interrupt number */
80 	uint32_t		ih_vector;	/* vector number */
81 	uint16_t		ih_ver;		/* Version */
82 	uint_t			ih_state;	/* interrupt handle state */
83 	uint_t			ih_cap;		/* interrupt capabilities */
84 	uint_t			ih_pri;		/* priority - bus dependent */
85 	krwlock_t		ih_rwlock;	/* read/write lock per handle */
86 
87 	uint_t			(*ih_cb_func)(caddr_t, caddr_t);
88 	void			*ih_cb_arg1;
89 	void			*ih_cb_arg2;
90 
91 	/*
92 	 * The following 3 members are used to support MSI-X specific features
93 	 */
94 	uint_t			ih_flags;	/* Misc flags */
95 	uint_t			ih_dup_cnt;	/* # of dupped msi-x vectors */
96 	struct ddi_intr_handle_impl	*ih_main;
97 						/* pntr to the main vector */
98 	/*
99 	 * The next set of members are for 'scratch' purpose only.
100 	 * The DDI interrupt framework uses them internally and their
101 	 * interpretation is left to the framework. For now,
102 	 *	scratch1	- used to send NINTRs information
103 	 *			  to various nexus drivers.
104 	 *	scratch2	- used to send 'behavior' flag
105 	 *			  information to the nexus drivers
106 	 *			  from ddi_intr_alloc().  It is also
107 	 *			  used to send 'h_array' to the nexus drivers
108 	 *			  for ddi_intr_block_enable/disable() on x86.
109 	 *	private		- On X86 it usually carries a pointer to
110 	 *			  ihdl_plat_t.  Not used on SPARC platforms.
111 	 */
112 	void			*ih_private;	/* Platform specific data */
113 	uint_t			ih_scratch1;	/* Scratch1: #interrupts */
114 	void			*ih_scratch2;	/* Scratch2: flag/h_array */
115 } ddi_intr_handle_impl_t;
116 
117 /* values for ih_state (strictly for interrupt handle) */
118 #define	DDI_IHDL_STATE_ALLOC	0x01	/* Allocated. ddi_intr_alloc() called */
119 #define	DDI_IHDL_STATE_ADDED	0x02	/* Added interrupt handler */
120 					/* ddi_intr_add_handler() called */
121 #define	DDI_IHDL_STATE_ENABLE	0x04	/* Enabled. ddi_intr_enable() called */
122 
123 #define	DDI_INTR_IS_MSI_OR_MSIX(type) \
124 	((type) == DDI_INTR_TYPE_MSI || (type) == DDI_INTR_TYPE_MSIX)
125 
126 #define	DDI_INTR_BEHAVIOR_FLAG_VALID(f) \
127 	    (((f) == DDI_INTR_ALLOC_NORMAL) || ((f) == DDI_INTR_ALLOC_STRICT))
128 
129 #define	DDI_INTR_TYPE_FLAG_VALID(t) \
130 	    (((t) == DDI_INTR_TYPE_FIXED) || \
131 	    ((t) == DDI_INTR_TYPE_MSI) || \
132 	    ((t) == DDI_INTR_TYPE_MSIX))
133 
134 /* values for ih_flags */
135 #define	DDI_INTR_MSIX_DUP	0x01	/* MSI-X vector which has been dupped */
136 
137 /* Maximum number of MSI resources to allocate */
138 #define	DDI_MAX_MSI_ALLOC	2
139 
140 /*
141  * The following MSI-X limits will change with Interrupt Resource Management
142  * (IRM) support.
143  */
144 /* Default number of MSI-X resources to allocate */
145 #define	DDI_DEFAULT_MSIX_ALLOC	2
146 
147 /* Maximum number of MSI-X resources to allocate */
148 #define	DDI_MAX_MSIX_ALLOC	8
149 
150 struct av_softinfo;
151 
152 /*
153  * One such data structure is allocated per ddi_soft_intr_handle
154  * This is the incore copy of the softint info.
155  */
156 typedef struct ddi_softint_hdl_impl {
157 	dev_info_t	*ih_dip;		/* dip associated with handle */
158 	uint_t		ih_pri;			/* priority - bus dependent */
159 	krwlock_t	ih_rwlock;		/* read/write lock per handle */
160 	struct av_softinfo *ih_pending;		/* whether softint is pending */
161 
162 	uint_t		(*ih_cb_func)(caddr_t, caddr_t);
163 						/* cb function for soft ints */
164 	void		*ih_cb_arg1;		/* arg1 of callback function */
165 	void		*ih_cb_arg2;		/* arg2 passed to "trigger" */
166 
167 	/*
168 	 * The next member is for 'scratch' purpose only.
169 	 * The DDI interrupt framework uses it internally and its
170 	 * interpretation is left to the framework.
171 	 *	private		- used by the DDI framework to pass back
172 	 *			  and forth 'softid' information on SPARC
173 	 *			  side only. Not used on X86 platform.
174 	 */
175 	void		*ih_private;		/* Platform specific data */
176 } ddi_softint_hdl_impl_t;
177 
178 /* Softint internal implementation defines */
179 #define	DDI_SOFT_INTR_PRI_M	4
180 #define	DDI_SOFT_INTR_PRI_H	6
181 
182 /*
183  * One such data structure is allocated for MSI-X enabled
184  * device. If no MSI-X is enabled then it is NULL
185  */
186 typedef struct ddi_intr_msix {
187 	/* MSI-X Table related information */
188 	ddi_acc_handle_t	msix_tbl_hdl;		/* MSI-X table handle */
189 	uint32_t		*msix_tbl_addr;		/* MSI-X table addr */
190 	uint32_t		msix_tbl_offset;	/* MSI-X table offset */
191 
192 	/* MSI-X PBA Table related information */
193 	ddi_acc_handle_t	msix_pba_hdl;		/* MSI-X PBA handle */
194 	uint32_t		*msix_pba_addr;		/* MSI-X PBA addr */
195 	uint32_t		msix_pba_offset;	/* MSI-X PBA offset */
196 
197 	ddi_device_acc_attr_t	msix_dev_attr;		/* MSI-X device attr */
198 } ddi_intr_msix_t;
199 
200 /*
201  * Interrupt Resource Management (IRM).
202  */
203 
204 #define	DDI_IRM_POLICY_LARGE	1
205 #define	DDI_IRM_POLICY_EVEN	2
206 
207 #define	DDI_IRM_POLICY_VALID(p)	(((p) == DDI_IRM_POLICY_LARGE) || \
208 				((p) == DDI_IRM_POLICY_EVEN))
209 
210 #define	DDI_IRM_FLAG_ACTIVE	0x1		/* Pool is active */
211 #define	DDI_IRM_FLAG_QUEUED	0x2		/* Pool is queued */
212 #define	DDI_IRM_FLAG_WAITERS	0x4		/* Pool has waiters */
213 #define	DDI_IRM_FLAG_EXIT	0x8		/* Balance thread must exit */
214 #define	DDI_IRM_FLAG_NEW	0x10		/* Request is new */
215 #define	DDI_IRM_FLAG_CALLBACK	0x20		/* Request has callback */
216 
217 /*
218  * One such data structure for each supply of interrupt vectors.
219  * Contains information about the size and policies defining the
220  * supply, and a list of associated device-specific requests.
221  */
222 typedef struct ddi_irm_pool {
223 	int		ipool_flags;		/* Status flags of the pool */
224 	int		ipool_types;		/* Types of interrupts */
225 	int		ipool_policy;		/* Rebalancing policy */
226 	uint_t		ipool_totsz;		/* Total size of the pool */
227 	uint_t		ipool_defsz;		/* Default allocation size */
228 	uint_t		ipool_minno;		/* Minimum number consumed */
229 	uint_t		ipool_reqno;		/* Total number requested */
230 	uint_t		ipool_resno;		/* Total number reserved */
231 	kmutex_t	ipool_lock;		/* Protects all pool usage */
232 	kmutex_t	ipool_navail_lock;	/* Protects 'navail' of reqs */
233 	kcondvar_t	ipool_cv;		/* Condition variable */
234 	kthread_t	*ipool_thread;		/* Balancing thread */
235 	dev_info_t	*ipool_owner;		/* Device that created pool */
236 	list_t		ipool_req_list;		/* All requests in pool */
237 	list_t		ipool_scratch_list;	/* Requests being reduced */
238 	list_node_t	ipool_link;		/* Links in global pool list */
239 } ddi_irm_pool_t;
240 
241 /*
242  * One such data structure for each dip's devinfo_intr_t.
243  * Contains information about vectors requested from IRM.
244  */
245 typedef struct ddi_irm_req {
246 	int		ireq_flags;		/* Flags for request */
247 	int		ireq_type;		/* Type requested */
248 	uint_t		ireq_nreq;		/* Number requested */
249 	uint_t		ireq_navail;		/* Number available */
250 	uint_t		ireq_scratch;		/* Scratch value */
251 	dev_info_t	*ireq_dip;		/* Requesting device */
252 	ddi_irm_pool_t	*ireq_pool_p;		/* Supplying pool */
253 	list_node_t	ireq_link;		/* Request list link */
254 	list_node_t	ireq_scratch_link;	/* Scratch list link */
255 } ddi_irm_req_t;
256 
257 /*
258  * This structure is used to pass parameters to ndi_create_irm(),
259  * and describes the operating parameters of an IRM pool.
260  */
261 typedef struct ddi_irm_params {
262 	int	iparams_types;		/* Types of interrupts in pool */
263 	uint_t	iparams_total;		/* Total size of the pool */
264 	uint_t	iparams_default;	/* Default allocation size */
265 } ddi_irm_params_t;
266 
267 /*
268  * One such data structure is allocated for each dip.
269  * It has interrupt related information that can be
270  * stored/retrieved for convenience.
271  */
272 typedef struct devinfo_intr {
273 	/* These three fields show what the device is capable of */
274 	uint_t		devi_intr_sup_types;	/* Intrs supported by device */
275 
276 	ddi_intr_msix_t	*devi_msix_p;		/* MSI-X info, if supported */
277 
278 	/* Next three fields show current status for the device */
279 	uint_t		devi_intr_curr_type;	/* Interrupt type being used */
280 	uint_t		devi_intr_sup_nintrs;	/* #intr supported */
281 	uint_t		devi_intr_curr_nintrs;	/* #intr currently being used */
282 	/*
283 	 * #intr currently being enabled
284 	 * (for MSI block enable, the valuse is either 1 or 0.)
285 	 */
286 	uint_t		devi_intr_curr_nenables;
287 
288 	ddi_intr_handle_t *devi_intr_handle_p;	/* Hdl for legacy intr APIs */
289 
290 #if defined(__i386) || defined(__amd64)
291 	/* Save the PCI config space handle */
292 	ddi_acc_handle_t devi_cfg_handle;
293 	int		 devi_cap_ptr;		/* MSI or MSI-X cap pointer */
294 #endif
295 
296 	ddi_irm_req_t	*devi_irm_req_p;	/* IRM request information */
297 } devinfo_intr_t;
298 
299 #define	NEXUS_HAS_INTR_OP(dip)	\
300 	((DEVI(dip)->devi_ops->devo_bus_ops) && \
301 	(DEVI(dip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_9) && \
302 	(DEVI(dip)->devi_ops->devo_bus_ops->bus_intr_op))
303 
304 int	i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
305 	    ddi_intr_handle_impl_t *hdlp, void *result);
306 
307 int	i_ddi_add_softint(ddi_softint_hdl_impl_t *);
308 void	i_ddi_remove_softint(ddi_softint_hdl_impl_t *);
309 int	i_ddi_trigger_softint(ddi_softint_hdl_impl_t *, void *);
310 int	i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *, uint_t);
311 
312 void	i_ddi_intr_devi_init(dev_info_t *dip);
313 void	i_ddi_intr_devi_fini(dev_info_t *dip);
314 
315 uint_t	i_ddi_intr_get_supported_types(dev_info_t *dip);
316 void	i_ddi_intr_set_supported_types(dev_info_t *dip, int sup_type);
317 uint_t	i_ddi_intr_get_current_type(dev_info_t *dip);
318 void	i_ddi_intr_set_current_type(dev_info_t *dip, int intr_type);
319 uint_t	i_ddi_intr_get_supported_nintrs(dev_info_t *dip, int intr_type);
320 void	i_ddi_intr_set_supported_nintrs(dev_info_t *dip, int nintrs);
321 uint_t	i_ddi_intr_get_current_nintrs(dev_info_t *dip);
322 void	i_ddi_intr_set_current_nintrs(dev_info_t *dip, int nintrs);
323 uint_t	i_ddi_intr_get_current_nenables(dev_info_t *dip);
324 void	i_ddi_intr_set_current_nenables(dev_info_t *dip, int nintrs);
325 uint_t	i_ddi_intr_get_current_navail(dev_info_t *dip, int intr_type);
326 
327 ddi_irm_pool_t	*i_ddi_intr_get_pool(dev_info_t *dip, int intr_type);
328 
329 void	irm_init(void);
330 int	i_ddi_irm_insert(dev_info_t *dip, int intr_type, int count);
331 int	i_ddi_irm_modify(dev_info_t *dip, int nreq);
332 int	i_ddi_irm_remove(dev_info_t *dip);
333 void	i_ddi_irm_set_cb(dev_info_t *dip, boolean_t cb_flag);
334 
335 ddi_intr_handle_t i_ddi_get_intr_handle(dev_info_t *dip, int inum);
336 void	i_ddi_set_intr_handle(dev_info_t *dip, int inum, ddi_intr_handle_t hdl);
337 
338 ddi_intr_msix_t	*i_ddi_get_msix(dev_info_t *dip);
339 void	i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p);
340 
341 #if defined(__i386) || defined(__amd64)
342 ddi_acc_handle_t	i_ddi_get_pci_config_handle(dev_info_t *dip);
343 void	i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle);
344 int	i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip);
345 void	i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr);
346 #endif
347 
348 uint_t	i_ddi_get_msix_alloc_limit(dev_info_t *dip);
349 
350 int32_t i_ddi_get_intr_weight(dev_info_t *);
351 int32_t i_ddi_set_intr_weight(dev_info_t *, int32_t);
352 
353 void	i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *);
354 void	i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *);
355 
356 #define	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, func, arg1, arg2) \
357 	hdlp->ih_cb_func = func; \
358 	hdlp->ih_cb_arg1 = arg1; \
359 	hdlp->ih_cb_arg2 = arg2;
360 
361 #ifdef DEBUG
362 #define	I_DDI_VERIFY_MSIX_HANDLE(hdlp)					\
363 	if ((hdlp->ih_type == DDI_INTR_TYPE_MSIX) && 			\
364 	    (hdlp->ih_flags & DDI_INTR_MSIX_DUP)) {			\
365 		ASSERT(hdlp->ih_dip == hdlp->ih_main->ih_dip);		\
366 		ASSERT(hdlp->ih_type == hdlp->ih_main->ih_type);	\
367 		ASSERT(hdlp->ih_vector == hdlp->ih_main->ih_vector);	\
368 		ASSERT(hdlp->ih_ver == hdlp->ih_main->ih_ver);		\
369 		ASSERT(hdlp->ih_cap == hdlp->ih_main->ih_cap);		\
370 		ASSERT(hdlp->ih_pri == hdlp->ih_main->ih_pri);		\
371 	}
372 #else
373 #define	I_DDI_VERIFY_MSIX_HANDLE(hdlp)
374 #endif
375 
376 #else	/* _KERNEL */
377 
378 typedef struct devinfo_intr devinfo_intr_t;
379 
380 #endif	/* _KERNEL */
381 
382 /*
383  * Used only by old DDI interrupt interfaces.
384  */
385 
386 /*
387  * This structure represents one interrupt possible from the given
388  * device. It is used in an array for devices with multiple interrupts.
389  */
390 struct intrspec {
391 	uint_t intrspec_pri;		/* interrupt priority */
392 	uint_t intrspec_vec;		/* vector # (0 if none) */
393 	uint_t (*intrspec_func)();	/* function to call for interrupt, */
394 					/* If (uint_t (*)()) 0, none. */
395 					/* If (uint_t (*)()) 1, then */
396 };
397 
398 #ifdef _KERNEL
399 
400 /*
401  * Figure out how many FIXED nintrs are supported
402  */
403 int	i_ddi_get_intx_nintrs(dev_info_t *dip);
404 
405 /*
406  * NOTE:
407  *	The following 4 busops entry points are obsoleted with version
408  *	9 or greater. Use i_ddi_intr_op interface in place of these
409  *	obsolete interfaces.
410  *
411  *	Remove these busops entry points and all related data structures
412  *	in future minor/major solaris release.
413  */
414 typedef enum {DDI_INTR_CTLOPS_NONE} ddi_intr_ctlop_t;
415 
416 /* The following are obsolete interfaces */
417 ddi_intrspec_t	i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip,
418 	    uint_t inumber);
419 
420 int	i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip,
421 	    ddi_intrspec_t intrspec, ddi_iblock_cookie_t *iblock_cookiep,
422 	    ddi_idevice_cookie_t *idevice_cookiep,
423 	    uint_t (*int_handler)(caddr_t int_handler_arg),
424 	    caddr_t int_handler_arg, int kind);
425 
426 void	i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
427 	    ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie);
428 
429 int	i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip,
430 	    ddi_intr_ctlop_t op, void *arg, void *val);
431 
432 #endif	/* _KERNEL */
433 
434 #ifdef	__cplusplus
435 }
436 #endif
437 
438 #endif	/* _SYS_DDI_INTR_IMPL_H */
439