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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* tod driver module for Serengeti
* This module implements a soft tod since
* Serengeti has no tod part.
*/
#include <sys/modctl.h>
#include <sys/systm.h>
#include <sys/cpuvar.h>
#include <sys/promif.h>
#include <sys/sgsbbc_iosram.h>
#include <sys/todsg.h>
#include <sys/cmn_err.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include <sys/clock.h>
#if defined(DEBUG) || defined(lint)
static int todsg_debug = 0;
#define DCMNERR if (todsg_debug) cmn_err
#else
#define DCMNERR
#endif /* DEBUG */
#define OFFSET(base, field) ((char *)&base.field - (char *)&base)
#define SC_DOWN_COUNT_THRESHOLD 2
#define SC_TOD_MIN_REV 2
static timestruc_t todsg_get(void);
static void todsg_set(timestruc_t);
static uint32_t todsg_set_watchdog_timer(uint_t);
static uint32_t todsg_clear_watchdog_timer(void);
static void todsg_set_power_alarm(timestruc_t);
static void todsg_clear_power_alarm(void);
static uint64_t todsg_get_cpufrequency(void);
static int update_heartbeat(void);
static int verify_sc_tod_version(void);
static int update_tod_skew(time_t skew);
static uint32_t i_am_alive = 0;
static uint32_t sc_tod_version = 0;
static time_t skew_adjust = 0;
static int is_sc_down = 0;
static int adjust_sc_down = 0;
/*
* Module linkage information for the kernel.
*/
static struct modlmisc modlmisc = {
&mod_miscops, "Serengeti tod module"
};
static struct modlinkage modlinkage = {
MODREV_1, (void *)&modlmisc, NULL
};
int
_init(void)
{
DCMNERR(CE_NOTE, "todsg:_init(): begins");
if (strcmp(tod_module_name, "todsg") == 0) {
time_t ssc_time = (time_t)0;
char obp_string[80];
/*
* To obtain the initial start of day time, we use an
* OBP callback; this is because the iosram is not yet
* accessible from the OS at this early stage of startup.
*/
/*
* Set the string to pass to OBP
*/
(void) sprintf(obp_string,
"h# %p \" unix-get-tod\" $find if execute else 3drop then",
(void *)&ssc_time);
prom_interpret(obp_string, 0, 0, 0, 0, 0);
if (ssc_time == (time_t)0) {
cmn_err(CE_WARN, "Initial date is invalid. "
"This can be caused by older firmware.");
cmn_err(CE_CONT, "Please flashupdate the System "
"Controller firmware to the latest version.\n");
cmn_err(CE_CONT, "Attempting to set the date and time "
"based on the last shutdown.\n");
cmn_err(CE_CONT, "Please inspect the date and time and "
"correct if necessary.\n");
}
hrestime.tv_sec = ssc_time;
DCMNERR(CE_NOTE, "todsg: _init(): time from OBP 0x%lX",
ssc_time);
/*
* Verify whether the received date/clock has overflowed
* an integer(32bit), so that we capture any corrupted
* date from SC, thereby preventing boot failure.
*/
if (TIMESPEC_OVERFLOW(&hrestime)) {
cmn_err(CE_WARN, "Date overflow detected.");
cmn_err(CE_CONT, "Attempting to set the date and time "
"based on the last shutdown.\n");
cmn_err(CE_CONT, "Please inspect the date and time and "
"correct if necessary.\n");
/*
* By setting hrestime.tv_sec to zero
* we force the vfs_mountroot() to set
* the date from the last shutdown.
*/
hrestime.tv_sec = (time_t)0;
/*
* Save the skew so that we can update
* IOSRAM when it becomes accessible.
*/
skew_adjust = -ssc_time;
}
DCMNERR(CE_NOTE, "todsg:_init(): set tod_ops");
tod_ops.tod_get = todsg_get;
tod_ops.tod_set = todsg_set;
tod_ops.tod_set_watchdog_timer = todsg_set_watchdog_timer;
tod_ops.tod_clear_watchdog_timer = todsg_clear_watchdog_timer;
tod_ops.tod_set_power_alarm = todsg_set_power_alarm;
tod_ops.tod_clear_power_alarm = todsg_clear_power_alarm;
tod_ops.tod_get_cpufrequency = todsg_get_cpufrequency;
}
return (mod_install(&modlinkage));
}
int
_fini(void)
{
if (strcmp(tod_module_name, "todsg") == 0)
return (EBUSY);
else
return (mod_remove(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
update_heartbeat(void)
{
tod_iosram_t tod_buf;
int complained = 0;
/* Update the heartbeat */
if (i_am_alive == UINT32_MAX)
i_am_alive = 0;
else
i_am_alive++;
if (iosram_write(SBBC_TOD_KEY, OFFSET(tod_buf, tod_i_am_alive),
(char *)&i_am_alive, sizeof (uint32_t))) {
complained++;
cmn_err(CE_WARN, "update_heartbeat(): write heartbeat failed");
}
return (complained);
}
static int
verify_sc_tod_version(void)
{
uint32_t magic;
tod_iosram_t tod_buf;
if (!todsg_use_sc)
return (FALSE);
/*
* read tod_version only when the first time and
* when there has been a previous sc down time
*/
if (!sc_tod_version || is_sc_down >= SC_DOWN_COUNT_THRESHOLD) {
if (iosram_read(SBBC_TOD_KEY, OFFSET(tod_buf, tod_magic),
(char *)&magic, sizeof (uint32_t)) ||
magic != TODSG_MAGIC) {
cmn_err(CE_WARN, "get_sc_tod_version(): "
"TOD SRAM magic error");
return (FALSE);
}
if (iosram_read(SBBC_TOD_KEY, OFFSET(tod_buf, tod_version),
(char *)&sc_tod_version, sizeof (uint32_t))) {
cmn_err(CE_WARN, "get_sc_tod_version(): "
"read tod version failed");
sc_tod_version = 0;
return (FALSE);
}
}
if (sc_tod_version >= SC_TOD_MIN_REV) {
return (TRUE);
} else {
todsg_use_sc = 0;
cmn_err(CE_WARN, "todsg_get(): incorrect firmware version, "
"(%d): expected version >= %d.", sc_tod_version,
SC_TOD_MIN_REV);
}
return (FALSE);
}
static int
update_tod_skew(time_t skew)
{
time_t domain_skew;
tod_iosram_t tod_buf;
int complained = 0;
DCMNERR(CE_NOTE, "update_tod_skew(): skew 0x%lX", skew);
if (iosram_read(SBBC_TOD_KEY, OFFSET(tod_buf, tod_domain_skew),
(char *)&domain_skew, sizeof (time_t))) {
complained++;
cmn_err(CE_WARN,
"update_tod_skew(): read tod domain skew failed");
}
domain_skew += skew;
/* we shall update the skew_adjust too now */
domain_skew += skew_adjust;
if (!complained && iosram_write(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_domain_skew), (char *)&domain_skew,
sizeof (time_t))) {
complained++;
cmn_err(CE_WARN,
"update_tod_skew(): write domain skew failed");
}
if (!complained)
skew_adjust = 0;
return (complained);
}
/*
* Return time value read from IOSRAM.
* Must be called with tod_lock held.
*/
static timestruc_t
todsg_get(void)
{
tod_iosram_t tod_buf;
time_t seconds;
time_t domain_skew;
int complained = 0;
static time_t pre_seconds = (time_t)0;
ASSERT(MUTEX_HELD(&tod_lock));
if (!verify_sc_tod_version()) {
/* if we can't use SC */
goto return_hrestime;
}
if (watchdog_activated != 0 || watchdog_enable != 0)
complained = update_heartbeat();
if (!complained && (iosram_read(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_get_value), (char *)&seconds,
sizeof (time_t)))) {
complained++;
cmn_err(CE_WARN, "todsg_get(): read 64-bit tod value failed");
}
if (!complained && skew_adjust) {
/*
* This is our first chance to update IOSRAM
* with local copy of the skew, so update it.
*/
complained = update_tod_skew(0);
}
if (!complained && iosram_read(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_domain_skew), (char *)&domain_skew,
sizeof (time_t))) {
complained++;
cmn_err(CE_WARN, "todsg_get(): read tod domain skew failed");
}
if (complained) {
cmn_err(CE_WARN, "todsg_get(): turned off using tod");
todsg_use_sc = 0;
goto return_hrestime;
}
/*
* If the SC gets rebooted, and we are using NTP, then we need
* to sync the IOSRAM to hrestime when the SC comes back. We
* can determine that either NTP slew (or date -a) was called if
* the global timedelta was non-zero at any point while the SC
* was away. If timedelta remains zero throughout, then the
* default action will be to sync hrestime to IOSRAM
*/
if (seconds != pre_seconds) { /* SC still alive */
pre_seconds = seconds;
if (is_sc_down >= SC_DOWN_COUNT_THRESHOLD && adjust_sc_down) {
skew_adjust = hrestime.tv_sec - (seconds + domain_skew);
complained = update_tod_skew(0);
if (!complained && (iosram_read(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_domain_skew),
(char *)&domain_skew, sizeof (time_t)))) {
complained++;
cmn_err(CE_WARN, "todsg_get(): "
"read tod domain skew failed");
}
}
is_sc_down = 0;
adjust_sc_down = 0;
/*
* If complained then domain_skew is invalid.
* Hand back hrestime instead.
*/
if (!complained) {
/*
* The read was successful so ensure the failure
* flag is clear.
*/
tod_status_clear(TOD_GET_FAILED);
timestruc_t ts = {0, 0};
ts.tv_sec = seconds + domain_skew;
return (ts);
} else {
goto return_hrestime;
}
}
/* SC/TOD is down */
is_sc_down++;
if (timedelta != 0) {
adjust_sc_down = 1;
}
return_hrestime:
/*
* We need to inform the tod_validate() code to stop checking until
* the SC comes back up again. Note we will return hrestime below
* which may be different to the previous TOD value we returned.
*/
tod_status_set(TOD_GET_FAILED);
return (hrestime);
}
static void
todsg_set(timestruc_t ts)
{
int complained = 0;
tod_iosram_t tod_buf;
time_t domain_skew;
time_t seconds;
time_t hwtod;
ASSERT(MUTEX_HELD(&tod_lock));
if (!verify_sc_tod_version()) {
/* if we can't use SC */
return;
}
/*
* If the SC is down just note the fact that we should
* have adjusted the hardware skew which caters for calls
* to stime(). (eg NTP step, as opposed to NTP skew)
*/
if (is_sc_down) {
adjust_sc_down = 1;
return;
}
/*
* reason to update i_am_alive here:
* To work around a generic Solaris bug that can
* cause tod_get() to be starved by too frequent
* calls to the stime() system call.
*/
if (watchdog_activated != 0 || watchdog_enable != 0)
complained = update_heartbeat();
/*
* We are passed hrestime from clock.c so we need to read the
* IOSRAM for the hardware's idea of the time to see if we need
* to update the skew.
*/
if (!complained && (iosram_read(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_get_value), (char *)&seconds,
sizeof (time_t)))) {
complained++;
cmn_err(CE_WARN, "todsg_set(): read 64-bit tod value failed");
}
if (!complained && iosram_read(SBBC_TOD_KEY,
OFFSET(tod_buf, tod_domain_skew), (char *)&domain_skew,
sizeof (time_t))) {
complained++;
cmn_err(CE_WARN, "todsg_set(): read tod domain skew failed");
}
/*
* Only update the skew if the time passed differs from
* what the hardware thinks & no errors talking to SC
*/
if (!complained && (ts.tv_sec != (seconds + domain_skew))) {
hwtod = seconds + domain_skew;
complained = update_tod_skew(ts.tv_sec - hwtod);
DCMNERR(CE_NOTE, "todsg_set(): set time %lX (%lX)%s",
ts.tv_sec, hwtod, complained ? " failed" : "");
}
if (complained) {
cmn_err(CE_WARN, "todsg_set(): turned off using tod");
todsg_use_sc = 0;
}
}
static uint32_t
todsg_set_watchdog_timer(uint32_t timeoutval)
{
tod_iosram_t tod_buf;
ASSERT(MUTEX_HELD(&tod_lock));
if (!verify_sc_tod_version()) {
DCMNERR(CE_NOTE, "todsg_set_watchdog_timer(): "
"verify_sc_tod_version failed");
return (0);
}
DCMNERR(CE_NOTE, "todsg_set_watchdog_timer(): "
"set watchdog timer value = %d", timeoutval);
if (iosram_write(SBBC_TOD_KEY, OFFSET(tod_buf, tod_timeout_period),
(char *)&timeoutval, sizeof (uint32_t))) {
DCMNERR(CE_NOTE, "todsg_set_watchdog_timer(): "
"write new timeout value failed");
return (0);
}
watchdog_activated = 1;
return (timeoutval);
}
static uint32_t
todsg_clear_watchdog_timer(void)
{
tod_iosram_t tod_buf;
uint32_t r_timeout_period;
uint32_t w_timeout_period;
ASSERT(MUTEX_HELD(&tod_lock));
if ((watchdog_activated == 0) || !verify_sc_tod_version()) {
DCMNERR(CE_NOTE, "todsg_set_watchdog_timer(): "
"either watchdog not activated or "
"verify_sc_tod_version failed");
return (0);
}
if (iosram_read(SBBC_TOD_KEY, OFFSET(tod_buf, tod_timeout_period),
(char *)&r_timeout_period, sizeof (uint32_t))) {
DCMNERR(CE_NOTE, "todsg_clear_watchdog_timer(): "
"read timeout value failed");
return (0);
}
DCMNERR(CE_NOTE, "todsg_clear_watchdog_timer(): "
"clear watchdog timer (old value=%d)", r_timeout_period);
w_timeout_period = 0;
if (iosram_write(SBBC_TOD_KEY, OFFSET(tod_buf, tod_timeout_period),
(char *)&w_timeout_period, sizeof (uint32_t))) {
DCMNERR(CE_NOTE, "todsg_clear_watchdog_timer(): "
"write zero timeout value failed");
return (0);
}
watchdog_activated = 0;
return (r_timeout_period);
}
/*
* Null function.
*/
/* ARGSUSED */
static void
todsg_set_power_alarm(timestruc_t ts)
{
ASSERT(MUTEX_HELD(&tod_lock));
}
/*
* Null function
*/
static void
todsg_clear_power_alarm()
{
ASSERT(MUTEX_HELD(&tod_lock));
}
/*
* Get clock freq from the cpunode
*/
uint64_t
todsg_get_cpufrequency(void)
{
DCMNERR(CE_NOTE, "todsg_get_cpufrequency(): frequency=%ldMHz",
cpunodes[CPU->cpu_id].clock_freq/1000000);
return (cpunodes[CPU->cpu_id].clock_freq);
}
|