core.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * CPU core abstract device
  3. *
  4. * Copyright (C) 2016 Bharata B Rao <bharata@linux.vnet.ibm.com>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/boards.h"
  11. #include "hw/cpu/core.h"
  12. #include "qapi/error.h"
  13. #include "qapi/visitor.h"
  14. static void core_prop_get_core_id(Object *obj, Visitor *v, const char *name,
  15. void *opaque, Error **errp)
  16. {
  17. CPUCore *core = CPU_CORE(obj);
  18. int64_t value = core->core_id;
  19. visit_type_int(v, name, &value, errp);
  20. }
  21. static void core_prop_set_core_id(Object *obj, Visitor *v, const char *name,
  22. void *opaque, Error **errp)
  23. {
  24. CPUCore *core = CPU_CORE(obj);
  25. int64_t value;
  26. if (!visit_type_int(v, name, &value, errp)) {
  27. return;
  28. }
  29. if (value < 0) {
  30. error_setg(errp, "Invalid core id %"PRId64, value);
  31. return;
  32. }
  33. core->core_id = value;
  34. }
  35. static void core_prop_get_nr_threads(Object *obj, Visitor *v, const char *name,
  36. void *opaque, Error **errp)
  37. {
  38. CPUCore *core = CPU_CORE(obj);
  39. int64_t value = core->nr_threads;
  40. visit_type_int(v, name, &value, errp);
  41. }
  42. static void core_prop_set_nr_threads(Object *obj, Visitor *v, const char *name,
  43. void *opaque, Error **errp)
  44. {
  45. CPUCore *core = CPU_CORE(obj);
  46. int64_t value;
  47. if (!visit_type_int(v, name, &value, errp)) {
  48. return;
  49. }
  50. core->nr_threads = value;
  51. }
  52. static void cpu_core_instance_init(Object *obj)
  53. {
  54. CPUCore *core = CPU_CORE(obj);
  55. /*
  56. * Only '-device something-cpu-core,help' can get us there before
  57. * the machine has been created. We don't care to set nr_threads
  58. * in this case since it isn't used afterwards.
  59. */
  60. if (current_machine) {
  61. core->nr_threads = current_machine->smp.threads;
  62. }
  63. }
  64. static void cpu_core_class_init(ObjectClass *oc, void *data)
  65. {
  66. DeviceClass *dc = DEVICE_CLASS(oc);
  67. set_bit(DEVICE_CATEGORY_CPU, dc->categories);
  68. object_class_property_add(oc, "core-id", "int", core_prop_get_core_id,
  69. core_prop_set_core_id, NULL, NULL);
  70. object_class_property_add(oc, "nr-threads", "int", core_prop_get_nr_threads,
  71. core_prop_set_nr_threads, NULL, NULL);
  72. }
  73. static const TypeInfo cpu_core_type_info = {
  74. .name = TYPE_CPU_CORE,
  75. .parent = TYPE_DEVICE,
  76. .abstract = true,
  77. .class_init = cpu_core_class_init,
  78. .instance_size = sizeof(CPUCore),
  79. .instance_init = cpu_core_instance_init,
  80. };
  81. static void cpu_core_register_types(void)
  82. {
  83. type_register_static(&cpu_core_type_info);
  84. }
  85. type_init(cpu_core_register_types)