spapr_caps.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * QEMU PowerPC pSeries Logical Partition capabilities handling
  3. *
  4. * Copyright (c) 2017 David Gibson, Red Hat Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/error-report.h"
  26. #include "qapi/error.h"
  27. #include "qapi/visitor.h"
  28. #include "system/hw_accel.h"
  29. #include "exec/ram_addr.h"
  30. #include "target/ppc/cpu.h"
  31. #include "target/ppc/mmu-hash64.h"
  32. #include "cpu-models.h"
  33. #include "kvm_ppc.h"
  34. #include "migration/vmstate.h"
  35. #include "system/tcg.h"
  36. #include "system/hostmem.h"
  37. #include "hw/ppc/spapr.h"
  38. typedef struct SpaprCapPossible {
  39. int num; /* size of vals array below */
  40. const char *help; /* help text for vals */
  41. /*
  42. * Note:
  43. * - because of the way compatibility is determined vals MUST be ordered
  44. * such that later options are a superset of all preceding options.
  45. * - the order of vals must be preserved, that is their index is important,
  46. * however vals may be added to the end of the list so long as the above
  47. * point is observed
  48. */
  49. const char *vals[];
  50. } SpaprCapPossible;
  51. typedef struct SpaprCapabilityInfo {
  52. const char *name;
  53. const char *description;
  54. int index;
  55. /* Getter and Setter Function Pointers */
  56. ObjectPropertyAccessor *get;
  57. ObjectPropertyAccessor *set;
  58. const char *type;
  59. /* Possible values if this is a custom string type */
  60. SpaprCapPossible *possible;
  61. /* Make sure the virtual hardware can support this capability */
  62. void (*apply)(SpaprMachineState *spapr, uint8_t val, Error **errp);
  63. void (*cpu_apply)(SpaprMachineState *spapr, PowerPCCPU *cpu,
  64. uint8_t val, Error **errp);
  65. bool (*migrate_needed)(void *opaque);
  66. } SpaprCapabilityInfo;
  67. static void spapr_cap_get_bool(Object *obj, Visitor *v, const char *name,
  68. void *opaque, Error **errp)
  69. {
  70. SpaprCapabilityInfo *cap = opaque;
  71. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  72. bool value = spapr_get_cap(spapr, cap->index) == SPAPR_CAP_ON;
  73. visit_type_bool(v, name, &value, errp);
  74. }
  75. static void spapr_cap_set_bool(Object *obj, Visitor *v, const char *name,
  76. void *opaque, Error **errp)
  77. {
  78. SpaprCapabilityInfo *cap = opaque;
  79. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  80. bool value;
  81. if (!visit_type_bool(v, name, &value, errp)) {
  82. return;
  83. }
  84. spapr->cmd_line_caps[cap->index] = true;
  85. spapr->eff.caps[cap->index] = value ? SPAPR_CAP_ON : SPAPR_CAP_OFF;
  86. }
  87. static void spapr_cap_get_string(Object *obj, Visitor *v, const char *name,
  88. void *opaque, Error **errp)
  89. {
  90. SpaprCapabilityInfo *cap = opaque;
  91. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  92. g_autofree char *val = NULL;
  93. uint8_t value = spapr_get_cap(spapr, cap->index);
  94. if (value >= cap->possible->num) {
  95. error_setg(errp, "Invalid value (%d) for cap-%s", value, cap->name);
  96. return;
  97. }
  98. val = g_strdup(cap->possible->vals[value]);
  99. visit_type_str(v, name, &val, errp);
  100. }
  101. static void spapr_cap_set_string(Object *obj, Visitor *v, const char *name,
  102. void *opaque, Error **errp)
  103. {
  104. SpaprCapabilityInfo *cap = opaque;
  105. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  106. uint8_t i;
  107. g_autofree char *val = NULL;
  108. if (!visit_type_str(v, name, &val, errp)) {
  109. return;
  110. }
  111. if (!strcmp(val, "?")) {
  112. error_setg(errp, "%s", cap->possible->help);
  113. return;
  114. }
  115. for (i = 0; i < cap->possible->num; i++) {
  116. if (!strcasecmp(val, cap->possible->vals[i])) {
  117. spapr->cmd_line_caps[cap->index] = true;
  118. spapr->eff.caps[cap->index] = i;
  119. return;
  120. }
  121. }
  122. error_setg(errp, "Invalid capability mode \"%s\" for cap-%s", val,
  123. cap->name);
  124. }
  125. static void spapr_cap_get_pagesize(Object *obj, Visitor *v, const char *name,
  126. void *opaque, Error **errp)
  127. {
  128. SpaprCapabilityInfo *cap = opaque;
  129. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  130. uint8_t val = spapr_get_cap(spapr, cap->index);
  131. uint64_t pagesize = (1ULL << val);
  132. visit_type_size(v, name, &pagesize, errp);
  133. }
  134. static void spapr_cap_set_pagesize(Object *obj, Visitor *v, const char *name,
  135. void *opaque, Error **errp)
  136. {
  137. SpaprCapabilityInfo *cap = opaque;
  138. SpaprMachineState *spapr = SPAPR_MACHINE(obj);
  139. uint64_t pagesize;
  140. uint8_t val;
  141. if (!visit_type_size(v, name, &pagesize, errp)) {
  142. return;
  143. }
  144. if (!is_power_of_2(pagesize)) {
  145. error_setg(errp, "cap-%s must be a power of 2", cap->name);
  146. return;
  147. }
  148. val = ctz64(pagesize);
  149. spapr->cmd_line_caps[cap->index] = true;
  150. spapr->eff.caps[cap->index] = val;
  151. }
  152. static void cap_htm_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
  153. {
  154. ERRP_GUARD();
  155. if (!val) {
  156. /* TODO: We don't support disabling htm yet */
  157. return;
  158. }
  159. if (tcg_enabled()) {
  160. error_setg(errp, "No Transactional Memory support in TCG");
  161. error_append_hint(errp, "Try appending -machine cap-htm=off\n");
  162. } else if (kvm_enabled() && !kvmppc_has_cap_htm()) {
  163. error_setg(errp,
  164. "KVM implementation does not support Transactional Memory");
  165. error_append_hint(errp, "Try appending -machine cap-htm=off\n");
  166. }
  167. }
  168. static void cap_vsx_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
  169. {
  170. ERRP_GUARD();
  171. CPUPPCState *env = cpu_env(first_cpu);
  172. if (!val) {
  173. /* TODO: We don't support disabling vsx yet */
  174. return;
  175. }
  176. /* Allowable CPUs in spapr_cpu_core.c should already have gotten
  177. * rid of anything that doesn't do VMX */
  178. g_assert(env->insns_flags & PPC_ALTIVEC);
  179. if (!(env->insns_flags2 & PPC2_VSX)) {
  180. error_setg(errp, "VSX support not available");
  181. error_append_hint(errp, "Try appending -machine cap-vsx=off\n");
  182. }
  183. }
  184. static void cap_dfp_apply(SpaprMachineState *spapr, uint8_t val, Error **errp)
  185. {
  186. ERRP_GUARD();
  187. if (!val) {
  188. /* TODO: We don't support disabling dfp yet */
  189. return;
  190. }
  191. if (!(cpu_env(first_cpu)->insns_flags2 & PPC2_DFP)) {
  192. error_setg(errp, "DFP support not available");
  193. error_append_hint(errp, "Try appending -machine cap-dfp=off\n");
  194. }
  195. }
  196. SpaprCapPossible cap_cfpc_possible = {
  197. .num = 3,
  198. .vals = {"broken", "workaround", "fixed"},
  199. .help = "broken - no protection, workaround - workaround available,"
  200. " fixed - fixed in hardware",
  201. };
  202. static void cap_safe_cache_apply(SpaprMachineState *spapr, uint8_t val,
  203. Error **errp)
  204. {
  205. ERRP_GUARD();
  206. uint8_t kvm_val = kvmppc_get_cap_safe_cache();
  207. if (tcg_enabled() && val) {
  208. /* TCG only supports broken, allow other values and print a warning */
  209. warn_report("TCG doesn't support requested feature, cap-cfpc=%s",
  210. cap_cfpc_possible.vals[val]);
  211. } else if (kvm_enabled() && (val > kvm_val)) {
  212. error_setg(errp,
  213. "Requested safe cache capability level not supported by KVM");
  214. error_append_hint(errp, "Try appending -machine cap-cfpc=%s\n",
  215. cap_cfpc_possible.vals[kvm_val]);
  216. }
  217. }
  218. SpaprCapPossible cap_sbbc_possible = {
  219. .num = 3,
  220. .vals = {"broken", "workaround", "fixed"},
  221. .help = "broken - no protection, workaround - workaround available,"
  222. " fixed - fixed in hardware",
  223. };
  224. static void cap_safe_bounds_check_apply(SpaprMachineState *spapr, uint8_t val,
  225. Error **errp)
  226. {
  227. ERRP_GUARD();
  228. uint8_t kvm_val = kvmppc_get_cap_safe_bounds_check();
  229. if (tcg_enabled() && val) {
  230. /* TCG only supports broken, allow other values and print a warning */
  231. warn_report("TCG doesn't support requested feature, cap-sbbc=%s",
  232. cap_sbbc_possible.vals[val]);
  233. } else if (kvm_enabled() && (val > kvm_val)) {
  234. error_setg(errp,
  235. "Requested safe bounds check capability level not supported by KVM");
  236. error_append_hint(errp, "Try appending -machine cap-sbbc=%s\n",
  237. cap_sbbc_possible.vals[kvm_val]);
  238. }
  239. }
  240. SpaprCapPossible cap_ibs_possible = {
  241. .num = 5,
  242. /* Note workaround only maintained for compatibility */
  243. .vals = {"broken", "workaround", "fixed-ibs", "fixed-ccd", "fixed-na"},
  244. .help = "broken - no protection, workaround - count cache flush"
  245. ", fixed-ibs - indirect branch serialisation,"
  246. " fixed-ccd - cache count disabled,"
  247. " fixed-na - fixed in hardware (no longer applicable)",
  248. };
  249. static void cap_safe_indirect_branch_apply(SpaprMachineState *spapr,
  250. uint8_t val, Error **errp)
  251. {
  252. ERRP_GUARD();
  253. uint8_t kvm_val = kvmppc_get_cap_safe_indirect_branch();
  254. if (tcg_enabled() && val) {
  255. /* TCG only supports broken, allow other values and print a warning */
  256. warn_report("TCG doesn't support requested feature, cap-ibs=%s",
  257. cap_ibs_possible.vals[val]);
  258. } else if (kvm_enabled() && (val > kvm_val)) {
  259. error_setg(errp,
  260. "Requested safe indirect branch capability level not supported by KVM");
  261. error_append_hint(errp, "Try appending -machine cap-ibs=%s\n",
  262. cap_ibs_possible.vals[kvm_val]);
  263. }
  264. }
  265. #define VALUE_DESC_TRISTATE " (broken, workaround, fixed)"
  266. bool spapr_check_pagesize(SpaprMachineState *spapr, hwaddr pagesize,
  267. Error **errp)
  268. {
  269. hwaddr maxpagesize = (1ULL << spapr->eff.caps[SPAPR_CAP_HPT_MAXPAGESIZE]);
  270. if (!kvmppc_hpt_needs_host_contiguous_pages()) {
  271. return true;
  272. }
  273. if (maxpagesize > pagesize) {
  274. error_setg(errp,
  275. "Can't support %"HWADDR_PRIu" kiB guest pages with %"
  276. HWADDR_PRIu" kiB host pages with this KVM implementation",
  277. maxpagesize >> 10, pagesize >> 10);
  278. return false;
  279. }
  280. return true;
  281. }
  282. static void cap_hpt_maxpagesize_apply(SpaprMachineState *spapr,
  283. uint8_t val, Error **errp)
  284. {
  285. if (val < 12) {
  286. error_setg(errp, "Require at least 4kiB hpt-max-page-size");
  287. return;
  288. } else if (val < 16) {
  289. warn_report("Many guests require at least 64kiB hpt-max-page-size");
  290. }
  291. spapr_check_pagesize(spapr, qemu_minrampagesize(), errp);
  292. }
  293. static bool cap_hpt_maxpagesize_migrate_needed(void *opaque)
  294. {
  295. return !SPAPR_MACHINE_GET_CLASS(opaque)->pre_4_1_migration;
  296. }
  297. static bool spapr_pagesize_cb(void *opaque, uint32_t seg_pshift,
  298. uint32_t pshift)
  299. {
  300. unsigned maxshift = *((unsigned *)opaque);
  301. assert(pshift >= seg_pshift);
  302. /* Don't allow the guest to use pages bigger than the configured
  303. * maximum size */
  304. if (pshift > maxshift) {
  305. return false;
  306. }
  307. /* For whatever reason, KVM doesn't allow multiple pagesizes
  308. * within a segment, *except* for the case of 16M pages in a 4k or
  309. * 64k segment. Always exclude other cases, so that TCG and KVM
  310. * guests see a consistent environment */
  311. if ((pshift != seg_pshift) && (pshift != 24)) {
  312. return false;
  313. }
  314. return true;
  315. }
  316. static void ppc_hash64_filter_pagesizes(PowerPCCPU *cpu,
  317. bool (*cb)(void *, uint32_t, uint32_t),
  318. void *opaque)
  319. {
  320. PPCHash64Options *opts = cpu->hash64_opts;
  321. int i;
  322. int n = 0;
  323. bool ci_largepage = false;
  324. assert(opts);
  325. n = 0;
  326. for (i = 0; i < ARRAY_SIZE(opts->sps); i++) {
  327. PPCHash64SegmentPageSizes *sps = &opts->sps[i];
  328. int j;
  329. int m = 0;
  330. assert(n <= i);
  331. if (!sps->page_shift) {
  332. break;
  333. }
  334. for (j = 0; j < ARRAY_SIZE(sps->enc); j++) {
  335. PPCHash64PageSize *ps = &sps->enc[j];
  336. assert(m <= j);
  337. if (!ps->page_shift) {
  338. break;
  339. }
  340. if (cb(opaque, sps->page_shift, ps->page_shift)) {
  341. if (ps->page_shift >= 16) {
  342. ci_largepage = true;
  343. }
  344. sps->enc[m++] = *ps;
  345. }
  346. }
  347. /* Clear rest of the row */
  348. for (j = m; j < ARRAY_SIZE(sps->enc); j++) {
  349. memset(&sps->enc[j], 0, sizeof(sps->enc[j]));
  350. }
  351. if (m) {
  352. n++;
  353. }
  354. }
  355. /* Clear the rest of the table */
  356. for (i = n; i < ARRAY_SIZE(opts->sps); i++) {
  357. memset(&opts->sps[i], 0, sizeof(opts->sps[i]));
  358. }
  359. if (!ci_largepage) {
  360. opts->flags &= ~PPC_HASH64_CI_LARGEPAGE;
  361. }
  362. }
  363. static void cap_hpt_maxpagesize_cpu_apply(SpaprMachineState *spapr,
  364. PowerPCCPU *cpu,
  365. uint8_t val, Error **errp)
  366. {
  367. unsigned maxshift = val;
  368. ppc_hash64_filter_pagesizes(cpu, spapr_pagesize_cb, &maxshift);
  369. }
  370. static void cap_nested_kvm_hv_apply(SpaprMachineState *spapr,
  371. uint8_t val, Error **errp)
  372. {
  373. ERRP_GUARD();
  374. PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
  375. CPUPPCState *env = &cpu->env;
  376. if (!val) {
  377. /* capability disabled by default */
  378. return;
  379. }
  380. if (!(env->insns_flags2 & PPC2_ISA300)) {
  381. error_setg(errp, "Nested-HV only supported on POWER9 and later");
  382. error_append_hint(errp, "Try appending -machine cap-nested-hv=off\n");
  383. return;
  384. }
  385. if (kvm_enabled()) {
  386. if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
  387. spapr->max_compat_pvr)) {
  388. error_setg(errp, "Nested-HV only supported on POWER9 and later");
  389. error_append_hint(errp,
  390. "Try appending -machine max-cpu-compat=power9\n");
  391. return;
  392. }
  393. if (!kvmppc_has_cap_nested_kvm_hv()) {
  394. error_setg(errp,
  395. "KVM implementation does not support Nested-HV");
  396. error_append_hint(errp,
  397. "Try appending -machine cap-nested-hv=off\n");
  398. } else if (kvmppc_set_cap_nested_kvm_hv(val) < 0) {
  399. error_setg(errp, "Error enabling cap-nested-hv with KVM");
  400. error_append_hint(errp,
  401. "Try appending -machine cap-nested-hv=off\n");
  402. }
  403. } else if (tcg_enabled()) {
  404. MachineState *ms = MACHINE(spapr);
  405. unsigned int smp_threads = ms->smp.threads;
  406. /*
  407. * Nested-HV vCPU env state to L2, so SMT-shared SPR updates, for
  408. * example, do not necessarily update the correct SPR value on sibling
  409. * threads that are in a different guest/host context.
  410. */
  411. if (smp_threads > 1) {
  412. error_setg(errp, "TCG does not support nested-HV with SMT");
  413. error_append_hint(errp, "Try appending -machine cap-nested-hv=off "
  414. "or use threads=1 with -smp\n");
  415. }
  416. if (spapr_nested_api(spapr) &&
  417. spapr_nested_api(spapr) != NESTED_API_KVM_HV) {
  418. error_setg(errp, "Nested-HV APIs are mutually exclusive");
  419. error_append_hint(errp, "Please use either cap-nested-hv or "
  420. "cap-nested-papr to proceed.\n");
  421. return;
  422. } else {
  423. spapr->nested.api = NESTED_API_KVM_HV;
  424. }
  425. }
  426. }
  427. static void cap_nested_papr_apply(SpaprMachineState *spapr,
  428. uint8_t val, Error **errp)
  429. {
  430. ERRP_GUARD();
  431. PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
  432. CPUPPCState *env = &cpu->env;
  433. if (!val) {
  434. /* capability disabled by default */
  435. return;
  436. }
  437. if (tcg_enabled()) {
  438. if (!(env->insns_flags2 & PPC2_ISA300)) {
  439. error_setg(errp, "Nested-PAPR only supported on POWER9 and later");
  440. error_append_hint(errp,
  441. "Try appending -machine cap-nested-papr=off\n");
  442. return;
  443. }
  444. if (spapr_nested_api(spapr) &&
  445. spapr_nested_api(spapr) != NESTED_API_PAPR) {
  446. error_setg(errp, "Nested-HV APIs are mutually exclusive");
  447. error_append_hint(errp, "Please use either cap-nested-hv or "
  448. "cap-nested-papr to proceed.\n");
  449. return;
  450. } else {
  451. spapr->nested.api = NESTED_API_PAPR;
  452. }
  453. } else if (kvm_enabled()) {
  454. error_setg(errp, "KVM implementation does not support Nested-PAPR");
  455. error_append_hint(errp,
  456. "Try appending -machine cap-nested-papr=off\n");
  457. }
  458. }
  459. static void cap_large_decr_apply(SpaprMachineState *spapr,
  460. uint8_t val, Error **errp)
  461. {
  462. ERRP_GUARD();
  463. PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
  464. PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
  465. if (!val) {
  466. return; /* Disabled by default */
  467. }
  468. if (tcg_enabled()) {
  469. if (!ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0,
  470. spapr->max_compat_pvr)) {
  471. error_setg(errp, "Large decrementer only supported on POWER9");
  472. error_append_hint(errp, "Try -cpu POWER9\n");
  473. return;
  474. }
  475. } else if (kvm_enabled()) {
  476. int kvm_nr_bits = kvmppc_get_cap_large_decr();
  477. if (!kvm_nr_bits) {
  478. error_setg(errp, "No large decrementer support");
  479. error_append_hint(errp,
  480. "Try appending -machine cap-large-decr=off\n");
  481. } else if (pcc->lrg_decr_bits != kvm_nr_bits) {
  482. error_setg(errp,
  483. "KVM large decrementer size (%d) differs to model (%d)",
  484. kvm_nr_bits, pcc->lrg_decr_bits);
  485. error_append_hint(errp,
  486. "Try appending -machine cap-large-decr=off\n");
  487. }
  488. }
  489. }
  490. static void cap_large_decr_cpu_apply(SpaprMachineState *spapr,
  491. PowerPCCPU *cpu,
  492. uint8_t val, Error **errp)
  493. {
  494. ERRP_GUARD();
  495. CPUPPCState *env = &cpu->env;
  496. target_ulong lpcr = env->spr[SPR_LPCR];
  497. if (kvm_enabled()) {
  498. if (kvmppc_enable_cap_large_decr(cpu, val)) {
  499. error_setg(errp, "No large decrementer support");
  500. error_append_hint(errp,
  501. "Try appending -machine cap-large-decr=off\n");
  502. }
  503. }
  504. if (val) {
  505. lpcr |= LPCR_LD;
  506. } else {
  507. lpcr &= ~LPCR_LD;
  508. }
  509. ppc_store_lpcr(cpu, lpcr);
  510. }
  511. static void cap_ccf_assist_apply(SpaprMachineState *spapr, uint8_t val,
  512. Error **errp)
  513. {
  514. ERRP_GUARD();
  515. uint8_t kvm_val = kvmppc_get_cap_count_cache_flush_assist();
  516. if (tcg_enabled() && val) {
  517. /* TCG doesn't implement anything here, but allow with a warning */
  518. warn_report("TCG doesn't support requested feature, cap-ccf-assist=on");
  519. } else if (kvm_enabled() && (val > kvm_val)) {
  520. uint8_t kvm_ibs = kvmppc_get_cap_safe_indirect_branch();
  521. if (kvm_ibs == SPAPR_CAP_FIXED_CCD) {
  522. /*
  523. * If we don't have CCF assist on the host, the assist
  524. * instruction is a harmless no-op. It won't correctly
  525. * implement the cache count flush *but* if we have
  526. * count-cache-disabled in the host, that flush is
  527. * unnecessary. So, specifically allow this case. This
  528. * allows us to have better performance on POWER9 DD2.3,
  529. * while still working on POWER9 DD2.2 and POWER8 host
  530. * cpus.
  531. */
  532. return;
  533. }
  534. error_setg(errp,
  535. "Requested count cache flush assist capability level not supported by KVM");
  536. error_append_hint(errp, "Try appending -machine cap-ccf-assist=off\n");
  537. }
  538. }
  539. static void cap_fwnmi_apply(SpaprMachineState *spapr, uint8_t val,
  540. Error **errp)
  541. {
  542. ERRP_GUARD();
  543. if (!val) {
  544. return; /* Disabled by default */
  545. }
  546. if (kvm_enabled()) {
  547. if (!kvmppc_get_fwnmi()) {
  548. error_setg(errp,
  549. "Firmware Assisted Non-Maskable Interrupts(FWNMI) not supported by KVM.");
  550. error_append_hint(errp, "Try appending -machine cap-fwnmi=off\n");
  551. }
  552. }
  553. }
  554. static void cap_rpt_invalidate_apply(SpaprMachineState *spapr,
  555. uint8_t val, Error **errp)
  556. {
  557. ERRP_GUARD();
  558. if (!val) {
  559. /* capability disabled by default */
  560. return;
  561. }
  562. if (tcg_enabled()) {
  563. error_setg(errp, "No H_RPT_INVALIDATE support in TCG");
  564. error_append_hint(errp,
  565. "Try appending -machine cap-rpt-invalidate=off\n");
  566. } else if (kvm_enabled()) {
  567. if (!kvmppc_has_cap_mmu_radix()) {
  568. error_setg(errp, "H_RPT_INVALIDATE only supported on Radix");
  569. return;
  570. }
  571. if (!kvmppc_has_cap_rpt_invalidate()) {
  572. error_setg(errp,
  573. "KVM implementation does not support H_RPT_INVALIDATE");
  574. error_append_hint(errp,
  575. "Try appending -machine cap-rpt-invalidate=off\n");
  576. } else {
  577. kvmppc_enable_h_rpt_invalidate();
  578. }
  579. }
  580. }
  581. static void cap_ail_mode_3_apply(SpaprMachineState *spapr,
  582. uint8_t val, Error **errp)
  583. {
  584. ERRP_GUARD();
  585. PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
  586. PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
  587. if (!val) {
  588. return;
  589. }
  590. if (tcg_enabled()) {
  591. /* AIL-3 is only supported on POWER8 and above CPUs. */
  592. if (!(pcc->insns_flags2 & PPC2_ISA207S)) {
  593. error_setg(errp, "TCG only supports cap-ail-mode-3 on POWER8 and later CPUs");
  594. error_append_hint(errp, "Try appending -machine cap-ail-mode-3=off\n");
  595. return;
  596. }
  597. } else if (kvm_enabled()) {
  598. if (!kvmppc_supports_ail_3()) {
  599. error_setg(errp, "KVM implementation does not support cap-ail-mode-3");
  600. error_append_hint(errp, "Try appending -machine cap-ail-mode-3=off\n");
  601. return;
  602. }
  603. }
  604. }
  605. static void cap_dawr1_apply(SpaprMachineState *spapr, uint8_t val,
  606. Error **errp)
  607. {
  608. ERRP_GUARD();
  609. if (!val) {
  610. return; /* Disable by default */
  611. }
  612. if (!ppc_type_check_compat(MACHINE(spapr)->cpu_type,
  613. CPU_POWERPC_LOGICAL_3_10, 0,
  614. spapr->max_compat_pvr)) {
  615. error_setg(errp, "DAWR1 supported only on POWER10 and later CPUs");
  616. error_append_hint(errp, "Try appending -machine cap-dawr1=off\n");
  617. return;
  618. }
  619. if (kvm_enabled()) {
  620. if (!kvmppc_has_cap_dawr1()) {
  621. error_setg(errp, "DAWR1 not supported by KVM.");
  622. error_append_hint(errp, "Try appending -machine cap-dawr1=off");
  623. } else if (kvmppc_set_cap_dawr1(val) < 0) {
  624. error_setg(errp, "Error enabling cap-dawr1 with KVM.");
  625. error_append_hint(errp, "Try appending -machine cap-dawr1=off");
  626. }
  627. }
  628. }
  629. SpaprCapabilityInfo capability_table[SPAPR_CAP_NUM] = {
  630. [SPAPR_CAP_HTM] = {
  631. .name = "htm",
  632. .description = "Allow Hardware Transactional Memory (HTM)",
  633. .index = SPAPR_CAP_HTM,
  634. .get = spapr_cap_get_bool,
  635. .set = spapr_cap_set_bool,
  636. .type = "bool",
  637. .apply = cap_htm_apply,
  638. },
  639. [SPAPR_CAP_VSX] = {
  640. .name = "vsx",
  641. .description = "Allow Vector Scalar Extensions (VSX)",
  642. .index = SPAPR_CAP_VSX,
  643. .get = spapr_cap_get_bool,
  644. .set = spapr_cap_set_bool,
  645. .type = "bool",
  646. .apply = cap_vsx_apply,
  647. },
  648. [SPAPR_CAP_DFP] = {
  649. .name = "dfp",
  650. .description = "Allow Decimal Floating Point (DFP)",
  651. .index = SPAPR_CAP_DFP,
  652. .get = spapr_cap_get_bool,
  653. .set = spapr_cap_set_bool,
  654. .type = "bool",
  655. .apply = cap_dfp_apply,
  656. },
  657. [SPAPR_CAP_CFPC] = {
  658. .name = "cfpc",
  659. .description = "Cache Flush on Privilege Change" VALUE_DESC_TRISTATE,
  660. .index = SPAPR_CAP_CFPC,
  661. .get = spapr_cap_get_string,
  662. .set = spapr_cap_set_string,
  663. .type = "string",
  664. .possible = &cap_cfpc_possible,
  665. .apply = cap_safe_cache_apply,
  666. },
  667. [SPAPR_CAP_SBBC] = {
  668. .name = "sbbc",
  669. .description = "Speculation Barrier Bounds Checking" VALUE_DESC_TRISTATE,
  670. .index = SPAPR_CAP_SBBC,
  671. .get = spapr_cap_get_string,
  672. .set = spapr_cap_set_string,
  673. .type = "string",
  674. .possible = &cap_sbbc_possible,
  675. .apply = cap_safe_bounds_check_apply,
  676. },
  677. [SPAPR_CAP_IBS] = {
  678. .name = "ibs",
  679. .description =
  680. "Indirect Branch Speculation (broken, workaround, fixed-ibs,"
  681. "fixed-ccd, fixed-na)",
  682. .index = SPAPR_CAP_IBS,
  683. .get = spapr_cap_get_string,
  684. .set = spapr_cap_set_string,
  685. .type = "string",
  686. .possible = &cap_ibs_possible,
  687. .apply = cap_safe_indirect_branch_apply,
  688. },
  689. [SPAPR_CAP_HPT_MAXPAGESIZE] = {
  690. .name = "hpt-max-page-size",
  691. .description = "Maximum page size for Hash Page Table guests",
  692. .index = SPAPR_CAP_HPT_MAXPAGESIZE,
  693. .get = spapr_cap_get_pagesize,
  694. .set = spapr_cap_set_pagesize,
  695. .type = "int",
  696. .apply = cap_hpt_maxpagesize_apply,
  697. .cpu_apply = cap_hpt_maxpagesize_cpu_apply,
  698. .migrate_needed = cap_hpt_maxpagesize_migrate_needed,
  699. },
  700. [SPAPR_CAP_NESTED_KVM_HV] = {
  701. .name = "nested-hv",
  702. .description = "Allow Nested KVM-HV",
  703. .index = SPAPR_CAP_NESTED_KVM_HV,
  704. .get = spapr_cap_get_bool,
  705. .set = spapr_cap_set_bool,
  706. .type = "bool",
  707. .apply = cap_nested_kvm_hv_apply,
  708. },
  709. [SPAPR_CAP_NESTED_PAPR] = {
  710. .name = "nested-papr",
  711. .description = "Allow Nested HV (PAPR API)",
  712. .index = SPAPR_CAP_NESTED_PAPR,
  713. .get = spapr_cap_get_bool,
  714. .set = spapr_cap_set_bool,
  715. .type = "bool",
  716. .apply = cap_nested_papr_apply,
  717. },
  718. [SPAPR_CAP_LARGE_DECREMENTER] = {
  719. .name = "large-decr",
  720. .description = "Allow Large Decrementer",
  721. .index = SPAPR_CAP_LARGE_DECREMENTER,
  722. .get = spapr_cap_get_bool,
  723. .set = spapr_cap_set_bool,
  724. .type = "bool",
  725. .apply = cap_large_decr_apply,
  726. .cpu_apply = cap_large_decr_cpu_apply,
  727. },
  728. [SPAPR_CAP_CCF_ASSIST] = {
  729. .name = "ccf-assist",
  730. .description = "Count Cache Flush Assist via HW Instruction",
  731. .index = SPAPR_CAP_CCF_ASSIST,
  732. .get = spapr_cap_get_bool,
  733. .set = spapr_cap_set_bool,
  734. .type = "bool",
  735. .apply = cap_ccf_assist_apply,
  736. },
  737. [SPAPR_CAP_FWNMI] = {
  738. .name = "fwnmi",
  739. .description = "Implements PAPR FWNMI option",
  740. .index = SPAPR_CAP_FWNMI,
  741. .get = spapr_cap_get_bool,
  742. .set = spapr_cap_set_bool,
  743. .type = "bool",
  744. .apply = cap_fwnmi_apply,
  745. },
  746. [SPAPR_CAP_RPT_INVALIDATE] = {
  747. .name = "rpt-invalidate",
  748. .description = "Allow H_RPT_INVALIDATE",
  749. .index = SPAPR_CAP_RPT_INVALIDATE,
  750. .get = spapr_cap_get_bool,
  751. .set = spapr_cap_set_bool,
  752. .type = "bool",
  753. .apply = cap_rpt_invalidate_apply,
  754. },
  755. [SPAPR_CAP_AIL_MODE_3] = {
  756. .name = "ail-mode-3",
  757. .description = "Alternate Interrupt Location (AIL) mode 3 support",
  758. .index = SPAPR_CAP_AIL_MODE_3,
  759. .get = spapr_cap_get_bool,
  760. .set = spapr_cap_set_bool,
  761. .type = "bool",
  762. .apply = cap_ail_mode_3_apply,
  763. },
  764. [SPAPR_CAP_DAWR1] = {
  765. .name = "dawr1",
  766. .description = "Allow 2nd Data Address Watchpoint Register (DAWR1)",
  767. .index = SPAPR_CAP_DAWR1,
  768. .get = spapr_cap_get_bool,
  769. .set = spapr_cap_set_bool,
  770. .type = "bool",
  771. .apply = cap_dawr1_apply,
  772. },
  773. };
  774. static SpaprCapabilities default_caps_with_cpu(SpaprMachineState *spapr,
  775. const char *cputype)
  776. {
  777. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  778. SpaprCapabilities caps;
  779. caps = smc->default_caps;
  780. if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_10,
  781. 0, spapr->max_compat_pvr)) {
  782. caps.caps[SPAPR_CAP_DAWR1] = SPAPR_CAP_OFF;
  783. }
  784. if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_3_00,
  785. 0, spapr->max_compat_pvr)) {
  786. caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_OFF;
  787. }
  788. if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_07,
  789. 0, spapr->max_compat_pvr)) {
  790. caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
  791. caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_BROKEN;
  792. caps.caps[SPAPR_CAP_AIL_MODE_3] = SPAPR_CAP_OFF;
  793. }
  794. if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_06_PLUS,
  795. 0, spapr->max_compat_pvr)) {
  796. caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_BROKEN;
  797. }
  798. if (!ppc_type_check_compat(cputype, CPU_POWERPC_LOGICAL_2_06,
  799. 0, spapr->max_compat_pvr)) {
  800. caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_OFF;
  801. caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_OFF;
  802. caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
  803. }
  804. /* This is for pseries-2.12 and older */
  805. if (smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] == 0) {
  806. uint8_t mps;
  807. if (kvmppc_hpt_needs_host_contiguous_pages()) {
  808. mps = ctz64(qemu_minrampagesize());
  809. } else {
  810. mps = 34; /* allow everything up to 16GiB, i.e. everything */
  811. }
  812. caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = mps;
  813. }
  814. return caps;
  815. }
  816. int spapr_caps_pre_load(void *opaque)
  817. {
  818. SpaprMachineState *spapr = opaque;
  819. /* Set to default so we can tell if this came in with the migration */
  820. spapr->mig = spapr->def;
  821. return 0;
  822. }
  823. int spapr_caps_pre_save(void *opaque)
  824. {
  825. SpaprMachineState *spapr = opaque;
  826. spapr->mig = spapr->eff;
  827. return 0;
  828. }
  829. /* This has to be called from the top-level spapr post_load, not the
  830. * caps specific one. Otherwise it wouldn't be called when the source
  831. * caps are all defaults, which could still conflict with overridden
  832. * caps on the destination */
  833. int spapr_caps_post_migration(SpaprMachineState *spapr)
  834. {
  835. int i;
  836. bool ok = true;
  837. SpaprCapabilities dstcaps = spapr->eff;
  838. SpaprCapabilities srccaps;
  839. srccaps = default_caps_with_cpu(spapr, MACHINE(spapr)->cpu_type);
  840. for (i = 0; i < SPAPR_CAP_NUM; i++) {
  841. /* If not default value then assume came in with the migration */
  842. if (spapr->mig.caps[i] != spapr->def.caps[i]) {
  843. srccaps.caps[i] = spapr->mig.caps[i];
  844. }
  845. }
  846. for (i = 0; i < SPAPR_CAP_NUM; i++) {
  847. SpaprCapabilityInfo *info = &capability_table[i];
  848. if (srccaps.caps[i] > dstcaps.caps[i]) {
  849. error_report("cap-%s higher level (%d) in incoming stream than on destination (%d)",
  850. info->name, srccaps.caps[i], dstcaps.caps[i]);
  851. ok = false;
  852. }
  853. if (srccaps.caps[i] < dstcaps.caps[i]) {
  854. warn_report("cap-%s lower level (%d) in incoming stream than on destination (%d)",
  855. info->name, srccaps.caps[i], dstcaps.caps[i]);
  856. }
  857. }
  858. return ok ? 0 : -EINVAL;
  859. }
  860. /* Used to generate the migration field and needed function for a spapr cap */
  861. #define SPAPR_CAP_MIG_STATE(sname, cap) \
  862. static bool spapr_cap_##sname##_needed(void *opaque) \
  863. { \
  864. SpaprMachineState *spapr = opaque; \
  865. bool (*needed)(void *opaque) = \
  866. capability_table[cap].migrate_needed; \
  867. \
  868. return needed ? needed(opaque) : true && \
  869. spapr->cmd_line_caps[cap] && \
  870. (spapr->eff.caps[cap] != \
  871. spapr->def.caps[cap]); \
  872. } \
  873. \
  874. const VMStateDescription vmstate_spapr_cap_##sname = { \
  875. .name = "spapr/cap/" #sname, \
  876. .version_id = 1, \
  877. .minimum_version_id = 1, \
  878. .needed = spapr_cap_##sname##_needed, \
  879. .fields = (const VMStateField[]) { \
  880. VMSTATE_UINT8(mig.caps[cap], \
  881. SpaprMachineState), \
  882. VMSTATE_END_OF_LIST() \
  883. }, \
  884. }
  885. SPAPR_CAP_MIG_STATE(htm, SPAPR_CAP_HTM);
  886. SPAPR_CAP_MIG_STATE(vsx, SPAPR_CAP_VSX);
  887. SPAPR_CAP_MIG_STATE(dfp, SPAPR_CAP_DFP);
  888. SPAPR_CAP_MIG_STATE(cfpc, SPAPR_CAP_CFPC);
  889. SPAPR_CAP_MIG_STATE(sbbc, SPAPR_CAP_SBBC);
  890. SPAPR_CAP_MIG_STATE(ibs, SPAPR_CAP_IBS);
  891. SPAPR_CAP_MIG_STATE(hpt_maxpagesize, SPAPR_CAP_HPT_MAXPAGESIZE);
  892. SPAPR_CAP_MIG_STATE(nested_kvm_hv, SPAPR_CAP_NESTED_KVM_HV);
  893. SPAPR_CAP_MIG_STATE(nested_papr, SPAPR_CAP_NESTED_PAPR);
  894. SPAPR_CAP_MIG_STATE(large_decr, SPAPR_CAP_LARGE_DECREMENTER);
  895. SPAPR_CAP_MIG_STATE(ccf_assist, SPAPR_CAP_CCF_ASSIST);
  896. SPAPR_CAP_MIG_STATE(fwnmi, SPAPR_CAP_FWNMI);
  897. SPAPR_CAP_MIG_STATE(rpt_invalidate, SPAPR_CAP_RPT_INVALIDATE);
  898. SPAPR_CAP_MIG_STATE(ail_mode_3, SPAPR_CAP_AIL_MODE_3);
  899. SPAPR_CAP_MIG_STATE(dawr1, SPAPR_CAP_DAWR1);
  900. void spapr_caps_init(SpaprMachineState *spapr)
  901. {
  902. SpaprCapabilities default_caps;
  903. int i;
  904. /* Compute the actual set of caps we should run with */
  905. default_caps = default_caps_with_cpu(spapr, MACHINE(spapr)->cpu_type);
  906. for (i = 0; i < SPAPR_CAP_NUM; i++) {
  907. /* Store the defaults */
  908. spapr->def.caps[i] = default_caps.caps[i];
  909. /* If not set on the command line then apply the default value */
  910. if (!spapr->cmd_line_caps[i]) {
  911. spapr->eff.caps[i] = default_caps.caps[i];
  912. }
  913. }
  914. }
  915. void spapr_caps_apply(SpaprMachineState *spapr)
  916. {
  917. int i;
  918. for (i = 0; i < SPAPR_CAP_NUM; i++) {
  919. SpaprCapabilityInfo *info = &capability_table[i];
  920. /*
  921. * If the apply function can't set the desired level and thinks it's
  922. * fatal, it should cause that.
  923. */
  924. info->apply(spapr, spapr->eff.caps[i], &error_fatal);
  925. }
  926. }
  927. void spapr_caps_cpu_apply(SpaprMachineState *spapr, PowerPCCPU *cpu)
  928. {
  929. int i;
  930. for (i = 0; i < SPAPR_CAP_NUM; i++) {
  931. SpaprCapabilityInfo *info = &capability_table[i];
  932. /*
  933. * If the apply function can't set the desired level and thinks it's
  934. * fatal, it should cause that.
  935. */
  936. if (info->cpu_apply) {
  937. info->cpu_apply(spapr, cpu, spapr->eff.caps[i], &error_fatal);
  938. }
  939. }
  940. }
  941. void spapr_caps_add_properties(SpaprMachineClass *smc)
  942. {
  943. ObjectClass *klass = OBJECT_CLASS(smc);
  944. int i;
  945. for (i = 0; i < ARRAY_SIZE(capability_table); i++) {
  946. SpaprCapabilityInfo *cap = &capability_table[i];
  947. g_autofree char *name = g_strdup_printf("cap-%s", cap->name);
  948. g_autofree char *desc = g_strdup(cap->description);
  949. object_class_property_add(klass, name, cap->type,
  950. cap->get, cap->set,
  951. NULL, cap);
  952. object_class_property_set_description(klass, name, desc);
  953. }
  954. }