percpu.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2014-2017 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. *
  18. * Per-CPU variables.
  19. *
  20. * This module supports statically allocated per-CPU variables only. Each
  21. * active processor gets its own block of pages, called percpu area, where
  22. * percpu variables are stored. The offset of a percpu variable is fixed
  23. * and added to the base of the percpu area to obtain the real address of
  24. * the variable.
  25. *
  26. * A statically allocated percpu variable should be defined with the
  27. * __percpu macro, e.g. :
  28. *
  29. * struct s var __percpu;
  30. *
  31. * Obviously, the variable cannot be directly accessed. Instead, percpu
  32. * variables can be accessed with the following accessors :
  33. * - percpu_ptr()
  34. * - percpu_var()
  35. *
  36. * The cpu module is expected to provide the following accessors to access
  37. * percpu variables from the local processor :
  38. * - cpu_local_ptr()
  39. * - cpu_local_var()
  40. *
  41. * These accessors may generate optimized code.
  42. *
  43. * Architecture-specific code must enforce that the percpu section starts
  44. * at 0, thereby making the addresses of percpu variables offsets into the
  45. * percpu area. It must also make sure the _percpu and _percpu_end symbols
  46. * have valid virtual addresses, included between _init (but not part of
  47. * the init section) and _end.
  48. *
  49. * Unless otherwise specified, accessing a percpu variable is not
  50. * interrupt-safe.
  51. */
  52. #ifndef KERN_PERCPU_H
  53. #define KERN_PERCPU_H
  54. #include <assert.h>
  55. #include <stddef.h>
  56. #include <stdint.h>
  57. #include <kern/init.h>
  58. #include <kern/macros.h>
  59. #include <kern/slist_types.h>
  60. #define PERCPU_SECTION .percpu
  61. #define __percpu __section(QUOTE(PERCPU_SECTION))
  62. typedef void (*percpu_op_fn_t)(void);
  63. /*
  64. * Per-CPU operation.
  65. *
  66. * These operations allow initialization code to register functions to be run
  67. * on APs when they're started.
  68. */
  69. struct percpu_op {
  70. struct slist_node node;
  71. percpu_op_fn_t fn;
  72. };
  73. #define PERCPU_OP_INITIALIZER(op_fn) { .fn = op_fn }
  74. /*
  75. * Boundaries of the percpu section.
  76. *
  77. * The addresses of these symbols must be valid, even if the percpu section
  78. * itself has different addresses.
  79. */
  80. extern char _percpu;
  81. extern char _percpu_end;
  82. /*
  83. * Expands to the address of a percpu variable.
  84. */
  85. #define percpu_ptr(var, cpu) \
  86. ((typeof(var) *)(percpu_area(cpu) + ((uintptr_t)(&(var)))))
  87. /*
  88. * Expands to the lvalue of a percpu variable.
  89. */
  90. #define percpu_var(var, cpu) (*(percpu_ptr(var, cpu)))
  91. static inline void *
  92. percpu_area(unsigned int cpu)
  93. {
  94. extern void *percpu_areas[CONFIG_MAX_CPUS];
  95. void *area;
  96. assert(cpu < ARRAY_SIZE(percpu_areas));
  97. area = percpu_areas[cpu];
  98. assert(area != NULL);
  99. return area;
  100. }
  101. /*
  102. * Register a percpu operation to be run on all processors when
  103. * they're started.
  104. *
  105. * The operation is run on the BSP when it's registered. It's run as late as
  106. * possible on APs, normally right before scheduling is enabled.
  107. */
  108. void percpu_register_op(struct percpu_op *op);
  109. /*
  110. * Register a processor.
  111. *
  112. * This function creates a percpu area from kernel virtual memory for the
  113. * given processor. The created area is filled from the content of the
  114. * percpu section.
  115. */
  116. int percpu_add(unsigned int cpu);
  117. /*
  118. * Run registered percpu operations on an AP.
  119. */
  120. void percpu_ap_setup(void);
  121. /*
  122. * This init operation provides :
  123. * - access to percpu variables on processor 0
  124. */
  125. INIT_OP_DECLARE(percpu_bootstrap);
  126. /*
  127. * This init operation provides :
  128. * - percpu operations can be registered
  129. * - new percpu areas can be created
  130. *
  131. * The dependency that provides access to percpu variables on all processors
  132. * is cpu_mp_probe.
  133. *
  134. * TODO Add percpu alias to cpu_mp_probe.
  135. */
  136. INIT_OP_DECLARE(percpu_setup);
  137. #endif /* KERN_PERCPU_H */