accel.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * QEMU System Emulator, accelerator interfaces
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2014 Red Hat Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "sysemu/accel.h"
  27. #include "hw/boards.h"
  28. #include "sysemu/arch_init.h"
  29. #include "sysemu/sysemu.h"
  30. #include "sysemu/kvm.h"
  31. #include "sysemu/qtest.h"
  32. #include "hw/xen/xen.h"
  33. #include "qom/object.h"
  34. #include "qemu/error-report.h"
  35. #include "qemu/option.h"
  36. #include "qapi/error.h"
  37. static const TypeInfo accel_type = {
  38. .name = TYPE_ACCEL,
  39. .parent = TYPE_OBJECT,
  40. .class_size = sizeof(AccelClass),
  41. .instance_size = sizeof(AccelState),
  42. };
  43. /* Lookup AccelClass from opt_name. Returns NULL if not found */
  44. static AccelClass *accel_find(const char *opt_name)
  45. {
  46. char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
  47. AccelClass *ac = ACCEL_CLASS(object_class_by_name(class_name));
  48. g_free(class_name);
  49. return ac;
  50. }
  51. static int accel_init_machine(AccelClass *acc, MachineState *ms)
  52. {
  53. ObjectClass *oc = OBJECT_CLASS(acc);
  54. const char *cname = object_class_get_name(oc);
  55. AccelState *accel = ACCEL(object_new(cname));
  56. int ret;
  57. ms->accelerator = accel;
  58. *(acc->allowed) = true;
  59. ret = acc->init_machine(ms);
  60. if (ret < 0) {
  61. ms->accelerator = NULL;
  62. *(acc->allowed) = false;
  63. object_unref(OBJECT(accel));
  64. } else {
  65. object_set_accelerator_compat_props(acc->compat_props);
  66. }
  67. return ret;
  68. }
  69. void configure_accelerator(MachineState *ms, const char *progname)
  70. {
  71. const char *accel;
  72. char **accel_list, **tmp;
  73. int ret;
  74. bool accel_initialised = false;
  75. bool init_failed = false;
  76. AccelClass *acc = NULL;
  77. accel = qemu_opt_get(qemu_get_machine_opts(), "accel");
  78. if (accel == NULL) {
  79. /* Select the default accelerator */
  80. int pnlen = strlen(progname);
  81. if (pnlen >= 3 && g_str_equal(&progname[pnlen - 3], "kvm")) {
  82. /* If the program name ends with "kvm", we prefer KVM */
  83. accel = "kvm:tcg";
  84. } else {
  85. #if defined(CONFIG_TCG)
  86. accel = "tcg";
  87. #elif defined(CONFIG_KVM)
  88. accel = "kvm";
  89. #else
  90. error_report("No accelerator selected and"
  91. " no default accelerator available");
  92. exit(1);
  93. #endif
  94. }
  95. }
  96. accel_list = g_strsplit(accel, ":", 0);
  97. for (tmp = accel_list; !accel_initialised && tmp && *tmp; tmp++) {
  98. acc = accel_find(*tmp);
  99. if (!acc) {
  100. continue;
  101. }
  102. ret = accel_init_machine(acc, ms);
  103. if (ret < 0) {
  104. init_failed = true;
  105. error_report("failed to initialize %s: %s",
  106. acc->name, strerror(-ret));
  107. } else {
  108. accel_initialised = true;
  109. }
  110. }
  111. g_strfreev(accel_list);
  112. if (!accel_initialised) {
  113. if (!init_failed) {
  114. error_report("-machine accel=%s: No accelerator found", accel);
  115. }
  116. exit(1);
  117. }
  118. if (init_failed) {
  119. error_report("Back to %s accelerator", acc->name);
  120. }
  121. }
  122. void accel_setup_post(MachineState *ms)
  123. {
  124. AccelState *accel = ms->accelerator;
  125. AccelClass *acc = ACCEL_GET_CLASS(accel);
  126. if (acc->setup_post) {
  127. acc->setup_post(ms, accel);
  128. }
  129. }
  130. static void register_accel_types(void)
  131. {
  132. type_register_static(&accel_type);
  133. }
  134. type_init(register_accel_types);