sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. #include <linux/device.h>
  2. #include <linux/cpu.h>
  3. #include <linux/smp.h>
  4. #include <linux/percpu.h>
  5. #include <linux/init.h>
  6. #include <linux/sched.h>
  7. #include <linux/export.h>
  8. #include <linux/nodemask.h>
  9. #include <linux/cpumask.h>
  10. #include <linux/notifier.h>
  11. #include <asm/current.h>
  12. #include <asm/processor.h>
  13. #include <asm/cputable.h>
  14. #include <asm/hvcall.h>
  15. #include <asm/prom.h>
  16. #include <asm/machdep.h>
  17. #include <asm/smp.h>
  18. #include <asm/pmc.h>
  19. #include <asm/firmware.h>
  20. #include "cacheinfo.h"
  21. #ifdef CONFIG_PPC64
  22. #include <asm/paca.h>
  23. #include <asm/lppaca.h>
  24. #endif
  25. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  26. /*
  27. * SMT snooze delay stuff, 64-bit only for now
  28. */
  29. #ifdef CONFIG_PPC64
  30. /* Time in microseconds we delay before sleeping in the idle loop */
  31. DEFINE_PER_CPU(long, smt_snooze_delay) = { 100 };
  32. static ssize_t store_smt_snooze_delay(struct device *dev,
  33. struct device_attribute *attr,
  34. const char *buf,
  35. size_t count)
  36. {
  37. struct cpu *cpu = container_of(dev, struct cpu, dev);
  38. ssize_t ret;
  39. long snooze;
  40. ret = sscanf(buf, "%ld", &snooze);
  41. if (ret != 1)
  42. return -EINVAL;
  43. per_cpu(smt_snooze_delay, cpu->dev.id) = snooze;
  44. return count;
  45. }
  46. static ssize_t show_smt_snooze_delay(struct device *dev,
  47. struct device_attribute *attr,
  48. char *buf)
  49. {
  50. struct cpu *cpu = container_of(dev, struct cpu, dev);
  51. return sprintf(buf, "%ld\n", per_cpu(smt_snooze_delay, cpu->dev.id));
  52. }
  53. static DEVICE_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
  54. store_smt_snooze_delay);
  55. static int __init setup_smt_snooze_delay(char *str)
  56. {
  57. unsigned int cpu;
  58. long snooze;
  59. if (!cpu_has_feature(CPU_FTR_SMT))
  60. return 1;
  61. snooze = simple_strtol(str, NULL, 10);
  62. for_each_possible_cpu(cpu)
  63. per_cpu(smt_snooze_delay, cpu) = snooze;
  64. return 1;
  65. }
  66. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  67. #endif /* CONFIG_PPC64 */
  68. #ifdef CONFIG_PPC_FSL_BOOK3E
  69. #define MAX_BIT 63
  70. static u64 pw20_wt;
  71. static u64 altivec_idle_wt;
  72. static unsigned int get_idle_ticks_bit(u64 ns)
  73. {
  74. u64 cycle;
  75. if (ns >= 10000)
  76. cycle = div_u64(ns + 500, 1000) * tb_ticks_per_usec;
  77. else
  78. cycle = div_u64(ns * tb_ticks_per_usec, 1000);
  79. if (!cycle)
  80. return 0;
  81. return ilog2(cycle);
  82. }
  83. static void do_show_pwrmgtcr0(void *val)
  84. {
  85. u32 *value = val;
  86. *value = mfspr(SPRN_PWRMGTCR0);
  87. }
  88. static ssize_t show_pw20_state(struct device *dev,
  89. struct device_attribute *attr, char *buf)
  90. {
  91. u32 value;
  92. unsigned int cpu = dev->id;
  93. smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
  94. value &= PWRMGTCR0_PW20_WAIT;
  95. return sprintf(buf, "%u\n", value ? 1 : 0);
  96. }
  97. static void do_store_pw20_state(void *val)
  98. {
  99. u32 *value = val;
  100. u32 pw20_state;
  101. pw20_state = mfspr(SPRN_PWRMGTCR0);
  102. if (*value)
  103. pw20_state |= PWRMGTCR0_PW20_WAIT;
  104. else
  105. pw20_state &= ~PWRMGTCR0_PW20_WAIT;
  106. mtspr(SPRN_PWRMGTCR0, pw20_state);
  107. }
  108. static ssize_t store_pw20_state(struct device *dev,
  109. struct device_attribute *attr,
  110. const char *buf, size_t count)
  111. {
  112. u32 value;
  113. unsigned int cpu = dev->id;
  114. if (kstrtou32(buf, 0, &value))
  115. return -EINVAL;
  116. if (value > 1)
  117. return -EINVAL;
  118. smp_call_function_single(cpu, do_store_pw20_state, &value, 1);
  119. return count;
  120. }
  121. static ssize_t show_pw20_wait_time(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. u32 value;
  125. u64 tb_cycle = 1;
  126. u64 time;
  127. unsigned int cpu = dev->id;
  128. if (!pw20_wt) {
  129. smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
  130. value = (value & PWRMGTCR0_PW20_ENT) >>
  131. PWRMGTCR0_PW20_ENT_SHIFT;
  132. tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
  133. /* convert ms to ns */
  134. if (tb_ticks_per_usec > 1000) {
  135. time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
  136. } else {
  137. u32 rem_us;
  138. time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
  139. &rem_us);
  140. time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
  141. }
  142. } else {
  143. time = pw20_wt;
  144. }
  145. return sprintf(buf, "%llu\n", time > 0 ? time : 0);
  146. }
  147. static void set_pw20_wait_entry_bit(void *val)
  148. {
  149. u32 *value = val;
  150. u32 pw20_idle;
  151. pw20_idle = mfspr(SPRN_PWRMGTCR0);
  152. /* Set Automatic PW20 Core Idle Count */
  153. /* clear count */
  154. pw20_idle &= ~PWRMGTCR0_PW20_ENT;
  155. /* set count */
  156. pw20_idle |= ((MAX_BIT - *value) << PWRMGTCR0_PW20_ENT_SHIFT);
  157. mtspr(SPRN_PWRMGTCR0, pw20_idle);
  158. }
  159. static ssize_t store_pw20_wait_time(struct device *dev,
  160. struct device_attribute *attr,
  161. const char *buf, size_t count)
  162. {
  163. u32 entry_bit;
  164. u64 value;
  165. unsigned int cpu = dev->id;
  166. if (kstrtou64(buf, 0, &value))
  167. return -EINVAL;
  168. if (!value)
  169. return -EINVAL;
  170. entry_bit = get_idle_ticks_bit(value);
  171. if (entry_bit > MAX_BIT)
  172. return -EINVAL;
  173. pw20_wt = value;
  174. smp_call_function_single(cpu, set_pw20_wait_entry_bit,
  175. &entry_bit, 1);
  176. return count;
  177. }
  178. static ssize_t show_altivec_idle(struct device *dev,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. u32 value;
  182. unsigned int cpu = dev->id;
  183. smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
  184. value &= PWRMGTCR0_AV_IDLE_PD_EN;
  185. return sprintf(buf, "%u\n", value ? 1 : 0);
  186. }
  187. static void do_store_altivec_idle(void *val)
  188. {
  189. u32 *value = val;
  190. u32 altivec_idle;
  191. altivec_idle = mfspr(SPRN_PWRMGTCR0);
  192. if (*value)
  193. altivec_idle |= PWRMGTCR0_AV_IDLE_PD_EN;
  194. else
  195. altivec_idle &= ~PWRMGTCR0_AV_IDLE_PD_EN;
  196. mtspr(SPRN_PWRMGTCR0, altivec_idle);
  197. }
  198. static ssize_t store_altivec_idle(struct device *dev,
  199. struct device_attribute *attr,
  200. const char *buf, size_t count)
  201. {
  202. u32 value;
  203. unsigned int cpu = dev->id;
  204. if (kstrtou32(buf, 0, &value))
  205. return -EINVAL;
  206. if (value > 1)
  207. return -EINVAL;
  208. smp_call_function_single(cpu, do_store_altivec_idle, &value, 1);
  209. return count;
  210. }
  211. static ssize_t show_altivec_idle_wait_time(struct device *dev,
  212. struct device_attribute *attr, char *buf)
  213. {
  214. u32 value;
  215. u64 tb_cycle = 1;
  216. u64 time;
  217. unsigned int cpu = dev->id;
  218. if (!altivec_idle_wt) {
  219. smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
  220. value = (value & PWRMGTCR0_AV_IDLE_CNT) >>
  221. PWRMGTCR0_AV_IDLE_CNT_SHIFT;
  222. tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
  223. /* convert ms to ns */
  224. if (tb_ticks_per_usec > 1000) {
  225. time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
  226. } else {
  227. u32 rem_us;
  228. time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
  229. &rem_us);
  230. time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
  231. }
  232. } else {
  233. time = altivec_idle_wt;
  234. }
  235. return sprintf(buf, "%llu\n", time > 0 ? time : 0);
  236. }
  237. static void set_altivec_idle_wait_entry_bit(void *val)
  238. {
  239. u32 *value = val;
  240. u32 altivec_idle;
  241. altivec_idle = mfspr(SPRN_PWRMGTCR0);
  242. /* Set Automatic AltiVec Idle Count */
  243. /* clear count */
  244. altivec_idle &= ~PWRMGTCR0_AV_IDLE_CNT;
  245. /* set count */
  246. altivec_idle |= ((MAX_BIT - *value) << PWRMGTCR0_AV_IDLE_CNT_SHIFT);
  247. mtspr(SPRN_PWRMGTCR0, altivec_idle);
  248. }
  249. static ssize_t store_altivec_idle_wait_time(struct device *dev,
  250. struct device_attribute *attr,
  251. const char *buf, size_t count)
  252. {
  253. u32 entry_bit;
  254. u64 value;
  255. unsigned int cpu = dev->id;
  256. if (kstrtou64(buf, 0, &value))
  257. return -EINVAL;
  258. if (!value)
  259. return -EINVAL;
  260. entry_bit = get_idle_ticks_bit(value);
  261. if (entry_bit > MAX_BIT)
  262. return -EINVAL;
  263. altivec_idle_wt = value;
  264. smp_call_function_single(cpu, set_altivec_idle_wait_entry_bit,
  265. &entry_bit, 1);
  266. return count;
  267. }
  268. /*
  269. * Enable/Disable interface:
  270. * 0, disable. 1, enable.
  271. */
  272. static DEVICE_ATTR(pw20_state, 0600, show_pw20_state, store_pw20_state);
  273. static DEVICE_ATTR(altivec_idle, 0600, show_altivec_idle, store_altivec_idle);
  274. /*
  275. * Set wait time interface:(Nanosecond)
  276. * Example: Base on TBfreq is 41MHZ.
  277. * 1~48(ns): TB[63]
  278. * 49~97(ns): TB[62]
  279. * 98~195(ns): TB[61]
  280. * 196~390(ns): TB[60]
  281. * 391~780(ns): TB[59]
  282. * 781~1560(ns): TB[58]
  283. * ...
  284. */
  285. static DEVICE_ATTR(pw20_wait_time, 0600,
  286. show_pw20_wait_time,
  287. store_pw20_wait_time);
  288. static DEVICE_ATTR(altivec_idle_wait_time, 0600,
  289. show_altivec_idle_wait_time,
  290. store_altivec_idle_wait_time);
  291. #endif
  292. /*
  293. * Enabling PMCs will slow partition context switch times so we only do
  294. * it the first time we write to the PMCs.
  295. */
  296. static DEFINE_PER_CPU(char, pmcs_enabled);
  297. void ppc_enable_pmcs(void)
  298. {
  299. ppc_set_pmu_inuse(1);
  300. /* Only need to enable them once */
  301. if (__get_cpu_var(pmcs_enabled))
  302. return;
  303. __get_cpu_var(pmcs_enabled) = 1;
  304. if (ppc_md.enable_pmcs)
  305. ppc_md.enable_pmcs();
  306. }
  307. EXPORT_SYMBOL(ppc_enable_pmcs);
  308. #define __SYSFS_SPRSETUP(NAME, ADDRESS, EXTRA) \
  309. static void read_##NAME(void *val) \
  310. { \
  311. *(unsigned long *)val = mfspr(ADDRESS); \
  312. } \
  313. static void write_##NAME(void *val) \
  314. { \
  315. EXTRA; \
  316. mtspr(ADDRESS, *(unsigned long *)val); \
  317. } \
  318. static ssize_t show_##NAME(struct device *dev, \
  319. struct device_attribute *attr, \
  320. char *buf) \
  321. { \
  322. struct cpu *cpu = container_of(dev, struct cpu, dev); \
  323. unsigned long val; \
  324. smp_call_function_single(cpu->dev.id, read_##NAME, &val, 1); \
  325. return sprintf(buf, "%lx\n", val); \
  326. } \
  327. static ssize_t __used \
  328. store_##NAME(struct device *dev, struct device_attribute *attr, \
  329. const char *buf, size_t count) \
  330. { \
  331. struct cpu *cpu = container_of(dev, struct cpu, dev); \
  332. unsigned long val; \
  333. int ret = sscanf(buf, "%lx", &val); \
  334. if (ret != 1) \
  335. return -EINVAL; \
  336. smp_call_function_single(cpu->dev.id, write_##NAME, &val, 1); \
  337. return count; \
  338. }
  339. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  340. __SYSFS_SPRSETUP(NAME, ADDRESS, ppc_enable_pmcs())
  341. #define SYSFS_SPRSETUP(NAME, ADDRESS) \
  342. __SYSFS_SPRSETUP(NAME, ADDRESS, )
  343. /* Let's define all possible registers, we'll only hook up the ones
  344. * that are implemented on the current processor
  345. */
  346. #if defined(CONFIG_PPC64)
  347. #define HAS_PPC_PMC_CLASSIC 1
  348. #define HAS_PPC_PMC_IBM 1
  349. #define HAS_PPC_PMC_PA6T 1
  350. #elif defined(CONFIG_6xx)
  351. #define HAS_PPC_PMC_CLASSIC 1
  352. #define HAS_PPC_PMC_IBM 1
  353. #define HAS_PPC_PMC_G4 1
  354. #endif
  355. #ifdef HAS_PPC_PMC_CLASSIC
  356. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  357. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  358. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  359. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  360. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  361. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  362. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  363. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  364. #ifdef HAS_PPC_PMC_G4
  365. SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
  366. #endif
  367. #ifdef CONFIG_PPC64
  368. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  369. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  370. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  371. SYSFS_SPRSETUP(purr, SPRN_PURR);
  372. SYSFS_SPRSETUP(spurr, SPRN_SPURR);
  373. SYSFS_SPRSETUP(dscr, SPRN_DSCR);
  374. SYSFS_SPRSETUP(pir, SPRN_PIR);
  375. /*
  376. Lets only enable read for phyp resources and
  377. enable write when needed with a separate function.
  378. Lets be conservative and default to pseries.
  379. */
  380. static DEVICE_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  381. static DEVICE_ATTR(spurr, 0400, show_spurr, NULL);
  382. static DEVICE_ATTR(dscr, 0600, show_dscr, store_dscr);
  383. static DEVICE_ATTR(purr, 0400, show_purr, store_purr);
  384. static DEVICE_ATTR(pir, 0400, show_pir, NULL);
  385. unsigned long dscr_default = 0;
  386. EXPORT_SYMBOL(dscr_default);
  387. static void add_write_permission_dev_attr(struct device_attribute *attr)
  388. {
  389. attr->attr.mode |= 0200;
  390. }
  391. static ssize_t show_dscr_default(struct device *dev,
  392. struct device_attribute *attr, char *buf)
  393. {
  394. return sprintf(buf, "%lx\n", dscr_default);
  395. }
  396. static void update_dscr(void *dummy)
  397. {
  398. if (!current->thread.dscr_inherit) {
  399. current->thread.dscr = dscr_default;
  400. mtspr(SPRN_DSCR, dscr_default);
  401. }
  402. }
  403. static ssize_t __used store_dscr_default(struct device *dev,
  404. struct device_attribute *attr, const char *buf,
  405. size_t count)
  406. {
  407. unsigned long val;
  408. int ret = 0;
  409. ret = sscanf(buf, "%lx", &val);
  410. if (ret != 1)
  411. return -EINVAL;
  412. dscr_default = val;
  413. on_each_cpu(update_dscr, NULL, 1);
  414. return count;
  415. }
  416. static DEVICE_ATTR(dscr_default, 0600,
  417. show_dscr_default, store_dscr_default);
  418. static void sysfs_create_dscr_default(void)
  419. {
  420. int err = 0;
  421. if (cpu_has_feature(CPU_FTR_DSCR))
  422. err = device_create_file(cpu_subsys.dev_root, &dev_attr_dscr_default);
  423. }
  424. #endif /* CONFIG_PPC64 */
  425. #ifdef HAS_PPC_PMC_PA6T
  426. SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
  427. SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
  428. SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
  429. SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
  430. SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
  431. SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
  432. #ifdef CONFIG_DEBUG_KERNEL
  433. SYSFS_SPRSETUP(hid0, SPRN_HID0);
  434. SYSFS_SPRSETUP(hid1, SPRN_HID1);
  435. SYSFS_SPRSETUP(hid4, SPRN_HID4);
  436. SYSFS_SPRSETUP(hid5, SPRN_HID5);
  437. SYSFS_SPRSETUP(ima0, SPRN_PA6T_IMA0);
  438. SYSFS_SPRSETUP(ima1, SPRN_PA6T_IMA1);
  439. SYSFS_SPRSETUP(ima2, SPRN_PA6T_IMA2);
  440. SYSFS_SPRSETUP(ima3, SPRN_PA6T_IMA3);
  441. SYSFS_SPRSETUP(ima4, SPRN_PA6T_IMA4);
  442. SYSFS_SPRSETUP(ima5, SPRN_PA6T_IMA5);
  443. SYSFS_SPRSETUP(ima6, SPRN_PA6T_IMA6);
  444. SYSFS_SPRSETUP(ima7, SPRN_PA6T_IMA7);
  445. SYSFS_SPRSETUP(ima8, SPRN_PA6T_IMA8);
  446. SYSFS_SPRSETUP(ima9, SPRN_PA6T_IMA9);
  447. SYSFS_SPRSETUP(imaat, SPRN_PA6T_IMAAT);
  448. SYSFS_SPRSETUP(btcr, SPRN_PA6T_BTCR);
  449. SYSFS_SPRSETUP(pccr, SPRN_PA6T_PCCR);
  450. SYSFS_SPRSETUP(rpccr, SPRN_PA6T_RPCCR);
  451. SYSFS_SPRSETUP(der, SPRN_PA6T_DER);
  452. SYSFS_SPRSETUP(mer, SPRN_PA6T_MER);
  453. SYSFS_SPRSETUP(ber, SPRN_PA6T_BER);
  454. SYSFS_SPRSETUP(ier, SPRN_PA6T_IER);
  455. SYSFS_SPRSETUP(sier, SPRN_PA6T_SIER);
  456. SYSFS_SPRSETUP(siar, SPRN_PA6T_SIAR);
  457. SYSFS_SPRSETUP(tsr0, SPRN_PA6T_TSR0);
  458. SYSFS_SPRSETUP(tsr1, SPRN_PA6T_TSR1);
  459. SYSFS_SPRSETUP(tsr2, SPRN_PA6T_TSR2);
  460. SYSFS_SPRSETUP(tsr3, SPRN_PA6T_TSR3);
  461. #endif /* CONFIG_DEBUG_KERNEL */
  462. #endif /* HAS_PPC_PMC_PA6T */
  463. #ifdef HAS_PPC_PMC_IBM
  464. static struct device_attribute ibm_common_attrs[] = {
  465. __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  466. __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  467. };
  468. #endif /* HAS_PPC_PMC_G4 */
  469. #ifdef HAS_PPC_PMC_G4
  470. static struct device_attribute g4_common_attrs[] = {
  471. __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  472. __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  473. __ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
  474. };
  475. #endif /* HAS_PPC_PMC_G4 */
  476. static struct device_attribute classic_pmc_attrs[] = {
  477. __ATTR(pmc1, 0600, show_pmc1, store_pmc1),
  478. __ATTR(pmc2, 0600, show_pmc2, store_pmc2),
  479. __ATTR(pmc3, 0600, show_pmc3, store_pmc3),
  480. __ATTR(pmc4, 0600, show_pmc4, store_pmc4),
  481. __ATTR(pmc5, 0600, show_pmc5, store_pmc5),
  482. __ATTR(pmc6, 0600, show_pmc6, store_pmc6),
  483. #ifdef CONFIG_PPC64
  484. __ATTR(pmc7, 0600, show_pmc7, store_pmc7),
  485. __ATTR(pmc8, 0600, show_pmc8, store_pmc8),
  486. #endif
  487. };
  488. #ifdef HAS_PPC_PMC_PA6T
  489. static struct device_attribute pa6t_attrs[] = {
  490. __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  491. __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  492. __ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
  493. __ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
  494. __ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
  495. __ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
  496. __ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
  497. __ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
  498. #ifdef CONFIG_DEBUG_KERNEL
  499. __ATTR(hid0, 0600, show_hid0, store_hid0),
  500. __ATTR(hid1, 0600, show_hid1, store_hid1),
  501. __ATTR(hid4, 0600, show_hid4, store_hid4),
  502. __ATTR(hid5, 0600, show_hid5, store_hid5),
  503. __ATTR(ima0, 0600, show_ima0, store_ima0),
  504. __ATTR(ima1, 0600, show_ima1, store_ima1),
  505. __ATTR(ima2, 0600, show_ima2, store_ima2),
  506. __ATTR(ima3, 0600, show_ima3, store_ima3),
  507. __ATTR(ima4, 0600, show_ima4, store_ima4),
  508. __ATTR(ima5, 0600, show_ima5, store_ima5),
  509. __ATTR(ima6, 0600, show_ima6, store_ima6),
  510. __ATTR(ima7, 0600, show_ima7, store_ima7),
  511. __ATTR(ima8, 0600, show_ima8, store_ima8),
  512. __ATTR(ima9, 0600, show_ima9, store_ima9),
  513. __ATTR(imaat, 0600, show_imaat, store_imaat),
  514. __ATTR(btcr, 0600, show_btcr, store_btcr),
  515. __ATTR(pccr, 0600, show_pccr, store_pccr),
  516. __ATTR(rpccr, 0600, show_rpccr, store_rpccr),
  517. __ATTR(der, 0600, show_der, store_der),
  518. __ATTR(mer, 0600, show_mer, store_mer),
  519. __ATTR(ber, 0600, show_ber, store_ber),
  520. __ATTR(ier, 0600, show_ier, store_ier),
  521. __ATTR(sier, 0600, show_sier, store_sier),
  522. __ATTR(siar, 0600, show_siar, store_siar),
  523. __ATTR(tsr0, 0600, show_tsr0, store_tsr0),
  524. __ATTR(tsr1, 0600, show_tsr1, store_tsr1),
  525. __ATTR(tsr2, 0600, show_tsr2, store_tsr2),
  526. __ATTR(tsr3, 0600, show_tsr3, store_tsr3),
  527. #endif /* CONFIG_DEBUG_KERNEL */
  528. };
  529. #endif /* HAS_PPC_PMC_PA6T */
  530. #endif /* HAS_PPC_PMC_CLASSIC */
  531. static void register_cpu_online(unsigned int cpu)
  532. {
  533. struct cpu *c = &per_cpu(cpu_devices, cpu);
  534. struct device *s = &c->dev;
  535. struct device_attribute *attrs, *pmc_attrs;
  536. int i, nattrs;
  537. #ifdef CONFIG_PPC64
  538. if (cpu_has_feature(CPU_FTR_SMT))
  539. device_create_file(s, &dev_attr_smt_snooze_delay);
  540. #endif
  541. /* PMC stuff */
  542. switch (cur_cpu_spec->pmc_type) {
  543. #ifdef HAS_PPC_PMC_IBM
  544. case PPC_PMC_IBM:
  545. attrs = ibm_common_attrs;
  546. nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
  547. pmc_attrs = classic_pmc_attrs;
  548. break;
  549. #endif /* HAS_PPC_PMC_IBM */
  550. #ifdef HAS_PPC_PMC_G4
  551. case PPC_PMC_G4:
  552. attrs = g4_common_attrs;
  553. nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
  554. pmc_attrs = classic_pmc_attrs;
  555. break;
  556. #endif /* HAS_PPC_PMC_G4 */
  557. #ifdef HAS_PPC_PMC_PA6T
  558. case PPC_PMC_PA6T:
  559. /* PA Semi starts counting at PMC0 */
  560. attrs = pa6t_attrs;
  561. nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
  562. pmc_attrs = NULL;
  563. break;
  564. #endif /* HAS_PPC_PMC_PA6T */
  565. default:
  566. attrs = NULL;
  567. nattrs = 0;
  568. pmc_attrs = NULL;
  569. }
  570. for (i = 0; i < nattrs; i++)
  571. device_create_file(s, &attrs[i]);
  572. if (pmc_attrs)
  573. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  574. device_create_file(s, &pmc_attrs[i]);
  575. #ifdef CONFIG_PPC64
  576. if (cpu_has_feature(CPU_FTR_MMCRA))
  577. device_create_file(s, &dev_attr_mmcra);
  578. if (cpu_has_feature(CPU_FTR_PURR)) {
  579. if (!firmware_has_feature(FW_FEATURE_LPAR))
  580. add_write_permission_dev_attr(&dev_attr_purr);
  581. device_create_file(s, &dev_attr_purr);
  582. }
  583. if (cpu_has_feature(CPU_FTR_SPURR))
  584. device_create_file(s, &dev_attr_spurr);
  585. if (cpu_has_feature(CPU_FTR_DSCR))
  586. device_create_file(s, &dev_attr_dscr);
  587. if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
  588. device_create_file(s, &dev_attr_pir);
  589. #endif /* CONFIG_PPC64 */
  590. #ifdef CONFIG_PPC_FSL_BOOK3E
  591. if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
  592. device_create_file(s, &dev_attr_pw20_state);
  593. device_create_file(s, &dev_attr_pw20_wait_time);
  594. device_create_file(s, &dev_attr_altivec_idle);
  595. device_create_file(s, &dev_attr_altivec_idle_wait_time);
  596. }
  597. #endif
  598. cacheinfo_cpu_online(cpu);
  599. }
  600. #ifdef CONFIG_HOTPLUG_CPU
  601. static void unregister_cpu_online(unsigned int cpu)
  602. {
  603. struct cpu *c = &per_cpu(cpu_devices, cpu);
  604. struct device *s = &c->dev;
  605. struct device_attribute *attrs, *pmc_attrs;
  606. int i, nattrs;
  607. BUG_ON(!c->hotpluggable);
  608. #ifdef CONFIG_PPC64
  609. if (cpu_has_feature(CPU_FTR_SMT))
  610. device_remove_file(s, &dev_attr_smt_snooze_delay);
  611. #endif
  612. /* PMC stuff */
  613. switch (cur_cpu_spec->pmc_type) {
  614. #ifdef HAS_PPC_PMC_IBM
  615. case PPC_PMC_IBM:
  616. attrs = ibm_common_attrs;
  617. nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
  618. pmc_attrs = classic_pmc_attrs;
  619. break;
  620. #endif /* HAS_PPC_PMC_IBM */
  621. #ifdef HAS_PPC_PMC_G4
  622. case PPC_PMC_G4:
  623. attrs = g4_common_attrs;
  624. nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
  625. pmc_attrs = classic_pmc_attrs;
  626. break;
  627. #endif /* HAS_PPC_PMC_G4 */
  628. #ifdef HAS_PPC_PMC_PA6T
  629. case PPC_PMC_PA6T:
  630. /* PA Semi starts counting at PMC0 */
  631. attrs = pa6t_attrs;
  632. nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
  633. pmc_attrs = NULL;
  634. break;
  635. #endif /* HAS_PPC_PMC_PA6T */
  636. default:
  637. attrs = NULL;
  638. nattrs = 0;
  639. pmc_attrs = NULL;
  640. }
  641. for (i = 0; i < nattrs; i++)
  642. device_remove_file(s, &attrs[i]);
  643. if (pmc_attrs)
  644. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  645. device_remove_file(s, &pmc_attrs[i]);
  646. #ifdef CONFIG_PPC64
  647. if (cpu_has_feature(CPU_FTR_MMCRA))
  648. device_remove_file(s, &dev_attr_mmcra);
  649. if (cpu_has_feature(CPU_FTR_PURR))
  650. device_remove_file(s, &dev_attr_purr);
  651. if (cpu_has_feature(CPU_FTR_SPURR))
  652. device_remove_file(s, &dev_attr_spurr);
  653. if (cpu_has_feature(CPU_FTR_DSCR))
  654. device_remove_file(s, &dev_attr_dscr);
  655. if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
  656. device_remove_file(s, &dev_attr_pir);
  657. #endif /* CONFIG_PPC64 */
  658. #ifdef CONFIG_PPC_FSL_BOOK3E
  659. if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
  660. device_remove_file(s, &dev_attr_pw20_state);
  661. device_remove_file(s, &dev_attr_pw20_wait_time);
  662. device_remove_file(s, &dev_attr_altivec_idle);
  663. device_remove_file(s, &dev_attr_altivec_idle_wait_time);
  664. }
  665. #endif
  666. cacheinfo_cpu_offline(cpu);
  667. }
  668. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  669. ssize_t arch_cpu_probe(const char *buf, size_t count)
  670. {
  671. if (ppc_md.cpu_probe)
  672. return ppc_md.cpu_probe(buf, count);
  673. return -EINVAL;
  674. }
  675. ssize_t arch_cpu_release(const char *buf, size_t count)
  676. {
  677. if (ppc_md.cpu_release)
  678. return ppc_md.cpu_release(buf, count);
  679. return -EINVAL;
  680. }
  681. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  682. #endif /* CONFIG_HOTPLUG_CPU */
  683. static int sysfs_cpu_notify(struct notifier_block *self,
  684. unsigned long action, void *hcpu)
  685. {
  686. unsigned int cpu = (unsigned int)(long)hcpu;
  687. switch (action) {
  688. case CPU_ONLINE:
  689. case CPU_ONLINE_FROZEN:
  690. register_cpu_online(cpu);
  691. break;
  692. #ifdef CONFIG_HOTPLUG_CPU
  693. case CPU_DEAD:
  694. case CPU_DEAD_FROZEN:
  695. unregister_cpu_online(cpu);
  696. break;
  697. #endif
  698. }
  699. return NOTIFY_OK;
  700. }
  701. static struct notifier_block sysfs_cpu_nb = {
  702. .notifier_call = sysfs_cpu_notify,
  703. };
  704. static DEFINE_MUTEX(cpu_mutex);
  705. int cpu_add_dev_attr(struct device_attribute *attr)
  706. {
  707. int cpu;
  708. mutex_lock(&cpu_mutex);
  709. for_each_possible_cpu(cpu) {
  710. device_create_file(get_cpu_device(cpu), attr);
  711. }
  712. mutex_unlock(&cpu_mutex);
  713. return 0;
  714. }
  715. EXPORT_SYMBOL_GPL(cpu_add_dev_attr);
  716. int cpu_add_dev_attr_group(struct attribute_group *attrs)
  717. {
  718. int cpu;
  719. struct device *dev;
  720. int ret;
  721. mutex_lock(&cpu_mutex);
  722. for_each_possible_cpu(cpu) {
  723. dev = get_cpu_device(cpu);
  724. ret = sysfs_create_group(&dev->kobj, attrs);
  725. WARN_ON(ret != 0);
  726. }
  727. mutex_unlock(&cpu_mutex);
  728. return 0;
  729. }
  730. EXPORT_SYMBOL_GPL(cpu_add_dev_attr_group);
  731. void cpu_remove_dev_attr(struct device_attribute *attr)
  732. {
  733. int cpu;
  734. mutex_lock(&cpu_mutex);
  735. for_each_possible_cpu(cpu) {
  736. device_remove_file(get_cpu_device(cpu), attr);
  737. }
  738. mutex_unlock(&cpu_mutex);
  739. }
  740. EXPORT_SYMBOL_GPL(cpu_remove_dev_attr);
  741. void cpu_remove_dev_attr_group(struct attribute_group *attrs)
  742. {
  743. int cpu;
  744. struct device *dev;
  745. mutex_lock(&cpu_mutex);
  746. for_each_possible_cpu(cpu) {
  747. dev = get_cpu_device(cpu);
  748. sysfs_remove_group(&dev->kobj, attrs);
  749. }
  750. mutex_unlock(&cpu_mutex);
  751. }
  752. EXPORT_SYMBOL_GPL(cpu_remove_dev_attr_group);
  753. /* NUMA stuff */
  754. #ifdef CONFIG_NUMA
  755. static void register_nodes(void)
  756. {
  757. int i;
  758. for (i = 0; i < MAX_NUMNODES; i++)
  759. register_one_node(i);
  760. }
  761. int sysfs_add_device_to_node(struct device *dev, int nid)
  762. {
  763. struct node *node = node_devices[nid];
  764. return sysfs_create_link(&node->dev.kobj, &dev->kobj,
  765. kobject_name(&dev->kobj));
  766. }
  767. EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
  768. void sysfs_remove_device_from_node(struct device *dev, int nid)
  769. {
  770. struct node *node = node_devices[nid];
  771. sysfs_remove_link(&node->dev.kobj, kobject_name(&dev->kobj));
  772. }
  773. EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
  774. #else
  775. static void register_nodes(void)
  776. {
  777. return;
  778. }
  779. #endif
  780. /* Only valid if CPU is present. */
  781. static ssize_t show_physical_id(struct device *dev,
  782. struct device_attribute *attr, char *buf)
  783. {
  784. struct cpu *cpu = container_of(dev, struct cpu, dev);
  785. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->dev.id));
  786. }
  787. static DEVICE_ATTR(physical_id, 0444, show_physical_id, NULL);
  788. static int __init topology_init(void)
  789. {
  790. int cpu;
  791. register_nodes();
  792. cpu_notifier_register_begin();
  793. for_each_possible_cpu(cpu) {
  794. struct cpu *c = &per_cpu(cpu_devices, cpu);
  795. /*
  796. * For now, we just see if the system supports making
  797. * the RTAS calls for CPU hotplug. But, there may be a
  798. * more comprehensive way to do this for an individual
  799. * CPU. For instance, the boot cpu might never be valid
  800. * for hotplugging.
  801. */
  802. if (ppc_md.cpu_die)
  803. c->hotpluggable = 1;
  804. if (cpu_online(cpu) || c->hotpluggable) {
  805. register_cpu(c, cpu);
  806. device_create_file(&c->dev, &dev_attr_physical_id);
  807. }
  808. if (cpu_online(cpu))
  809. register_cpu_online(cpu);
  810. }
  811. __register_cpu_notifier(&sysfs_cpu_nb);
  812. cpu_notifier_register_done();
  813. #ifdef CONFIG_PPC64
  814. sysfs_create_dscr_default();
  815. #endif /* CONFIG_PPC64 */
  816. return 0;
  817. }
  818. subsys_initcall(topology_init);