numa.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #include <linux/acpi.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/memblock.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <asm/acpi.h>
  25. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  26. EXPORT_SYMBOL(node_data);
  27. nodemask_t numa_nodes_parsed __initdata;
  28. static int cpu_to_node_map[NR_CPUS] = { [0 ... NR_CPUS-1] = NUMA_NO_NODE };
  29. static int numa_distance_cnt;
  30. static u8 *numa_distance;
  31. static bool numa_off;
  32. static __init int numa_parse_early_param(char *opt)
  33. {
  34. if (!opt)
  35. return -EINVAL;
  36. if (!strncmp(opt, "off", 3)) {
  37. pr_info("%s\n", "NUMA turned off");
  38. numa_off = true;
  39. }
  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 map_cpu_to_node(unsigned int cpu, int nid)
  60. {
  61. set_cpu_numa_node(cpu, nid);
  62. if (nid >= 0)
  63. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  64. }
  65. void numa_clear_node(unsigned int cpu)
  66. {
  67. int nid = cpu_to_node(cpu);
  68. if (nid >= 0)
  69. cpumask_clear_cpu(cpu, node_to_cpumask_map[nid]);
  70. set_cpu_numa_node(cpu, NUMA_NO_NODE);
  71. }
  72. /*
  73. * Allocate node_to_cpumask_map based on number of available nodes
  74. * Requires node_possible_map to be valid.
  75. *
  76. * Note: cpumask_of_node() is not valid until after this is done.
  77. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  78. */
  79. static void __init setup_node_to_cpumask_map(void)
  80. {
  81. unsigned int cpu;
  82. int node;
  83. /* setup nr_node_ids if not done yet */
  84. if (nr_node_ids == MAX_NUMNODES)
  85. setup_nr_node_ids();
  86. /* allocate and clear the mapping */
  87. for (node = 0; node < nr_node_ids; node++) {
  88. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  89. cpumask_clear(node_to_cpumask_map[node]);
  90. }
  91. for_each_possible_cpu(cpu)
  92. set_cpu_numa_node(cpu, NUMA_NO_NODE);
  93. /* cpumask_of_node() will now work */
  94. pr_debug("NUMA: Node to cpumask map for %d nodes\n", nr_node_ids);
  95. }
  96. /*
  97. * Set the cpu to node and mem mapping
  98. */
  99. void numa_store_cpu_info(unsigned int cpu)
  100. {
  101. map_cpu_to_node(cpu, numa_off ? 0 : cpu_to_node_map[cpu]);
  102. }
  103. void __init early_map_cpu_to_node(unsigned int cpu, int nid)
  104. {
  105. /* fallback to node 0 */
  106. if (nid < 0 || nid >= MAX_NUMNODES)
  107. nid = 0;
  108. cpu_to_node_map[cpu] = nid;
  109. }
  110. /**
  111. * numa_add_memblk - Set node id to memblk
  112. * @nid: NUMA node ID of the new memblk
  113. * @start: Start address of the new memblk
  114. * @end: End address of the new memblk
  115. *
  116. * RETURNS:
  117. * 0 on success, -errno on failure.
  118. */
  119. int __init numa_add_memblk(int nid, u64 start, u64 end)
  120. {
  121. int ret;
  122. ret = memblock_set_node(start, (end - start), &memblock.memory, nid);
  123. if (ret < 0) {
  124. pr_err("NUMA: memblock [0x%llx - 0x%llx] failed to add on node %d\n",
  125. start, (end - 1), nid);
  126. return ret;
  127. }
  128. node_set(nid, numa_nodes_parsed);
  129. pr_info("NUMA: Adding memblock [0x%llx - 0x%llx] on node %d\n",
  130. start, (end - 1), nid);
  131. return ret;
  132. }
  133. /**
  134. * Initialize NODE_DATA for a node on the local memory
  135. */
  136. static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)
  137. {
  138. const size_t nd_size = roundup(sizeof(pg_data_t), SMP_CACHE_BYTES);
  139. u64 nd_pa;
  140. void *nd;
  141. int tnid;
  142. pr_info("NUMA: Initmem setup node %d [mem %#010Lx-%#010Lx]\n",
  143. nid, start_pfn << PAGE_SHIFT,
  144. (end_pfn << PAGE_SHIFT) - 1);
  145. nd_pa = memblock_alloc_try_nid(nd_size, SMP_CACHE_BYTES, nid);
  146. nd = __va(nd_pa);
  147. /* report and initialize */
  148. pr_info("NUMA: NODE_DATA [mem %#010Lx-%#010Lx]\n",
  149. nd_pa, nd_pa + nd_size - 1);
  150. tnid = early_pfn_to_nid(nd_pa >> PAGE_SHIFT);
  151. if (tnid != nid)
  152. pr_info("NUMA: NODE_DATA(%d) on node %d\n", nid, tnid);
  153. node_data[nid] = nd;
  154. memset(NODE_DATA(nid), 0, sizeof(pg_data_t));
  155. NODE_DATA(nid)->node_id = nid;
  156. NODE_DATA(nid)->node_start_pfn = start_pfn;
  157. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  158. }
  159. /**
  160. * numa_free_distance
  161. *
  162. * The current table is freed.
  163. */
  164. void __init numa_free_distance(void)
  165. {
  166. size_t size;
  167. if (!numa_distance)
  168. return;
  169. size = numa_distance_cnt * numa_distance_cnt *
  170. sizeof(numa_distance[0]);
  171. memblock_free(__pa(numa_distance), size);
  172. numa_distance_cnt = 0;
  173. numa_distance = NULL;
  174. }
  175. /**
  176. *
  177. * Create a new NUMA distance table.
  178. *
  179. */
  180. static int __init numa_alloc_distance(void)
  181. {
  182. size_t size;
  183. u64 phys;
  184. int i, j;
  185. size = nr_node_ids * nr_node_ids * sizeof(numa_distance[0]);
  186. phys = memblock_find_in_range(0, PFN_PHYS(max_pfn),
  187. size, PAGE_SIZE);
  188. if (WARN_ON(!phys))
  189. return -ENOMEM;
  190. memblock_reserve(phys, size);
  191. numa_distance = __va(phys);
  192. numa_distance_cnt = nr_node_ids;
  193. /* fill with the default distances */
  194. for (i = 0; i < numa_distance_cnt; i++)
  195. for (j = 0; j < numa_distance_cnt; j++)
  196. numa_distance[i * numa_distance_cnt + j] = i == j ?
  197. LOCAL_DISTANCE : REMOTE_DISTANCE;
  198. pr_debug("NUMA: Initialized distance table, cnt=%d\n",
  199. numa_distance_cnt);
  200. return 0;
  201. }
  202. /**
  203. * numa_set_distance - Set inter node NUMA distance from node to node.
  204. * @from: the 'from' node to set distance
  205. * @to: the 'to' node to set distance
  206. * @distance: NUMA distance
  207. *
  208. * Set the distance from node @from to @to to @distance.
  209. * If distance table doesn't exist, a warning is printed.
  210. *
  211. * If @from or @to is higher than the highest known node or lower than zero
  212. * or @distance doesn't make sense, the call is ignored.
  213. *
  214. */
  215. void __init numa_set_distance(int from, int to, int distance)
  216. {
  217. if (!numa_distance) {
  218. pr_warn_once("NUMA: Warning: distance table not allocated yet\n");
  219. return;
  220. }
  221. if (from >= numa_distance_cnt || to >= numa_distance_cnt ||
  222. from < 0 || to < 0) {
  223. pr_warn_once("NUMA: Warning: node ids are out of bound, from=%d to=%d distance=%d\n",
  224. from, to, distance);
  225. return;
  226. }
  227. if ((u8)distance != distance ||
  228. (from == to && distance != LOCAL_DISTANCE)) {
  229. pr_warn_once("NUMA: Warning: invalid distance parameter, from=%d to=%d distance=%d\n",
  230. from, to, distance);
  231. return;
  232. }
  233. numa_distance[from * numa_distance_cnt + to] = distance;
  234. }
  235. /**
  236. * Return NUMA distance @from to @to
  237. */
  238. int __node_distance(int from, int to)
  239. {
  240. if (from >= numa_distance_cnt || to >= numa_distance_cnt)
  241. return from == to ? LOCAL_DISTANCE : REMOTE_DISTANCE;
  242. return numa_distance[from * numa_distance_cnt + to];
  243. }
  244. EXPORT_SYMBOL(__node_distance);
  245. static int __init numa_register_nodes(void)
  246. {
  247. int nid;
  248. struct memblock_region *mblk;
  249. /* Check that valid nid is set to memblks */
  250. for_each_memblock(memory, mblk)
  251. if (mblk->nid == NUMA_NO_NODE || mblk->nid >= MAX_NUMNODES) {
  252. pr_warn("NUMA: Warning: invalid memblk node %d [mem %#010Lx-%#010Lx]\n",
  253. mblk->nid, mblk->base,
  254. mblk->base + mblk->size - 1);
  255. return -EINVAL;
  256. }
  257. /* Finally register nodes. */
  258. for_each_node_mask(nid, numa_nodes_parsed) {
  259. unsigned long start_pfn, end_pfn;
  260. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  261. setup_node_data(nid, start_pfn, end_pfn);
  262. node_set_online(nid);
  263. }
  264. /* Setup online nodes to actual nodes*/
  265. node_possible_map = numa_nodes_parsed;
  266. return 0;
  267. }
  268. static int __init numa_init(int (*init_func)(void))
  269. {
  270. int ret;
  271. nodes_clear(numa_nodes_parsed);
  272. nodes_clear(node_possible_map);
  273. nodes_clear(node_online_map);
  274. numa_free_distance();
  275. ret = numa_alloc_distance();
  276. if (ret < 0)
  277. return ret;
  278. ret = init_func();
  279. if (ret < 0)
  280. return ret;
  281. if (nodes_empty(numa_nodes_parsed))
  282. return -EINVAL;
  283. ret = numa_register_nodes();
  284. if (ret < 0)
  285. return ret;
  286. setup_node_to_cpumask_map();
  287. /* init boot processor */
  288. cpu_to_node_map[0] = 0;
  289. map_cpu_to_node(0, 0);
  290. return 0;
  291. }
  292. /**
  293. * dummy_numa_init - Fallback dummy NUMA init
  294. *
  295. * Used if there's no underlying NUMA architecture, NUMA initialization
  296. * fails, or NUMA is disabled on the command line.
  297. *
  298. * Must online at least one node (node 0) and add memory blocks that cover all
  299. * allowed memory. It is unlikely that this function fails.
  300. */
  301. static int __init dummy_numa_init(void)
  302. {
  303. int ret;
  304. struct memblock_region *mblk;
  305. if (numa_off)
  306. pr_info("NUMA disabled\n"); /* Forced off on command line. */
  307. else
  308. pr_info("No NUMA configuration found\n");
  309. pr_info("NUMA: Faking a node at [mem %#018Lx-%#018Lx]\n",
  310. 0LLU, PFN_PHYS(max_pfn) - 1);
  311. for_each_memblock(memory, mblk) {
  312. ret = numa_add_memblk(0, mblk->base, mblk->base + mblk->size);
  313. if (!ret)
  314. continue;
  315. pr_err("NUMA init failed\n");
  316. return ret;
  317. }
  318. numa_off = true;
  319. return 0;
  320. }
  321. /**
  322. * arm64_numa_init - Initialize NUMA
  323. *
  324. * Try each configured NUMA initialization method until one succeeds. The
  325. * last fallback is dummy single node config encomapssing whole memory.
  326. */
  327. void __init arm64_numa_init(void)
  328. {
  329. if (!numa_off) {
  330. if (!acpi_disabled && !numa_init(arm64_acpi_numa_init))
  331. return;
  332. if (acpi_disabled && !numa_init(of_numa_init))
  333. return;
  334. }
  335. numa_init(dummy_numa_init);
  336. }