xref: /openbmc/linux/arch/alpha/kernel/core_titan.c (revision 8cb5d748)
1 /*
2  *	linux/arch/alpha/kernel/core_titan.c
3  *
4  * Code common to all TITAN core logic chips.
5  */
6 
7 #define __EXTERN_INLINE inline
8 #include <asm/io.h>
9 #include <asm/core_titan.h>
10 #undef __EXTERN_INLINE
11 
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/pci.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
17 #include <linux/vmalloc.h>
18 #include <linux/bootmem.h>
19 
20 #include <asm/ptrace.h>
21 #include <asm/smp.h>
22 #include <asm/pgalloc.h>
23 #include <asm/tlbflush.h>
24 #include <asm/vga.h>
25 
26 #include "proto.h"
27 #include "pci_impl.h"
28 
29 /* Save Titan configuration data as the console had it set up.  */
30 
31 struct
32 {
33 	unsigned long wsba[4];
34 	unsigned long wsm[4];
35 	unsigned long tba[4];
36 } saved_config[4] __attribute__((common));
37 
38 /*
39  * Is PChip 1 present? No need to query it more than once.
40  */
41 static int titan_pchip1_present;
42 
43 /*
44  * BIOS32-style PCI interface:
45  */
46 
47 #define DEBUG_CONFIG 0
48 
49 #if DEBUG_CONFIG
50 # define DBG_CFG(args)	printk args
51 #else
52 # define DBG_CFG(args)
53 #endif
54 
55 
56 /*
57  * Routines to access TIG registers.
58  */
59 static inline volatile unsigned long *
60 mk_tig_addr(int offset)
61 {
62 	return (volatile unsigned long *)(TITAN_TIG_SPACE + (offset << 6));
63 }
64 
65 static inline u8
66 titan_read_tig(int offset, u8 value)
67 {
68 	volatile unsigned long *tig_addr = mk_tig_addr(offset);
69 	return (u8)(*tig_addr & 0xff);
70 }
71 
72 static inline void
73 titan_write_tig(int offset, u8 value)
74 {
75 	volatile unsigned long *tig_addr = mk_tig_addr(offset);
76 	*tig_addr = (unsigned long)value;
77 }
78 
79 
80 /*
81  * Given a bus, device, and function number, compute resulting
82  * configuration space address
83  * accordingly.  It is therefore not safe to have concurrent
84  * invocations to configuration space access routines, but there
85  * really shouldn't be any need for this.
86  *
87  * Note that all config space accesses use Type 1 address format.
88  *
89  * Note also that type 1 is determined by non-zero bus number.
90  *
91  * Type 1:
92  *
93  *  3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
94  *  3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
95  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96  * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
97  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98  *
99  *	31:24	reserved
100  *	23:16	bus number (8 bits = 128 possible buses)
101  *	15:11	Device number (5 bits)
102  *	10:8	function number
103  *	 7:2	register number
104  *
105  * Notes:
106  *	The function number selects which function of a multi-function device
107  *	(e.g., SCSI and Ethernet).
108  *
109  *	The register selects a DWORD (32 bit) register offset.  Hence it
110  *	doesn't get shifted by 2 bits as we want to "drop" the bottom two
111  *	bits.
112  */
113 
114 static int
115 mk_conf_addr(struct pci_bus *pbus, unsigned int device_fn, int where,
116 	     unsigned long *pci_addr, unsigned char *type1)
117 {
118 	struct pci_controller *hose = pbus->sysdata;
119 	unsigned long addr;
120 	u8 bus = pbus->number;
121 
122 	DBG_CFG(("mk_conf_addr(bus=%d ,device_fn=0x%x, where=0x%x, "
123 		 "pci_addr=0x%p, type1=0x%p)\n",
124 		 bus, device_fn, where, pci_addr, type1));
125 
126 	if (!pbus->parent) /* No parent means peer PCI bus. */
127 		bus = 0;
128         *type1 = (bus != 0);
129 
130         addr = (bus << 16) | (device_fn << 8) | where;
131 	addr |= hose->config_space_base;
132 
133 	*pci_addr = addr;
134 	DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
135 	return 0;
136 }
137 
138 static int
139 titan_read_config(struct pci_bus *bus, unsigned int devfn, int where,
140 		  int size, u32 *value)
141 {
142 	unsigned long addr;
143 	unsigned char type1;
144 
145 	if (mk_conf_addr(bus, devfn, where, &addr, &type1))
146 		return PCIBIOS_DEVICE_NOT_FOUND;
147 
148 	switch (size) {
149 	case 1:
150 		*value = __kernel_ldbu(*(vucp)addr);
151 		break;
152 	case 2:
153 		*value = __kernel_ldwu(*(vusp)addr);
154 		break;
155 	case 4:
156 		*value = *(vuip)addr;
157 		break;
158 	}
159 
160 	return PCIBIOS_SUCCESSFUL;
161 }
162 
163 static int
164 titan_write_config(struct pci_bus *bus, unsigned int devfn, int where,
165 		   int size, u32 value)
166 {
167 	unsigned long addr;
168 	unsigned char type1;
169 
170 	if (mk_conf_addr(bus, devfn, where, &addr, &type1))
171 		return PCIBIOS_DEVICE_NOT_FOUND;
172 
173 	switch (size) {
174 	case 1:
175 		__kernel_stb(value, *(vucp)addr);
176 		mb();
177 		__kernel_ldbu(*(vucp)addr);
178 		break;
179 	case 2:
180 		__kernel_stw(value, *(vusp)addr);
181 		mb();
182 		__kernel_ldwu(*(vusp)addr);
183 		break;
184 	case 4:
185 		*(vuip)addr = value;
186 		mb();
187 		*(vuip)addr;
188 		break;
189 	}
190 
191 	return PCIBIOS_SUCCESSFUL;
192 }
193 
194 struct pci_ops titan_pci_ops =
195 {
196 	.read =		titan_read_config,
197 	.write =	titan_write_config,
198 };
199 
200 
201 void
202 titan_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
203 {
204 	titan_pachip *pachip =
205 	  (hose->index & 1) ? TITAN_pachip1 : TITAN_pachip0;
206 	titan_pachip_port *port;
207 	volatile unsigned long *csr;
208 	unsigned long value;
209 
210 	/* Get the right hose.  */
211 	port = &pachip->g_port;
212 	if (hose->index & 2)
213 		port = &pachip->a_port;
214 
215 	/* We can invalidate up to 8 tlb entries in a go.  The flush
216 	   matches against <31:16> in the pci address.
217 	   Note that gtlbi* and atlbi* are in the same place in the g_port
218 	   and a_port, respectively, so the g_port offset can be used
219 	   even if hose is an a_port */
220 	csr = &port->port_specific.g.gtlbia.csr;
221 	if (((start ^ end) & 0xffff0000) == 0)
222 		csr = &port->port_specific.g.gtlbiv.csr;
223 
224 	/* For TBIA, it doesn't matter what value we write.  For TBI,
225 	   it's the shifted tag bits.  */
226 	value = (start & 0xffff0000) >> 12;
227 
228 	wmb();
229 	*csr = value;
230 	mb();
231 	*csr;
232 }
233 
234 static int
235 titan_query_agp(titan_pachip_port *port)
236 {
237 	union TPAchipPCTL pctl;
238 
239 	/* set up APCTL */
240 	pctl.pctl_q_whole = port->pctl.csr;
241 
242 	return pctl.pctl_r_bits.apctl_v_agp_present;
243 
244 }
245 
246 static void __init
247 titan_init_one_pachip_port(titan_pachip_port *port, int index)
248 {
249 	struct pci_controller *hose;
250 
251 	hose = alloc_pci_controller();
252 	if (index == 0)
253 		pci_isa_hose = hose;
254 	hose->io_space = alloc_resource();
255 	hose->mem_space = alloc_resource();
256 
257 	/*
258 	 * This is for userland consumption.  The 40-bit PIO bias that we
259 	 * use in the kernel through KSEG doesn't work in the page table
260 	 * based user mappings. (43-bit KSEG sign extends the physical
261 	 * address from bit 40 to hit the I/O bit - mapped addresses don't).
262 	 * So make sure we get the 43-bit PIO bias.
263 	 */
264 	hose->sparse_mem_base = 0;
265 	hose->sparse_io_base = 0;
266 	hose->dense_mem_base
267 	  = (TITAN_MEM(index) & 0xffffffffffUL) | 0x80000000000UL;
268 	hose->dense_io_base
269 	  = (TITAN_IO(index) & 0xffffffffffUL) | 0x80000000000UL;
270 
271 	hose->config_space_base = TITAN_CONF(index);
272 	hose->index = index;
273 
274 	hose->io_space->start = TITAN_IO(index) - TITAN_IO_BIAS;
275 	hose->io_space->end = hose->io_space->start + TITAN_IO_SPACE - 1;
276 	hose->io_space->name = pci_io_names[index];
277 	hose->io_space->flags = IORESOURCE_IO;
278 
279 	hose->mem_space->start = TITAN_MEM(index) - TITAN_MEM_BIAS;
280 	hose->mem_space->end = hose->mem_space->start + 0xffffffff;
281 	hose->mem_space->name = pci_mem_names[index];
282 	hose->mem_space->flags = IORESOURCE_MEM;
283 
284 	if (request_resource(&ioport_resource, hose->io_space) < 0)
285 		printk(KERN_ERR "Failed to request IO on hose %d\n", index);
286 	if (request_resource(&iomem_resource, hose->mem_space) < 0)
287 		printk(KERN_ERR "Failed to request MEM on hose %d\n", index);
288 
289 	/*
290 	 * Save the existing PCI window translations.  SRM will
291 	 * need them when we go to reboot.
292 	 */
293 	saved_config[index].wsba[0] = port->wsba[0].csr;
294 	saved_config[index].wsm[0]  = port->wsm[0].csr;
295 	saved_config[index].tba[0]  = port->tba[0].csr;
296 
297 	saved_config[index].wsba[1] = port->wsba[1].csr;
298 	saved_config[index].wsm[1]  = port->wsm[1].csr;
299 	saved_config[index].tba[1]  = port->tba[1].csr;
300 
301 	saved_config[index].wsba[2] = port->wsba[2].csr;
302 	saved_config[index].wsm[2]  = port->wsm[2].csr;
303 	saved_config[index].tba[2]  = port->tba[2].csr;
304 
305 	saved_config[index].wsba[3] = port->wsba[3].csr;
306 	saved_config[index].wsm[3]  = port->wsm[3].csr;
307 	saved_config[index].tba[3]  = port->tba[3].csr;
308 
309 	/*
310 	 * Set up the PCI to main memory translation windows.
311 	 *
312 	 * Note: Window 3 on Titan is Scatter-Gather ONLY.
313 	 *
314 	 * Window 0 is scatter-gather 8MB at 8MB (for isa)
315 	 * Window 1 is direct access 1GB at 2GB
316 	 * Window 2 is scatter-gather 1GB at 3GB
317 	 */
318 	hose->sg_isa = iommu_arena_new(hose, 0x00800000, 0x00800000, 0);
319 	hose->sg_isa->align_entry = 8; /* 64KB for ISA */
320 
321 	hose->sg_pci = iommu_arena_new(hose, 0xc0000000, 0x40000000, 0);
322 	hose->sg_pci->align_entry = 4; /* Titan caches 4 PTEs at a time */
323 
324 	port->wsba[0].csr = hose->sg_isa->dma_base | 3;
325 	port->wsm[0].csr  = (hose->sg_isa->size - 1) & 0xfff00000;
326 	port->tba[0].csr  = virt_to_phys(hose->sg_isa->ptes);
327 
328 	port->wsba[1].csr = __direct_map_base | 1;
329 	port->wsm[1].csr  = (__direct_map_size - 1) & 0xfff00000;
330 	port->tba[1].csr  = 0;
331 
332 	port->wsba[2].csr = hose->sg_pci->dma_base | 3;
333 	port->wsm[2].csr  = (hose->sg_pci->size - 1) & 0xfff00000;
334 	port->tba[2].csr  = virt_to_phys(hose->sg_pci->ptes);
335 
336 	port->wsba[3].csr = 0;
337 
338 	/* Enable the Monster Window to make DAC pci64 possible.  */
339 	port->pctl.csr |= pctl_m_mwin;
340 
341 	/*
342 	 * If it's an AGP port, initialize agplastwr.
343 	 */
344 	if (titan_query_agp(port))
345 		port->port_specific.a.agplastwr.csr = __direct_map_base;
346 
347 	titan_pci_tbi(hose, 0, -1);
348 }
349 
350 static void __init
351 titan_init_pachips(titan_pachip *pachip0, titan_pachip *pachip1)
352 {
353 	titan_pchip1_present = TITAN_cchip->csc.csr & 1L<<14;
354 
355 	/* Init the ports in hose order... */
356 	titan_init_one_pachip_port(&pachip0->g_port, 0);	/* hose 0 */
357 	if (titan_pchip1_present)
358 		titan_init_one_pachip_port(&pachip1->g_port, 1);/* hose 1 */
359 	titan_init_one_pachip_port(&pachip0->a_port, 2);	/* hose 2 */
360 	if (titan_pchip1_present)
361 		titan_init_one_pachip_port(&pachip1->a_port, 3);/* hose 3 */
362 }
363 
364 void __init
365 titan_init_arch(void)
366 {
367 #if 0
368 	printk("%s: titan_init_arch()\n", __func__);
369 	printk("%s: CChip registers:\n", __func__);
370 	printk("%s: CSR_CSC 0x%lx\n", __func__, TITAN_cchip->csc.csr);
371 	printk("%s: CSR_MTR 0x%lx\n", __func__, TITAN_cchip->mtr.csr);
372 	printk("%s: CSR_MISC 0x%lx\n", __func__, TITAN_cchip->misc.csr);
373 	printk("%s: CSR_DIM0 0x%lx\n", __func__, TITAN_cchip->dim0.csr);
374 	printk("%s: CSR_DIM1 0x%lx\n", __func__, TITAN_cchip->dim1.csr);
375 	printk("%s: CSR_DIR0 0x%lx\n", __func__, TITAN_cchip->dir0.csr);
376 	printk("%s: CSR_DIR1 0x%lx\n", __func__, TITAN_cchip->dir1.csr);
377 	printk("%s: CSR_DRIR 0x%lx\n", __func__, TITAN_cchip->drir.csr);
378 
379 	printk("%s: DChip registers:\n", __func__);
380 	printk("%s: CSR_DSC 0x%lx\n", __func__, TITAN_dchip->dsc.csr);
381 	printk("%s: CSR_STR 0x%lx\n", __func__, TITAN_dchip->str.csr);
382 	printk("%s: CSR_DREV 0x%lx\n", __func__, TITAN_dchip->drev.csr);
383 #endif
384 
385 	boot_cpuid = __hard_smp_processor_id();
386 
387 	/* With multiple PCI busses, we play with I/O as physical addrs.  */
388 	ioport_resource.end = ~0UL;
389 	iomem_resource.end = ~0UL;
390 
391 	/* PCI DMA Direct Mapping is 1GB at 2GB.  */
392 	__direct_map_base = 0x80000000;
393 	__direct_map_size = 0x40000000;
394 
395 	/* Init the PA chip(s).  */
396 	titan_init_pachips(TITAN_pachip0, TITAN_pachip1);
397 
398 	/* Check for graphic console location (if any).  */
399 	find_console_vga_hose();
400 }
401 
402 static void
403 titan_kill_one_pachip_port(titan_pachip_port *port, int index)
404 {
405 	port->wsba[0].csr = saved_config[index].wsba[0];
406 	port->wsm[0].csr  = saved_config[index].wsm[0];
407 	port->tba[0].csr  = saved_config[index].tba[0];
408 
409 	port->wsba[1].csr = saved_config[index].wsba[1];
410 	port->wsm[1].csr  = saved_config[index].wsm[1];
411 	port->tba[1].csr  = saved_config[index].tba[1];
412 
413 	port->wsba[2].csr = saved_config[index].wsba[2];
414 	port->wsm[2].csr  = saved_config[index].wsm[2];
415 	port->tba[2].csr  = saved_config[index].tba[2];
416 
417 	port->wsba[3].csr = saved_config[index].wsba[3];
418 	port->wsm[3].csr  = saved_config[index].wsm[3];
419 	port->tba[3].csr  = saved_config[index].tba[3];
420 }
421 
422 static void
423 titan_kill_pachips(titan_pachip *pachip0, titan_pachip *pachip1)
424 {
425 	if (titan_pchip1_present) {
426 		titan_kill_one_pachip_port(&pachip1->g_port, 1);
427 		titan_kill_one_pachip_port(&pachip1->a_port, 3);
428 	}
429 	titan_kill_one_pachip_port(&pachip0->g_port, 0);
430 	titan_kill_one_pachip_port(&pachip0->a_port, 2);
431 }
432 
433 void
434 titan_kill_arch(int mode)
435 {
436 	titan_kill_pachips(TITAN_pachip0, TITAN_pachip1);
437 }
438 
439 
440 /*
441  * IO map support.
442  */
443 
444 void __iomem *
445 titan_ioportmap(unsigned long addr)
446 {
447 	FIXUP_IOADDR_VGA(addr);
448 	return (void __iomem *)(addr + TITAN_IO_BIAS);
449 }
450 
451 
452 void __iomem *
453 titan_ioremap(unsigned long addr, unsigned long size)
454 {
455 	int h = (addr & TITAN_HOSE_MASK) >> TITAN_HOSE_SHIFT;
456 	unsigned long baddr = addr & ~TITAN_HOSE_MASK;
457 	unsigned long last = baddr + size - 1;
458 	struct pci_controller *hose;
459 	struct vm_struct *area;
460 	unsigned long vaddr;
461 	unsigned long *ptes;
462 	unsigned long pfn;
463 
464 #ifdef CONFIG_VGA_HOSE
465 	/*
466 	 * Adjust the address and hose, if necessary.
467 	 */
468 	if (pci_vga_hose && __is_mem_vga(addr)) {
469 		h = pci_vga_hose->index;
470 		addr += pci_vga_hose->mem_space->start;
471 	}
472 #endif
473 
474 	/*
475 	 * Find the hose.
476 	 */
477 	for (hose = hose_head; hose; hose = hose->next)
478 		if (hose->index == h)
479 			break;
480 	if (!hose)
481 		return NULL;
482 
483 	/*
484 	 * Is it direct-mapped?
485 	 */
486 	if ((baddr >= __direct_map_base) &&
487 	    ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
488 		vaddr = addr - __direct_map_base + TITAN_MEM_BIAS;
489 		return (void __iomem *) vaddr;
490 	}
491 
492 	/*
493 	 * Check the scatter-gather arena.
494 	 */
495 	if (hose->sg_pci &&
496 	    baddr >= (unsigned long)hose->sg_pci->dma_base &&
497 	    last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size){
498 
499 		/*
500 		 * Adjust the limits (mappings must be page aligned)
501 		 */
502 		baddr -= hose->sg_pci->dma_base;
503 		last -= hose->sg_pci->dma_base;
504 		baddr &= PAGE_MASK;
505 		size = PAGE_ALIGN(last) - baddr;
506 
507 		/*
508 		 * Map it
509 		 */
510 		area = get_vm_area(size, VM_IOREMAP);
511 		if (!area) {
512 			printk("ioremap failed... no vm_area...\n");
513 			return NULL;
514 		}
515 
516 		ptes = hose->sg_pci->ptes;
517 		for (vaddr = (unsigned long)area->addr;
518 		    baddr <= last;
519 		    baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
520 			pfn = ptes[baddr >> PAGE_SHIFT];
521 			if (!(pfn & 1)) {
522 				printk("ioremap failed... pte not valid...\n");
523 				vfree(area->addr);
524 				return NULL;
525 			}
526 			pfn >>= 1;	/* make it a true pfn */
527 
528 			if (__alpha_remap_area_pages(vaddr,
529 						     pfn << PAGE_SHIFT,
530 						     PAGE_SIZE, 0)) {
531 				printk("FAILED to remap_area_pages...\n");
532 				vfree(area->addr);
533 				return NULL;
534 			}
535 		}
536 
537 		flush_tlb_all();
538 
539 		vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
540 		return (void __iomem *) vaddr;
541 	}
542 
543 	/* Assume a legacy (read: VGA) address, and return appropriately. */
544 	return (void __iomem *)(addr + TITAN_MEM_BIAS);
545 }
546 
547 void
548 titan_iounmap(volatile void __iomem *xaddr)
549 {
550 	unsigned long addr = (unsigned long) xaddr;
551 	if (addr >= VMALLOC_START)
552 		vfree((void *)(PAGE_MASK & addr));
553 }
554 
555 int
556 titan_is_mmio(const volatile void __iomem *xaddr)
557 {
558 	unsigned long addr = (unsigned long) xaddr;
559 
560 	if (addr >= VMALLOC_START)
561 		return 1;
562 	else
563 		return (addr & 0x100000000UL) == 0;
564 }
565 
566 #ifndef CONFIG_ALPHA_GENERIC
567 EXPORT_SYMBOL(titan_ioportmap);
568 EXPORT_SYMBOL(titan_ioremap);
569 EXPORT_SYMBOL(titan_iounmap);
570 EXPORT_SYMBOL(titan_is_mmio);
571 #endif
572 
573 /*
574  * AGP GART Support.
575  */
576 #include <linux/agp_backend.h>
577 #include <asm/agp_backend.h>
578 #include <linux/slab.h>
579 #include <linux/delay.h>
580 
581 struct titan_agp_aperture {
582 	struct pci_iommu_arena *arena;
583 	long pg_start;
584 	long pg_count;
585 };
586 
587 static int
588 titan_agp_setup(alpha_agp_info *agp)
589 {
590 	struct titan_agp_aperture *aper;
591 
592 	if (!alpha_agpgart_size)
593 		return -ENOMEM;
594 
595 	aper = kmalloc(sizeof(struct titan_agp_aperture), GFP_KERNEL);
596 	if (aper == NULL)
597 		return -ENOMEM;
598 
599 	aper->arena = agp->hose->sg_pci;
600 	aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
601 	aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
602 				       aper->pg_count - 1);
603 	if (aper->pg_start < 0) {
604 		printk(KERN_ERR "Failed to reserve AGP memory\n");
605 		kfree(aper);
606 		return -ENOMEM;
607 	}
608 
609 	agp->aperture.bus_base =
610 		aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
611 	agp->aperture.size = aper->pg_count * PAGE_SIZE;
612 	agp->aperture.sysdata = aper;
613 
614 	return 0;
615 }
616 
617 static void
618 titan_agp_cleanup(alpha_agp_info *agp)
619 {
620 	struct titan_agp_aperture *aper = agp->aperture.sysdata;
621 	int status;
622 
623 	status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
624 	if (status == -EBUSY) {
625 		printk(KERN_WARNING
626 		       "Attempted to release bound AGP memory - unbinding\n");
627 		iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
628 		status = iommu_release(aper->arena, aper->pg_start,
629 				       aper->pg_count);
630 	}
631 	if (status < 0)
632 		printk(KERN_ERR "Failed to release AGP memory\n");
633 
634 	kfree(aper);
635 	kfree(agp);
636 }
637 
638 static int
639 titan_agp_configure(alpha_agp_info *agp)
640 {
641 	union TPAchipPCTL pctl;
642 	titan_pachip_port *port = agp->private;
643 	pctl.pctl_q_whole = port->pctl.csr;
644 
645 	/* Side-Band Addressing? */
646 	pctl.pctl_r_bits.apctl_v_agp_sba_en = agp->mode.bits.sba;
647 
648 	/* AGP Rate? */
649 	pctl.pctl_r_bits.apctl_v_agp_rate = 0;		/* 1x */
650 	if (agp->mode.bits.rate & 2)
651 		pctl.pctl_r_bits.apctl_v_agp_rate = 1;	/* 2x */
652 #if 0
653 	if (agp->mode.bits.rate & 4)
654 		pctl.pctl_r_bits.apctl_v_agp_rate = 2;	/* 4x */
655 #endif
656 
657 	/* RQ Depth? */
658 	pctl.pctl_r_bits.apctl_v_agp_hp_rd = 2;
659 	pctl.pctl_r_bits.apctl_v_agp_lp_rd = 7;
660 
661 	/*
662 	 * AGP Enable.
663 	 */
664 	pctl.pctl_r_bits.apctl_v_agp_en = agp->mode.bits.enable;
665 
666 	/* Tell the user.  */
667 	printk("Enabling AGP: %dX%s\n",
668 	       1 << pctl.pctl_r_bits.apctl_v_agp_rate,
669 	       pctl.pctl_r_bits.apctl_v_agp_sba_en ? " - SBA" : "");
670 
671 	/* Write it.  */
672 	port->pctl.csr = pctl.pctl_q_whole;
673 
674 	/* And wait at least 5000 66MHz cycles (per Titan spec).  */
675 	udelay(100);
676 
677 	return 0;
678 }
679 
680 static int
681 titan_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
682 {
683 	struct titan_agp_aperture *aper = agp->aperture.sysdata;
684 	return iommu_bind(aper->arena, aper->pg_start + pg_start,
685 			  mem->page_count, mem->pages);
686 }
687 
688 static int
689 titan_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
690 {
691 	struct titan_agp_aperture *aper = agp->aperture.sysdata;
692 	return iommu_unbind(aper->arena, aper->pg_start + pg_start,
693 			    mem->page_count);
694 }
695 
696 static unsigned long
697 titan_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
698 {
699 	struct titan_agp_aperture *aper = agp->aperture.sysdata;
700 	unsigned long baddr = addr - aper->arena->dma_base;
701 	unsigned long pte;
702 
703 	if (addr < agp->aperture.bus_base ||
704 	    addr >= agp->aperture.bus_base + agp->aperture.size) {
705 		printk("%s: addr out of range\n", __func__);
706 		return -EINVAL;
707 	}
708 
709 	pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
710 	if (!(pte & 1)) {
711 		printk("%s: pte not valid\n", __func__);
712 		return -EINVAL;
713 	}
714 
715 	return (pte >> 1) << PAGE_SHIFT;
716 }
717 
718 struct alpha_agp_ops titan_agp_ops =
719 {
720 	.setup		= titan_agp_setup,
721 	.cleanup	= titan_agp_cleanup,
722 	.configure	= titan_agp_configure,
723 	.bind		= titan_agp_bind_memory,
724 	.unbind		= titan_agp_unbind_memory,
725 	.translate	= titan_agp_translate
726 };
727 
728 alpha_agp_info *
729 titan_agp_info(void)
730 {
731 	alpha_agp_info *agp;
732 	struct pci_controller *hose;
733 	titan_pachip_port *port;
734 	int hosenum = -1;
735 	union TPAchipPCTL pctl;
736 
737 	/*
738 	 * Find the AGP port.
739 	 */
740 	port = &TITAN_pachip0->a_port;
741 	if (titan_query_agp(port))
742 		hosenum = 2;
743 	if (hosenum < 0 &&
744 	    titan_pchip1_present &&
745 	    titan_query_agp(port = &TITAN_pachip1->a_port))
746 		hosenum = 3;
747 
748 	/*
749 	 * Find the hose the port is on.
750 	 */
751 	for (hose = hose_head; hose; hose = hose->next)
752 		if (hose->index == hosenum)
753 			break;
754 
755 	if (!hose || !hose->sg_pci)
756 		return NULL;
757 
758 	/*
759 	 * Allocate the info structure.
760 	 */
761 	agp = kmalloc(sizeof(*agp), GFP_KERNEL);
762 	if (!agp)
763 		return NULL;
764 
765 	/*
766 	 * Fill it in.
767 	 */
768 	agp->hose = hose;
769 	agp->private = port;
770 	agp->ops = &titan_agp_ops;
771 
772 	/*
773 	 * Aperture - not configured until ops.setup().
774 	 *
775 	 * FIXME - should we go ahead and allocate it here?
776 	 */
777 	agp->aperture.bus_base = 0;
778 	agp->aperture.size = 0;
779 	agp->aperture.sysdata = NULL;
780 
781 	/*
782 	 * Capabilities.
783 	 */
784 	agp->capability.lw = 0;
785 	agp->capability.bits.rate = 3; 	/* 2x, 1x */
786 	agp->capability.bits.sba = 1;
787 	agp->capability.bits.rq = 7;	/* 8 - 1 */
788 
789 	/*
790 	 * Mode.
791 	 */
792 	pctl.pctl_q_whole = port->pctl.csr;
793 	agp->mode.lw = 0;
794 	agp->mode.bits.rate = 1 << pctl.pctl_r_bits.apctl_v_agp_rate;
795 	agp->mode.bits.sba = pctl.pctl_r_bits.apctl_v_agp_sba_en;
796 	agp->mode.bits.rq = 7;	/* RQ Depth? */
797 	agp->mode.bits.enable = pctl.pctl_r_bits.apctl_v_agp_en;
798 
799 	return agp;
800 }
801