core_titan.c 20 KB

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