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