k8topology_64.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. header != (PCI_VENDOR_ID_AMD | (0x1200<<16)) &&
  30. header != (PCI_VENDOR_ID_AMD | (0x1300<<16)))
  31. continue;
  32. header = read_pci_config(0, num, 1, 0x00);
  33. if (header != (PCI_VENDOR_ID_AMD | (0x1101<<16)) &&
  34. header != (PCI_VENDOR_ID_AMD | (0x1201<<16)) &&
  35. header != (PCI_VENDOR_ID_AMD | (0x1301<<16)))
  36. continue;
  37. return num;
  38. }
  39. return -1;
  40. }
  41. int __init k8_scan_nodes(unsigned long start, unsigned long end)
  42. {
  43. unsigned long prevbase;
  44. struct bootnode nodes[8];
  45. int nodeid, i, nb;
  46. unsigned char nodeids[8];
  47. int found = 0;
  48. u32 reg;
  49. unsigned numnodes;
  50. unsigned cores;
  51. unsigned bits;
  52. int j;
  53. if (!early_pci_allowed())
  54. return -1;
  55. nb = find_northbridge();
  56. if (nb < 0)
  57. return nb;
  58. printk(KERN_INFO "Scanning NUMA topology in Northbridge %d\n", nb);
  59. reg = read_pci_config(0, nb, 0, 0x60);
  60. numnodes = ((reg >> 4) & 0xF) + 1;
  61. if (numnodes <= 1)
  62. return -1;
  63. printk(KERN_INFO "Number of nodes %d\n", numnodes);
  64. memset(&nodes, 0, sizeof(nodes));
  65. prevbase = 0;
  66. for (i = 0; i < 8; i++) {
  67. unsigned long base, limit;
  68. u32 nodeid;
  69. base = read_pci_config(0, nb, 1, 0x40 + i*8);
  70. limit = read_pci_config(0, nb, 1, 0x44 + i*8);
  71. nodeid = limit & 7;
  72. nodeids[i] = nodeid;
  73. if ((base & 3) == 0) {
  74. if (i < numnodes)
  75. printk("Skipping disabled node %d\n", i);
  76. continue;
  77. }
  78. if (nodeid >= numnodes) {
  79. printk("Ignoring excess node %d (%lx:%lx)\n", nodeid,
  80. base, limit);
  81. continue;
  82. }
  83. if (!limit) {
  84. printk(KERN_INFO "Skipping node entry %d (base %lx)\n",
  85. i, base);
  86. continue;
  87. }
  88. if ((base >> 8) & 3 || (limit >> 8) & 3) {
  89. printk(KERN_ERR "Node %d using interleaving mode %lx/%lx\n",
  90. nodeid, (base>>8)&3, (limit>>8) & 3);
  91. return -1;
  92. }
  93. if (node_isset(nodeid, node_possible_map)) {
  94. printk(KERN_INFO "Node %d already present. Skipping\n",
  95. nodeid);
  96. continue;
  97. }
  98. limit >>= 16;
  99. limit <<= 24;
  100. limit |= (1<<24)-1;
  101. limit++;
  102. if (limit > end_pfn << PAGE_SHIFT)
  103. limit = end_pfn << PAGE_SHIFT;
  104. if (limit <= base)
  105. continue;
  106. base >>= 16;
  107. base <<= 24;
  108. if (base < start)
  109. base = start;
  110. if (limit > end)
  111. limit = end;
  112. if (limit == base) {
  113. printk(KERN_ERR "Empty node %d\n", nodeid);
  114. continue;
  115. }
  116. if (limit < base) {
  117. printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
  118. nodeid, base, limit);
  119. continue;
  120. }
  121. /* Could sort here, but pun for now. Should not happen anyroads. */
  122. if (prevbase > base) {
  123. printk(KERN_ERR "Node map not sorted %lx,%lx\n",
  124. prevbase, base);
  125. return -1;
  126. }
  127. printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
  128. nodeid, base, limit);
  129. found++;
  130. nodes[nodeid].start = base;
  131. nodes[nodeid].end = limit;
  132. e820_register_active_regions(nodeid,
  133. nodes[nodeid].start >> PAGE_SHIFT,
  134. nodes[nodeid].end >> PAGE_SHIFT);
  135. prevbase = base;
  136. node_set(nodeid, node_possible_map);
  137. }
  138. if (!found)
  139. return -1;
  140. memnode_shift = compute_hash_shift(nodes, 8, NULL);
  141. if (memnode_shift < 0) {
  142. printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
  143. return -1;
  144. }
  145. printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
  146. /* use the coreid bits from early_identify_cpu */
  147. bits = boot_cpu_data.x86_coreid_bits;
  148. cores = (1<<bits);
  149. for (i = 0; i < 8; i++) {
  150. if (nodes[i].start != nodes[i].end) {
  151. nodeid = nodeids[i];
  152. for (j = 0; j < cores; j++)
  153. apicid_to_node[(nodeid << bits) + j] = i;
  154. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  155. }
  156. }
  157. numa_init_array();
  158. return 0;
  159. }