setup.c 15 KB

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