1 /*
2  *	linux/arch/alpha/kernel/pci.c
3  *
4  * Extruded from code written by
5  *	Dave Rusling (david.rusling@reo.mts.dec.com)
6  *	David Mosberger (davidm@cs.arizona.edu)
7  */
8 
9 /* 2.3.x PCI/resources, 1999 Andrea Arcangeli <andrea@suse.de> */
10 
11 /*
12  * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
13  *	     PCI-PCI bridges cleanup
14  */
15 
16 #include <linux/string.h>
17 #include <linux/pci.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/kernel.h>
21 #include <linux/bootmem.h>
22 #include <linux/slab.h>
23 #include <asm/machvec.h>
24 
25 #include "proto.h"
26 #include "pci_impl.h"
27 
28 
29 /*
30  * Some string constants used by the various core logics.
31  */
32 
33 const char *const pci_io_names[] = {
34   "PCI IO bus 0", "PCI IO bus 1", "PCI IO bus 2", "PCI IO bus 3",
35   "PCI IO bus 4", "PCI IO bus 5", "PCI IO bus 6", "PCI IO bus 7"
36 };
37 
38 const char *const pci_mem_names[] = {
39   "PCI mem bus 0", "PCI mem bus 1", "PCI mem bus 2", "PCI mem bus 3",
40   "PCI mem bus 4", "PCI mem bus 5", "PCI mem bus 6", "PCI mem bus 7"
41 };
42 
43 const char pci_hae0_name[] = "HAE0";
44 
45 /* Indicate whether we respect the PCI setup left by console. */
46 int pci_probe_only;
47 
48 /*
49  * The PCI controller list.
50  */
51 
52 struct pci_controller *hose_head, **hose_tail = &hose_head;
53 struct pci_controller *pci_isa_hose;
54 
55 /*
56  * Quirks.
57  */
58 
59 static void __init
quirk_eisa_bridge(struct pci_dev * dev)60 quirk_eisa_bridge(struct pci_dev *dev)
61 {
62 	dev->class = PCI_CLASS_BRIDGE_EISA << 8;
63 }
64 
65 static void __init
quirk_isa_bridge(struct pci_dev * dev)66 quirk_isa_bridge(struct pci_dev *dev)
67 {
68 	dev->class = PCI_CLASS_BRIDGE_ISA << 8;
69 }
70 
71 static void __init
quirk_cypress(struct pci_dev * dev)72 quirk_cypress(struct pci_dev *dev)
73 {
74 	/* The Notorious Cy82C693 chip.  */
75 
76 	/* The Cypress IDE controller doesn't support native mode, but it
77 	   has programmable addresses of IDE command/control registers.
78 	   This violates PCI specifications, confuses the IDE subsystem and
79 	   causes resource conflicts between the primary HD_CMD register and
80 	   the floppy controller.  Ugh.  Fix that.  */
81 	if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE) {
82 		dev->resource[0].flags = 0;
83 		dev->resource[1].flags = 0;
84 	}
85 
86 	/* The Cypress bridge responds on the PCI bus in the address range
87 	   0xffff0000-0xffffffff (conventional x86 BIOS ROM).  There is no
88 	   way to turn this off.  The bridge also supports several extended
89 	   BIOS ranges (disabled after power-up), and some consoles do turn
90 	   them on.  So if we use a large direct-map window, or a large SG
91 	   window, we must avoid entire 0xfff00000-0xffffffff region.  */
92 	else if (dev->class >> 8 == PCI_CLASS_BRIDGE_ISA) {
93 		if (__direct_map_base + __direct_map_size >= 0xfff00000)
94 			__direct_map_size = 0xfff00000 - __direct_map_base;
95 		else {
96 			struct pci_controller *hose = dev->sysdata;
97 			struct pci_iommu_arena *pci = hose->sg_pci;
98 			if (pci && pci->dma_base + pci->size >= 0xfff00000)
99 				pci->size = 0xfff00000 - pci->dma_base;
100 		}
101 	}
102 }
103 
104 struct pci_fixup pcibios_fixups[] __initdata = {
105 	{ PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82375,
106 	  quirk_eisa_bridge },
107 	{ PCI_FIXUP_HEADER, PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82378,
108 	  quirk_isa_bridge },
109 	{ PCI_FIXUP_HEADER, PCI_VENDOR_ID_CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693,
110 	  quirk_cypress },
111 	{ 0 }
112 };
113 
114 #define MAX(val1, val2)		((val1) > (val2) ? (val1) : (val2))
115 #define ALIGN(val,align)	(((val) + ((align) - 1)) & ~((align) - 1))
116 #define KB			1024
117 #define MB			(1024*KB)
118 #define GB			(1024*MB)
119 
120 void
pcibios_align_resource(void * data,struct resource * res,unsigned long size,unsigned long align)121 pcibios_align_resource(void *data, struct resource *res,
122 		       unsigned long size, unsigned long align)
123 {
124 	struct pci_dev *dev = data;
125 	struct pci_controller *hose = dev->sysdata;
126 	unsigned long alignto;
127 	unsigned long start = res->start;
128 
129 	if (res->flags & IORESOURCE_IO) {
130 		/* Make sure we start at our min on all hoses */
131 		if (start - hose->io_space->start < PCIBIOS_MIN_IO)
132 			start = PCIBIOS_MIN_IO + hose->io_space->start;
133 
134 		/*
135 		 * Put everything into 0x00-0xff region modulo 0x400
136 		 */
137 		if (start & 0x300)
138 			start = (start + 0x3ff) & ~0x3ff;
139 	}
140 	else if	(res->flags & IORESOURCE_MEM) {
141 		/* Make sure we start at our min on all hoses */
142 		if (start - hose->mem_space->start < PCIBIOS_MIN_MEM)
143 			start = PCIBIOS_MIN_MEM + hose->mem_space->start;
144 
145 		/*
146 		 * The following holds at least for the Low Cost
147 		 * Alpha implementation of the PCI interface:
148 		 *
149 		 * In sparse memory address space, the first
150 		 * octant (16MB) of every 128MB segment is
151 		 * aliased to the very first 16 MB of the
152 		 * address space (i.e., it aliases the ISA
153 		 * memory address space).  Thus, we try to
154 		 * avoid allocating PCI devices in that range.
155 		 * Can be allocated in 2nd-7th octant only.
156 		 * Devices that need more than 112MB of
157 		 * address space must be accessed through
158 		 * dense memory space only!
159 		 */
160 
161 		/* Align to multiple of size of minimum base.  */
162 		alignto = MAX(0x1000, align);
163 		start = ALIGN(start, alignto);
164 		if (hose->sparse_mem_base && size <= 7 * 16*MB) {
165 			if (((start / (16*MB)) & 0x7) == 0) {
166 				start &= ~(128*MB - 1);
167 				start += 16*MB;
168 				start  = ALIGN(start, alignto);
169 			}
170 			if (start/(128*MB) != (start + size - 1)/(128*MB)) {
171 				start &= ~(128*MB - 1);
172 				start += (128 + 16)*MB;
173 				start  = ALIGN(start, alignto);
174 			}
175 		}
176 	}
177 
178 	res->start = start;
179 }
180 #undef MAX
181 #undef ALIGN
182 #undef KB
183 #undef MB
184 #undef GB
185 
186 void __init
pcibios_init(void)187 pcibios_init(void)
188 {
189 	if (!alpha_mv.init_pci)
190 		return;
191 	alpha_mv.init_pci();
192 }
193 
194 char * __init
pcibios_setup(char * str)195 pcibios_setup(char *str)
196 {
197 	return str;
198 }
199 
200 #ifdef ALPHA_RESTORE_SRM_SETUP
201 static struct pdev_srm_saved_conf *srm_saved_configs;
202 
203 void __init
pdev_save_srm_config(struct pci_dev * dev)204 pdev_save_srm_config(struct pci_dev *dev)
205 {
206 	struct pdev_srm_saved_conf *tmp;
207 	static int printed = 0;
208 
209 	if (!alpha_using_srm || pci_probe_only)
210 		return;
211 
212 	if (!printed) {
213 		printk(KERN_INFO "pci: enabling save/restore of SRM state\n");
214 		printed = 1;
215 	}
216 
217 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
218 	if (!tmp) {
219 		printk(KERN_ERR "%s: kmalloc() failed!\n", __FUNCTION__);
220 		return;
221 	}
222 	tmp->next = srm_saved_configs;
223 	tmp->dev = dev;
224 
225 	pci_save_state(dev, tmp->regs);
226 
227 	srm_saved_configs = tmp;
228 }
229 
230 void
pci_restore_srm_config(void)231 pci_restore_srm_config(void)
232 {
233 	struct pdev_srm_saved_conf *tmp;
234 
235 	/* No need to restore if probed only. */
236 	if (pci_probe_only)
237 		return;
238 
239 	/* Restore SRM config. */
240 	for (tmp = srm_saved_configs; tmp; tmp = tmp->next) {
241 		pci_restore_state(tmp->dev, tmp->regs);
242 	}
243 }
244 #endif
245 
246 void __init
pcibios_fixup_resource(struct resource * res,struct resource * root)247 pcibios_fixup_resource(struct resource *res, struct resource *root)
248 {
249 	res->start += root->start;
250 	res->end += root->start;
251 }
252 
253 void __init
pcibios_fixup_device_resources(struct pci_dev * dev,struct pci_bus * bus)254 pcibios_fixup_device_resources(struct pci_dev *dev, struct pci_bus *bus)
255 {
256 	/* Update device resources.  */
257 	struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
258 	int i;
259 
260 	for (i = 0; i < PCI_NUM_RESOURCES; i++) {
261 		if (!dev->resource[i].start)
262 			continue;
263 		if (dev->resource[i].flags & IORESOURCE_IO)
264 			pcibios_fixup_resource(&dev->resource[i],
265 					       hose->io_space);
266 		else if (dev->resource[i].flags & IORESOURCE_MEM)
267 			pcibios_fixup_resource(&dev->resource[i],
268 					       hose->mem_space);
269 	}
270 }
271 
272 void __init
pcibios_fixup_bus(struct pci_bus * bus)273 pcibios_fixup_bus(struct pci_bus *bus)
274 {
275 	/* Propogate hose info into the subordinate devices.  */
276 
277 	struct pci_controller *hose = bus->sysdata;
278 	struct list_head *ln;
279 	struct pci_dev *dev = bus->self;
280 
281 	if (!dev) {
282 		/* Root bus. */
283 		u32 pci_mem_end;
284 		u32 sg_base = hose->sg_pci ? hose->sg_pci->dma_base : ~0;
285 		unsigned long end;
286 
287 		bus->resource[0] = hose->io_space;
288 		bus->resource[1] = hose->mem_space;
289 
290 		/* Adjust hose mem_space limit to prevent PCI allocations
291 		   in the iommu windows. */
292 		pci_mem_end = min((u32)__direct_map_base, sg_base) - 1;
293 		end = hose->mem_space->start + pci_mem_end;
294 		if (hose->mem_space->end > end)
295 			hose->mem_space->end = end;
296  	} else if (pci_probe_only &&
297  		   (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
298  		pci_read_bridge_bases(bus);
299  		pcibios_fixup_device_resources(dev, bus);
300 	}
301 
302 	for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
303 		struct pci_dev *dev = pci_dev_b(ln);
304 
305 		pdev_save_srm_config(dev);
306 		if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
307 			pcibios_fixup_device_resources(dev, bus);
308 	}
309 }
310 
311 void
pcibios_update_resource(struct pci_dev * dev,struct resource * root,struct resource * res,int resource)312 pcibios_update_resource(struct pci_dev *dev, struct resource *root,
313 			struct resource *res, int resource)
314 {
315 	struct pci_controller *hose = dev->sysdata;
316 	int where;
317 	u32 reg;
318 
319 	if (resource < PCI_ROM_RESOURCE)
320 		where = PCI_BASE_ADDRESS_0 + (resource * 4);
321 	else if (resource == PCI_ROM_RESOURCE)
322 		where = dev->rom_base_reg;
323 	else {
324 		return; /* Don't update non-standard resources here. */
325 	}
326 
327 	/* Point root at the hose root. */
328 	if (res->flags & IORESOURCE_IO)
329 		root = hose->io_space;
330 	if (res->flags & IORESOURCE_MEM)
331 		root = hose->mem_space;
332 
333 	reg = (res->start - root->start) | (res->flags & 0xf);
334 	pci_write_config_dword(dev, where, reg);
335 	if ((res->flags & (PCI_BASE_ADDRESS_SPACE
336 			   | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
337 	    == (PCI_BASE_ADDRESS_SPACE_MEMORY
338 		| PCI_BASE_ADDRESS_MEM_TYPE_64)) {
339 		pci_write_config_dword(dev, where+4, 0);
340 		printk(KERN_WARNING "PCI: dev %s type 64-bit\n", dev->name);
341 	}
342 
343 	/* ??? FIXME -- record old value for shutdown.  */
344 }
345 
346 void __init
pcibios_update_irq(struct pci_dev * dev,int irq)347 pcibios_update_irq(struct pci_dev *dev, int irq)
348 {
349 	pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
350 }
351 
352 /* Most Alphas have straight-forward swizzling needs.  */
353 
354 u8 __init
common_swizzle(struct pci_dev * dev,u8 * pinp)355 common_swizzle(struct pci_dev *dev, u8 *pinp)
356 {
357 	struct pci_controller *hose = dev->sysdata;
358 
359 	if (dev->bus->number != hose->first_busno) {
360 		u8 pin = *pinp;
361 		do {
362 			pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
363 			/* Move up the chain of bridges. */
364 			dev = dev->bus->self;
365 		} while (dev->bus->parent);
366 		*pinp = pin;
367 
368 		/* The slot is the slot of the last bridge. */
369 	}
370 
371 	return PCI_SLOT(dev->devfn);
372 }
373 
374 void __init
pcibios_fixup_pbus_ranges(struct pci_bus * bus,struct pbus_set_ranges_data * ranges)375 pcibios_fixup_pbus_ranges(struct pci_bus * bus,
376 			  struct pbus_set_ranges_data * ranges)
377 {
378 	struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
379 
380 	ranges->io_start -= hose->io_space->start;
381 	ranges->io_end -= hose->io_space->start;
382 	ranges->mem_start -= hose->mem_space->start;
383 	ranges->mem_end -= hose->mem_space->start;
384 /* FIXME: On older alphas we could use dense memory space
385 	  to access prefetchable resources. */
386 	ranges->prefetch_start -= hose->mem_space->start;
387 	ranges->prefetch_end -= hose->mem_space->start;
388 }
389 
390 int
pcibios_enable_device(struct pci_dev * dev,int mask)391 pcibios_enable_device(struct pci_dev *dev, int mask)
392 {
393 	/* Nothing to do, since we enable all devices at startup.  */
394 	return 0;
395 }
396 
397 /*
398  *  If we set up a device for bus mastering, we need to check the latency
399  *  timer as certain firmware forgets to set it properly, as seen
400  *  on SX164 and LX164 with SRM.
401  */
402 void
pcibios_set_master(struct pci_dev * dev)403 pcibios_set_master(struct pci_dev *dev)
404 {
405 	u8 lat;
406 	pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
407 	if (lat >= 16) return;
408 	printk("PCI: Setting latency timer of device %s to 64\n",
409 							dev->slot_name);
410 	pci_write_config_byte(dev, PCI_LATENCY_TIMER, 64);
411 }
412 
413 static void __init
pcibios_claim_one_bus(struct pci_bus * b)414 pcibios_claim_one_bus(struct pci_bus *b)
415 {
416 	struct list_head *ld;
417 	struct pci_bus *child_bus;
418 
419 	for (ld = b->devices.next; ld != &b->devices; ld = ld->next) {
420 		struct pci_dev *dev = pci_dev_b(ld);
421 		int i;
422 
423 		for (i = 0; i < PCI_NUM_RESOURCES; i++) {
424 			struct resource *r = &dev->resource[i];
425 
426 			if (r->parent || !r->start || !r->flags)
427 				continue;
428 			pci_claim_resource(dev, i);
429 		}
430 	}
431 
432 	list_for_each_entry(child_bus, &b->children, node)
433 		pcibios_claim_one_bus(child_bus);
434 }
435 
436 static void __init
pcibios_claim_console_setup(void)437 pcibios_claim_console_setup(void)
438 {
439 	struct list_head *lb;
440 
441 	for(lb = pci_root_buses.next; lb != &pci_root_buses; lb = lb->next) {
442 		struct pci_bus *b = pci_bus_b(lb);
443 		pcibios_claim_one_bus(b);
444 	}
445 }
446 
447 void __init
common_init_pci(void)448 common_init_pci(void)
449 {
450 	struct pci_controller *hose;
451 	struct pci_bus *bus;
452 	int next_busno;
453 
454 	/* Scan all of the recorded PCI controllers.  */
455 	for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
456 		hose->first_busno = next_busno;
457 		hose->last_busno = 0xff;
458 		bus = pci_scan_bus(next_busno, alpha_mv.pci_ops, hose);
459 		hose->bus = bus;
460 		next_busno = hose->last_busno = bus->subordinate;
461 		next_busno += 1;
462 	}
463 
464 	if (pci_probe_only)
465 		pcibios_claim_console_setup();
466 	else	/* FIXME: `else' will be removed when
467 		   pci_assign_unassigned_resources() is able to work
468 		   correctly with [partially] allocated PCI tree. */
469 		pci_assign_unassigned_resources();
470 	pci_fixup_irqs(alpha_mv.pci_swizzle, alpha_mv.pci_map_irq);
471 }
472 
473 
474 struct pci_controller * __init
alloc_pci_controller(void)475 alloc_pci_controller(void)
476 {
477 	struct pci_controller *hose;
478 
479 	hose = alloc_bootmem(sizeof(*hose));
480 
481 	*hose_tail = hose;
482 	hose_tail = &hose->next;
483 
484 	return hose;
485 }
486 
487 struct resource * __init
alloc_resource(void)488 alloc_resource(void)
489 {
490 	struct resource *res;
491 
492 	res = alloc_bootmem(sizeof(*res));
493 
494 	return res;
495 }
496 
497 
498 /* Provide information on locations of various I/O regions in physical
499    memory.  Do this on a per-card basis so that we choose the right hose.  */
500 
501 asmlinkage long
sys_pciconfig_iobase(long which,unsigned long bus,unsigned long dfn)502 sys_pciconfig_iobase(long which, unsigned long bus, unsigned long dfn)
503 {
504 	struct pci_controller *hose;
505 	struct pci_dev *dev;
506 
507 	/* from hose or from bus.devfn */
508 	if (which & IOBASE_FROM_HOSE) {
509 		for(hose = hose_head; hose; hose = hose->next)
510 			if (hose->index == bus) break;
511 		if (!hose) return -ENODEV;
512 	} else {
513 		/* Special hook for ISA access.  */
514 		if (bus == 0 && dfn == 0) {
515 			hose = pci_isa_hose;
516 		} else {
517 			dev = pci_find_slot(bus, dfn);
518 			if (!dev)
519 				return -ENODEV;
520 			hose = dev->sysdata;
521 		}
522 	}
523 
524 	switch (which & ~IOBASE_FROM_HOSE) {
525 	case IOBASE_HOSE:
526 		return hose->index;
527 	case IOBASE_SPARSE_MEM:
528 		return hose->sparse_mem_base;
529 	case IOBASE_DENSE_MEM:
530 		return hose->dense_mem_base;
531 	case IOBASE_SPARSE_IO:
532 		return hose->sparse_io_base;
533 	case IOBASE_DENSE_IO:
534 		return hose->dense_io_base;
535 	case IOBASE_ROOT_BUS:
536 		return hose->bus->number;
537 	}
538 
539 	return -EOPNOTSUPP;
540 }
541 
542 /* Return the index of the PCI controller for device PDEV. */
543 int
pci_controller_num(struct pci_dev * pdev)544 pci_controller_num(struct pci_dev *pdev)
545 {
546         struct pci_controller *hose = pdev->sysdata;
547 	return (hose ? hose->index : -ENXIO);
548 }
549