xref: /titanic_44/usr/src/uts/i86pc/io/pcplusmp/apic_introp.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * apic_introp.c:
28  *	Has code for Advanced DDI interrupt framework support.
29  */
30 
31 #include <sys/cpuvar.h>
32 #include <sys/psm.h>
33 #include <sys/archsystm.h>
34 #include <sys/apic.h>
35 #include <sys/sunddi.h>
36 #include <sys/ddi_impldefs.h>
37 #include <sys/mach_intr.h>
38 #include <sys/sysmacros.h>
39 #include <sys/trap.h>
40 #include <sys/pci.h>
41 #include <sys/pci_intr_lib.h>
42 
43 extern struct av_head autovect[];
44 
45 /*
46  *	Local Function Prototypes
47  */
48 apic_irq_t	*apic_find_irq(dev_info_t *, struct intrspec *, int);
49 
50 /*
51  * MSI support flag:
52  * reflects whether MSI is supported at APIC level
53  * it can also be patched through /etc/system
54  *
55  *  0 = default value - don't know and need to call apic_check_msi_support()
56  *      to find out then set it accordingly
57  *  1 = supported
58  * -1 = not supported
59  */
60 int	apic_support_msi = 0;
61 
62 /* Multiple vector support for MSI */
63 int	apic_multi_msi_enable = 1;
64 
65 /* Multiple vector support for MSI-X */
66 int	apic_msix_enable = 1;
67 
68 /*
69  * apic_pci_msi_enable_vector:
70  *	Set the address/data fields in the MSI/X capability structure
71  *	XXX: MSI-X support
72  */
73 /* ARGSUSED */
74 void
75 apic_pci_msi_enable_vector(apic_irq_t *irq_ptr, int type, int inum, int vector,
76     int count, int target_apic_id)
77 {
78 	uint64_t		msi_addr, msi_data;
79 	ushort_t		msi_ctrl;
80 	dev_info_t		*dip = irq_ptr->airq_dip;
81 	int			cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip);
82 	ddi_acc_handle_t	handle = i_ddi_get_pci_config_handle(dip);
83 #if !defined(__xpv)
84 	msi_regs_t		msi_regs;
85 #endif	/* ! __xpv */
86 
87 	DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: dip=0x%p\n"
88 	    "\tdriver = %s, inum=0x%x vector=0x%x apicid=0x%x\n", (void *)dip,
89 	    ddi_driver_name(dip), inum, vector, target_apic_id));
90 
91 	ASSERT((handle != NULL) && (cap_ptr != 0));
92 
93 #if !defined(__xpv)
94 	msi_regs.mr_data = vector;
95 	msi_regs.mr_addr = target_apic_id;
96 
97 	apic_vt_ops->apic_intrmap_alloc_entry(irq_ptr);
98 	apic_vt_ops->apic_intrmap_map_entry(irq_ptr, (void *)&msi_regs);
99 	apic_vt_ops->apic_intrmap_record_msi(irq_ptr, &msi_regs);
100 
101 	/* MSI Address */
102 	msi_addr = msi_regs.mr_addr;
103 
104 	/* MSI Data: MSI is edge triggered according to spec */
105 	msi_data = msi_regs.mr_data;
106 #else
107 	/* MSI Address */
108 	msi_addr = (MSI_ADDR_HDR |
109 	    (target_apic_id << MSI_ADDR_DEST_SHIFT));
110 	msi_addr |= ((MSI_ADDR_RH_FIXED << MSI_ADDR_RH_SHIFT) |
111 	    (MSI_ADDR_DM_PHYSICAL << MSI_ADDR_DM_SHIFT));
112 
113 	/* MSI Data: MSI is edge triggered according to spec */
114 	msi_data = ((MSI_DATA_TM_EDGE << MSI_DATA_TM_SHIFT) | vector);
115 #endif	/* ! __xpv */
116 
117 	DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: addr=0x%lx "
118 	    "data=0x%lx\n", (long)msi_addr, (long)msi_data));
119 
120 	if (type == DDI_INTR_TYPE_MSI) {
121 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
122 
123 		/* Set the bits to inform how many MSIs are enabled */
124 		msi_ctrl |= ((highbit(count) -1) << PCI_MSI_MME_SHIFT);
125 		pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
126 
127 #if !defined(__xpv)
128 		/*
129 		 * Only set vector if not on hypervisor
130 		 */
131 		pci_config_put32(handle,
132 		    cap_ptr + PCI_MSI_ADDR_OFFSET, msi_addr);
133 
134 		if (msi_ctrl &  PCI_MSI_64BIT_MASK) {
135 			pci_config_put32(handle,
136 			    cap_ptr + PCI_MSI_ADDR_OFFSET + 4, msi_addr >> 32);
137 			pci_config_put16(handle,
138 			    cap_ptr + PCI_MSI_64BIT_DATA, msi_data);
139 		} else {
140 			pci_config_put16(handle,
141 			    cap_ptr + PCI_MSI_32BIT_DATA, msi_data);
142 		}
143 
144 	} else if (type == DDI_INTR_TYPE_MSIX) {
145 		uintptr_t	off;
146 		ddi_intr_msix_t	*msix_p = i_ddi_get_msix(dip);
147 
148 		ASSERT(msix_p != NULL);
149 
150 		/* Offset into the "inum"th entry in the MSI-X table */
151 		off = (uintptr_t)msix_p->msix_tbl_addr +
152 		    (inum  * PCI_MSIX_VECTOR_SIZE);
153 
154 		ddi_put32(msix_p->msix_tbl_hdl,
155 		    (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), msi_data);
156 		ddi_put64(msix_p->msix_tbl_hdl,
157 		    (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET), msi_addr);
158 #endif	/* ! __xpv */
159 	}
160 }
161 
162 
163 #if !defined(__xpv)
164 
165 /*
166  * This function returns the no. of vectors available for the pri.
167  * dip is not used at this moment.  If we really don't need that,
168  * it will be removed.
169  */
170 /*ARGSUSED*/
171 int
172 apic_navail_vector(dev_info_t *dip, int pri)
173 {
174 	int	lowest, highest, i, navail, count;
175 
176 	DDI_INTR_IMPLDBG((CE_CONT, "apic_navail_vector: dip: %p, pri: %x\n",
177 	    (void *)dip, pri));
178 
179 	highest = apic_ipltopri[pri] + APIC_VECTOR_MASK;
180 	lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL;
181 	navail = count = 0;
182 
183 	if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
184 		lowest -= APIC_VECTOR_PER_IPL;
185 
186 	/* It has to be contiguous */
187 	for (i = lowest; i <= highest; i++) {
188 		count = 0;
189 		while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) &&
190 		    (i <= highest)) {
191 			if (APIC_CHECK_RESERVE_VECTORS(i))
192 				break;
193 			count++;
194 			i++;
195 		}
196 		if (count > navail)
197 			navail = count;
198 	}
199 	return (navail);
200 }
201 
202 #endif	/* ! __xpv */
203 
204 /*
205  * Finds "count" contiguous MSI vectors starting at the proper alignment
206  * at "pri".
207  * Caller needs to make sure that count has to be power of 2 and should not
208  * be < 1.
209  */
210 uchar_t
211 apic_find_multi_vectors(int pri, int count)
212 {
213 	int	lowest, highest, i, navail, start, msibits;
214 
215 	DDI_INTR_IMPLDBG((CE_CONT, "apic_find_mult: pri: %x, count: %x\n",
216 	    pri, count));
217 
218 	highest = apic_ipltopri[pri] + APIC_VECTOR_MASK;
219 	lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL;
220 	navail = 0;
221 
222 	if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
223 		lowest -= APIC_VECTOR_PER_IPL;
224 
225 	/*
226 	 * msibits is the no. of lower order message data bits for the
227 	 * allocated MSI vectors and is used to calculate the aligned
228 	 * starting vector
229 	 */
230 	msibits = count - 1;
231 
232 	/* It has to be contiguous */
233 	for (i = lowest; i <= highest; i++) {
234 		navail = 0;
235 
236 		/*
237 		 * starting vector has to be aligned accordingly for
238 		 * multiple MSIs
239 		 */
240 		if (msibits)
241 			i = (i + msibits) & ~msibits;
242 		start = i;
243 		while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) &&
244 		    (i <= highest)) {
245 			if (APIC_CHECK_RESERVE_VECTORS(i))
246 				break;
247 			navail++;
248 			if (navail >= count)
249 				return (start);
250 			i++;
251 		}
252 	}
253 	return (0);
254 }
255 
256 
257 /*
258  * It finds the apic_irq_t associates with the dip, ispec and type.
259  */
260 apic_irq_t *
261 apic_find_irq(dev_info_t *dip, struct intrspec *ispec, int type)
262 {
263 	apic_irq_t	*irqp;
264 	int i;
265 
266 	DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: dip=0x%p vec=0x%x "
267 	    "ipl=0x%x type=0x%x\n", (void *)dip, ispec->intrspec_vec,
268 	    ispec->intrspec_pri, type));
269 
270 	for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
271 		for (irqp = apic_irq_table[i]; irqp; irqp = irqp->airq_next) {
272 			if ((irqp->airq_dip == dip) &&
273 			    (irqp->airq_origirq == ispec->intrspec_vec) &&
274 			    (irqp->airq_ipl == ispec->intrspec_pri)) {
275 				if (type == DDI_INTR_TYPE_MSI) {
276 					if (irqp->airq_mps_intr_index ==
277 					    MSI_INDEX)
278 						return (irqp);
279 				} else if (type == DDI_INTR_TYPE_MSIX) {
280 					if (irqp->airq_mps_intr_index ==
281 					    MSIX_INDEX)
282 						return (irqp);
283 				} else
284 					return (irqp);
285 			}
286 		}
287 	}
288 	DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: return NULL\n"));
289 	return (NULL);
290 }
291 
292 
293 #if !defined(__xpv)
294 
295 /*
296  * This function will return the pending bit of the irqp.
297  * It either comes from the IRR register of the APIC or the RDT
298  * entry of the I/O APIC.
299  * For the IRR to work, it needs to be to its binding CPU
300  */
301 static int
302 apic_get_pending(apic_irq_t *irqp, int type)
303 {
304 	int			bit, index, irr, pending;
305 	int			intin_no;
306 	int			apic_ix;
307 
308 	DDI_INTR_IMPLDBG((CE_CONT, "apic_get_pending: irqp: %p, cpuid: %x "
309 	    "type: %x\n", (void *)irqp, irqp->airq_cpu & ~IRQ_USER_BOUND,
310 	    type));
311 
312 	/* need to get on the bound cpu */
313 	mutex_enter(&cpu_lock);
314 	affinity_set(irqp->airq_cpu & ~IRQ_USER_BOUND);
315 
316 	index = irqp->airq_vector / 32;
317 	bit = irqp->airq_vector % 32;
318 	irr = apic_reg_ops->apic_read(APIC_IRR_REG + index);
319 
320 	affinity_clear();
321 	mutex_exit(&cpu_lock);
322 
323 	pending = (irr & (1 << bit)) ? 1 : 0;
324 	if (!pending && (type == DDI_INTR_TYPE_FIXED)) {
325 		/* check I/O APIC for fixed interrupt */
326 		intin_no = irqp->airq_intin_no;
327 		apic_ix = irqp->airq_ioapicindex;
328 		pending = (READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no) &
329 		    AV_PENDING) ? 1 : 0;
330 	}
331 	return (pending);
332 }
333 
334 
335 /*
336  * This function will clear the mask for the interrupt on the I/O APIC
337  */
338 static void
339 apic_clear_mask(apic_irq_t *irqp)
340 {
341 	int			intin_no;
342 	ulong_t			iflag;
343 	int32_t			rdt_entry;
344 	int 			apic_ix;
345 
346 	DDI_INTR_IMPLDBG((CE_CONT, "apic_clear_mask: irqp: %p\n",
347 	    (void *)irqp));
348 
349 	intin_no = irqp->airq_intin_no;
350 	apic_ix = irqp->airq_ioapicindex;
351 
352 	iflag = intr_clear();
353 	lock_set(&apic_ioapic_lock);
354 
355 	rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no);
356 
357 	/* clear mask */
358 	WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no,
359 	    ((~AV_MASK) & rdt_entry));
360 
361 	lock_clear(&apic_ioapic_lock);
362 	intr_restore(iflag);
363 }
364 
365 
366 /*
367  * This function will mask the interrupt on the I/O APIC
368  */
369 static void
370 apic_set_mask(apic_irq_t *irqp)
371 {
372 	int			intin_no;
373 	int 			apic_ix;
374 	ulong_t			iflag;
375 	int32_t			rdt_entry;
376 
377 	DDI_INTR_IMPLDBG((CE_CONT, "apic_set_mask: irqp: %p\n", (void *)irqp));
378 
379 	intin_no = irqp->airq_intin_no;
380 	apic_ix = irqp->airq_ioapicindex;
381 
382 	iflag = intr_clear();
383 
384 	lock_set(&apic_ioapic_lock);
385 
386 	rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no);
387 
388 	/* mask it */
389 	WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no,
390 	    (AV_MASK | rdt_entry));
391 
392 	lock_clear(&apic_ioapic_lock);
393 	intr_restore(iflag);
394 }
395 
396 
397 void
398 apic_free_vectors(dev_info_t *dip, int inum, int count, int pri, int type)
399 {
400 	int i;
401 	apic_irq_t *irqptr;
402 	struct intrspec ispec;
403 
404 	DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: dip: %p inum: %x "
405 	    "count: %x pri: %x type: %x\n",
406 	    (void *)dip, inum, count, pri, type));
407 
408 	/* for MSI/X only */
409 	if (!DDI_INTR_IS_MSI_OR_MSIX(type))
410 		return;
411 
412 	for (i = 0; i < count; i++) {
413 		DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: inum=0x%x "
414 		    "pri=0x%x count=0x%x\n", inum, pri, count));
415 		ispec.intrspec_vec = inum + i;
416 		ispec.intrspec_pri = pri;
417 		if ((irqptr = apic_find_irq(dip, &ispec, type)) == NULL) {
418 			DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: "
419 			    "dip=0x%p inum=0x%x pri=0x%x apic_find_irq() "
420 			    "failed\n", (void *)dip, inum, pri));
421 			continue;
422 		}
423 		irqptr->airq_mps_intr_index = FREE_INDEX;
424 		apic_vector_to_irq[irqptr->airq_vector] = APIC_RESV_IRQ;
425 	}
426 }
427 
428 #endif	/* ! __xpv */
429 
430 /*
431  * check whether the system supports MSI
432  *
433  * If PCI-E capability is found, then this must be a PCI-E system.
434  * Since MSI is required for PCI-E system, it returns PSM_SUCCESS
435  * to indicate this system supports MSI.
436  */
437 int
438 apic_check_msi_support()
439 {
440 	dev_info_t *cdip;
441 	char dev_type[16];
442 	int dev_len;
443 
444 	DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support:\n"));
445 
446 	/*
447 	 * check whether the first level children of root_node have
448 	 * PCI-E capability
449 	 */
450 	for (cdip = ddi_get_child(ddi_root_node()); cdip != NULL;
451 	    cdip = ddi_get_next_sibling(cdip)) {
452 
453 		DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: cdip: 0x%p,"
454 		    " driver: %s, binding: %s, nodename: %s\n", (void *)cdip,
455 		    ddi_driver_name(cdip), ddi_binding_name(cdip),
456 		    ddi_node_name(cdip)));
457 		dev_len = sizeof (dev_type);
458 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
459 		    "device_type", (caddr_t)dev_type, &dev_len)
460 		    != DDI_PROP_SUCCESS)
461 			continue;
462 		if (strcmp(dev_type, "pciex") == 0)
463 			return (PSM_SUCCESS);
464 	}
465 
466 	/* MSI is not supported on this system */
467 	DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: no 'pciex' "
468 	    "device_type found\n"));
469 	return (PSM_FAILURE);
470 }
471 
472 #if !defined(__xpv)
473 
474 /*
475  * apic_pci_msi_unconfigure:
476  *
477  * This and next two interfaces are copied from pci_intr_lib.c
478  * Do ensure that these two files stay in sync.
479  * These needed to be copied over here to avoid a deadlock situation on
480  * certain mp systems that use MSI interrupts.
481  *
482  * IMPORTANT regards next three interfaces:
483  * i) are called only for MSI/X interrupts.
484  * ii) called with interrupts disabled, and must not block
485  */
486 void
487 apic_pci_msi_unconfigure(dev_info_t *rdip, int type, int inum)
488 {
489 	ushort_t		msi_ctrl;
490 	int			cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
491 	ddi_acc_handle_t	handle = i_ddi_get_pci_config_handle(rdip);
492 
493 	ASSERT((handle != NULL) && (cap_ptr != 0));
494 
495 	if (type == DDI_INTR_TYPE_MSI) {
496 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
497 		msi_ctrl &= (~PCI_MSI_MME_MASK);
498 		pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
499 		pci_config_put32(handle, cap_ptr + PCI_MSI_ADDR_OFFSET, 0);
500 
501 		if (msi_ctrl &  PCI_MSI_64BIT_MASK) {
502 			pci_config_put16(handle,
503 			    cap_ptr + PCI_MSI_64BIT_DATA, 0);
504 			pci_config_put32(handle,
505 			    cap_ptr + PCI_MSI_ADDR_OFFSET + 4, 0);
506 		} else {
507 			pci_config_put16(handle,
508 			    cap_ptr + PCI_MSI_32BIT_DATA, 0);
509 		}
510 
511 	} else if (type == DDI_INTR_TYPE_MSIX) {
512 		uintptr_t	off;
513 		uint32_t	mask;
514 		ddi_intr_msix_t	*msix_p = i_ddi_get_msix(rdip);
515 
516 		ASSERT(msix_p != NULL);
517 
518 		/* Offset into "inum"th entry in the MSI-X table & mask it */
519 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
520 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
521 
522 		mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off);
523 
524 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask | 1));
525 
526 		/* Offset into the "inum"th entry in the MSI-X table */
527 		off = (uintptr_t)msix_p->msix_tbl_addr +
528 		    (inum * PCI_MSIX_VECTOR_SIZE);
529 
530 		/* Reset the "data" and "addr" bits */
531 		ddi_put32(msix_p->msix_tbl_hdl,
532 		    (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), 0);
533 		ddi_put64(msix_p->msix_tbl_hdl, (uint64_t *)off, 0);
534 	}
535 }
536 
537 #endif	/* __xpv */
538 
539 /*
540  * apic_pci_msi_enable_mode:
541  */
542 void
543 apic_pci_msi_enable_mode(dev_info_t *rdip, int type, int inum)
544 {
545 	ushort_t		msi_ctrl;
546 	int			cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
547 	ddi_acc_handle_t	handle = i_ddi_get_pci_config_handle(rdip);
548 
549 	ASSERT((handle != NULL) && (cap_ptr != 0));
550 
551 	if (type == DDI_INTR_TYPE_MSI) {
552 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
553 		if ((msi_ctrl & PCI_MSI_ENABLE_BIT))
554 			return;
555 
556 		msi_ctrl |= PCI_MSI_ENABLE_BIT;
557 		pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
558 
559 	} else if (type == DDI_INTR_TYPE_MSIX) {
560 		uintptr_t	off;
561 		uint32_t	mask;
562 		ddi_intr_msix_t	*msix_p;
563 
564 		msix_p = i_ddi_get_msix(rdip);
565 
566 		ASSERT(msix_p != NULL);
567 
568 		/* Offset into "inum"th entry in the MSI-X table & clear mask */
569 		off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
570 		    PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
571 
572 		mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off);
573 
574 		ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask & ~1));
575 
576 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL);
577 
578 		if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT)) {
579 			msi_ctrl |= PCI_MSIX_ENABLE_BIT;
580 			pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL,
581 			    msi_ctrl);
582 		}
583 	}
584 }
585 
586 /*
587  * apic_pci_msi_disable_mode:
588  */
589 void
590 apic_pci_msi_disable_mode(dev_info_t *rdip, int type)
591 {
592 	ushort_t		msi_ctrl;
593 	int			cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
594 	ddi_acc_handle_t	handle = i_ddi_get_pci_config_handle(rdip);
595 
596 	ASSERT((handle != NULL) && (cap_ptr != 0));
597 
598 	if (type == DDI_INTR_TYPE_MSI) {
599 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
600 		if (!(msi_ctrl & PCI_MSI_ENABLE_BIT))
601 			return;
602 
603 		msi_ctrl &= ~PCI_MSI_ENABLE_BIT;	/* MSI disable */
604 		pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
605 
606 	} else if (type == DDI_INTR_TYPE_MSIX) {
607 		msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL);
608 		if (msi_ctrl & PCI_MSIX_ENABLE_BIT) {
609 			msi_ctrl &= ~PCI_MSIX_ENABLE_BIT;
610 			pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL,
611 			    msi_ctrl);
612 		}
613 	}
614 }
615 
616 #if !defined(__xpv)
617 
618 static int
619 apic_set_cpu(int irqno, int cpu, int *result)
620 {
621 	apic_irq_t *irqp;
622 	ulong_t iflag;
623 	int ret;
624 
625 	DDI_INTR_IMPLDBG((CE_CONT, "APIC_SET_CPU\n"));
626 
627 	mutex_enter(&airq_mutex);
628 	irqp = apic_irq_table[irqno];
629 	mutex_exit(&airq_mutex);
630 
631 	if (irqp == NULL) {
632 		*result = ENXIO;
633 		return (PSM_FAILURE);
634 	}
635 
636 	/* Fail if this is an MSI intr and is part of a group. */
637 	if ((irqp->airq_mps_intr_index == MSI_INDEX) &&
638 	    (irqp->airq_intin_no > 1)) {
639 		*result = ENXIO;
640 		return (PSM_FAILURE);
641 	}
642 
643 	iflag = intr_clear();
644 	lock_set(&apic_ioapic_lock);
645 
646 	ret = apic_rebind_all(irqp, cpu);
647 
648 	lock_clear(&apic_ioapic_lock);
649 	intr_restore(iflag);
650 
651 	if (ret) {
652 		*result = EIO;
653 		return (PSM_FAILURE);
654 	}
655 	/*
656 	 * keep tracking the default interrupt cpu binding
657 	 */
658 	irqp->airq_cpu = cpu;
659 
660 	*result = 0;
661 	return (PSM_SUCCESS);
662 }
663 
664 static int
665 apic_grp_set_cpu(int irqno, int new_cpu, int *result)
666 {
667 	dev_info_t *orig_dip;
668 	uint32_t orig_cpu;
669 	ulong_t iflag;
670 	apic_irq_t *irqps[PCI_MSI_MAX_INTRS];
671 	int i;
672 	int cap_ptr;
673 	int msi_mask_off;
674 	ushort_t msi_ctrl;
675 	uint32_t msi_pvm;
676 	ddi_acc_handle_t handle;
677 	int num_vectors = 0;
678 	uint32_t vector;
679 
680 	DDI_INTR_IMPLDBG((CE_CONT, "APIC_GRP_SET_CPU\n"));
681 
682 	/*
683 	 * Take mutex to insure that table doesn't change out from underneath
684 	 * us while we're playing with it.
685 	 */
686 	mutex_enter(&airq_mutex);
687 	irqps[0] = apic_irq_table[irqno];
688 	orig_cpu = irqps[0]->airq_temp_cpu;
689 	orig_dip = irqps[0]->airq_dip;
690 	num_vectors = irqps[0]->airq_intin_no;
691 	vector = irqps[0]->airq_vector;
692 
693 	/* A "group" of 1 */
694 	if (num_vectors == 1) {
695 		mutex_exit(&airq_mutex);
696 		return (apic_set_cpu(irqno, new_cpu, result));
697 	}
698 
699 	*result = ENXIO;
700 
701 	if (irqps[0]->airq_mps_intr_index != MSI_INDEX) {
702 		mutex_exit(&airq_mutex);
703 		DDI_INTR_IMPLDBG((CE_CONT, "set_grp: intr not MSI\n"));
704 		goto set_grp_intr_done;
705 	}
706 	if ((num_vectors < 1) || ((num_vectors - 1) & vector)) {
707 		mutex_exit(&airq_mutex);
708 		DDI_INTR_IMPLDBG((CE_CONT,
709 		    "set_grp: base vec not part of a grp or not aligned: "
710 		    "vec:0x%x, num_vec:0x%x\n", vector, num_vectors));
711 		goto set_grp_intr_done;
712 	}
713 	DDI_INTR_IMPLDBG((CE_CONT, "set_grp: num intrs in grp: %d\n",
714 	    num_vectors));
715 
716 	ASSERT((num_vectors + vector) < APIC_MAX_VECTOR);
717 
718 	*result = EIO;
719 
720 	/*
721 	 * All IRQ entries in the table for the given device will be not
722 	 * shared.  Since they are not shared, the dip in the table will
723 	 * be true to the device of interest.
724 	 */
725 	for (i = 1; i < num_vectors; i++) {
726 		irqps[i] = apic_irq_table[apic_vector_to_irq[vector + i]];
727 		if (irqps[i] == NULL) {
728 			mutex_exit(&airq_mutex);
729 			goto set_grp_intr_done;
730 		}
731 #ifdef DEBUG
732 		/* Sanity check: CPU and dip is the same for all entries. */
733 		if ((irqps[i]->airq_dip != orig_dip) ||
734 		    (irqps[i]->airq_temp_cpu != orig_cpu)) {
735 			mutex_exit(&airq_mutex);
736 			DDI_INTR_IMPLDBG((CE_CONT,
737 			    "set_grp: cpu or dip for vec 0x%x difft than for "
738 			    "vec 0x%x\n", vector, vector + i));
739 			DDI_INTR_IMPLDBG((CE_CONT,
740 			    "  cpu: %d vs %d, dip: 0x%p vs 0x%p\n", orig_cpu,
741 			    irqps[i]->airq_temp_cpu, (void *)orig_dip,
742 			    (void *)irqps[i]->airq_dip));
743 			goto set_grp_intr_done;
744 		}
745 #endif /* DEBUG */
746 	}
747 	mutex_exit(&airq_mutex);
748 
749 	cap_ptr = i_ddi_get_msi_msix_cap_ptr(orig_dip);
750 	handle = i_ddi_get_pci_config_handle(orig_dip);
751 	msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
752 
753 	/* MSI Per vector masking is supported. */
754 	if (msi_ctrl & PCI_MSI_PVM_MASK) {
755 		if (msi_ctrl &  PCI_MSI_64BIT_MASK)
756 			msi_mask_off = cap_ptr + PCI_MSI_64BIT_MASKBITS;
757 		else
758 			msi_mask_off = cap_ptr + PCI_MSI_32BIT_MASK;
759 		msi_pvm = pci_config_get32(handle, msi_mask_off);
760 		pci_config_put32(handle, msi_mask_off, (uint32_t)-1);
761 		DDI_INTR_IMPLDBG((CE_CONT,
762 		    "set_grp: pvm supported.  Mask set to 0x%x\n",
763 		    pci_config_get32(handle, msi_mask_off)));
764 	}
765 
766 	iflag = intr_clear();
767 	lock_set(&apic_ioapic_lock);
768 
769 	/*
770 	 * Do the first rebind and check for errors.  Apic_rebind_all returns
771 	 * an error if the CPU is not accepting interrupts.  If the first one
772 	 * succeeds they all will.
773 	 */
774 	if (apic_rebind_all(irqps[0], new_cpu))
775 		(void) apic_rebind_all(irqps[0], orig_cpu);
776 	else {
777 		irqps[0]->airq_cpu = new_cpu;
778 
779 		for (i = 1; i < num_vectors; i++) {
780 			(void) apic_rebind_all(irqps[i], new_cpu);
781 			irqps[i]->airq_cpu = new_cpu;
782 		}
783 		*result = 0;	/* SUCCESS */
784 	}
785 
786 	lock_clear(&apic_ioapic_lock);
787 	intr_restore(iflag);
788 
789 	/* Reenable vectors if per vector masking is supported. */
790 	if (msi_ctrl & PCI_MSI_PVM_MASK) {
791 		pci_config_put32(handle, msi_mask_off, msi_pvm);
792 		DDI_INTR_IMPLDBG((CE_CONT,
793 		    "set_grp: pvm supported.  Mask restored to 0x%x\n",
794 		    pci_config_get32(handle, msi_mask_off)));
795 	}
796 
797 set_grp_intr_done:
798 	if (*result != 0)
799 		return (PSM_FAILURE);
800 
801 	return (PSM_SUCCESS);
802 }
803 
804 #else	/* __xpv */
805 
806 /*
807  * We let the hypervisor deal with msi configutation
808  * so just stub this out.
809  */
810 
811 /* ARGSUSED */
812 void
813 apic_pci_msi_unconfigure(dev_info_t *rdip, int type, int inum)
814 {
815 }
816 
817 #endif	/* __xpv */
818 
819 int
820 apic_get_vector_intr_info(int vecirq, apic_get_intr_t *intr_params_p)
821 {
822 	struct autovec *av_dev;
823 	uchar_t irqno;
824 	int i;
825 	apic_irq_t *irq_p;
826 
827 	/* Sanity check the vector/irq argument. */
828 	ASSERT((vecirq >= 0) || (vecirq <= APIC_MAX_VECTOR));
829 
830 	mutex_enter(&airq_mutex);
831 
832 	/*
833 	 * Convert the vecirq arg to an irq using vector_to_irq table
834 	 * if the arg is a vector.  Pass thru if already an irq.
835 	 */
836 	if ((intr_params_p->avgi_req_flags & PSMGI_INTRBY_FLAGS) ==
837 	    PSMGI_INTRBY_VEC)
838 		irqno = apic_vector_to_irq[vecirq];
839 	else
840 		irqno = vecirq;
841 
842 	irq_p = apic_irq_table[irqno];
843 
844 	if ((irq_p == NULL) ||
845 	    (irq_p->airq_temp_cpu == IRQ_UNBOUND) ||
846 	    (irq_p->airq_temp_cpu == IRQ_UNINIT)) {
847 		mutex_exit(&airq_mutex);
848 		return (PSM_FAILURE);
849 	}
850 
851 	if (intr_params_p->avgi_req_flags & PSMGI_REQ_CPUID) {
852 
853 		/* Get the (temp) cpu from apic_irq table, indexed by irq. */
854 		intr_params_p->avgi_cpu_id = irq_p->airq_temp_cpu;
855 
856 		/* Return user bound info for intrd. */
857 		if (intr_params_p->avgi_cpu_id & IRQ_USER_BOUND) {
858 			intr_params_p->avgi_cpu_id &= ~IRQ_USER_BOUND;
859 			intr_params_p->avgi_cpu_id |= PSMGI_CPU_USER_BOUND;
860 		}
861 	}
862 
863 	if (intr_params_p->avgi_req_flags & PSMGI_REQ_VECTOR)
864 		intr_params_p->avgi_vector = irq_p->airq_vector;
865 
866 	if (intr_params_p->avgi_req_flags &
867 	    (PSMGI_REQ_NUM_DEVS | PSMGI_REQ_GET_DEVS))
868 		/* Get number of devices from apic_irq table shared field. */
869 		intr_params_p->avgi_num_devs = irq_p->airq_share;
870 
871 	if (intr_params_p->avgi_req_flags &  PSMGI_REQ_GET_DEVS) {
872 
873 		intr_params_p->avgi_req_flags  |= PSMGI_REQ_NUM_DEVS;
874 
875 		/* Some devices have NULL dip.  Don't count these. */
876 		if (intr_params_p->avgi_num_devs > 0) {
877 			for (i = 0, av_dev = autovect[irqno].avh_link;
878 			    av_dev; av_dev = av_dev->av_link)
879 				if (av_dev->av_vector && av_dev->av_dip)
880 					i++;
881 			intr_params_p->avgi_num_devs =
882 			    MIN(intr_params_p->avgi_num_devs, i);
883 		}
884 
885 		/* There are no viable dips to return. */
886 		if (intr_params_p->avgi_num_devs == 0)
887 			intr_params_p->avgi_dip_list = NULL;
888 
889 		else {	/* Return list of dips */
890 
891 			/* Allocate space in array for that number of devs. */
892 			intr_params_p->avgi_dip_list = kmem_zalloc(
893 			    intr_params_p->avgi_num_devs *
894 			    sizeof (dev_info_t *),
895 			    KM_SLEEP);
896 
897 			/*
898 			 * Loop through the device list of the autovec table
899 			 * filling in the dip array.
900 			 *
901 			 * Note that the autovect table may have some special
902 			 * entries which contain NULL dips.  These will be
903 			 * ignored.
904 			 */
905 			for (i = 0, av_dev = autovect[irqno].avh_link;
906 			    av_dev; av_dev = av_dev->av_link)
907 				if (av_dev->av_vector && av_dev->av_dip)
908 					intr_params_p->avgi_dip_list[i++] =
909 					    av_dev->av_dip;
910 		}
911 	}
912 
913 	mutex_exit(&airq_mutex);
914 
915 	return (PSM_SUCCESS);
916 }
917 
918 
919 #if !defined(__xpv)
920 
921 /*
922  * This function provides external interface to the nexus for all
923  * functionalities related to the new DDI interrupt framework.
924  *
925  * Input:
926  * dip     - pointer to the dev_info structure of the requested device
927  * hdlp    - pointer to the internal interrupt handle structure for the
928  *	     requested interrupt
929  * intr_op - opcode for this call
930  * result  - pointer to the integer that will hold the result to be
931  *	     passed back if return value is PSM_SUCCESS
932  *
933  * Output:
934  * return value is either PSM_SUCCESS or PSM_FAILURE
935  */
936 int
937 apic_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
938     psm_intr_op_t intr_op, int *result)
939 {
940 	int		cap;
941 	int		count_vec;
942 	int		old_priority;
943 	int		new_priority;
944 	int		new_cpu;
945 	apic_irq_t	*irqp;
946 	struct intrspec *ispec, intr_spec;
947 
948 	DDI_INTR_IMPLDBG((CE_CONT, "apic_intr_ops: dip: %p hdlp: %p "
949 	    "intr_op: %x\n", (void *)dip, (void *)hdlp, intr_op));
950 
951 	ispec = &intr_spec;
952 	ispec->intrspec_pri = hdlp->ih_pri;
953 	ispec->intrspec_vec = hdlp->ih_inum;
954 	ispec->intrspec_func = hdlp->ih_cb_func;
955 
956 	switch (intr_op) {
957 	case PSM_INTR_OP_CHECK_MSI:
958 		/*
959 		 * Check MSI/X is supported or not at APIC level and
960 		 * masked off the MSI/X bits in hdlp->ih_type if not
961 		 * supported before return.  If MSI/X is supported,
962 		 * leave the ih_type unchanged and return.
963 		 *
964 		 * hdlp->ih_type passed in from the nexus has all the
965 		 * interrupt types supported by the device.
966 		 */
967 		if (apic_support_msi == 0) {
968 			/*
969 			 * if apic_support_msi is not set, call
970 			 * apic_check_msi_support() to check whether msi
971 			 * is supported first
972 			 */
973 			if (apic_check_msi_support() == PSM_SUCCESS)
974 				apic_support_msi = 1;
975 			else
976 				apic_support_msi = -1;
977 		}
978 		if (apic_support_msi == 1) {
979 			if (apic_msix_enable)
980 				*result = hdlp->ih_type;
981 			else
982 				*result = hdlp->ih_type & ~DDI_INTR_TYPE_MSIX;
983 		} else
984 			*result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
985 			    DDI_INTR_TYPE_MSIX);
986 		break;
987 	case PSM_INTR_OP_ALLOC_VECTORS:
988 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
989 			*result = apic_alloc_msi_vectors(dip, hdlp->ih_inum,
990 			    hdlp->ih_scratch1, hdlp->ih_pri,
991 			    (int)(uintptr_t)hdlp->ih_scratch2);
992 		else
993 			*result = apic_alloc_msix_vectors(dip, hdlp->ih_inum,
994 			    hdlp->ih_scratch1, hdlp->ih_pri,
995 			    (int)(uintptr_t)hdlp->ih_scratch2);
996 		break;
997 	case PSM_INTR_OP_FREE_VECTORS:
998 		apic_free_vectors(dip, hdlp->ih_inum, hdlp->ih_scratch1,
999 		    hdlp->ih_pri, hdlp->ih_type);
1000 		break;
1001 	case PSM_INTR_OP_NAVAIL_VECTORS:
1002 		*result = apic_navail_vector(dip, hdlp->ih_pri);
1003 		break;
1004 	case PSM_INTR_OP_XLATE_VECTOR:
1005 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
1006 		*result = apic_introp_xlate(dip, ispec, hdlp->ih_type);
1007 		if (*result == -1)
1008 			return (PSM_FAILURE);
1009 		break;
1010 	case PSM_INTR_OP_GET_PENDING:
1011 		if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
1012 			return (PSM_FAILURE);
1013 		*result = apic_get_pending(irqp, hdlp->ih_type);
1014 		break;
1015 	case PSM_INTR_OP_CLEAR_MASK:
1016 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
1017 			return (PSM_FAILURE);
1018 		irqp = apic_find_irq(dip, ispec, hdlp->ih_type);
1019 		if (irqp == NULL)
1020 			return (PSM_FAILURE);
1021 		apic_clear_mask(irqp);
1022 		break;
1023 	case PSM_INTR_OP_SET_MASK:
1024 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
1025 			return (PSM_FAILURE);
1026 		if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
1027 			return (PSM_FAILURE);
1028 		apic_set_mask(irqp);
1029 		break;
1030 	case PSM_INTR_OP_GET_CAP:
1031 		cap = DDI_INTR_FLAG_PENDING;
1032 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
1033 			cap |= DDI_INTR_FLAG_MASKABLE;
1034 		else if (hdlp->ih_type == DDI_INTR_TYPE_MSIX)
1035 			cap |= DDI_INTR_FLAG_RETARGETABLE;
1036 		*result = cap;
1037 		break;
1038 	case PSM_INTR_OP_GET_SHARED:
1039 		if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
1040 			return (PSM_FAILURE);
1041 		ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
1042 		if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
1043 			return (PSM_FAILURE);
1044 		*result = (irqp->airq_share > 1) ? 1: 0;
1045 		break;
1046 	case PSM_INTR_OP_SET_PRI:
1047 		old_priority = hdlp->ih_pri;	/* save old value */
1048 		new_priority = *(int *)result;	/* try the new value */
1049 
1050 		if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) {
1051 			return (PSM_SUCCESS);
1052 		}
1053 
1054 		/* Now allocate the vectors */
1055 		if (hdlp->ih_type == DDI_INTR_TYPE_MSI) {
1056 			/* SET_PRI does not support the case of multiple MSI */
1057 			if (i_ddi_intr_get_current_nintrs(hdlp->ih_dip) > 1)
1058 				return (PSM_FAILURE);
1059 
1060 			count_vec = apic_alloc_msi_vectors(dip, hdlp->ih_inum,
1061 			    1, new_priority,
1062 			    DDI_INTR_ALLOC_STRICT);
1063 		} else {
1064 			count_vec = apic_alloc_msix_vectors(dip, hdlp->ih_inum,
1065 			    1, new_priority,
1066 			    DDI_INTR_ALLOC_STRICT);
1067 		}
1068 
1069 		/* Did we get new vectors? */
1070 		if (!count_vec)
1071 			return (PSM_FAILURE);
1072 
1073 		/* Finally, free the previously allocated vectors */
1074 		apic_free_vectors(dip, hdlp->ih_inum, count_vec,
1075 		    old_priority, hdlp->ih_type);
1076 		break;
1077 	case PSM_INTR_OP_SET_CPU:
1078 	case PSM_INTR_OP_GRP_SET_CPU:
1079 		/*
1080 		 * The interrupt handle given here has been allocated
1081 		 * specifically for this command, and ih_private carries
1082 		 * a CPU value.
1083 		 */
1084 		new_cpu = (int)(intptr_t)hdlp->ih_private;
1085 		if (!apic_cpu_in_range(new_cpu)) {
1086 			DDI_INTR_IMPLDBG((CE_CONT,
1087 			    "[grp_]set_cpu: cpu out of range: %d\n", new_cpu));
1088 			*result = EINVAL;
1089 			return (PSM_FAILURE);
1090 		}
1091 		if (hdlp->ih_vector > APIC_MAX_VECTOR) {
1092 			DDI_INTR_IMPLDBG((CE_CONT,
1093 			    "[grp_]set_cpu: vector out of range: %d\n",
1094 			    hdlp->ih_vector));
1095 			*result = EINVAL;
1096 			return (PSM_FAILURE);
1097 		}
1098 		if (!(hdlp->ih_flags & PSMGI_INTRBY_IRQ))
1099 			hdlp->ih_vector = apic_vector_to_irq[hdlp->ih_vector];
1100 		if (intr_op == PSM_INTR_OP_SET_CPU) {
1101 			if (apic_set_cpu(hdlp->ih_vector, new_cpu, result) !=
1102 			    PSM_SUCCESS)
1103 				return (PSM_FAILURE);
1104 		} else {
1105 			if (apic_grp_set_cpu(hdlp->ih_vector, new_cpu,
1106 			    result) != PSM_SUCCESS)
1107 				return (PSM_FAILURE);
1108 		}
1109 		break;
1110 	case PSM_INTR_OP_GET_INTR:
1111 		/*
1112 		 * The interrupt handle given here has been allocated
1113 		 * specifically for this command, and ih_private carries
1114 		 * a pointer to a apic_get_intr_t.
1115 		 */
1116 		if (apic_get_vector_intr_info(
1117 		    hdlp->ih_vector, hdlp->ih_private) != PSM_SUCCESS)
1118 			return (PSM_FAILURE);
1119 		break;
1120 	case PSM_INTR_OP_APIC_TYPE:
1121 		hdlp->ih_private = apic_get_apic_type();
1122 		hdlp->ih_ver = apic_get_apic_version();
1123 		break;
1124 	case PSM_INTR_OP_SET_CAP:
1125 	default:
1126 		return (PSM_FAILURE);
1127 	}
1128 	return (PSM_SUCCESS);
1129 }
1130 #endif	/* !__xpv */
1131