k8topology.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * AMD K8 NUMA support.
  3. * Discover the memory map and associated nodes.
  4. *
  5. * This version reads it directly from the K8 northbridge.
  6. *
  7. * Copyright 2002,2003 Andi Kleen, SuSE Labs.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/string.h>
  12. #include <linux/module.h>
  13. #include <linux/nodemask.h>
  14. #include <asm/io.h>
  15. #include <linux/pci_ids.h>
  16. #include <asm/types.h>
  17. #include <asm/mmzone.h>
  18. #include <asm/proto.h>
  19. #include <asm/e820.h>
  20. #include <asm/pci-direct.h>
  21. #include <asm/numa.h>
  22. static __init int find_northbridge(void)
  23. {
  24. int num;
  25. for (num = 0; num < 32; num++) {
  26. u32 header;
  27. header = read_pci_config(0, num, 0, 0x00);
  28. if (header != (PCI_VENDOR_ID_AMD | (0x1100<<16)))
  29. continue;
  30. header = read_pci_config(0, num, 1, 0x00);
  31. if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)))
  32. continue;
  33. return num;
  34. }
  35. return -1;
  36. }
  37. int __init k8_scan_nodes(unsigned long start, unsigned long end)
  38. {
  39. unsigned long prevbase;
  40. struct node nodes[8];
  41. int nodeid, i, nb;
  42. int found = 0;
  43. u32 reg;
  44. unsigned numnodes;
  45. nodemask_t nodes_parsed;
  46. nodes_clear(nodes_parsed);
  47. nb = find_northbridge();
  48. if (nb < 0)
  49. return nb;
  50. printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
  51. reg = read_pci_config(0, nb, 0, 0x60);
  52. numnodes = ((reg >> 4) & 0xF) + 1;
  53. printk(KERN_INFO "Number of nodes %d\n", numnodes);
  54. memset(&nodes,0,sizeof(nodes));
  55. prevbase = 0;
  56. for (i = 0; i < 8; i++) {
  57. unsigned long base,limit;
  58. base = read_pci_config(0, nb, 1, 0x40 + i*8);
  59. limit = read_pci_config(0, nb, 1, 0x44 + i*8);
  60. nodeid = limit & 7;
  61. if ((base & 3) == 0) {
  62. if (i < numnodes)
  63. printk("Skipping disabled node %d\n", i);
  64. continue;
  65. }
  66. if (nodeid >= numnodes) {
  67. printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
  68. base, limit);
  69. continue;
  70. }
  71. if (!limit) {
  72. printk(KERN_INFO "Skipping node entry %d (base %lx)\n", i,
  73. base);
  74. continue;
  75. }
  76. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  77. printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
  78. nodeid, (base>>8)&3, (limit>>8) & 3);
  79. return -1;
  80. }
  81. if (node_isset(nodeid, nodes_parsed)) {
  82. printk(KERN_INFO "Node %d already present. Skipping\n",
  83. nodeid);
  84. continue;
  85. }
  86. limit >>= 16;
  87. limit <<= 24;
  88. limit |= (1<<24)-1;
  89. if (limit > end_pfn << PAGE_SHIFT)
  90. limit = end_pfn << PAGE_SHIFT;
  91. if (limit <= base)
  92. continue;
  93. base >>= 16;
  94. base <<= 24;
  95. if (base < start)
  96. base = start;
  97. if (limit > end)
  98. limit = end;
  99. if (limit == base) {
  100. printk(KERN_ERR "Empty node %d\n", nodeid);
  101. continue;
  102. }
  103. if (limit < base) {
  104. printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
  105. nodeid, base, limit);
  106. continue;
  107. }
  108. /* Could sort here, but pun for now. Should not happen anyroads. */
  109. if (prevbase > base) {
  110. printk(KERN_ERR "Node map not sorted %lx,%lx\n",
  111. prevbase,base);
  112. return -1;
  113. }
  114. printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
  115. nodeid, base, limit);
  116. found++;
  117. nodes[nodeid].start = base;
  118. nodes[nodeid].end = limit;
  119. prevbase = base;
  120. node_set(nodeid, nodes_parsed);
  121. }
  122. if (!found)
  123. return -1;
  124. memnode_shift = compute_hash_shift(nodes, numnodes);
  125. if (memnode_shift < 0) {
  126. printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
  127. return -1;
  128. }
  129. printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
  130. for (i = 0; i < 8; i++) {
  131. if (nodes[i].start != nodes[i].end) {
  132. /* assume 1:1 NODE:CPU */
  133. cpu_to_node[i] = i;
  134. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  135. }
  136. }
  137. numa_init_array();
  138. return 0;
  139. }