summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/sunfire/io/simmstat.c
blob: 2fe1d3c6c71f0fcebc4786449a8a8a2d323768ed (plain)
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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
 */


#include <sys/types.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/obpdefs.h>
#include <sys/cmn_err.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <sys/ivintr.h>
#include <sys/intr.h>
#include <sys/intreg.h>
#include <sys/autoconf.h>
#include <sys/modctl.h>
#include <sys/spl.h>

#include <sys/fhc.h>
#include <sys/simmstat.h>

/* Useful debugging Stuff */
#include <sys/nexusdebug.h>

/*
 * Function prototypes
 */

static int simmstat_attach(dev_info_t *, ddi_attach_cmd_t);

static int simmstat_detach(dev_info_t *, ddi_detach_cmd_t);

static void simmstat_add_kstats(struct simmstat_soft_state *);

static int simmstat_kstat_update(kstat_t *, int);

/*
 * Configuration data structures
 */
static struct cb_ops simmstat_cb_ops = {
	nulldev,			/* open */
	nulldev,			/* close */
	nulldev,			/* strategy */
	nulldev,			/* print */
	nodev,				/* dump */
	nulldev,			/* read */
	nulldev,			/* write */
	nulldev,			/* ioctl */
	nodev,				/* devmap */
	nodev,				/* mmap */
	nodev,				/* segmap */
	nochpoll,			/* poll */
	ddi_prop_op,			/* cb_prop_op */
	0,				/* streamtab */
	D_MP | D_NEW | D_HOTPLUG,	/* Driver compatibility flag */
	CB_REV,				/* rev */
	nodev,				/* cb_aread */
	nodev				/* cb_awrite */
};

static struct dev_ops simmstat_ops = {
	DEVO_REV,			/* rev */
	0,				/* refcnt  */
	ddi_no_info,			/* getinfo */
	nulldev,			/* identify */
	nulldev,			/* probe */
	simmstat_attach,		/* attach */
	simmstat_detach,		/* detach */
	nulldev,			/* reset */
	&simmstat_cb_ops,		/* cb_ops */
	(struct bus_ops *)0,		/* bus_ops */
	nulldev,			/* power */
	ddi_quiesce_not_needed,			/* quiesce */
};

static uint_t simmstat_reg_read_delay_us = 10;

/*
 * Driver globals
 */
void *simmstatp;

extern struct mod_ops mod_driverops;

static struct modldrv modldrv = {
	&mod_driverops,			/* module type, this one is a driver */
	"SIMM-status Leaf",		/* module name */
	&simmstat_ops,			/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1,		/* rev */
	(void *)&modldrv,
	NULL
};

/*
 * These are the module initialization routines.
 */

int
_init(void)
{
	int error;

	if ((error = ddi_soft_state_init(&simmstatp,
	    sizeof (struct simmstat_soft_state), 1)) != 0)
		return (error);

	return (mod_install(&modlinkage));
}

int
_fini(void)
{
	int error;

	if ((error = mod_remove(&modlinkage)) != 0)
		return (error);

	ddi_soft_state_fini(&simmstatp);
	return (0);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}

static int
simmstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	struct simmstat_soft_state *softsp;
	int instance;

	switch (cmd) {
	case DDI_ATTACH:
		break;

	case DDI_RESUME:
		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(devi);

	if (ddi_soft_state_zalloc(simmstatp, instance) != DDI_SUCCESS)
		return (DDI_FAILURE);

	softsp = ddi_get_soft_state(simmstatp, instance);

	/* Set the dip in the soft state */
	softsp->dip = devi;

	/* Get the board number from this nodes parent device. */
	softsp->pdip = ddi_get_parent(softsp->dip);
	if ((softsp->board = (int)ddi_getprop(DDI_DEV_T_ANY, softsp->pdip,
	    DDI_PROP_DONTPASS, OBP_BOARDNUM, -1)) == -1) {
		cmn_err(CE_WARN, "simmstat%d: unable to retrieve %s property",
		    instance, OBP_BOARDNUM);
		goto bad;
	}

	DPRINTF(SIMMSTAT_ATTACH_DEBUG, ("simmstat%d: devi= 0x%p\n, "
	    " softsp=0x%p\n", instance, (void *)devi, (void *)softsp));

	/* map in the registers for this device. */
	if (ddi_map_regs(softsp->dip, 0,
	    (caddr_t *)&softsp->simmstat_base, 0, 0)) {
		cmn_err(CE_WARN, "simmstat%d: unable to map registers",
		    instance);
		goto bad;
	}

	/* nothing to suspend/resume here */
	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
	    "pm-hardware-state", "no-suspend-resume");

	/* create the kstats for this device */
	simmstat_add_kstats(softsp);

	ddi_report_dev(devi);

	return (DDI_SUCCESS);

