k8topology.c 3.9 KB

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