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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/* Copyright 2013-2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <skiboot.h>
#include <fsp.h>
#include <lock.h>
#include <timebase.h>
#include <time.h>
#include <time-utils.h>
#include <opal-msg.h>
#include <fsp-elog.h>
#include <device.h>
//#define DBG(fmt...) printf("RTC: " fmt)
#define DBG(fmt...) do { } while(0)
/*
* Note on how those operate:
*
* Because the RTC calls can be pretty slow, these functions will shoot
* an asynchronous request to the FSP (if none is already pending)
*
* The requests will return OPAL_BUSY_EVENT as long as the event has
* not been completed.
*
* WARNING: An attempt at doing an RTC write while one is already pending
* will simply ignore the new arguments and continue returning
* OPAL_BUSY_EVENT. This is to be compatible with existing Linux code.
*
* Completion of the request will result in an event OPAL_EVENT_RTC
* being signaled, which will remain raised until a corresponding call
* to opal_rtc_read() or opal_rtc_write() finally returns OPAL_SUCCESS,
* at which point the operation is complete and the event cleared.
*
* If we end up taking longer than rtc_read_timeout_ms millieconds waiting
* for the response from a read request, we simply return a cached value (plus
* an offset calculated from the timebase. When the read request finally
* returns, we update our cache value accordingly.
*
* There is two separate set of state for reads and writes. If both are
* attempted at the same time, the event bit will remain set as long as either
* of the two has a pending event to signal.
*/
enum {
RTC_TOD_VALID,
RTC_TOD_INVALID,
RTC_TOD_PERMANENT_ERROR,
} rtc_tod_state = RTC_TOD_INVALID;
static struct lock rtc_lock;
static struct fsp_msg *rtc_read_msg;
static struct fsp_msg *rtc_write_msg;
/* TODO We'd probably want to export and use this variable declared in fsp.c,
* instead of each component individually maintaining the state.. may be for
* later optimization
*/
static bool fsp_in_reset = false;
/* last synchonisation point */
static struct {
struct tm tm;
unsigned long tb;
bool dirty;
} rtc_tod_cache;
struct opal_tpo_data {
uint64_t tpo_async_token;
uint32_t *year_month_day;
uint32_t *hour_min;
};
/* Timebase value when we last initiated a RTC read request */
static unsigned long read_req_tb;
/* If a RTC read takes longer than this, we return a value generated
* from the cache + timebase */
static const int rtc_read_timeout_ms = 1500;
DEFINE_LOG_ENTRY(OPAL_RC_RTC_TOD, OPAL_PLATFORM_ERR_EVT, OPAL_RTC,
OPAL_PLATFORM_FIRMWARE, OPAL_INFO,
OPAL_NA, NULL);
DEFINE_LOG_ENTRY(OPAL_RC_RTC_READ, OPAL_PLATFORM_ERR_EVT, OPAL_RTC,
OPAL_PLATFORM_FIRMWARE, OPAL_INFO,
OPAL_NA, NULL);
static void tm_add(struct tm *in, struct tm *out, unsigned long secs)
{
assert(in);
assert(out);
*out = *in;
out->tm_sec += secs;
mktime(out);
}
static void fsp_tpo_req_complete(struct fsp_msg *read_resp)
{
struct opal_tpo_data *attr = read_resp->user_data;
int val;
int rc;
val = (read_resp->resp->word1 >> 8) & 0xff;
switch (val) {
case FSP_STATUS_TOD_RESET:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TPO in invalid state\n");
rc = OPAL_INTERNAL_ERROR;
break;
case FSP_STATUS_TOD_PERMANENT_ERROR:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TPO in permanent error state\n");
rc = OPAL_INTERNAL_ERROR;
break;
case FSP_STATUS_INVALID_DATA:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TPO in permanent error state\n");
rc = OPAL_PARAMETER;
break;
case FSP_STATUS_SUCCESS:
/* Save the read TPO value in our cache */
if (attr->year_month_day)
*(attr->year_month_day) =
read_resp->resp->data.words[0];
if (attr->hour_min)
*(attr->hour_min) = read_resp->resp->data.words[1];
rc = OPAL_SUCCESS;
break;
default:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"TPO read failed: %d\n", val);
rc = OPAL_INTERNAL_ERROR;
break;
}
opal_queue_msg(OPAL_MSG_ASYNC_COMP, NULL, NULL,
attr->tpo_async_token, rc);
free(attr);
fsp_freemsg(read_resp);
}
static void fsp_rtc_process_read(struct fsp_msg *read_resp)
{
int val = (read_resp->word1 >> 8) & 0xff;
switch (val) {
case FSP_STATUS_TOD_RESET:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TOD in invalid state\n");
rtc_tod_state = RTC_TOD_INVALID;
break;
case FSP_STATUS_TOD_PERMANENT_ERROR:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TOD in permanent error state\n");
rtc_tod_state = RTC_TOD_PERMANENT_ERROR;
break;
case FSP_STATUS_SUCCESS:
/* Save the read RTC value in our cache */
datetime_to_tm(read_resp->data.words[0],
(u64) read_resp->data.words[1] << 32, &rtc_tod_cache.tm);
rtc_tod_cache.tb = mftb();
rtc_tod_state = RTC_TOD_VALID;
break;
default:
log_simple_error(&e_info(OPAL_RC_RTC_TOD),
"RTC TOD read failed: %d\n", val);
rtc_tod_state = RTC_TOD_INVALID;
}
}
static void opal_rtc_eval_events(void)
{
bool pending = false;
if (rtc_read_msg && !fsp_msg_busy(rtc_read_msg))
pending = true;
if (rtc_write_msg && !fsp_msg_busy(rtc_write_msg))
pending = true;
opal_update_pending_evt(OPAL_EVENT_RTC, pending ? OPAL_EVENT_RTC : 0);
}
static void fsp_rtc_req_complete(struct fsp_msg *msg)
{
lock(&rtc_lock);
DBG("RTC completion %p\n", msg);
if (msg == rtc_read_msg)
fsp_rtc_process_read(msg->resp);
opal_rtc_eval_events();
unlock(&rtc_lock);
}
static int64_t fsp_rtc_send_read_request(void)
{
struct fsp_msg *msg;
int rc;
msg = fsp_mkmsg(FSP_CMD_READ_TOD, 0);
if (!msg) {
log_simple_error(&e_info(OPAL_RC_RTC_READ),
"RTC: failed to allocate read message\n");
return OPAL_INTERNAL_ERROR;
}
rc = fsp_queue_msg(msg, fsp_rtc_req_complete);
if (rc) {
fsp_freemsg(msg);
log_simple_error(&e_info(OPAL_RC_RTC_READ),
"RTC: failed to queue read message: %d\n", rc);
return OPAL_INTERNAL_ERROR;
}
read_req_tb = mftb();
rtc_read_msg = msg;
return OPAL_BUSY_EVENT;
}
static void encode_cached_tod(uint32_t *year_month_day,
uint64_t *hour_minute_second_millisecond)
{
unsigned long cache_age_sec;
struct tm tm;
cache_age_sec = tb_to_msecs(mftb() - rtc_tod_cache.tb) / 1000;
tm_add(&rtc_tod_cache.tm, &tm, cache_age_sec);
/* Format to OPAL API values */
tm_to_datetime(&tm, year_month_day, hour_minute_second_millisecond);
}
int fsp_rtc_get_cached_tod(uint32_t *year_month_day,
uint64_t *hour_minute_second_millisecond)
{
if (rtc_tod_state != RTC_TOD_VALID)
return -1;
encode_cached_tod(year_month_day,
hour_minute_second_millisecond);
return 0;
}
static int64_t fsp_opal_rtc_read(uint32_t *year_month_day,
uint64_t *hour_minute_second_millisecond)
{
struct fsp_msg *msg;
int64_t rc;
if (!year_month_day || !hour_minute_second_millisecond)
return OPAL_PARAMETER;
lock(&rtc_lock);
/* During R/R of FSP, read cached TOD */
if (fsp_in_reset) {
fsp_rtc_get_cached_tod(year_month_day,
hour_minute_second_millisecond);
rc = OPAL_SUCCESS;
goto out;
}
msg = rtc_read_msg;
if (rtc_tod_state == RTC_TOD_PERMANENT_ERROR) {
if (msg && !fsp_msg_busy(msg))
fsp_freemsg(msg);
rc = OPAL_HARDWARE;
goto out;
}
/* If we don't have a read pending already, fire off a request and
* return */
if (!msg) {
DBG("Sending new RTC read request\n");
rc = fsp_rtc_send_read_request();
/* If our pending read is done, clear events and return the time
* from the cache */
} else if (!fsp_msg_busy(msg)) {
DBG("RTC read complete, state %d\n", rtc_tod_state);
rtc_read_msg = NULL;
opal_rtc_eval_events();
fsp_freemsg(msg);
if (rtc_tod_state == RTC_TOD_VALID) {
encode_cached_tod(year_month_day,
hour_minute_second_millisecond);
rc = OPAL_SUCCESS;
} else
rc = OPAL_INTERNAL_ERROR;
/* Timeout: return our cached value (updated from tb), but leave the
* read request pending so it will update the cache later */
} else if (mftb() > read_req_tb + msecs_to_tb(rtc_read_timeout_ms)) {
DBG("RTC read timed out\n");
encode_cached_tod(year_month_day,
hour_minute_second_millisecond);
rc = OPAL_SUCCESS;
/* Otherwise, we're still waiting on the read to complete */
} else {
rc = OPAL_BUSY_EVENT;
}
out:
unlock(&rtc_lock);
return rc;
}
static int64_t fsp_opal_rtc_write(uint32_t year_month_day,
uint64_t hour_minute_second_millisecond)
{
struct fsp_msg *msg;
uint32_t w0, w1, w2;
int64_t rc;
lock(&rtc_lock);
if (rtc_tod_state == RTC_TOD_PERMANENT_ERROR) {
rc = OPAL_HARDWARE;
msg = NULL;
goto bail;
}
/* Do we have a request already ? */
msg = rtc_write_msg;
if (msg) {
/* If it's still in progress, return */
if (fsp_msg_busy(msg)) {
/* Don't free the message */
msg = NULL;
rc = OPAL_BUSY_EVENT;
goto bail;
}
DBG("Completed write request @%p, state=%d\n", msg, msg->state);
/* It's complete, clear events */
rtc_write_msg = NULL;
opal_rtc_eval_events();
/* Check error state */
if (msg->state != fsp_msg_done) {
DBG(" -> request not in done state -> error !\n");
rc = OPAL_INTERNAL_ERROR;
goto bail;
}
rc = OPAL_SUCCESS;
goto bail;
}
DBG("Sending new write request...\n");
/* Create a request and send it. Just like for read, we ignore
* the "millisecond" field which is probably supposed to be
* microseconds and which Linux ignores as well anyway
*/
w0 = year_month_day;
w1 = (hour_minute_second_millisecond >> 32) & 0xffffff00;
w2 = 0;
rtc_write_msg = fsp_mkmsg(FSP_CMD_WRITE_TOD, 3, w0, w1, w2);
if (!rtc_write_msg) {
DBG(" -> allocation failed !\n");
rc = OPAL_INTERNAL_ERROR;
goto bail;
}
DBG(" -> req at %p\n", rtc_write_msg);
if (fsp_in_reset) {
datetime_to_tm(rtc_write_msg->data.words[0],
(u64) rtc_write_msg->data.words[1] << 32, &rtc_tod_cache.tm);
rtc_tod_cache.tb = mftb();
rtc_tod_cache.dirty = true;
fsp_freemsg(rtc_write_msg);
rtc_write_msg = NULL;
rc = OPAL_SUCCESS;
goto bail;
} else if (fsp_queue_msg(rtc_write_msg, fsp_rtc_req_complete)) {
DBG(" -> queueing failed !\n");
rc = OPAL_INTERNAL_ERROR;
fsp_freemsg(rtc_write_msg);
rtc_write_msg = NULL;
goto bail;
}
rc = OPAL_BUSY_EVENT;
bail:
unlock(&rtc_lock);
if (msg)
fsp_freemsg(msg);
return rc;
}
/* Set timed power on values to fsp */
static int64_t fsp_opal_tpo_write(uint64_t async_token, uint32_t y_m_d,
uint32_t hr_min)
{
static struct opal_tpo_data *attr;
struct fsp_msg *msg;
if (!fsp_present())
return OPAL_HARDWARE;
attr = zalloc(sizeof(struct opal_tpo_data));
if (!attr)
return OPAL_NO_MEM;
/* Create a request and send it.*/
attr->tpo_async_token = async_token;
DBG("Sending TPO write request...\n");
msg = fsp_mkmsg(FSP_CMD_TPO_WRITE, 2, y_m_d, hr_min);
if (!msg) {
prerror("TPO: Failed to create message for WRITE to FSP\n");
free(attr);
return OPAL_INTERNAL_ERROR;
}
msg->user_data = attr;
if (fsp_queue_msg(msg, fsp_tpo_req_complete)) {
free(attr);
fsp_freemsg(msg);
return OPAL_INTERNAL_ERROR;
}
return OPAL_ASYNC_COMPLETION;
}
/* Read Timed power on (TPO) from FSP */
static int64_t fsp_opal_tpo_read(uint64_t async_token, uint32_t *y_m_d,
uint32_t *hr_min)
{
static struct opal_tpo_data *attr;
struct fsp_msg *msg;
int64_t rc;
if (!fsp_present())
return OPAL_HARDWARE;
if (!y_m_d || !hr_min)
return OPAL_PARAMETER;
attr = zalloc(sizeof(*attr));
if (!attr)
return OPAL_NO_MEM;
/* Send read requet to FSP */
attr->tpo_async_token = async_token;
attr->year_month_day = y_m_d;
attr->hour_min = hr_min;
DBG("Sending new TPO read request\n");
msg = fsp_mkmsg(FSP_CMD_TPO_READ, 0);
if (!msg) {
log_simple_error(&e_info(OPAL_RC_RTC_READ),
"TPO: failed to allocate read message\n");
free(attr);
return OPAL_INTERNAL_ERROR;
}
msg->user_data = attr;
rc = fsp_queue_msg(msg, fsp_tpo_req_complete);
if (rc) {
free(attr);
fsp_freemsg(msg);
log_simple_error(&e_info(OPAL_RC_RTC_READ),
"TPO: failed to queue read message: %lld\n", rc);
return OPAL_INTERNAL_ERROR;
}
return OPAL_ASYNC_COMPLETION;
}
static void rtc_flush_cached_tod(void)
{
struct fsp_msg *msg;
uint64_t h_m_s_m;
uint32_t y_m_d;
if (fsp_rtc_get_cached_tod(&y_m_d, &h_m_s_m))
return;
msg = fsp_mkmsg(FSP_CMD_WRITE_TOD, 3, y_m_d,
(h_m_s_m >> 32) & 0xffffff00, 0);
if (msg)
fsp_queue_msg(msg, fsp_freemsg);
}
static bool fsp_rtc_msg_rr(u32 cmd_sub_mod, struct fsp_msg *msg)
{
int rc = false;
assert(msg == NULL);
switch (cmd_sub_mod) {
case FSP_RESET_START:
lock(&rtc_lock);
fsp_in_reset = true;
unlock(&rtc_lock);
rc = true;
break;
case FSP_RELOAD_COMPLETE:
lock(&rtc_lock);
fsp_in_reset = false;
if (rtc_tod_cache.dirty) {
rtc_flush_cached_tod();
rtc_tod_cache.dirty = false;
}
unlock(&rtc_lock);
rc = true;
break;
}
return rc;
}
static struct fsp_client fsp_rtc_client_rr = {
.message = fsp_rtc_msg_rr,
};
void fsp_rtc_init(void)
{
struct fsp_msg msg, resp;
struct dt_node *np;
int rc;
if (!fsp_present()) {
rtc_tod_state = RTC_TOD_PERMANENT_ERROR;
return;
}
opal_register(OPAL_RTC_READ, fsp_opal_rtc_read, 2);
opal_register(OPAL_RTC_WRITE, fsp_opal_rtc_write, 2);
opal_register(OPAL_WRITE_TPO, fsp_opal_tpo_write, 3);
opal_register(OPAL_READ_TPO, fsp_opal_tpo_read, 3);
np = dt_new(opal_node, "rtc");
dt_add_property_strings(np, "compatible", "ibm,opal-rtc");
dt_add_property(np, "has-tpo", NULL, 0);
/* Register for the reset/reload event */
fsp_register_client(&fsp_rtc_client_rr, FSP_MCLASS_RR_EVENT);
msg.resp = &resp;
fsp_fillmsg(&msg, FSP_CMD_READ_TOD, 0);
DBG("Getting initial RTC TOD\n");
lock(&rtc_lock);
rc = fsp_sync_msg(&msg, false);
if (rc >= 0)
fsp_rtc_process_read(&resp);
else
rtc_tod_state = RTC_TOD_PERMANENT_ERROR;
unlock(&rtc_lock);
}
|