blob: 4ffc38df7ac5bdedcba23a1ddf39f9747ce1bd1c [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) },
Andi Kleena32073b2006-06-26 13:56:40 +020018 {}
19};
20EXPORT_SYMBOL(k8_nb_ids);
21
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020022struct k8_northbridge_info k8_northbridges;
Andi Kleena32073b2006-06-26 13:56:40 +020023EXPORT_SYMBOL(k8_northbridges);
24
25static struct pci_dev *next_k8_northbridge(struct pci_dev *dev)
26{
27 do {
28 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
29 if (!dev)
30 break;
31 } while (!pci_match_id(&k8_nb_ids[0], dev));
32 return dev;
33}
34
35int cache_k8_northbridges(void)
36{
37 int i;
38 struct pci_dev *dev;
Ben Collins3c6df2a2007-05-23 13:57:43 -070039
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020040 if (k8_northbridges.num)
Andi Kleena32073b2006-06-26 13:56:40 +020041 return 0;
42
Andi Kleena32073b2006-06-26 13:56:40 +020043 dev = NULL;
44 while ((dev = next_k8_northbridge(dev)) != NULL)
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020045 k8_northbridges.num++;
Andi Kleena32073b2006-06-26 13:56:40 +020046
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020047 /* some CPU families (e.g. family 0x11) do not support GART */
48 if (boot_cpu_data.x86 == 0xf || boot_cpu_data.x86 == 0x10)
49 k8_northbridges.gart_supported = 1;
50
51 k8_northbridges.nb_misc = kmalloc((k8_northbridges.num + 1) *
52 sizeof(void *), GFP_KERNEL);
53 if (!k8_northbridges.nb_misc)
Andi Kleena32073b2006-06-26 13:56:40 +020054 return -ENOMEM;
55
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020056 if (!k8_northbridges.num) {
57 k8_northbridges.nb_misc[0] = NULL;
Ben Collins3c6df2a2007-05-23 13:57:43 -070058 return 0;
59 }
60
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020061 if (k8_northbridges.gart_supported) {
62 flush_words = kmalloc(k8_northbridges.num * sizeof(u32),
63 GFP_KERNEL);
64 if (!flush_words) {
65 kfree(k8_northbridges.nb_misc);
66 return -ENOMEM;
67 }
Andi Kleena32073b2006-06-26 13:56:40 +020068 }
69
70 dev = NULL;
71 i = 0;
72 while ((dev = next_k8_northbridge(dev)) != NULL) {
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020073 k8_northbridges.nb_misc[i] = dev;
74 if (k8_northbridges.gart_supported)
75 pci_read_config_dword(dev, 0x9c, &flush_words[i++]);
Andi Kleena32073b2006-06-26 13:56:40 +020076 }
Andreas Herrmann900f9ac2010-09-17 18:02:54 +020077 k8_northbridges.nb_misc[i] = NULL;
Andi Kleena32073b2006-06-26 13:56:40 +020078 return 0;
79}
80EXPORT_SYMBOL_GPL(cache_k8_northbridges);
81
82/* Ignores subdevice/subvendor but as far as I can figure out
83 they're useless anyways */
84int __init early_is_k8_nb(u32 device)
85{
86 struct pci_device_id *id;
87 u32 vendor = device & 0xffff;
88 device >>= 16;
89 for (id = k8_nb_ids; id->vendor; id++)
90 if (vendor == id->vendor && device == id->device)
91 return 1;
92 return 0;
93}
94
95void k8_flush_garts(void)
96{
97 int flushed, i;
98 unsigned long flags;
99 static DEFINE_SPINLOCK(gart_lock);
100
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200101 if (!k8_northbridges.gart_supported)
102 return;
103
Andi Kleena32073b2006-06-26 13:56:40 +0200104 /* Avoid races between AGP and IOMMU. In theory it's not needed
105 but I'm not sure if the hardware won't lose flush requests
106 when another is pending. This whole thing is so expensive anyways
107 that it doesn't matter to serialize more. -AK */
108 spin_lock_irqsave(&gart_lock, flags);
109 flushed = 0;
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200110 for (i = 0; i < k8_northbridges.num; i++) {
111 pci_write_config_dword(k8_northbridges.nb_misc[i], 0x9c,
Andi Kleena32073b2006-06-26 13:56:40 +0200112 flush_words[i]|1);
113 flushed++;
114 }
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200115 for (i = 0; i < k8_northbridges.num; i++) {
Andi Kleena32073b2006-06-26 13:56:40 +0200116 u32 w;
117 /* Make sure the hardware actually executed the flush*/
118 for (;;) {
Andreas Herrmann900f9ac2010-09-17 18:02:54 +0200119 pci_read_config_dword(k8_northbridges.nb_misc[i],
Andi Kleena32073b2006-06-26 13:56:40 +0200120 0x9c, &w);
121 if (!(w & 1))
122 break;
123 cpu_relax();
124 }
125 }
126 spin_unlock_irqrestore(&gart_lock, flags);
127 if (!flushed)
128 printk("nothing to flush?\n");
129}
130EXPORT_SYMBOL_GPL(k8_flush_garts);
131
Borislav Petkov0e152cd2010-03-12 15:43:03 +0100132static __init int init_k8_nbs(void)
133{
134 int err = 0;
135
136 err = cache_k8_northbridges();
137
138 if (err < 0)
139 printk(KERN_NOTICE "K8 NB: Cannot enumerate AMD northbridges.\n");
140
141 return err;
142}
143
144/* This has to go after the PCI subsystem */
145fs_initcall(init_k8_nbs);