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