2 * Copyright (C) 2005-2012 Imagination Technologies Ltd.
4 * This file contains the architecture-dependant parts of system setup.
8 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/delay.h>
12 #include <linux/interrupt.h>
14 #include <linux/console.h>
15 #include <linux/genhd.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/init.h>
19 #include <linux/bootmem.h>
20 #include <linux/root_dev.h>
21 #include <linux/initrd.h>
22 #include <linux/seq_file.h>
23 #include <linux/pfn.h>
24 #include <linux/start_kernel.h>
25 #include <linux/cpu.h>
26 #include <linux/memblock.h>
27 #include <linux/of_fdt.h>
29 #include <asm/cachepart.h>
30 #include <asm/clock.h>
31 #include <asm/sections.h>
32 #include <asm/setup.h>
33 #include <asm/processor.h>
34 #include <asm/traps.h>
37 #include <asm/hwthread.h>
38 #include <asm/mmzone.h>
39 #include <asm/l2cache.h>
41 #include <asm/mach/arch.h>
42 #include <asm/core_reg.h>
43 #include <asm/highmem.h>
45 /* PRIV protect as many registers as possible. */
46 #define DEFAULT_PRIV 0xff0f7f00
48 /* Enable unaligned access checking. */
49 #define UNALIGNED_PRIV 0x00000010
51 #ifdef CONFIG_METAG_UNALIGNED
52 #define PRIV_BITS (DEFAULT_PRIV | UNALIGNED_PRIV)
54 #define PRIV_BITS DEFAULT_PRIV
57 extern char _heap_start[];
59 #ifdef CONFIG_METAG_BUILTIN_DTB
60 extern u32 __dtb_start[];
63 struct machine_desc *machine_desc __initdata;
66 * Map a Linux CPU number to a hardware thread ID
67 * In SMP this will be setup with the correct mapping at startup; in UP this
68 * will map to the HW thread on which we are running.
70 u8 cpu_2_hwthread_id[NR_CPUS] __read_mostly = {
71 [0 ... NR_CPUS-1] = BAD_HWTHREAD_ID
75 * Map a hardware thread ID to a Linux CPU number
76 * In SMP this will be fleshed out with the correct CPU ID for a particular
77 * hardware thread. In UP this will be initialised with the boot CPU ID.
79 u8 hwthread_id_2_cpu[4] __read_mostly = {
80 [0 ... 3] = BAD_CPU_ID
83 /* The relative offset of the MMU mapped memory (from ldlk or bootloader)
84 * to the real physical memory. This is needed as we have to use the
85 * physical addresses in the MMU tables (pte entries), and not the virtual
87 * This variable is used in the __pa() and __va() macros, and should
88 * probably only be used via them.
90 unsigned int meta_memoffset;
92 static char __initdata *original_cmd_line;
94 DEFINE_PER_CPU(PTBI, pTBI);
97 * Mapping are specified as "CPU_ID:HWTHREAD_ID", e.g.
99 * "hwthread_map=0:1,1:2,2:3,3:0"
101 * Linux CPU ID HWTHREAD_ID
102 * ---------------------------
108 static int __init parse_hwthread_map(char *p)
114 if (cpu < 0 || cpu > 9)
117 p++; /* skip semi-colon */
118 cpu_2_hwthread_id[cpu] = (*p++) - '0';
119 if (cpu_2_hwthread_id[cpu] >= 4)
121 hwthread_id_2_cpu[cpu_2_hwthread_id[cpu]] = cpu;
124 p++; /* skip comma */
129 pr_err("%s: hwthread_map cpu argument out of range\n", __func__);
132 pr_err("%s: hwthread_map thread argument out of range\n", __func__);
135 early_param("hwthread_map", parse_hwthread_map);
137 void __init dump_machine_table(void)
139 struct machine_desc *p;
142 pr_info("Available machine support:\n\tNAME\t\tCOMPATIBLE LIST\n");
143 for_each_machine_desc(p) {
144 pr_info("\t%s\t[", p->name);
145 for (compat = p->dt_compat; compat && *compat; ++compat)
146 printk(" '%s'", *compat);
150 pr_info("\nPlease check your kernel config and/or bootloader.\n");
152 hard_processor_halt(HALT_PANIC);
155 #ifdef CONFIG_METAG_HALT_ON_PANIC
156 static int metag_panic_event(struct notifier_block *this, unsigned long event,
159 hard_processor_halt(HALT_PANIC);
163 static struct notifier_block metag_panic_block = {
170 void __init setup_arch(char **cmdline_p)
172 unsigned long start_pfn;
173 unsigned long text_start = (unsigned long)(&_stext);
174 unsigned long cpu = smp_processor_id();
175 unsigned long heap_start, heap_end;
176 unsigned long start_pte;
183 /* try interpreting the argument as a device tree */
184 machine_desc = setup_machine_fdt(original_cmd_line);
185 /* if it doesn't look like a device tree it must be a command line */
187 #ifdef CONFIG_METAG_BUILTIN_DTB
188 /* try the embedded device tree */
189 machine_desc = setup_machine_fdt(__dtb_start);
191 panic("Invalid embedded device tree.");
193 /* use the default machine description */
194 machine_desc = default_machine_desc();
196 #ifndef CONFIG_CMDLINE_FORCE
197 /* append the bootloader cmdline to any builtin fdt cmdline */
198 if (boot_command_line[0] && original_cmd_line[0])
199 strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
200 strlcat(boot_command_line, original_cmd_line,
204 setup_meta_clocks(machine_desc->clocks);
206 *cmdline_p = boot_command_line;
210 * Make sure we don't alias in dcache or icache
212 check_for_cache_aliasing(cpu);
215 #ifdef CONFIG_METAG_HALT_ON_PANIC
216 atomic_notifier_chain_register(&panic_notifier_list,
220 #ifdef CONFIG_DUMMY_CONSOLE
221 conswitchp = &dummy_con;
224 if (!(__core_reg_get(TXSTATUS) & TXSTATUS_PSTAT_BIT))
225 panic("Privilege must be enabled for this thread.");
227 _pTBI = __TBI(TBID_ISTAT_BIT);
229 per_cpu(pTBI, cpu) = _pTBI;
231 if (!per_cpu(pTBI, cpu))
232 panic("No TBI found!");
235 * Initialize all interrupt vectors to our copy of __TBIUnExpXXX,
236 * rather than the version from the bootloader. This makes call
237 * stacks easier to understand and may allow us to unmap the
238 * bootloader at some point.
240 * We need to keep the LWK handler that TBI installed in order to
241 * be able to do inter-thread comms.
243 for (i = 0; i <= TBID_SIGNUM_MAX; i++)
244 if (i != TBID_SIGNUM_LWK)
245 _pTBI->fnSigs[i] = __TBIUnExpXXX;
247 /* A Meta requirement is that the kernel is loaded (virtually)
248 * at the PAGE_OFFSET.
250 if (PAGE_OFFSET != text_start)
251 panic("Kernel not loaded at PAGE_OFFSET (%#x) but at %#lx.",
252 PAGE_OFFSET, text_start);
254 start_pte = mmu_read_second_level_page(text_start);
257 * Kernel pages should have the PRIV bit set by the bootloader.
259 if (!(start_pte & _PAGE_KERNEL))
260 panic("kernel pte does not have PRIV set");
263 * See __pa and __va in include/asm/page.h.
264 * This value is negative when running in local space but the
265 * calculations work anyway.
267 meta_memoffset = text_start - (start_pte & PAGE_MASK);
269 /* Now lets look at the heap space */
270 heap_id = (__TBIThreadId() & TBID_THREAD_BITS)
271 + TBID_SEG(0, TBID_SEGSCOPE_LOCAL, TBID_SEGTYPE_HEAP);
273 p_heap = __TBIFindSeg(NULL, heap_id);
276 panic("Could not find heap from TBI!");
278 /* The heap begins at the first full page after the kernel data. */
279 heap_start = (unsigned long) &_heap_start;
281 /* The heap ends at the end of the heap segment specified with
284 if (is_global_space(text_start)) {
285 pr_debug("WARNING: running in global space!\n");
286 heap_end = (unsigned long)p_heap->pGAddr + p_heap->Bytes;
288 heap_end = (unsigned long)p_heap->pLAddr + p_heap->Bytes;
291 ROOT_DEV = Root_RAM0;
293 /* init_mm is the mm struct used for the first task. It is then
294 * cloned for all other tasks spawned from that task.
296 * Note - we are using the virtual addresses here.
298 init_mm.start_code = (unsigned long)(&_stext);
299 init_mm.end_code = (unsigned long)(&_etext);
300 init_mm.end_data = (unsigned long)(&_edata);
301 init_mm.brk = (unsigned long)heap_start;
303 min_low_pfn = PFN_UP(__pa(text_start));
304 max_low_pfn = PFN_DOWN(__pa(heap_end));
306 pfn_base = min_low_pfn;
308 /* Round max_pfn up to a 4Mb boundary. The free_bootmem_node()
309 * call later makes sure to keep the rounded up pages marked reserved.
311 max_pfn = max_low_pfn + ((1 << MAX_ORDER) - 1);
312 max_pfn &= ~((1 << MAX_ORDER) - 1);
314 start_pfn = PFN_UP(__pa(heap_start));
316 if (min_low_pfn & ((1 << MAX_ORDER) - 1)) {
317 /* Theoretically, we could expand the space that the
318 * bootmem allocator covers - much as we do for the
319 * 'high' address, and then tell the bootmem system
320 * that the lowest chunk is 'not available'. Right
321 * now it is just much easier to constrain the
322 * user to always MAX_ORDER align their kernel space.
325 panic("Kernel must be %d byte aligned, currently at %#lx.",
326 1 << (MAX_ORDER + PAGE_SHIFT),
327 min_low_pfn << PAGE_SHIFT);
330 #ifdef CONFIG_HIGHMEM
331 highstart_pfn = highend_pfn = max_pfn;
332 high_memory = (void *) __va(PFN_PHYS(highstart_pfn));
334 high_memory = (void *)__va(PFN_PHYS(max_pfn));
337 paging_init(heap_end);
341 /* Setup the boot cpu's mapping. The rest will be setup below. */
342 cpu_2_hwthread_id[smp_processor_id()] = hard_processor_id();
343 hwthread_id_2_cpu[hard_processor_id()] = smp_processor_id();
345 unflatten_device_tree();
351 if (machine_desc->init_early)
352 machine_desc->init_early();
355 static int __init customize_machine(void)
357 /* customizes platform devices, or adds new ones */
358 if (machine_desc->init_machine)
359 machine_desc->init_machine();
362 arch_initcall(customize_machine);
364 static int __init init_machine_late(void)
366 if (machine_desc->init_late)
367 machine_desc->init_late();
370 late_initcall(init_machine_late);
372 #ifdef CONFIG_PROC_FS
374 * Get CPU information for use by the procfs.
376 static const char *get_cpu_capabilities(unsigned int txenable)
378 #ifdef CONFIG_METAG_META21
379 /* See CORE_ID in META HTP.GP TRM - Architecture Overview 2.1.238 */
380 int coreid = metag_in32(METAC_CORE_ID);
381 unsigned int dsp_type = (coreid >> 3) & 7;
382 unsigned int fpu_type = (coreid >> 7) & 3;
384 switch (dsp_type | fpu_type << 3) {
385 case (0x00): return "EDSP";
386 case (0x01): return "DSP";
387 case (0x08): return "EDSP+LFPU";
388 case (0x09): return "DSP+LFPU";
389 case (0x10): return "EDSP+FPU";
390 case (0x11): return "DSP+FPU";
395 if (!(txenable & TXENABLE_CLASS_BITS))
402 static int show_cpuinfo(struct seq_file *m, void *v)
405 unsigned int txenable, thread_id, major, minor;
406 unsigned long clockfreq = get_coreclock();
414 txenable = __core_reg_get(TXENABLE);
415 major = (txenable & TXENABLE_MAJOR_REV_BITS) >> TXENABLE_MAJOR_REV_S;
416 minor = (txenable & TXENABLE_MINOR_REV_BITS) >> TXENABLE_MINOR_REV_S;
417 thread_id = (txenable >> 8) & 0x3;
420 for_each_online_cpu(i) {
421 lpj = per_cpu(cpu_data, i).loops_per_jiffy;
422 txenable = core_reg_read(TXUCT_ID, TXENABLE_REGNUM,
423 cpu_2_hwthread_id[i]);
425 seq_printf(m, "CPU:\t\t%s %d.%d (thread %d)\n"
426 "Clocking:\t%lu.%1luMHz\n"
427 "BogoMips:\t%lu.%02lu\n"
428 "Calibration:\t%lu loops\n"
429 "Capabilities:\t%s\n\n",
430 cpu, major, minor, i,
431 clockfreq / 1000000, (clockfreq / 100000) % 10,
432 lpj / (500000 / HZ), (lpj / (5000 / HZ)) % 100,
434 get_cpu_capabilities(txenable));
437 seq_printf(m, "CPU:\t\t%s %d.%d (thread %d)\n"
438 "Clocking:\t%lu.%1luMHz\n"
439 "BogoMips:\t%lu.%02lu\n"
440 "Calibration:\t%lu loops\n"
441 "Capabilities:\t%s\n",
442 cpu, major, minor, thread_id,
443 clockfreq / 1000000, (clockfreq / 100000) % 10,
444 loops_per_jiffy / (500000 / HZ),
445 (loops_per_jiffy / (5000 / HZ)) % 100,
447 get_cpu_capabilities(txenable));
448 #endif /* CONFIG_SMP */
450 #ifdef CONFIG_METAG_L2C
451 if (meta_l2c_is_present()) {
452 seq_printf(m, "L2 cache:\t%s\n"
453 "L2 cache size:\t%d KB\n",
454 meta_l2c_is_enabled() ? "enabled" : "disabled",
455 meta_l2c_size() >> 10);
461 static void *c_start(struct seq_file *m, loff_t *pos)
463 return (void *)(*pos == 0);
465 static void *c_next(struct seq_file *m, void *v, loff_t *pos)
469 static void c_stop(struct seq_file *m, void *v)
472 const struct seq_operations cpuinfo_op = {
476 .show = show_cpuinfo,
478 #endif /* CONFIG_PROC_FS */
480 void __init metag_start_kernel(char *args)
482 /* Zero the timer register so timestamps are from the point at
483 * which the kernel started running.
485 __core_reg_set(TXTIMER, 0);
488 memset(__bss_start, 0,
489 (unsigned long)__bss_stop - (unsigned long)__bss_start);
491 /* Remember where these are for use in setup_arch */
492 original_cmd_line = args;
494 current_thread_info()->cpu = hard_processor_id();
500 * Setup TXPRIVEXT register to be prevent userland from touching our
501 * precious registers.
503 void setup_txprivext(void)
505 __core_reg_set(TXPRIVEXT, PRIV_BITS);
508 PTBI pTBI_get(unsigned int cpu)
510 return per_cpu(pTBI, cpu);
513 #if defined(CONFIG_METAG_DSP) && defined(CONFIG_METAG_FPU)
514 char capabilites[] = "dsp fpu";
515 #elif defined(CONFIG_METAG_DSP)
516 char capabilites[] = "dsp";
517 #elif defined(CONFIG_METAG_FPU)
518 char capabilites[] = "fpu";
520 char capabilites[] = "";
523 static struct ctl_table caps_kern_table[] = {
525 .procname = "capabilities",
527 .maxlen = sizeof(capabilites),
529 .proc_handler = proc_dostring,
534 static struct ctl_table caps_root_table[] = {
536 .procname = "kernel",
538 .child = caps_kern_table,
543 static int __init capabilities_register_sysctl(void)
545 struct ctl_table_header *caps_table_header;
547 caps_table_header = register_sysctl_table(caps_root_table);
548 if (!caps_table_header) {
549 pr_err("Unable to register CAPABILITIES sysctl\n");
556 core_initcall(capabilities_register_sysctl);