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