dcb1d6d51ce49ac4ef732be5d7991b73667c4a23
[pandora-kernel.git] / arch / metag / kernel / setup.c
1 /*
2  * Copyright (C) 2005-2012 Imagination Technologies Ltd.
3  *
4  * This file contains the architecture-dependant parts of system setup.
5  *
6  */
7
8 #include <linux/bootmem.h>
9 #include <linux/console.h>
10 #include <linux/cpu.h>
11 #include <linux/delay.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/genhd.h>
15 #include <linux/init.h>
16 #include <linux/initrd.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/memblock.h>
20 #include <linux/mm.h>
21 #include <linux/of_fdt.h>
22 #include <linux/pfn.h>
23 #include <linux/root_dev.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/start_kernel.h>
27 #include <linux/string.h>
28
29 #include <asm/cachepart.h>
30 #include <asm/clock.h>
31 #include <asm/core_reg.h>
32 #include <asm/cpu.h>
33 #include <asm/da.h>
34 #include <asm/highmem.h>
35 #include <asm/hwthread.h>
36 #include <asm/l2cache.h>
37 #include <asm/mach/arch.h>
38 #include <asm/metag_regs.h>
39 #include <asm/mmu.h>
40 #include <asm/mmzone.h>
41 #include <asm/processor.h>
42 #include <asm/prom.h>
43 #include <asm/sections.h>
44 #include <asm/setup.h>
45 #include <asm/traps.h>
46
47 /* Priv protect as many registers as possible. */
48 #define DEFAULT_PRIV    (TXPRIVEXT_COPRO_BITS           | \
49                          TXPRIVEXT_TXTRIGGER_BIT        | \
50                          TXPRIVEXT_TXGBLCREG_BIT        | \
51                          TXPRIVEXT_ILOCK_BIT            | \
52                          TXPRIVEXT_TXITACCYC_BIT        | \
53                          TXPRIVEXT_TXDIVTIME_BIT        | \
54                          TXPRIVEXT_TXAMAREGX_BIT        | \
55                          TXPRIVEXT_TXTIMERI_BIT         | \
56                          TXPRIVEXT_TXSTATUS_BIT         | \
57                          TXPRIVEXT_TXDISABLE_BIT)
58
59 /* Meta2 specific bits. */
60 #ifdef CONFIG_METAG_META12
61 #define META2_PRIV      0
62 #else
63 #define META2_PRIV      (TXPRIVEXT_TXTIMER_BIT          | \
64                          TXPRIVEXT_TRACE_BIT)
65 #endif
66
67 /* Unaligned access checking bits. */
68 #ifdef CONFIG_METAG_UNALIGNED
69 #define UNALIGNED_PRIV  TXPRIVEXT_ALIGNREW_BIT
70 #else
71 #define UNALIGNED_PRIV  0
72 #endif
73
74 #define PRIV_BITS       (DEFAULT_PRIV                   | \
75                          META2_PRIV                     | \
76                          UNALIGNED_PRIV)
77
78 extern char _heap_start[];
79
80 #ifdef CONFIG_METAG_BUILTIN_DTB
81 extern u32 __dtb_start[];
82 #endif
83
84 #ifdef CONFIG_DA_CONSOLE
85 /* Our early channel based console driver */
86 extern struct console dash_console;
87 #endif
88
89 struct machine_desc *machine_desc __initdata;
90
91 /*
92  * Map a Linux CPU number to a hardware thread ID
93  * In SMP this will be setup with the correct mapping at startup; in UP this
94  * will map to the HW thread on which we are running.
95  */
96 u8 cpu_2_hwthread_id[NR_CPUS] __read_mostly = {
97         [0 ... NR_CPUS-1] = BAD_HWTHREAD_ID
98 };
99
100 /*
101  * Map a hardware thread ID to a Linux CPU number
102  * In SMP this will be fleshed out with the correct CPU ID for a particular
103  * hardware thread. In UP this will be initialised with the boot CPU ID.
104  */
105 u8 hwthread_id_2_cpu[4] __read_mostly = {
106         [0 ... 3] = BAD_CPU_ID
107 };
108
109 /* The relative offset of the MMU mapped memory (from ldlk or bootloader)
110  * to the real physical memory.  This is needed as we have to use the
111  * physical addresses in the MMU tables (pte entries), and not the virtual
112  * addresses.
113  * This variable is used in the __pa() and __va() macros, and should
114  * probably only be used via them.
115  */
116 unsigned int meta_memoffset;
117
118 static char __initdata *original_cmd_line;
119
120 DEFINE_PER_CPU(PTBI, pTBI);
121
122 /*
123  * Mapping are specified as "CPU_ID:HWTHREAD_ID", e.g.
124  *
125  *      "hwthread_map=0:1,1:2,2:3,3:0"
126  *
127  *      Linux CPU ID    HWTHREAD_ID
128  *      ---------------------------
129  *          0                 1
130  *          1                 2
131  *          2                 3
132  *          3                 0
133  */
134 static int __init parse_hwthread_map(char *p)
135 {
136         int cpu;
137
138         while (*p) {
139                 cpu = (*p++) - '0';
140                 if (cpu < 0 || cpu > 9)
141                         goto err_cpu;
142
143                 p++;            /* skip semi-colon */
144                 cpu_2_hwthread_id[cpu] = (*p++) - '0';
145                 if (cpu_2_hwthread_id[cpu] >= 4)
146                         goto err_thread;
147                 hwthread_id_2_cpu[cpu_2_hwthread_id[cpu]] = cpu;
148
149                 if (*p == ',')
150                         p++;            /* skip comma */
151         }
152
153         return 0;
154 err_cpu:
155         pr_err("%s: hwthread_map cpu argument out of range\n", __func__);
156         return -EINVAL;
157 err_thread:
158         pr_err("%s: hwthread_map thread argument out of range\n", __func__);
159         return -EINVAL;
160 }
161 early_param("hwthread_map", parse_hwthread_map);
162
163 void __init dump_machine_table(void)
164 {
165         struct machine_desc *p;
166         const char **compat;
167
168         pr_info("Available machine support:\n\tNAME\t\tCOMPATIBLE LIST\n");
169         for_each_machine_desc(p) {
170                 pr_info("\t%s\t[", p->name);
171                 for (compat = p->dt_compat; compat && *compat; ++compat)
172                         printk(" '%s'", *compat);
173                 printk(" ]\n");
174         }
175
176         pr_info("\nPlease check your kernel config and/or bootloader.\n");
177
178         hard_processor_halt(HALT_PANIC);
179 }
180
181 #ifdef CONFIG_METAG_HALT_ON_PANIC
182 static int metag_panic_event(struct notifier_block *this, unsigned long event,
183                              void *ptr)
184 {
185         hard_processor_halt(HALT_PANIC);
186         return NOTIFY_DONE;
187 }
188
189 static struct notifier_block metag_panic_block = {
190         metag_panic_event,
191         NULL,
192         0
193 };
194 #endif
195
196 void __init setup_arch(char **cmdline_p)
197 {
198         unsigned long start_pfn;
199         unsigned long text_start = (unsigned long)(&_stext);
200         unsigned long cpu = smp_processor_id();
201         unsigned long heap_start, heap_end;
202         unsigned long start_pte;
203         PTBI _pTBI;
204         PTBISEG p_heap;
205         int heap_id, i;
206
207         metag_cache_probe();
208
209         metag_da_probe();
210 #ifdef CONFIG_DA_CONSOLE
211         if (metag_da_enabled()) {
212                 /* An early channel based console driver */
213                 register_console(&dash_console);
214                 add_preferred_console("ttyDA", 1, NULL);
215         }
216 #endif
217
218         /* try interpreting the argument as a device tree */
219         machine_desc = setup_machine_fdt(original_cmd_line);
220         /* if it doesn't look like a device tree it must be a command line */
221         if (!machine_desc) {
222 #ifdef CONFIG_METAG_BUILTIN_DTB
223                 /* try the embedded device tree */
224                 machine_desc = setup_machine_fdt(__dtb_start);
225                 if (!machine_desc)
226                         panic("Invalid embedded device tree.");
227 #else
228                 /* use the default machine description */
229                 machine_desc = default_machine_desc();
230 #endif
231 #ifndef CONFIG_CMDLINE_FORCE
232                 /* append the bootloader cmdline to any builtin fdt cmdline */
233                 if (boot_command_line[0] && original_cmd_line[0])
234                         strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
235                 strlcat(boot_command_line, original_cmd_line,
236                         COMMAND_LINE_SIZE);
237 #endif
238         }
239         setup_meta_clocks(machine_desc->clocks);
240
241         *cmdline_p = boot_command_line;
242         parse_early_param();
243
244         /*
245          * Make sure we don't alias in dcache or icache
246          */
247         check_for_cache_aliasing(cpu);
248
249
250 #ifdef CONFIG_METAG_HALT_ON_PANIC
251         atomic_notifier_chain_register(&panic_notifier_list,
252                                        &metag_panic_block);
253 #endif
254
255 #ifdef CONFIG_DUMMY_CONSOLE
256         conswitchp = &dummy_con;
257 #endif
258
259         if (!(__core_reg_get(TXSTATUS) & TXSTATUS_PSTAT_BIT))
260                 panic("Privilege must be enabled for this thread.");
261
262         _pTBI = __TBI(TBID_ISTAT_BIT);
263
264         per_cpu(pTBI, cpu) = _pTBI;
265
266         if (!per_cpu(pTBI, cpu))
267                 panic("No TBI found!");
268
269         /*
270          * Initialize all interrupt vectors to our copy of __TBIUnExpXXX,
271          * rather than the version from the bootloader. This makes call
272          * stacks easier to understand and may allow us to unmap the
273          * bootloader at some point.
274          *
275          * We need to keep the LWK handler that TBI installed in order to
276          * be able to do inter-thread comms.
277          */
278         for (i = 0; i <= TBID_SIGNUM_MAX; i++)
279                 if (i != TBID_SIGNUM_LWK)
280                         _pTBI->fnSigs[i] = __TBIUnExpXXX;
281
282         /* A Meta requirement is that the kernel is loaded (virtually)
283          * at the PAGE_OFFSET.
284          */
285         if (PAGE_OFFSET != text_start)
286                 panic("Kernel not loaded at PAGE_OFFSET (%#x) but at %#lx.",
287                       PAGE_OFFSET, text_start);
288
289         start_pte = mmu_read_second_level_page(text_start);
290
291         /*
292          * Kernel pages should have the PRIV bit set by the bootloader.
293          */
294         if (!(start_pte & _PAGE_KERNEL))
295                 panic("kernel pte does not have PRIV set");
296
297         /*
298          * See __pa and __va in include/asm/page.h.
299          * This value is negative when running in local space but the
300          * calculations work anyway.
301          */
302         meta_memoffset = text_start - (start_pte & PAGE_MASK);
303
304         /* Now lets look at the heap space */
305         heap_id = (__TBIThreadId() & TBID_THREAD_BITS)
306                 + TBID_SEG(0, TBID_SEGSCOPE_LOCAL, TBID_SEGTYPE_HEAP);
307
308         p_heap = __TBIFindSeg(NULL, heap_id);
309
310         if (!p_heap)
311                 panic("Could not find heap from TBI!");
312
313         /* The heap begins at the first full page after the kernel data. */
314         heap_start = (unsigned long) &_heap_start;
315
316         /* The heap ends at the end of the heap segment specified with
317          * ldlk.
318          */
319         if (is_global_space(text_start)) {
320                 pr_debug("WARNING: running in global space!\n");
321                 heap_end = (unsigned long)p_heap->pGAddr + p_heap->Bytes;
322         } else {
323                 heap_end = (unsigned long)p_heap->pLAddr + p_heap->Bytes;
324         }
325
326         ROOT_DEV = Root_RAM0;
327
328         /* init_mm is the mm struct used for the first task.  It is then
329          * cloned for all other tasks spawned from that task.
330          *
331          * Note - we are using the virtual addresses here.
332          */
333         init_mm.start_code = (unsigned long)(&_stext);
334         init_mm.end_code = (unsigned long)(&_etext);
335         init_mm.end_data = (unsigned long)(&_edata);
336         init_mm.brk = (unsigned long)heap_start;
337
338         min_low_pfn = PFN_UP(__pa(text_start));
339         max_low_pfn = PFN_DOWN(__pa(heap_end));
340
341         pfn_base = min_low_pfn;
342
343         /* Round max_pfn up to a 4Mb boundary. The free_bootmem_node()
344          * call later makes sure to keep the rounded up pages marked reserved.
345          */
346         max_pfn = max_low_pfn + ((1 << MAX_ORDER) - 1);
347         max_pfn &= ~((1 << MAX_ORDER) - 1);
348
349         start_pfn = PFN_UP(__pa(heap_start));
350
351         if (min_low_pfn & ((1 << MAX_ORDER) - 1)) {
352                 /* Theoretically, we could expand the space that the
353                  * bootmem allocator covers - much as we do for the
354                  * 'high' address, and then tell the bootmem system
355                  * that the lowest chunk is 'not available'.  Right
356                  * now it is just much easier to constrain the
357                  * user to always MAX_ORDER align their kernel space.
358                  */
359
360                 panic("Kernel must be %d byte aligned, currently at %#lx.",
361                       1 << (MAX_ORDER + PAGE_SHIFT),
362                       min_low_pfn << PAGE_SHIFT);
363         }
364
365 #ifdef CONFIG_HIGHMEM
366         highstart_pfn = highend_pfn = max_pfn;
367         high_memory = (void *) __va(PFN_PHYS(highstart_pfn));
368 #else
369         high_memory = (void *)__va(PFN_PHYS(max_pfn));
370 #endif
371
372         paging_init(heap_end);
373
374         setup_txprivext();
375
376         /* Setup the boot cpu's mapping. The rest will be setup below. */
377         cpu_2_hwthread_id[smp_processor_id()] = hard_processor_id();
378         hwthread_id_2_cpu[hard_processor_id()] = smp_processor_id();
379
380         unflatten_device_tree();
381
382 #ifdef CONFIG_SMP
383         smp_init_cpus();
384 #endif
385
386         if (machine_desc->init_early)
387                 machine_desc->init_early();
388 }
389
390 static int __init customize_machine(void)
391 {
392         /* customizes platform devices, or adds new ones */
393         if (machine_desc->init_machine)
394                 machine_desc->init_machine();
395         return 0;
396 }
397 arch_initcall(customize_machine);
398
399 static int __init init_machine_late(void)
400 {
401         if (machine_desc->init_late)
402                 machine_desc->init_late();
403         return 0;
404 }
405 late_initcall(init_machine_late);
406
407 #ifdef CONFIG_PROC_FS
408 /*
409  *      Get CPU information for use by the procfs.
410  */
411 static const char *get_cpu_capabilities(unsigned int txenable)
412 {
413 #ifdef CONFIG_METAG_META21
414         /* See CORE_ID in META HTP.GP TRM - Architecture Overview 2.1.238 */
415         int coreid = metag_in32(METAC_CORE_ID);
416         unsigned int dsp_type = (coreid >> 3) & 7;
417         unsigned int fpu_type = (coreid >> 7) & 3;
418
419         switch (dsp_type | fpu_type << 3) {
420         case (0x00): return "EDSP";
421         case (0x01): return "DSP";
422         case (0x08): return "EDSP+LFPU";
423         case (0x09): return "DSP+LFPU";
424         case (0x10): return "EDSP+FPU";
425         case (0x11): return "DSP+FPU";
426         }
427         return "UNKNOWN";
428
429 #else
430         if (!(txenable & TXENABLE_CLASS_BITS))
431                 return "DSP";
432         else
433                 return "";
434 #endif
435 }
436
437 static int show_cpuinfo(struct seq_file *m, void *v)
438 {
439         const char *cpu;
440         unsigned int txenable, thread_id, major, minor;
441         unsigned long clockfreq = get_coreclock();
442 #ifdef CONFIG_SMP
443         int i;
444         unsigned long lpj;
445 #endif
446
447         cpu = "META";
448
449         txenable = __core_reg_get(TXENABLE);
450         major = (txenable & TXENABLE_MAJOR_REV_BITS) >> TXENABLE_MAJOR_REV_S;
451         minor = (txenable & TXENABLE_MINOR_REV_BITS) >> TXENABLE_MINOR_REV_S;
452         thread_id = (txenable >> 8) & 0x3;
453
454 #ifdef CONFIG_SMP
455         for_each_online_cpu(i) {
456                 lpj = per_cpu(cpu_data, i).loops_per_jiffy;
457                 txenable = core_reg_read(TXUCT_ID, TXENABLE_REGNUM,
458                                                         cpu_2_hwthread_id[i]);
459
460                 seq_printf(m, "CPU:\t\t%s %d.%d (thread %d)\n"
461                               "Clocking:\t%lu.%1luMHz\n"
462                               "BogoMips:\t%lu.%02lu\n"
463                               "Calibration:\t%lu loops\n"
464                               "Capabilities:\t%s\n\n",
465                               cpu, major, minor, i,
466                               clockfreq / 1000000, (clockfreq / 100000) % 10,
467                               lpj / (500000 / HZ), (lpj / (5000 / HZ)) % 100,
468                               lpj,
469                               get_cpu_capabilities(txenable));
470         }
471 #else
472         seq_printf(m, "CPU:\t\t%s %d.%d (thread %d)\n"
473                    "Clocking:\t%lu.%1luMHz\n"
474                    "BogoMips:\t%lu.%02lu\n"
475                    "Calibration:\t%lu loops\n"
476                    "Capabilities:\t%s\n",
477                    cpu, major, minor, thread_id,
478                    clockfreq / 1000000, (clockfreq / 100000) % 10,
479                    loops_per_jiffy / (500000 / HZ),
480                    (loops_per_jiffy / (5000 / HZ)) % 100,
481                    loops_per_jiffy,
482                    get_cpu_capabilities(txenable));
483 #endif /* CONFIG_SMP */
484
485 #ifdef CONFIG_METAG_L2C
486         if (meta_l2c_is_present()) {
487                 seq_printf(m, "L2 cache:\t%s\n"
488                               "L2 cache size:\t%d KB\n",
489                               meta_l2c_is_enabled() ? "enabled" : "disabled",
490                               meta_l2c_size() >> 10);
491         }
492 #endif
493         return 0;
494 }
495
496 static void *c_start(struct seq_file *m, loff_t *pos)
497 {
498         return (void *)(*pos == 0);
499 }
500 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
501 {
502         return NULL;
503 }
504 static void c_stop(struct seq_file *m, void *v)
505 {
506 }
507 const struct seq_operations cpuinfo_op = {
508         .start = c_start,
509         .next  = c_next,
510         .stop  = c_stop,
511         .show  = show_cpuinfo,
512 };
513 #endif /* CONFIG_PROC_FS */
514
515 void __init metag_start_kernel(char *args)
516 {
517         /* Zero the timer register so timestamps are from the point at
518          * which the kernel started running.
519          */
520         __core_reg_set(TXTIMER, 0);
521
522         /* Clear the bss. */
523         memset(__bss_start, 0,
524                (unsigned long)__bss_stop - (unsigned long)__bss_start);
525
526         /* Remember where these are for use in setup_arch */
527         original_cmd_line = args;
528
529         current_thread_info()->cpu = hard_processor_id();
530
531         start_kernel();
532 }
533
534 /*
535  * Setup TXPRIVEXT register to be prevent userland from touching our
536  * precious registers.
537  */
538 void setup_txprivext(void)
539 {
540         __core_reg_set(TXPRIVEXT, PRIV_BITS);
541 }
542
543 PTBI pTBI_get(unsigned int cpu)
544 {
545         return per_cpu(pTBI, cpu);
546 }
547
548 #if defined(CONFIG_METAG_DSP) && defined(CONFIG_METAG_FPU)
549 char capabilites[] = "dsp fpu";
550 #elif defined(CONFIG_METAG_DSP)
551 char capabilites[] = "dsp";
552 #elif defined(CONFIG_METAG_FPU)
553 char capabilites[] = "fpu";
554 #else
555 char capabilites[] = "";
556 #endif
557
558 static struct ctl_table caps_kern_table[] = {
559         {
560                 .procname       = "capabilities",
561                 .data           = capabilites,
562                 .maxlen         = sizeof(capabilites),
563                 .mode           = 0444,
564                 .proc_handler   = proc_dostring,
565         },
566         {}
567 };
568
569 static struct ctl_table caps_root_table[] = {
570         {
571                 .procname       = "kernel",
572                 .mode           = 0555,
573                 .child          = caps_kern_table,
574         },
575         {}
576 };
577
578 static int __init capabilities_register_sysctl(void)
579 {
580         struct ctl_table_header *caps_table_header;
581
582         caps_table_header = register_sysctl_table(caps_root_table);
583         if (!caps_table_header) {
584                 pr_err("Unable to register CAPABILITIES sysctl\n");
585                 return -ENOMEM;
586         }
587
588         return 0;
589 }
590
591 core_initcall(capabilities_register_sysctl);