10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51701Sjohnny * Common Development and Distribution License (the "License").
61701Sjohnny * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2212243SEvan.Yan@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * apic_introp.c:
270Sstevel@tonic-gate * Has code for Advanced DDI interrupt framework support.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <sys/cpuvar.h>
310Sstevel@tonic-gate #include <sys/psm.h>
323446Smrj #include <sys/archsystm.h>
333446Smrj #include <sys/apic.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
36916Sschwartz #include <sys/mach_intr.h>
37916Sschwartz #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/trap.h>
390Sstevel@tonic-gate #include <sys/pci.h>
400Sstevel@tonic-gate #include <sys/pci_intr_lib.h>
4112683SJimmy.Vetayases@oracle.com #include <sys/apic_common.h>
420Sstevel@tonic-gate
43916Sschwartz extern struct av_head autovect[];
44916Sschwartz
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Local Function Prototypes
470Sstevel@tonic-gate */
480Sstevel@tonic-gate apic_irq_t *apic_find_irq(dev_info_t *, struct intrspec *, int);
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * apic_pci_msi_enable_vector:
520Sstevel@tonic-gate * Set the address/data fields in the MSI/X capability structure
530Sstevel@tonic-gate * XXX: MSI-X support
540Sstevel@tonic-gate */
550Sstevel@tonic-gate /* ARGSUSED */
564937Sjohnny void
apic_pci_msi_enable_vector(apic_irq_t * irq_ptr,int type,int inum,int vector,int count,int target_apic_id)578675SVikram.Hegde@Sun.COM apic_pci_msi_enable_vector(apic_irq_t *irq_ptr, int type, int inum, int vector,
580Sstevel@tonic-gate int count, int target_apic_id)
590Sstevel@tonic-gate {
601997Sanish uint64_t msi_addr, msi_data;
611997Sanish ushort_t msi_ctrl;
628675SVikram.Hegde@Sun.COM dev_info_t *dip = irq_ptr->airq_dip;
631997Sanish int cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip);
641997Sanish ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(dip);
658675SVikram.Hegde@Sun.COM msi_regs_t msi_regs;
6612683SJimmy.Vetayases@oracle.com int irqno, i;
6712683SJimmy.Vetayases@oracle.com void *intrmap_tbl[PCI_MSI_MAX_INTRS];
680Sstevel@tonic-gate
690Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: dip=0x%p\n"
700Sstevel@tonic-gate "\tdriver = %s, inum=0x%x vector=0x%x apicid=0x%x\n", (void *)dip,
710Sstevel@tonic-gate ddi_driver_name(dip), inum, vector, target_apic_id));
720Sstevel@tonic-gate
734937Sjohnny ASSERT((handle != NULL) && (cap_ptr != 0));
741997Sanish
758675SVikram.Hegde@Sun.COM msi_regs.mr_data = vector;
768675SVikram.Hegde@Sun.COM msi_regs.mr_addr = target_apic_id;
778675SVikram.Hegde@Sun.COM
78*12822SJudy.Chen@Sun.COM for (i = 0; i < count; i++) {
79*12822SJudy.Chen@Sun.COM irqno = apic_vector_to_irq[vector + i];
80*12822SJudy.Chen@Sun.COM intrmap_tbl[i] = apic_irq_table[irqno]->airq_intrmap_private;
81*12822SJudy.Chen@Sun.COM }
8212683SJimmy.Vetayases@oracle.com apic_vt_ops->apic_intrmap_alloc_entry(intrmap_tbl, dip, type,
8312683SJimmy.Vetayases@oracle.com count, 0xff);
8412683SJimmy.Vetayases@oracle.com for (i = 0; i < count; i++) {
8512683SJimmy.Vetayases@oracle.com irqno = apic_vector_to_irq[vector + i];
8612683SJimmy.Vetayases@oracle.com apic_irq_table[irqno]->airq_intrmap_private =
8712683SJimmy.Vetayases@oracle.com intrmap_tbl[i];
8812683SJimmy.Vetayases@oracle.com }
8912683SJimmy.Vetayases@oracle.com
9012683SJimmy.Vetayases@oracle.com apic_vt_ops->apic_intrmap_map_entry(irq_ptr->airq_intrmap_private,
9112683SJimmy.Vetayases@oracle.com (void *)&msi_regs, type, count);
9212683SJimmy.Vetayases@oracle.com apic_vt_ops->apic_intrmap_record_msi(irq_ptr->airq_intrmap_private,
9312683SJimmy.Vetayases@oracle.com &msi_regs);
948678SVikram.Hegde@Sun.COM
950Sstevel@tonic-gate /* MSI Address */
968675SVikram.Hegde@Sun.COM msi_addr = msi_regs.mr_addr;
970Sstevel@tonic-gate
980Sstevel@tonic-gate /* MSI Data: MSI is edge triggered according to spec */
998675SVikram.Hegde@Sun.COM msi_data = msi_regs.mr_data;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: addr=0x%lx "
1020Sstevel@tonic-gate "data=0x%lx\n", (long)msi_addr, (long)msi_data));
1030Sstevel@tonic-gate
1041997Sanish if (type == DDI_INTR_TYPE_MSI) {
1051997Sanish msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
1061997Sanish
1071997Sanish /* Set the bits to inform how many MSIs are enabled */
1081997Sanish msi_ctrl |= ((highbit(count) -1) << PCI_MSI_MME_SHIFT);
1091997Sanish pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
1101997Sanish
11110175SStuart.Maybee@Sun.COM /*
11210175SStuart.Maybee@Sun.COM * Only set vector if not on hypervisor
11310175SStuart.Maybee@Sun.COM */
1141997Sanish pci_config_put32(handle,
1151997Sanish cap_ptr + PCI_MSI_ADDR_OFFSET, msi_addr);
1161997Sanish
1171997Sanish if (msi_ctrl & PCI_MSI_64BIT_MASK) {
1181997Sanish pci_config_put32(handle,
1191997Sanish cap_ptr + PCI_MSI_ADDR_OFFSET + 4, msi_addr >> 32);
1201997Sanish pci_config_put16(handle,
1211997Sanish cap_ptr + PCI_MSI_64BIT_DATA, msi_data);
1221997Sanish } else {
1231997Sanish pci_config_put16(handle,
1241997Sanish cap_ptr + PCI_MSI_32BIT_DATA, msi_data);
1251997Sanish }
1261997Sanish
1271997Sanish } else if (type == DDI_INTR_TYPE_MSIX) {
1281997Sanish uintptr_t off;
1291997Sanish ddi_intr_msix_t *msix_p = i_ddi_get_msix(dip);
1301997Sanish
13110647SLipeng.Sang@Sun.COM ASSERT(msix_p != NULL);
13210647SLipeng.Sang@Sun.COM
1331997Sanish /* Offset into the "inum"th entry in the MSI-X table */
1341997Sanish off = (uintptr_t)msix_p->msix_tbl_addr +
1351997Sanish (inum * PCI_MSIX_VECTOR_SIZE);
1361997Sanish
1371997Sanish ddi_put32(msix_p->msix_tbl_hdl,
1381997Sanish (uint32_t *)(off + PCI_MSIX_DATA_OFFSET), msi_data);
1391997Sanish ddi_put64(msix_p->msix_tbl_hdl,
1401997Sanish (uint64_t *)(off + PCI_MSIX_LOWER_ADDR_OFFSET), msi_addr);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * This function returns the no. of vectors available for the pri.
1460Sstevel@tonic-gate * dip is not used at this moment. If we really don't need that,
1470Sstevel@tonic-gate * it will be removed.
1480Sstevel@tonic-gate */
1490Sstevel@tonic-gate /*ARGSUSED*/
1500Sstevel@tonic-gate int
apic_navail_vector(dev_info_t * dip,int pri)1510Sstevel@tonic-gate apic_navail_vector(dev_info_t *dip, int pri)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate int lowest, highest, i, navail, count;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_navail_vector: dip: %p, pri: %x\n",
1560Sstevel@tonic-gate (void *)dip, pri));
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate highest = apic_ipltopri[pri] + APIC_VECTOR_MASK;
1590Sstevel@tonic-gate lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL;
1600Sstevel@tonic-gate navail = count = 0;
1610Sstevel@tonic-gate
1627282Smishra if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
1637282Smishra lowest -= APIC_VECTOR_PER_IPL;
1647282Smishra
1650Sstevel@tonic-gate /* It has to be contiguous */
16611641SKerry.Shu@Sun.COM for (i = lowest; i <= highest; i++) {
1670Sstevel@tonic-gate count = 0;
1680Sstevel@tonic-gate while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) &&
16911641SKerry.Shu@Sun.COM (i <= highest)) {
1702335Sjohnny if (APIC_CHECK_RESERVE_VECTORS(i))
1710Sstevel@tonic-gate break;
1720Sstevel@tonic-gate count++;
1730Sstevel@tonic-gate i++;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate if (count > navail)
1760Sstevel@tonic-gate navail = count;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate return (navail);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1812335Sjohnny /*
1822335Sjohnny * Finds "count" contiguous MSI vectors starting at the proper alignment
1832335Sjohnny * at "pri".
1842335Sjohnny * Caller needs to make sure that count has to be power of 2 and should not
1852335Sjohnny * be < 1.
1862335Sjohnny */
1873446Smrj uchar_t
apic_find_multi_vectors(int pri,int count)1880Sstevel@tonic-gate apic_find_multi_vectors(int pri, int count)
1890Sstevel@tonic-gate {
1902335Sjohnny int lowest, highest, i, navail, start, msibits;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_find_mult: pri: %x, count: %x\n",
1930Sstevel@tonic-gate pri, count));
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate highest = apic_ipltopri[pri] + APIC_VECTOR_MASK;
1960Sstevel@tonic-gate lowest = apic_ipltopri[pri - 1] + APIC_VECTOR_PER_IPL;
1970Sstevel@tonic-gate navail = 0;
1980Sstevel@tonic-gate
1997282Smishra if (highest < lowest) /* Both ipl and ipl - 1 map to same pri */
2007282Smishra lowest -= APIC_VECTOR_PER_IPL;
2017282Smishra
2022335Sjohnny /*
2032335Sjohnny * msibits is the no. of lower order message data bits for the
2042335Sjohnny * allocated MSI vectors and is used to calculate the aligned
2052335Sjohnny * starting vector
2062335Sjohnny */
2072335Sjohnny msibits = count - 1;
2082335Sjohnny
2090Sstevel@tonic-gate /* It has to be contiguous */
21011641SKerry.Shu@Sun.COM for (i = lowest; i <= highest; i++) {
2110Sstevel@tonic-gate navail = 0;
2122335Sjohnny
2132335Sjohnny /*
2142335Sjohnny * starting vector has to be aligned accordingly for
2152335Sjohnny * multiple MSIs
2162335Sjohnny */
2172335Sjohnny if (msibits)
2182335Sjohnny i = (i + msibits) & ~msibits;
2190Sstevel@tonic-gate start = i;
2200Sstevel@tonic-gate while ((apic_vector_to_irq[i] == APIC_RESV_IRQ) &&
22111641SKerry.Shu@Sun.COM (i <= highest)) {
2222335Sjohnny if (APIC_CHECK_RESERVE_VECTORS(i))
2230Sstevel@tonic-gate break;
2240Sstevel@tonic-gate navail++;
2250Sstevel@tonic-gate if (navail >= count)
2260Sstevel@tonic-gate return (start);
2270Sstevel@tonic-gate i++;
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2333446Smrj
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate * It finds the apic_irq_t associates with the dip, ispec and type.
2360Sstevel@tonic-gate */
2370Sstevel@tonic-gate apic_irq_t *
apic_find_irq(dev_info_t * dip,struct intrspec * ispec,int type)2380Sstevel@tonic-gate apic_find_irq(dev_info_t *dip, struct intrspec *ispec, int type)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate apic_irq_t *irqp;
2410Sstevel@tonic-gate int i;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: dip=0x%p vec=0x%x "
2440Sstevel@tonic-gate "ipl=0x%x type=0x%x\n", (void *)dip, ispec->intrspec_vec,
2450Sstevel@tonic-gate ispec->intrspec_pri, type));
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
24811465SKerry.Shu@Sun.COM for (irqp = apic_irq_table[i]; irqp; irqp = irqp->airq_next) {
24911465SKerry.Shu@Sun.COM if ((irqp->airq_dip == dip) &&
25011465SKerry.Shu@Sun.COM (irqp->airq_origirq == ispec->intrspec_vec) &&
25111465SKerry.Shu@Sun.COM (irqp->airq_ipl == ispec->intrspec_pri)) {
25211465SKerry.Shu@Sun.COM if (type == DDI_INTR_TYPE_MSI) {
25311465SKerry.Shu@Sun.COM if (irqp->airq_mps_intr_index ==
25411465SKerry.Shu@Sun.COM MSI_INDEX)
25511465SKerry.Shu@Sun.COM return (irqp);
25611465SKerry.Shu@Sun.COM } else if (type == DDI_INTR_TYPE_MSIX) {
25711465SKerry.Shu@Sun.COM if (irqp->airq_mps_intr_index ==
25811465SKerry.Shu@Sun.COM MSIX_INDEX)
25911465SKerry.Shu@Sun.COM return (irqp);
26011465SKerry.Shu@Sun.COM } else
2614937Sjohnny return (irqp);
26211465SKerry.Shu@Sun.COM }
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: return NULL\n"));
2660Sstevel@tonic-gate return (NULL);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate * This function will return the pending bit of the irqp.
2710Sstevel@tonic-gate * It either comes from the IRR register of the APIC or the RDT
2720Sstevel@tonic-gate * entry of the I/O APIC.
2730Sstevel@tonic-gate * For the IRR to work, it needs to be to its binding CPU
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate static int
apic_get_pending(apic_irq_t * irqp,int type)2760Sstevel@tonic-gate apic_get_pending(apic_irq_t *irqp, int type)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate int bit, index, irr, pending;
2790Sstevel@tonic-gate int intin_no;
2803446Smrj int apic_ix;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_get_pending: irqp: %p, cpuid: %x "
2830Sstevel@tonic-gate "type: %x\n", (void *)irqp, irqp->airq_cpu & ~IRQ_USER_BOUND,
2840Sstevel@tonic-gate type));
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate /* need to get on the bound cpu */
2870Sstevel@tonic-gate mutex_enter(&cpu_lock);
2880Sstevel@tonic-gate affinity_set(irqp->airq_cpu & ~IRQ_USER_BOUND);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate index = irqp->airq_vector / 32;
2910Sstevel@tonic-gate bit = irqp->airq_vector % 32;
2927282Smishra irr = apic_reg_ops->apic_read(APIC_IRR_REG + index);
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate affinity_clear();
2950Sstevel@tonic-gate mutex_exit(&cpu_lock);
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate pending = (irr & (1 << bit)) ? 1 : 0;
2980Sstevel@tonic-gate if (!pending && (type == DDI_INTR_TYPE_FIXED)) {
2990Sstevel@tonic-gate /* check I/O APIC for fixed interrupt */
3000Sstevel@tonic-gate intin_no = irqp->airq_intin_no;
3013446Smrj apic_ix = irqp->airq_ioapicindex;
3023446Smrj pending = (READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no) &
3030Sstevel@tonic-gate AV_PENDING) ? 1 : 0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate return (pending);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * This function will clear the mask for the interrupt on the I/O APIC
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate static void
apic_clear_mask(apic_irq_t * irqp)3130Sstevel@tonic-gate apic_clear_mask(apic_irq_t *irqp)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate int intin_no;
3163446Smrj ulong_t iflag;
3170Sstevel@tonic-gate int32_t rdt_entry;
3183446Smrj int apic_ix;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_clear_mask: irqp: %p\n",
3210Sstevel@tonic-gate (void *)irqp));
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate intin_no = irqp->airq_intin_no;
3243446Smrj apic_ix = irqp->airq_ioapicindex;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate iflag = intr_clear();
3270Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
3280Sstevel@tonic-gate
3293446Smrj rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no);
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate /* clear mask */
3323446Smrj WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no,
3330Sstevel@tonic-gate ((~AV_MASK) & rdt_entry));
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate lock_clear(&apic_ioapic_lock);
3360Sstevel@tonic-gate intr_restore(iflag);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate * This function will mask the interrupt on the I/O APIC
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate static void
apic_set_mask(apic_irq_t * irqp)3440Sstevel@tonic-gate apic_set_mask(apic_irq_t *irqp)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate int intin_no;
3473446Smrj int apic_ix;
3483446Smrj ulong_t iflag;
3490Sstevel@tonic-gate int32_t rdt_entry;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_set_mask: irqp: %p\n", (void *)irqp));
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate intin_no = irqp->airq_intin_no;
3543446Smrj apic_ix = irqp->airq_ioapicindex;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate iflag = intr_clear();
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate lock_set(&apic_ioapic_lock);
3590Sstevel@tonic-gate
3603446Smrj rdt_entry = READ_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no);
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /* mask it */
3633446Smrj WRITE_IOAPIC_RDT_ENTRY_LOW_DWORD(apic_ix, intin_no,
3640Sstevel@tonic-gate (AV_MASK | rdt_entry));
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate lock_clear(&apic_ioapic_lock);
3670Sstevel@tonic-gate intr_restore(iflag);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate void
apic_free_vectors(dev_info_t * dip,int inum,int count,int pri,int type)3720Sstevel@tonic-gate apic_free_vectors(dev_info_t *dip, int inum, int count, int pri, int type)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate int i;
3750Sstevel@tonic-gate apic_irq_t *irqptr;
3760Sstevel@tonic-gate struct intrspec ispec;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: dip: %p inum: %x "
3790Sstevel@tonic-gate "count: %x pri: %x type: %x\n",
3800Sstevel@tonic-gate (void *)dip, inum, count, pri, type));
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /* for MSI/X only */
3830Sstevel@tonic-gate if (!DDI_INTR_IS_MSI_OR_MSIX(type))
3840Sstevel@tonic-gate return;
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate for (i = 0; i < count; i++) {
3870Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: inum=0x%x "
3880Sstevel@tonic-gate "pri=0x%x count=0x%x\n", inum, pri, count));
3890Sstevel@tonic-gate ispec.intrspec_vec = inum + i;
3900Sstevel@tonic-gate ispec.intrspec_pri = pri;
3910Sstevel@tonic-gate if ((irqptr = apic_find_irq(dip, &ispec, type)) == NULL) {
3920Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_free_vectors: "
3930Sstevel@tonic-gate "dip=0x%p inum=0x%x pri=0x%x apic_find_irq() "
3940Sstevel@tonic-gate "failed\n", (void *)dip, inum, pri));
3950Sstevel@tonic-gate continue;
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate irqptr->airq_mps_intr_index = FREE_INDEX;
3980Sstevel@tonic-gate apic_vector_to_irq[irqptr->airq_vector] = APIC_RESV_IRQ;
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4021997Sanish /*
4031997Sanish * apic_pci_msi_enable_mode:
4041997Sanish */
4054937Sjohnny void
apic_pci_msi_enable_mode(dev_info_t * rdip,int type,int inum)4061997Sanish apic_pci_msi_enable_mode(dev_info_t *rdip, int type, int inum)
4071997Sanish {
4081997Sanish ushort_t msi_ctrl;
4091997Sanish int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
4101997Sanish ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip);
4111997Sanish
4124937Sjohnny ASSERT((handle != NULL) && (cap_ptr != 0));
4131997Sanish
4141997Sanish if (type == DDI_INTR_TYPE_MSI) {
4151997Sanish msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
4161997Sanish if ((msi_ctrl & PCI_MSI_ENABLE_BIT))
4174937Sjohnny return;
4181997Sanish
4191997Sanish msi_ctrl |= PCI_MSI_ENABLE_BIT;
4201997Sanish pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
4211997Sanish
4221997Sanish } else if (type == DDI_INTR_TYPE_MSIX) {
4231997Sanish uintptr_t off;
4244937Sjohnny uint32_t mask;
4251997Sanish ddi_intr_msix_t *msix_p;
4261997Sanish
4271997Sanish msix_p = i_ddi_get_msix(rdip);
4281997Sanish
42910647SLipeng.Sang@Sun.COM ASSERT(msix_p != NULL);
43010647SLipeng.Sang@Sun.COM
4311997Sanish /* Offset into "inum"th entry in the MSI-X table & clear mask */
4321997Sanish off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
4331997Sanish PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
4344937Sjohnny
4354937Sjohnny mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off);
4364937Sjohnny
4374937Sjohnny ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask & ~1));
4384937Sjohnny
4394937Sjohnny msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL);
4404937Sjohnny
4414937Sjohnny if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT)) {
4424937Sjohnny msi_ctrl |= PCI_MSIX_ENABLE_BIT;
4434937Sjohnny pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL,
4444937Sjohnny msi_ctrl);
4454937Sjohnny }
4461997Sanish }
4471997Sanish }
4481997Sanish
4494397Sschwartz static int
apic_set_cpu(int irqno,int cpu,int * result)45010053SEvan.Yan@Sun.COM apic_set_cpu(int irqno, int cpu, int *result)
4514397Sschwartz {
4524397Sschwartz apic_irq_t *irqp;
4536336Sbholler ulong_t iflag;
4544397Sschwartz int ret;
4554397Sschwartz
4564397Sschwartz DDI_INTR_IMPLDBG((CE_CONT, "APIC_SET_CPU\n"));
4574397Sschwartz
4584397Sschwartz mutex_enter(&airq_mutex);
45910053SEvan.Yan@Sun.COM irqp = apic_irq_table[irqno];
4604397Sschwartz mutex_exit(&airq_mutex);
4614397Sschwartz
4624397Sschwartz if (irqp == NULL) {
4634397Sschwartz *result = ENXIO;
4644397Sschwartz return (PSM_FAILURE);
4654397Sschwartz }
4664397Sschwartz
4674397Sschwartz /* Fail if this is an MSI intr and is part of a group. */
4684397Sschwartz if ((irqp->airq_mps_intr_index == MSI_INDEX) &&
4694397Sschwartz (irqp->airq_intin_no > 1)) {
4704397Sschwartz *result = ENXIO;
4714397Sschwartz return (PSM_FAILURE);
4724397Sschwartz }
4734397Sschwartz
4744397Sschwartz iflag = intr_clear();
4754397Sschwartz lock_set(&apic_ioapic_lock);
4764397Sschwartz
4774397Sschwartz ret = apic_rebind_all(irqp, cpu);
4784397Sschwartz
4794397Sschwartz lock_clear(&apic_ioapic_lock);
4804397Sschwartz intr_restore(iflag);
4814397Sschwartz
4824397Sschwartz if (ret) {
4834397Sschwartz *result = EIO;
4844397Sschwartz return (PSM_FAILURE);
4854397Sschwartz }
48610053SEvan.Yan@Sun.COM /*
48710053SEvan.Yan@Sun.COM * keep tracking the default interrupt cpu binding
48810053SEvan.Yan@Sun.COM */
48910053SEvan.Yan@Sun.COM irqp->airq_cpu = cpu;
49010053SEvan.Yan@Sun.COM
4914397Sschwartz *result = 0;
4924397Sschwartz return (PSM_SUCCESS);
4934397Sschwartz }
4944397Sschwartz
4954397Sschwartz static int
apic_grp_set_cpu(int irqno,int new_cpu,int * result)49610053SEvan.Yan@Sun.COM apic_grp_set_cpu(int irqno, int new_cpu, int *result)
4974397Sschwartz {
4984397Sschwartz dev_info_t *orig_dip;
4997282Smishra uint32_t orig_cpu;
5006336Sbholler ulong_t iflag;
5014397Sschwartz apic_irq_t *irqps[PCI_MSI_MAX_INTRS];
5024397Sschwartz int i;
5034397Sschwartz int cap_ptr;
5044397Sschwartz int msi_mask_off;
5054397Sschwartz ushort_t msi_ctrl;
5064397Sschwartz uint32_t msi_pvm;
5074397Sschwartz ddi_acc_handle_t handle;
5084397Sschwartz int num_vectors = 0;
50910053SEvan.Yan@Sun.COM uint32_t vector;
5104397Sschwartz
5114397Sschwartz DDI_INTR_IMPLDBG((CE_CONT, "APIC_GRP_SET_CPU\n"));
5124397Sschwartz
5134397Sschwartz /*
5144397Sschwartz * Take mutex to insure that table doesn't change out from underneath
5154397Sschwartz * us while we're playing with it.
5164397Sschwartz */
5174397Sschwartz mutex_enter(&airq_mutex);
51810053SEvan.Yan@Sun.COM irqps[0] = apic_irq_table[irqno];
5194397Sschwartz orig_cpu = irqps[0]->airq_temp_cpu;
5204397Sschwartz orig_dip = irqps[0]->airq_dip;
5214397Sschwartz num_vectors = irqps[0]->airq_intin_no;
52210053SEvan.Yan@Sun.COM vector = irqps[0]->airq_vector;
5234397Sschwartz
5244397Sschwartz /* A "group" of 1 */
5254397Sschwartz if (num_vectors == 1) {
5264397Sschwartz mutex_exit(&airq_mutex);
52710053SEvan.Yan@Sun.COM return (apic_set_cpu(irqno, new_cpu, result));
5284397Sschwartz }
5294397Sschwartz
5304397Sschwartz *result = ENXIO;
5314397Sschwartz
5324397Sschwartz if (irqps[0]->airq_mps_intr_index != MSI_INDEX) {
5334397Sschwartz mutex_exit(&airq_mutex);
5344397Sschwartz DDI_INTR_IMPLDBG((CE_CONT, "set_grp: intr not MSI\n"));
5354397Sschwartz goto set_grp_intr_done;
5364397Sschwartz }
5374397Sschwartz if ((num_vectors < 1) || ((num_vectors - 1) & vector)) {
5384397Sschwartz mutex_exit(&airq_mutex);
5394397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
5404397Sschwartz "set_grp: base vec not part of a grp or not aligned: "
5414397Sschwartz "vec:0x%x, num_vec:0x%x\n", vector, num_vectors));
5424397Sschwartz goto set_grp_intr_done;
5434397Sschwartz }
5444397Sschwartz DDI_INTR_IMPLDBG((CE_CONT, "set_grp: num intrs in grp: %d\n",
5454397Sschwartz num_vectors));
5464397Sschwartz
5474397Sschwartz ASSERT((num_vectors + vector) < APIC_MAX_VECTOR);
5484397Sschwartz
5494397Sschwartz *result = EIO;
5504397Sschwartz
5514397Sschwartz /*
5524397Sschwartz * All IRQ entries in the table for the given device will be not
5534397Sschwartz * shared. Since they are not shared, the dip in the table will
5544397Sschwartz * be true to the device of interest.
5554397Sschwartz */
5564397Sschwartz for (i = 1; i < num_vectors; i++) {
5574397Sschwartz irqps[i] = apic_irq_table[apic_vector_to_irq[vector + i]];
5584397Sschwartz if (irqps[i] == NULL) {
5594397Sschwartz mutex_exit(&airq_mutex);
5604397Sschwartz goto set_grp_intr_done;
5614397Sschwartz }
5624397Sschwartz #ifdef DEBUG
5634397Sschwartz /* Sanity check: CPU and dip is the same for all entries. */
5644397Sschwartz if ((irqps[i]->airq_dip != orig_dip) ||
5654397Sschwartz (irqps[i]->airq_temp_cpu != orig_cpu)) {
5664397Sschwartz mutex_exit(&airq_mutex);
5674397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
5684397Sschwartz "set_grp: cpu or dip for vec 0x%x difft than for "
5694397Sschwartz "vec 0x%x\n", vector, vector + i));
5704397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
5714397Sschwartz " cpu: %d vs %d, dip: 0x%p vs 0x%p\n", orig_cpu,
5724397Sschwartz irqps[i]->airq_temp_cpu, (void *)orig_dip,
5734397Sschwartz (void *)irqps[i]->airq_dip));
5744397Sschwartz goto set_grp_intr_done;
5754397Sschwartz }
5764397Sschwartz #endif /* DEBUG */
5774397Sschwartz }
5784397Sschwartz mutex_exit(&airq_mutex);
5794397Sschwartz
5804397Sschwartz cap_ptr = i_ddi_get_msi_msix_cap_ptr(orig_dip);
5814397Sschwartz handle = i_ddi_get_pci_config_handle(orig_dip);
5824397Sschwartz msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
5834397Sschwartz
5844397Sschwartz /* MSI Per vector masking is supported. */
5854397Sschwartz if (msi_ctrl & PCI_MSI_PVM_MASK) {
5864397Sschwartz if (msi_ctrl & PCI_MSI_64BIT_MASK)
5874397Sschwartz msi_mask_off = cap_ptr + PCI_MSI_64BIT_MASKBITS;
5884397Sschwartz else
5894397Sschwartz msi_mask_off = cap_ptr + PCI_MSI_32BIT_MASK;
5904397Sschwartz msi_pvm = pci_config_get32(handle, msi_mask_off);
5914397Sschwartz pci_config_put32(handle, msi_mask_off, (uint32_t)-1);
5924397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
5934397Sschwartz "set_grp: pvm supported. Mask set to 0x%x\n",
5944397Sschwartz pci_config_get32(handle, msi_mask_off)));
5954397Sschwartz }
5964397Sschwartz
5974397Sschwartz iflag = intr_clear();
5984397Sschwartz lock_set(&apic_ioapic_lock);
5994397Sschwartz
6004397Sschwartz /*
6014397Sschwartz * Do the first rebind and check for errors. Apic_rebind_all returns
6024397Sschwartz * an error if the CPU is not accepting interrupts. If the first one
6034397Sschwartz * succeeds they all will.
6044397Sschwartz */
6054397Sschwartz if (apic_rebind_all(irqps[0], new_cpu))
6064397Sschwartz (void) apic_rebind_all(irqps[0], orig_cpu);
6074397Sschwartz else {
60810053SEvan.Yan@Sun.COM irqps[0]->airq_cpu = new_cpu;
60910053SEvan.Yan@Sun.COM
61010053SEvan.Yan@Sun.COM for (i = 1; i < num_vectors; i++) {
6114397Sschwartz (void) apic_rebind_all(irqps[i], new_cpu);
61210053SEvan.Yan@Sun.COM irqps[i]->airq_cpu = new_cpu;
61310053SEvan.Yan@Sun.COM }
6144397Sschwartz *result = 0; /* SUCCESS */
6154397Sschwartz }
6164397Sschwartz
6174397Sschwartz lock_clear(&apic_ioapic_lock);
6184397Sschwartz intr_restore(iflag);
6194397Sschwartz
6204397Sschwartz /* Reenable vectors if per vector masking is supported. */
6214397Sschwartz if (msi_ctrl & PCI_MSI_PVM_MASK) {
6224397Sschwartz pci_config_put32(handle, msi_mask_off, msi_pvm);
6234397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
6244397Sschwartz "set_grp: pvm supported. Mask restored to 0x%x\n",
6254397Sschwartz pci_config_get32(handle, msi_mask_off)));
6264397Sschwartz }
6274397Sschwartz
6284397Sschwartz set_grp_intr_done:
6294397Sschwartz if (*result != 0)
6304397Sschwartz return (PSM_FAILURE);
6314397Sschwartz
6324397Sschwartz return (PSM_SUCCESS);
6334397Sschwartz }
6344397Sschwartz
6355084Sjohnlev int
apic_get_vector_intr_info(int vecirq,apic_get_intr_t * intr_params_p)6364397Sschwartz apic_get_vector_intr_info(int vecirq, apic_get_intr_t *intr_params_p)
6374397Sschwartz {
6384397Sschwartz struct autovec *av_dev;
6394397Sschwartz uchar_t irqno;
6404397Sschwartz int i;
6414397Sschwartz apic_irq_t *irq_p;
6424397Sschwartz
6434397Sschwartz /* Sanity check the vector/irq argument. */
6444397Sschwartz ASSERT((vecirq >= 0) || (vecirq <= APIC_MAX_VECTOR));
6454397Sschwartz
6464397Sschwartz mutex_enter(&airq_mutex);
6474397Sschwartz
6484397Sschwartz /*
6494397Sschwartz * Convert the vecirq arg to an irq using vector_to_irq table
6504397Sschwartz * if the arg is a vector. Pass thru if already an irq.
6514397Sschwartz */
6524397Sschwartz if ((intr_params_p->avgi_req_flags & PSMGI_INTRBY_FLAGS) ==
6534397Sschwartz PSMGI_INTRBY_VEC)
6544397Sschwartz irqno = apic_vector_to_irq[vecirq];
6554397Sschwartz else
6564397Sschwartz irqno = vecirq;
6574397Sschwartz
6584397Sschwartz irq_p = apic_irq_table[irqno];
6594397Sschwartz
6604397Sschwartz if ((irq_p == NULL) ||
66112243SEvan.Yan@Sun.COM ((irq_p->airq_mps_intr_index != RESERVE_INDEX) &&
66212243SEvan.Yan@Sun.COM ((irq_p->airq_temp_cpu == IRQ_UNBOUND) ||
66312243SEvan.Yan@Sun.COM (irq_p->airq_temp_cpu == IRQ_UNINIT)))) {
6644397Sschwartz mutex_exit(&airq_mutex);
6654397Sschwartz return (PSM_FAILURE);
6664397Sschwartz }
6674397Sschwartz
6684397Sschwartz if (intr_params_p->avgi_req_flags & PSMGI_REQ_CPUID) {
6694397Sschwartz
6704397Sschwartz /* Get the (temp) cpu from apic_irq table, indexed by irq. */
6714397Sschwartz intr_params_p->avgi_cpu_id = irq_p->airq_temp_cpu;
6724397Sschwartz
6734397Sschwartz /* Return user bound info for intrd. */
6744397Sschwartz if (intr_params_p->avgi_cpu_id & IRQ_USER_BOUND) {
6754397Sschwartz intr_params_p->avgi_cpu_id &= ~IRQ_USER_BOUND;
6764397Sschwartz intr_params_p->avgi_cpu_id |= PSMGI_CPU_USER_BOUND;
6774397Sschwartz }
6784397Sschwartz }
6794397Sschwartz
6804397Sschwartz if (intr_params_p->avgi_req_flags & PSMGI_REQ_VECTOR)
6814397Sschwartz intr_params_p->avgi_vector = irq_p->airq_vector;
6824397Sschwartz
6834397Sschwartz if (intr_params_p->avgi_req_flags &
6844397Sschwartz (PSMGI_REQ_NUM_DEVS | PSMGI_REQ_GET_DEVS))
6854397Sschwartz /* Get number of devices from apic_irq table shared field. */
6864397Sschwartz intr_params_p->avgi_num_devs = irq_p->airq_share;
6874397Sschwartz
6884397Sschwartz if (intr_params_p->avgi_req_flags & PSMGI_REQ_GET_DEVS) {
6894397Sschwartz
6904397Sschwartz intr_params_p->avgi_req_flags |= PSMGI_REQ_NUM_DEVS;
6914397Sschwartz
6924397Sschwartz /* Some devices have NULL dip. Don't count these. */
6934397Sschwartz if (intr_params_p->avgi_num_devs > 0) {
6944397Sschwartz for (i = 0, av_dev = autovect[irqno].avh_link;
6954397Sschwartz av_dev; av_dev = av_dev->av_link)
6964397Sschwartz if (av_dev->av_vector && av_dev->av_dip)
6974397Sschwartz i++;
6984397Sschwartz intr_params_p->avgi_num_devs =
6994397Sschwartz MIN(intr_params_p->avgi_num_devs, i);
7004397Sschwartz }
7014397Sschwartz
7024397Sschwartz /* There are no viable dips to return. */
7034397Sschwartz if (intr_params_p->avgi_num_devs == 0)
7044397Sschwartz intr_params_p->avgi_dip_list = NULL;
7054397Sschwartz
7064397Sschwartz else { /* Return list of dips */
7074397Sschwartz
7084397Sschwartz /* Allocate space in array for that number of devs. */
7094397Sschwartz intr_params_p->avgi_dip_list = kmem_zalloc(
7104397Sschwartz intr_params_p->avgi_num_devs *
7114397Sschwartz sizeof (dev_info_t *),
7124397Sschwartz KM_SLEEP);
7134397Sschwartz
7144397Sschwartz /*
7154397Sschwartz * Loop through the device list of the autovec table
7164397Sschwartz * filling in the dip array.
7174397Sschwartz *
7184397Sschwartz * Note that the autovect table may have some special
7194397Sschwartz * entries which contain NULL dips. These will be
7204397Sschwartz * ignored.
7214397Sschwartz */
7224397Sschwartz for (i = 0, av_dev = autovect[irqno].avh_link;
7234397Sschwartz av_dev; av_dev = av_dev->av_link)
7244397Sschwartz if (av_dev->av_vector && av_dev->av_dip)
7254397Sschwartz intr_params_p->avgi_dip_list[i++] =
7264397Sschwartz av_dev->av_dip;
7274397Sschwartz }
7284397Sschwartz }
7294397Sschwartz
7304397Sschwartz mutex_exit(&airq_mutex);
7314397Sschwartz
7324397Sschwartz return (PSM_SUCCESS);
7334397Sschwartz }
7344397Sschwartz
7351997Sanish /*
7360Sstevel@tonic-gate * This function provides external interface to the nexus for all
7370Sstevel@tonic-gate * functionalities related to the new DDI interrupt framework.
7380Sstevel@tonic-gate *
7390Sstevel@tonic-gate * Input:
7400Sstevel@tonic-gate * dip - pointer to the dev_info structure of the requested device
7410Sstevel@tonic-gate * hdlp - pointer to the internal interrupt handle structure for the
7420Sstevel@tonic-gate * requested interrupt
7430Sstevel@tonic-gate * intr_op - opcode for this call
7440Sstevel@tonic-gate * result - pointer to the integer that will hold the result to be
7450Sstevel@tonic-gate * passed back if return value is PSM_SUCCESS
7460Sstevel@tonic-gate *
7470Sstevel@tonic-gate * Output:
7480Sstevel@tonic-gate * return value is either PSM_SUCCESS or PSM_FAILURE
7490Sstevel@tonic-gate */
7500Sstevel@tonic-gate int
apic_intr_ops(dev_info_t * dip,ddi_intr_handle_impl_t * hdlp,psm_intr_op_t intr_op,int * result)7510Sstevel@tonic-gate apic_intr_ops(dev_info_t *dip, ddi_intr_handle_impl_t *hdlp,
7520Sstevel@tonic-gate psm_intr_op_t intr_op, int *result)
7530Sstevel@tonic-gate {
7544397Sschwartz int cap;
7550Sstevel@tonic-gate int count_vec;
7560Sstevel@tonic-gate int old_priority;
7570Sstevel@tonic-gate int new_priority;
7584397Sschwartz int new_cpu;
7590Sstevel@tonic-gate apic_irq_t *irqp;
7600Sstevel@tonic-gate struct intrspec *ispec, intr_spec;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate DDI_INTR_IMPLDBG((CE_CONT, "apic_intr_ops: dip: %p hdlp: %p "
7630Sstevel@tonic-gate "intr_op: %x\n", (void *)dip, (void *)hdlp, intr_op));
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate ispec = &intr_spec;
7660Sstevel@tonic-gate ispec->intrspec_pri = hdlp->ih_pri;
7670Sstevel@tonic-gate ispec->intrspec_vec = hdlp->ih_inum;
7680Sstevel@tonic-gate ispec->intrspec_func = hdlp->ih_cb_func;
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate switch (intr_op) {
7710Sstevel@tonic-gate case PSM_INTR_OP_CHECK_MSI:
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate * Check MSI/X is supported or not at APIC level and
7740Sstevel@tonic-gate * masked off the MSI/X bits in hdlp->ih_type if not
7750Sstevel@tonic-gate * supported before return. If MSI/X is supported,
7760Sstevel@tonic-gate * leave the ih_type unchanged and return.
7770Sstevel@tonic-gate *
7780Sstevel@tonic-gate * hdlp->ih_type passed in from the nexus has all the
7790Sstevel@tonic-gate * interrupt types supported by the device.
7800Sstevel@tonic-gate */
7810Sstevel@tonic-gate if (apic_support_msi == 0) {
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate * if apic_support_msi is not set, call
7840Sstevel@tonic-gate * apic_check_msi_support() to check whether msi
7850Sstevel@tonic-gate * is supported first
7860Sstevel@tonic-gate */
7871701Sjohnny if (apic_check_msi_support() == PSM_SUCCESS)
7880Sstevel@tonic-gate apic_support_msi = 1;
7890Sstevel@tonic-gate else
7900Sstevel@tonic-gate apic_support_msi = -1;
7910Sstevel@tonic-gate }
7924937Sjohnny if (apic_support_msi == 1) {
7934937Sjohnny if (apic_msix_enable)
7944937Sjohnny *result = hdlp->ih_type;
7954937Sjohnny else
7964937Sjohnny *result = hdlp->ih_type & ~DDI_INTR_TYPE_MSIX;
7974937Sjohnny } else
7980Sstevel@tonic-gate *result = hdlp->ih_type & ~(DDI_INTR_TYPE_MSI |
7990Sstevel@tonic-gate DDI_INTR_TYPE_MSIX);
8000Sstevel@tonic-gate break;
8010Sstevel@tonic-gate case PSM_INTR_OP_ALLOC_VECTORS:
8024937Sjohnny if (hdlp->ih_type == DDI_INTR_TYPE_MSI)
8034937Sjohnny *result = apic_alloc_msi_vectors(dip, hdlp->ih_inum,
8044937Sjohnny hdlp->ih_scratch1, hdlp->ih_pri,
8054937Sjohnny (int)(uintptr_t)hdlp->ih_scratch2);
8064937Sjohnny else
8074937Sjohnny *result = apic_alloc_msix_vectors(dip, hdlp->ih_inum,
8084937Sjohnny hdlp->ih_scratch1, hdlp->ih_pri,
8094937Sjohnny (int)(uintptr_t)hdlp->ih_scratch2);
8100Sstevel@tonic-gate break;
8110Sstevel@tonic-gate case PSM_INTR_OP_FREE_VECTORS:
8120Sstevel@tonic-gate apic_free_vectors(dip, hdlp->ih_inum, hdlp->ih_scratch1,
8130Sstevel@tonic-gate hdlp->ih_pri, hdlp->ih_type);
8140Sstevel@tonic-gate break;
8150Sstevel@tonic-gate case PSM_INTR_OP_NAVAIL_VECTORS:
8160Sstevel@tonic-gate *result = apic_navail_vector(dip, hdlp->ih_pri);
8170Sstevel@tonic-gate break;
8180Sstevel@tonic-gate case PSM_INTR_OP_XLATE_VECTOR:
819916Sschwartz ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
8200Sstevel@tonic-gate *result = apic_introp_xlate(dip, ispec, hdlp->ih_type);
82111465SKerry.Shu@Sun.COM if (*result == -1)
82211465SKerry.Shu@Sun.COM return (PSM_FAILURE);
8230Sstevel@tonic-gate break;
8240Sstevel@tonic-gate case PSM_INTR_OP_GET_PENDING:
8250Sstevel@tonic-gate if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
8260Sstevel@tonic-gate return (PSM_FAILURE);
8270Sstevel@tonic-gate *result = apic_get_pending(irqp, hdlp->ih_type);
8280Sstevel@tonic-gate break;
8290Sstevel@tonic-gate case PSM_INTR_OP_CLEAR_MASK:
8300Sstevel@tonic-gate if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
8310Sstevel@tonic-gate return (PSM_FAILURE);
8320Sstevel@tonic-gate irqp = apic_find_irq(dip, ispec, hdlp->ih_type);
8330Sstevel@tonic-gate if (irqp == NULL)
8340Sstevel@tonic-gate return (PSM_FAILURE);
8350Sstevel@tonic-gate apic_clear_mask(irqp);
8360Sstevel@tonic-gate break;
8370Sstevel@tonic-gate case PSM_INTR_OP_SET_MASK:
8380Sstevel@tonic-gate if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
8390Sstevel@tonic-gate return (PSM_FAILURE);
8400Sstevel@tonic-gate if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
8410Sstevel@tonic-gate return (PSM_FAILURE);
8420Sstevel@tonic-gate apic_set_mask(irqp);
8430Sstevel@tonic-gate break;
8440Sstevel@tonic-gate case PSM_INTR_OP_GET_CAP:
8450Sstevel@tonic-gate cap = DDI_INTR_FLAG_PENDING;
8460Sstevel@tonic-gate if (hdlp->ih_type == DDI_INTR_TYPE_FIXED)
8470Sstevel@tonic-gate cap |= DDI_INTR_FLAG_MASKABLE;
8480Sstevel@tonic-gate *result = cap;
8490Sstevel@tonic-gate break;
8500Sstevel@tonic-gate case PSM_INTR_OP_GET_SHARED:
8510Sstevel@tonic-gate if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
8520Sstevel@tonic-gate return (PSM_FAILURE);
85310190SSophia.Li@Sun.COM ispec = ((ihdl_plat_t *)hdlp->ih_private)->ip_ispecp;
8540Sstevel@tonic-gate if ((irqp = apic_find_irq(dip, ispec, hdlp->ih_type)) == NULL)
8550Sstevel@tonic-gate return (PSM_FAILURE);
85610190SSophia.Li@Sun.COM *result = (irqp->airq_share > 1) ? 1: 0;
8570Sstevel@tonic-gate break;
8580Sstevel@tonic-gate case PSM_INTR_OP_SET_PRI:
8590Sstevel@tonic-gate old_priority = hdlp->ih_pri; /* save old value */
8600Sstevel@tonic-gate new_priority = *(int *)result; /* try the new value */
8610Sstevel@tonic-gate
86210190SSophia.Li@Sun.COM if (hdlp->ih_type == DDI_INTR_TYPE_FIXED) {
86310190SSophia.Li@Sun.COM return (PSM_SUCCESS);
86410190SSophia.Li@Sun.COM }
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate /* Now allocate the vectors */
86710190SSophia.Li@Sun.COM if (hdlp->ih_type == DDI_INTR_TYPE_MSI) {
86810190SSophia.Li@Sun.COM /* SET_PRI does not support the case of multiple MSI */
86910190SSophia.Li@Sun.COM if (i_ddi_intr_get_current_nintrs(hdlp->ih_dip) > 1)
87010190SSophia.Li@Sun.COM return (PSM_FAILURE);
87110190SSophia.Li@Sun.COM
8724937Sjohnny count_vec = apic_alloc_msi_vectors(dip, hdlp->ih_inum,
87310190SSophia.Li@Sun.COM 1, new_priority,
8744937Sjohnny DDI_INTR_ALLOC_STRICT);
87510190SSophia.Li@Sun.COM } else {
8764937Sjohnny count_vec = apic_alloc_msix_vectors(dip, hdlp->ih_inum,
87710190SSophia.Li@Sun.COM 1, new_priority,
8784937Sjohnny DDI_INTR_ALLOC_STRICT);
87910190SSophia.Li@Sun.COM }
8800Sstevel@tonic-gate
8813446Smrj /* Did we get new vectors? */
8822335Sjohnny if (!count_vec)
8830Sstevel@tonic-gate return (PSM_FAILURE);
8840Sstevel@tonic-gate
8850Sstevel@tonic-gate /* Finally, free the previously allocated vectors */
8860Sstevel@tonic-gate apic_free_vectors(dip, hdlp->ih_inum, count_vec,
8870Sstevel@tonic-gate old_priority, hdlp->ih_type);
8880Sstevel@tonic-gate break;
889916Sschwartz case PSM_INTR_OP_SET_CPU:
8904397Sschwartz case PSM_INTR_OP_GRP_SET_CPU:
891916Sschwartz /*
892916Sschwartz * The interrupt handle given here has been allocated
893916Sschwartz * specifically for this command, and ih_private carries
894916Sschwartz * a CPU value.
895916Sschwartz */
8964397Sschwartz new_cpu = (int)(intptr_t)hdlp->ih_private;
8974397Sschwartz if (!apic_cpu_in_range(new_cpu)) {
8984397Sschwartz DDI_INTR_IMPLDBG((CE_CONT,
8994397Sschwartz "[grp_]set_cpu: cpu out of range: %d\n", new_cpu));
900916Sschwartz *result = EINVAL;
901916Sschwartz return (PSM_FAILURE);
902916Sschwartz }
90310053SEvan.Yan@Sun.COM if (hdlp->ih_vector > APIC_MAX_VECTOR) {
90410053SEvan.Yan@Sun.COM DDI_INTR_IMPLDBG((CE_CONT,
90510053SEvan.Yan@Sun.COM "[grp_]set_cpu: vector out of range: %d\n",
90610053SEvan.Yan@Sun.COM hdlp->ih_vector));
90710053SEvan.Yan@Sun.COM *result = EINVAL;
90810053SEvan.Yan@Sun.COM return (PSM_FAILURE);
90910053SEvan.Yan@Sun.COM }
91012683SJimmy.Vetayases@oracle.com if ((hdlp->ih_flags & PSMGI_INTRBY_FLAGS) == PSMGI_INTRBY_VEC)
91110053SEvan.Yan@Sun.COM hdlp->ih_vector = apic_vector_to_irq[hdlp->ih_vector];
9124397Sschwartz if (intr_op == PSM_INTR_OP_SET_CPU) {
9134397Sschwartz if (apic_set_cpu(hdlp->ih_vector, new_cpu, result) !=
9144397Sschwartz PSM_SUCCESS)
9154397Sschwartz return (PSM_FAILURE);
9164397Sschwartz } else {
9174397Sschwartz if (apic_grp_set_cpu(hdlp->ih_vector, new_cpu,
9184397Sschwartz result) != PSM_SUCCESS)
9194397Sschwartz return (PSM_FAILURE);
920916Sschwartz }
921916Sschwartz break;
922916Sschwartz case PSM_INTR_OP_GET_INTR:
923916Sschwartz /*
924916Sschwartz * The interrupt handle given here has been allocated
925916Sschwartz * specifically for this command, and ih_private carries
926916Sschwartz * a pointer to a apic_get_intr_t.
927916Sschwartz */
928916Sschwartz if (apic_get_vector_intr_info(
929916Sschwartz hdlp->ih_vector, hdlp->ih_private) != PSM_SUCCESS)
930916Sschwartz return (PSM_FAILURE);
931916Sschwartz break;
9324397Sschwartz case PSM_INTR_OP_APIC_TYPE:
93312683SJimmy.Vetayases@oracle.com ((apic_get_type_t *)(hdlp->ih_private))->avgi_type =
93412683SJimmy.Vetayases@oracle.com apic_get_apic_type();
93512683SJimmy.Vetayases@oracle.com ((apic_get_type_t *)(hdlp->ih_private))->avgi_num_intr =
93612683SJimmy.Vetayases@oracle.com APIC_MAX_VECTOR;
93712683SJimmy.Vetayases@oracle.com ((apic_get_type_t *)(hdlp->ih_private))->avgi_num_cpu =
93812683SJimmy.Vetayases@oracle.com boot_ncpus;
9394397Sschwartz hdlp->ih_ver = apic_get_apic_version();
9404397Sschwartz break;
9410Sstevel@tonic-gate case PSM_INTR_OP_SET_CAP:
9420Sstevel@tonic-gate default:
9430Sstevel@tonic-gate return (PSM_FAILURE);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate return (PSM_SUCCESS);
9460Sstevel@tonic-gate }
947