blob: 8f6463d8ed0de1ebfece6cb1138a15697f657197 [file] [log] [blame]
Andi Kleena32073b2006-06-26 13:56:40 +02001/*
2 * Shared support code for AMD K8 northbridges and derivates.
3 * Copyright 2006 Andi Kleen, SUSE Labs. Subject to GPLv2.
4 */
Andi Kleena32073b2006-06-26 13:56:40 +02005#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Andi Kleena32073b2006-06-26 13:56:40 +02007#include <linux/init.h>
8#include <linux/errno.h>
9#include <linux/module.h>
10#include <linux/spinlock.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020011#include <asm/amd_nb.h>
Andi Kleena32073b2006-06-26 13:56:40 +020012
Andi Kleena32073b2006-06-26 13:56:40 +020013static u32 *flush_words;
14
15struct pci_device_id k8_nb_ids[] = {
Joerg Roedelcf169702008-09-02 13:13:40 +020016 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
17 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_10H_NB_MISC) },
Andreas Herrmann5c80cc72010-09-30 14:43:16 +020018 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_15H_NB_MISC) },
Andi Kleena32073b2006-06-26 13:56:40 +020019 {}
20};
21EXPORT_SYMBOL(k8_nb_ids);
22
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020023struct k8_northbridge_info k8_northbridges;
Andi Kleena32073b2006-06-26 13:56:40 +020024EXPORT_SYMBOL(k8_northbridges);
25
26static struct pci_dev *next_k8_northbridge(struct pci_dev *dev)
27{
28 do {
29 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
30 if (!dev)
31 break;
32 } while (!pci_match_id(&k8_nb_ids[0], dev));
33 return dev;
34}
35
36int cache_k8_northbridges(void)
37{
38 int i;
39 struct pci_dev *dev;
Ben Collins3c6df2a2007-05-23 13:57:43 -070040
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020041 if (k8_northbridges.num)
Andi Kleena32073b2006-06-26 13:56:40 +020042 return 0;
43
Andi Kleena32073b2006-06-26 13:56:40 +020044 dev = NULL;
45 while ((dev = next_k8_northbridge(dev)) != NULL)
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020046 k8_northbridges.num++;
Andi Kleena32073b2006-06-26 13:56:40 +020047
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020048 /* some CPU families (e.g. family 0x11) do not support GART */
Andreas Herrmann5c80cc72010-09-30 14:43:16 +020049 if (boot_cpu_data.x86 == 0xf || boot_cpu_data.x86 == 0x10 ||
50 boot_cpu_data.x86 == 0x15)
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020051 k8_northbridges.gart_supported = 1;
52
53 k8_northbridges.nb_misc = kmalloc((k8_northbridges.num + 1) *
54 sizeof(void *), GFP_KERNEL);
55 if (!k8_northbridges.nb_misc)
Andi Kleena32073b2006-06-26 13:56:40 +020056 return -ENOMEM;
57
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020058 if (!k8_northbridges.num) {
59 k8_northbridges.nb_misc[0] = NULL;
Ben Collins3c6df2a2007-05-23 13:57:43 -070060 return 0;
61 }
62
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020063 if (k8_northbridges.gart_supported) {
64 flush_words = kmalloc(k8_northbridges.num * sizeof(u32),
65 GFP_KERNEL);
66 if (!flush_words) {
67 kfree(k8_northbridges.nb_misc);
68 return -ENOMEM;
69 }
Andi Kleena32073b2006-06-26 13:56:40 +020070 }
71
72 dev = NULL;
73 i = 0;
74 while ((dev = next_k8_northbridge(dev)) != NULL) {
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020075 k8_northbridges.nb_misc[i] = dev;
76 if (k8_northbridges.gart_supported)
77 pci_read_config_dword(dev, 0x9c, &flush_words[i++]);
Andi Kleena32073b2006-06-26 13:56:40 +020078 }
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020079 k8_northbridges.nb_misc[i] = NULL;
Andi Kleena32073b2006-06-26 13:56:40 +020080 return 0;
81}
82EXPORT_SYMBOL_GPL(cache_k8_northbridges);
83
84/* Ignores subdevice/subvendor but as far as I can figure out
85 they're useless anyways */
86int __init early_is_k8_nb(u32 device)
87{
88 struct pci_device_id *id;
89 u32 vendor = device & 0xffff;
90 device >>= 16;
91 for (id = k8_nb_ids; id->vendor; id++)
92 if (vendor == id->vendor && device == id->device)
93 return 1;
94 return 0;
95}
96
97void k8_flush_garts(void)
98{
99 int flushed, i;
100 unsigned long flags;
101 static DEFINE_SPINLOCK(gart_lock);
102
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200103 if (!k8_northbridges.gart_supported)
104 return;
105
Andi Kleena32073b2006-06-26 13:56:40 +0200106 /* Avoid races between AGP and IOMMU. In theory it's not needed
107 but I'm not sure if the hardware won't lose flush requests
108 when another is pending. This whole thing is so expensive anyways
109 that it doesn't matter to serialize more. -AK */
110 spin_lock_irqsave(&gart_lock, flags);
111 flushed = 0;
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200112 for (i = 0; i < k8_northbridges.num; i++) {
113 pci_write_config_dword(k8_northbridges.nb_misc[i], 0x9c,
Andi Kleena32073b2006-06-26 13:56:40 +0200114 flush_words[i]|1);
115 flushed++;
116 }
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200117 for (i = 0; i < k8_northbridges.num; i++) {
Andi Kleena32073b2006-06-26 13:56:40 +0200118 u32 w;
119 /* Make sure the hardware actually executed the flush*/
120 for (;;) {
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200121 pci_read_config_dword(k8_northbridges.nb_misc[i],
Andi Kleena32073b2006-06-26 13:56:40 +0200122 0x9c, &w);
123 if (!(w & 1))
124 break;
125 cpu_relax();
126 }
127 }
128 spin_unlock_irqrestore(&gart_lock, flags);
129 if (!flushed)
130 printk("nothing to flush?\n");
131}
132EXPORT_SYMBOL_GPL(k8_flush_garts);
133
Borislav Petkov0e152cd2010-03-12 15:43:03 +0100134static __init int init_k8_nbs(void)
135{
136 int err = 0;
137
138 err = cache_k8_northbridges();
139
140 if (err < 0)
141 printk(KERN_NOTICE "K8 NB: Cannot enumerate AMD northbridges.\n");
142
143 return err;
144}
145
146/* This has to go after the PCI subsystem */
147fs_initcall(init_k8_nbs);