percpu.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  71. struct slist_node node;
  72. percpu_op_fn_t fn;
  73. };
  74. #define PERCPU_OP_INITIALIZER(op_fn) { .fn = op_fn }
  75. /*
  76. * Boundaries of the percpu section.
  77. *
  78. * The addresses of these symbols must be valid, even if the percpu section
  79. * itself has different addresses.
  80. */
  81. extern char _percpu;
  82. extern char _percpu_end;
  83. // Expands to the address of a percpu variable.
  84. #define percpu_ptr(var, cpu) \
  85. ((typeof (var) *)(percpu_area (cpu) + ((uintptr_t)(&(var)))))
  86. // Expands to the lvalue of a percpu variable.
  87. #define percpu_var(var, cpu) (*percpu_ptr(var, cpu))
  88. static inline void*
  89. percpu_area (uint32_t cpu)
  90. {
  91. extern void *percpu_areas[CONFIG_MAX_CPUS];
  92. assert (cpu < ARRAY_SIZE (percpu_areas));
  93. void *area = percpu_areas[cpu];
  94. assert (area);
  95. return (area);
  96. }
  97. /*
  98. * Register a percpu operation to be run on all processors when
  99. * they're started.
  100. *
  101. * The operation is run on the BSP when it's registered. It's run as late as
  102. * possible on APs, normally right before scheduling is enabled.
  103. */
  104. void percpu_register_op (struct percpu_op *op);
  105. /*
  106. * Register a processor.
  107. *
  108. * This function creates a percpu area from kernel virtual memory for the
  109. * given processor. The created area is filled from the content of the
  110. * percpu section.
  111. */
  112. int percpu_add (uint32_t cpu);
  113. /*
  114. * Run registered percpu operations on an AP.
  115. */
  116. void percpu_ap_setup (void);
  117. /*
  118. * This init operation provides :
  119. * - access to percpu variables on processor 0
  120. */
  121. INIT_OP_DECLARE (percpu_bootstrap);
  122. /*
  123. * This init operation provides :
  124. * - percpu operations can be registered
  125. * - new percpu areas can be created
  126. *
  127. * The dependency that provides access to percpu variables on all processors
  128. * is cpu_mp_probe.
  129. *
  130. * TODO Add percpu alias to cpu_mp_probe.
  131. */
  132. INIT_OP_DECLARE (percpu_setup);
  133. #endif