amd_nb.c 12 KB

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