cpumap.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. kmem_cache_init (&cpumap_cache, "cpumap", sizeof (struct cpumap), 0, NULL, 0);
  31. cpumap_zero (&cpumap_active_cpus);
  32. for (unsigned i = 0; i < cpu_count (); i++)
  33. cpumap_set (&cpumap_active_cpus, i);
  34. return (0);
  35. }
  36. INIT_OP_DEFINE (cpumap_setup,
  37. INIT_OP_DEP (kmem_setup, true),
  38. INIT_OP_DEP (cpu_mp_probe, true));
  39. const struct cpumap*
  40. cpumap_all (void)
  41. {
  42. return (&cpumap_active_cpus);
  43. }
  44. int
  45. cpumap_create (struct cpumap **cpumapp)
  46. {
  47. struct cpumap *cpumap = kmem_cache_alloc (&cpumap_cache);
  48. if (! cpumap)
  49. return (ENOMEM);
  50. *cpumapp = cpumap;
  51. return (0);
  52. }
  53. void
  54. cpumap_destroy (struct cpumap *cpumap)
  55. {
  56. kmem_cache_free (&cpumap_cache, cpumap);
  57. }
  58. int
  59. cpumap_check (const struct cpumap *cpumap)
  60. {
  61. int index = bitmap_find_first (cpumap->cpus, cpu_count ());
  62. return (index < 0 ? EINVAL : 0);
  63. }