powerpc: move iSeries_setup.[ch] and mf.c into platforms/iseries
[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
36 #include <asm/time.h>
37 #include <asm/uaccess.h>
38 #include <asm/paca.h>
39 #include <asm/iSeries/vio.h>
40 #include <asm/iSeries/mf.h>
41 #include <asm/iSeries/HvLpConfig.h>
42 #include <asm/iSeries/ItLpQueue.h>
43
44 #include "setup.h"
45
46 extern int piranha_simulator;
47
48 /*
49  * This is the structure layout for the Machine Facilites LPAR event
50  * flows.
51  */
52 struct vsp_cmd_data {
53         u64 token;
54         u16 cmd;
55         HvLpIndex lp_index;
56         u8 result_code;
57         u32 reserved;
58         union {
59                 u64 state;      /* GetStateOut */
60                 u64 ipl_type;   /* GetIplTypeOut, Function02SelectIplTypeIn */
61                 u64 ipl_mode;   /* GetIplModeOut, Function02SelectIplModeIn */
62                 u64 page[4];    /* GetSrcHistoryIn */
63                 u64 flag;       /* GetAutoIplWhenPrimaryIplsOut,
64                                    SetAutoIplWhenPrimaryIplsIn,
65                                    WhiteButtonPowerOffIn,
66                                    Function08FastPowerOffIn,
67                                    IsSpcnRackPowerIncompleteOut */
68                 struct {
69                         u64 token;
70                         u64 address_type;
71                         u64 side;
72                         u32 length;
73                         u32 offset;
74                 } kern;         /* SetKernelImageIn, GetKernelImageIn,
75                                    SetKernelCmdLineIn, GetKernelCmdLineIn */
76                 u32 length_out; /* GetKernelImageOut, GetKernelCmdLineOut */
77                 u8 reserved[80];
78         } sub_data;
79 };
80
81 struct vsp_rsp_data {
82         struct completion com;
83         struct vsp_cmd_data *response;
84 };
85
86 struct alloc_data {
87         u16 size;
88         u16 type;
89         u32 count;
90         u16 reserved1;
91         u8 reserved2;
92         HvLpIndex target_lp;
93 };
94
95 struct ce_msg_data;
96
97 typedef void (*ce_msg_comp_hdlr)(void *token, struct ce_msg_data *vsp_cmd_rsp);
98
99 struct ce_msg_comp_data {
100         ce_msg_comp_hdlr handler;
101         void *token;
102 };
103
104 struct ce_msg_data {
105         u8 ce_msg[12];
106         char reserved[4];
107         struct ce_msg_comp_data *completion;
108 };
109
110 struct io_mf_lp_event {
111         struct HvLpEvent hp_lp_event;
112         u16 subtype_result_code;
113         u16 reserved1;
114         u32 reserved2;
115         union {
116                 struct alloc_data alloc;
117                 struct ce_msg_data ce_msg;
118                 struct vsp_cmd_data vsp_cmd;
119         } data;
120 };
121
122 #define subtype_data(a, b, c, d)        \
123                 (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
124
125 /*
126  * All outgoing event traffic is kept on a FIFO queue.  The first
127  * pointer points to the one that is outstanding, and all new
128  * requests get stuck on the end.  Also, we keep a certain number of
129  * preallocated pending events so that we can operate very early in
130  * the boot up sequence (before kmalloc is ready).
131  */
132 struct pending_event {
133         struct pending_event *next;
134         struct io_mf_lp_event event;
135         MFCompleteHandler hdlr;
136         char dma_data[72];
137         unsigned dma_data_length;
138         unsigned remote_address;
139 };
140 static spinlock_t pending_event_spinlock;
141 static struct pending_event *pending_event_head;
142 static struct pending_event *pending_event_tail;
143 static struct pending_event *pending_event_avail;
144 static struct pending_event pending_event_prealloc[16];
145
146 /*
147  * Put a pending event onto the available queue, so it can get reused.
148  * Attention! You must have the pending_event_spinlock before calling!
149  */
150 static void free_pending_event(struct pending_event *ev)
151 {
152         if (ev != NULL) {
153                 ev->next = pending_event_avail;
154                 pending_event_avail = ev;
155         }
156 }
157
158 /*
159  * Enqueue the outbound event onto the stack.  If the queue was
160  * empty to begin with, we must also issue it via the Hypervisor
161  * interface.  There is a section of code below that will touch
162  * the first stack pointer without the protection of the pending_event_spinlock.
163  * This is OK, because we know that nobody else will be modifying
164  * the first pointer when we do this.
165  */
166 static int signal_event(struct pending_event *ev)
167 {
168         int rc = 0;
169         unsigned long flags;
170         int go = 1;
171         struct pending_event *ev1;
172         HvLpEvent_Rc hv_rc;
173
174         /* enqueue the event */
175         if (ev != NULL) {
176                 ev->next = NULL;
177                 spin_lock_irqsave(&pending_event_spinlock, flags);
178                 if (pending_event_head == NULL)
179                         pending_event_head = ev;
180                 else {
181                         go = 0;
182                         pending_event_tail->next = ev;
183                 }
184                 pending_event_tail = ev;
185                 spin_unlock_irqrestore(&pending_event_spinlock, flags);
186         }
187
188         /* send the event */
189         while (go) {
190                 go = 0;
191
192                 /* any DMA data to send beforehand? */
193                 if (pending_event_head->dma_data_length > 0)
194                         HvCallEvent_dmaToSp(pending_event_head->dma_data,
195                                         pending_event_head->remote_address,
196                                         pending_event_head->dma_data_length,
197                                         HvLpDma_Direction_LocalToRemote);
198
199                 hv_rc = HvCallEvent_signalLpEvent(
200                                 &pending_event_head->event.hp_lp_event);
201                 if (hv_rc != HvLpEvent_Rc_Good) {
202                         printk(KERN_ERR "mf.c: HvCallEvent_signalLpEvent() "
203                                         "failed with %d\n", (int)hv_rc);
204
205                         spin_lock_irqsave(&pending_event_spinlock, flags);
206                         ev1 = pending_event_head;
207                         pending_event_head = pending_event_head->next;
208                         if (pending_event_head != NULL)
209                                 go = 1;
210                         spin_unlock_irqrestore(&pending_event_spinlock, flags);
211
212                         if (ev1 == ev)
213                                 rc = -EIO;
214                         else if (ev1->hdlr != NULL)
215                                 (*ev1->hdlr)((void *)ev1->event.hp_lp_event.xCorrelationToken, -EIO);
216
217                         spin_lock_irqsave(&pending_event_spinlock, flags);
218                         free_pending_event(ev1);
219                         spin_unlock_irqrestore(&pending_event_spinlock, flags);
220                 }
221         }
222
223         return rc;
224 }
225
226 /*
227  * Allocate a new pending_event structure, and initialize it.
228  */
229 static struct pending_event *new_pending_event(void)
230 {
231         struct pending_event *ev = NULL;
232         HvLpIndex primary_lp = HvLpConfig_getPrimaryLpIndex();
233         unsigned long flags;
234         struct HvLpEvent *hev;
235
236         spin_lock_irqsave(&pending_event_spinlock, flags);
237         if (pending_event_avail != NULL) {
238                 ev = pending_event_avail;
239                 pending_event_avail = pending_event_avail->next;
240         }
241         spin_unlock_irqrestore(&pending_event_spinlock, flags);
242         if (ev == NULL) {
243                 ev = kmalloc(sizeof(struct pending_event), GFP_ATOMIC);
244                 if (ev == NULL) {
245                         printk(KERN_ERR "mf.c: unable to kmalloc %ld bytes\n",
246                                         sizeof(struct pending_event));
247                         return NULL;
248                 }
249         }
250         memset(ev, 0, sizeof(struct pending_event));
251         hev = &ev->event.hp_lp_event;
252         hev->xFlags.xValid = 1;
253         hev->xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
254         hev->xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
255         hev->xFlags.xFunction = HvLpEvent_Function_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_proc(1, 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, struct pt_regs *regs)
517 {
518         if ((event != NULL) && (event->xType == HvLpEvent_Type_MachineFac)) {
519                 switch(event->xFlags.xFunction) {
520                 case HvLpEvent_Function_Ack:
521                         handle_ack((struct io_mf_lp_event *)event);
522                         break;
523                 case HvLpEvent_Function_Int:
524                         handle_int((struct io_mf_lp_event *)event);
525                         break;
526                 default:
527                         printk(KERN_ERR "mf.c: non ack/int event received\n");
528                         break;
529                 }
530         } else
531                 printk(KERN_ERR "mf.c: alien event received\n");
532 }
533
534 /*
535  * Global kernel interface to allocate and seed events into the
536  * Hypervisor.
537  */
538 void mf_allocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
539                 unsigned size, unsigned count, MFCompleteHandler hdlr,
540                 void *user_token)
541 {
542         struct pending_event *ev = new_pending_event();
543         int rc;
544
545         if (ev == NULL) {
546                 rc = -ENOMEM;
547         } else {
548                 ev->event.hp_lp_event.xSubtype = 4;
549                 ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
550                 ev->event.hp_lp_event.x.xSubtypeData =
551                         subtype_data('M', 'F', 'M', 'A');
552                 ev->event.data.alloc.target_lp = target_lp;
553                 ev->event.data.alloc.type = type;
554                 ev->event.data.alloc.size = size;
555                 ev->event.data.alloc.count = count;
556                 ev->hdlr = hdlr;
557                 rc = signal_event(ev);
558         }
559         if ((rc != 0) && (hdlr != NULL))
560                 (*hdlr)(user_token, rc);
561 }
562 EXPORT_SYMBOL(mf_allocate_lp_events);
563
564 /*
565  * Global kernel interface to unseed and deallocate events already in
566  * Hypervisor.
567  */
568 void mf_deallocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
569                 unsigned count, MFCompleteHandler hdlr, void *user_token)
570 {
571         struct pending_event *ev = new_pending_event();
572         int rc;
573
574         if (ev == NULL)
575                 rc = -ENOMEM;
576         else {
577                 ev->event.hp_lp_event.xSubtype = 5;
578                 ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
579                 ev->event.hp_lp_event.x.xSubtypeData =
580                         subtype_data('M', 'F', 'M', 'D');
581                 ev->event.data.alloc.target_lp = target_lp;
582                 ev->event.data.alloc.type = type;
583                 ev->event.data.alloc.count = count;
584                 ev->hdlr = hdlr;
585                 rc = signal_event(ev);
586         }
587         if ((rc != 0) && (hdlr != NULL))
588                 (*hdlr)(user_token, rc);
589 }
590 EXPORT_SYMBOL(mf_deallocate_lp_events);
591
592 /*
593  * Global kernel interface to tell the VSP object in the primary
594  * partition to power this partition off.
595  */
596 void mf_power_off(void)
597 {
598         printk(KERN_INFO "mf.c: Down it goes...\n");
599         signal_ce_msg_simple(0x4d, NULL);
600         for (;;)
601                 ;
602 }
603
604 /*
605  * Global kernel interface to tell the VSP object in the primary
606  * partition to reboot this partition.
607  */
608 void mf_reboot(void)
609 {
610         printk(KERN_INFO "mf.c: Preparing to bounce...\n");
611         signal_ce_msg_simple(0x4e, NULL);
612         for (;;)
613                 ;
614 }
615
616 /*
617  * Display a single word SRC onto the VSP control panel.
618  */
619 void mf_display_src(u32 word)
620 {
621         u8 ce[12];
622
623         memset(ce, 0, sizeof(ce));
624         ce[3] = 0x4a;
625         ce[7] = 0x01;
626         ce[8] = word >> 24;
627         ce[9] = word >> 16;
628         ce[10] = word >> 8;
629         ce[11] = word;
630         signal_ce_msg(ce, NULL);
631 }
632
633 /*
634  * Display a single word SRC of the form "PROGXXXX" on the VSP control panel.
635  */
636 void mf_display_progress(u16 value)
637 {
638         u8 ce[12];
639         u8 src[72];
640
641         memcpy(ce, "\x00\x00\x04\x4A\x00\x00\x00\x48\x00\x00\x00\x00", 12);
642         memcpy(src, "\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
643                 "\x00\x00\x00\x00\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\x00PROGxxxx                        ",
646                 72);
647         src[6] = value >> 8;
648         src[7] = value & 255;
649         src[44] = "0123456789ABCDEF"[(value >> 12) & 15];
650         src[45] = "0123456789ABCDEF"[(value >> 8) & 15];
651         src[46] = "0123456789ABCDEF"[(value >> 4) & 15];
652         src[47] = "0123456789ABCDEF"[value & 15];
653         dma_and_signal_ce_msg(ce, NULL, src, sizeof(src), 9 * 64 * 1024);
654 }
655
656 /*
657  * Clear the VSP control panel.  Used to "erase" an SRC that was
658  * previously displayed.
659  */
660 void mf_clear_src(void)
661 {
662         signal_ce_msg_simple(0x4b, NULL);
663 }
664
665 /*
666  * Initialization code here.
667  */
668 void mf_init(void)
669 {
670         int i;
671
672         /* initialize */
673         spin_lock_init(&pending_event_spinlock);
674         for (i = 0;
675              i < sizeof(pending_event_prealloc) / sizeof(*pending_event_prealloc);
676              ++i)
677                 free_pending_event(&pending_event_prealloc[i]);
678         HvLpEvent_registerHandler(HvLpEvent_Type_MachineFac, &hv_handler);
679
680         /* virtual continue ack */
681         signal_ce_msg_simple(0x57, NULL);
682
683         /* initialization complete */
684         printk(KERN_NOTICE "mf.c: iSeries Linux LPAR Machine Facilities "
685                         "initialized\n");
686 }
687
688 struct rtc_time_data {
689         struct completion com;
690         struct ce_msg_data ce_msg;
691         int rc;
692 };
693
694 static void get_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
695 {
696         struct rtc_time_data *rtc = token;
697
698         memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
699         rtc->rc = 0;
700         complete(&rtc->com);
701 }
702
703 static int rtc_set_tm(int rc, u8 *ce_msg, struct rtc_time *tm)
704 {
705         tm->tm_wday = 0;
706         tm->tm_yday = 0;
707         tm->tm_isdst = 0;
708         if (rc) {
709                 tm->tm_sec = 0;
710                 tm->tm_min = 0;
711                 tm->tm_hour = 0;
712                 tm->tm_mday = 15;
713                 tm->tm_mon = 5;
714                 tm->tm_year = 52;
715                 return rc;
716         }
717
718         if ((ce_msg[2] == 0xa9) ||
719             (ce_msg[2] == 0xaf)) {
720                 /* TOD clock is not set */
721                 tm->tm_sec = 1;
722                 tm->tm_min = 1;
723                 tm->tm_hour = 1;
724                 tm->tm_mday = 10;
725                 tm->tm_mon = 8;
726                 tm->tm_year = 71;
727                 mf_set_rtc(tm);
728         }
729         {
730                 u8 year = ce_msg[5];
731                 u8 sec = ce_msg[6];
732                 u8 min = ce_msg[7];
733                 u8 hour = ce_msg[8];
734                 u8 day = ce_msg[10];
735                 u8 mon = ce_msg[11];
736
737                 BCD_TO_BIN(sec);
738                 BCD_TO_BIN(min);
739                 BCD_TO_BIN(hour);
740                 BCD_TO_BIN(day);
741                 BCD_TO_BIN(mon);
742                 BCD_TO_BIN(year);
743
744                 if (year <= 69)
745                         year += 100;
746
747                 tm->tm_sec = sec;
748                 tm->tm_min = min;
749                 tm->tm_hour = hour;
750                 tm->tm_mday = day;
751                 tm->tm_mon = mon;
752                 tm->tm_year = year;
753         }
754
755         return 0;
756 }
757
758 int mf_get_rtc(struct rtc_time *tm)
759 {
760         struct ce_msg_comp_data ce_complete;
761         struct rtc_time_data rtc_data;
762         int rc;
763
764         memset(&ce_complete, 0, sizeof(ce_complete));
765         memset(&rtc_data, 0, sizeof(rtc_data));
766         init_completion(&rtc_data.com);
767         ce_complete.handler = &get_rtc_time_complete;
768         ce_complete.token = &rtc_data;
769         rc = signal_ce_msg_simple(0x40, &ce_complete);
770         if (rc)
771                 return rc;
772         wait_for_completion(&rtc_data.com);
773         return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
774 }
775
776 struct boot_rtc_time_data {
777         int busy;
778         struct ce_msg_data ce_msg;
779         int rc;
780 };
781
782 static void get_boot_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
783 {
784         struct boot_rtc_time_data *rtc = token;
785
786         memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
787         rtc->rc = 0;
788         rtc->busy = 0;
789 }
790
791 int mf_get_boot_rtc(struct rtc_time *tm)
792 {
793         struct ce_msg_comp_data ce_complete;
794         struct boot_rtc_time_data rtc_data;
795         int rc;
796
797         memset(&ce_complete, 0, sizeof(ce_complete));
798         memset(&rtc_data, 0, sizeof(rtc_data));
799         rtc_data.busy = 1;
800         ce_complete.handler = &get_boot_rtc_time_complete;
801         ce_complete.token = &rtc_data;
802         rc = signal_ce_msg_simple(0x40, &ce_complete);
803         if (rc)
804                 return rc;
805         /* We need to poll here as we are not yet taking interrupts */
806         while (rtc_data.busy) {
807                 if (hvlpevent_is_pending())
808                         process_hvlpevents(NULL);
809         }
810         return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
811 }
812
813 int mf_set_rtc(struct rtc_time *tm)
814 {
815         char ce_time[12];
816         u8 day, mon, hour, min, sec, y1, y2;
817         unsigned year;
818
819         year = 1900 + tm->tm_year;
820         y1 = year / 100;
821         y2 = year % 100;
822
823         sec = tm->tm_sec;
824         min = tm->tm_min;
825         hour = tm->tm_hour;
826         day = tm->tm_mday;
827         mon = tm->tm_mon + 1;
828
829         BIN_TO_BCD(sec);
830         BIN_TO_BCD(min);
831         BIN_TO_BCD(hour);
832         BIN_TO_BCD(mon);
833         BIN_TO_BCD(day);
834         BIN_TO_BCD(y1);
835         BIN_TO_BCD(y2);
836
837         memset(ce_time, 0, sizeof(ce_time));
838         ce_time[3] = 0x41;
839         ce_time[4] = y1;
840         ce_time[5] = y2;
841         ce_time[6] = sec;
842         ce_time[7] = min;
843         ce_time[8] = hour;
844         ce_time[10] = day;
845         ce_time[11] = mon;
846
847         return signal_ce_msg(ce_time, NULL);
848 }
849
850 #ifdef CONFIG_PROC_FS
851
852 static int proc_mf_dump_cmdline(char *page, char **start, off_t off,
853                 int count, int *eof, void *data)
854 {
855         int len;
856         char *p;
857         struct vsp_cmd_data vsp_cmd;
858         int rc;
859         dma_addr_t dma_addr;
860
861         /* The HV appears to return no more than 256 bytes of command line */
862         if (off >= 256)
863                 return 0;
864         if ((off + count) > 256)
865                 count = 256 - off;
866
867         dma_addr = dma_map_single(iSeries_vio_dev, page, off + count,
868                         DMA_FROM_DEVICE);
869         if (dma_mapping_error(dma_addr))
870                 return -ENOMEM;
871         memset(page, 0, off + count);
872         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
873         vsp_cmd.cmd = 33;
874         vsp_cmd.sub_data.kern.token = dma_addr;
875         vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
876         vsp_cmd.sub_data.kern.side = (u64)data;
877         vsp_cmd.sub_data.kern.length = off + count;
878         mb();
879         rc = signal_vsp_instruction(&vsp_cmd);
880         dma_unmap_single(iSeries_vio_dev, dma_addr, off + count,
881                         DMA_FROM_DEVICE);
882         if (rc)
883                 return rc;
884         if (vsp_cmd.result_code != 0)
885                 return -ENOMEM;
886         p = page;
887         len = 0;
888         while (len < (off + count)) {
889                 if ((*p == '\0') || (*p == '\n')) {
890                         if (*p == '\0')
891                                 *p = '\n';
892                         p++;
893                         len++;
894                         *eof = 1;
895                         break;
896                 }
897                 p++;
898                 len++;
899         }
900
901         if (len < off) {
902                 *eof = 1;
903                 len = 0;
904         }
905         return len;
906 }
907
908 #if 0
909 static int mf_getVmlinuxChunk(char *buffer, int *size, int offset, u64 side)
910 {
911         struct vsp_cmd_data vsp_cmd;
912         int rc;
913         int len = *size;
914         dma_addr_t dma_addr;
915
916         dma_addr = dma_map_single(iSeries_vio_dev, buffer, len,
917                         DMA_FROM_DEVICE);
918         memset(buffer, 0, len);
919         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
920         vsp_cmd.cmd = 32;
921         vsp_cmd.sub_data.kern.token = dma_addr;
922         vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
923         vsp_cmd.sub_data.kern.side = side;
924         vsp_cmd.sub_data.kern.offset = offset;
925         vsp_cmd.sub_data.kern.length = len;
926         mb();
927         rc = signal_vsp_instruction(&vsp_cmd);
928         if (rc == 0) {
929                 if (vsp_cmd.result_code == 0)
930                         *size = vsp_cmd.sub_data.length_out;
931                 else
932                         rc = -ENOMEM;
933         }
934
935         dma_unmap_single(iSeries_vio_dev, dma_addr, len, DMA_FROM_DEVICE);
936
937         return rc;
938 }
939
940 static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,
941                 int count, int *eof, void *data)
942 {
943         int sizeToGet = count;
944
945         if (!capable(CAP_SYS_ADMIN))
946                 return -EACCES;
947
948         if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {
949                 if (sizeToGet != 0) {
950                         *start = page + off;
951                         return sizeToGet;
952                 }
953                 *eof = 1;
954                 return 0;
955         }
956         *eof = 1;
957         return 0;
958 }
959 #endif
960
961 static int proc_mf_dump_side(char *page, char **start, off_t off,
962                 int count, int *eof, void *data)
963 {
964         int len;
965         char mf_current_side = ' ';
966         struct vsp_cmd_data vsp_cmd;
967
968         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
969         vsp_cmd.cmd = 2;
970         vsp_cmd.sub_data.ipl_type = 0;
971         mb();
972
973         if (signal_vsp_instruction(&vsp_cmd) == 0) {
974                 if (vsp_cmd.result_code == 0) {
975                         switch (vsp_cmd.sub_data.ipl_type) {
976                         case 0: mf_current_side = 'A';
977                                 break;
978                         case 1: mf_current_side = 'B';
979                                 break;
980                         case 2: mf_current_side = 'C';
981                                 break;
982                         default:        mf_current_side = 'D';
983                                 break;
984                         }
985                 }
986         }
987
988         len = sprintf(page, "%c\n", mf_current_side);
989
990         if (len <= (off + count))
991                 *eof = 1;
992         *start = page + off;
993         len -= off;
994         if (len > count)
995                 len = count;
996         if (len < 0)
997                 len = 0;
998         return len;
999 }
1000
1001 static int proc_mf_change_side(struct file *file, const char __user *buffer,
1002                 unsigned long count, void *data)
1003 {
1004         char side;
1005         u64 newSide;
1006         struct vsp_cmd_data vsp_cmd;
1007
1008         if (!capable(CAP_SYS_ADMIN))
1009                 return -EACCES;
1010
1011         if (count == 0)
1012                 return 0;
1013
1014         if (get_user(side, buffer))
1015                 return -EFAULT;
1016
1017         switch (side) {
1018         case 'A':       newSide = 0;
1019                         break;
1020         case 'B':       newSide = 1;
1021                         break;
1022         case 'C':       newSide = 2;
1023                         break;
1024         case 'D':       newSide = 3;
1025                         break;
1026         default:
1027                 printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");
1028                 return -EINVAL;
1029         }
1030
1031         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1032         vsp_cmd.sub_data.ipl_type = newSide;
1033         vsp_cmd.cmd = 10;
1034
1035         (void)signal_vsp_instruction(&vsp_cmd);
1036
1037         return count;
1038 }
1039
1040 #if 0
1041 static void mf_getSrcHistory(char *buffer, int size)
1042 {
1043         struct IplTypeReturnStuff return_stuff;
1044         struct pending_event *ev = new_pending_event();
1045         int rc = 0;
1046         char *pages[4];
1047
1048         pages[0] = kmalloc(4096, GFP_ATOMIC);
1049         pages[1] = kmalloc(4096, GFP_ATOMIC);
1050         pages[2] = kmalloc(4096, GFP_ATOMIC);
1051         pages[3] = kmalloc(4096, GFP_ATOMIC);
1052         if ((ev == NULL) || (pages[0] == NULL) || (pages[1] == NULL)
1053                          || (pages[2] == NULL) || (pages[3] == NULL))
1054                 return -ENOMEM;
1055
1056         return_stuff.xType = 0;
1057         return_stuff.xRc = 0;
1058         return_stuff.xDone = 0;
1059         ev->event.hp_lp_event.xSubtype = 6;
1060         ev->event.hp_lp_event.x.xSubtypeData =
1061                 subtype_data('M', 'F', 'V', 'I');
1062         ev->event.data.vsp_cmd.xEvent = &return_stuff;
1063         ev->event.data.vsp_cmd.cmd = 4;
1064         ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
1065         ev->event.data.vsp_cmd.result_code = 0xFF;
1066         ev->event.data.vsp_cmd.reserved = 0;
1067         ev->event.data.vsp_cmd.sub_data.page[0] = ISERIES_HV_ADDR(pages[0]);
1068         ev->event.data.vsp_cmd.sub_data.page[1] = ISERIES_HV_ADDR(pages[1]);
1069         ev->event.data.vsp_cmd.sub_data.page[2] = ISERIES_HV_ADDR(pages[2]);
1070         ev->event.data.vsp_cmd.sub_data.page[3] = ISERIES_HV_ADDR(pages[3]);
1071         mb();
1072         if (signal_event(ev) != 0)
1073                 return;
1074
1075         while (return_stuff.xDone != 1)
1076                 udelay(10);
1077         if (return_stuff.xRc == 0)
1078                 memcpy(buffer, pages[0], size);
1079         kfree(pages[0]);
1080         kfree(pages[1]);
1081         kfree(pages[2]);
1082         kfree(pages[3]);
1083 }
1084 #endif
1085
1086 static int proc_mf_dump_src(char *page, char **start, off_t off,
1087                 int count, int *eof, void *data)
1088 {
1089 #if 0
1090         int len;
1091
1092         mf_getSrcHistory(page, count);
1093         len = count;
1094         len -= off;
1095         if (len < count) {
1096                 *eof = 1;
1097                 if (len <= 0)
1098                         return 0;
1099         } else
1100                 len = count;
1101         *start = page + off;
1102         return len;
1103 #else
1104         return 0;
1105 #endif
1106 }
1107
1108 static int proc_mf_change_src(struct file *file, const char __user *buffer,
1109                 unsigned long count, void *data)
1110 {
1111         char stkbuf[10];
1112
1113         if (!capable(CAP_SYS_ADMIN))
1114                 return -EACCES;
1115
1116         if ((count < 4) && (count != 1)) {
1117                 printk(KERN_ERR "mf_proc: invalid src\n");
1118                 return -EINVAL;
1119         }
1120
1121         if (count > (sizeof(stkbuf) - 1))
1122                 count = sizeof(stkbuf) - 1;
1123         if (copy_from_user(stkbuf, buffer, count))
1124                 return -EFAULT;
1125
1126         if ((count == 1) && (*stkbuf == '\0'))
1127                 mf_clear_src();
1128         else
1129                 mf_display_src(*(u32 *)stkbuf);
1130
1131         return count;
1132 }
1133
1134 static int proc_mf_change_cmdline(struct file *file, const char __user *buffer,
1135                 unsigned long count, void *data)
1136 {
1137         struct vsp_cmd_data vsp_cmd;
1138         dma_addr_t dma_addr;
1139         char *page;
1140         int ret = -EACCES;
1141
1142         if (!capable(CAP_SYS_ADMIN))
1143                 goto out;
1144
1145         dma_addr = 0;
1146         page = dma_alloc_coherent(iSeries_vio_dev, count, &dma_addr,
1147                         GFP_ATOMIC);
1148         ret = -ENOMEM;
1149         if (page == NULL)
1150                 goto out;
1151
1152         ret = -EFAULT;
1153         if (copy_from_user(page, buffer, count))
1154                 goto out_free;
1155
1156         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1157         vsp_cmd.cmd = 31;
1158         vsp_cmd.sub_data.kern.token = dma_addr;
1159         vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1160         vsp_cmd.sub_data.kern.side = (u64)data;
1161         vsp_cmd.sub_data.kern.length = count;
1162         mb();
1163         (void)signal_vsp_instruction(&vsp_cmd);
1164         ret = count;
1165
1166 out_free:
1167         dma_free_coherent(iSeries_vio_dev, count, page, dma_addr);
1168 out:
1169         return ret;
1170 }
1171
1172 static ssize_t proc_mf_change_vmlinux(struct file *file,
1173                                       const char __user *buf,
1174                                       size_t count, loff_t *ppos)
1175 {
1176         struct proc_dir_entry *dp = PDE(file->f_dentry->d_inode);
1177         ssize_t rc;
1178         dma_addr_t dma_addr;
1179         char *page;
1180         struct vsp_cmd_data vsp_cmd;
1181
1182         rc = -EACCES;
1183         if (!capable(CAP_SYS_ADMIN))
1184                 goto out;
1185
1186         dma_addr = 0;
1187         page = dma_alloc_coherent(iSeries_vio_dev, count, &dma_addr,
1188                         GFP_ATOMIC);
1189         rc = -ENOMEM;
1190         if (page == NULL) {
1191                 printk(KERN_ERR "mf.c: couldn't allocate memory to set vmlinux chunk\n");
1192                 goto out;
1193         }
1194         rc = -EFAULT;
1195         if (copy_from_user(page, buf, count))
1196                 goto out_free;
1197
1198         memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1199         vsp_cmd.cmd = 30;
1200         vsp_cmd.sub_data.kern.token = dma_addr;
1201         vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1202         vsp_cmd.sub_data.kern.side = (u64)dp->data;
1203         vsp_cmd.sub_data.kern.offset = *ppos;
1204         vsp_cmd.sub_data.kern.length = count;
1205         mb();
1206         rc = signal_vsp_instruction(&vsp_cmd);
1207         if (rc)
1208                 goto out_free;
1209         rc = -ENOMEM;
1210         if (vsp_cmd.result_code != 0)
1211                 goto out_free;
1212
1213         *ppos += count;
1214         rc = count;
1215 out_free:
1216         dma_free_coherent(iSeries_vio_dev, count, page, dma_addr);
1217 out:
1218         return rc;
1219 }
1220
1221 static struct file_operations proc_vmlinux_operations = {
1222         .write          = proc_mf_change_vmlinux,
1223 };
1224
1225 static int __init mf_proc_init(void)
1226 {
1227         struct proc_dir_entry *mf_proc_root;
1228         struct proc_dir_entry *ent;
1229         struct proc_dir_entry *mf;
1230         char name[2];
1231         int i;
1232
1233         mf_proc_root = proc_mkdir("iSeries/mf", NULL);
1234         if (!mf_proc_root)
1235                 return 1;
1236
1237         name[1] = '\0';
1238         for (i = 0; i < 4; i++) {
1239                 name[0] = 'A' + i;
1240                 mf = proc_mkdir(name, mf_proc_root);
1241                 if (!mf)
1242                         return 1;
1243
1244                 ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
1245                 if (!ent)
1246                         return 1;
1247                 ent->nlink = 1;
1248                 ent->data = (void *)(long)i;
1249                 ent->read_proc = proc_mf_dump_cmdline;
1250                 ent->write_proc = proc_mf_change_cmdline;
1251
1252                 if (i == 3)     /* no vmlinux entry for 'D' */
1253                         continue;
1254
1255                 ent = create_proc_entry("vmlinux", S_IFREG|S_IWUSR, mf);
1256                 if (!ent)
1257                         return 1;
1258                 ent->nlink = 1;
1259                 ent->data = (void *)(long)i;
1260                 ent->proc_fops = &proc_vmlinux_operations;
1261         }
1262
1263         ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1264         if (!ent)
1265                 return 1;
1266         ent->nlink = 1;
1267         ent->data = (void *)0;
1268         ent->read_proc = proc_mf_dump_side;
1269         ent->write_proc = proc_mf_change_side;
1270
1271         ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1272         if (!ent)
1273                 return 1;
1274         ent->nlink = 1;
1275         ent->data = (void *)0;
1276         ent->read_proc = proc_mf_dump_src;
1277         ent->write_proc = proc_mf_change_src;
1278
1279         return 0;
1280 }
1281
1282 __initcall(mf_proc_init);
1283
1284 #endif /* CONFIG_PROC_FS */
1285
1286 /*
1287  * Get the RTC from the virtual service processor
1288  * This requires flowing LpEvents to the primary partition
1289  */
1290 void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
1291 {
1292         if (piranha_simulator)
1293                 return;
1294
1295         mf_get_rtc(rtc_tm);
1296         rtc_tm->tm_mon--;
1297 }
1298
1299 /*
1300  * Set the RTC in the virtual service processor
1301  * This requires flowing LpEvents to the primary partition
1302  */
1303 int iSeries_set_rtc_time(struct rtc_time *tm)
1304 {
1305         mf_set_rtc(tm);
1306         return 0;
1307 }
1308
1309 void iSeries_get_boot_time(struct rtc_time *tm)
1310 {
1311         if (piranha_simulator)
1312                 return;
1313
1314         mf_get_boot_rtc(tm);
1315         tm->tm_mon  -= 1;
1316 }