core_titan.c 20 KB

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