Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / arch / ppc64 / kernel / iSeries_setup.c
1 /*
2  *    Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
3  *    Copyright (c) 1999-2000 Grant Erickson <grant@lcse.umn.edu>
4  *
5  *    Module name: iSeries_setup.c
6  *
7  *    Description:
8  *      Architecture- / platform-specific boot-time initialization code for
9  *      the IBM iSeries LPAR.  Adapted from original code by Grant Erickson and
10  *      code by Gary Thomas, Cort Dougan <cort@fsmlabs.com>, and Dan Malek
11  *      <dan@net4x.com>.
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License
15  *      as published by the Free Software Foundation; either version
16  *      2 of the License, or (at your option) any later version.
17  */
18
19 #undef DEBUG
20
21 #include <linux/config.h>
22 #include <linux/init.h>
23 #include <linux/threads.h>
24 #include <linux/smp.h>
25 #include <linux/param.h>
26 #include <linux/string.h>
27 #include <linux/bootmem.h>
28 #include <linux/initrd.h>
29 #include <linux/seq_file.h>
30 #include <linux/kdev_t.h>
31 #include <linux/major.h>
32 #include <linux/root_dev.h>
33
34 #include <asm/processor.h>
35 #include <asm/machdep.h>
36 #include <asm/page.h>
37 #include <asm/mmu.h>
38 #include <asm/pgtable.h>
39 #include <asm/mmu_context.h>
40 #include <asm/cputable.h>
41 #include <asm/sections.h>
42 #include <asm/iommu.h>
43
44 #include <asm/time.h>
45 #include "iSeries_setup.h"
46 #include <asm/naca.h>
47 #include <asm/paca.h>
48 #include <asm/cache.h>
49 #include <asm/sections.h>
50 #include <asm/abs_addr.h>
51 #include <asm/iSeries/HvCallHpt.h>
52 #include <asm/iSeries/HvLpConfig.h>
53 #include <asm/iSeries/HvCallEvent.h>
54 #include <asm/iSeries/HvCallSm.h>
55 #include <asm/iSeries/HvCallXm.h>
56 #include <asm/iSeries/ItLpQueue.h>
57 #include <asm/iSeries/IoHriMainStore.h>
58 #include <asm/iSeries/mf.h>
59 #include <asm/iSeries/HvLpEvent.h>
60 #include <asm/iSeries/iSeries_irq.h>
61 #include <asm/iSeries/IoHriProcessorVpd.h>
62 #include <asm/iSeries/ItVpdAreas.h>
63 #include <asm/iSeries/LparMap.h>
64
65 extern void hvlog(char *fmt, ...);
66
67 #ifdef DEBUG
68 #define DBG(fmt...) hvlog(fmt)
69 #else
70 #define DBG(fmt...)
71 #endif
72
73 /* Function Prototypes */
74 extern void ppcdbg_initialize(void);
75
76 static void build_iSeries_Memory_Map(void);
77 static void setup_iSeries_cache_sizes(void);
78 static void iSeries_bolt_kernel(unsigned long saddr, unsigned long eaddr);
79 #ifdef CONFIG_PCI
80 extern void iSeries_pci_final_fixup(void);
81 #else
82 static void iSeries_pci_final_fixup(void) { }
83 #endif
84
85 /* Global Variables */
86 static unsigned long procFreqHz;
87 static unsigned long procFreqMhz;
88 static unsigned long procFreqMhzHundreths;
89
90 static unsigned long tbFreqHz;
91 static unsigned long tbFreqMhz;
92 static unsigned long tbFreqMhzHundreths;
93
94 int piranha_simulator;
95
96 extern int rd_size;             /* Defined in drivers/block/rd.c */
97 extern unsigned long klimit;
98 extern unsigned long embedded_sysmap_start;
99 extern unsigned long embedded_sysmap_end;
100
101 extern unsigned long iSeries_recal_tb;
102 extern unsigned long iSeries_recal_titan;
103
104 static int mf_initialized;
105
106 struct MemoryBlock {
107         unsigned long absStart;
108         unsigned long absEnd;
109         unsigned long logicalStart;
110         unsigned long logicalEnd;
111 };
112
113 /*
114  * Process the main store vpd to determine where the holes in memory are
115  * and return the number of physical blocks and fill in the array of
116  * block data.
117  */
118 static unsigned long iSeries_process_Condor_mainstore_vpd(
119                 struct MemoryBlock *mb_array, unsigned long max_entries)
120 {
121         unsigned long holeFirstChunk, holeSizeChunks;
122         unsigned long numMemoryBlocks = 1;
123         struct IoHriMainStoreSegment4 *msVpd =
124                 (struct IoHriMainStoreSegment4 *)xMsVpd;
125         unsigned long holeStart = msVpd->nonInterleavedBlocksStartAdr;
126         unsigned long holeEnd = msVpd->nonInterleavedBlocksEndAdr;
127         unsigned long holeSize = holeEnd - holeStart;
128
129         printk("Mainstore_VPD: Condor\n");
130         /*
131          * Determine if absolute memory has any
132          * holes so that we can interpret the
133          * access map we get back from the hypervisor
134          * correctly.
135          */
136         mb_array[0].logicalStart = 0;
137         mb_array[0].logicalEnd = 0x100000000;
138         mb_array[0].absStart = 0;
139         mb_array[0].absEnd = 0x100000000;
140
141         if (holeSize) {
142                 numMemoryBlocks = 2;
143                 holeStart = holeStart & 0x000fffffffffffff;
144                 holeStart = addr_to_chunk(holeStart);
145                 holeFirstChunk = holeStart;
146                 holeSize = addr_to_chunk(holeSize);
147                 holeSizeChunks = holeSize;
148                 printk( "Main store hole: start chunk = %0lx, size = %0lx chunks\n",
149                                 holeFirstChunk, holeSizeChunks );
150                 mb_array[0].logicalEnd = holeFirstChunk;
151                 mb_array[0].absEnd = holeFirstChunk;
152                 mb_array[1].logicalStart = holeFirstChunk;
153                 mb_array[1].logicalEnd = 0x100000000 - holeSizeChunks;
154                 mb_array[1].absStart = holeFirstChunk + holeSizeChunks;
155                 mb_array[1].absEnd = 0x100000000;
156         }
157         return numMemoryBlocks;
158 }
159
160 #define MaxSegmentAreas                 32
161 #define MaxSegmentAdrRangeBlocks        128
162 #define MaxAreaRangeBlocks              4
163
164 static unsigned long iSeries_process_Regatta_mainstore_vpd(
165                 struct MemoryBlock *mb_array, unsigned long max_entries)
166 {
167         struct IoHriMainStoreSegment5 *msVpdP =
168                 (struct IoHriMainStoreSegment5 *)xMsVpd;
169         unsigned long numSegmentBlocks = 0;
170         u32 existsBits = msVpdP->msAreaExists;
171         unsigned long area_num;
172
173         printk("Mainstore_VPD: Regatta\n");
174
175         for (area_num = 0; area_num < MaxSegmentAreas; ++area_num ) {
176                 unsigned long numAreaBlocks;
177                 struct IoHriMainStoreArea4 *currentArea;
178
179                 if (existsBits & 0x80000000) {
180                         unsigned long block_num;
181
182                         currentArea = &msVpdP->msAreaArray[area_num];
183                         numAreaBlocks = currentArea->numAdrRangeBlocks;
184                         printk("ms_vpd: processing area %2ld  blocks=%ld",
185                                         area_num, numAreaBlocks);
186                         for (block_num = 0; block_num < numAreaBlocks;
187                                         ++block_num ) {
188                                 /* Process an address range block */
189                                 struct MemoryBlock tempBlock;
190                                 unsigned long i;
191
192                                 tempBlock.absStart =
193                                         (unsigned long)currentArea->xAdrRangeBlock[block_num].blockStart;
194                                 tempBlock.absEnd =
195                                         (unsigned long)currentArea->xAdrRangeBlock[block_num].blockEnd;
196                                 tempBlock.logicalStart = 0;
197                                 tempBlock.logicalEnd   = 0;
198                                 printk("\n          block %ld absStart=%016lx absEnd=%016lx",
199                                                 block_num, tempBlock.absStart,
200                                                 tempBlock.absEnd);
201
202                                 for (i = 0; i < numSegmentBlocks; ++i) {
203                                         if (mb_array[i].absStart ==
204                                                         tempBlock.absStart)
205                                                 break;
206                                 }
207                                 if (i == numSegmentBlocks) {
208                                         if (numSegmentBlocks == max_entries)
209                                                 panic("iSeries_process_mainstore_vpd: too many memory blocks");
210                                         mb_array[numSegmentBlocks] = tempBlock;
211                                         ++numSegmentBlocks;
212                                 } else
213                                         printk(" (duplicate)");
214                         }
215                         printk("\n");
216                 }
217                 existsBits <<= 1;
218         }
219         /* Now sort the blocks found into ascending sequence */
220         if (numSegmentBlocks > 1) {
221                 unsigned long m, n;
222
223                 for (m = 0; m < numSegmentBlocks - 1; ++m) {
224                         for (n = numSegmentBlocks - 1; m < n; --n) {
225                                 if (mb_array[n].absStart <
226                                                 mb_array[n-1].absStart) {
227                                         struct MemoryBlock tempBlock;
228
229                                         tempBlock = mb_array[n];
230                                         mb_array[n] = mb_array[n-1];
231                                         mb_array[n-1] = tempBlock;
232                                 }
233                         }
234                 }
235         }
236         /*
237          * Assign "logical" addresses to each block.  These
238          * addresses correspond to the hypervisor "bitmap" space.
239          * Convert all addresses into units of 256K chunks.
240          */
241         {
242         unsigned long i, nextBitmapAddress;
243
244         printk("ms_vpd: %ld sorted memory blocks\n", numSegmentBlocks);
245         nextBitmapAddress = 0;
246         for (i = 0; i < numSegmentBlocks; ++i) {
247                 unsigned long length = mb_array[i].absEnd -
248                         mb_array[i].absStart;
249
250                 mb_array[i].logicalStart = nextBitmapAddress;
251                 mb_array[i].logicalEnd = nextBitmapAddress + length;
252                 nextBitmapAddress += length;
253                 printk("          Bitmap range: %016lx - %016lx\n"
254                                 "        Absolute range: %016lx - %016lx\n",
255                                 mb_array[i].logicalStart,
256                                 mb_array[i].logicalEnd,
257                                 mb_array[i].absStart, mb_array[i].absEnd);
258                 mb_array[i].absStart = addr_to_chunk(mb_array[i].absStart &
259                                 0x000fffffffffffff);
260                 mb_array[i].absEnd = addr_to_chunk(mb_array[i].absEnd &
261                                 0x000fffffffffffff);
262                 mb_array[i].logicalStart =
263                         addr_to_chunk(mb_array[i].logicalStart);
264                 mb_array[i].logicalEnd = addr_to_chunk(mb_array[i].logicalEnd);
265         }
266         }
267
268         return numSegmentBlocks;
269 }
270
271 static unsigned long iSeries_process_mainstore_vpd(struct MemoryBlock *mb_array,
272                 unsigned long max_entries)
273 {
274         unsigned long i;
275         unsigned long mem_blocks = 0;
276
277         if (cpu_has_feature(CPU_FTR_SLB))
278                 mem_blocks = iSeries_process_Regatta_mainstore_vpd(mb_array,
279                                 max_entries);
280         else
281                 mem_blocks = iSeries_process_Condor_mainstore_vpd(mb_array,
282                                 max_entries);
283
284         printk("Mainstore_VPD: numMemoryBlocks = %ld \n", mem_blocks);
285         for (i = 0; i < mem_blocks; ++i) {
286                 printk("Mainstore_VPD: block %3ld logical chunks %016lx - %016lx\n"
287                        "                             abs chunks %016lx - %016lx\n",
288                         i, mb_array[i].logicalStart, mb_array[i].logicalEnd,
289                         mb_array[i].absStart, mb_array[i].absEnd);
290         }
291         return mem_blocks;
292 }
293
294 static void __init iSeries_get_cmdline(void)
295 {
296         char *p, *q;
297
298         /* copy the command line parameter from the primary VSP  */
299         HvCallEvent_dmaToSp(cmd_line, 2 * 64* 1024, 256,
300                         HvLpDma_Direction_RemoteToLocal);
301
302         p = cmd_line;
303         q = cmd_line + 255;
304         while(p < q) {
305                 if (!*p || *p == '\n')
306                         break;
307                 ++p;
308         }
309         *p = 0;
310 }
311
312 static void __init iSeries_init_early(void)
313 {
314         extern unsigned long memory_limit;
315
316         DBG(" -> iSeries_init_early()\n");
317
318         ppcdbg_initialize();
319
320 #if defined(CONFIG_BLK_DEV_INITRD)
321         /*
322          * If the init RAM disk has been configured and there is
323          * a non-zero starting address for it, set it up
324          */
325         if (naca.xRamDisk) {
326                 initrd_start = (unsigned long)__va(naca.xRamDisk);
327                 initrd_end = initrd_start + naca.xRamDiskSize * PAGE_SIZE;
328                 initrd_below_start_ok = 1;      // ramdisk in kernel space
329                 ROOT_DEV = Root_RAM0;
330                 if (((rd_size * 1024) / PAGE_SIZE) < naca.xRamDiskSize)
331                         rd_size = (naca.xRamDiskSize * PAGE_SIZE) / 1024;
332         } else
333 #endif /* CONFIG_BLK_DEV_INITRD */
334         {
335             /* ROOT_DEV = MKDEV(VIODASD_MAJOR, 1); */
336         }
337
338         iSeries_recal_tb = get_tb();
339         iSeries_recal_titan = HvCallXm_loadTod();
340
341         /*
342          * Cache sizes must be initialized before hpte_init_iSeries is called
343          * as the later need them for flush_icache_range()
344          */
345         setup_iSeries_cache_sizes();
346
347         /*
348          * Initialize the hash table management pointers
349          */
350         hpte_init_iSeries();
351
352         /*
353          * Initialize the DMA/TCE management
354          */
355         iommu_init_early_iSeries();
356
357         /*
358          * Initialize the table which translate Linux physical addresses to
359          * AS/400 absolute addresses
360          */
361         build_iSeries_Memory_Map();
362
363         iSeries_get_cmdline();
364
365         /* Save unparsed command line copy for /proc/cmdline */
366         strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
367
368         /* Parse early parameters, in particular mem=x */
369         parse_early_param();
370
371         if (memory_limit) {
372                 if (memory_limit < systemcfg->physicalMemorySize)
373                         systemcfg->physicalMemorySize = memory_limit;
374                 else {
375                         printk("Ignoring mem=%lu >= ram_top.\n", memory_limit);
376                         memory_limit = 0;
377                 }
378         }
379
380         /* Bolt kernel mappings for all of memory (or just a bit if we've got a limit) */
381         iSeries_bolt_kernel(0, systemcfg->physicalMemorySize);
382
383         lmb_init();
384         lmb_add(0, systemcfg->physicalMemorySize);
385         lmb_analyze();
386         lmb_reserve(0, __pa(klimit));
387
388         /* Initialize machine-dependency vectors */
389 #ifdef CONFIG_SMP
390         smp_init_iSeries();
391 #endif
392         if (itLpNaca.xPirEnvironMode == 0)
393                 piranha_simulator = 1;
394
395         /* Associate Lp Event Queue 0 with processor 0 */
396         HvCallEvent_setLpEventQueueInterruptProc(0, 0);
397
398         mf_init();
399         mf_initialized = 1;
400         mb();
401
402         /* If we were passed an initrd, set the ROOT_DEV properly if the values
403          * look sensible. If not, clear initrd reference.
404          */
405 #ifdef CONFIG_BLK_DEV_INITRD
406         if (initrd_start >= KERNELBASE && initrd_end >= KERNELBASE &&
407             initrd_end > initrd_start)
408                 ROOT_DEV = Root_RAM0;
409         else
410                 initrd_start = initrd_end = 0;
411 #endif /* CONFIG_BLK_DEV_INITRD */
412
413         DBG(" <- iSeries_init_early()\n");
414 }
415
416 /*
417  * The iSeries may have very large memories ( > 128 GB ) and a partition
418  * may get memory in "chunks" that may be anywhere in the 2**52 real
419  * address space.  The chunks are 256K in size.  To map this to the
420  * memory model Linux expects, the AS/400 specific code builds a
421  * translation table to translate what Linux thinks are "physical"
422  * addresses to the actual real addresses.  This allows us to make
423  * it appear to Linux that we have contiguous memory starting at
424  * physical address zero while in fact this could be far from the truth.
425  * To avoid confusion, I'll let the words physical and/or real address
426  * apply to the Linux addresses while I'll use "absolute address" to
427  * refer to the actual hardware real address.
428  *
429  * build_iSeries_Memory_Map gets information from the Hypervisor and
430  * looks at the Main Store VPD to determine the absolute addresses
431  * of the memory that has been assigned to our partition and builds
432  * a table used to translate Linux's physical addresses to these
433  * absolute addresses.  Absolute addresses are needed when
434  * communicating with the hypervisor (e.g. to build HPT entries)
435  */
436
437 static void __init build_iSeries_Memory_Map(void)
438 {
439         u32 loadAreaFirstChunk, loadAreaLastChunk, loadAreaSize;
440         u32 nextPhysChunk;
441         u32 hptFirstChunk, hptLastChunk, hptSizeChunks, hptSizePages;
442         u32 num_ptegs;
443         u32 totalChunks,moreChunks;
444         u32 currChunk, thisChunk, absChunk;
445         u32 currDword;
446         u32 chunkBit;
447         u64 map;
448         struct MemoryBlock mb[32];
449         unsigned long numMemoryBlocks, curBlock;
450
451         /* Chunk size on iSeries is 256K bytes */
452         totalChunks = (u32)HvLpConfig_getMsChunks();
453         klimit = msChunks_alloc(klimit, totalChunks, 1UL << 18);
454
455         /*
456          * Get absolute address of our load area
457          * and map it to physical address 0
458          * This guarantees that the loadarea ends up at physical 0
459          * otherwise, it might not be returned by PLIC as the first
460          * chunks
461          */
462
463         loadAreaFirstChunk = (u32)addr_to_chunk(itLpNaca.xLoadAreaAddr);
464         loadAreaSize =  itLpNaca.xLoadAreaChunks;
465
466         /*
467          * Only add the pages already mapped here.
468          * Otherwise we might add the hpt pages
469          * The rest of the pages of the load area
470          * aren't in the HPT yet and can still
471          * be assigned an arbitrary physical address
472          */
473         if ((loadAreaSize * 64) > HvPagesToMap)
474                 loadAreaSize = HvPagesToMap / 64;
475
476         loadAreaLastChunk = loadAreaFirstChunk + loadAreaSize - 1;
477
478         /*
479          * TODO Do we need to do something if the HPT is in the 64MB load area?
480          * This would be required if the itLpNaca.xLoadAreaChunks includes
481          * the HPT size
482          */
483
484         printk("Mapping load area - physical addr = 0000000000000000\n"
485                 "                    absolute addr = %016lx\n",
486                 chunk_to_addr(loadAreaFirstChunk));
487         printk("Load area size %dK\n", loadAreaSize * 256);
488
489         for (nextPhysChunk = 0; nextPhysChunk < loadAreaSize; ++nextPhysChunk)
490                 msChunks.abs[nextPhysChunk] =
491                         loadAreaFirstChunk + nextPhysChunk;
492
493         /*
494          * Get absolute address of our HPT and remember it so
495          * we won't map it to any physical address
496          */
497         hptFirstChunk = (u32)addr_to_chunk(HvCallHpt_getHptAddress());
498         hptSizePages = (u32)HvCallHpt_getHptPages();
499         hptSizeChunks = hptSizePages >> (msChunks.chunk_shift - PAGE_SHIFT);
500         hptLastChunk = hptFirstChunk + hptSizeChunks - 1;
501
502         printk("HPT absolute addr = %016lx, size = %dK\n",
503                         chunk_to_addr(hptFirstChunk), hptSizeChunks * 256);
504
505         /* Fill in the hashed page table hash mask */
506         num_ptegs = hptSizePages *
507                 (PAGE_SIZE / (sizeof(HPTE) * HPTES_PER_GROUP));
508         htab_hash_mask = num_ptegs - 1;
509
510         /*
511          * The actual hashed page table is in the hypervisor,
512          * we have no direct access
513          */
514         htab_address = NULL;
515
516         /*
517          * Determine if absolute memory has any
518          * holes so that we can interpret the
519          * access map we get back from the hypervisor
520          * correctly.
521          */
522         numMemoryBlocks = iSeries_process_mainstore_vpd(mb, 32);
523
524         /*
525          * Process the main store access map from the hypervisor
526          * to build up our physical -> absolute translation table
527          */
528         curBlock = 0;
529         currChunk = 0;
530         currDword = 0;
531         moreChunks = totalChunks;
532
533         while (moreChunks) {
534                 map = HvCallSm_get64BitsOfAccessMap(itLpNaca.xLpIndex,
535                                 currDword);
536                 thisChunk = currChunk;
537                 while (map) {
538                         chunkBit = map >> 63;
539                         map <<= 1;
540                         if (chunkBit) {
541                                 --moreChunks;
542                                 while (thisChunk >= mb[curBlock].logicalEnd) {
543                                         ++curBlock;
544                                         if (curBlock >= numMemoryBlocks)
545                                                 panic("out of memory blocks");
546                                 }
547                                 if (thisChunk < mb[curBlock].logicalStart)
548                                         panic("memory block error");
549
550                                 absChunk = mb[curBlock].absStart +
551                                         (thisChunk - mb[curBlock].logicalStart);
552                                 if (((absChunk < hptFirstChunk) ||
553                                      (absChunk > hptLastChunk)) &&
554                                     ((absChunk < loadAreaFirstChunk) ||
555                                      (absChunk > loadAreaLastChunk))) {
556                                         msChunks.abs[nextPhysChunk] = absChunk;
557                                         ++nextPhysChunk;
558                                 }
559                         }
560                         ++thisChunk;
561                 }
562                 ++currDword;
563                 currChunk += 64;
564         }
565
566         /*
567          * main store size (in chunks) is
568          *   totalChunks - hptSizeChunks
569          * which should be equal to
570          *   nextPhysChunk
571          */
572         systemcfg->physicalMemorySize = chunk_to_addr(nextPhysChunk);
573 }
574
575 /*
576  * Set up the variables that describe the cache line sizes
577  * for this machine.
578  */
579 static void __init setup_iSeries_cache_sizes(void)
580 {
581         unsigned int i, n;
582         unsigned int procIx = get_paca()->lppaca.dyn_hv_phys_proc_index;
583
584         systemcfg->icache_size =
585         ppc64_caches.isize = xIoHriProcessorVpd[procIx].xInstCacheSize * 1024;
586         systemcfg->icache_line_size =
587         ppc64_caches.iline_size =
588                 xIoHriProcessorVpd[procIx].xInstCacheOperandSize;
589         systemcfg->dcache_size =
590         ppc64_caches.dsize =
591                 xIoHriProcessorVpd[procIx].xDataL1CacheSizeKB * 1024;
592         systemcfg->dcache_line_size =
593         ppc64_caches.dline_size =
594                 xIoHriProcessorVpd[procIx].xDataCacheOperandSize;
595         ppc64_caches.ilines_per_page = PAGE_SIZE / ppc64_caches.iline_size;
596         ppc64_caches.dlines_per_page = PAGE_SIZE / ppc64_caches.dline_size;
597
598         i = ppc64_caches.iline_size;
599         n = 0;
600         while ((i = (i / 2)))
601                 ++n;
602         ppc64_caches.log_iline_size = n;
603
604         i = ppc64_caches.dline_size;
605         n = 0;
606         while ((i = (i / 2)))
607                 ++n;
608         ppc64_caches.log_dline_size = n;
609
610         printk("D-cache line size = %d\n",
611                         (unsigned int)ppc64_caches.dline_size);
612         printk("I-cache line size = %d\n",
613                         (unsigned int)ppc64_caches.iline_size);
614 }
615
616 /*
617  * Create a pte. Used during initialization only.
618  */
619 static void iSeries_make_pte(unsigned long va, unsigned long pa,
620                              int mode)
621 {
622         HPTE local_hpte, rhpte;
623         unsigned long hash, vpn;
624         long slot;
625
626         vpn = va >> PAGE_SHIFT;
627         hash = hpt_hash(vpn, 0);
628
629         local_hpte.dw1.dword1 = pa | mode;
630         local_hpte.dw0.dword0 = 0;
631         local_hpte.dw0.dw0.avpn = va >> 23;
632         local_hpte.dw0.dw0.bolted = 1;          /* bolted */
633         local_hpte.dw0.dw0.v = 1;
634
635         slot = HvCallHpt_findValid(&rhpte, vpn);
636         if (slot < 0) {
637                 /* Must find space in primary group */
638                 panic("hash_page: hpte already exists\n");
639         }
640         HvCallHpt_addValidate(slot, 0, (HPTE *)&local_hpte );
641 }
642
643 /*
644  * Bolt the kernel addr space into the HPT
645  */
646 static void __init iSeries_bolt_kernel(unsigned long saddr, unsigned long eaddr)
647 {
648         unsigned long pa;
649         unsigned long mode_rw = _PAGE_ACCESSED | _PAGE_COHERENT | PP_RWXX;
650         HPTE hpte;
651
652         for (pa = saddr; pa < eaddr ;pa += PAGE_SIZE) {
653                 unsigned long ea = (unsigned long)__va(pa);
654                 unsigned long vsid = get_kernel_vsid(ea);
655                 unsigned long va = (vsid << 28) | (pa & 0xfffffff);
656                 unsigned long vpn = va >> PAGE_SHIFT;
657                 unsigned long slot = HvCallHpt_findValid(&hpte, vpn);
658
659                 /* Make non-kernel text non-executable */
660                 if (!in_kernel_text(ea))
661                         mode_rw |= HW_NO_EXEC;
662
663                 if (hpte.dw0.dw0.v) {
664                         /* HPTE exists, so just bolt it */
665                         HvCallHpt_setSwBits(slot, 0x10, 0);
666                         /* And make sure the pp bits are correct */
667                         HvCallHpt_setPp(slot, PP_RWXX);
668                 } else
669                         /* No HPTE exists, so create a new bolted one */
670                         iSeries_make_pte(va, phys_to_abs(pa), mode_rw);
671         }
672 }
673
674 /*
675  * Document me.
676  */
677 static void __init iSeries_setup_arch(void)
678 {
679         void *eventStack;
680         unsigned procIx = get_paca()->lppaca.dyn_hv_phys_proc_index;
681
682         /* Add an eye catcher and the systemcfg layout version number */
683         strcpy(systemcfg->eye_catcher, "SYSTEMCFG:PPC64");
684         systemcfg->version.major = SYSTEMCFG_MAJOR;
685         systemcfg->version.minor = SYSTEMCFG_MINOR;
686
687         /* Setup the Lp Event Queue */
688
689         /* Allocate a page for the Event Stack
690          * The hypervisor wants the absolute real address, so
691          * we subtract out the KERNELBASE and add in the
692          * absolute real address of the kernel load area
693          */
694         eventStack = alloc_bootmem_pages(LpEventStackSize);
695         memset(eventStack, 0, LpEventStackSize);
696
697         /* Invoke the hypervisor to initialize the event stack */
698         HvCallEvent_setLpEventStack(0, eventStack, LpEventStackSize);
699
700         /* Initialize fields in our Lp Event Queue */
701         xItLpQueue.xSlicEventStackPtr = (char *)eventStack;
702         xItLpQueue.xSlicCurEventPtr = (char *)eventStack;
703         xItLpQueue.xSlicLastValidEventPtr = (char *)eventStack +
704                                         (LpEventStackSize - LpEventMaxSize);
705         xItLpQueue.xIndex = 0;
706
707         /* Compute processor frequency */
708         procFreqHz = ((1UL << 34) * 1000000) /
709                         xIoHriProcessorVpd[procIx].xProcFreq;
710         procFreqMhz = procFreqHz / 1000000;
711         procFreqMhzHundreths = (procFreqHz / 10000) - (procFreqMhz * 100);
712         ppc_proc_freq = procFreqHz;
713
714         /* Compute time base frequency */
715         tbFreqHz = ((1UL << 32) * 1000000) /
716                 xIoHriProcessorVpd[procIx].xTimeBaseFreq;
717         tbFreqMhz = tbFreqHz / 1000000;
718         tbFreqMhzHundreths = (tbFreqHz / 10000) - (tbFreqMhz * 100);
719         ppc_tb_freq = tbFreqHz;
720
721         printk("Max  logical processors = %d\n",
722                         itVpdAreas.xSlicMaxLogicalProcs);
723         printk("Max physical processors = %d\n",
724                         itVpdAreas.xSlicMaxPhysicalProcs);
725         printk("Processor frequency = %lu.%02lu\n", procFreqMhz,
726                         procFreqMhzHundreths);
727         printk("Time base frequency = %lu.%02lu\n", tbFreqMhz,
728                         tbFreqMhzHundreths);
729         systemcfg->processor = xIoHriProcessorVpd[procIx].xPVR;
730         printk("Processor version = %x\n", systemcfg->processor);
731 }
732
733 static void iSeries_get_cpuinfo(struct seq_file *m)
734 {
735         seq_printf(m, "machine\t\t: 64-bit iSeries Logical Partition\n");
736 }
737
738 /*
739  * Document me.
740  * and Implement me.
741  */
742 static int iSeries_get_irq(struct pt_regs *regs)
743 {
744         /* -2 means ignore this interrupt */
745         return -2;
746 }
747
748 /*
749  * Document me.
750  */
751 static void iSeries_restart(char *cmd)
752 {
753         mf_reboot();
754 }
755
756 /*
757  * Document me.
758  */
759 static void iSeries_power_off(void)
760 {
761         mf_power_off();
762 }
763
764 /*
765  * Document me.
766  */
767 static void iSeries_halt(void)
768 {
769         mf_power_off();
770 }
771
772 /*
773  * void __init iSeries_calibrate_decr()
774  *
775  * Description:
776  *   This routine retrieves the internal processor frequency from the VPD,
777  *   and sets up the kernel timer decrementer based on that value.
778  *
779  */
780 static void __init iSeries_calibrate_decr(void)
781 {
782         unsigned long   cyclesPerUsec;
783         struct div_result divres;
784
785         /* Compute decrementer (and TB) frequency in cycles/sec */
786         cyclesPerUsec = ppc_tb_freq / 1000000;
787
788         /*
789          * Set the amount to refresh the decrementer by.  This
790          * is the number of decrementer ticks it takes for
791          * 1/HZ seconds.
792          */
793         tb_ticks_per_jiffy = ppc_tb_freq / HZ;
794
795 #if 0
796         /* TEST CODE FOR ADJTIME */
797         tb_ticks_per_jiffy += tb_ticks_per_jiffy / 5000;
798         /* END OF TEST CODE */
799 #endif
800
801         /*
802          * tb_ticks_per_sec = freq; would give better accuracy
803          * but tb_ticks_per_sec = tb_ticks_per_jiffy*HZ; assures
804          * that jiffies (and xtime) will match the time returned
805          * by do_gettimeofday.
806          */
807         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
808         tb_ticks_per_usec = cyclesPerUsec;
809         tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
810         div128_by_32(1024 * 1024, 0, tb_ticks_per_sec, &divres);
811         tb_to_xs = divres.result_low;
812         setup_default_decr();
813 }
814
815 static void __init iSeries_progress(char * st, unsigned short code)
816 {
817         printk("Progress: [%04x] - %s\n", (unsigned)code, st);
818         if (!piranha_simulator && mf_initialized) {
819                 if (code != 0xffff)
820                         mf_display_progress(code);
821                 else
822                         mf_clear_src();
823         }
824 }
825
826 static void __init iSeries_fixup_klimit(void)
827 {
828         /*
829          * Change klimit to take into account any ram disk
830          * that may be included
831          */
832         if (naca.xRamDisk)
833                 klimit = KERNELBASE + (u64)naca.xRamDisk +
834                         (naca.xRamDiskSize * PAGE_SIZE);
835         else {
836                 /*
837                  * No ram disk was included - check and see if there
838                  * was an embedded system map.  Change klimit to take
839                  * into account any embedded system map
840                  */
841                 if (embedded_sysmap_end)
842                         klimit = KERNELBASE + ((embedded_sysmap_end + 4095) &
843                                         0xfffffffffffff000);
844         }
845 }
846
847 static int __init iSeries_src_init(void)
848 {
849         /* clear the progress line */
850         ppc_md.progress(" ", 0xffff);
851         return 0;
852 }
853
854 late_initcall(iSeries_src_init);
855
856 static int set_spread_lpevents(char *str)
857 {
858         unsigned long i;
859         unsigned long val = simple_strtoul(str, NULL, 0);
860
861         /*
862          * The parameter is the number of processors to share in processing
863          * lp events.
864          */
865         if (( val > 0) && (val <= NR_CPUS)) {
866                 for (i = 1; i < val; ++i)
867                         paca[i].lpqueue_ptr = paca[0].lpqueue_ptr;
868
869                 printk("lpevent processing spread over %ld processors\n", val);
870         } else {
871                 printk("invalid spread_lpevents %ld\n", val);
872         }
873
874         return 1;
875 }
876 __setup("spread_lpevents=", set_spread_lpevents);
877
878 #ifndef CONFIG_PCI
879 void __init iSeries_init_IRQ(void) { }
880 #endif
881
882 void __init iSeries_early_setup(void)
883 {
884         iSeries_fixup_klimit();
885
886         ppc_md.setup_arch = iSeries_setup_arch;
887         ppc_md.get_cpuinfo = iSeries_get_cpuinfo;
888         ppc_md.init_IRQ = iSeries_init_IRQ;
889         ppc_md.get_irq = iSeries_get_irq;
890         ppc_md.init_early = iSeries_init_early,
891
892         ppc_md.pcibios_fixup  = iSeries_pci_final_fixup;
893
894         ppc_md.restart = iSeries_restart;
895         ppc_md.power_off = iSeries_power_off;
896         ppc_md.halt = iSeries_halt;
897
898         ppc_md.get_boot_time = iSeries_get_boot_time;
899         ppc_md.set_rtc_time = iSeries_set_rtc_time;
900         ppc_md.get_rtc_time = iSeries_get_rtc_time;
901         ppc_md.calibrate_decr = iSeries_calibrate_decr;
902         ppc_md.progress = iSeries_progress;
903 }
904