cpumap.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2013-2014 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <errno.h>
  18. #include <stddef.h>
  19. #include <kern/bitmap.h>
  20. #include <kern/cpumap.h>
  21. #include <kern/init.h>
  22. #include <kern/kmem.h>
  23. #include <kern/macros.h>
  24. #include <machine/cpu.h>
  25. static struct cpumap cpumap_active_cpus __read_mostly = { { 1 } };
  26. static struct kmem_cache cpumap_cache;
  27. static int __init
  28. cpumap_setup(void)
  29. {
  30. unsigned int i, nr_cpus;
  31. kmem_cache_init(&cpumap_cache, "cpumap", sizeof(struct cpumap), 0, NULL, 0);
  32. cpumap_zero(&cpumap_active_cpus);
  33. nr_cpus = cpu_count();
  34. for (i = 0; i < nr_cpus; i++) {
  35. cpumap_set(&cpumap_active_cpus, i);
  36. }
  37. return 0;
  38. }
  39. INIT_OP_DEFINE(cpumap_setup,
  40. INIT_OP_DEP(kmem_setup, true),
  41. INIT_OP_DEP(cpu_mp_probe, true));
  42. const struct cpumap *
  43. cpumap_all(void)
  44. {
  45. return &cpumap_active_cpus;
  46. }
  47. int
  48. cpumap_create(struct cpumap **cpumapp)
  49. {
  50. struct cpumap *cpumap;
  51. cpumap = kmem_cache_alloc(&cpumap_cache);
  52. if (cpumap == NULL) {
  53. return ENOMEM;
  54. }
  55. *cpumapp = cpumap;
  56. return 0;
  57. }
  58. void
  59. cpumap_destroy(struct cpumap *cpumap)
  60. {
  61. kmem_cache_free(&cpumap_cache, cpumap);
  62. }
  63. int
  64. cpumap_check(const struct cpumap *cpumap)
  65. {
  66. int index;
  67. index = bitmap_find_first(cpumap->cpus, cpu_count());
  68. if (index == -1) {
  69. return EINVAL;
  70. }
  71. return 0;
  72. }