k8topology.c 4.1 KB

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