amd_nb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Shared support code for AMD K8 northbridges and derivates.
  3. * Copyright 2006 Andi Kleen, SUSE Labs. Subject to GPLv2.
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/errno.h>
  10. #include <linux/export.h>
  11. #include <linux/spinlock.h>
  12. #include <asm/amd_nb.h>
  13. #define PCI_DEVICE_ID_AMD_17H_ROOT 0x1450
  14. #define PCI_DEVICE_ID_AMD_17H_DF_F3 0x1463
  15. #define PCI_DEVICE_ID_AMD_17H_DF_F4 0x1464
  16. /* Protect the PCI config register pairs used for SMN and DF indirect access. */
  17. static DEFINE_MUTEX(smn_mutex);
  18. static u32 *flush_words;
  19. static const struct pci_device_id amd_root_ids[] = {
  20. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_17H_ROOT) },
  21. {}
  22. };
  23. #define PCI_DEVICE_ID_AMD_CNB17H_F4 0x1704
  24. const struct pci_device_id amd_nb_misc_ids[] = {
  25. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
  26. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_10H_NB_MISC) },
  27. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_NB_F3) },
  28. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_M10H_F3) },
  29. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F3) },
  30. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F3) },
  31. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_16H_NB_F3) },
  32. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F3) },
  33. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_17H_DF_F3) },
  34. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CNB17H_F3) },
  35. {}
  36. };
  37. EXPORT_SYMBOL_GPL(amd_nb_misc_ids);
  38. static const struct pci_device_id amd_nb_link_ids[] = {
  39. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  40. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
  41. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
  42. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
  43. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
  44. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_17H_DF_F4) },
  45. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CNB17H_F4) },
  46. {}
  47. };
  48. const struct amd_nb_bus_dev_range amd_nb_bus_dev_ranges[] __initconst = {
  49. { 0x00, 0x18, 0x20 },
  50. { 0xff, 0x00, 0x20 },
  51. { 0xfe, 0x00, 0x20 },
  52. { }
  53. };
  54. static struct amd_northbridge_info amd_northbridges;
  55. u16 amd_nb_num(void)
  56. {
  57. return amd_northbridges.num;
  58. }
  59. EXPORT_SYMBOL_GPL(amd_nb_num);
  60. bool amd_nb_has_feature(unsigned int feature)
  61. {
  62. return ((amd_northbridges.flags & feature) == feature);
  63. }
  64. EXPORT_SYMBOL_GPL(amd_nb_has_feature);
  65. struct amd_northbridge *node_to_amd_nb(int node)
  66. {
  67. return (node < amd_northbridges.num) ? &amd_northbridges.nb[node] : NULL;
  68. }
  69. EXPORT_SYMBOL_GPL(node_to_amd_nb);
  70. static struct pci_dev *next_northbridge(struct pci_dev *dev,
  71. const struct pci_device_id *ids)
  72. {
  73. do {
  74. dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
  75. if (!dev)
  76. break;
  77. } while (!pci_match_id(ids, dev));
  78. return dev;
  79. }
  80. static int __amd_smn_rw(u16 node, u32 address, u32 *value, bool write)
  81. {
  82. struct pci_dev *root;
  83. int err = -ENODEV;
  84. if (node >= amd_northbridges.num)
  85. goto out;
  86. root = node_to_amd_nb(node)->root;
  87. if (!root)
  88. goto out;
  89. mutex_lock(&smn_mutex);
  90. err = pci_write_config_dword(root, 0x60, address);
  91. if (err) {
  92. pr_warn("Error programming SMN address 0x%x.\n", address);
  93. goto out_unlock;
  94. }
  95. err = (write ? pci_write_config_dword(root, 0x64, *value)
  96. : pci_read_config_dword(root, 0x64, value));
  97. if (err)
  98. pr_warn("Error %s SMN address 0x%x.\n",
  99. (write ? "writing to" : "reading from"), address);
  100. out_unlock:
  101. mutex_unlock(&smn_mutex);
  102. out:
  103. return err;
  104. }
  105. int amd_smn_read(u16 node, u32 address, u32 *value)
  106. {
  107. return __amd_smn_rw(node, address, value, false);
  108. }
  109. EXPORT_SYMBOL_GPL(amd_smn_read);
  110. int amd_smn_write(u16 node, u32 address, u32 value)
  111. {
  112. return __amd_smn_rw(node, address, &value, true);
  113. }
  114. EXPORT_SYMBOL_GPL(amd_smn_write);
  115. /*
  116. * Data Fabric Indirect Access uses FICAA/FICAD.
  117. *
  118. * Fabric Indirect Configuration Access Address (FICAA): Constructed based
  119. * on the device's Instance Id and the PCI function and register offset of
  120. * the desired register.
  121. *
  122. * Fabric Indirect Configuration Access Data (FICAD): There are FICAD LO
  123. * and FICAD HI registers but so far we only need the LO register.
  124. */
  125. int amd_df_indirect_read(u16 node, u8 func, u16 reg, u8 instance_id, u32 *lo)
  126. {
  127. struct pci_dev *F4;
  128. u32 ficaa;
  129. int err = -ENODEV;
  130. if (node >= amd_northbridges.num)
  131. goto out;
  132. F4 = node_to_amd_nb(node)->link;
  133. if (!F4)
  134. goto out;
  135. ficaa = 1;
  136. ficaa |= reg & 0x3FC;
  137. ficaa |= (func & 0x7) << 11;
  138. ficaa |= instance_id << 16;
  139. mutex_lock(&smn_mutex);
  140. err = pci_write_config_dword(F4, 0x5C, ficaa);
  141. if (err) {
  142. pr_warn("Error writing DF Indirect FICAA, FICAA=0x%x\n", ficaa);
  143. goto out_unlock;
  144. }
  145. err = pci_read_config_dword(F4, 0x98, lo);
  146. if (err)
  147. pr_warn("Error reading DF Indirect FICAD LO, FICAA=0x%x.\n", ficaa);
  148. out_unlock:
  149. mutex_unlock(&smn_mutex);
  150. out:
  151. return err;
  152. }
  153. EXPORT_SYMBOL_GPL(amd_df_indirect_read);
  154. int amd_cache_northbridges(void)
  155. {
  156. u16 i = 0;
  157. struct amd_northbridge *nb;
  158. struct pci_dev *root, *misc, *link;
  159. if (amd_northbridges.num)
  160. return 0;
  161. misc = NULL;
  162. while ((misc = next_northbridge(misc, amd_nb_misc_ids)) != NULL)
  163. i++;
  164. if (!i)
  165. return -ENODEV;
  166. nb = kcalloc(i, sizeof(struct amd_northbridge), GFP_KERNEL);
  167. if (!nb)
  168. return -ENOMEM;
  169. amd_northbridges.nb = nb;
  170. amd_northbridges.num = i;
  171. link = misc = root = NULL;
  172. for (i = 0; i != amd_northbridges.num; i++) {
  173. node_to_amd_nb(i)->root = root =
  174. next_northbridge(root, amd_root_ids);
  175. node_to_amd_nb(i)->misc = misc =
  176. next_northbridge(misc, amd_nb_misc_ids);
  177. node_to_amd_nb(i)->link = link =
  178. next_northbridge(link, amd_nb_link_ids);
  179. }
  180. if (amd_gart_present())
  181. amd_northbridges.flags |= AMD_NB_GART;
  182. /*
  183. * Check for L3 cache presence.
  184. */
  185. if (!cpuid_edx(0x80000006))
  186. return 0;
  187. /*
  188. * Some CPU families support L3 Cache Index Disable. There are some
  189. * limitations because of E382 and E388 on family 0x10.
  190. */
  191. if (boot_cpu_data.x86 == 0x10 &&
  192. boot_cpu_data.x86_model >= 0x8 &&
  193. (boot_cpu_data.x86_model > 0x9 ||
  194. boot_cpu_data.x86_mask >= 0x1))
  195. amd_northbridges.flags |= AMD_NB_L3_INDEX_DISABLE;
  196. if (boot_cpu_data.x86 == 0x15)
  197. amd_northbridges.flags |= AMD_NB_L3_INDEX_DISABLE;
  198. /* L3 cache partitioning is supported on family 0x15 */
  199. if (boot_cpu_data.x86 == 0x15)
  200. amd_northbridges.flags |= AMD_NB_L3_PARTITIONING;
  201. return 0;
  202. }
  203. EXPORT_SYMBOL_GPL(amd_cache_northbridges);
  204. /*
  205. * Ignores subdevice/subvendor but as far as I can figure out
  206. * they're useless anyways
  207. */
  208. bool __init early_is_amd_nb(u32 device)
  209. {
  210. const struct pci_device_id *id;
  211. u32 vendor = device & 0xffff;
  212. device >>= 16;
  213. for (id = amd_nb_misc_ids; id->vendor; id++)
  214. if (vendor == id->vendor && device == id->device)
  215. return true;
  216. return false;
  217. }
  218. struct resource *amd_get_mmconfig_range(struct resource *res)
  219. {
  220. u32 address;
  221. u64 base, msr;
  222. unsigned int segn_busn_bits;
  223. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  224. return NULL;
  225. /* assume all cpus from fam10h have mmconfig */
  226. if (boot_cpu_data.x86 < 0x10)
  227. return NULL;
  228. address = MSR_FAM10H_MMIO_CONF_BASE;
  229. rdmsrl(address, msr);
  230. /* mmconfig is not enabled */
  231. if (!(msr & FAM10H_MMIO_CONF_ENABLE))
  232. return NULL;
  233. base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);
  234. segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
  235. FAM10H_MMIO_CONF_BUSRANGE_MASK;
  236. res->flags = IORESOURCE_MEM;
  237. res->start = base;
  238. res->end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
  239. return res;
  240. }
  241. int amd_get_subcaches(int cpu)
  242. {
  243. struct pci_dev *link = node_to_amd_nb(amd_get_nb_id(cpu))->link;
  244. unsigned int mask;
  245. if (!amd_nb_has_feature(AMD_NB_L3_PARTITIONING))
  246. return 0;
  247. pci_read_config_dword(link, 0x1d4, &mask);
  248. return (mask >> (4 * cpu_data(cpu).cpu_core_id)) & 0xf;
  249. }
  250. int amd_set_subcaches(int cpu, unsigned long mask)
  251. {
  252. static unsigned int reset, ban;
  253. struct amd_northbridge *nb = node_to_amd_nb(amd_get_nb_id(cpu));
  254. unsigned int reg;
  255. int cuid;
  256. if (!amd_nb_has_feature(AMD_NB_L3_PARTITIONING) || mask > 0xf)
  257. return -EINVAL;
  258. /* if necessary, collect reset state of L3 partitioning and BAN mode */
  259. if (reset == 0) {
  260. pci_read_config_dword(nb->link, 0x1d4, &reset);
  261. pci_read_config_dword(nb->misc, 0x1b8, &ban);
  262. ban &= 0x180000;
  263. }
  264. /* deactivate BAN mode if any subcaches are to be disabled */
  265. if (mask != 0xf) {
  266. pci_read_config_dword(nb->misc, 0x1b8, &reg);
  267. pci_write_config_dword(nb->misc, 0x1b8, reg & ~0x180000);
  268. }
  269. cuid = cpu_data(cpu).cpu_core_id;
  270. mask <<= 4 * cuid;
  271. mask |= (0xf ^ (1 << cuid)) << 26;
  272. pci_write_config_dword(nb->link, 0x1d4, mask);
  273. /* reset BAN mode if L3 partitioning returned to reset state */
  274. pci_read_config_dword(nb->link, 0x1d4, &reg);
  275. if (reg == reset) {
  276. pci_read_config_dword(nb->misc, 0x1b8, &reg);
  277. reg &= ~0x180000;
  278. pci_write_config_dword(nb->misc, 0x1b8, reg | ban);
  279. }
  280. return 0;
  281. }
  282. static void amd_cache_gart(void)
  283. {
  284. u16 i;
  285. if (!amd_nb_has_feature(AMD_NB_GART))
  286. return;
  287. flush_words = kmalloc_array(amd_northbridges.num, sizeof(u32), GFP_KERNEL);
  288. if (!flush_words) {
  289. amd_northbridges.flags &= ~AMD_NB_GART;
  290. pr_notice("Cannot initialize GART flush words, GART support disabled\n");
  291. return;
  292. }
  293. for (i = 0; i != amd_northbridges.num; i++)
  294. pci_read_config_dword(node_to_amd_nb(i)->misc, 0x9c, &flush_words[i]);
  295. }
  296. void amd_flush_garts(void)
  297. {
  298. int flushed, i;
  299. unsigned long flags;
  300. static DEFINE_SPINLOCK(gart_lock);
  301. if (!amd_nb_has_feature(AMD_NB_GART))
  302. return;
  303. /*
  304. * Avoid races between AGP and IOMMU. In theory it's not needed
  305. * but I'm not sure if the hardware won't lose flush requests
  306. * when another is pending. This whole thing is so expensive anyways
  307. * that it doesn't matter to serialize more. -AK
  308. */
  309. spin_lock_irqsave(&gart_lock, flags);
  310. flushed = 0;
  311. for (i = 0; i < amd_northbridges.num; i++) {
  312. pci_write_config_dword(node_to_amd_nb(i)->misc, 0x9c,
  313. flush_words[i] | 1);
  314. flushed++;
  315. }
  316. for (i = 0; i < amd_northbridges.num; i++) {
  317. u32 w;
  318. /* Make sure the hardware actually executed the flush*/
  319. for (;;) {
  320. pci_read_config_dword(node_to_amd_nb(i)->misc,
  321. 0x9c, &w);
  322. if (!(w & 1))
  323. break;
  324. cpu_relax();
  325. }
  326. }
  327. spin_unlock_irqrestore(&gart_lock, flags);
  328. if (!flushed)
  329. pr_notice("nothing to flush?\n");
  330. }
  331. EXPORT_SYMBOL_GPL(amd_flush_garts);
  332. static void __fix_erratum_688(void *info)
  333. {
  334. #define MSR_AMD64_IC_CFG 0xC0011021
  335. msr_set_bit(MSR_AMD64_IC_CFG, 3);
  336. msr_set_bit(MSR_AMD64_IC_CFG, 14);
  337. }
  338. /* Apply erratum 688 fix so machines without a BIOS fix work. */
  339. static __init void fix_erratum_688(void)
  340. {
  341. struct pci_dev *F4;
  342. u32 val;
  343. if (boot_cpu_data.x86 != 0x14)
  344. return;
  345. if (!amd_northbridges.num)
  346. return;
  347. F4 = node_to_amd_nb(0)->link;
  348. if (!F4)
  349. return;
  350. if (pci_read_config_dword(F4, 0x164, &val))
  351. return;
  352. if (val & BIT(2))
  353. return;
  354. on_each_cpu(__fix_erratum_688, NULL, 0);
  355. pr_info("x86/cpu/AMD: CPU erratum 688 worked around\n");
  356. }
  357. static __init int init_amd_nbs(void)
  358. {
  359. amd_cache_northbridges();
  360. amd_cache_gart();
  361. fix_erratum_688();
  362. return 0;
  363. }
  364. /* This has to go after the PCI subsystem */
  365. fs_initcall(init_amd_nbs);