sthyi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * store hypervisor information instruction emulation functions.
  4. *
  5. * Copyright IBM Corp. 2016
  6. * Author(s): Janosch Frank <frankja@linux.vnet.ibm.com>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/mutex.h>
  13. #include <asm/asm-offsets.h>
  14. #include <asm/sclp.h>
  15. #include <asm/diag.h>
  16. #include <asm/sysinfo.h>
  17. #include <asm/ebcdic.h>
  18. #include <asm/facility.h>
  19. #include <asm/sthyi.h>
  20. #include "entry.h"
  21. #define DED_WEIGHT 0xffff
  22. /*
  23. * CP and IFL as EBCDIC strings, SP/0x40 determines the end of string
  24. * as they are justified with spaces.
  25. */
  26. #define CP 0xc3d7404040404040UL
  27. #define IFL 0xc9c6d34040404040UL
  28. enum hdr_flags {
  29. HDR_NOT_LPAR = 0x10,
  30. HDR_STACK_INCM = 0x20,
  31. HDR_STSI_UNAV = 0x40,
  32. HDR_PERF_UNAV = 0x80,
  33. };
  34. enum mac_validity {
  35. MAC_NAME_VLD = 0x20,
  36. MAC_ID_VLD = 0x40,
  37. MAC_CNT_VLD = 0x80,
  38. };
  39. enum par_flag {
  40. PAR_MT_EN = 0x80,
  41. };
  42. enum par_validity {
  43. PAR_GRP_VLD = 0x08,
  44. PAR_ID_VLD = 0x10,
  45. PAR_ABS_VLD = 0x20,
  46. PAR_WGHT_VLD = 0x40,
  47. PAR_PCNT_VLD = 0x80,
  48. };
  49. struct hdr_sctn {
  50. u8 infhflg1;
  51. u8 infhflg2; /* reserved */
  52. u8 infhval1; /* reserved */
  53. u8 infhval2; /* reserved */
  54. u8 reserved[3];
  55. u8 infhygct;
  56. u16 infhtotl;
  57. u16 infhdln;
  58. u16 infmoff;
  59. u16 infmlen;
  60. u16 infpoff;
  61. u16 infplen;
  62. u16 infhoff1;
  63. u16 infhlen1;
  64. u16 infgoff1;
  65. u16 infglen1;
  66. u16 infhoff2;
  67. u16 infhlen2;
  68. u16 infgoff2;
  69. u16 infglen2;
  70. u16 infhoff3;
  71. u16 infhlen3;
  72. u16 infgoff3;
  73. u16 infglen3;
  74. u8 reserved2[4];
  75. } __packed;
  76. struct mac_sctn {
  77. u8 infmflg1; /* reserved */
  78. u8 infmflg2; /* reserved */
  79. u8 infmval1;
  80. u8 infmval2; /* reserved */
  81. u16 infmscps;
  82. u16 infmdcps;
  83. u16 infmsifl;
  84. u16 infmdifl;
  85. char infmname[8];
  86. char infmtype[4];
  87. char infmmanu[16];
  88. char infmseq[16];
  89. char infmpman[4];
  90. u8 reserved[4];
  91. } __packed;
  92. struct par_sctn {
  93. u8 infpflg1;
  94. u8 infpflg2; /* reserved */
  95. u8 infpval1;
  96. u8 infpval2; /* reserved */
  97. u16 infppnum;
  98. u16 infpscps;
  99. u16 infpdcps;
  100. u16 infpsifl;
  101. u16 infpdifl;
  102. u16 reserved;
  103. char infppnam[8];
  104. u32 infpwbcp;
  105. u32 infpabcp;
  106. u32 infpwbif;
  107. u32 infpabif;
  108. char infplgnm[8];
  109. u32 infplgcp;
  110. u32 infplgif;
  111. } __packed;
  112. struct sthyi_sctns {
  113. struct hdr_sctn hdr;
  114. struct mac_sctn mac;
  115. struct par_sctn par;
  116. } __packed;
  117. struct cpu_inf {
  118. u64 lpar_cap;
  119. u64 lpar_grp_cap;
  120. u64 lpar_weight;
  121. u64 all_weight;
  122. int cpu_num_ded;
  123. int cpu_num_shd;
  124. };
  125. struct lpar_cpu_inf {
  126. struct cpu_inf cp;
  127. struct cpu_inf ifl;
  128. };
  129. /*
  130. * STHYI requires extensive locking in the higher hypervisors
  131. * and is very computational/memory expensive. Therefore we
  132. * cache the retrieved data whose valid period is 1s.
  133. */
  134. #define CACHE_VALID_JIFFIES HZ
  135. struct sthyi_info {
  136. void *info;
  137. unsigned long end;
  138. };
  139. static DEFINE_MUTEX(sthyi_mutex);
  140. static struct sthyi_info sthyi_cache;
  141. static inline u64 cpu_id(u8 ctidx, void *diag224_buf)
  142. {
  143. return *((u64 *)(diag224_buf + (ctidx + 1) * DIAG204_CPU_NAME_LEN));
  144. }
  145. /*
  146. * Scales the cpu capping from the lpar range to the one expected in
  147. * sthyi data.
  148. *
  149. * diag204 reports a cap in hundredths of processor units.
  150. * z/VM's range for one core is 0 - 0x10000.
  151. */
  152. static u32 scale_cap(u32 in)
  153. {
  154. return (0x10000 * in) / 100;
  155. }
  156. static void fill_hdr(struct sthyi_sctns *sctns)
  157. {
  158. sctns->hdr.infhdln = sizeof(sctns->hdr);
  159. sctns->hdr.infmoff = sizeof(sctns->hdr);
  160. sctns->hdr.infmlen = sizeof(sctns->mac);
  161. sctns->hdr.infplen = sizeof(sctns->par);
  162. sctns->hdr.infpoff = sctns->hdr.infhdln + sctns->hdr.infmlen;
  163. sctns->hdr.infhtotl = sctns->hdr.infpoff + sctns->hdr.infplen;
  164. }
  165. static void fill_stsi_mac(struct sthyi_sctns *sctns,
  166. struct sysinfo_1_1_1 *sysinfo)
  167. {
  168. if (stsi(sysinfo, 1, 1, 1))
  169. return;
  170. sclp_ocf_cpc_name_copy(sctns->mac.infmname);
  171. memcpy(sctns->mac.infmtype, sysinfo->type, sizeof(sctns->mac.infmtype));
  172. memcpy(sctns->mac.infmmanu, sysinfo->manufacturer, sizeof(sctns->mac.infmmanu));
  173. memcpy(sctns->mac.infmpman, sysinfo->plant, sizeof(sctns->mac.infmpman));
  174. memcpy(sctns->mac.infmseq, sysinfo->sequence, sizeof(sctns->mac.infmseq));
  175. sctns->mac.infmval1 |= MAC_ID_VLD | MAC_NAME_VLD;
  176. }
  177. static void fill_stsi_par(struct sthyi_sctns *sctns,
  178. struct sysinfo_2_2_2 *sysinfo)
  179. {
  180. if (stsi(sysinfo, 2, 2, 2))
  181. return;
  182. sctns->par.infppnum = sysinfo->lpar_number;
  183. memcpy(sctns->par.infppnam, sysinfo->name, sizeof(sctns->par.infppnam));
  184. sctns->par.infpval1 |= PAR_ID_VLD;
  185. }
  186. static void fill_stsi(struct sthyi_sctns *sctns)
  187. {
  188. void *sysinfo;
  189. /* Errors are handled through the validity bits in the response. */
  190. sysinfo = (void *)__get_free_page(GFP_KERNEL);
  191. if (!sysinfo)
  192. return;
  193. fill_stsi_mac(sctns, sysinfo);
  194. fill_stsi_par(sctns, sysinfo);
  195. free_pages((unsigned long)sysinfo, 0);
  196. }
  197. static void fill_diag_mac(struct sthyi_sctns *sctns,
  198. struct diag204_x_phys_block *block,
  199. void *diag224_buf)
  200. {
  201. int i;
  202. for (i = 0; i < block->hdr.cpus; i++) {
  203. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  204. case CP:
  205. if (block->cpus[i].weight == DED_WEIGHT)
  206. sctns->mac.infmdcps++;
  207. else
  208. sctns->mac.infmscps++;
  209. break;
  210. case IFL:
  211. if (block->cpus[i].weight == DED_WEIGHT)
  212. sctns->mac.infmdifl++;
  213. else
  214. sctns->mac.infmsifl++;
  215. break;
  216. }
  217. }
  218. sctns->mac.infmval1 |= MAC_CNT_VLD;
  219. }
  220. /* Returns a pointer to the the next partition block. */
  221. static struct diag204_x_part_block *lpar_cpu_inf(struct lpar_cpu_inf *part_inf,
  222. bool this_lpar,
  223. void *diag224_buf,
  224. struct diag204_x_part_block *block)
  225. {
  226. int i, capped = 0, weight_cp = 0, weight_ifl = 0;
  227. struct cpu_inf *cpu_inf;
  228. for (i = 0; i < block->hdr.rcpus; i++) {
  229. if (!(block->cpus[i].cflag & DIAG204_CPU_ONLINE))
  230. continue;
  231. switch (cpu_id(block->cpus[i].ctidx, diag224_buf)) {
  232. case CP:
  233. cpu_inf = &part_inf->cp;
  234. if (block->cpus[i].cur_weight < DED_WEIGHT)
  235. weight_cp |= block->cpus[i].cur_weight;
  236. break;
  237. case IFL:
  238. cpu_inf = &part_inf->ifl;
  239. if (block->cpus[i].cur_weight < DED_WEIGHT)
  240. weight_ifl |= block->cpus[i].cur_weight;
  241. break;
  242. default:
  243. continue;
  244. }
  245. if (!this_lpar)
  246. continue;
  247. capped |= block->cpus[i].cflag & DIAG204_CPU_CAPPED;
  248. cpu_inf->lpar_cap |= block->cpus[i].cpu_type_cap;
  249. cpu_inf->lpar_grp_cap |= block->cpus[i].group_cpu_type_cap;
  250. if (block->cpus[i].weight == DED_WEIGHT)
  251. cpu_inf->cpu_num_ded += 1;
  252. else
  253. cpu_inf->cpu_num_shd += 1;
  254. }
  255. if (this_lpar && capped) {
  256. part_inf->cp.lpar_weight = weight_cp;
  257. part_inf->ifl.lpar_weight = weight_ifl;
  258. }
  259. part_inf->cp.all_weight += weight_cp;
  260. part_inf->ifl.all_weight += weight_ifl;
  261. return (struct diag204_x_part_block *)&block->cpus[i];
  262. }
  263. static void fill_diag(struct sthyi_sctns *sctns)
  264. {
  265. int i, r, pages;
  266. bool this_lpar;
  267. void *diag204_buf;
  268. void *diag224_buf = NULL;
  269. struct diag204_x_info_blk_hdr *ti_hdr;
  270. struct diag204_x_part_block *part_block;
  271. struct diag204_x_phys_block *phys_block;
  272. struct lpar_cpu_inf lpar_inf = {};
  273. /* Errors are handled through the validity bits in the response. */
  274. pages = diag204((unsigned long)DIAG204_SUBC_RSI |
  275. (unsigned long)DIAG204_INFO_EXT, 0, NULL);
  276. if (pages <= 0)
  277. return;
  278. diag204_buf = vmalloc(array_size(pages, PAGE_SIZE));
  279. if (!diag204_buf)
  280. return;
  281. r = diag204((unsigned long)DIAG204_SUBC_STIB7 |
  282. (unsigned long)DIAG204_INFO_EXT, pages, diag204_buf);
  283. if (r < 0)
  284. goto out;
  285. diag224_buf = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  286. if (!diag224_buf || diag224(diag224_buf))
  287. goto out;
  288. ti_hdr = diag204_buf;
  289. part_block = diag204_buf + sizeof(*ti_hdr);
  290. for (i = 0; i < ti_hdr->npar; i++) {
  291. /*
  292. * For the calling lpar we also need to get the cpu
  293. * caps and weights. The time information block header
  294. * specifies the offset to the partition block of the
  295. * caller lpar, so we know when we process its data.
  296. */
  297. this_lpar = (void *)part_block - diag204_buf == ti_hdr->this_part;
  298. part_block = lpar_cpu_inf(&lpar_inf, this_lpar, diag224_buf,
  299. part_block);
  300. }
  301. phys_block = (struct diag204_x_phys_block *)part_block;
  302. part_block = diag204_buf + ti_hdr->this_part;
  303. if (part_block->hdr.mtid)
  304. sctns->par.infpflg1 = PAR_MT_EN;
  305. sctns->par.infpval1 |= PAR_GRP_VLD;
  306. sctns->par.infplgcp = scale_cap(lpar_inf.cp.lpar_grp_cap);
  307. sctns->par.infplgif = scale_cap(lpar_inf.ifl.lpar_grp_cap);
  308. memcpy(sctns->par.infplgnm, part_block->hdr.hardware_group_name,
  309. sizeof(sctns->par.infplgnm));
  310. sctns->par.infpscps = lpar_inf.cp.cpu_num_shd;
  311. sctns->par.infpdcps = lpar_inf.cp.cpu_num_ded;
  312. sctns->par.infpsifl = lpar_inf.ifl.cpu_num_shd;
  313. sctns->par.infpdifl = lpar_inf.ifl.cpu_num_ded;
  314. sctns->par.infpval1 |= PAR_PCNT_VLD;
  315. sctns->par.infpabcp = scale_cap(lpar_inf.cp.lpar_cap);
  316. sctns->par.infpabif = scale_cap(lpar_inf.ifl.lpar_cap);
  317. sctns->par.infpval1 |= PAR_ABS_VLD;
  318. /*
  319. * Everything below needs global performance data to be
  320. * meaningful.
  321. */
  322. if (!(ti_hdr->flags & DIAG204_LPAR_PHYS_FLG)) {
  323. sctns->hdr.infhflg1 |= HDR_PERF_UNAV;
  324. goto out;
  325. }
  326. fill_diag_mac(sctns, phys_block, diag224_buf);
  327. if (lpar_inf.cp.lpar_weight) {
  328. sctns->par.infpwbcp = sctns->mac.infmscps * 0x10000 *
  329. lpar_inf.cp.lpar_weight / lpar_inf.cp.all_weight;
  330. }
  331. if (lpar_inf.ifl.lpar_weight) {
  332. sctns->par.infpwbif = sctns->mac.infmsifl * 0x10000 *
  333. lpar_inf.ifl.lpar_weight / lpar_inf.ifl.all_weight;
  334. }
  335. sctns->par.infpval1 |= PAR_WGHT_VLD;
  336. out:
  337. free_page((unsigned long)diag224_buf);
  338. vfree(diag204_buf);
  339. }
  340. static int sthyi(u64 vaddr, u64 *rc)
  341. {
  342. register u64 code asm("0") = 0;
  343. register u64 addr asm("2") = vaddr;
  344. register u64 rcode asm("3");
  345. int cc;
  346. asm volatile(
  347. ".insn rre,0xB2560000,%[code],%[addr]\n"
  348. "ipm %[cc]\n"
  349. "srl %[cc],28\n"
  350. : [cc] "=d" (cc), "=d" (rcode)
  351. : [code] "d" (code), [addr] "a" (addr)
  352. : "memory", "cc");
  353. *rc = rcode;
  354. return cc;
  355. }
  356. static int fill_dst(void *dst, u64 *rc)
  357. {
  358. struct sthyi_sctns *sctns = (struct sthyi_sctns *)dst;
  359. /*
  360. * If the facility is on, we don't want to emulate the instruction.
  361. * We ask the hypervisor to provide the data.
  362. */
  363. if (test_facility(74))
  364. return sthyi((u64)dst, rc);
  365. fill_hdr(sctns);
  366. fill_stsi(sctns);
  367. fill_diag(sctns);
  368. *rc = 0;
  369. return 0;
  370. }
  371. static int sthyi_init_cache(void)
  372. {
  373. if (sthyi_cache.info)
  374. return 0;
  375. sthyi_cache.info = (void *)get_zeroed_page(GFP_KERNEL);
  376. if (!sthyi_cache.info)
  377. return -ENOMEM;
  378. sthyi_cache.end = jiffies - 1; /* expired */
  379. return 0;
  380. }
  381. static int sthyi_update_cache(u64 *rc)
  382. {
  383. int r;
  384. memset(sthyi_cache.info, 0, PAGE_SIZE);
  385. r = fill_dst(sthyi_cache.info, rc);
  386. if (r)
  387. return r;
  388. sthyi_cache.end = jiffies + CACHE_VALID_JIFFIES;
  389. return r;
  390. }
  391. /*
  392. * sthyi_fill - Fill page with data returned by the STHYI instruction
  393. *
  394. * @dst: Pointer to zeroed page
  395. * @rc: Pointer for storing the return code of the instruction
  396. *
  397. * Fills the destination with system information returned by the STHYI
  398. * instruction. The data is generated by emulation or execution of STHYI,
  399. * if available. The return value is the condition code that would be
  400. * returned, the rc parameter is the return code which is passed in
  401. * register R2 + 1.
  402. */
  403. int sthyi_fill(void *dst, u64 *rc)
  404. {
  405. int r;
  406. mutex_lock(&sthyi_mutex);
  407. r = sthyi_init_cache();
  408. if (r)
  409. goto out;
  410. if (time_is_before_jiffies(sthyi_cache.end)) {
  411. /* cache expired */
  412. r = sthyi_update_cache(rc);
  413. if (r)
  414. goto out;
  415. }
  416. *rc = 0;
  417. memcpy(dst, sthyi_cache.info, PAGE_SIZE);
  418. out:
  419. mutex_unlock(&sthyi_mutex);
  420. return r;
  421. }
  422. EXPORT_SYMBOL_GPL(sthyi_fill);
  423. SYSCALL_DEFINE4(s390_sthyi, unsigned long, function_code, void __user *, buffer,
  424. u64 __user *, return_code, unsigned long, flags)
  425. {
  426. u64 sthyi_rc;
  427. void *info;
  428. int r;
  429. if (flags)
  430. return -EINVAL;
  431. if (function_code != STHYI_FC_CP_IFL_CAP)
  432. return -EOPNOTSUPP;
  433. info = (void *)get_zeroed_page(GFP_KERNEL);
  434. if (!info)
  435. return -ENOMEM;
  436. r = sthyi_fill(info, &sthyi_rc);
  437. if (r < 0)
  438. goto out;
  439. if (return_code && put_user(sthyi_rc, return_code)) {
  440. r = -EFAULT;
  441. goto out;
  442. }
  443. if (copy_to_user(buffer, info, PAGE_SIZE))
  444. r = -EFAULT;
  445. out:
  446. free_page((unsigned long)info);
  447. return r;
  448. }