cpu.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  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/init.h>
  9. #include <linux/sysdev.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/cpu.h>
  12. #include <linux/module.h>
  13. #include <linux/percpu.h>
  14. #include <linux/param.h>
  15. #include <linux/errno.h>
  16. #include <linux/clk.h>
  17. #include <asm/setup.h>
  18. #include <asm/sysreg.h>
  19. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  20. #ifdef CONFIG_PERFORMANCE_COUNTERS
  21. /*
  22. * XXX: If/when a SMP-capable implementation of AVR32 will ever be
  23. * made, we must make sure that the code executes on the correct CPU.
  24. */
  25. static ssize_t show_pc0event(struct sys_device *dev, char *buf)
  26. {
  27. unsigned long pccr;
  28. pccr = sysreg_read(PCCR);
  29. return sprintf(buf, "0x%lx\n", (pccr >> 12) & 0x3f);
  30. }
  31. static ssize_t store_pc0event(struct sys_device *dev, const char *buf,
  32. size_t count)
  33. {
  34. unsigned long val;
  35. char *endp;
  36. val = simple_strtoul(buf, &endp, 0);
  37. if (endp == buf || val > 0x3f)
  38. return -EINVAL;
  39. val = (val << 12) | (sysreg_read(PCCR) & 0xfffc0fff);
  40. sysreg_write(PCCR, val);
  41. return count;
  42. }
  43. static ssize_t show_pc0count(struct sys_device *dev, char *buf)
  44. {
  45. unsigned long pcnt0;
  46. pcnt0 = sysreg_read(PCNT0);
  47. return sprintf(buf, "%lu\n", pcnt0);
  48. }
  49. static ssize_t store_pc0count(struct sys_device *dev, const char *buf,
  50. size_t count)
  51. {
  52. unsigned long val;
  53. char *endp;
  54. val = simple_strtoul(buf, &endp, 0);
  55. if (endp == buf)
  56. return -EINVAL;
  57. sysreg_write(PCNT0, val);
  58. return count;
  59. }
  60. static ssize_t show_pc1event(struct sys_device *dev, char *buf)
  61. {
  62. unsigned long pccr;
  63. pccr = sysreg_read(PCCR);
  64. return sprintf(buf, "0x%lx\n", (pccr >> 18) & 0x3f);
  65. }
  66. static ssize_t store_pc1event(struct sys_device *dev, const char *buf,
  67. size_t count)
  68. {
  69. unsigned long val;
  70. char *endp;
  71. val = simple_strtoul(buf, &endp, 0);
  72. if (endp == buf || val > 0x3f)
  73. return -EINVAL;
  74. val = (val << 18) | (sysreg_read(PCCR) & 0xff03ffff);
  75. sysreg_write(PCCR, val);
  76. return count;
  77. }
  78. static ssize_t show_pc1count(struct sys_device *dev, char *buf)
  79. {
  80. unsigned long pcnt1;
  81. pcnt1 = sysreg_read(PCNT1);
  82. return sprintf(buf, "%lu\n", pcnt1);
  83. }
  84. static ssize_t store_pc1count(struct sys_device *dev, const char *buf,
  85. size_t count)
  86. {
  87. unsigned long val;
  88. char *endp;
  89. val = simple_strtoul(buf, &endp, 0);
  90. if (endp == buf)
  91. return -EINVAL;
  92. sysreg_write(PCNT1, val);
  93. return count;
  94. }
  95. static ssize_t show_pccycles(struct sys_device *dev, char *buf)
  96. {
  97. unsigned long pccnt;
  98. pccnt = sysreg_read(PCCNT);
  99. return sprintf(buf, "%lu\n", pccnt);
  100. }
  101. static ssize_t store_pccycles(struct sys_device *dev, const char *buf,
  102. size_t count)
  103. {
  104. unsigned long val;
  105. char *endp;
  106. val = simple_strtoul(buf, &endp, 0);
  107. if (endp == buf)
  108. return -EINVAL;
  109. sysreg_write(PCCNT, val);
  110. return count;
  111. }
  112. static ssize_t show_pcenable(struct sys_device *dev, char *buf)
  113. {
  114. unsigned long pccr;
  115. pccr = sysreg_read(PCCR);
  116. return sprintf(buf, "%c\n", (pccr & 1)?'1':'0');
  117. }
  118. static ssize_t store_pcenable(struct sys_device *dev, const char *buf,
  119. size_t count)
  120. {
  121. unsigned long pccr, val;
  122. char *endp;
  123. val = simple_strtoul(buf, &endp, 0);
  124. if (endp == buf)
  125. return -EINVAL;
  126. if (val)
  127. val = 1;
  128. pccr = sysreg_read(PCCR);
  129. pccr = (pccr & ~1UL) | val;
  130. sysreg_write(PCCR, pccr);
  131. return count;
  132. }
  133. static SYSDEV_ATTR(pc0event, 0600, show_pc0event, store_pc0event);
  134. static SYSDEV_ATTR(pc0count, 0600, show_pc0count, store_pc0count);
  135. static SYSDEV_ATTR(pc1event, 0600, show_pc1event, store_pc1event);
  136. static SYSDEV_ATTR(pc1count, 0600, show_pc1count, store_pc1count);
  137. static SYSDEV_ATTR(pccycles, 0600, show_pccycles, store_pccycles);
  138. static SYSDEV_ATTR(pcenable, 0600, show_pcenable, store_pcenable);
  139. #endif /* CONFIG_PERFORMANCE_COUNTERS */
  140. static int __init topology_init(void)
  141. {
  142. int cpu;
  143. for_each_possible_cpu(cpu) {
  144. struct cpu *c = &per_cpu(cpu_devices, cpu);
  145. register_cpu(c, cpu);
  146. #ifdef CONFIG_PERFORMANCE_COUNTERS
  147. sysdev_create_file(&c->sysdev, &attr_pc0event);
  148. sysdev_create_file(&c->sysdev, &attr_pc0count);
  149. sysdev_create_file(&c->sysdev, &attr_pc1event);
  150. sysdev_create_file(&c->sysdev, &attr_pc1count);
  151. sysdev_create_file(&c->sysdev, &attr_pccycles);
  152. sysdev_create_file(&c->sysdev, &attr_pcenable);
  153. #endif
  154. }
  155. return 0;
  156. }
  157. subsys_initcall(topology_init);
  158. struct chip_id_map {
  159. u16 mid;
  160. u16 pn;
  161. const char *name;
  162. };
  163. static const struct chip_id_map chip_names[] = {
  164. { .mid = 0x1f, .pn = 0x1e82, .name = "AT32AP700x" },
  165. };
  166. #define NR_CHIP_NAMES ARRAY_SIZE(chip_names)
  167. static const char *cpu_names[] = {
  168. "Morgan",
  169. "AP7",
  170. };
  171. #define NR_CPU_NAMES ARRAY_SIZE(cpu_names)
  172. static const char *arch_names[] = {
  173. "AVR32A",
  174. "AVR32B",
  175. };
  176. #define NR_ARCH_NAMES ARRAY_SIZE(arch_names)
  177. static const char *mmu_types[] = {
  178. "No MMU",
  179. "ITLB and DTLB",
  180. "Shared TLB",
  181. "MPU"
  182. };
  183. static const char *cpu_feature_flags[] = {
  184. "rmw", "dsp", "simd", "ocd", "perfctr", "java", "fpu",
  185. };
  186. static const char *get_chip_name(struct avr32_cpuinfo *cpu)
  187. {
  188. unsigned int i;
  189. unsigned int mid = avr32_get_manufacturer_id(cpu);
  190. unsigned int pn = avr32_get_product_number(cpu);
  191. for (i = 0; i < NR_CHIP_NAMES; i++) {
  192. if (chip_names[i].mid == mid && chip_names[i].pn == pn)
  193. return chip_names[i].name;
  194. }
  195. return "(unknown)";
  196. }
  197. void __init setup_processor(void)
  198. {
  199. unsigned long config0, config1;
  200. unsigned long features;
  201. unsigned cpu_id, cpu_rev, arch_id, arch_rev, mmu_type;
  202. unsigned device_id;
  203. unsigned tmp;
  204. unsigned i;
  205. config0 = sysreg_read(CONFIG0);
  206. config1 = sysreg_read(CONFIG1);
  207. cpu_id = SYSREG_BFEXT(PROCESSORID, config0);
  208. cpu_rev = SYSREG_BFEXT(PROCESSORREVISION, config0);
  209. arch_id = SYSREG_BFEXT(AT, config0);
  210. arch_rev = SYSREG_BFEXT(AR, config0);
  211. mmu_type = SYSREG_BFEXT(MMUT, config0);
  212. device_id = ocd_read(DID);
  213. boot_cpu_data.arch_type = arch_id;
  214. boot_cpu_data.cpu_type = cpu_id;
  215. boot_cpu_data.arch_revision = arch_rev;
  216. boot_cpu_data.cpu_revision = cpu_rev;
  217. boot_cpu_data.tlb_config = mmu_type;
  218. boot_cpu_data.device_id = device_id;
  219. tmp = SYSREG_BFEXT(ILSZ, config1);
  220. if (tmp) {
  221. boot_cpu_data.icache.ways = 1 << SYSREG_BFEXT(IASS, config1);
  222. boot_cpu_data.icache.sets = 1 << SYSREG_BFEXT(ISET, config1);
  223. boot_cpu_data.icache.linesz = 1 << (tmp + 1);
  224. }
  225. tmp = SYSREG_BFEXT(DLSZ, config1);
  226. if (tmp) {
  227. boot_cpu_data.dcache.ways = 1 << SYSREG_BFEXT(DASS, config1);
  228. boot_cpu_data.dcache.sets = 1 << SYSREG_BFEXT(DSET, config1);
  229. boot_cpu_data.dcache.linesz = 1 << (tmp + 1);
  230. }
  231. if ((cpu_id >= NR_CPU_NAMES) || (arch_id >= NR_ARCH_NAMES)) {
  232. printk ("Unknown CPU configuration (ID %02x, arch %02x), "
  233. "continuing anyway...\n",
  234. cpu_id, arch_id);
  235. return;
  236. }
  237. printk ("CPU: %s chip revision %c\n", get_chip_name(&boot_cpu_data),
  238. avr32_get_chip_revision(&boot_cpu_data) + 'A');
  239. printk ("CPU: %s [%02x] core revision %d (%s arch revision %d)\n",
  240. cpu_names[cpu_id], cpu_id, cpu_rev,
  241. arch_names[arch_id], arch_rev);
  242. printk ("CPU: MMU configuration: %s\n", mmu_types[mmu_type]);
  243. printk ("CPU: features:");
  244. features = 0;
  245. if (config0 & SYSREG_BIT(CONFIG0_R))
  246. features |= AVR32_FEATURE_RMW;
  247. if (config0 & SYSREG_BIT(CONFIG0_D))
  248. features |= AVR32_FEATURE_DSP;
  249. if (config0 & SYSREG_BIT(CONFIG0_S))
  250. features |= AVR32_FEATURE_SIMD;
  251. if (config0 & SYSREG_BIT(CONFIG0_O))
  252. features |= AVR32_FEATURE_OCD;
  253. if (config0 & SYSREG_BIT(CONFIG0_P))
  254. features |= AVR32_FEATURE_PCTR;
  255. if (config0 & SYSREG_BIT(CONFIG0_J))
  256. features |= AVR32_FEATURE_JAVA;
  257. if (config0 & SYSREG_BIT(CONFIG0_F))
  258. features |= AVR32_FEATURE_FPU;
  259. for (i = 0; i < ARRAY_SIZE(cpu_feature_flags); i++)
  260. if (features & (1 << i))
  261. printk(" %s", cpu_feature_flags[i]);
  262. printk("\n");
  263. boot_cpu_data.features = features;
  264. }
  265. #ifdef CONFIG_PROC_FS
  266. static int c_show(struct seq_file *m, void *v)
  267. {
  268. unsigned int icache_size, dcache_size;
  269. unsigned int cpu = smp_processor_id();
  270. unsigned int freq;
  271. unsigned int i;
  272. icache_size = boot_cpu_data.icache.ways *
  273. boot_cpu_data.icache.sets *
  274. boot_cpu_data.icache.linesz;
  275. dcache_size = boot_cpu_data.dcache.ways *
  276. boot_cpu_data.dcache.sets *
  277. boot_cpu_data.dcache.linesz;
  278. seq_printf(m, "processor\t: %d\n", cpu);
  279. seq_printf(m, "chip type\t: %s revision %c\n",
  280. get_chip_name(&boot_cpu_data),
  281. avr32_get_chip_revision(&boot_cpu_data) + 'A');
  282. if (boot_cpu_data.arch_type < NR_ARCH_NAMES)
  283. seq_printf(m, "cpu arch\t: %s revision %d\n",
  284. arch_names[boot_cpu_data.arch_type],
  285. boot_cpu_data.arch_revision);
  286. if (boot_cpu_data.cpu_type < NR_CPU_NAMES)
  287. seq_printf(m, "cpu core\t: %s revision %d\n",
  288. cpu_names[boot_cpu_data.cpu_type],
  289. boot_cpu_data.cpu_revision);
  290. freq = (clk_get_rate(boot_cpu_data.clk) + 500) / 1000;
  291. seq_printf(m, "cpu MHz\t\t: %u.%03u\n", freq / 1000, freq % 1000);
  292. seq_printf(m, "i-cache\t\t: %dK (%u ways x %u sets x %u)\n",
  293. icache_size >> 10,
  294. boot_cpu_data.icache.ways,
  295. boot_cpu_data.icache.sets,
  296. boot_cpu_data.icache.linesz);
  297. seq_printf(m, "d-cache\t\t: %dK (%u ways x %u sets x %u)\n",
  298. dcache_size >> 10,
  299. boot_cpu_data.dcache.ways,
  300. boot_cpu_data.dcache.sets,
  301. boot_cpu_data.dcache.linesz);
  302. seq_printf(m, "features\t:");
  303. for (i = 0; i < ARRAY_SIZE(cpu_feature_flags); i++)
  304. if (boot_cpu_data.features & (1 << i))
  305. seq_printf(m, " %s", cpu_feature_flags[i]);
  306. seq_printf(m, "\nbogomips\t: %lu.%02lu\n",
  307. boot_cpu_data.loops_per_jiffy / (500000/HZ),
  308. (boot_cpu_data.loops_per_jiffy / (5000/HZ)) % 100);
  309. return 0;
  310. }
  311. static void *c_start(struct seq_file *m, loff_t *pos)
  312. {
  313. return *pos < 1 ? (void *)1 : NULL;
  314. }
  315. static void *c_next(struct seq_file *m, void *v, loff_t *pos)
  316. {
  317. ++*pos;
  318. return NULL;
  319. }
  320. static void c_stop(struct seq_file *m, void *v)
  321. {
  322. }
  323. const struct seq_operations cpuinfo_op = {
  324. .start = c_start,
  325. .next = c_next,
  326. .stop = c_stop,
  327. .show = c_show
  328. };
  329. #endif /* CONFIG_PROC_FS */