sysfs.c 28 KB

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