amd_nb.c 12 KB

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