numa.c 11 KB

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