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