setup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/seq_file.h>
  9. #include <linux/fs.h>
  10. #include <linux/delay.h>
  11. #include <linux/root_dev.h>
  12. #include <linux/clk.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/console.h>
  16. #include <linux/module.h>
  17. #include <linux/cpu.h>
  18. #include <linux/of_fdt.h>
  19. #include <linux/of.h>
  20. #include <linux/cache.h>
  21. #include <asm/sections.h>
  22. #include <asm/arcregs.h>
  23. #include <asm/tlb.h>
  24. #include <asm/setup.h>
  25. #include <asm/page.h>
  26. #include <asm/irq.h>
  27. #include <asm/unwind.h>
  28. #include <asm/mach_desc.h>
  29. #include <asm/smp.h>
  30. #define FIX_PTR(x) __asm__ __volatile__(";" : "+r"(x))
  31. unsigned int intr_to_DE_cnt;
  32. /* Part of U-boot ABI: see head.S */
  33. int __initdata uboot_tag;
  34. char __initdata *uboot_arg;
  35. const struct machine_desc *machine_desc;
  36. struct task_struct *_current_task[NR_CPUS]; /* For stack switching */
  37. struct cpuinfo_arc cpuinfo_arc700[NR_CPUS];
  38. static const struct id_to_str arc_cpu_rel[] = {
  39. #ifdef CONFIG_ISA_ARCOMPACT
  40. { 0x34, "R4.10"},
  41. { 0x35, "R4.11"},
  42. #else
  43. { 0x51, "R2.0" },
  44. { 0x52, "R2.1" },
  45. { 0x53, "R3.0" },
  46. #endif
  47. { 0x00, NULL }
  48. };
  49. static const struct id_to_str arc_cpu_nm[] = {
  50. #ifdef CONFIG_ISA_ARCOMPACT
  51. { 0x20, "ARC 600" },
  52. { 0x30, "ARC 770" }, /* 750 identified seperately */
  53. #else
  54. { 0x40, "ARC EM" },
  55. { 0x50, "ARC HS38" },
  56. #endif
  57. { 0x00, "Unknown" }
  58. };
  59. static void read_decode_ccm_bcr(struct cpuinfo_arc *cpu)
  60. {
  61. if (is_isa_arcompact()) {
  62. struct bcr_iccm_arcompact iccm;
  63. struct bcr_dccm_arcompact dccm;
  64. READ_BCR(ARC_REG_ICCM_BUILD, iccm);
  65. if (iccm.ver) {
  66. cpu->iccm.sz = 4096 << iccm.sz; /* 8K to 512K */
  67. cpu->iccm.base_addr = iccm.base << 16;
  68. }
  69. READ_BCR(ARC_REG_DCCM_BUILD, dccm);
  70. if (dccm.ver) {
  71. unsigned long base;
  72. cpu->dccm.sz = 2048 << dccm.sz; /* 2K to 256K */
  73. base = read_aux_reg(ARC_REG_DCCM_BASE_BUILD);
  74. cpu->dccm.base_addr = base & ~0xF;
  75. }
  76. } else {
  77. struct bcr_iccm_arcv2 iccm;
  78. struct bcr_dccm_arcv2 dccm;
  79. unsigned long region;
  80. READ_BCR(ARC_REG_ICCM_BUILD, iccm);
  81. if (iccm.ver) {
  82. cpu->iccm.sz = 256 << iccm.sz00; /* 512B to 16M */
  83. if (iccm.sz00 == 0xF && iccm.sz01 > 0)
  84. cpu->iccm.sz <<= iccm.sz01;
  85. region = read_aux_reg(ARC_REG_AUX_ICCM);
  86. cpu->iccm.base_addr = region & 0xF0000000;
  87. }
  88. READ_BCR(ARC_REG_DCCM_BUILD, dccm);
  89. if (dccm.ver) {
  90. cpu->dccm.sz = 256 << dccm.sz0;
  91. if (dccm.sz0 == 0xF && dccm.sz1 > 0)
  92. cpu->dccm.sz <<= dccm.sz1;
  93. region = read_aux_reg(ARC_REG_AUX_DCCM);
  94. cpu->dccm.base_addr = region & 0xF0000000;
  95. }
  96. }
  97. }
  98. static void read_arc_build_cfg_regs(void)
  99. {
  100. struct bcr_timer timer;
  101. struct bcr_generic bcr;
  102. struct cpuinfo_arc *cpu = &cpuinfo_arc700[smp_processor_id()];
  103. const struct id_to_str *tbl;
  104. FIX_PTR(cpu);
  105. READ_BCR(AUX_IDENTITY, cpu->core);
  106. READ_BCR(ARC_REG_ISA_CFG_BCR, cpu->isa);
  107. for (tbl = &arc_cpu_rel[0]; tbl->id != 0; tbl++) {
  108. if (cpu->core.family == tbl->id) {
  109. cpu->details = tbl->str;
  110. break;
  111. }
  112. }
  113. for (tbl = &arc_cpu_nm[0]; tbl->id != 0; tbl++) {
  114. if ((cpu->core.family & 0xF0) == tbl->id)
  115. break;
  116. }
  117. cpu->name = tbl->str;
  118. READ_BCR(ARC_REG_TIMERS_BCR, timer);
  119. cpu->extn.timer0 = timer.t0;
  120. cpu->extn.timer1 = timer.t1;
  121. cpu->extn.rtc = timer.rtc;
  122. cpu->vec_base = read_aux_reg(AUX_INTR_VEC_BASE);
  123. READ_BCR(ARC_REG_MUL_BCR, cpu->extn_mpy);
  124. cpu->extn.norm = read_aux_reg(ARC_REG_NORM_BCR) > 1 ? 1 : 0; /* 2,3 */
  125. cpu->extn.barrel = read_aux_reg(ARC_REG_BARREL_BCR) > 1 ? 1 : 0; /* 2,3 */
  126. cpu->extn.swap = read_aux_reg(ARC_REG_SWAP_BCR) ? 1 : 0; /* 1,3 */
  127. cpu->extn.crc = read_aux_reg(ARC_REG_CRC_BCR) ? 1 : 0;
  128. cpu->extn.minmax = read_aux_reg(ARC_REG_MIXMAX_BCR) > 1 ? 1 : 0; /* 2 */
  129. cpu->extn.swape = (cpu->core.family >= 0x34) ? 1 :
  130. IS_ENABLED(CONFIG_ARC_HAS_SWAPE);
  131. READ_BCR(ARC_REG_XY_MEM_BCR, cpu->extn_xymem);
  132. /* Read CCM BCRs for boot reporting even if not enabled in Kconfig */
  133. read_decode_ccm_bcr(cpu);
  134. read_decode_mmu_bcr();
  135. read_decode_cache_bcr();
  136. if (is_isa_arcompact()) {
  137. struct bcr_fp_arcompact sp, dp;
  138. struct bcr_bpu_arcompact bpu;
  139. READ_BCR(ARC_REG_FP_BCR, sp);
  140. READ_BCR(ARC_REG_DPFP_BCR, dp);
  141. cpu->extn.fpu_sp = sp.ver ? 1 : 0;
  142. cpu->extn.fpu_dp = dp.ver ? 1 : 0;
  143. READ_BCR(ARC_REG_BPU_BCR, bpu);
  144. cpu->bpu.ver = bpu.ver;
  145. cpu->bpu.full = bpu.fam ? 1 : 0;
  146. if (bpu.ent) {
  147. cpu->bpu.num_cache = 256 << (bpu.ent - 1);
  148. cpu->bpu.num_pred = 256 << (bpu.ent - 1);
  149. }
  150. } else {
  151. struct bcr_fp_arcv2 spdp;
  152. struct bcr_bpu_arcv2 bpu;
  153. READ_BCR(ARC_REG_FP_V2_BCR, spdp);
  154. cpu->extn.fpu_sp = spdp.sp ? 1 : 0;
  155. cpu->extn.fpu_dp = spdp.dp ? 1 : 0;
  156. READ_BCR(ARC_REG_BPU_BCR, bpu);
  157. cpu->bpu.ver = bpu.ver;
  158. cpu->bpu.full = bpu.ft;
  159. cpu->bpu.num_cache = 256 << bpu.bce;
  160. cpu->bpu.num_pred = 2048 << bpu.pte;
  161. }
  162. READ_BCR(ARC_REG_AP_BCR, bcr);
  163. cpu->extn.ap = bcr.ver ? 1 : 0;
  164. READ_BCR(ARC_REG_SMART_BCR, bcr);
  165. cpu->extn.smart = bcr.ver ? 1 : 0;
  166. READ_BCR(ARC_REG_RTT_BCR, bcr);
  167. cpu->extn.rtt = bcr.ver ? 1 : 0;
  168. cpu->extn.debug = cpu->extn.ap | cpu->extn.smart | cpu->extn.rtt;
  169. /* some hacks for lack of feature BCR info in old ARC700 cores */
  170. if (is_isa_arcompact()) {
  171. if (!cpu->isa.ver) /* ISA BCR absent, use Kconfig info */
  172. cpu->isa.atomic = IS_ENABLED(CONFIG_ARC_HAS_LLSC);
  173. else
  174. cpu->isa.atomic = cpu->isa.atomic1;
  175. cpu->isa.be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
  176. /* there's no direct way to distinguish 750 vs. 770 */
  177. if (unlikely(cpu->core.family < 0x34 || cpu->mmu.ver < 3))
  178. cpu->name = "ARC750";
  179. }
  180. }
  181. static char *arc_cpu_mumbojumbo(int cpu_id, char *buf, int len)
  182. {
  183. struct cpuinfo_arc *cpu = &cpuinfo_arc700[cpu_id];
  184. struct bcr_identity *core = &cpu->core;
  185. int i, n = 0;
  186. FIX_PTR(cpu);
  187. n += scnprintf(buf + n, len - n,
  188. "\nIDENTITY\t: ARCVER [%#02x] ARCNUM [%#02x] CHIPID [%#4x]\n",
  189. core->family, core->cpu_id, core->chip_id);
  190. n += scnprintf(buf + n, len - n, "processor [%d]\t: %s %s (%s ISA) %s\n",
  191. cpu_id, cpu->name, cpu->details,
  192. is_isa_arcompact() ? "ARCompact" : "ARCv2",
  193. IS_AVAIL1(cpu->isa.be, "[Big-Endian]"));
  194. n += scnprintf(buf + n, len - n, "Timers\t\t: %s%s%s%s%s%s\nISA Extn\t: ",
  195. IS_AVAIL1(cpu->extn.timer0, "Timer0 "),
  196. IS_AVAIL1(cpu->extn.timer1, "Timer1 "),
  197. IS_AVAIL2(cpu->extn.rtc, "RTC [UP 64-bit] ", CONFIG_ARC_TIMERS_64BIT),
  198. IS_AVAIL2(cpu->extn.gfrc, "GFRC [SMP 64-bit] ", CONFIG_ARC_TIMERS_64BIT));
  199. n += i = scnprintf(buf + n, len - n, "%s%s%s%s%s",
  200. IS_AVAIL2(cpu->isa.atomic, "atomic ", CONFIG_ARC_HAS_LLSC),
  201. IS_AVAIL2(cpu->isa.ldd, "ll64 ", CONFIG_ARC_HAS_LL64),
  202. IS_AVAIL1(cpu->isa.unalign, "unalign (not used)"));
  203. if (i)
  204. n += scnprintf(buf + n, len - n, "\n\t\t: ");
  205. if (cpu->extn_mpy.ver) {
  206. if (cpu->extn_mpy.ver <= 0x2) { /* ARCompact */
  207. n += scnprintf(buf + n, len - n, "mpy ");
  208. } else {
  209. int opt = 2; /* stock MPY/MPYH */
  210. if (cpu->extn_mpy.dsp) /* OPT 7-9 */
  211. opt = cpu->extn_mpy.dsp + 6;
  212. n += scnprintf(buf + n, len - n, "mpy[opt %d] ", opt);
  213. }
  214. }
  215. n += scnprintf(buf + n, len - n, "%s%s%s%s%s%s%s%s\n",
  216. IS_AVAIL1(cpu->isa.div_rem, "div_rem "),
  217. IS_AVAIL1(cpu->extn.norm, "norm "),
  218. IS_AVAIL1(cpu->extn.barrel, "barrel-shift "),
  219. IS_AVAIL1(cpu->extn.swap, "swap "),
  220. IS_AVAIL1(cpu->extn.minmax, "minmax "),
  221. IS_AVAIL1(cpu->extn.crc, "crc "),
  222. IS_AVAIL2(cpu->extn.swape, "swape", CONFIG_ARC_HAS_SWAPE));
  223. if (cpu->bpu.ver)
  224. n += scnprintf(buf + n, len - n,
  225. "BPU\t\t: %s%s match, cache:%d, Predict Table:%d\n",
  226. IS_AVAIL1(cpu->bpu.full, "full"),
  227. IS_AVAIL1(!cpu->bpu.full, "partial"),
  228. cpu->bpu.num_cache, cpu->bpu.num_pred);
  229. return buf;
  230. }
  231. static char *arc_extn_mumbojumbo(int cpu_id, char *buf, int len)
  232. {
  233. int n = 0;
  234. struct cpuinfo_arc *cpu = &cpuinfo_arc700[cpu_id];
  235. FIX_PTR(cpu);
  236. n += scnprintf(buf + n, len - n, "Vector Table\t: %#x\n", cpu->vec_base);
  237. if (cpu->extn.fpu_sp || cpu->extn.fpu_dp)
  238. n += scnprintf(buf + n, len - n, "FPU\t\t: %s%s\n",
  239. IS_AVAIL1(cpu->extn.fpu_sp, "SP "),
  240. IS_AVAIL1(cpu->extn.fpu_dp, "DP "));
  241. if (cpu->extn.debug)
  242. n += scnprintf(buf + n, len - n, "DEBUG\t\t: %s%s%s\n",
  243. IS_AVAIL1(cpu->extn.ap, "ActionPoint "),
  244. IS_AVAIL1(cpu->extn.smart, "smaRT "),
  245. IS_AVAIL1(cpu->extn.rtt, "RTT "));
  246. if (cpu->dccm.sz || cpu->iccm.sz)
  247. n += scnprintf(buf + n, len - n, "Extn [CCM]\t: DCCM @ %x, %d KB / ICCM: @ %x, %d KB\n",
  248. cpu->dccm.base_addr, TO_KB(cpu->dccm.sz),
  249. cpu->iccm.base_addr, TO_KB(cpu->iccm.sz));
  250. n += scnprintf(buf + n, len - n, "OS ABI [v%d]\t: %s\n",
  251. EF_ARC_OSABI_CURRENT >> 8,
  252. EF_ARC_OSABI_CURRENT == EF_ARC_OSABI_V3 ?
  253. "no-legacy-syscalls" : "64-bit data any register aligned");
  254. return buf;
  255. }
  256. static void arc_chk_core_config(void)
  257. {
  258. struct cpuinfo_arc *cpu = &cpuinfo_arc700[smp_processor_id()];
  259. int saved = 0, present = 0;
  260. char *opt_nm = NULL;;
  261. if (!cpu->extn.timer0)
  262. panic("Timer0 is not present!\n");
  263. if (!cpu->extn.timer1)
  264. panic("Timer1 is not present!\n");
  265. #ifdef CONFIG_ARC_HAS_DCCM
  266. /*
  267. * DCCM can be arbit placed in hardware.
  268. * Make sure it's placement/sz matches what Linux is built with
  269. */
  270. if ((unsigned int)__arc_dccm_base != cpu->dccm.base_addr)
  271. panic("Linux built with incorrect DCCM Base address\n");
  272. if (CONFIG_ARC_DCCM_SZ != cpu->dccm.sz)
  273. panic("Linux built with incorrect DCCM Size\n");
  274. #endif
  275. #ifdef CONFIG_ARC_HAS_ICCM
  276. if (CONFIG_ARC_ICCM_SZ != cpu->iccm.sz)
  277. panic("Linux built with incorrect ICCM Size\n");
  278. #endif
  279. /*
  280. * FP hardware/software config sanity
  281. * -If hardware present, kernel needs to save/restore FPU state
  282. * -If not, it will crash trying to save/restore the non-existant regs
  283. */
  284. if (is_isa_arcompact()) {
  285. opt_nm = "CONFIG_ARC_FPU_SAVE_RESTORE";
  286. saved = IS_ENABLED(CONFIG_ARC_FPU_SAVE_RESTORE);
  287. /* only DPDP checked since SP has no arch visible regs */
  288. present = cpu->extn.fpu_dp;
  289. } else {
  290. opt_nm = "CONFIG_ARC_HAS_ACCL_REGS";
  291. saved = IS_ENABLED(CONFIG_ARC_HAS_ACCL_REGS);
  292. /* Accumulator Low:High pair (r58:59) present if DSP MPY or FPU */
  293. present = cpu->extn_mpy.dsp | cpu->extn.fpu_sp | cpu->extn.fpu_dp;
  294. }
  295. if (present && !saved)
  296. pr_warn("Enable %s for working apps\n", opt_nm);
  297. else if (!present && saved)
  298. panic("Disable %s, hardware NOT present\n", opt_nm);
  299. }
  300. /*
  301. * Initialize and setup the processor core
  302. * This is called by all the CPUs thus should not do special case stuff
  303. * such as only for boot CPU etc
  304. */
  305. void setup_processor(void)
  306. {
  307. char str[512];
  308. int cpu_id = smp_processor_id();
  309. read_arc_build_cfg_regs();
  310. arc_init_IRQ();
  311. printk(arc_cpu_mumbojumbo(cpu_id, str, sizeof(str)));
  312. arc_mmu_init();
  313. arc_cache_init();
  314. printk(arc_extn_mumbojumbo(cpu_id, str, sizeof(str)));
  315. printk(arc_platform_smp_cpuinfo());
  316. arc_chk_core_config();
  317. }
  318. static inline int is_kernel(unsigned long addr)
  319. {
  320. if (addr >= (unsigned long)_stext && addr <= (unsigned long)_end)
  321. return 1;
  322. return 0;
  323. }
  324. void __init setup_arch(char **cmdline_p)
  325. {
  326. #ifdef CONFIG_ARC_UBOOT_SUPPORT
  327. /* make sure that uboot passed pointer to cmdline/dtb is valid */
  328. if (uboot_tag && is_kernel((unsigned long)uboot_arg))
  329. panic("Invalid uboot arg\n");
  330. /* See if u-boot passed an external Device Tree blob */
  331. machine_desc = setup_machine_fdt(uboot_arg); /* uboot_tag == 2 */
  332. if (!machine_desc)
  333. #endif
  334. {
  335. /* No, so try the embedded one */
  336. machine_desc = setup_machine_fdt(__dtb_start);
  337. if (!machine_desc)
  338. panic("Embedded DT invalid\n");
  339. /*
  340. * If we are here, it is established that @uboot_arg didn't
  341. * point to DT blob. Instead if u-boot says it is cmdline,
  342. * append to embedded DT cmdline.
  343. * setup_machine_fdt() would have populated @boot_command_line
  344. */
  345. if (uboot_tag == 1) {
  346. /* Ensure a whitespace between the 2 cmdlines */
  347. strlcat(boot_command_line, " ", COMMAND_LINE_SIZE);
  348. strlcat(boot_command_line, uboot_arg,
  349. COMMAND_LINE_SIZE);
  350. }
  351. }
  352. /* Save unparsed command line copy for /proc/cmdline */
  353. *cmdline_p = boot_command_line;
  354. /* To force early parsing of things like mem=xxx */
  355. parse_early_param();
  356. /* Platform/board specific: e.g. early console registration */
  357. if (machine_desc->init_early)
  358. machine_desc->init_early();
  359. smp_init_cpus();
  360. setup_processor();
  361. setup_arch_memory();
  362. /* copy flat DT out of .init and then unflatten it */
  363. unflatten_and_copy_device_tree();
  364. /* Can be issue if someone passes cmd line arg "ro"
  365. * But that is unlikely so keeping it as it is
  366. */
  367. root_mountflags &= ~MS_RDONLY;
  368. #if defined(CONFIG_VT) && defined(CONFIG_DUMMY_CONSOLE)
  369. conswitchp = &dummy_con;
  370. #endif
  371. arc_unwind_init();
  372. }
  373. /*
  374. * Called from start_kernel() - boot CPU only
  375. */
  376. void __init time_init(void)
  377. {
  378. of_clk_init(NULL);
  379. timer_probe();
  380. }
  381. static int __init customize_machine(void)
  382. {
  383. if (machine_desc->init_machine)
  384. machine_desc->init_machine();
  385. return 0;
  386. }
  387. arch_initcall(customize_machine);
  388. static int __init init_late_machine(void)
  389. {
  390. if (machine_desc->init_late)
  391. machine_desc->init_late();
  392. return 0;
  393. }
  394. late_initcall(init_late_machine);
  395. /*
  396. * Get CPU information for use by the procfs.
  397. */
  398. #define cpu_to_ptr(c) ((void *)(0xFFFF0000 | (unsigned int)(c)))
  399. #define ptr_to_cpu(p) (~0xFFFF0000UL & (unsigned int)(p))
  400. static int show_cpuinfo(struct seq_file *m, void *v)
  401. {
  402. char *str;
  403. int cpu_id = ptr_to_cpu(v);
  404. struct device *cpu_dev = get_cpu_device(cpu_id);
  405. struct clk *cpu_clk;
  406. unsigned long freq = 0;
  407. if (!cpu_online(cpu_id)) {
  408. seq_printf(m, "processor [%d]\t: Offline\n", cpu_id);
  409. goto done;
  410. }
  411. str = (char *)__get_free_page(GFP_TEMPORARY);
  412. if (!str)
  413. goto done;
  414. seq_printf(m, arc_cpu_mumbojumbo(cpu_id, str, PAGE_SIZE));
  415. cpu_clk = clk_get(cpu_dev, NULL);
  416. if (IS_ERR(cpu_clk)) {
  417. seq_printf(m, "CPU speed \t: Cannot get clock for processor [%d]\n",
  418. cpu_id);
  419. } else {
  420. freq = clk_get_rate(cpu_clk);
  421. }
  422. if (freq)
  423. seq_printf(m, "CPU speed\t: %lu.%02lu Mhz\n",
  424. freq / 1000000, (freq / 10000) % 100);
  425. seq_printf(m, "Bogo MIPS\t: %lu.%02lu\n",
  426. loops_per_jiffy / (500000 / HZ),
  427. (loops_per_jiffy / (5000 / HZ)) % 100);
  428. seq_printf(m, arc_mmu_mumbojumbo(cpu_id, str, PAGE_SIZE));
  429. seq_printf(m, arc_cache_mumbojumbo(cpu_id, str, PAGE_SIZE));
  430. seq_printf(m, arc_extn_mumbojumbo(cpu_id, str, PAGE_SIZE));
  431. seq_printf(m, arc_platform_smp_cpuinfo());
  432. free_page((unsigned long)str);
  433. done:
  434. seq_printf(m, "\n");
  435. return 0;
  436. }
  437. static void *c_start(struct seq_file *m, loff_t *pos)
  438. {
  439. /*
  440. * Callback returns cpu-id to iterator for show routine, NULL to stop.
  441. * However since NULL is also a valid cpu-id (0), we use a round-about
  442. * way to pass it w/o having to kmalloc/free a 2 byte string.
  443. * Encode cpu-id as 0xFFcccc, which is decoded by show routine.
  444. */
  445. return *pos < nr_cpu_ids ? cpu_to_ptr(*pos) : NULL;
  446. }
  447. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  448. {
  449. ++*pos;
  450. return c_start(m, pos);
  451. }
  452. static void c_stop(struct seq_file *m, void *v)
  453. {
  454. }
  455. const struct seq_operations cpuinfo_op = {
  456. .start = c_start,
  457. .next = c_next,
  458. .stop = c_stop,
  459. .show = show_cpuinfo
  460. };
  461. static DEFINE_PER_CPU(struct cpu, cpu_topology);
  462. static int __init topology_init(void)
  463. {
  464. int cpu;
  465. for_each_present_cpu(cpu)
  466. register_cpu(&per_cpu(cpu_topology, cpu), cpu);
  467. return 0;
  468. }
  469. subsys_initcall(topology_init);