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