numa.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * This file contains NUMA specific variables and functions which can
  7. * be split away from DISCONTIGMEM and are used on NUMA machines with
  8. * contiguous memory.
  9. *
  10. * 2002/08/07 Erich Focht <efocht@ess.nec.de>
  11. */
  12. #include <linux/cpu.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/node.h>
  16. #include <linux/init.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/module.h>
  19. #include <linux/random.h>
  20. #include <asm/mmzone.h>
  21. #include <asm/numa.h>
  22. /*
  23. * The following structures are usually initialized by ACPI or
  24. * similar mechanisms and describe the NUMA characteristics of the machine.
  25. */
  26. int num_node_memblks;
  27. struct node_memblk_s node_memblk[NR_NODE_MEMBLKS];
  28. struct node_cpuid_s node_cpuid[NR_CPUS] =
  29. { [0 ... NR_CPUS-1] = { .phys_id = 0, .nid = NUMA_NO_NODE } };
  30. /*
  31. * This is a matrix with "distances" between nodes, they should be
  32. * proportional to the memory access latency ratios.
  33. */
  34. u8 numa_slit[MAX_NUMNODES * MAX_NUMNODES];
  35. /* Identify which cnode a physical address resides on */
  36. int
  37. paddr_to_nid(unsigned long paddr)
  38. {
  39. int i;
  40. for (i = 0; i < num_node_memblks; i++)
  41. if (paddr >= node_memblk[i].start_paddr &&
  42. paddr < node_memblk[i].start_paddr + node_memblk[i].size)
  43. break;
  44. return (i < num_node_memblks) ? node_memblk[i].nid : (num_node_memblks ? -1 : 0);
  45. }
  46. /*
  47. * Return the bit number of a random bit set in the nodemask.
  48. * (returns -1 if nodemask is empty)
  49. */
  50. int __node_random(const nodemask_t *maskp)
  51. {
  52. int w, bit = -1;
  53. w = nodes_weight(*maskp);
  54. if (w)
  55. bit = bitmap_ord_to_pos(maskp->bits,
  56. get_random_int() % w, MAX_NUMNODES);
  57. return bit;
  58. }
  59. EXPORT_SYMBOL(__node_random);
  60. #if defined(CONFIG_SPARSEMEM) && defined(CONFIG_NUMA)
  61. /*
  62. * Because of holes evaluate on section limits.
  63. * If the section of memory exists, then return the node where the section
  64. * resides. Otherwise return node 0 as the default. This is used by
  65. * SPARSEMEM to allocate the SPARSEMEM sectionmap on the NUMA node where
  66. * the section resides.
  67. */
  68. int __meminit __early_pfn_to_nid(unsigned long pfn)
  69. {
  70. int i, section = pfn >> PFN_SECTION_SHIFT, ssec, esec;
  71. for (i = 0; i < num_node_memblks; i++) {
  72. ssec = node_memblk[i].start_paddr >> PA_SECTION_SHIFT;
  73. esec = (node_memblk[i].start_paddr + node_memblk[i].size +
  74. ((1L << PA_SECTION_SHIFT) - 1)) >> PA_SECTION_SHIFT;
  75. if (section >= ssec && section < esec)
  76. return node_memblk[i].nid;
  77. }
  78. return -1;
  79. }
  80. #ifdef CONFIG_MEMORY_HOTPLUG
  81. /*
  82. * SRAT information is stored in node_memblk[], then we can use SRAT
  83. * information at memory-hot-add if necessary.
  84. */
  85. int memory_add_physaddr_to_nid(u64 addr)
  86. {
  87. int nid = paddr_to_nid(addr);
  88. if (nid < 0)
  89. return 0;
  90. return nid;
  91. }
  92. EXPORT_SYMBOL_GPL(memory_add_physaddr_to_nid);
  93. #endif
  94. #endif