amd_nb.c 11 KB

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