lparcfg.c 16 KB

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