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