setup.c 16 KB

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