xref: /titanic_50/usr/src/uts/common/os/ddi_intr_impl.c (revision 08d2fdc210e92c7a249e69a795663019da6f23ca)
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_nenables(dev_info_t *dip)
206 {
207 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
208 
209 	return (intr_p ? intr_p->devi_intr_curr_nenables : 0);
210 }
211 
212 void
213 i_ddi_intr_set_current_nenables(dev_info_t *dip, int nintrs)
214 {
215 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
216 
217 	if (intr_p)
218 		intr_p->devi_intr_curr_nenables = nintrs;
219 }
220 
221 uint_t
222 i_ddi_intr_get_current_navail(dev_info_t *dip, int type)
223 {
224 	ddi_intr_handle_impl_t	hdl;
225 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
226 	ddi_cb_t		*cb_p;
227 	ddi_irm_pool_t		*pool_p;
228 	ddi_irm_req_t		*req_p;
229 	uint_t			navail = 0, nintrs, nreq;
230 
231 	/* Get maximum number of supported interrupts */
232 	nintrs = i_ddi_intr_get_supported_nintrs(dip, type);
233 
234 	/* Check for an interrupt pool */
235 	pool_p = i_ddi_intr_get_pool(dip, type);
236 
237 	/*
238 	 * If a pool exists, then IRM determines the availability.
239 	 * Otherwise, use the older INTROP method.
240 	 */
241 	if (pool_p) {
242 		if (intr_p && (req_p = intr_p->devi_irm_req_p) &&
243 		    (type == req_p->ireq_type)) {
244 			mutex_enter(&pool_p->ipool_navail_lock);
245 			navail = req_p->ireq_navail;
246 			mutex_exit(&pool_p->ipool_navail_lock);
247 			return (navail);
248 		}
249 		if ((type == DDI_INTR_TYPE_MSIX) &&
250 		    (cb_p = DEVI(dip)->devi_cb_p) &&
251 		    (cb_p->cb_flags & DDI_CB_FLAG_INTR)) {
252 			return (nintrs);
253 		}
254 		navail = pool_p->ipool_defsz;
255 	} else {
256 		bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
257 		hdl.ih_dip = dip;
258 		hdl.ih_type = type;
259 
260 		if (i_ddi_intr_ops(dip, dip, DDI_INTROP_NAVAIL, &hdl,
261 		    (void *)&navail) != DDI_SUCCESS) {
262 			return (0);
263 		}
264 	}
265 
266 	/* Apply MSI-X workarounds */
267 	if (type == DDI_INTR_TYPE_MSIX) {
268 		/* Global tunable workaround */
269 		if (navail < nintrs)
270 			navail = MIN(nintrs, ddi_msix_alloc_limit);
271 		/* Device property workaround */
272 		if ((nreq = i_ddi_get_msix_alloc_limit(dip)) > 0)
273 			navail = MAX(navail, nreq);
274 	}
275 
276 	/* Always restrict MSI to a precise limit */
277 	if (type == DDI_INTR_TYPE_MSI)
278 		navail = MIN(navail, DDI_MAX_MSI_ALLOC);
279 
280 	/* Ensure availability doesn't exceed what's supported */
281 	navail = MIN(navail, nintrs);
282 
283 	return (navail);
284 }
285 
286 ddi_intr_msix_t *
287 i_ddi_get_msix(dev_info_t *dip)
288 {
289 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
290 
291 	return (intr_p ? intr_p->devi_msix_p : NULL);
292 }
293 
294 void
295 i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p)
296 {
297 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
298 
299 	if (intr_p)
300 		intr_p->devi_msix_p = msix_p;
301 }
302 
303 ddi_intr_handle_t
304 i_ddi_get_intr_handle(dev_info_t *dip, int inum)
305 {
306 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
307 
308 	if (intr_p == NULL)
309 		return (NULL);
310 
311 	/*
312 	 * Changed this to a check and return NULL if an invalid inum
313 	 * is passed to retrieve a handle
314 	 */
315 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
316 		return (NULL);
317 
318 	return ((intr_p->devi_intr_handle_p) ?
319 	    intr_p->devi_intr_handle_p[inum] : NULL);
320 }
321 
322 void
323 i_ddi_set_intr_handle(dev_info_t *dip, int inum, ddi_intr_handle_t intr_hdl)
324 {
325 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
326 
327 	if (intr_p == NULL)
328 		return;
329 
330 	/*
331 	 * Changed this to a check and return if an invalid inum
332 	 * is passed to set a handle
333 	 */
334 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
335 		return;
336 
337 	if (intr_hdl && (intr_p->devi_intr_handle_p == NULL)) {
338 		/* nintrs could be zero; so check for it first */
339 		if (intr_p->devi_intr_sup_nintrs)
340 			intr_p->devi_intr_handle_p = kmem_zalloc(
341 			    sizeof (ddi_intr_handle_t) *
342 			    intr_p->devi_intr_sup_nintrs, KM_SLEEP);
343 	}
344 
345 	if (intr_p->devi_intr_handle_p)
346 		intr_p->devi_intr_handle_p[inum] = intr_hdl;
347 }
348 
349 /*
350  * The "ddi-intr-weight" property contains the weight of each interrupt
351  * associated with a dev_info node. For devices with multiple interrupts per
352  * dev_info node, the total load of the device is "devi_intr_weight * nintr",
353  * possibly spread out over multiple CPUs.
354  *
355  * Maintaining this as a property permits possible tweaking in the product
356  * in response to customer problems via driver.conf property definitions at
357  * the driver or the instance level.  This does not mean that "ddi-intr_weight"
358  * is a formal or committed interface.
359  */
360 int32_t
361 i_ddi_get_intr_weight(dev_info_t *dip)
362 {
363 	int32_t	weight;
364 
365 	weight = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
366 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "ddi-intr-weight", -1);
367 	if (weight < -1)
368 		weight = -1;			/* undefined */
369 	return (weight);
370 }
371 
372 int32_t
373 i_ddi_set_intr_weight(dev_info_t *dip, int32_t weight)
374 {
375 	int32_t oweight;
376 
377 	oweight = i_ddi_get_intr_weight(dip);
378 	if ((weight > 0) && (oweight != weight))
379 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
380 		    "ddi-intr-weight", weight);
381 	return (oweight);
382 }
383 
384 /*
385  * Old DDI interrupt framework
386  *
387  * NOTE:
388  *	The following 4 busops entry points are obsoleted with version
389  *	9 or greater. Use i_ddi_intr_op interface in place of these
390  *	obsolete interfaces.
391  *
392  *	Remove these busops entry points and all related data structures
393  *	in future major/minor solaris release.
394  */
395 
396 /* ARGSUSED */
397 ddi_intrspec_t
398 i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, uint_t inumber)
399 {
400 	dev_info_t	*pdip = ddi_get_parent(dip);
401 
402 	cmn_err(CE_WARN, "Failed to process interrupt "
403 	    "for %s%d due to down-rev nexus driver %s%d",
404 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
405 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
406 
407 	return (NULL);
408 }
409 
410 /* ARGSUSED */
411 int
412 i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, ddi_intrspec_t intrspec,
413     ddi_iblock_cookie_t *iblock_cookiep,
414     ddi_idevice_cookie_t *idevice_cookiep,
415     uint_t (*int_handler)(caddr_t int_handler_arg),
416     caddr_t int_handler_arg, int kind)
417 {
418 	dev_info_t	*pdip = ddi_get_parent(dip);
419 
420 	cmn_err(CE_WARN, "Failed to process interrupt "
421 	    "for %s%d due to down-rev nexus driver %s%d",
422 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
423 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
424 
425 	return (DDI_ENOTSUP);
426 }
427 
428 /* ARGSUSED */
429 void
430 i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
431     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie)
432 {
433 	dev_info_t	*pdip = ddi_get_parent(dip);
434 
435 	cmn_err(CE_WARN, "Failed to process interrupt "
436 	    "for %s%d due to down-rev nexus driver %s%d",
437 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
438 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
439 }
440 
441 /* ARGSUSED */
442 int
443 i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_ctlop_t op,
444     void *arg, void *val)
445 {
446 	dev_info_t	*pdip = ddi_get_parent(dip);
447 
448 	cmn_err(CE_WARN, "Failed to process interrupt "
449 	    "for %s%d due to down-rev nexus driver %s%d",
450 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
451 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
452 
453 	return (DDI_ENOTSUP);
454 }
455 
456 #if defined(__i386) || defined(__amd64)
457 ddi_acc_handle_t
458 i_ddi_get_pci_config_handle(dev_info_t *dip)
459 {
460 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
461 
462 	return (intr_p ? intr_p->devi_cfg_handle : NULL);
463 }
464 
465 void
466 i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle)
467 {
468 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
469 
470 	if (intr_p)
471 		intr_p->devi_cfg_handle = handle;
472 }
473 
474 
475 int
476 i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip)
477 {
478 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
479 
480 	return (intr_p ? intr_p->devi_cap_ptr : 0);
481 }
482 
483 void
484 i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr)
485 {
486 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
487 
488 	if (intr_p)
489 		intr_p->devi_cap_ptr = cap_ptr;
490 }
491 #endif
492 
493 /* ARGSUSED */
494 uint_t
495 i_ddi_get_msix_alloc_limit(dev_info_t *dip)
496 {
497 	uint_t	msix_alloc_limit = ddi_msix_alloc_limit;
498 
499 #if defined(__sparc)
500 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_NOTPROM |
501 	    DDI_PROP_DONTPASS, "#msix-request")) {
502 		msix_alloc_limit = MAX(DDI_MAX_MSIX_ALLOC,
503 		    ddi_msix_alloc_limit);
504 	}
505 #endif
506 
507 	return (msix_alloc_limit);
508 }
509