ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / pSeries_lpar.c
1 /*
2  * pSeries_lpar.c
3  * Copyright (C) 2001 Todd Inglett, IBM Corporation
4  *
5  * pSeries LPAR support.
6  * 
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/dma-mapping.h>
25 #include <asm/processor.h>
26 #include <asm/mmu.h>
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
29 #include <asm/machdep.h>
30 #include <asm/abs_addr.h>
31 #include <asm/mmu_context.h>
32 #include <asm/ppcdebug.h>
33 #include <asm/iommu.h>
34 #include <asm/naca.h>
35 #include <asm/tlbflush.h>
36 #include <asm/tlb.h>
37 #include <asm/hvcall.h>
38 #include <asm/prom.h>
39 #include <asm/abs_addr.h>
40 #include <asm/cputable.h>
41
42 /* in pSeries_hvCall.S */
43 EXPORT_SYMBOL(plpar_hcall);
44 EXPORT_SYMBOL(plpar_hcall_4out);
45 EXPORT_SYMBOL(plpar_hcall_norets);
46 EXPORT_SYMBOL(plpar_hcall_8arg_2ret);
47
48 long poll_pending(void)
49 {
50         unsigned long dummy;
51         return plpar_hcall(H_POLL_PENDING, 0, 0, 0, 0,
52                            &dummy, &dummy, &dummy);
53 }
54
55 long prod_processor(void)
56 {
57         plpar_hcall_norets(H_PROD);
58         return(0); 
59 }
60
61 long cede_processor(void)
62 {
63         plpar_hcall_norets(H_CEDE);
64         return(0); 
65 }
66
67 long register_vpa(unsigned long flags, unsigned long proc, unsigned long vpa)
68 {
69         plpar_hcall_norets(H_REGISTER_VPA, flags, proc, vpa);
70         return(0); 
71 }
72
73 long plpar_pte_remove(unsigned long flags,
74                       unsigned long ptex,
75                       unsigned long avpn,
76                       unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
77 {
78         unsigned long dummy;
79         return plpar_hcall(H_REMOVE, flags, ptex, avpn, 0,
80                            old_pteh_ret, old_ptel_ret, &dummy);
81 }
82
83 long plpar_pte_read(unsigned long flags,
84                     unsigned long ptex,
85                     unsigned long *old_pteh_ret, unsigned long *old_ptel_ret)
86 {
87         unsigned long dummy;
88         return plpar_hcall(H_READ, flags, ptex, 0, 0,
89                            old_pteh_ret, old_ptel_ret, &dummy);
90 }
91
92 long plpar_pte_protect(unsigned long flags,
93                        unsigned long ptex,
94                        unsigned long avpn)
95 {
96         return plpar_hcall_norets(H_PROTECT, flags, ptex, avpn);
97 }
98
99 long plpar_tce_get(unsigned long liobn,
100                    unsigned long ioba,
101                    unsigned long *tce_ret)
102 {
103         unsigned long dummy;
104         return plpar_hcall(H_GET_TCE, liobn, ioba, 0, 0,
105                            tce_ret, &dummy, &dummy);
106 }
107
108 long plpar_tce_put(unsigned long liobn,
109                    unsigned long ioba,
110                    unsigned long tceval)
111 {
112         return plpar_hcall_norets(H_PUT_TCE, liobn, ioba, tceval);
113 }
114
115 long plpar_get_term_char(unsigned long termno,
116                          unsigned long *len_ret,
117                          char *buf_ret)
118 {
119         unsigned long *lbuf = (unsigned long *)buf_ret;  /* ToDo: alignment? */
120         return plpar_hcall(H_GET_TERM_CHAR, termno, 0, 0, 0,
121                            len_ret, lbuf+0, lbuf+1);
122 }
123
124 long plpar_put_term_char(unsigned long termno,
125                          unsigned long len,
126                          const char *buffer)
127 {
128         unsigned long *lbuf = (unsigned long *)buffer;  /* ToDo: alignment? */
129         return plpar_hcall_norets(H_PUT_TERM_CHAR, termno, len, lbuf[0],
130                                   lbuf[1]);
131 }
132
133 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
134                 long npages, unsigned long uaddr,
135                 enum dma_data_direction direction)
136 {
137         u64 rc;
138         union tce_entry tce;
139
140         tce.te_word = 0;
141         tce.te_rpn = (virt_to_abs(uaddr)) >> PAGE_SHIFT;
142         tce.te_rdwr = 1;
143         if (direction != DMA_TO_DEVICE)
144                 tce.te_pciwr = 1;
145
146         while (npages--) {
147                 rc = plpar_tce_put((u64)tbl->it_index, 
148                                    (u64)tcenum << 12, 
149                                    tce.te_word );
150                 
151                 if (rc && printk_ratelimit()) {
152                         printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
153                         printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
154                         printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
155                         printk("\ttce val = 0x%lx\n", tce.te_word );
156                         show_stack(current, (unsigned long *)__get_SP());
157                 }
158                         
159                 tcenum++;
160                 tce.te_rpn++;
161         }
162 }
163
164 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
165 {
166         u64 rc;
167         union tce_entry tce;
168
169         tce.te_word = 0;
170
171         while (npages--) {
172                 rc = plpar_tce_put((u64)tbl->it_index, 
173                                    (u64)tcenum << 12,
174                                    tce.te_word );
175                 
176                 if (rc && printk_ratelimit()) {
177                         printk("tce_free_pSeriesLP: plpar_tce_put failed\n");
178                         printk("\trc      = %ld\n", rc);
179                         printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
180                         printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
181                         printk("\ttce val = 0x%lx\n", tce.te_word );
182                         show_stack(current, (unsigned long *)__get_SP());
183                 }
184                 
185                 tcenum++;
186         }
187 }
188
189 int vtermno;    /* virtual terminal# for udbg  */
190
191 static void udbg_putcLP(unsigned char c)
192 {
193         char buf[16];
194         unsigned long rc;
195
196         if (c == '\n')
197                 udbg_putcLP('\r');
198
199         buf[0] = c;
200         do {
201                 rc = plpar_put_term_char(vtermno, 1, buf);
202         } while(rc == H_Busy);
203 }
204
205 /* Buffered chars getc */
206 static long inbuflen;
207 static long inbuf[2];   /* must be 2 longs */
208
209 static int udbg_getc_pollLP(void)
210 {
211         /* The interface is tricky because it may return up to 16 chars.
212          * We save them statically for future calls to udbg_getc().
213          */
214         char ch, *buf = (char *)inbuf;
215         int i;
216         long rc;
217         if (inbuflen == 0) {
218                 /* get some more chars. */
219                 inbuflen = 0;
220                 rc = plpar_get_term_char(vtermno, &inbuflen, buf);
221                 if (rc != H_Success)
222                         inbuflen = 0;   /* otherwise inbuflen is garbage */
223         }
224         if (inbuflen <= 0 || inbuflen > 16) {
225                 /* Catch error case as well as other oddities (corruption) */
226                 inbuflen = 0;
227                 return -1;
228         }
229         ch = buf[0];
230         for (i = 1; i < inbuflen; i++)  /* shuffle them down. */
231                 buf[i-1] = buf[i];
232         inbuflen--;
233         return ch;
234 }
235
236 static unsigned char udbg_getcLP(void)
237 {
238         int ch;
239         for (;;) {
240                 ch = udbg_getc_pollLP();
241                 if (ch == -1) {
242                         /* This shouldn't be needed...but... */
243                         volatile unsigned long delay;
244                         for (delay=0; delay < 2000000; delay++)
245                                 ;
246                 } else {
247                         return ch;
248                 }
249         }
250 }
251
252 /* returns 0 if couldn't find or use /chosen/stdout as console */
253 static int find_udbg_vterm(void)
254 {
255         struct device_node *stdout_node;
256         u32 *termno;
257         char *name;
258         int found = 0;
259
260         /* find the boot console from /chosen/stdout */
261         if (!of_stdout_device) {
262                 printk(KERN_WARNING "couldn't get path from /chosen/stdout!\n");
263                 return found;
264         }
265         stdout_node = of_find_node_by_path(of_stdout_device);
266         if (!stdout_node) {
267                 printk(KERN_WARNING "couldn't find node from /chosen/stdout\n");
268                 return found;
269         }
270
271         /* now we have the stdout node; figure out what type of device it is. */
272         name = (char *)get_property(stdout_node, "name", 0);
273         if (!name) {
274                 printk(KERN_WARNING "stdout node missing 'name' property!\n");
275                 goto out;
276         }
277
278         if (strncmp(name, "vty", 3) == 0) {
279                 if (device_is_compatible(stdout_node, "hvterm1")) {
280                         termno = (u32 *)get_property(stdout_node, "reg", 0);
281                         if (termno) {
282                                 vtermno = termno[0];
283                                 ppc_md.udbg_putc = udbg_putcLP;
284                                 ppc_md.udbg_getc = udbg_getcLP;
285                                 ppc_md.udbg_getc_poll = udbg_getc_pollLP;
286                                 found = 1;
287                         }
288                 } else {
289                         /* XXX implement udbg_putcLP_vtty for hvterm-protocol1 case */
290                         printk(KERN_WARNING "%s doesn't speak hvterm1; "
291                                         "can't print udbg messages\n", of_stdout_device);
292                 }
293         } else if (strncmp(name, "serial", 6)) {
294                 /* XXX fix ISA serial console */
295                 printk(KERN_WARNING "serial stdout on LPAR ('%s')! "
296                                 "can't print udbg messages\n", of_stdout_device);
297         } else {
298                 printk(KERN_WARNING "don't know how to print to stdout '%s'\n",
299                                 of_stdout_device);
300         }
301
302 out:
303         of_node_put(stdout_node);
304         return found;
305 }
306
307 void pSeries_lpar_mm_init(void);
308
309 /* This is called early in setup.c.
310  * Use it to setup page table ppc_md stuff as well as udbg.
311  */
312 void pSeriesLP_init_early(void)
313 {
314         pSeries_lpar_mm_init();
315
316         tce_init_pSeries();
317
318         ppc_md.tce_build = tce_build_pSeriesLP;
319         ppc_md.tce_free  = tce_free_pSeriesLP;
320
321         pci_iommu_init();
322
323 #ifdef CONFIG_SMP
324         smp_init_pSeries();
325 #endif
326
327         /* The keyboard is not useful in the LPAR environment.
328          * Leave all the ppc_md keyboard interfaces NULL.
329          */
330
331         if (0 == find_udbg_vterm()) {
332                 printk(KERN_WARNING
333                         "can't use stdout; can't print early debug messages.\n");
334         }
335 }
336
337 long pSeries_lpar_hpte_insert(unsigned long hpte_group,
338                               unsigned long va, unsigned long prpn,
339                               int secondary, unsigned long hpteflags,
340                               int bolted, int large)
341 {
342         unsigned long arpn = physRpn_to_absRpn(prpn);
343         unsigned long lpar_rc;
344         unsigned long flags;
345         unsigned long slot;
346         HPTE lhpte;
347         unsigned long dummy0, dummy1;
348
349         /* Fill in the local HPTE with absolute rpn, avpn and flags */
350         lhpte.dw1.dword1      = 0;
351         lhpte.dw1.dw1.rpn     = arpn;
352         lhpte.dw1.flags.flags = hpteflags;
353
354         lhpte.dw0.dword0      = 0;
355         lhpte.dw0.dw0.avpn    = va >> 23;
356         lhpte.dw0.dw0.h       = secondary;
357         lhpte.dw0.dw0.bolted  = bolted;
358         lhpte.dw0.dw0.v       = 1;
359
360         if (large) {
361                 lhpte.dw0.dw0.l = 1;
362                 lhpte.dw0.dw0.avpn &= ~0x1UL;
363         }
364
365         /* Now fill in the actual HPTE */
366         /* Set CEC cookie to 0         */
367         /* Zero page = 0               */
368         /* I-cache Invalidate = 0      */
369         /* I-cache synchronize = 0     */
370         /* Exact = 0                   */
371         flags = 0;
372
373         /* XXX why is this here? - Anton */
374         if (hpteflags & (_PAGE_GUARDED|_PAGE_NO_CACHE))
375                 lhpte.dw1.flags.flags &= ~_PAGE_COHERENT;
376
377         lpar_rc = plpar_hcall(H_ENTER, flags, hpte_group, lhpte.dw0.dword0,
378                               lhpte.dw1.dword1, &slot, &dummy0, &dummy1);
379
380         if (lpar_rc == H_PTEG_Full)
381                 return -1;
382
383         /*
384          * Since we try and ioremap PHBs we don't own, the pte insert
385          * will fail. However we must catch the failure in hash_page
386          * or we will loop forever, so return -2 in this case.
387          */
388         if (lpar_rc != H_Success)
389                 return -2;
390
391         /* Because of iSeries, we have to pass down the secondary
392          * bucket bit here as well
393          */
394         return (slot & 7) | (secondary << 3);
395 }
396
397 static spinlock_t pSeries_lpar_tlbie_lock = SPIN_LOCK_UNLOCKED;
398
399 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
400 {
401         unsigned long slot_offset;
402         unsigned long lpar_rc;
403         int i;
404         unsigned long dummy1, dummy2;
405
406         /* pick a random slot to start at */
407         slot_offset = mftb() & 0x7;
408
409         for (i = 0; i < HPTES_PER_GROUP; i++) {
410
411                 /* don't remove a bolted entry */
412                 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
413                                            (0x1UL << 4), &dummy1, &dummy2);
414
415                 if (lpar_rc == H_Success)
416                         return i;
417
418                 if (lpar_rc != H_Not_Found)
419                         panic("Bad return code from pte remove rc = %lx\n",
420                               lpar_rc);
421
422                 slot_offset++;
423                 slot_offset &= 0x7;
424         }
425
426         return -1;
427 }
428
429 /*
430  * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
431  * the low 3 bits of flags happen to line up.  So no transform is needed.
432  * We can probably optimize here and assume the high bits of newpp are
433  * already zero.  For now I am paranoid.
434  */
435 static long pSeries_lpar_hpte_updatepp(unsigned long slot, unsigned long newpp,
436                                        unsigned long va, int large, int local)
437 {
438         unsigned long lpar_rc;
439         unsigned long flags = (newpp & 7) | H_AVPN;
440         unsigned long avpn = va >> 23;
441
442         if (large)
443                 avpn &= ~0x1UL;
444
445         lpar_rc = plpar_pte_protect(flags, slot, (avpn << 7));
446
447         if (lpar_rc == H_Not_Found)
448                 return -1;
449
450         if (lpar_rc != H_Success)
451                 panic("bad return code from pte protect rc = %lx\n", lpar_rc);
452
453         return 0;
454 }
455
456 static unsigned long pSeries_lpar_hpte_getword0(unsigned long slot)
457 {
458         unsigned long dword0;
459         unsigned long lpar_rc;
460         unsigned long dummy_word1;
461         unsigned long flags;
462
463         /* Read 1 pte at a time                        */
464         /* Do not need RPN to logical page translation */
465         /* No cross CEC PFT access                     */
466         flags = 0;
467         
468         lpar_rc = plpar_pte_read(flags, slot, &dword0, &dummy_word1);
469
470         if (lpar_rc != H_Success)
471                 panic("Error on pte read in get_hpte0 rc = %lx\n", lpar_rc);
472
473         return dword0;
474 }
475
476 static long pSeries_lpar_hpte_find(unsigned long vpn)
477 {
478         unsigned long hash;
479         unsigned long i, j;
480         long slot;
481         union {
482                 unsigned long dword0;
483                 Hpte_dword0 dw0;
484         } hpte_dw0;
485         Hpte_dword0 dw0;
486
487         hash = hpt_hash(vpn, 0);
488
489         for (j = 0; j < 2; j++) {
490                 slot = (hash & htab_data.htab_hash_mask) * HPTES_PER_GROUP;
491                 for (i = 0; i < HPTES_PER_GROUP; i++) {
492                         hpte_dw0.dword0 = pSeries_lpar_hpte_getword0(slot);
493                         dw0 = hpte_dw0.dw0;
494
495                         if ((dw0.avpn == (vpn >> 11)) && dw0.v &&
496                             (dw0.h == j)) {
497                                 /* HPTE matches */
498                                 if (j)
499                                         slot = -slot;
500                                 return slot;
501                         }
502                         ++slot;
503                 }
504                 hash = ~hash;
505         }
506
507         return -1;
508
509
510 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
511                                              unsigned long ea)
512 {
513         unsigned long lpar_rc;
514         unsigned long vsid, va, vpn, flags;
515         long slot;
516
517         vsid = get_kernel_vsid(ea);
518         va = (vsid << 28) | (ea & 0x0fffffff);
519         vpn = va >> PAGE_SHIFT;
520
521         slot = pSeries_lpar_hpte_find(vpn);
522         if (slot == -1)
523                 panic("updateboltedpp: Could not find page to bolt\n");
524
525         flags = newpp & 3;
526         lpar_rc = plpar_pte_protect(flags, slot, 0);
527
528         if (lpar_rc != H_Success)
529                 panic("Bad return code from pte bolted protect rc = %lx\n",
530                       lpar_rc); 
531 }
532
533 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long va,
534                                          int large, int local)
535 {
536         unsigned long avpn = va >> 23;
537         unsigned long lpar_rc;
538         unsigned long dummy1, dummy2;
539
540         if (large)
541                 avpn &= ~0x1UL;
542
543         lpar_rc = plpar_pte_remove(H_AVPN, slot, (avpn << 7), &dummy1,
544                                    &dummy2);
545
546         if (lpar_rc == H_Not_Found)
547                 return;
548
549         if (lpar_rc != H_Success)
550                 panic("Bad return code from invalidate rc = %lx\n", lpar_rc);
551 }
552
553 /*
554  * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
555  * lock.
556  */
557 void pSeries_lpar_flush_hash_range(unsigned long context, unsigned long number,
558                                    int local)
559 {
560         int i;
561         unsigned long flags;
562         struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);
563
564         if (!(cur_cpu_spec->cpu_features & CPU_FTR_LOCKLESS_TLBIE))
565                 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
566
567         for (i = 0; i < number; i++)
568                 flush_hash_page(context, batch->addr[i], batch->pte[i], local);
569
570         if (!(cur_cpu_spec->cpu_features & CPU_FTR_LOCKLESS_TLBIE))
571                 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
572 }
573
574 void pSeries_lpar_mm_init(void)
575 {
576         ppc_md.hpte_invalidate  = pSeries_lpar_hpte_invalidate;
577         ppc_md.hpte_updatepp    = pSeries_lpar_hpte_updatepp;
578         ppc_md.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
579         ppc_md.hpte_insert      = pSeries_lpar_hpte_insert;
580         ppc_md.hpte_remove      = pSeries_lpar_hpte_remove;
581         ppc_md.flush_hash_range = pSeries_lpar_flush_hash_range;
582 }