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
|
/*
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/file.h>
#include <sys/hypervisor_api.h>
#include <sys/hsvc.h>
#include <sys/sunndi.h>
#include <fpc.h>
#include <fpc-impl.h>
#include <fpc-impl-4v.h>
#define PCIE_ROOTNEX_COMPATIBLE_NAME "SUNW,sun4v-pci"
#define FPC_MODULE_NAME "fpc"
#define FPC_REQ_MAJOR_VER 1
#define FPC_REQ_MINOR_VER 0
static hsvc_info_t fpc_hsvc = {
HSVC_REV_1,
NULL,
HSVC_GROUP_FIRE_PERF,
FPC_REQ_MAJOR_VER,
FPC_REQ_MINOR_VER,
FPC_MODULE_NAME
};
static int hyp_regd_users = 0;
static uint64_t fpc_sup_minor;
/*
* The following typedef is used to represent a
* 1275 "reg" property of a PCI nexus.
*/
typedef struct nexus_regspec {
uint64_t phys_addr;
uint64_t size;
} nexus_regspec_t;
static uint64_t counter_select_index[] = {
HVIO_FIRE_PERFREG_JBC_SEL,
HVIO_FIRE_PERFREG_PCIE_IMU_SEL,
HVIO_FIRE_PERFREG_PCIE_MMU_SEL,
HVIO_FIRE_PERFREG_PCIE_TLU_SEL,
HVIO_FIRE_PERFREG_PCIE_LNK_SEL
};
/*
* The following event and offset arrays is organized by grouping in major
* order the fire_perfcnt_t register types, and in minor order the register
* numbers within that type.
*/
/*
* This table maps the above order into the hypervisor interface register
* indices.
*/
static uint64_t counter_reg_index[] = {
HVIO_FIRE_PERFREG_JBC_CNT0,
HVIO_FIRE_PERFREG_JBC_CNT1,
HVIO_FIRE_PERFREG_PCIE_IMU_CNT0,
HVIO_FIRE_PERFREG_PCIE_IMU_CNT1,
HVIO_FIRE_PERFREG_PCIE_MMU_CNT0,
HVIO_FIRE_PERFREG_PCIE_MMU_CNT1,
HVIO_FIRE_PERFREG_PCIE_TLU_CNT0,
HVIO_FIRE_PERFREG_PCIE_TLU_CNT1,
HVIO_FIRE_PERFREG_PCIE_TLU_CNT2,
HVIO_FIRE_PERFREG_PCIE_LNK_CNT1,
HVIO_FIRE_PERFREG_PCIE_LNK_CNT2
};
/* Called by _init to determine if it is OK to install driver. */
int
fpc_platform_check()
{
int regstat;
if ((regstat = hsvc_register(&fpc_hsvc, &fpc_sup_minor)) == SUCCESS) {
(void) hsvc_unregister(&fpc_hsvc);
}
fpc_sup_minor = 0;
return (regstat);
}
/* Called during attach to do module-wide initialization. */
/*ARGSUSED*/
int
fpc_platform_module_init(dev_info_t *dip)
{
return (DDI_SUCCESS);
}
int
fpc_platform_node_init(dev_info_t *dip, int *avail)
{
nexus_regspec_t *rp;
uint_t reglen;
devhandle_t dev_hdl;
int regstat;
int index;
boolean_t is_root_pcie_nexus;
uint64_t dummy_data;
char *name = NULL;
boolean_t jbus_regs_avail;
boolean_t pcie_regs_avail;
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "compatible", &name) != DDI_PROP_SUCCESS)
return (DDI_SUCCESS);
is_root_pcie_nexus = (strcmp(name, PCIE_ROOTNEX_COMPATIBLE_NAME) == 0);
ddi_prop_free(name);
if (!is_root_pcie_nexus)
return (DDI_SUCCESS);
if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "reg", (uchar_t **)&rp, ®len) !=
DDI_PROP_SUCCESS)
return (DDI_FAILURE);
/*
* Initilize device handle. The device handle uniquely
* identifies a SUN4V device. It consists of the lower 28-bits
* of the hi-cell of the first entry of the SUN4V device's
* "reg" property as defined by the SUN4V Bus Binding to Open
* Firmware.
*/
dev_hdl = (devhandle_t)((rp->phys_addr >> 32) & DEVHDLE_MASK);
ddi_prop_free(rp);
/*
* If this is the first time through here, negotiate with hypervisor
* that it has the services needed to operate. Don't do this in _init
* since we may want to modload the driver without attaching,
* for debugging purposes.
*
* Note that this is another way of weeding out unsupported platforms
*/
if (hyp_regd_users == 0) {
regstat = hsvc_register(&fpc_hsvc, &fpc_sup_minor);
if (regstat != SUCCESS) {
/*
* Fail silently since we don't want to print an error
* on future platforms which don't support this driver.
*/
return (DDI_FAILURE);
}
}
hyp_regd_users++;
/* See which register sets are usable from this node. */
jbus_regs_avail = (fpc_event_io(
(fire_perfreg_handle_t)dev_hdl, jbc, &dummy_data, IS_READ) ==
SUCCESS);
pcie_regs_avail = (fpc_event_io(
(fire_perfreg_handle_t)dev_hdl, imu, &dummy_data, IS_READ) ==
SUCCESS);
/* Nothing usable at this node. */
if ((!jbus_regs_avail) && (!pcie_regs_avail))
return (DDI_SUCCESS);
fpc_common_node_setup(dip, &index);
if (pcie_regs_avail)
*avail |=
((index == 0) ? PCIE_A_REGS_AVAIL : PCIE_B_REGS_AVAIL);
if (jbus_regs_avail) {
*avail |= JBUS_REGS_AVAIL;
if (index != 0)
cmn_err(CE_WARN,
"fpc: JBUS regs available on device idx %d!\n",
index);
}
(void) fpc_set_platform_data_by_number(index, (void *)dev_hdl);
return (DDI_SUCCESS);
}
/*ARGSUSED*/
void
fpc_platform_node_fini(void *arg)
{
if (--hyp_regd_users == 0)
(void) hsvc_unregister(&fpc_hsvc);
}
/*ARGSUSED*/
void
fpc_platform_module_fini(dev_info_t *dip)
{
}
fire_perfreg_handle_t
fpc_get_perfreg_handle(int devnum)
{
void *platform_specific_data;
if ((platform_specific_data =
fpc_get_platform_data_by_number(devnum)) == NULL)
return ((fire_perfreg_handle_t)-1);
else
return ((fire_perfreg_handle_t)platform_specific_data);
}
/*ARGSUSED*/
int
fpc_free_counter_handle(fire_perfreg_handle_t handle)
{
return (SUCCESS);
}
static int
fpc_hv_perfreg_io(fire_perfreg_handle_t handle, uint64_t hv_if_index,
uint64_t *reg_data, boolean_t is_write)
{
int rval;
devhandle_t dev_hdl = (devhandle_t)handle;
if (is_write)
rval = fpc_set_fire_perfreg(dev_hdl, hv_if_index, *reg_data);
else
rval = fpc_get_fire_perfreg(dev_hdl, hv_if_index, reg_data);
return ((rval == H_EOK) ? SUCCESS : EIO);
}
int
fpc_event_io(fire_perfreg_handle_t handle, fire_perfcnt_t group,
uint64_t *reg_data, boolean_t is_write)
{
uint64_t hv_if_index = counter_select_index[group];
return (fpc_hv_perfreg_io(handle, hv_if_index, reg_data, is_write));
}
/*ARGSUSED*/
int
fpc_counter_io(fire_perfreg_handle_t handle, fire_perfcnt_t group,
int counter_index, uint64_t *reg_data, boolean_t is_write)
{
uint64_t hv_if_index = counter_reg_index[counter_index];
return (fpc_hv_perfreg_io(handle, hv_if_index, reg_data, is_write));
}
|