1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <sys/mutex.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/clock.h>
#include <sys/machlock.h>
#include <sys/smp_impldefs.h>
#include <sys/uadmin.h>
#include <sys/promif.h>
#include <sys/psm.h>
#include <sys/psm_common.h>
#include <sys/atomic.h>
#include <sys/apic.h>
#include <sys/archsystm.h>
#include <sys/mach_intr.h>
#include <sys/modctl.h>
#include <sys/sysmacros.h>
#include <sys/pci_intr_lib.h>
/* Multiple vector support for MSI */
int apic_multi_msi_enable = 1;
/* Multiple vector support for MSI-X */
int apic_msix_enable = 1;
/*
* check whether the system supports MSI
*
* If PCI-E capability is found, then this must be a PCI-E system.
* Since MSI is required for PCI-E system, it returns PSM_SUCCESS
* to indicate this system supports MSI.
*/
int
apic_check_msi_support()
{
dev_info_t *cdip;
char dev_type[16];
int dev_len;
DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support:\n"));
/*
* check whether the first level children of root_node have
* PCI-E capability
*/
for (cdip = ddi_get_child(ddi_root_node()); cdip != NULL;
cdip = ddi_get_next_sibling(cdip)) {
DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: cdip: 0x%p,"
" driver: %s, binding: %s, nodename: %s\n", (void *)cdip,
ddi_driver_name(cdip), ddi_binding_name(cdip),
ddi_node_name(cdip)));
dev_len = sizeof (dev_type);
if (ddi_getlongprop_buf(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
"device_type", (caddr_t)dev_type, &dev_len)
!= DDI_PROP_SUCCESS)
continue;
if (strcmp(dev_type, "pciex") == 0)
return (PSM_SUCCESS);
}
/* MSI is not supported on this system */
DDI_INTR_IMPLDBG((CE_CONT, "apic_check_msi_support: no 'pciex' "
"device_type found\n"));
return (PSM_FAILURE);
}
/*
* It finds the apic_irq_t associates with the dip, ispec and type.
*/
apic_irq_t *
apic_find_irq(dev_info_t *dip, struct intrspec *ispec, int type)
{
apic_irq_t *irqp;
int i;
DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: dip=0x%p vec=0x%x "
"ipl=0x%x type=0x%x\n", (void *)dip, ispec->intrspec_vec,
ispec->intrspec_pri, type));
for (i = apic_min_device_irq; i <= apic_max_device_irq; i++) {
for (irqp = apic_irq_table[i]; irqp; irqp = irqp->airq_next) {
if ((irqp->airq_dip == dip) &&
(irqp->airq_origirq == ispec->intrspec_vec) &&
(irqp->airq_ipl == ispec->intrspec_pri)) {
if (type == DDI_INTR_TYPE_MSI) {
if (irqp->airq_mps_intr_index ==
MSI_INDEX)
return (irqp);
} else if (type == DDI_INTR_TYPE_MSIX) {
if (irqp->airq_mps_intr_index ==
MSIX_INDEX)
return (irqp);
} else
return (irqp);
}
}
}
DDI_INTR_IMPLDBG((CE_CONT, "apic_find_irq: return NULL\n"));
return (NULL);
}
int
apic_get_vector_intr_info(int vecirq, apic_get_intr_t *intr_params_p)
{
struct autovec *av_dev;
uchar_t irqno;
int i;
apic_irq_t *irq_p;
/* Sanity check the vector/irq argument. */
ASSERT((vecirq >= 0) || (vecirq <= APIC_MAX_VECTOR));
mutex_enter(&airq_mutex);
/*
* Convert the vecirq arg to an irq using vector_to_irq table
* if the arg is a vector. Pass thru if already an irq.
*/
if ((intr_params_p->avgi_req_flags & PSMGI_INTRBY_FLAGS) ==
PSMGI_INTRBY_VEC)
irqno = apic_vector_to_irq[vecirq];
else
irqno = vecirq;
irq_p = apic_irq_table[irqno];
if ((irq_p == NULL) ||
((irq_p->airq_mps_intr_index != RESERVE_INDEX) &&
((irq_p->airq_temp_cpu == IRQ_UNBOUND) ||
(irq_p->airq_temp_cpu == IRQ_UNINIT)))) {
mutex_exit(&airq_mutex);
return (PSM_FAILURE);
}
if (intr_params_p->avgi_req_flags & PSMGI_REQ_CPUID) {
/* Get the (temp) cpu from apic_irq table, indexed by irq. */
intr_params_p->avgi_cpu_id = irq_p->airq_temp_cpu;
/* Return user bound info for intrd. */
if (intr_params_p->avgi_cpu_id & IRQ_USER_BOUND) {
intr_params_p->avgi_cpu_id &= ~IRQ_USER_BOUND;
intr_params_p->avgi_cpu_id |= PSMGI_CPU_USER_BOUND;
}
}
if (intr_params_p->avgi_req_flags & PSMGI_REQ_VECTOR)
intr_params_p->avgi_vector = irq_p->airq_vector;
if (intr_params_p->avgi_req_flags &
(PSMGI_REQ_NUM_DEVS | PSMGI_REQ_GET_DEVS))
/* Get number of devices from apic_irq table shared field. */
intr_params_p->avgi_num_devs = irq_p->airq_share;
if (intr_params_p->avgi_req_flags & PSMGI_REQ_GET_DEVS) {
intr_params_p->avgi_req_flags |= PSMGI_REQ_NUM_DEVS;
/* Some devices have NULL dip. Don't count these. */
if (intr_params_p->avgi_num_devs > 0) {
for (i = 0, av_dev = autovect[irqno].avh_link;
av_dev; av_dev = av_dev->av_link)
if (av_dev->av_vector && av_dev->av_dip)
i++;
intr_params_p->avgi_num_devs =
MIN(intr_params_p->avgi_num_devs, i);
}
/* There are no viable dips to return. */
if (intr_params_p->avgi_num_devs == 0)
intr_params_p->avgi_dip_list = NULL;
else { /* Return list of dips */
/* Allocate space in array for that number of devs. */
intr_params_p->avgi_dip_list = kmem_zalloc(
intr_params_p->avgi_num_devs *
sizeof (dev_info_t *),
KM_SLEEP);
/*
* Loop through the device list of the autovec table
* filling in the dip array.
*
* Note that the autovect table may have some special
* entries which contain NULL dips. These will be
* ignored.
*/
for (i = 0, av_dev = autovect[irqno].avh_link;
av_dev; av_dev = av_dev->av_link)
if (av_dev->av_vector && av_dev->av_dip)
intr_params_p->avgi_dip_list[i++] =
av_dev->av_dip;
}
}
mutex_exit(&airq_mutex);
return (PSM_SUCCESS);
}
/*
* apic_pci_msi_enable_vector:
* Set the address/data fields in the MSI/X capability structure
* XXX: MSI-X support
*/
/* ARGSUSED */
void
apic_pci_msi_enable_vector(apic_irq_t *irq_ptr, int type, int inum, int vector,
int count, int target_apic_id)
{
uint64_t msi_addr, msi_data;
ushort_t msi_ctrl;
dev_info_t *dip = irq_ptr->airq_dip;
int cap_ptr = i_ddi_get_msi_msix_cap_ptr(dip);
ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(dip);
DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: dip=0x%p\n"
"\tdriver = %s, inum=0x%x vector=0x%x apicid=0x%x\n", (void *)dip,
ddi_driver_name(dip), inum, vector, target_apic_id));
ASSERT((handle != NULL) && (cap_ptr != 0));
/* MSI Address */
msi_addr = (MSI_ADDR_HDR |
(target_apic_id << MSI_ADDR_DEST_SHIFT));
msi_addr |= ((MSI_ADDR_RH_FIXED << MSI_ADDR_RH_SHIFT) |
(MSI_ADDR_DM_PHYSICAL << MSI_ADDR_DM_SHIFT));
/* MSI Data: MSI is edge triggered according to spec */
msi_data = ((MSI_DATA_TM_EDGE << MSI_DATA_TM_SHIFT) | vector);
DDI_INTR_IMPLDBG((CE_CONT, "apic_pci_msi_enable_vector: addr=0x%lx "
"data=0x%lx\n", (long)msi_addr, (long)msi_data));
if (type == DDI_INTR_TYPE_MSI) {
msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
/* Set the bits to inform how many MSIs are enabled */
msi_ctrl |= ((highbit(count) -1) << PCI_MSI_MME_SHIFT);
pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
}
}
/*
* apic_pci_msi_disable_mode:
*/
void
apic_pci_msi_disable_mode(dev_info_t *rdip, int type)
{
ushort_t msi_ctrl;
int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip);
ASSERT((handle != NULL) && (cap_ptr != 0));
if (type == DDI_INTR_TYPE_MSI) {
msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
if (!(msi_ctrl & PCI_MSI_ENABLE_BIT))
return;
msi_ctrl &= ~PCI_MSI_ENABLE_BIT; /* MSI disable */
pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
} else if (type == DDI_INTR_TYPE_MSIX) {
msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL);
if (msi_ctrl & PCI_MSIX_ENABLE_BIT) {
msi_ctrl &= ~PCI_MSIX_ENABLE_BIT;
pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL,
msi_ctrl);
}
}
}
/*
* apic_pci_msi_enable_mode:
*/
void
apic_pci_msi_enable_mode(dev_info_t *rdip, int type, int inum)
{
ushort_t msi_ctrl;
int cap_ptr = i_ddi_get_msi_msix_cap_ptr(rdip);
ddi_acc_handle_t handle = i_ddi_get_pci_config_handle(rdip);
ASSERT((handle != NULL) && (cap_ptr != 0));
if (type == DDI_INTR_TYPE_MSI) {
msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSI_CTRL);
if ((msi_ctrl & PCI_MSI_ENABLE_BIT))
return;
msi_ctrl |= PCI_MSI_ENABLE_BIT;
pci_config_put16(handle, cap_ptr + PCI_MSI_CTRL, msi_ctrl);
} else if (type == DDI_INTR_TYPE_MSIX) {
uintptr_t off;
uint32_t mask;
ddi_intr_msix_t *msix_p;
msix_p = i_ddi_get_msix(rdip);
ASSERT(msix_p != NULL);
/* Offset into "inum"th entry in the MSI-X table & clear mask */
off = (uintptr_t)msix_p->msix_tbl_addr + (inum *
PCI_MSIX_VECTOR_SIZE) + PCI_MSIX_VECTOR_CTRL_OFFSET;
mask = ddi_get32(msix_p->msix_tbl_hdl, (uint32_t *)off);
ddi_put32(msix_p->msix_tbl_hdl, (uint32_t *)off, (mask & ~1));
msi_ctrl = pci_config_get16(handle, cap_ptr + PCI_MSIX_CTRL);
if (!(msi_ctrl & PCI_MSIX_ENABLE_BIT)) {
msi_ctrl |= PCI_MSIX_ENABLE_BIT;
pci_config_put16(handle, cap_ptr + PCI_MSIX_CTRL,
msi_ctrl);
}
}
}
/*
* We let the hypervisor deal with msi configutation
* so just stub this out.
*/
/* ARGSUSED */
void
apic_pci_msi_unconfigure(dev_info_t *rdip, int type, int inum)
{
}
|