xref: /illumos-gate/usr/src/uts/common/os/ddi_intr_impl.c (revision 560f878bce5cdf0661659001415019ca5c8a01b4)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/note.h>
29 #include <sys/sysmacros.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kmem.h>
34 #include <sys/cmn_err.h>
35 #include <sys/debug.h>
36 #include <sys/avintr.h>
37 #include <sys/autoconf.h>
38 #include <sys/sunndi.h>
39 #include <sys/ndi_impldefs.h>	/* include prototypes */
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),
56 	    KM_SLEEP);
57 
58 	supported_types = i_ddi_intr_get_supported_types(dip);
59 
60 	/* Save supported interrupt types information */
61 	i_ddi_intr_set_supported_types(dip, supported_types);
62 }
63 
64 void
65 i_ddi_intr_devi_fini(dev_info_t *dip)
66 {
67 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
68 
69 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_fini: dip %p\n",
70 	    (void *)dip));
71 
72 	if (DEVI(dip)->devi_intr_p == NULL)
73 		return;
74 
75 	/*
76 	 * devi_intr_handle_p will only be used for devices
77 	 * which are using the legacy DDI Interrupt interfaces.
78 	 */
79 	if (intr_p->devi_intr_handle_p) {
80 		/* nintrs could be zero; so check for it first */
81 		if (intr_p->devi_intr_sup_nintrs) {
82 			kmem_free(intr_p->devi_intr_handle_p,
83 			    intr_p->devi_intr_sup_nintrs *
84 			    sizeof (ddi_intr_handle_t));
85 		}
86 	}
87 
88 	kmem_free(DEVI(dip)->devi_intr_p, sizeof (devinfo_intr_t));
89 	DEVI(dip)->devi_intr_p = NULL;
90 }
91 
92 uint_t
93 i_ddi_intr_get_supported_types(dev_info_t *dip)
94 {
95 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
96 	ddi_intr_handle_impl_t	hdl;
97 	int			ret, intr_types;
98 
99 	if ((intr_p) && (intr_p->devi_intr_sup_types))
100 		return (intr_p->devi_intr_sup_types);
101 
102 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
103 	hdl.ih_dip = dip;
104 
105 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
106 	    (void *)&intr_types);
107 
108 	return ((ret == DDI_SUCCESS) ? intr_types : 0);
109 }
110 
111 /*
112  * NOTE: This function is only called by i_ddi_dev_init().
113  */
114 void
115 i_ddi_intr_set_supported_types(dev_info_t *dip, int intr_types)
116 {
117 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
118 
119 	if (intr_p)
120 		intr_p->devi_intr_sup_types = intr_types;
121 }
122 
123 uint_t
124 i_ddi_intr_get_supported_nintrs(dev_info_t *dip, int intr_type)
125 {
126 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
127 	ddi_intr_handle_impl_t	hdl;
128 	int			ret, nintrs;
129 
130 	if ((intr_p) && (intr_p->devi_intr_curr_type == intr_type) &&
131 	    (intr_p->devi_intr_sup_nintrs))
132 		return (intr_p->devi_intr_sup_nintrs);
133 
134 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
135 	hdl.ih_dip = dip;
136 	hdl.ih_type = intr_type;
137 
138 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
139 	    (void *)&nintrs);
140 
141 	return ((ret == DDI_SUCCESS) ? nintrs : 0);
142 }
143 
144 /*
145  * NOTE: This function is only called by ddi_intr_alloc().
146  */
147 void
148 i_ddi_intr_set_supported_nintrs(dev_info_t *dip, int nintrs)
149 {
150 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
151 
152 	if (intr_p)
153 		intr_p->devi_intr_sup_nintrs = nintrs;
154 }
155 
156 uint_t
157 i_ddi_intr_get_current_type(dev_info_t *dip)
158 {
159 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
160 
161 	return (intr_p ? intr_p->devi_intr_curr_type : 0);
162 }
163 
164 /*
165  * NOTE: This function is only called by
166  *       ddi_intr_alloc() and ddi_intr_free().
167  */
168 void
169 i_ddi_intr_set_current_type(dev_info_t *dip, int intr_type)
170 {
171 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
172 
173 	if (intr_p)
174 		intr_p->devi_intr_curr_type = intr_type;
175 }
176 
177 uint_t
178 i_ddi_intr_get_current_nintrs(dev_info_t *dip)
179 {
180 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
181 
182 	return (intr_p ? intr_p->devi_intr_curr_nintrs : 0);
183 }
184 
185 /*
186  * NOTE: This function is only called by
187  *       ddi_intr_alloc() and ddi_intr_free().
188  */
189 void
190 i_ddi_intr_set_current_nintrs(dev_info_t *dip, int nintrs)
191 {
192 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
193 
194 	if (intr_p)
195 		intr_p->devi_intr_curr_nintrs = nintrs;
196 }
197 
198 ddi_intr_msix_t *
199 i_ddi_get_msix(dev_info_t *dip)
200 {
201 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
202 
203 	return (intr_p ? intr_p->devi_msix_p : NULL);
204 }
205 
206 void
207 i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p)
208 {
209 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
210 
211 	if (intr_p)
212 		intr_p->devi_msix_p = msix_p;
213 }
214 
215 ddi_intr_handle_t *
216 i_ddi_get_intr_handle(dev_info_t *dip, int inum)
217 {
218 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
219 
220 	if (intr_p == NULL)
221 		return (NULL);
222 
223 	/*
224 	 * Changed this to a check and return NULL if an invalid inum
225 	 * is passed to retrieve a handle
226 	 */
227 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
228 		return (NULL);
229 
230 	return ((intr_p->devi_intr_handle_p) ?
231 	    intr_p->devi_intr_handle_p[inum] : NULL);
232 }
233 
234 void
235 i_ddi_set_intr_handle(dev_info_t *dip, int inum,
236     ddi_intr_handle_t *intr_hdlp)
237 {
238 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
239 
240 	if (intr_p == NULL)
241 		return;
242 
243 	/*
244 	 * Changed this to a check and return if an invalid inum
245 	 * is passed to set a handle
246 	 */
247 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
248 		return;
249 
250 	if (intr_hdlp && (intr_p->devi_intr_handle_p == NULL)) {
251 		/* nintrs could be zero; so check for it first */
252 		if (intr_p->devi_intr_sup_nintrs)
253 			intr_p->devi_intr_handle_p = kmem_zalloc(
254 			    sizeof (ddi_intr_handle_t) *
255 			    intr_p->devi_intr_sup_nintrs, KM_SLEEP);
256 	}
257 
258 	if (intr_p->devi_intr_handle_p)
259 		intr_p->devi_intr_handle_p[inum] = intr_hdlp;
260 }
261 
262 /*
263  * The "ddi-intr-weight" property contains the weight of each interrupt
264  * associated with a dev_info node. For devices with multiple interrupts per
265  * dev_info node, the total load of the device is "devi_intr_weight * nintr",
266  * possibly spread out over multiple CPUs.
267  *
268  * Maintaining this as a property permits possible tweaking in the product
269  * in response to customer problems via driver.conf property definitions at
270  * the driver or the instance level.  This does not mean that "ddi-intr_weight"
271  * is a formal or committed interface.
272  */
273 int32_t
274 i_ddi_get_intr_weight(dev_info_t *dip)
275 {
276 	int32_t	weight;
277 
278 	weight = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
279 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "ddi-intr-weight", -1);
280 	if (weight < -1)
281 		weight = -1;			/* undefined */
282 	return (weight);
283 }
284 
285 int32_t
286 i_ddi_set_intr_weight(dev_info_t *dip, int32_t weight)
287 {
288 	int32_t oweight;
289 
290 	oweight = i_ddi_get_intr_weight(dip);
291 	if ((weight > 0) && (oweight != weight))
292 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
293 		    "ddi-intr-weight", weight);
294 	return (oweight);
295 }
296 
297 /*
298  * Old DDI interrupt framework
299  *
300  * NOTE:
301  *	The following 4 busops entry points are obsoleted with version
302  *	9 or greater. Use i_ddi_intr_op interface in place of these
303  *	obsolete interfaces.
304  *
305  *	Remove these busops entry points and all related data structures
306  *	in future major/minor solaris release.
307  */
308 
309 /* ARGSUSED */
310 ddi_intrspec_t
311 i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, uint_t inumber)
312 {
313 	dev_info_t	*pdip = ddi_get_parent(dip);
314 
315 	cmn_err(CE_WARN, "Failed to process interrupt "
316 	    "for %s%d due to down-rev nexus driver %s%d",
317 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
318 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
319 
320 	return (NULL);
321 }
322 
323 /* ARGSUSED */
324 int
325 i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, ddi_intrspec_t intrspec,
326     ddi_iblock_cookie_t *iblock_cookiep,
327     ddi_idevice_cookie_t *idevice_cookiep,
328     uint_t (*int_handler)(caddr_t int_handler_arg),
329     caddr_t int_handler_arg, int kind)
330 {
331 	dev_info_t	*pdip = ddi_get_parent(dip);
332 
333 	cmn_err(CE_WARN, "Failed to process interrupt "
334 	    "for %s%d due to down-rev nexus driver %s%d",
335 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
336 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
337 
338 	return (DDI_ENOTSUP);
339 }
340 
341 /* ARGSUSED */
342 void
343 i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
344     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie)
345 {
346 	dev_info_t	*pdip = ddi_get_parent(dip);
347 
348 	cmn_err(CE_WARN, "Failed to process interrupt "
349 	    "for %s%d due to down-rev nexus driver %s%d",
350 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
351 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
352 }
353 
354 /* ARGSUSED */
355 int
356 i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_ctlop_t op,
357     void *arg, void *val)
358 {
359 	dev_info_t	*pdip = ddi_get_parent(dip);
360 
361 	cmn_err(CE_WARN, "Failed to process interrupt "
362 	    "for %s%d due to down-rev nexus driver %s%d",
363 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
364 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
365 
366 	return (DDI_ENOTSUP);
367 }
368 
369 #if defined(__i386) || defined(__amd64)
370 ddi_acc_handle_t
371 i_ddi_get_pci_config_handle(dev_info_t *dip)
372 {
373 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
374 
375 	return (intr_p ? intr_p->devi_cfg_handle : NULL);
376 }
377 
378 void
379 i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle)
380 {
381 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
382 
383 	if (intr_p)
384 		intr_p->devi_cfg_handle = handle;
385 }
386 
387 
388 int
389 i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip)
390 {
391 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
392 
393 	return (intr_p ? intr_p->devi_cap_ptr : 0);
394 }
395 
396 void
397 i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr)
398 {
399 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
400 
401 	if (intr_p)
402 		intr_p->devi_cap_ptr = cap_ptr;
403 }
404 #endif
405