numa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * NUMA support, based on the x86 implementation.
  3. *
  4. * Copyright (C) 2015 Cavium Inc.
  5. * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define pr_fmt(fmt) "NUMA: " fmt
  20. #include <linux/acpi.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/memblock.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <asm/acpi.h>
  26. #include <asm/sections.h>
  27. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  28. EXPORT_SYMBOL(node_data);
  29. nodemask_t numa_nodes_parsed __initdata;
  30. static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
  31. static int numa_distance_cnt;
  32. static u8 *numa_distance;
  33. bool numa_off;
  34. static __init int numa_parse_early_param(char *opt)
  35. {
  36. if (!opt)
  37. return -EINVAL;
  38. if (!strncmp(opt, "off", 3))
  39. numa_off = true;
  40. return 0;
  41. }
  42. early_param("numa", numa_parse_early_param);
  43. cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
  44. EXPORT_SYMBOL(node_to_cpumask_map);
  45. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  46. /*
  47. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  48. */
  49. const struct cpumask *cpumask_of_node(int node)
  50. {
  51. if (node == NUMA_NO_NODE)
  52. return cpu_all_mask;
  53. if (WARN_ON(node < 0 || node >= nr_node_ids))
  54. return cpu_none_mask;
  55. if (WARN_ON(node_to_cpumask_map[node] == NULL))
  56. return cpu_online_mask;
  57. return node_to_cpumask_map[node];
  58. }
  59. EXPORT_SYMBOL(cpumask_of_node);
  60. #endif
  61. static void numa_update_cpu(unsigned int cpu, bool remove)
  62. {
  63. int nid = cpu_to_node(cpu);
  64. if (nid == NUMA_NO_NODE)
  65. return;
  66. if (remove)
  67. cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
  68. else
  69. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  70. }
  71. void numa_add_cpu(unsigned int cpu)
  72. {
  73. numa_update_cpu(cpu, false);
  74. }
  75. void numa_remove_cpu(unsigned int cpu)
  76. {
  77. numa_update_cpu(cpu, true);
  78. }
  79. void numa_clear_node(unsigned int cpu)
  80. {
  81. numa_remove_cpu(cpu);
  82. set_cpu_numa_node(cpu, NUMA_NO_NODE);
  83. }
  84. /*
  85. * Allocate node_to_cpumask_map based on number of available nodes
  86. * Requires node_possible_map to be valid.
  87. *
  88. * Note: cpumask_of_node() is not valid until after this is done.
  89. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  90. */
  91. static void __init setup_node_to_cpumask_map(void)
  92. {
  93. int node;
  94. /* setup nr_node_ids if not done yet */
  95. if (nr_node_ids == MAX_NUMNODES)
  96. setup_nr_node_ids();
  97. /* allocate and clear the mapping */
  98. for (node = 0; node < nr_node_ids; node++) {
  99. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  100. cpumask_clear(node_to_cpumask_map[node]);
  101. }
  102. /* cpumask_of_node() will now work */
  103. pr_debug("Node to cpumask map for %d nodes\n", nr_node_ids);
  104. }
  105. /*
  106. * Set the cpu to node and mem mapping
  107. */
  108. void numa_store_cpu_info(unsigned int cpu)
  109. {
  110. set_cpu_numa_node(cpu, cpu_to_node_map[cpu]);
  111. }
  112. void __init early_map_cpu_to_node(unsigned int cpu, int nid)
  113. {
  114. /* fallback to node 0 */
  115. if (nid < 0 || nid >= MAX_NUMNODES || numa_off)
  116. nid = 0;
  117. cpu_to_node_map[cpu] = nid;
  118. /*
  119. * We should set the numa node of cpu0 as soon as possible, because it
  120. * has already been set up online before. cpu_to_node(0) will soon be
  121. * called.
  122. */
  123. if (!cpu)
  124. set_cpu_numa_node(cpu, nid);
  125. }
  126. #ifdef CONFIG_HAVE_SETUP_PER_CPU_AREA
  127. unsigned long __per_cpu_offset[NR_CPUS] __read_mostly;
  128. EXPORT_SYMBOL(__per_cpu_offset);
  129. static int __init early_cpu_to_node(int cpu)
  130. {
  131. return cpu_to_node_map[cpu];
  132. }
  133. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  134. {
  135. return node_distance(early_cpu_to_node(from), early_cpu_to_node(to));
  136. }
  137. static void * __init pcpu_fc_alloc(unsigned int cpu, size_t size,
  138. size_t align)
  139. {
  140. int nid = early_cpu_to_node(cpu);
  141. return memblock_virt_alloc_try_nid(size, align,
  142. __pa(MAX_DMA_ADDRESS), MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  143. }
  144. static void __init pcpu_fc_free(void *ptr, size_t size)
  145. {
  146. memblock_free_early(__pa(ptr), size);
  147. }
  148. void __init setup_per_cpu_areas(void)
  149. {
  150. unsigned long delta;
  151. unsigned int cpu;
  152. int rc;
  153. /*
  154. * Always reserve area for module percpu variables. That's
  155. * what the legacy allocator did.
  156. */
  157. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  158. PERCPU_DYNAMIC_RESERVE, PAGE_SIZE,
  159. pcpu_cpu_distance,
  160. pcpu_fc_alloc, pcpu_fc_free);
  161. if (rc < 0)
  162. panic("Failed to initialize percpu areas.");
  163. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  164. for_each_possible_cpu(cpu)
  165. __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
  166. }
  167. #endif
  168. /**
  169. * numa_add_memblk - Set node id to memblk
  170. * @nid: NUMA node ID of the new memblk
  171. * @start: Start address of the new memblk
  172. * @end: End address of the new memblk
  173. *
  174. * RETURNS:
  175. * 0 on success, -errno on failure.
  176. */
  177. int __init numa_add_memblk(int nid, u64 start, u64 end)
  178. {
  179. int ret;
  180. ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
  181. if (ret < 0) {
  182. pr_err("memblock [0x%llx - 0x%llx] failed to add on node %d\n",
  183. start, (end - 1), nid);
  184. return ret;
  185. }
  186. node_set(nid, numa_nodes_parsed);
  187. return ret;
  188. }
  189. /**
  190. * Initialize NODE_DATA for a node on the local memory
  191. */
  192. static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
  193. {
  194. const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
  195. u64 nd_pa;
  196. void *nd;
  197. int tnid;
  198. if (start_pfn >= end_pfn)
  199. pr_info("Initmem setup node %d [<memory-less node>]\n", nid);
  200. nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
  201. nd = __va(nd_pa);
  202. /* report and initialize */
  203. pr_info("NODE_DATA [mem %#010Lx-%#010Lx]\n",
  204. nd_pa, nd_pa + nd_size - 1);
  205. tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
  206. if (tnid != nid)
  207. pr_info("NODE_DATA(%d) on node %d\n", nid, tnid);
  208. node_data[nid] = nd;
  209. memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
  210. NODE_DATA(nid)->node_id = nid;
  211. NODE_DATA(nid)->node_start_pfn = start_pfn;
  212. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  213. }
  214. /**
  215. * numa_free_distance
  216. *
  217. * The current table is freed.
  218. */
  219. void __init numa_free_distance(void)
  220. {
  221. size_t size;
  222. if (!numa_distance)
  223. return;
  224. size = numa_distance_cnt * numa_distance_cnt *
  225. sizeof(numa_distance[0]);
  226. memblock_free(__pa(numa_distance), size);
  227. numa_distance_cnt = 0;
  228. numa_distance = NULL;
  229. }
  230. /**
  231. *
  232. * Create a new NUMA distance table.
  233. *
  234. */
  235. static int __init numa_alloc_distance(void)
  236. {
  237. size_t size;
  238. u64 phys;
  239. int i, j;
  240. size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
  241. phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
  242. size, PAGE_SIZE);
  243. if (WARN_ON(!phys))
  244. return -ENOMEM;
  245. memblock_reserve(phys, size);
  246. numa_distance = __va(phys);
  247. numa_distance_cnt = nr_node_ids;
  248. /* fill with the default distances */
  249. for (i = 0; i < numa_distance_cnt; i++)
  250. for (j = 0; j < numa_distance_cnt; j++)
  251. numa_distance[i * numa_distance_cnt + j] = i == j ?
  252. LOCAL_DISTANCE : REMOTE_DISTANCE;
  253. pr_debug("Initialized distance table, cnt=%d\n", numa_distance_cnt);
  254. return 0;
  255. }
  256. /**
  257. * numa_set_distance - Set inter node NUMA distance from node to node.
  258. * @from: the 'from' node to set distance
  259. * @to: the 'to' node to set distance
  260. * @distance: NUMA distance
  261. *
  262. * Set the distance from node @from to @to to @distance.
  263. * If distance table doesn't exist, a warning is printed.
  264. *
  265. * If @from or @to is higher than the highest known node or lower than zero
  266. * or @distance doesn't make sense, the call is ignored.
  267. *
  268. */
  269. void __init numa_set_distance(int from, int to, int distance)
  270. {
  271. if (!numa_distance) {
  272. pr_warn_once("Warning: distance table not allocated yet\n");
  273. return;
  274. }
  275. if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
  276. from < 0 || to < 0) {
  277. pr_warn_once("Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
  278. from, to, distance);
  279. return;
  280. }
  281. if ((u8)distance != distance ||
  282. (from == to && distance != LOCAL_DISTANCE)) {
  283. pr_warn_once("Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  284. from, to, distance);
  285. return;
  286. }
  287. numa_distance[from * numa_distance_cnt + to] = distance;
  288. }
  289. /**
  290. * Return NUMA distance @from to @to
  291. */
  292. int __node_distance(int from, int to)
  293. {
  294. if (from >= numa_distance_cnt || to >= numa_distance_cnt)
  295. return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
  296. return numa_distance[from * numa_distance_cnt + to];
  297. }
  298. EXPORT_SYMBOL(__node_distance);
  299. static int __init numa_register_nodes(void)
  300. {
  301. int nid;
  302. struct memblock_region *mblk;
  303. /* Check that valid nid is set to memblks */
  304. for_each_memblock(memory, mblk)
  305. if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
  306. pr_warn("Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  307. mblk->nid, mblk->base,
  308. mblk->base + mblk->size - 1);
  309. return -EINVAL;
  310. }
  311. /* Finally register nodes. */
  312. for_each_node_mask(nid, numa_nodes_parsed) {
  313. unsigned long start_pfn, end_pfn;
  314. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  315. setup_node_data(nid, start_pfn, end_pfn);
  316. node_set_online(nid);
  317. }
  318. /* Setup online nodes to actual nodes*/
  319. node_possible_map = numa_nodes_parsed;
  320. return 0;
  321. }
  322. static int __init numa_init(int (*init_func)(void))
  323. {
  324. int ret;
  325. nodes_clear(numa_nodes_parsed);
  326. nodes_clear(node_possible_map);
  327. nodes_clear(node_online_map);
  328. numa_free_distance();
  329. ret = numa_alloc_distance();
  330. if (ret < 0)
  331. return ret;
  332. ret = init_func();
  333. if (ret < 0)
  334. return ret;
  335. if (nodes_empty(numa_nodes_parsed)) {
  336. pr_info("No NUMA configuration found\n");
  337. return -EINVAL;
  338. }
  339. ret = numa_register_nodes();
  340. if (ret < 0)
  341. return ret;
  342. setup_node_to_cpumask_map();
  343. return 0;
  344. }
  345. /**
  346. * dummy_numa_init - Fallback dummy NUMA init
  347. *
  348. * Used if there's no underlying NUMA architecture, NUMA initialization
  349. * fails, or NUMA is disabled on the command line.
  350. *
  351. * Must online at least one node (node 0) and add memory blocks that cover all
  352. * allowed memory. It is unlikely that this function fails.
  353. */
  354. static int __init dummy_numa_init(void)
  355. {
  356. int ret;
  357. struct memblock_region *mblk;
  358. if (numa_off)
  359. pr_info("NUMA disabled\n"); /* Forced off on command line. */
  360. pr_info("Faking a node at [mem %#018Lx-%#018Lx]\n",
  361. memblock_start_of_DRAM(), memblock_end_of_DRAM() - 1);
  362. for_each_memblock(memory, mblk) {
  363. ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
  364. if (!ret)
  365. continue;
  366. pr_err("NUMA init failed\n");
  367. return ret;
  368. }
  369. numa_off = true;
  370. return 0;
  371. }
  372. /**
  373. * arm64_numa_init - Initialize NUMA
  374. *
  375. * Try each configured NUMA initialization method until one succeeds. The
  376. * last fallback is dummy single node config encomapssing whole memory.
  377. */
  378. void __init arm64_numa_init(void)
  379. {
  380. if (!numa_off) {
  381. if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
  382. return;
  383. if (acpi_disabled && !numa_init(of_numa_init))
  384. return;
  385. }
  386. numa_init(dummy_numa_init);
  387. }