lparcfg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. /*
  110. * H_GET_PPP hcall returns info in 4 parms.
  111. * entitled_capacity,unallocated_capacity,
  112. * aggregation, resource_capability).
  113. *
  114. * R4 = Entitled Processor Capacity Percentage.
  115. * R5 = Unallocated Processor Capacity Percentage.
  116. * R6 (AABBCCDDEEFFGGHH).
  117. * XXXX - reserved (0)
  118. * XXXX - reserved (0)
  119. * XXXX - Group Number
  120. * XXXX - Pool Number.
  121. * R7 (IIJJKKLLMMNNOOPP).
  122. * XX - reserved. (0)
  123. * XX - bit 0-6 reserved (0). bit 7 is Capped indicator.
  124. * XX - variable processor Capacity Weight
  125. * XX - Unallocated Variable Processor Capacity Weight.
  126. * XXXX - Active processors in Physical Processor Pool.
  127. * XXXX - Processors active on platform.
  128. */
  129. static unsigned int h_get_ppp(unsigned long *entitled,
  130. unsigned long *unallocated,
  131. unsigned long *aggregation,
  132. unsigned long *resource)
  133. {
  134. unsigned long rc;
  135. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  136. rc = plpar_hcall(H_GET_PPP, retbuf);
  137. *entitled = retbuf[0];
  138. *unallocated = retbuf[1];
  139. *aggregation = retbuf[2];
  140. *resource = retbuf[3];
  141. return rc;
  142. }
  143. static void h_pic(unsigned long *pool_idle_time, unsigned long *num_procs)
  144. {
  145. unsigned long rc;
  146. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  147. rc = plpar_hcall(H_PIC, retbuf);
  148. *pool_idle_time = retbuf[0];
  149. *num_procs = retbuf[1];
  150. }
  151. #define SPLPAR_CHARACTERISTICS_TOKEN 20
  152. #define SPLPAR_MAXLENGTH 1026*(sizeof(char))
  153. /*
  154. * parse_system_parameter_string()
  155. * Retrieve the potential_processors, max_entitled_capacity and friends
  156. * through the get-system-parameter rtas call. Replace keyword strings as
  157. * necessary.
  158. */
  159. static void parse_system_parameter_string(struct seq_file *m)
  160. {
  161. int call_status;
  162. unsigned char *local_buffer = kmalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
  163. if (!local_buffer) {
  164. printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
  165. __FILE__, __func__, __LINE__);
  166. return;
  167. }
  168. spin_lock(&rtas_data_buf_lock);
  169. memset(rtas_data_buf, 0, SPLPAR_MAXLENGTH);
  170. call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
  171. NULL,
  172. SPLPAR_CHARACTERISTICS_TOKEN,
  173. __pa(rtas_data_buf),
  174. RTAS_DATA_BUF_SIZE);
  175. memcpy(local_buffer, rtas_data_buf, SPLPAR_MAXLENGTH);
  176. spin_unlock(&rtas_data_buf_lock);
  177. if (call_status != 0) {
  178. printk(KERN_INFO
  179. "%s %s Error calling get-system-parameter (0x%x)\n",
  180. __FILE__, __func__, call_status);
  181. } else {
  182. int splpar_strlen;
  183. int idx, w_idx;
  184. char *workbuffer = kzalloc(SPLPAR_MAXLENGTH, GFP_KERNEL);
  185. if (!workbuffer) {
  186. printk(KERN_ERR "%s %s kmalloc failure at line %d \n",
  187. __FILE__, __func__, __LINE__);
  188. kfree(local_buffer);
  189. return;
  190. }
  191. #ifdef LPARCFG_DEBUG
  192. printk(KERN_INFO "success calling get-system-parameter \n");
  193. #endif
  194. splpar_strlen = local_buffer[0] * 256 + local_buffer[1];
  195. local_buffer += 2; /* step over strlen value */
  196. w_idx = 0;
  197. idx = 0;
  198. while ((*local_buffer) && (idx < splpar_strlen)) {
  199. workbuffer[w_idx++] = local_buffer[idx++];
  200. if ((local_buffer[idx] == ',')
  201. || (local_buffer[idx] == '\0')) {
  202. workbuffer[w_idx] = '\0';
  203. if (w_idx) {
  204. /* avoid the empty string */
  205. seq_printf(m, "%s\n", workbuffer);
  206. }
  207. memset(workbuffer, 0, SPLPAR_MAXLENGTH);
  208. idx++; /* skip the comma */
  209. w_idx = 0;
  210. } else if (local_buffer[idx] == '=') {
  211. /* code here to replace workbuffer contents
  212. with different keyword strings */
  213. if (0 == strcmp(workbuffer, "MaxEntCap")) {
  214. strcpy(workbuffer,
  215. "partition_max_entitled_capacity");
  216. w_idx = strlen(workbuffer);
  217. }
  218. if (0 == strcmp(workbuffer, "MaxPlatProcs")) {
  219. strcpy(workbuffer,
  220. "system_potential_processors");
  221. w_idx = strlen(workbuffer);
  222. }
  223. }
  224. }
  225. kfree(workbuffer);
  226. local_buffer -= 2; /* back up over strlen value */
  227. }
  228. kfree(local_buffer);
  229. }
  230. /* Return the number of processors in the system.
  231. * This function reads through the device tree and counts
  232. * the virtual processors, this does not include threads.
  233. */
  234. static int lparcfg_count_active_processors(void)
  235. {
  236. struct device_node *cpus_dn = NULL;
  237. int count = 0;
  238. while ((cpus_dn = of_find_node_by_type(cpus_dn, "cpu"))) {
  239. #ifdef LPARCFG_DEBUG
  240. printk(KERN_ERR "cpus_dn %p \n", cpus_dn);
  241. #endif
  242. count++;
  243. }
  244. return count;
  245. }
  246. static int pseries_lparcfg_data(struct seq_file *m, void *v)
  247. {
  248. int partition_potential_processors;
  249. int partition_active_processors;
  250. struct device_node *rtas_node;
  251. const int *lrdrp = NULL;
  252. rtas_node = of_find_node_by_path("/rtas");
  253. if (rtas_node)
  254. lrdrp = of_get_property(rtas_node, "ibm,lrdr-capacity", NULL);
  255. if (lrdrp == NULL) {
  256. partition_potential_processors = vdso_data->processorCount;
  257. } else {
  258. partition_potential_processors = *(lrdrp + 4);
  259. }
  260. of_node_put(rtas_node);
  261. partition_active_processors = lparcfg_count_active_processors();
  262. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  263. unsigned long h_entitled, h_unallocated;
  264. unsigned long h_aggregation, h_resource;
  265. unsigned long pool_idle_time, pool_procs;
  266. unsigned long purr;
  267. h_get_ppp(&h_entitled, &h_unallocated, &h_aggregation,
  268. &h_resource);
  269. seq_printf(m, "R4=0x%lx\n", h_entitled);
  270. seq_printf(m, "R5=0x%lx\n", h_unallocated);
  271. seq_printf(m, "R6=0x%lx\n", h_aggregation);
  272. seq_printf(m, "R7=0x%lx\n", h_resource);
  273. purr = get_purr();
  274. /* this call handles the ibm,get-system-parameter contents */
  275. parse_system_parameter_string(m);
  276. seq_printf(m, "partition_entitled_capacity=%ld\n", h_entitled);
  277. seq_printf(m, "group=%ld\n", (h_aggregation >> 2 * 8) & 0xffff);
  278. seq_printf(m, "system_active_processors=%ld\n",
  279. (h_resource >> 0 * 8) & 0xffff);
  280. /* pool related entries are apropriate for shared configs */
  281. if (lppaca[0].shared_proc) {
  282. h_pic(&pool_idle_time, &pool_procs);
  283. seq_printf(m, "pool=%ld\n",
  284. (h_aggregation >> 0 * 8) & 0xffff);
  285. /* report pool_capacity in percentage */
  286. seq_printf(m, "pool_capacity=%ld\n",
  287. ((h_resource >> 2 * 8) & 0xffff) * 100);
  288. seq_printf(m, "pool_idle_time=%ld\n", pool_idle_time);
  289. seq_printf(m, "pool_num_procs=%ld\n", pool_procs);
  290. }
  291. seq_printf(m, "unallocated_capacity_weight=%ld\n",
  292. (h_resource >> 4 * 8) & 0xFF);
  293. seq_printf(m, "capacity_weight=%ld\n",
  294. (h_resource >> 5 * 8) & 0xFF);
  295. seq_printf(m, "capped=%ld\n", (h_resource >> 6 * 8) & 0x01);
  296. seq_printf(m, "unallocated_capacity=%ld\n", h_unallocated);
  297. seq_printf(m, "purr=%ld\n", purr);
  298. } else { /* non SPLPAR case */
  299. seq_printf(m, "system_active_processors=%d\n",
  300. partition_potential_processors);
  301. seq_printf(m, "system_potential_processors=%d\n",
  302. partition_potential_processors);
  303. seq_printf(m, "partition_max_entitled_capacity=%d\n",
  304. partition_potential_processors * 100);
  305. seq_printf(m, "partition_entitled_capacity=%d\n",
  306. partition_active_processors * 100);
  307. }
  308. seq_printf(m, "partition_active_processors=%d\n",
  309. partition_active_processors);
  310. seq_printf(m, "partition_potential_processors=%d\n",
  311. partition_potential_processors);
  312. seq_printf(m, "shared_processor_mode=%d\n", lppaca[0].shared_proc);
  313. return 0;
  314. }
  315. /*
  316. * Interface for changing system parameters (variable capacity weight
  317. * and entitled capacity). Format of input is "param_name=value";
  318. * anything after value is ignored. Valid parameters at this time are
  319. * "partition_entitled_capacity" and "capacity_weight". We use
  320. * H_SET_PPP to alter parameters.
  321. *
  322. * This function should be invoked only on systems with
  323. * FW_FEATURE_SPLPAR.
  324. */
  325. static ssize_t lparcfg_write(struct file *file, const char __user * buf,
  326. size_t count, loff_t * off)
  327. {
  328. char *kbuf;
  329. char *tmp;
  330. u64 new_entitled, *new_entitled_ptr = &new_entitled;
  331. u8 new_weight, *new_weight_ptr = &new_weight;
  332. unsigned long current_entitled; /* parameters for h_get_ppp */
  333. unsigned long dummy;
  334. unsigned long resource;
  335. u8 current_weight;
  336. ssize_t retval = -ENOMEM;
  337. if (!firmware_has_feature(FW_FEATURE_SPLPAR) ||
  338. firmware_has_feature(FW_FEATURE_ISERIES))
  339. return -EINVAL;
  340. kbuf = kmalloc(count, GFP_KERNEL);
  341. if (!kbuf)
  342. goto out;
  343. retval = -EFAULT;
  344. if (copy_from_user(kbuf, buf, count))
  345. goto out;
  346. retval = -EINVAL;
  347. kbuf[count - 1] = '\0';
  348. tmp = strchr(kbuf, '=');
  349. if (!tmp)
  350. goto out;
  351. *tmp++ = '\0';
  352. if (!strcmp(kbuf, "partition_entitled_capacity")) {
  353. char *endp;
  354. *new_entitled_ptr = (u64) simple_strtoul(tmp, &endp, 10);
  355. if (endp == tmp)
  356. goto out;
  357. new_weight_ptr = &current_weight;
  358. } else if (!strcmp(kbuf, "capacity_weight")) {
  359. char *endp;
  360. *new_weight_ptr = (u8) simple_strtoul(tmp, &endp, 10);
  361. if (endp == tmp)
  362. goto out;
  363. new_entitled_ptr = &current_entitled;
  364. } else
  365. goto out;
  366. /* Get our current parameters */
  367. retval = h_get_ppp(&current_entitled, &dummy, &dummy, &resource);
  368. if (retval) {
  369. retval = -EIO;
  370. goto out;
  371. }
  372. current_weight = (resource >> 5 * 8) & 0xFF;
  373. pr_debug("%s: current_entitled = %lu, current_weight = %u\n",
  374. __func__, current_entitled, current_weight);
  375. pr_debug("%s: new_entitled = %lu, new_weight = %u\n",
  376. __func__, *new_entitled_ptr, *new_weight_ptr);
  377. retval = plpar_hcall_norets(H_SET_PPP, *new_entitled_ptr,
  378. *new_weight_ptr);
  379. if (retval == H_SUCCESS || retval == H_CONSTRAINED) {
  380. retval = count;
  381. } else if (retval == H_BUSY) {
  382. retval = -EBUSY;
  383. } else if (retval == H_HARDWARE) {
  384. retval = -EIO;
  385. } else if (retval == H_PARAMETER) {
  386. retval = -EINVAL;
  387. } else {
  388. printk(KERN_WARNING "%s: received unknown hv return code %ld",
  389. __func__, retval);
  390. retval = -EIO;
  391. }
  392. out:
  393. kfree(kbuf);
  394. return retval;
  395. }
  396. #else /* CONFIG_PPC_PSERIES */
  397. static int pseries_lparcfg_data(struct seq_file *m, void *v)
  398. {
  399. return 0;
  400. }
  401. static ssize_t lparcfg_write(struct file *file, const char __user * buf,
  402. size_t count, loff_t * off)
  403. {
  404. return -EINVAL;
  405. }
  406. #endif /* CONFIG_PPC_PSERIES */
  407. static int lparcfg_data(struct seq_file *m, void *v)
  408. {
  409. struct device_node *rootdn;
  410. const char *model = "";
  411. const char *system_id = "";
  412. const char *tmp;
  413. const unsigned int *lp_index_ptr;
  414. unsigned int lp_index = 0;
  415. seq_printf(m, "%s %s \n", MODULE_NAME, MODULE_VERS);
  416. rootdn = of_find_node_by_path("/");
  417. if (rootdn) {
  418. tmp = of_get_property(rootdn, "model", NULL);
  419. if (tmp) {
  420. model = tmp;
  421. /* Skip "IBM," - see platforms/iseries/dt.c */
  422. if (firmware_has_feature(FW_FEATURE_ISERIES))
  423. model += 4;
  424. }
  425. tmp = of_get_property(rootdn, "system-id", NULL);
  426. if (tmp) {
  427. system_id = tmp;
  428. /* Skip "IBM," - see platforms/iseries/dt.c */
  429. if (firmware_has_feature(FW_FEATURE_ISERIES))
  430. system_id += 4;
  431. }
  432. lp_index_ptr = of_get_property(rootdn, "ibm,partition-no",
  433. NULL);
  434. if (lp_index_ptr)
  435. lp_index = *lp_index_ptr;
  436. of_node_put(rootdn);
  437. }
  438. seq_printf(m, "serial_number=%s\n", system_id);
  439. seq_printf(m, "system_type=%s\n", model);
  440. seq_printf(m, "partition_id=%d\n", (int)lp_index);
  441. if (firmware_has_feature(FW_FEATURE_ISERIES))
  442. return iseries_lparcfg_data(m, v);
  443. return pseries_lparcfg_data(m, v);
  444. }
  445. static int lparcfg_open(struct inode *inode, struct file *file)
  446. {
  447. return single_open(file, lparcfg_data, NULL);
  448. }
  449. static const struct file_operations lparcfg_fops = {
  450. .owner = THIS_MODULE,
  451. .read = seq_read,
  452. .write = lparcfg_write,
  453. .open = lparcfg_open,
  454. .release = single_release,
  455. };
  456. static int __init lparcfg_init(void)
  457. {
  458. struct proc_dir_entry *ent;
  459. mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
  460. /* Allow writing if we have FW_FEATURE_SPLPAR */
  461. if (firmware_has_feature(FW_FEATURE_SPLPAR) &&
  462. !firmware_has_feature(FW_FEATURE_ISERIES))
  463. mode |= S_IWUSR;
  464. ent = proc_create("ppc64/lparcfg", mode, NULL, &lparcfg_fops);
  465. if (!ent) {
  466. printk(KERN_ERR "Failed to create ppc64/lparcfg\n");
  467. return -EIO;
  468. }
  469. proc_ppc64_lparcfg = ent;
  470. return 0;
  471. }
  472. static void __exit lparcfg_cleanup(void)
  473. {
  474. if (proc_ppc64_lparcfg)
  475. remove_proc_entry("lparcfg", proc_ppc64_lparcfg->parent);
  476. }
  477. module_init(lparcfg_init);
  478. module_exit(lparcfg_cleanup);
  479. MODULE_DESCRIPTION("Interface for LPAR configuration data");
  480. MODULE_AUTHOR("Dave Engebretsen");
  481. MODULE_LICENSE("GPL");