blob: 3f25f7fc79fc97b61b0a7f73fca22145f22de701 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stephen Rothwellc8b84972005-09-27 18:44:42 +10002 * Copyright (C) 2001 Troy D. Armstrong IBM Corporation
3 * Copyright (C) 2004-2005 Stephen Rothwell IBM Corporation
4 *
5 * This modules exists as an interface between a Linux secondary partition
6 * running on an iSeries and the primary partition's Virtual Service
7 * Processor (VSP) object. The VSP has final authority over powering on/off
8 * all partitions in the iSeries. It also provides miscellaneous low-level
9 * machine facility type operations.
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/types.h>
28#include <linux/errno.h>
29#include <linux/kernel.h>
30#include <linux/init.h>
31#include <linux/completion.h>
32#include <linux/delay.h>
33#include <linux/dma-mapping.h>
34#include <linux/bcd.h>
35
36#include <asm/time.h>
37#include <asm/uaccess.h>
Stephen Rothwelld0e8e292005-05-25 16:29:26 +100038#include <asm/paca.h>
Stephen Rothwell426c1a12005-10-14 14:51:42 +100039#include <asm/abs_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/iSeries/vio.h>
41#include <asm/iSeries/mf.h>
42#include <asm/iSeries/HvLpConfig.h>
Stephen Rothwelld0e8e292005-05-25 16:29:26 +100043#include <asm/iSeries/ItLpQueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Stephen Rothwellc8b84972005-09-27 18:44:42 +100045#include "setup.h"
46
47extern int piranha_simulator;
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * This is the structure layout for the Machine Facilites LPAR event
51 * flows.
52 */
53struct vsp_cmd_data {
54 u64 token;
55 u16 cmd;
56 HvLpIndex lp_index;
57 u8 result_code;
58 u32 reserved;
59 union {
60 u64 state; /* GetStateOut */
61 u64 ipl_type; /* GetIplTypeOut, Function02SelectIplTypeIn */
62 u64 ipl_mode; /* GetIplModeOut, Function02SelectIplModeIn */
63 u64 page[4]; /* GetSrcHistoryIn */
64 u64 flag; /* GetAutoIplWhenPrimaryIplsOut,
65 SetAutoIplWhenPrimaryIplsIn,
66 WhiteButtonPowerOffIn,
67 Function08FastPowerOffIn,
68 IsSpcnRackPowerIncompleteOut */
69 struct {
70 u64 token;
71 u64 address_type;
72 u64 side;
73 u32 length;
74 u32 offset;
75 } kern; /* SetKernelImageIn, GetKernelImageIn,
76 SetKernelCmdLineIn, GetKernelCmdLineIn */
77 u32 length_out; /* GetKernelImageOut, GetKernelCmdLineOut */
78 u8 reserved[80];
79 } sub_data;
80};
81
82struct vsp_rsp_data {
83 struct completion com;
84 struct vsp_cmd_data *response;
85};
86
87struct alloc_data {
88 u16 size;
89 u16 type;
90 u32 count;
91 u16 reserved1;
92 u8 reserved2;
93 HvLpIndex target_lp;
94};
95
96struct ce_msg_data;
97
98typedef void (*ce_msg_comp_hdlr)(void *token, struct ce_msg_data *vsp_cmd_rsp);
99
100struct ce_msg_comp_data {
101 ce_msg_comp_hdlr handler;
102 void *token;
103};
104
105struct ce_msg_data {
106 u8 ce_msg[12];
107 char reserved[4];
108 struct ce_msg_comp_data *completion;
109};
110
111struct io_mf_lp_event {
112 struct HvLpEvent hp_lp_event;
113 u16 subtype_result_code;
114 u16 reserved1;
115 u32 reserved2;
116 union {
117 struct alloc_data alloc;
118 struct ce_msg_data ce_msg;
119 struct vsp_cmd_data vsp_cmd;
120 } data;
121};
122
123#define subtype_data(a, b, c, d) \
124 (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
125
126/*
127 * All outgoing event traffic is kept on a FIFO queue. The first
128 * pointer points to the one that is outstanding, and all new
129 * requests get stuck on the end. Also, we keep a certain number of
130 * preallocated pending events so that we can operate very early in
131 * the boot up sequence (before kmalloc is ready).
132 */
133struct pending_event {
134 struct pending_event *next;
135 struct io_mf_lp_event event;
136 MFCompleteHandler hdlr;
137 char dma_data[72];
138 unsigned dma_data_length;
139 unsigned remote_address;
140};
141static spinlock_t pending_event_spinlock;
142static struct pending_event *pending_event_head;
143static struct pending_event *pending_event_tail;
144static struct pending_event *pending_event_avail;
145static struct pending_event pending_event_prealloc[16];
146
147/*
148 * Put a pending event onto the available queue, so it can get reused.
149 * Attention! You must have the pending_event_spinlock before calling!
150 */
151static void free_pending_event(struct pending_event *ev)
152{
153 if (ev != NULL) {
154 ev->next = pending_event_avail;
155 pending_event_avail = ev;
156 }
157}
158
159/*
160 * Enqueue the outbound event onto the stack. If the queue was
161 * empty to begin with, we must also issue it via the Hypervisor
162 * interface. There is a section of code below that will touch
163 * the first stack pointer without the protection of the pending_event_spinlock.
164 * This is OK, because we know that nobody else will be modifying
165 * the first pointer when we do this.
166 */
167static int signal_event(struct pending_event *ev)
168{
169 int rc = 0;
170 unsigned long flags;
171 int go = 1;
172 struct pending_event *ev1;
173 HvLpEvent_Rc hv_rc;
174
175 /* enqueue the event */
176 if (ev != NULL) {
177 ev->next = NULL;
178 spin_lock_irqsave(&pending_event_spinlock, flags);
179 if (pending_event_head == NULL)
180 pending_event_head = ev;
181 else {
182 go = 0;
183 pending_event_tail->next = ev;
184 }
185 pending_event_tail = ev;
186 spin_unlock_irqrestore(&pending_event_spinlock, flags);
187 }
188
189 /* send the event */
190 while (go) {
191 go = 0;
192
193 /* any DMA data to send beforehand? */
194 if (pending_event_head->dma_data_length > 0)
195 HvCallEvent_dmaToSp(pending_event_head->dma_data,
196 pending_event_head->remote_address,
197 pending_event_head->dma_data_length,
198 HvLpDma_Direction_LocalToRemote);
199
200 hv_rc = HvCallEvent_signalLpEvent(
201 &pending_event_head->event.hp_lp_event);
202 if (hv_rc != HvLpEvent_Rc_Good) {
203 printk(KERN_ERR "mf.c: HvCallEvent_signalLpEvent() "
204 "failed with %d\n", (int)hv_rc);
205
206 spin_lock_irqsave(&pending_event_spinlock, flags);
207 ev1 = pending_event_head;
208 pending_event_head = pending_event_head->next;
209 if (pending_event_head != NULL)
210 go = 1;
211 spin_unlock_irqrestore(&pending_event_spinlock, flags);
212
213 if (ev1 == ev)
214 rc = -EIO;
215 else if (ev1->hdlr != NULL)
216 (*ev1->hdlr)((void *)ev1->event.hp_lp_event.xCorrelationToken, -EIO);
217
218 spin_lock_irqsave(&pending_event_spinlock, flags);
219 free_pending_event(ev1);
220 spin_unlock_irqrestore(&pending_event_spinlock, flags);
221 }
222 }
223
224 return rc;
225}
226
227/*
228 * Allocate a new pending_event structure, and initialize it.
229 */
230static struct pending_event *new_pending_event(void)
231{
232 struct pending_event *ev = NULL;
233 HvLpIndex primary_lp = HvLpConfig_getPrimaryLpIndex();
234 unsigned long flags;
235 struct HvLpEvent *hev;
236
237 spin_lock_irqsave(&pending_event_spinlock, flags);
238 if (pending_event_avail != NULL) {
239 ev = pending_event_avail;
240 pending_event_avail = pending_event_avail->next;
241 }
242 spin_unlock_irqrestore(&pending_event_spinlock, flags);
243 if (ev == NULL) {
244 ev = kmalloc(sizeof(struct pending_event), GFP_ATOMIC);
245 if (ev == NULL) {
246 printk(KERN_ERR "mf.c: unable to kmalloc %ld bytes\n",
247 sizeof(struct pending_event));
248 return NULL;
249 }
250 }
251 memset(ev, 0, sizeof(struct pending_event));
252 hev = &ev->event.hp_lp_event;
253 hev->xFlags.xValid = 1;
254 hev->xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
255 hev->xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
256 hev->xFlags.xFunction = HvLpEvent_Function_Int;
257 hev->xType = HvLpEvent_Type_MachineFac;
258 hev->xSourceLp = HvLpConfig_getLpIndex();
259 hev->xTargetLp = primary_lp;
260 hev->xSizeMinus1 = sizeof(ev->event) - 1;
261 hev->xRc = HvLpEvent_Rc_Good;
262 hev->xSourceInstanceId = HvCallEvent_getSourceLpInstanceId(primary_lp,
263 HvLpEvent_Type_MachineFac);
264 hev->xTargetInstanceId = HvCallEvent_getTargetLpInstanceId(primary_lp,
265 HvLpEvent_Type_MachineFac);
266
267 return ev;
268}
269
270static int signal_vsp_instruction(struct vsp_cmd_data *vsp_cmd)
271{
272 struct pending_event *ev = new_pending_event();
273 int rc;
274 struct vsp_rsp_data response;
275
276 if (ev == NULL)
277 return -ENOMEM;
278
279 init_completion(&response.com);
280 response.response = vsp_cmd;
281 ev->event.hp_lp_event.xSubtype = 6;
282 ev->event.hp_lp_event.x.xSubtypeData =
283 subtype_data('M', 'F', 'V', 'I');
284 ev->event.data.vsp_cmd.token = (u64)&response;
285 ev->event.data.vsp_cmd.cmd = vsp_cmd->cmd;
286 ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
287 ev->event.data.vsp_cmd.result_code = 0xFF;
288 ev->event.data.vsp_cmd.reserved = 0;
289 memcpy(&(ev->event.data.vsp_cmd.sub_data),
290 &(vsp_cmd->sub_data), sizeof(vsp_cmd->sub_data));
291 mb();
292
293 rc = signal_event(ev);
294 if (rc == 0)
295 wait_for_completion(&response.com);
296 return rc;
297}
298
299
300/*
301 * Send a 12-byte CE message to the primary partition VSP object
302 */
303static int signal_ce_msg(char *ce_msg, struct ce_msg_comp_data *completion)
304{
305 struct pending_event *ev = new_pending_event();
306
307 if (ev == NULL)
308 return -ENOMEM;
309
310 ev->event.hp_lp_event.xSubtype = 0;
311 ev->event.hp_lp_event.x.xSubtypeData =
312 subtype_data('M', 'F', 'C', 'E');
313 memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
314 ev->event.data.ce_msg.completion = completion;
315 return signal_event(ev);
316}
317
318/*
319 * Send a 12-byte CE message (with no data) to the primary partition VSP object
320 */
321static int signal_ce_msg_simple(u8 ce_op, struct ce_msg_comp_data *completion)
322{
323 u8 ce_msg[12];
324
325 memset(ce_msg, 0, sizeof(ce_msg));
326 ce_msg[3] = ce_op;
327 return signal_ce_msg(ce_msg, completion);
328}
329
330/*
331 * Send a 12-byte CE message and DMA data to the primary partition VSP object
332 */
333static int dma_and_signal_ce_msg(char *ce_msg,
334 struct ce_msg_comp_data *completion, void *dma_data,
335 unsigned dma_data_length, unsigned remote_address)
336{
337 struct pending_event *ev = new_pending_event();
338
339 if (ev == NULL)
340 return -ENOMEM;
341
342 ev->event.hp_lp_event.xSubtype = 0;
343 ev->event.hp_lp_event.x.xSubtypeData =
344 subtype_data('M', 'F', 'C', 'E');
345 memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
346 ev->event.data.ce_msg.completion = completion;
347 memcpy(ev->dma_data, dma_data, dma_data_length);
348 ev->dma_data_length = dma_data_length;
349 ev->remote_address = remote_address;
350 return signal_event(ev);
351}
352
353/*
354 * Initiate a nice (hopefully) shutdown of Linux. We simply are
355 * going to try and send the init process a SIGINT signal. If
356 * this fails (why?), we'll simply force it off in a not-so-nice
357 * manner.
358 */
359static int shutdown(void)
360{
361 int rc = kill_proc(1, SIGINT, 1);
362
363 if (rc) {
364 printk(KERN_ALERT "mf.c: SIGINT to init failed (%d), "
365 "hard shutdown commencing\n", rc);
366 mf_power_off();
367 } else
368 printk(KERN_INFO "mf.c: init has been successfully notified "
369 "to proceed with shutdown\n");
370 return rc;
371}
372
373/*
374 * The primary partition VSP object is sending us a new
375 * event flow. Handle it...
376 */
377static void handle_int(struct io_mf_lp_event *event)
378{
379 struct ce_msg_data *ce_msg_data;
380 struct ce_msg_data *pce_msg_data;
381 unsigned long flags;
382 struct pending_event *pev;
383
384 /* ack the interrupt */
385 event->hp_lp_event.xRc = HvLpEvent_Rc_Good;
386 HvCallEvent_ackLpEvent(&event->hp_lp_event);
387
388 /* process interrupt */
389 switch (event->hp_lp_event.xSubtype) {
390 case 0: /* CE message */
391 ce_msg_data = &event->data.ce_msg;
392 switch (ce_msg_data->ce_msg[3]) {
393 case 0x5B: /* power control notification */
394 if ((ce_msg_data->ce_msg[5] & 0x20) != 0) {
395 printk(KERN_INFO "mf.c: Commencing partition shutdown\n");
396 if (shutdown() == 0)
397 signal_ce_msg_simple(0xDB, NULL);
398 }
399 break;
400 case 0xC0: /* get time */
401 spin_lock_irqsave(&pending_event_spinlock, flags);
402 pev = pending_event_head;
403 if (pev != NULL)
404 pending_event_head = pending_event_head->next;
405 spin_unlock_irqrestore(&pending_event_spinlock, flags);
406 if (pev == NULL)
407 break;
408 pce_msg_data = &pev->event.data.ce_msg;
409 if (pce_msg_data->ce_msg[3] != 0x40)
410 break;
411 if (pce_msg_data->completion != NULL) {
412 ce_msg_comp_hdlr handler =
413 pce_msg_data->completion->handler;
414 void *token = pce_msg_data->completion->token;
415
416 if (handler != NULL)
417 (*handler)(token, ce_msg_data);
418 }
419 spin_lock_irqsave(&pending_event_spinlock, flags);
420 free_pending_event(pev);
421 spin_unlock_irqrestore(&pending_event_spinlock, flags);
422 /* send next waiting event */
423 if (pending_event_head != NULL)
424 signal_event(NULL);
425 break;
426 }
427 break;
428 case 1: /* IT sys shutdown */
429 printk(KERN_INFO "mf.c: Commencing system shutdown\n");
430 shutdown();
431 break;
432 }
433}
434
435/*
436 * The primary partition VSP object is acknowledging the receipt
437 * of a flow we sent to them. If there are other flows queued
438 * up, we must send another one now...
439 */
440static void handle_ack(struct io_mf_lp_event *event)
441{
442 unsigned long flags;
443 struct pending_event *two = NULL;
444 unsigned long free_it = 0;
445 struct ce_msg_data *ce_msg_data;
446 struct ce_msg_data *pce_msg_data;
447 struct vsp_rsp_data *rsp;
448
449 /* handle current event */
450 if (pending_event_head == NULL) {
451 printk(KERN_ERR "mf.c: stack empty for receiving ack\n");
452 return;
453 }
454
455 switch (event->hp_lp_event.xSubtype) {
456 case 0: /* CE msg */
457 ce_msg_data = &event->data.ce_msg;
458 if (ce_msg_data->ce_msg[3] != 0x40) {
459 free_it = 1;
460 break;
461 }
462 if (ce_msg_data->ce_msg[2] == 0)
463 break;
464 free_it = 1;
465 pce_msg_data = &pending_event_head->event.data.ce_msg;
466 if (pce_msg_data->completion != NULL) {
467 ce_msg_comp_hdlr handler =
468 pce_msg_data->completion->handler;
469 void *token = pce_msg_data->completion->token;
470
471 if (handler != NULL)
472 (*handler)(token, ce_msg_data);
473 }
474 break;
475 case 4: /* allocate */
476 case 5: /* deallocate */
477 if (pending_event_head->hdlr != NULL)
478 (*pending_event_head->hdlr)((void *)event->hp_lp_event.xCorrelationToken, event->data.alloc.count);
479 free_it = 1;
480 break;
481 case 6:
482 free_it = 1;
483 rsp = (struct vsp_rsp_data *)event->data.vsp_cmd.token;
484 if (rsp == NULL) {
485 printk(KERN_ERR "mf.c: no rsp\n");
486 break;
487 }
488 if (rsp->response != NULL)
489 memcpy(rsp->response, &event->data.vsp_cmd,
490 sizeof(event->data.vsp_cmd));
491 complete(&rsp->com);
492 break;
493 }
494
495 /* remove from queue */
496 spin_lock_irqsave(&pending_event_spinlock, flags);
497 if ((pending_event_head != NULL) && (free_it == 1)) {
498 struct pending_event *oldHead = pending_event_head;
499
500 pending_event_head = pending_event_head->next;
501 two = pending_event_head;
502 free_pending_event(oldHead);
503 }
504 spin_unlock_irqrestore(&pending_event_spinlock, flags);
505
506 /* send next waiting event */
507 if (two != NULL)
508 signal_event(NULL);
509}
510
511/*
512 * This is the generic event handler we are registering with
513 * the Hypervisor. Ensure the flows are for us, and then
514 * parse it enough to know if it is an interrupt or an
515 * acknowledge.
516 */
517static void hv_handler(struct HvLpEvent *event, struct pt_regs *regs)
518{
519 if ((event != NULL) && (event->xType == HvLpEvent_Type_MachineFac)) {
520 switch(event->xFlags.xFunction) {
521 case HvLpEvent_Function_Ack:
522 handle_ack((struct io_mf_lp_event *)event);
523 break;
524 case HvLpEvent_Function_Int:
525 handle_int((struct io_mf_lp_event *)event);
526 break;
527 default:
528 printk(KERN_ERR "mf.c: non ack/int event received\n");
529 break;
530 }
531 } else
532 printk(KERN_ERR "mf.c: alien event received\n");
533}
534
535/*
536 * Global kernel interface to allocate and seed events into the
537 * Hypervisor.
538 */
539void mf_allocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
540 unsigned size, unsigned count, MFCompleteHandler hdlr,
541 void *user_token)
542{
543 struct pending_event *ev = new_pending_event();
544 int rc;
545
546 if (ev == NULL) {
547 rc = -ENOMEM;
548 } else {
549 ev->event.hp_lp_event.xSubtype = 4;
550 ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
551 ev->event.hp_lp_event.x.xSubtypeData =
552 subtype_data('M', 'F', 'M', 'A');
553 ev->event.data.alloc.target_lp = target_lp;
554 ev->event.data.alloc.type = type;
555 ev->event.data.alloc.size = size;
556 ev->event.data.alloc.count = count;
557 ev->hdlr = hdlr;
558 rc = signal_event(ev);
559 }
560 if ((rc != 0) && (hdlr != NULL))
561 (*hdlr)(user_token, rc);
562}
563EXPORT_SYMBOL(mf_allocate_lp_events);
564
565/*
566 * Global kernel interface to unseed and deallocate events already in
567 * Hypervisor.
568 */
569void mf_deallocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
570 unsigned count, MFCompleteHandler hdlr, void *user_token)
571{
572 struct pending_event *ev = new_pending_event();
573 int rc;
574
575 if (ev == NULL)
576 rc = -ENOMEM;
577 else {
578 ev->event.hp_lp_event.xSubtype = 5;
579 ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
580 ev->event.hp_lp_event.x.xSubtypeData =
581 subtype_data('M', 'F', 'M', 'D');
582 ev->event.data.alloc.target_lp = target_lp;
583 ev->event.data.alloc.type = type;
584 ev->event.data.alloc.count = count;
585 ev->hdlr = hdlr;
586 rc = signal_event(ev);
587 }
588 if ((rc != 0) && (hdlr != NULL))
589 (*hdlr)(user_token, rc);
590}
591EXPORT_SYMBOL(mf_deallocate_lp_events);
592
593/*
594 * Global kernel interface to tell the VSP object in the primary
595 * partition to power this partition off.
596 */
597void mf_power_off(void)
598{
599 printk(KERN_INFO "mf.c: Down it goes...\n");
600 signal_ce_msg_simple(0x4d, NULL);
601 for (;;)
602 ;
603}
604
605/*
606 * Global kernel interface to tell the VSP object in the primary
607 * partition to reboot this partition.
608 */
609void mf_reboot(void)
610{
611 printk(KERN_INFO "mf.c: Preparing to bounce...\n");
612 signal_ce_msg_simple(0x4e, NULL);
613 for (;;)
614 ;
615}
616
617/*
618 * Display a single word SRC onto the VSP control panel.
619 */
620void mf_display_src(u32 word)
621{
622 u8 ce[12];
623
624 memset(ce, 0, sizeof(ce));
625 ce[3] = 0x4a;
626 ce[7] = 0x01;
627 ce[8] = word >> 24;
628 ce[9] = word >> 16;
629 ce[10] = word >> 8;
630 ce[11] = word;
631 signal_ce_msg(ce, NULL);
632}
633
634/*
635 * Display a single word SRC of the form "PROGXXXX" on the VSP control panel.
636 */
637void mf_display_progress(u16 value)
638{
639 u8 ce[12];
640 u8 src[72];
641
642 memcpy(ce, "\x00\x00\x04\x4A\x00\x00\x00\x48\x00\x00\x00\x00", 12);
643 memcpy(src, "\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
644 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
645 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
646 "\x00\x00\x00\x00PROGxxxx ",
647 72);
648 src[6] = value >> 8;
649 src[7] = value & 255;
650 src[44] = "0123456789ABCDEF"[(value >> 12) & 15];
651 src[45] = "0123456789ABCDEF"[(value >> 8) & 15];
652 src[46] = "0123456789ABCDEF"[(value >> 4) & 15];
653 src[47] = "0123456789ABCDEF"[value & 15];
654 dma_and_signal_ce_msg(ce, NULL, src, sizeof(src), 9 * 64 * 1024);
655}
656
657/*
658 * Clear the VSP control panel. Used to "erase" an SRC that was
659 * previously displayed.
660 */
661void mf_clear_src(void)
662{
663 signal_ce_msg_simple(0x4b, NULL);
664}
665
666/*
667 * Initialization code here.
668 */
669void mf_init(void)
670{
671 int i;
672
673 /* initialize */
674 spin_lock_init(&pending_event_spinlock);
675 for (i = 0;
676 i < sizeof(pending_event_prealloc) / sizeof(*pending_event_prealloc);
677 ++i)
678 free_pending_event(&pending_event_prealloc[i]);
679 HvLpEvent_registerHandler(HvLpEvent_Type_MachineFac, &hv_handler);
680
681 /* virtual continue ack */
682 signal_ce_msg_simple(0x57, NULL);
683
684 /* initialization complete */
685 printk(KERN_NOTICE "mf.c: iSeries Linux LPAR Machine Facilities "
686 "initialized\n");
687}
688
689struct rtc_time_data {
690 struct completion com;
691 struct ce_msg_data ce_msg;
692 int rc;
693};
694
695static void get_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
696{
697 struct rtc_time_data *rtc = token;
698
699 memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
700 rtc->rc = 0;
701 complete(&rtc->com);
702}
703
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000704static int rtc_set_tm(int rc, u8 *ce_msg, struct rtc_time *tm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 tm->tm_wday = 0;
707 tm->tm_yday = 0;
708 tm->tm_isdst = 0;
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000709 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 tm->tm_sec = 0;
711 tm->tm_min = 0;
712 tm->tm_hour = 0;
713 tm->tm_mday = 15;
714 tm->tm_mon = 5;
715 tm->tm_year = 52;
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000716 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000719 if ((ce_msg[2] == 0xa9) ||
720 (ce_msg[2] == 0xaf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 /* TOD clock is not set */
722 tm->tm_sec = 1;
723 tm->tm_min = 1;
724 tm->tm_hour = 1;
725 tm->tm_mday = 10;
726 tm->tm_mon = 8;
727 tm->tm_year = 71;
728 mf_set_rtc(tm);
729 }
730 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 u8 year = ce_msg[5];
732 u8 sec = ce_msg[6];
733 u8 min = ce_msg[7];
734 u8 hour = ce_msg[8];
735 u8 day = ce_msg[10];
736 u8 mon = ce_msg[11];
737
738 BCD_TO_BIN(sec);
739 BCD_TO_BIN(min);
740 BCD_TO_BIN(hour);
741 BCD_TO_BIN(day);
742 BCD_TO_BIN(mon);
743 BCD_TO_BIN(year);
744
745 if (year <= 69)
746 year += 100;
747
748 tm->tm_sec = sec;
749 tm->tm_min = min;
750 tm->tm_hour = hour;
751 tm->tm_mday = day;
752 tm->tm_mon = mon;
753 tm->tm_year = year;
754 }
755
756 return 0;
757}
758
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000759int mf_get_rtc(struct rtc_time *tm)
760{
761 struct ce_msg_comp_data ce_complete;
762 struct rtc_time_data rtc_data;
763 int rc;
764
765 memset(&ce_complete, 0, sizeof(ce_complete));
766 memset(&rtc_data, 0, sizeof(rtc_data));
767 init_completion(&rtc_data.com);
768 ce_complete.handler = &get_rtc_time_complete;
769 ce_complete.token = &rtc_data;
770 rc = signal_ce_msg_simple(0x40, &ce_complete);
771 if (rc)
772 return rc;
773 wait_for_completion(&rtc_data.com);
774 return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
775}
776
777struct boot_rtc_time_data {
778 int busy;
779 struct ce_msg_data ce_msg;
780 int rc;
781};
782
783static void get_boot_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
784{
785 struct boot_rtc_time_data *rtc = token;
786
787 memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
788 rtc->rc = 0;
789 rtc->busy = 0;
790}
791
792int mf_get_boot_rtc(struct rtc_time *tm)
793{
794 struct ce_msg_comp_data ce_complete;
795 struct boot_rtc_time_data rtc_data;
796 int rc;
797
798 memset(&ce_complete, 0, sizeof(ce_complete));
799 memset(&rtc_data, 0, sizeof(rtc_data));
800 rtc_data.busy = 1;
801 ce_complete.handler = &get_boot_rtc_time_complete;
802 ce_complete.token = &rtc_data;
803 rc = signal_ce_msg_simple(0x40, &ce_complete);
804 if (rc)
805 return rc;
806 /* We need to poll here as we are not yet taking interrupts */
807 while (rtc_data.busy) {
Michael Ellerman937b31b2005-06-30 15:15:42 +1000808 if (hvlpevent_is_pending())
Michael Ellerman74889802005-06-30 15:15:53 +1000809 process_hvlpevents(NULL);
Stephen Rothwelld0e8e292005-05-25 16:29:26 +1000810 }
811 return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814int mf_set_rtc(struct rtc_time *tm)
815{
816 char ce_time[12];
817 u8 day, mon, hour, min, sec, y1, y2;
818 unsigned year;
819
820 year = 1900 + tm->tm_year;
821 y1 = year / 100;
822 y2 = year % 100;
823
824 sec = tm->tm_sec;
825 min = tm->tm_min;
826 hour = tm->tm_hour;
827 day = tm->tm_mday;
828 mon = tm->tm_mon + 1;
829
830 BIN_TO_BCD(sec);
831 BIN_TO_BCD(min);
832 BIN_TO_BCD(hour);
833 BIN_TO_BCD(mon);
834 BIN_TO_BCD(day);
835 BIN_TO_BCD(y1);
836 BIN_TO_BCD(y2);
837
838 memset(ce_time, 0, sizeof(ce_time));
839 ce_time[3] = 0x41;
840 ce_time[4] = y1;
841 ce_time[5] = y2;
842 ce_time[6] = sec;
843 ce_time[7] = min;
844 ce_time[8] = hour;
845 ce_time[10] = day;
846 ce_time[11] = mon;
847
848 return signal_ce_msg(ce_time, NULL);
849}
850
851#ifdef CONFIG_PROC_FS
852
853static int proc_mf_dump_cmdline(char *page, char **start, off_t off,
854 int count, int *eof, void *data)
855{
856 int len;
857 char *p;
858 struct vsp_cmd_data vsp_cmd;
859 int rc;
860 dma_addr_t dma_addr;
861
862 /* The HV appears to return no more than 256 bytes of command line */
863 if (off >= 256)
864 return 0;
865 if ((off + count) > 256)
866 count = 256 - off;
867
868 dma_addr = dma_map_single(iSeries_vio_dev, page, off + count,
869 DMA_FROM_DEVICE);
870 if (dma_mapping_error(dma_addr))
871 return -ENOMEM;
872 memset(page, 0, off + count);
873 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
874 vsp_cmd.cmd = 33;
875 vsp_cmd.sub_data.kern.token = dma_addr;
876 vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
877 vsp_cmd.sub_data.kern.side = (u64)data;
878 vsp_cmd.sub_data.kern.length = off + count;
879 mb();
880 rc = signal_vsp_instruction(&vsp_cmd);
881 dma_unmap_single(iSeries_vio_dev, dma_addr, off + count,
882 DMA_FROM_DEVICE);
883 if (rc)
884 return rc;
885 if (vsp_cmd.result_code != 0)
886 return -ENOMEM;
887 p = page;
888 len = 0;
889 while (len < (off + count)) {
890 if ((*p == '\0') || (*p == '\n')) {
891 if (*p == '\0')
892 *p = '\n';
893 p++;
894 len++;
895 *eof = 1;
896 break;
897 }
898 p++;
899 len++;
900 }
901
902 if (len < off) {
903 *eof = 1;
904 len = 0;
905 }
906 return len;
907}
908
909#if 0
910static int mf_getVmlinuxChunk(char *buffer, int *size, int offset, u64 side)
911{
912 struct vsp_cmd_data vsp_cmd;
913 int rc;
914 int len = *size;
915 dma_addr_t dma_addr;
916
917 dma_addr = dma_map_single(iSeries_vio_dev, buffer, len,
918 DMA_FROM_DEVICE);
919 memset(buffer, 0, len);
920 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
921 vsp_cmd.cmd = 32;
922 vsp_cmd.sub_data.kern.token = dma_addr;
923 vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
924 vsp_cmd.sub_data.kern.side = side;
925 vsp_cmd.sub_data.kern.offset = offset;
926 vsp_cmd.sub_data.kern.length = len;
927 mb();
928 rc = signal_vsp_instruction(&vsp_cmd);
929 if (rc == 0) {
930 if (vsp_cmd.result_code == 0)
931 *size = vsp_cmd.sub_data.length_out;
932 else
933 rc = -ENOMEM;
934 }
935
936 dma_unmap_single(iSeries_vio_dev, dma_addr, len, DMA_FROM_DEVICE);
937
938 return rc;
939}
940
941static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,
942 int count, int *eof, void *data)
943{
944 int sizeToGet = count;
945
946 if (!capable(CAP_SYS_ADMIN))
947 return -EACCES;
948
949 if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {
950 if (sizeToGet != 0) {
951 *start = page + off;
952 return sizeToGet;
953 }
954 *eof = 1;
955 return 0;
956 }
957 *eof = 1;
958 return 0;
959}
960#endif
961
962static int proc_mf_dump_side(char *page, char **start, off_t off,
963 int count, int *eof, void *data)
964{
965 int len;
966 char mf_current_side = ' ';
967 struct vsp_cmd_data vsp_cmd;
968
969 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
970 vsp_cmd.cmd = 2;
971 vsp_cmd.sub_data.ipl_type = 0;
972 mb();
973
974 if (signal_vsp_instruction(&vsp_cmd) == 0) {
975 if (vsp_cmd.result_code == 0) {
976 switch (vsp_cmd.sub_data.ipl_type) {
977 case 0: mf_current_side = 'A';
978 break;
979 case 1: mf_current_side = 'B';
980 break;
981 case 2: mf_current_side = 'C';
982 break;
983 default: mf_current_side = 'D';
984 break;
985 }
986 }
987 }
988
989 len = sprintf(page, "%c\n", mf_current_side);
990
991 if (len <= (off + count))
992 *eof = 1;
993 *start = page + off;
994 len -= off;
995 if (len > count)
996 len = count;
997 if (len < 0)
998 len = 0;
999 return len;
1000}
1001
1002static int proc_mf_change_side(struct file *file, const char __user *buffer,
1003 unsigned long count, void *data)
1004{
1005 char side;
1006 u64 newSide;
1007 struct vsp_cmd_data vsp_cmd;
1008
1009 if (!capable(CAP_SYS_ADMIN))
1010 return -EACCES;
1011
1012 if (count == 0)
1013 return 0;
1014
1015 if (get_user(side, buffer))
1016 return -EFAULT;
1017
1018 switch (side) {
1019 case 'A': newSide = 0;
1020 break;
1021 case 'B': newSide = 1;
1022 break;
1023 case 'C': newSide = 2;
1024 break;
1025 case 'D': newSide = 3;
1026 break;
1027 default:
1028 printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");
1029 return -EINVAL;
1030 }
1031
1032 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1033 vsp_cmd.sub_data.ipl_type = newSide;
1034 vsp_cmd.cmd = 10;
1035
1036 (void)signal_vsp_instruction(&vsp_cmd);
1037
1038 return count;
1039}
1040
1041#if 0
1042static void mf_getSrcHistory(char *buffer, int size)
1043{
1044 struct IplTypeReturnStuff return_stuff;
1045 struct pending_event *ev = new_pending_event();
1046 int rc = 0;
1047 char *pages[4];
1048
1049 pages[0] = kmalloc(4096, GFP_ATOMIC);
1050 pages[1] = kmalloc(4096, GFP_ATOMIC);
1051 pages[2] = kmalloc(4096, GFP_ATOMIC);
1052 pages[3] = kmalloc(4096, GFP_ATOMIC);
1053 if ((ev == NULL) || (pages[0] == NULL) || (pages[1] == NULL)
1054 || (pages[2] == NULL) || (pages[3] == NULL))
1055 return -ENOMEM;
1056
1057 return_stuff.xType = 0;
1058 return_stuff.xRc = 0;
1059 return_stuff.xDone = 0;
1060 ev->event.hp_lp_event.xSubtype = 6;
1061 ev->event.hp_lp_event.x.xSubtypeData =
1062 subtype_data('M', 'F', 'V', 'I');
1063 ev->event.data.vsp_cmd.xEvent = &return_stuff;
1064 ev->event.data.vsp_cmd.cmd = 4;
1065 ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
1066 ev->event.data.vsp_cmd.result_code = 0xFF;
1067 ev->event.data.vsp_cmd.reserved = 0;
Stephen Rothwell426c1a12005-10-14 14:51:42 +10001068 ev->event.data.vsp_cmd.sub_data.page[0] = iseries_hv_addr(pages[0]);
1069 ev->event.data.vsp_cmd.sub_data.page[1] = iseries_hv_addr(pages[1]);
1070 ev->event.data.vsp_cmd.sub_data.page[2] = iseries_hv_addr(pages[2]);
1071 ev->event.data.vsp_cmd.sub_data.page[3] = iseries_hv_addr(pages[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 mb();
1073 if (signal_event(ev) != 0)
1074 return;
1075
1076 while (return_stuff.xDone != 1)
1077 udelay(10);
1078 if (return_stuff.xRc == 0)
1079 memcpy(buffer, pages[0], size);
1080 kfree(pages[0]);
1081 kfree(pages[1]);
1082 kfree(pages[2]);
1083 kfree(pages[3]);
1084}
1085#endif
1086
1087static int proc_mf_dump_src(char *page, char **start, off_t off,
1088 int count, int *eof, void *data)
1089{
1090#if 0
1091 int len;
1092
1093 mf_getSrcHistory(page, count);
1094 len = count;
1095 len -= off;
1096 if (len < count) {
1097 *eof = 1;
1098 if (len <= 0)
1099 return 0;
1100 } else
1101 len = count;
1102 *start = page + off;
1103 return len;
1104#else
1105 return 0;
1106#endif
1107}
1108
1109static int proc_mf_change_src(struct file *file, const char __user *buffer,
1110 unsigned long count, void *data)
1111{
1112 char stkbuf[10];
1113
1114 if (!capable(CAP_SYS_ADMIN))
1115 return -EACCES;
1116
1117 if ((count < 4) && (count != 1)) {
1118 printk(KERN_ERR "mf_proc: invalid src\n");
1119 return -EINVAL;
1120 }
1121
1122 if (count > (sizeof(stkbuf) - 1))
1123 count = sizeof(stkbuf) - 1;
1124 if (copy_from_user(stkbuf, buffer, count))
1125 return -EFAULT;
1126
1127 if ((count == 1) && (*stkbuf == '\0'))
1128 mf_clear_src();
1129 else
1130 mf_display_src(*(u32 *)stkbuf);
1131
1132 return count;
1133}
1134
1135static int proc_mf_change_cmdline(struct file *file, const char __user *buffer,
1136 unsigned long count, void *data)
1137{
1138 struct vsp_cmd_data vsp_cmd;
1139 dma_addr_t dma_addr;
1140 char *page;
1141 int ret = -EACCES;
1142
1143 if (!capable(CAP_SYS_ADMIN))
1144 goto out;
1145
1146 dma_addr = 0;
1147 page = dma_alloc_coherent(iSeries_vio_dev, count, &dma_addr,
1148 GFP_ATOMIC);
1149 ret = -ENOMEM;
1150 if (page == NULL)
1151 goto out;
1152
1153 ret = -EFAULT;
1154 if (copy_from_user(page, buffer, count))
1155 goto out_free;
1156
1157 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1158 vsp_cmd.cmd = 31;
1159 vsp_cmd.sub_data.kern.token = dma_addr;
1160 vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1161 vsp_cmd.sub_data.kern.side = (u64)data;
1162 vsp_cmd.sub_data.kern.length = count;
1163 mb();
1164 (void)signal_vsp_instruction(&vsp_cmd);
1165 ret = count;
1166
1167out_free:
1168 dma_free_coherent(iSeries_vio_dev, count, page, dma_addr);
1169out:
1170 return ret;
1171}
1172
1173static ssize_t proc_mf_change_vmlinux(struct file *file,
1174 const char __user *buf,
1175 size_t count, loff_t *ppos)
1176{
1177 struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
1178 ssize_t rc;
1179 dma_addr_t dma_addr;
1180 char *page;
1181 struct vsp_cmd_data vsp_cmd;
1182
1183 rc = -EACCES;
1184 if (!capable(CAP_SYS_ADMIN))
1185 goto out;
1186
1187 dma_addr = 0;
1188 page = dma_alloc_coherent(iSeries_vio_dev, count, &dma_addr,
1189 GFP_ATOMIC);
1190 rc = -ENOMEM;
1191 if (page == NULL) {
1192 printk(KERN_ERR "mf.c: couldn't allocate memory to set vmlinux chunk\n");
1193 goto out;
1194 }
1195 rc = -EFAULT;
1196 if (copy_from_user(page, buf, count))
1197 goto out_free;
1198
1199 memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1200 vsp_cmd.cmd = 30;
1201 vsp_cmd.sub_data.kern.token = dma_addr;
1202 vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1203 vsp_cmd.sub_data.kern.side = (u64)dp->data;
1204 vsp_cmd.sub_data.kern.offset = *ppos;
1205 vsp_cmd.sub_data.kern.length = count;
1206 mb();
1207 rc = signal_vsp_instruction(&vsp_cmd);
1208 if (rc)
1209 goto out_free;
1210 rc = -ENOMEM;
1211 if (vsp_cmd.result_code != 0)
1212 goto out_free;
1213
1214 *ppos += count;
1215 rc = count;
1216out_free:
1217 dma_free_coherent(iSeries_vio_dev, count, page, dma_addr);
1218out:
1219 return rc;
1220}
1221
1222static struct file_operations proc_vmlinux_operations = {
1223 .write = proc_mf_change_vmlinux,
1224};
1225
1226static int __init mf_proc_init(void)
1227{
1228 struct proc_dir_entry *mf_proc_root;
1229 struct proc_dir_entry *ent;
1230 struct proc_dir_entry *mf;
1231 char name[2];
1232 int i;
1233
1234 mf_proc_root = proc_mkdir("iSeries/mf", NULL);
1235 if (!mf_proc_root)
1236 return 1;
1237
1238 name[1] = '\0';
1239 for (i = 0; i < 4; i++) {
1240 name[0] = 'A' + i;
1241 mf = proc_mkdir(name, mf_proc_root);
1242 if (!mf)
1243 return 1;
1244
1245 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
1246 if (!ent)
1247 return 1;
1248 ent->nlink = 1;
1249 ent->data = (void *)(long)i;
1250 ent->read_proc = proc_mf_dump_cmdline;
1251 ent->write_proc = proc_mf_change_cmdline;
1252
1253 if (i == 3) /* no vmlinux entry for 'D' */
1254 continue;
1255
1256 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
1257 if (!ent)
1258 return 1;
1259 ent->nlink = 1;
1260 ent->data = (void *)(long)i;
1261 ent->proc_fops = &proc_vmlinux_operations;
1262 }
1263
1264 ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1265 if (!ent)
1266 return 1;
1267 ent->nlink = 1;
1268 ent->data = (void *)0;
1269 ent->read_proc = proc_mf_dump_side;
1270 ent->write_proc = proc_mf_change_side;
1271
1272 ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1273 if (!ent)
1274 return 1;
1275 ent->nlink = 1;
1276 ent->data = (void *)0;
1277 ent->read_proc = proc_mf_dump_src;
1278 ent->write_proc = proc_mf_change_src;
1279
1280 return 0;
1281}
1282
1283__initcall(mf_proc_init);
1284
1285#endif /* CONFIG_PROC_FS */
Stephen Rothwellc8b84972005-09-27 18:44:42 +10001286
1287/*
1288 * Get the RTC from the virtual service processor
1289 * This requires flowing LpEvents to the primary partition
1290 */
1291void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
1292{
1293 if (piranha_simulator)
1294 return;
1295
1296 mf_get_rtc(rtc_tm);
1297 rtc_tm->tm_mon--;
1298}
1299
1300/*
1301 * Set the RTC in the virtual service processor
1302 * This requires flowing LpEvents to the primary partition
1303 */
1304int iSeries_set_rtc_time(struct rtc_time *tm)
1305{
1306 mf_set_rtc(tm);
1307 return 0;
1308}
1309
1310void iSeries_get_boot_time(struct rtc_time *tm)
1311{
1312 if (piranha_simulator)
1313 return;
1314
1315 mf_get_boot_rtc(tm);
1316 tm->tm_mon -= 1;
1317}