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