Import 2.3.18pre1
[davej-history.git] / arch / sparc64 / kernel / pci.c
blob1776e6d7e43d176292cea81bb4f534f1bcc161af
1 /* $Id: pci.c,v 1.6 1999/09/08 03:40:41 davem Exp $
2 * pci.c: UltraSparc PCI controller support.
4 * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
5 * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
7 */
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/sched.h>
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/smp_lock.h>
16 #include <linux/init.h>
18 #include <asm/uaccess.h>
19 #include <asm/pbm.h>
20 #include <asm/irq.h>
21 #include <asm/ebus.h>
23 unsigned long pci_dvma_v2p_hash[PCI_DVMA_HASHSZ];
24 unsigned long pci_dvma_p2v_hash[PCI_DVMA_HASHSZ];
25 unsigned long pci_memspace_mask = 0xffffffffUL;
27 #ifndef CONFIG_PCI
28 /* A "nop" PCI implementation. */
29 int pcibios_present(void) { return 0; }
30 asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
31 unsigned long off, unsigned long len,
32 unsigned char *buf)
34 return 0;
36 asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
37 unsigned long off, unsigned long len,
38 unsigned char *buf)
40 return 0;
42 #else
44 /* List of all PCI controllers found in the system. */
45 spinlock_t pci_controller_lock = SPIN_LOCK_UNLOCKED;
46 struct pci_controller_info *pci_controller_root = NULL;
48 /* Each PCI controller found gets a unique index. */
49 int pci_num_controllers = 0;
51 /* Given an 8-bit PCI bus number, this yields the
52 * controlling PBM module info.
54 * Some explanation is in order here. The Linux APIs for
55 * the PCI subsystem require that the configuration space
56 * types are enough to signify PCI configuration space
57 * accesses correctly. This gives us 8-bits for the bus
58 * number, however we have multiple PCI controllers on
59 * UltraSparc systems.
61 * So what we do is give the PCI busses under each controller
62 * a unique portion of the 8-bit PCI bus number space.
63 * Therefore one can obtain the controller from the bus
64 * number. For example, say PSYCHO PBM-A a subordinate bus
65 * space of 0 to 4, and PBM-B has a space of 0 to 2. PBM-A
66 * will use 0 to 4, and PBM-B will use 5 to 7.
68 struct pci_pbm_info *pci_bus2pbm[256];
69 unsigned char pci_highest_busnum = 0;
71 /* At boot time the user can give the kernel a command
72 * line option which controls if and how PCI devices
73 * are reordered at PCI bus probing time.
75 int pci_device_reorder = 0;
77 spinlock_t pci_poke_lock = SPIN_LOCK_UNLOCKED;
78 volatile int pci_poke_in_progress;
79 volatile int pci_poke_faulted;
81 /* Probe for all PCI controllers in the system. */
82 extern void sabre_init(int);
83 extern void psycho_init(int);
85 static struct {
86 char *model_name;
87 void (*init)(int);
88 } pci_controller_table[] = {
89 { "SUNW,sabre", sabre_init },
90 { "pci108e,a000", sabre_init },
91 { "SUNW,psycho", psycho_init },
92 { "pci108e,8000", psycho_init }
94 #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
95 sizeof(pci_controller_table[0]))
97 static void pci_controller_init(char *model_name, int namelen, int node)
99 int i;
101 for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
102 if (!strncmp(model_name,
103 pci_controller_table[i].model_name,
104 namelen)) {
105 pci_controller_table[i].init(node);
106 return;
109 printk("PCI: Warning unknown controller, model name [%s]\n",
110 model_name);
111 printk("PCI: Ignoring controller...\n");
114 /* Find each controller in the system, attach and initialize
115 * software state structure for each and link into the
116 * pci_controller_root. Setup the controller enough such
117 * that bus scanning can be done.
119 static void pci_controller_probe(void)
121 char namebuf[16];
122 int node;
124 printk("PCI: Probing for controllers.\n");
125 node = prom_getchild(prom_root_node);
126 while ((node = prom_searchsiblings(node, "pci")) != 0) {
127 int len;
129 len = prom_getproperty(node, "model",
130 namebuf, sizeof(namebuf));
131 if (len > 0)
132 pci_controller_init(namebuf, len, node);
133 else {
134 len = prom_getproperty(node, "compatible",
135 namebuf, sizeof(namebuf));
136 if (len > 0)
137 pci_controller_init(namebuf, len, node);
139 node = prom_getsibling(node);
140 if (!node)
141 break;
145 static void pci_scan_each_controller_bus(void)
147 struct pci_controller_info *p;
148 unsigned long flags;
150 spin_lock_irqsave(&pci_controller_lock, flags);
151 for (p = pci_controller_root; p; p = p->next)
152 p->scan_bus(p);
153 spin_unlock_irqrestore(&pci_controller_lock, flags);
156 /* Reorder the pci_dev chain, so that onboard devices come first
157 * and then come the pluggable cards.
159 static void __init pci_reorder_devs(void)
161 struct pci_dev **pci_onboard = &pci_devices;
162 struct pci_dev **pci_tail = &pci_devices;
163 struct pci_dev *pdev = pci_devices, *pci_other = NULL;
165 while (pdev) {
166 if (pdev->irq && (__irq_ino(pdev->irq) & 0x20)) {
167 if (pci_other) {
168 *pci_onboard = pdev;
169 pci_onboard = &pdev->next;
170 pdev = pdev->next;
171 *pci_onboard = pci_other;
172 *pci_tail = pdev;
173 continue;
174 } else
175 pci_onboard = &pdev->next;
176 } else if (!pci_other)
177 pci_other = pdev;
178 pci_tail = &pdev->next;
179 pdev = pdev->next;
183 void __init pcibios_init(void)
185 pci_controller_probe();
186 if (pci_controller_root == NULL)
187 return;
189 pci_scan_each_controller_bus();
191 if (pci_device_reorder)
192 pci_reorder_devs();
194 ebus_init();
197 struct pci_fixup pcibios_fixups[] = {
198 { 0 }
201 void pcibios_fixup_bus(struct pci_bus *pbus)
205 int pcibios_assign_resource(struct pci_dev *pdev, int resource)
207 return 0;
210 char * __init pcibios_setup(char *str)
212 if (!strcmp(str, "onboardfirst")) {
213 pci_device_reorder = 1;
214 return NULL;
216 if (!strcmp(str, "noreorder")) {
217 pci_device_reorder = 0;
218 return NULL;
220 return str;
223 asmlinkage int sys_pciconfig_read(unsigned long bus,
224 unsigned long dfn,
225 unsigned long off,
226 unsigned long len,
227 unsigned char *buf)
229 struct pci_dev *dev;
230 u8 byte;
231 u16 word;
232 u32 dword;
233 int err = 0;
235 if(!capable(CAP_SYS_ADMIN))
236 return -EPERM;
238 dev = pci_find_slot(bus, dfn);
239 if (!dev) {
240 /* Xfree86 is such a turd, it does not check the
241 * return value and just relies on the buffer being
242 * set to all 1's to mean "device not present".
244 switch(len) {
245 case 1:
246 put_user(0xff, (unsigned char *)buf);
247 break;
248 case 2:
249 put_user(0xffff, (unsigned short *)buf);
250 break;
251 case 4:
252 put_user(0xffffffff, (unsigned int *)buf);
253 break;
254 default:
255 err = -EINVAL;
256 break;
258 goto out;
261 lock_kernel();
262 switch(len) {
263 case 1:
264 pci_read_config_byte(dev, off, &byte);
265 put_user(byte, (unsigned char *)buf);
266 break;
267 case 2:
268 pci_read_config_word(dev, off, &word);
269 put_user(word, (unsigned short *)buf);
270 break;
271 case 4:
272 pci_read_config_dword(dev, off, &dword);
273 put_user(dword, (unsigned int *)buf);
274 break;
276 default:
277 err = -EINVAL;
278 break;
280 unlock_kernel();
281 out:
282 return err;
285 asmlinkage int sys_pciconfig_write(unsigned long bus,
286 unsigned long dfn,
287 unsigned long off,
288 unsigned long len,
289 unsigned char *buf)
291 struct pci_dev *dev;
292 u8 byte;
293 u16 word;
294 u32 dword;
295 int err = 0;
297 if(!capable(CAP_SYS_ADMIN))
298 return -EPERM;
299 dev = pci_find_slot(bus, dfn);
300 if (!dev) {
301 /* See commentary above about Xfree86 */
302 goto out;
305 lock_kernel();
306 switch(len) {
307 case 1:
308 err = get_user(byte, (u8 *)buf);
309 if(err)
310 break;
311 pci_write_config_byte(dev, off, byte);
312 break;
314 case 2:
315 err = get_user(word, (u16 *)buf);
316 if(err)
317 break;
318 pci_write_config_byte(dev, off, word);
319 break;
321 case 4:
322 err = get_user(dword, (u32 *)buf);
323 if(err)
324 break;
325 pci_write_config_byte(dev, off, dword);
326 break;
328 default:
329 err = -EINVAL;
330 break;
333 unlock_kernel();
335 out:
336 return err;
339 #endif /* !(CONFIG_PCI) */