bad:
	ddi_soft_state_free(simmstatp, instance);
	return (DDI_FAILURE);
}

/* ARGSUSED */
static int
simmstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
	int instance;
	struct simmstat_soft_state *softsp;

	/* get the instance of this devi */
	instance = ddi_get_instance(devi);

	/* get the soft state pointer for this device node */
	softsp = ddi_get_soft_state(simmstatp, instance);

	switch (cmd) {
	case DDI_SUSPEND:
		return (DDI_SUCCESS);

	case DDI_DETACH:
		(void) fhc_bdlist_lock(softsp->board);
		if (fhc_bd_detachable(softsp->board))
			break;
		else
			fhc_bdlist_unlock();
		/* FALLTHROUGH */

	default:
		return (DDI_FAILURE);
	}

	fhc_bdlist_unlock();

	/* remove the kstat for this board */
	kstat_delete(softsp->simmstat_ksp);

	/* unmap the registers */
	ddi_unmap_regs(softsp->dip, 0,
	    (caddr_t *)&softsp->simmstat_base, 0, 0);

	/* free up the soft state */
	ddi_soft_state_free(simmstatp, instance);
	ddi_prop_remove_all(devi);

	return (DDI_SUCCESS);
}

static void
simmstat_add_kstats(struct simmstat_soft_state *softsp)
{
	struct kstat *simmstat_ksp;

	if ((simmstat_ksp = kstat_create("unix", softsp->board,
	    SIMMSTAT_KSTAT_NAME, "misc", KSTAT_TYPE_RAW,
	    SIMM_COUNT, KSTAT_FLAG_PERSISTENT)) == NULL) {
		cmn_err(CE_WARN, "simmstat%d: kstat_create failed",
		    ddi_get_instance(softsp->dip));
	}

	simmstat_ksp->ks_update = simmstat_kstat_update;
	simmstat_ksp->ks_private = (void *)softsp;
	softsp->simmstat_ksp = simmstat_ksp;
	kstat_install(simmstat_ksp);
}

/*
 * Kstats only need ks_update functions when they change dynamically
 * at run time.
 * In the case of the simmstat registers, they contain battery
 * information for NVSIMMs. These need to be updated whenever a
 * kstat_read asks for the data. There is currently no plan to
 * ship NVSIMMs on this platform, but this support must be present.
 */

static int
simmstat_kstat_update(kstat_t *ksp, int rw)
{
	struct simmstat_soft_state *softsp;
	volatile char *statp;	/* pointer to hardware register */
	char *kstatp;		/* pointer to kstat data buffer */
	int i;

	kstatp = (char *)ksp->ks_data;
	softsp = (struct simmstat_soft_state *)ksp->ks_private;

	statp = (char *)softsp->simmstat_base;

	/* this is a read-only kstat. Bail out on a write */
	if (rw == KSTAT_WRITE) {
		return (EACCES);
	} else {

		/*
		 * copy current status of hardware into the kstat
		 * structure.
		 */
		for (i = 0; i < SIMM_COUNT; i++, statp++, kstatp++) {
			*kstatp = *statp;
			DELAY(simmstat_reg_read_delay_us);
		}
	}
	return (0);
}