xref: /titanic_44/usr/src/uts/common/os/ddi_intr_impl.c (revision 2d6b5ea734bb47d251c82670646fde46af15fd69)
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 #include <sys/note.h>
27 #include <sys/sysmacros.h>
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kmem.h>
32 #include <sys/cmn_err.h>
33 #include <sys/debug.h>
34 #include <sys/avintr.h>
35 #include <sys/autoconf.h>
36 #include <sys/sunndi.h>
37 #include <sys/ndi_impldefs.h>	/* include prototypes */
38 
39 extern uint_t		ddi_msix_alloc_limit;
40 
41 /*
42  * New DDI interrupt framework
43  */
44 void
45 i_ddi_intr_devi_init(dev_info_t *dip)
46 {
47 	int	supported_types;
48 
49 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_init: dip %p\n",
50 	    (void *)dip));
51 
52 	if (DEVI(dip)->devi_intr_p)
53 		return;
54 
55 	DEVI(dip)->devi_intr_p = kmem_zalloc(sizeof (devinfo_intr_t), KM_SLEEP);
56 
57 	supported_types = i_ddi_intr_get_supported_types(dip);
58 
59 	/* Save supported interrupt types information */
60 	i_ddi_intr_set_supported_types(dip, supported_types);
61 }
62 
63 void
64 i_ddi_intr_devi_fini(dev_info_t *dip)
65 {
66 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
67 
68 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_fini: dip %p\n",
69 	    (void *)dip));
70 
71 	if ((intr_p == NULL) || i_ddi_intr_get_current_nintrs(dip))
72 		return;
73 
74 	/*
75 	 * devi_intr_handle_p will only be used for devices
76 	 * which are using the legacy DDI Interrupt interfaces.
77 	 */
78 	if (intr_p->devi_intr_handle_p) {
79 		/* nintrs could be zero; so check for it first */
80 		if (intr_p->devi_intr_sup_nintrs) {
81 			kmem_free(intr_p->devi_intr_handle_p,
82 			    intr_p->devi_intr_sup_nintrs *
83 			    sizeof (ddi_intr_handle_t));
84 		}
85 	}
86 
87 	/*
88 	 * devi_irm_req_p will only be used for devices which
89 	 * are mapped to an Interrupt Resource Management pool.
90 	 */
91 	if (intr_p->devi_irm_req_p)
92 		(void) i_ddi_irm_remove(dip);
93 
94 	kmem_free(DEVI(dip)->devi_intr_p, sizeof (devinfo_intr_t));
95 	DEVI(dip)->devi_intr_p = NULL;
96 }
97 
98 uint_t
99 i_ddi_intr_get_supported_types(dev_info_t *dip)
100 {
101 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
102 	ddi_intr_handle_impl_t	hdl;
103 	int			ret, intr_types;
104 
105 	if ((intr_p) && (intr_p->devi_intr_sup_types))
106 		return (intr_p->devi_intr_sup_types);
107 
108 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
109 	hdl.ih_dip = dip;
110 
111 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
112 	    (void *)&intr_types);
113 
114 	return ((ret == DDI_SUCCESS) ? intr_types : 0);
115 }
116 
117 /*
118  * NOTE: This function is only called by i_ddi_dev_init().
119  */
120 void
121 i_ddi_intr_set_supported_types(dev_info_t *dip, int intr_types)
122 {
123 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
124 
125 	if (intr_p)
126 		intr_p->devi_intr_sup_types = intr_types;
127 }
128 
129 uint_t
130 i_ddi_intr_get_supported_nintrs(dev_info_t *dip, int intr_type)
131 {
132 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
133 	ddi_intr_handle_impl_t	hdl;
134 	int			ret, nintrs;
135 
136 	if ((intr_p) && (intr_p->devi_intr_curr_type == intr_type) &&
137 	    (intr_p->devi_intr_sup_nintrs))
138 		return (intr_p->devi_intr_sup_nintrs);
139 
140 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
141 	hdl.ih_dip = dip;
142 	hdl.ih_type = intr_type;
143 
144 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
145 	    (void *)&nintrs);
146 
147 	return ((ret == DDI_SUCCESS) ? nintrs : 0);
148 }
149 
150 /*
151  * NOTE: This function is only called by ddi_intr_alloc().
152  */
153 void
154 i_ddi_intr_set_supported_nintrs(dev_info_t *dip, int nintrs)
155 {
156 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
157 
158 	if (intr_p)
159 		intr_p->devi_intr_sup_nintrs = nintrs;
160 }
161 
162 uint_t
163 i_ddi_intr_get_current_type(dev_info_t *dip)
164 {
165 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
166 
167 	return (intr_p ? intr_p->devi_intr_curr_type : 0);
168 }
169 
170 /*
171  * NOTE: This function is only called by
172  *       ddi_intr_alloc() and ddi_intr_free().
173  */
174 void
175 i_ddi_intr_set_current_type(dev_info_t *dip, int intr_type)
176 {
177 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
178 
179 	if (intr_p)
180 		intr_p->devi_intr_curr_type = intr_type;
181 }
182 
183 uint_t
184 i_ddi_intr_get_current_nintrs(dev_info_t *dip)
185 {
186 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
187 
188 	return (intr_p ? intr_p->devi_intr_curr_nintrs : 0);
189 }
190 
191 /*
192  * NOTE: This function is only called by
193  *       ddi_intr_alloc() and ddi_intr_free().
194  */
195 void
196 i_ddi_intr_set_current_nintrs(dev_info_t *dip, int nintrs)
197 {
198 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
199 
200 	if (intr_p)
201 		intr_p->devi_intr_curr_nintrs = nintrs;
202 }
203 
204 uint_t
205 i_ddi_intr_get_current_navail(dev_info_t *dip, int type)
206 {
207 	ddi_intr_handle_impl_t	hdl;
208 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
209 	ddi_cb_t		*cb_p;
210 	ddi_irm_pool_t		*pool_p;
211 	ddi_irm_req_t		*req_p;
212 	uint_t			navail = 0, nintrs, nreq;
213 
214 	/* Get maximum number of supported interrupts */
215 	nintrs = i_ddi_intr_get_supported_nintrs(dip, type);
216 
217 	/* Check for an interrupt pool */
218 	pool_p = i_ddi_intr_get_pool(dip, type);
219 
220 	/*
221 	 * If a pool exists, then IRM determines the availability.
222 	 * Otherwise, use the older INTROP method.
223 	 */
224 	if (pool_p) {
225 		if (intr_p && (req_p = intr_p->devi_irm_req_p) &&
226 		    (type == req_p->ireq_type)) {
227 			mutex_enter(&pool_p->ipool_navail_lock);
228 			navail = req_p->ireq_navail;
229 			mutex_exit(&pool_p->ipool_navail_lock);
230 			return (navail);
231 		}
232 		if ((type == DDI_INTR_TYPE_MSIX) &&
233 		    (cb_p = DEVI(dip)->devi_cb_p) &&
234 		    (cb_p->cb_flags & DDI_CB_FLAG_INTR)) {
235 			return (nintrs);
236 		}
237 		navail = pool_p->ipool_defsz;
238 	} else {
239 		bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
240 		hdl.ih_dip = dip;
241 		hdl.ih_type = type;
242 
243 		if (i_ddi_intr_ops(dip, dip, DDI_INTROP_NAVAIL, &hdl,
244 		    (void *)&navail) != DDI_SUCCESS) {
245 			return (0);
246 		}
247 	}
248 
249 	/* Apply MSI-X workarounds */
250 	if (type == DDI_INTR_TYPE_MSIX) {
251 		/* Global tunable workaround */
252 		if (navail < nintrs)
253 			navail = MIN(nintrs, ddi_msix_alloc_limit);
254 		/* Device property workaround */
255 		if ((nreq = i_ddi_get_msix_alloc_limit(dip)) > 0)
256 			navail = MAX(navail, nreq);
257 	}
258 
259 	/* Always restrict MSI to a precise limit */
260 	if (type == DDI_INTR_TYPE_MSI)
261 		navail = MIN(navail, DDI_MAX_MSI_ALLOC);
262 
263 	/* Ensure availability doesn't exceed what's supported */
264 	navail = MIN(navail, nintrs);
265 
266 	return (navail);
267 }
268 
269 ddi_intr_msix_t *
270 i_ddi_get_msix(dev_info_t *dip)
271 {
272 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
273 
274 	return (intr_p ? intr_p->devi_msix_p : NULL);
275 }
276 
277 void
278 i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p)
279 {
280 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
281 
282 	if (intr_p)
283 		intr_p->devi_msix_p = msix_p;
284 }
285 
286 ddi_intr_handle_t
287 i_ddi_get_intr_handle(dev_info_t *dip, int inum)
288 {
289 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
290 
291 	if (intr_p == NULL)
292 		return (NULL);
293 
294 	/*
295 	 * Changed this to a check and return NULL if an invalid inum
296 	 * is passed to retrieve a handle
297 	 */
298 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
299 		return (NULL);
300 
301 	return ((intr_p->devi_intr_handle_p) ?
302 	    intr_p->devi_intr_handle_p[inum] : NULL);
303 }
304 
305 void
306 i_ddi_set_intr_handle(dev_info_t *dip, int inum, ddi_intr_handle_t intr_hdl)
307 {
308 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
309 
310 	if (intr_p == NULL)
311 		return;
312 
313 	/*
314 	 * Changed this to a check and return if an invalid inum
315 	 * is passed to set a handle
316 	 */
317 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
318 		return;
319 
320 	if (intr_hdl && (intr_p->devi_intr_handle_p == NULL)) {
321 		/* nintrs could be zero; so check for it first */
322 		if (intr_p->devi_intr_sup_nintrs)
323 			intr_p->devi_intr_handle_p = kmem_zalloc(
324 			    sizeof (ddi_intr_handle_t) *
325 			    intr_p->devi_intr_sup_nintrs, KM_SLEEP);
326 	}
327 
328 	if (intr_p->devi_intr_handle_p)
329 		intr_p->devi_intr_handle_p[inum] = intr_hdl;
330 }
331 
332 /*
333  * The "ddi-intr-weight" property contains the weight of each interrupt
334  * associated with a dev_info node. For devices with multiple interrupts per
335  * dev_info node, the total load of the device is "devi_intr_weight * nintr",
336  * possibly spread out over multiple CPUs.
337  *
338  * Maintaining this as a property permits possible tweaking in the product
339  * in response to customer problems via driver.conf property definitions at
340  * the driver or the instance level.  This does not mean that "ddi-intr_weight"
341  * is a formal or committed interface.
342  */
343 int32_t
344 i_ddi_get_intr_weight(dev_info_t *dip)
345 {
346 	int32_t	weight;
347 
348 	weight = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
349 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "ddi-intr-weight", -1);
350 	if (weight < -1)
351 		weight = -1;			/* undefined */
352 	return (weight);
353 }
354 
355 int32_t
356 i_ddi_set_intr_weight(dev_info_t *dip, int32_t weight)
357 {
358 	int32_t oweight;
359 
360 	oweight = i_ddi_get_intr_weight(dip);
361 	if ((weight > 0) && (oweight != weight))
362 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
363 		    "ddi-intr-weight", weight);
364 	return (oweight);
365 }
366 
367 /*
368  * Old DDI interrupt framework
369  *
370  * NOTE:
371  *	The following 4 busops entry points are obsoleted with version
372  *	9 or greater. Use i_ddi_intr_op interface in place of these
373  *	obsolete interfaces.
374  *
375  *	Remove these busops entry points and all related data structures
376  *	in future major/minor solaris release.
377  */
378 
379 /* ARGSUSED */
380 ddi_intrspec_t
381 i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, uint_t inumber)
382 {
383 	dev_info_t	*pdip = ddi_get_parent(dip);
384 
385 	cmn_err(CE_WARN, "Failed to process interrupt "
386 	    "for %s%d due to down-rev nexus driver %s%d",
387 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
388 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
389 
390 	return (NULL);
391 }
392 
393 /* ARGSUSED */
394 int
395 i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, ddi_intrspec_t intrspec,
396     ddi_iblock_cookie_t *iblock_cookiep,
397     ddi_idevice_cookie_t *idevice_cookiep,
398     uint_t (*int_handler)(caddr_t int_handler_arg),
399     caddr_t int_handler_arg, int kind)
400 {
401 	dev_info_t	*pdip = ddi_get_parent(dip);
402 
403 	cmn_err(CE_WARN, "Failed to process interrupt "
404 	    "for %s%d due to down-rev nexus driver %s%d",
405 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
406 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
407 
408 	return (DDI_ENOTSUP);
409 }
410 
411 /* ARGSUSED */
412 void
413 i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
414     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie)
415 {
416 	dev_info_t	*pdip = ddi_get_parent(dip);
417 
418 	cmn_err(CE_WARN, "Failed to process interrupt "
419 	    "for %s%d due to down-rev nexus driver %s%d",
420 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
421 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
422 }
423 
424 /* ARGSUSED */
425 int
426 i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_ctlop_t op,
427     void *arg, void *val)
428 {
429 	dev_info_t	*pdip = ddi_get_parent(dip);
430 
431 	cmn_err(CE_WARN, "Failed to process interrupt "
432 	    "for %s%d due to down-rev nexus driver %s%d",
433 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
434 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
435 
436 	return (DDI_ENOTSUP);
437 }
438 
439 #if defined(__i386) || defined(__amd64)
440 ddi_acc_handle_t
441 i_ddi_get_pci_config_handle(dev_info_t *dip)
442 {
443 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
444 
445 	return (intr_p ? intr_p->devi_cfg_handle : NULL);
446 }
447 
448 void
449 i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle)
450 {
451 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
452 
453 	if (intr_p)
454 		intr_p->devi_cfg_handle = handle;
455 }
456 
457 
458 int
459 i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip)
460 {
461 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
462 
463 	return (intr_p ? intr_p->devi_cap_ptr : 0);
464 }
465 
466 void
467 i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr)
468 {
469 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
470 
471 	if (intr_p)
472 		intr_p->devi_cap_ptr = cap_ptr;
473 }
474 #endif
475 
476 /* ARGSUSED */
477 uint_t
478 i_ddi_get_msix_alloc_limit(dev_info_t *dip)
479 {
480 	uint_t	msix_alloc_limit = ddi_msix_alloc_limit;
481 
482 #if defined(__sparc)
483 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_NOTPROM |
484 	    DDI_PROP_DONTPASS, "#msix-request")) {
485 		msix_alloc_limit = MAX(DDI_MAX_MSIX_ALLOC,
486 		    ddi_msix_alloc_limit);
487 	}
488 #endif
489 
490 	return (msix_alloc_limit);
491 }
492