k8topology.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. 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. if (limit > end_pfn << PAGE_SHIFT)
  96. limit = end_pfn << PAGE_SHIFT;
  97. if (limit <= base)
  98. continue;
  99. base >>= 16;
  100. base <<= 24;
  101. if (base < start)
  102. base = start;
  103. if (limit > end)
  104. limit = end;
  105. if (limit == base) {
  106. printk(KERN_ERR "Empty node %d\n", nodeid);
  107. continue;
  108. }
  109. if (limit < base) {
  110. printk(KERN_ERR "Node %d bogus settings %lx-%lx.\n",
  111. nodeid, base, limit);
  112. continue;
  113. }
  114. /* Could sort here, but pun for now. Should not happen anyroads. */
  115. if (prevbase > base) {
  116. printk(KERN_ERR "Node map not sorted %lx,%lx\n",
  117. prevbase,base);
  118. return -1;
  119. }
  120. printk(KERN_INFO "Node %d MemBase %016lx Limit %016lx\n",
  121. nodeid, base, limit);
  122. found++;
  123. nodes[nodeid].start = base;
  124. nodes[nodeid].end = limit;
  125. prevbase = base;
  126. node_set(nodeid, nodes_parsed);
  127. }
  128. if (!found)
  129. return -1;
  130. memnode_shift = compute_hash_shift(nodes, numnodes);
  131. if (memnode_shift < 0) {
  132. printk(KERN_ERR "No NUMA node hash function found. Contact maintainer\n");
  133. return -1;
  134. }
  135. printk(KERN_INFO "Using node hash shift of %d\n", memnode_shift);
  136. for (i = 0; i < 8; i++) {
  137. if (nodes[i].start != nodes[i].end) {
  138. nodeid = nodeids[i];
  139. apicid_to_node[nodeid << dualcore] = i;
  140. apicid_to_node[(nodeid << dualcore) + dualcore] = i;
  141. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  142. }
  143. }
  144. numa_init_array();
  145. return 0;
  146. }