lparcfg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * PowerPC64 LPAR Configuration Information Driver
  3. *
  4. * Dave Engebretsen engebret@us.ibm.com
  5. * Copyright (c) 2003 Dave Engebretsen
  6. * Will Schmidt willschm@us.ibm.com
  7. * SPLPAR updates, Copyright (c) 2003 Will Schmidt IBM Corporation.
  8. * seq_file updates, Copyright (c) 2004 Will Schmidt IBM Corporation.
  9. * Nathan Lynch nathanl@austin.ibm.com
  10. * Added lparcfg_write, Copyright (C) 2004 Nathan Lynch IBM Corporation.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * This driver creates a proc file at /proc/ppc64/lparcfg which contains
  18. * keyword - value pairs that specify the configuration of the partition.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/errno.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/init.h>
  25. #include <linux/seq_file.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/iseries/hv_lp_config.h>
  28. #include <asm/lppaca.h>
  29. #include <asm/hvcall.h>
  30. #include <asm/firmware.h>
  31. #include <asm/rtas.h>
  32. #include <asm/system.h>
  33. #include <asm/time.h>
  34. #include <asm/prom.h>
  35. #include <asm/vdso_datapage.h>
  36. #define MODULE_VERS "1.7"
  37. #define MODULE_NAME "lparcfg"
  38. /* #define LPARCFG_DEBUG */
  39. static struct proc_dir_entry *proc_ppc64_lparcfg;
  40. /*
  41. * Track sum of all purrs across all processors. This is used to further
  42. * calculate usage values by different applications
  43. */
  44. static unsigned long get_purr(void)
  45. {
  46. unsigned long sum_purr = 0;
  47. int cpu;
  48. for_each_possible_cpu(cpu) {
  49. if (firmware_has_feature(FW_FEATURE_ISERIES))
  50. sum_purr += lppaca[cpu].emulated_time_base;
  51. else {
  52. struct cpu_usage *cu;
  53. cu = &per_cpu(cpu_usage_array, cpu);
  54. sum_purr += cu->current_tb;
  55. }
  56. }
  57. return sum_purr;
  58. }
  59. #ifdef CONFIG_PPC_ISERIES
  60. /*
  61. * Methods used to fetch LPAR data when running on an iSeries platform.
  62. */
  63. static int iseries_lparcfg_data(struct seq_file *m, void *v)
  64. {
  65. unsigned long pool_id;
  66. int shared, entitled_capacity, max_entitled_capacity;
  67. int processors, max_processors;
  68. unsigned long purr = get_purr();
  69. shared = (int)(local_paca->lppaca_ptr->shared_proc);
  70. seq_printf(m, "system_active_processors=%d\n",
  71. (int)HvLpConfig_getSystemPhysicalProcessors());
  72. seq_printf(m, "system_potential_processors=%d\n",
  73. (int)HvLpConfig_getSystemPhysicalProcessors());
  74. processors = (int)HvLpConfig_getPhysicalProcessors();
  75. seq_printf(m, "partition_active_processors=%d\n", processors);
  76. max_processors = (int)HvLpConfig_getMaxPhysicalProcessors();
  77. seq_printf(m, "partition_potential_processors=%d\n", max_processors);
  78. if (shared) {
  79. entitled_capacity = HvLpConfig_getSharedProcUnits();
  80. max_entitled_capacity = HvLpConfig_getMaxSharedProcUnits();
  81. } else {
  82. entitled_capacity = processors * 100;
  83. max_entitled_capacity = max_processors * 100;
  84. }
  85. seq_printf(m, "partition_entitled_capacity=%d\n", entitled_capacity);
  86. seq_printf(m, "partition_max_entitled_capacity=%d\n",
  87. max_entitled_capacity);
  88. if (shared) {
  89. pool_id = HvLpConfig_getSharedPoolIndex();
  90. seq_printf(m, "pool=%d\n", (int)pool_id);
  91. seq_printf(m, "pool_capacity=%d\n",
  92. (int)(HvLpConfig_getNumProcsInSharedPool(pool_id) *
  93. 100));
  94. seq_printf(m, "purr=%ld\n", purr);
  95. }
  96. seq_printf(m, "shared_processor_mode=%d\n", shared);
  97. return 0;
  98. }
  99. #else /* CONFIG_PPC_ISERIES */
  100. static int iseries_lparcfg_data(struct seq_file *m, void *v)
  101. {
  102. return 0;
  103. }
  104. #endif /* CONFIG_PPC_ISERIES */
  105. #ifdef CONFIG_PPC_PSERIES
  106. /*
  107. * Methods used to fetch LPAR data when running on a pSeries platform.
  108. */
  109. static void log_plpar_hcall_return(unsigned long rc, char *tag)
  110. {
  111. switch(rc) {
  112. case 0:
  113. return;
  114. case H_HARDWARE:
  115. printk(KERN_INFO "plpar-hcall (%s) "
  116. "Hardware fault\n", tag);
  117. return;
  118. case H_FUNCTION:
  119. printk(KERN_INFO "plpar-hcall (%s) "
  120. "Function not allowed\n", tag);
  121. return;
  122. case H_AUTHORITY:
  123. printk(KERN_INFO "plpar-hcall (%s) "
  124. "Not authorized to this function\n", tag);
  125. return;
  126. case H_PARAMETER:
  127. printk(KERN_INFO "plpar-hcall (%s) "
  128. "Bad parameter(s)\n",tag);
  129. return;
  130. default:
  131. printk(KERN_INFO "plpar-hcall (%s) "
  132. "Unexpected rc(0x%lx)\n", tag, rc);
  133. }
  134. }
  135. /*
  136. * H_GET_PPP hcall returns info in 4 parms.
  137. * entitled_capacity,unallocated_capacity,
  138. * aggregation, resource_capability).
  139. *
  140. * R4 = Entitled Processor Capacity Percentage.
  141. * R5 = Unallocated Processor Capacity Percentage.
  142. * R6 (AABBCCDDEEFFGGHH).
  143. * XXXX - reserved (0)
  144. * XXXX - reserved (0)
  145. * XXXX - Group Number
  146. * XXXX - Pool Number.
  147. * R7 (IIJJKKLLMMNNOOPP).
  148. * XX - reserved. (0)
  149. * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
  150. * XX - variable processor Capacity Weight
  151. * XX - Unallocated Variable Processor Capacity Weight.
  152. * XXXX - Active processors in Physical Processor Pool.
  153. * XXXX - Processors active on platform.
  154. */
  155. static unsigned int h_get_ppp(unsigned long *entitled,
  156. unsigned long *unallocated,
  157. unsigned long *aggregation,
  158. unsigned long *resource)
  159. {
  160. unsigned long rc;
  161. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  162. rc = plpar_hcall(H_GET_PPP, retbuf);
  163. *entitled = retbuf[0];
  164. *unallocated = retbuf[1];
  165. *aggregation = retbuf[2];
  166. *resource = retbuf[3];
  167. log_plpar_hcall_return(rc, "H_GET_PPP");
  168. return rc;
  169. }
  170. static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
  171. {
  172. unsigned long rc;
  173. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  174. rc = plpar_hcall(H_PIC, retbuf);
  175. *pool_idle_time = retbuf[0];
  176. *num_procs = retbuf[1];
  177. if (rc != H_AUTHORITY)
  178. log_plpar_hcall_return(rc, "H_PIC");
  179. }
  180. #define SPLPAR_CHARACTERISTICS_TOKEN 20
  181. #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
  182. /*
  183. * parse_system_parameter_string()
  184. * Retrieve the potential_processors, max_entitled_capacity and friends
  185. * through the get-system-parameter rtas call. Replace keyword strings as
  186. * necessary.
  187. */
  188. static void parse_system_parameter_string(struct seq_file *m)
  189. {
  190. int call_status;
  191. unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
  192. if (!local_buffer) {
  193. printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
  194. __FILE__, __func__, __LINE__);
  195. return;
  196. }
  197. spin_lock(&rtas_data_buf_lock);
  198. memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
  199. call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  200. NULL,
  201. SPLPAR_CHARACTERISTICS_TOKEN,
  202. __pa(rtas_data_buf),
  203. RTAS_DATA_BUF_SIZE);
  204. memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
  205. spin_unlock(&rtas_data_buf_lock);
  206. if (call_status != 0) {
  207. printk(KERN_INFO
  208. "%s %s Error calling get-system-parameter (0x%x)\n",
  209. __FILE__, __func__, call_status);
  210. } else {
  211. int splpar_strlen;
  212. int idx, w_idx;
  213. char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
  214. if (!workbuffer) {
  215. printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
  216. __FILE__, __func__, __LINE__);
  217. kfree(local_buffer);
  218. return;
  219. }
  220. #ifdef LPARCFG_DEBUG
  221. printk(KERN_INFO "success calling get-system-parameter \n");
  222. #endif
  223. splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
  224. local_buffer += 2; /* step over strlen value */
  225. w_idx = 0;
  226. idx = 0;
  227. while ((*local_buffer) && (idx < splpar_strlen)) {
  228. workbuffer[w_idx++] = local_buffer[idx++];
  229. if ((local_buffer[idx] == ',')
  230. || (local_buffer[idx] == '\0')) {
  231. workbuffer[w_idx] = '\0';
  232. if (w_idx) {
  233. /* avoid the empty string */
  234. seq_printf(m, "%s\n", workbuffer);
  235. }
  236. memset(workbuffer, 0, SPLPAR_MAXLENGTH);
  237. idx++; /* skip the comma */
  238. w_idx = 0;
  239. } else if (local_buffer[idx] == '=') {
  240. /* code here to replace workbuffer contents
  241. with different keyword strings */
  242. if (0 == strcmp(workbuffer, "MaxEntCap")) {
  243. strcpy(workbuffer,
  244. "partition_max_entitled_capacity");
  245. w_idx = strlen(workbuffer);
  246. }
  247. if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
  248. strcpy(workbuffer,
  249. "system_potential_processors");
  250. w_idx = strlen(workbuffer);
  251. }
  252. }
  253. }
  254. kfree(workbuffer);
  255. local_buffer -= 2; /* back up over strlen value */
  256. }
  257. kfree(local_buffer);
  258. }
  259. /* Return the number of processors in the system.
  260. * This function reads through the device tree and counts
  261. * the virtual processors, this does not include threads.
  262. */
  263. static int lparcfg_count_active_processors(void)
  264. {
  265. struct device_node *cpus_dn = NULL;
  266. int count = 0;
  267. while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
  268. #ifdef LPARCFG_DEBUG
  269. printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
  270. #endif
  271. count++;
  272. }
  273. return count;
  274. }
  275. static int pseries_lparcfg_data(struct seq_file *m, void *v)
  276. {
  277. int partition_potential_processors;
  278. int partition_active_processors;
  279. struct device_node *rtas_node;
  280. const int *lrdrp = NULL;
  281. rtas_node = of_find_node_by_path("/rtas");
  282. if (rtas_node)
  283. lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
  284. if (lrdrp == NULL) {
  285. partition_potential_processors = vdso_data->processorCount;
  286. } else {
  287. partition_potential_processors = *(lrdrp + 4);
  288. }
  289. of_node_put(rtas_node);
  290. partition_active_processors = lparcfg_count_active_processors();
  291. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  292. unsigned long h_entitled, h_unallocated;
  293. unsigned long h_aggregation, h_resource;
  294. unsigned long pool_idle_time, pool_procs;
  295. unsigned long purr;
  296. h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
  297. &h_resource);
  298. seq_printf(m, "R4=0x%lx\n", h_entitled);
  299. seq_printf(m, "R5=0x%lx\n", h_unallocated);
  300. seq_printf(m, "R6=0x%lx\n", h_aggregation);
  301. seq_printf(m, "R7=0x%lx\n", h_resource);
  302. purr = get_purr();
  303. /* this call handles the ibm,get-system-parameter contents */
  304. parse_system_parameter_string(m);
  305. seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
  306. seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
  307. seq_printf(m, "system_active_processors=%ld\n",
  308. (h_resource >> 0 * 8) & 0xffff);
  309. /* pool related entries are apropriate for shared configs */
  310. if (lppaca[0].shared_proc) {
  311. h_pic(&pool_idle_time, &pool_procs);
  312. seq_printf(m, "pool=%ld\n",
  313. (h_aggregation >> 0 * 8) & 0xffff);
  314. /* report pool_capacity in percentage */
  315. seq_printf(m, "pool_capacity=%ld\n",
  316. ((h_resource >> 2 * 8) & 0xffff) * 100);
  317. seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
  318. seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
  319. }
  320. seq_printf(m, "unallocated_capacity_weight=%ld\n",
  321. (h_resource >> 4 * 8) & 0xFF);
  322. seq_printf(m, "capacity_weight=%ld\n",
  323. (h_resource >> 5 * 8) & 0xFF);
  324. seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
  325. seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
  326. seq_printf(m, "purr=%ld\n", purr);
  327. } else { /* non SPLPAR case */
  328. seq_printf(m, "system_active_processors=%d\n",
  329. partition_potential_processors);
  330. seq_printf(m, "system_potential_processors=%d\n",
  331. partition_potential_processors);
  332. seq_printf(m, "partition_max_entitled_capacity=%d\n",
  333. partition_potential_processors * 100);
  334. seq_printf(m, "partition_entitled_capacity=%d\n",
  335. partition_active_processors * 100);
  336. }
  337. seq_printf(m, "partition_active_processors=%d\n",
  338. partition_active_processors);
  339. seq_printf(m, "partition_potential_processors=%d\n",
  340. partition_potential_processors);
  341. seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
  342. return 0;
  343. }
  344. /*
  345. * Interface for changing system parameters (variable capacity weight
  346. * and entitled capacity). Format of input is "param_name=value";
  347. * anything after value is ignored. Valid parameters at this time are
  348. * "partition_entitled_capacity" and "capacity_weight". We use
  349. * H_SET_PPP to alter parameters.
  350. *
  351. * This function should be invoked only on systems with
  352. * FW_FEATURE_SPLPAR.
  353. */
  354. static ssize_t lparcfg_write(struct file *file, const char __user * buf,
  355. size_t count, loff_t * off)
  356. {
  357. char *kbuf;
  358. char *tmp;
  359. u64 new_entitled, *new_entitled_ptr = &new_entitled;
  360. u8 new_weight, *new_weight_ptr = &new_weight;
  361. unsigned long current_entitled; /* parameters for h_get_ppp */
  362. unsigned long dummy;
  363. unsigned long resource;
  364. u8 current_weight;
  365. ssize_t retval = -ENOMEM;
  366. if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
  367. firmware_has_feature(FW_FEATURE_ISERIES))
  368. return -EINVAL;
  369. kbuf = kmalloc(count, GFP_KERNEL);
  370. if (!kbuf)
  371. goto out;
  372. retval = -EFAULT;
  373. if (copy_from_user(kbuf, buf, count))
  374. goto out;
  375. retval = -EINVAL;
  376. kbuf[count - 1] = '\0';
  377. tmp = strchr(kbuf, '=');
  378. if (!tmp)
  379. goto out;
  380. *tmp++ = '\0';
  381. if (!strcmp(kbuf, "partition_entitled_capacity")) {
  382. char *endp;
  383. *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
  384. if (endp == tmp)
  385. goto out;
  386. new_weight_ptr = &current_weight;
  387. } else if (!strcmp(kbuf, "capacity_weight")) {
  388. char *endp;
  389. *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
  390. if (endp == tmp)
  391. goto out;
  392. new_entitled_ptr = &current_entitled;
  393. } else
  394. goto out;
  395. /* Get our current parameters */
  396. retval = h_get_ppp(&current_entitled, &dummy, &dummy, &resource);
  397. if (retval) {
  398. retval = -EIO;
  399. goto out;
  400. }
  401. current_weight = (resource >> 5 * 8) & 0xFF;
  402. pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
  403. __func__, current_entitled, current_weight);
  404. pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
  405. __func__, *new_entitled_ptr, *new_weight_ptr);
  406. retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
  407. *new_weight_ptr);
  408. if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
  409. retval = count;
  410. } else if (retval == H_BUSY) {
  411. retval = -EBUSY;
  412. } else if (retval == H_HARDWARE) {
  413. retval = -EIO;
  414. } else if (retval == H_PARAMETER) {
  415. retval = -EINVAL;
  416. } else {
  417. printk(KERN_WARNING "%s: received unknown hv return code %ld",
  418. __func__, retval);
  419. retval = -EIO;
  420. }
  421. out:
  422. kfree(kbuf);
  423. return retval;
  424. }
  425. #else /* CONFIG_PPC_PSERIES */
  426. static int pseries_lparcfg_data(struct seq_file *m, void *v)
  427. {
  428. return 0;
  429. }
  430. static ssize_t lparcfg_write(struct file *file, const char __user * buf,
  431. size_t count, loff_t * off)
  432. {
  433. return -EINVAL;
  434. }
  435. #endif /* CONFIG_PPC_PSERIES */
  436. static int lparcfg_data(struct seq_file *m, void *v)
  437. {
  438. struct device_node *rootdn;
  439. const char *model = "";
  440. const char *system_id = "";
  441. const char *tmp;
  442. const unsigned int *lp_index_ptr;
  443. unsigned int lp_index = 0;
  444. seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
  445. rootdn = of_find_node_by_path("/");
  446. if (rootdn) {
  447. tmp = of_get_property(rootdn, "model", NULL);
  448. if (tmp) {
  449. model = tmp;
  450. /* Skip "IBM," - see platforms/iseries/dt.c */
  451. if (firmware_has_feature(FW_FEATURE_ISERIES))
  452. model += 4;
  453. }
  454. tmp = of_get_property(rootdn, "system-id", NULL);
  455. if (tmp) {
  456. system_id = tmp;
  457. /* Skip "IBM," - see platforms/iseries/dt.c */
  458. if (firmware_has_feature(FW_FEATURE_ISERIES))
  459. system_id += 4;
  460. }
  461. lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
  462. NULL);
  463. if (lp_index_ptr)
  464. lp_index = *lp_index_ptr;
  465. of_node_put(rootdn);
  466. }
  467. seq_printf(m, "serial_number=%s\n", system_id);
  468. seq_printf(m, "system_type=%s\n", model);
  469. seq_printf(m, "partition_id=%d\n", (int)lp_index);
  470. if (firmware_has_feature(FW_FEATURE_ISERIES))
  471. return iseries_lparcfg_data(m, v);
  472. return pseries_lparcfg_data(m, v);
  473. }
  474. static int lparcfg_open(struct inode *inode, struct file *file)
  475. {
  476. return single_open(file, lparcfg_data, NULL);
  477. }
  478. const struct file_operations lparcfg_fops = {
  479. .owner = THIS_MODULE,
  480. .read = seq_read,
  481. .write = lparcfg_write,
  482. .open = lparcfg_open,
  483. .release = single_release,
  484. };
  485. int __init lparcfg_init(void)
  486. {
  487. struct proc_dir_entry *ent;
  488. mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
  489. /* Allow writing if we have FW_FEATURE_SPLPAR */
  490. if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
  491. !firmware_has_feature(FW_FEATURE_ISERIES))
  492. mode |= S_IWUSR;
  493. ent = create_proc_entry("ppc64/lparcfg", mode, NULL);
  494. if (ent) {
  495. ent->proc_fops = &lparcfg_fops;
  496. } else {
  497. printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
  498. return -EIO;
  499. }
  500. proc_ppc64_lparcfg = ent;
  501. return 0;
  502. }
  503. void __exit lparcfg_cleanup(void)
  504. {
  505. if (proc_ppc64_lparcfg)
  506. remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
  507. }
  508. module_init(lparcfg_init);
  509. module_exit(lparcfg_cleanup);
  510. MODULE_DESCRIPTION("Interface for LPAR configuration data");
  511. MODULE_AUTHOR("Dave Engebretsen");
  512. MODULE_LICENSE("GPL");