memoryCall.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /*
  2. * linux/arch/alpha/kernel/core_marvel.c
  3. *
  4. * Code common to all Marvel based systems.
  5. */
  6. #define __EXTERN_INLINE inline
  7. #include <asm/io.h>
  8. #include <asm/core_marvel.h>
  9. #undef __EXTERN_INLINE
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mc146818rtc.h>
  16. #include <linux/rtc.h>
  17. #include <linux/module.h>
  18. #include <linux/bootmem.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/smp.h>
  21. #include <asm/gct.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/rtc.h>
  25. #include <asm/vga.h>
  26. #include "proto.h"
  27. #include "pci_impl.h"
  28. /*
  29. * Debug helpers
  30. */
  31. #define DEBUG_CONFIG 0
  32. #if DEBUG_CONFIG
  33. # define DBG_CFG(args) printk args
  34. #else
  35. # define DBG_CFG(args)
  36. #endif
  37. /*
  38. * Private data
  39. */
  40. static struct io7 *io7_head = NULL;
  41. /*
  42. * Helper functions
  43. */
  44. static unsigned long __attribute__ ((unused))
  45. read_ev7_csr(int pe, unsigned long offset)
  46. {
  47. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  48. unsigned long q;
  49. mb();
  50. q = ev7csr->csr;
  51. mb();
  52. return q;
  53. }
  54. static void __attribute__ ((unused))
  55. write_ev7_csr(int pe, unsigned long offset, unsigned long q)
  56. {
  57. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  58. mb();
  59. ev7csr->csr = q;
  60. mb();
  61. }
  62. static char * __init
  63. mk_resource_name(int pe, int port, char *str)
  64. {
  65. char tmp[80];
  66. char *name;
  67. sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
  68. name = alloc_bootmem(strlen(tmp) + 1);
  69. strcpy(name, tmp);
  70. return name;
  71. }
  72. inline struct io7 *
  73. marvel_next_io7(struct io7 *prev)
  74. {
  75. return (prev ? prev->next : io7_head);
  76. }
  77. struct io7 *
  78. marvel_find_io7(int pe)
  79. {
  80. struct io7 *io7;
  81. for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
  82. continue;
  83. return io7;
  84. }
  85. static struct io7 * __init
  86. alloc_io7(unsigned int pe)
  87. {
  88. struct io7 *io7;
  89. struct io7 *insp;
  90. int h;
  91. if (marvel_find_io7(pe)) {
  92. printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
  93. return NULL;
  94. }
  95. io7 = alloc_bootmem(sizeof(*io7));
  96. io7->pe = pe;
  97. spin_lock_init(&io7->irq_lock);
  98. for (h = 0; h < 4; h++) {
  99. io7->ports[h].io7 = io7;
  100. io7->ports[h].port = h;
  101. io7->ports[h].enabled = 0; /* default to disabled */
  102. }
  103. /*
  104. * Insert in pe sorted order.
  105. */
  106. if (NULL == io7_head) /* empty list */
  107. io7_head = io7;
  108. else if (io7_head->pe > io7->pe) { /* insert at head */
  109. io7->next = io7_head;
  110. io7_head = io7;
  111. } else { /* insert at position */
  112. for (insp = io7_head; insp; insp = insp->next) {
  113. if (insp->pe == io7->pe) {
  114. printk(KERN_ERR "Too many IO7s at PE %d\n",
  115. io7->pe);
  116. return NULL;
  117. }
  118. if (NULL == insp->next ||
  119. insp->next->pe > io7->pe) { /* insert here */
  120. io7->next = insp->next;
  121. insp->next = io7;
  122. break;
  123. }
  124. }
  125. if (NULL == insp) { /* couldn't insert ?!? */
  126. printk(KERN_WARNING "Failed to insert IO7 at PE %d "
  127. " - adding at head of list\n", io7->pe);
  128. io7->next = io7_head;
  129. io7_head = io7;
  130. }
  131. }
  132. return io7;
  133. }
  134. void
  135. io7_clear_errors(struct io7 *io7)
  136. {
  137. io7_port7_csrs *p7csrs;
  138. io7_ioport_csrs *csrs;
  139. int port;
  140. /*
  141. * First the IO ports.
  142. */
  143. for (port = 0; port < 4; port++) {
  144. csrs = IO7_CSRS_KERN(io7->pe, port);
  145. csrs->POx_ERR_SUM.csr = -1UL;
  146. csrs->POx_TLB_ERR.csr = -1UL;
  147. csrs->POx_SPL_COMPLT.csr = -1UL;
  148. csrs->POx_TRANS_SUM.csr = -1UL;
  149. }
  150. /*
  151. * Then the common ones.
  152. */
  153. p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  154. p7csrs->PO7_ERROR_SUM.csr = -1UL;
  155. p7csrs->PO7_UNCRR_SYM.csr = -1UL;
  156. p7csrs->PO7_CRRCT_SYM.csr = -1UL;
  157. }
  158. /*
  159. * IO7 PCI, PCI/X, AGP configuration.
  160. */
  161. static void __init
  162. io7_init_hose(struct io7 *io7, int port)
  163. {
  164. static int hose_index = 0;
  165. struct pci_controller *hose = alloc_pci_controller();
  166. struct io7_port *io7_port = &io7->ports[port];
  167. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
  168. int i;
  169. hose->index = hose_index++; /* arbitrary */
  170. /*
  171. * We don't have an isa or legacy hose, but glibc expects to be
  172. * able to use the bus == 0 / dev == 0 form of the iobase syscall
  173. * to determine information about the i/o system. Since XFree86
  174. * relies on glibc's determination to tell whether or not to use
  175. * sparse access, we need to point the pci_isa_hose at a real hose
  176. * so at least that determination is correct.
  177. */
  178. if (hose->index == 0)
  179. pci_isa_hose = hose;
  180. io7_port->csrs = csrs;
  181. io7_port->hose = hose;
  182. hose->sysdata = io7_port;
  183. hose->io_space = alloc_resource();
  184. hose->mem_space = alloc_resource();
  185. /*
  186. * Base addresses for userland consumption. Since these are going
  187. * to be mapped, they are pure physical addresses.
  188. */
  189. hose->sparse_mem_base = hose->sparse_io_base = 0;
  190. hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
  191. hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
  192. /*
  193. * Base addresses and resource ranges for kernel consumption.
  194. */
  195. hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
  196. hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
  197. hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
  198. hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
  199. hose->io_space->flags = IORESOURCE_IO;
  200. hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
  201. hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
  202. hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
  203. hose->mem_space->flags = IORESOURCE_MEM;
  204. if (request_resource(&ioport_resource, hose->io_space) < 0)
  205. printk(KERN_ERR "Failed to request IO on hose %d\n",
  206. hose->index);
  207. if (request_resource(&iomem_resource, hose->mem_space) < 0)
  208. printk(KERN_ERR "Failed to request MEM on hose %d\n",
  209. hose->index);
  210. /*
  211. * Save the existing DMA window settings for later restoration.
  212. */
  213. for (i = 0; i < 4; i++) {
  214. io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
  215. io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
  216. io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
  217. }
  218. /*
  219. * Set up the PCI to main memory translation windows.
  220. *
  221. * Window 0 is scatter-gather 8MB at 8MB
  222. * Window 1 is direct access 1GB at 2GB
  223. * Window 2 is scatter-gather (up-to) 1GB at 3GB
  224. * Window 3 is disabled
  225. */
  226. /*
  227. * TBIA before modifying windows.
  228. */
  229. marvel_pci_tbi(hose, 0, -1);
  230. /*
  231. * Set up window 0 for scatter-gather 8MB at 8MB.
  232. */
  233. hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  234. hose, 0x00800000, 0x00800000, 0);
  235. hose->sg_isa->align_entry = 8; /* cache line boundary */
  236. csrs->POx_WBASE[0].csr =
  237. hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
  238. csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
  239. csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
  240. /*
  241. * Set up window 1 for direct-mapped 1GB at 2GB.
  242. */
  243. csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
  244. csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
  245. csrs->POx_TBASE[1].csr = 0;
  246. /*
  247. * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
  248. */
  249. hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  250. hose, 0xc0000000, 0x40000000, 0);
  251. hose->sg_pci->align_entry = 8; /* cache line boundary */
  252. csrs->POx_WBASE[2].csr =
  253. hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
  254. csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
  255. csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
  256. /*
  257. * Disable window 3.
  258. */
  259. csrs->POx_WBASE[3].csr = 0;
  260. /*
  261. * Make sure that the AGP Monster Window is disabled.
  262. */
  263. csrs->POx_CTRL.csr &= ~(1UL << 61);
  264. #if 1
  265. printk("FIXME: disabling master aborts\n");
  266. csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
  267. #endif
  268. /*
  269. * TBIA after modifying windows.
  270. */
  271. marvel_pci_tbi(hose, 0, -1);
  272. }
  273. static void __init
  274. marvel_init_io7(struct io7 *io7)
  275. {
  276. int i;
  277. printk("Initializing IO7 at PID %d\n", io7->pe);
  278. /*
  279. * Get the Port 7 CSR pointer.
  280. */
  281. io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  282. /*
  283. * Init this IO7's hoses.
  284. */
  285. for (i = 0; i < IO7_NUM_PORTS; i++) {
  286. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
  287. if (csrs->POx_CACHE_CTL.csr == 8) {
  288. io7->ports[i].enabled = 1;
  289. io7_init_hose(io7, i);
  290. }
  291. }
  292. }
  293. void
  294. marvel_io7_present(gct6_node *node)
  295. {
  296. int pe;
  297. if (node->type != GCT_TYPE_HOSE ||
  298. node->subtype != GCT_SUBTYPE_IO_PORT_MODULE)
  299. return;
  300. pe = (node->id >> 8) & 0xff;
  301. printk("Found an IO7 at PID %d\n", pe);
  302. alloc_io7(pe);
  303. }
  304. static void __init
  305. marvel_find_console_vga_hose(void)
  306. {
  307. u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
  308. if (pu64[7] == 3) { /* TERM_TYPE == graphics */
  309. struct pci_controller *hose = NULL;
  310. int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
  311. struct io7 *io7;
  312. int pid, port;
  313. /* FIXME - encoding is going to have to change for Marvel
  314. * since hose will be able to overflow a byte...
  315. * need to fix this decode when the console
  316. * changes its encoding
  317. */
  318. printk("console graphics is on hose %d (console)\n", h);
  319. /*
  320. * The console's hose numbering is:
  321. *
  322. * hose<n:2>: PID
  323. * hose<1:0>: PORT
  324. *
  325. * We need to find the hose at that pid and port
  326. */
  327. pid = h >> 2;
  328. port = h & 3;
  329. if ((io7 = marvel_find_io7(pid)))
  330. hose = io7->ports[port].hose;
  331. if (hose) {
  332. printk("Console graphics on hose %d\n", hose->index);
  333. pci_vga_hose = hose;
  334. }
  335. }
  336. }
  337. gct6_search_struct gct_wanted_node_list[] = {
  338. { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
  339. { 0, 0, NULL }
  340. };
  341. /*
  342. * In case the GCT is not complete, let the user specify PIDs with IO7s
  343. * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
  344. * where IO7s are connected
  345. */
  346. static int __init
  347. marvel_specify_io7(char *str)
  348. {
  349. unsigned long pid;
  350. struct io7 *io7;
  351. char *pchar;
  352. do {
  353. pid = simple_strtoul(str, &pchar, 0);
  354. if (pchar != str) {
  355. printk("User-specified IO7 at PID %lu\n", pid);
  356. io7 = alloc_io7(pid);
  357. if (io7) marvel_init_io7(io7);
  358. }
  359. if (pchar == str) pchar++;
  360. str = pchar;
  361. } while(*str);
  362. return 1;
  363. }
  364. __setup("io7=", marvel_specify_io7);
  365. void __init
  366. marvel_init_arch(void)
  367. {
  368. struct io7 *io7;
  369. /* With multiple PCI busses, we play with I/O as physical addrs. */
  370. ioport_resource.end = ~0UL;
  371. /* PCI DMA Direct Mapping is 1GB at 2GB. */
  372. __direct_map_base = 0x80000000;
  373. __direct_map_size = 0x40000000;
  374. /* Parse the config tree. */
  375. gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
  376. /* Init the io7s. */
  377. for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); )
  378. marvel_init_io7(io7);
  379. /* Check for graphic console location (if any). */
  380. marvel_find_console_vga_hose();
  381. }
  382. void
  383. marvel_kill_arch(int mode)
  384. {
  385. }
  386. /*
  387. * PCI Configuration Space access functions
  388. *
  389. * Configuration space addresses have the following format:
  390. *
  391. * |2 2 2 2|1 1 1 1|1 1 1 1|1 1
  392. * |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  393. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  394. * |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
  395. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  396. *
  397. * n:24 reserved for hose base
  398. * 23:16 bus number (8 bits = 128 possible buses)
  399. * 15:11 Device number (5 bits)
  400. * 10:8 function number
  401. * 7:2 register number
  402. *
  403. * Notes:
  404. * IO7 determines whether to use a type 0 or type 1 config cycle
  405. * based on the bus number. Therefore the bus number must be set
  406. * to 0 for the root bus on any hose.
  407. *
  408. * The function number selects which function of a multi-function device
  409. * (e.g., SCSI and Ethernet).
  410. *
  411. */
  412. static inline unsigned long
  413. build_conf_addr(struct pci_controller *hose, u8 bus,
  414. unsigned int devfn, int where)
  415. {
  416. return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
  417. }
  418. static unsigned long
  419. mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
  420. {
  421. struct pci_controller *hose = pbus->sysdata;
  422. struct io7_port *io7_port;
  423. unsigned long addr = 0;
  424. u8 bus = pbus->number;
  425. if (!hose)
  426. return addr;
  427. /* Check for enabled. */
  428. io7_port = hose->sysdata;
  429. if (!io7_port->enabled)
  430. return addr;
  431. if (!pbus->parent) { /* No parent means peer PCI bus. */
  432. /* Don't support idsel > 20 on primary bus. */
  433. if (devfn >= PCI_DEVFN(21, 0))
  434. return addr;
  435. bus = 0;
  436. }
  437. addr = build_conf_addr(hose, bus, devfn, where);
  438. DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  439. return addr;
  440. }
  441. static int
  442. marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  443. int size, u32 *value)
  444. {
  445. unsigned long addr;
  446. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  447. return PCIBIOS_DEVICE_NOT_FOUND;
  448. switch(size) {
  449. case 1:
  450. *value = __kernel_ldbu(*(vucp)addr);
  451. break;
  452. case 2:
  453. *value = __kernel_ldwu(*(vusp)addr);
  454. break;
  455. case 4:
  456. *value = *(vuip)addr;
  457. break;
  458. default:
  459. return PCIBIOS_FUNC_NOT_SUPPORTED;
  460. }
  461. return PCIBIOS_SUCCESSFUL;
  462. }
  463. static int
  464. marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  465. int size, u32 value)
  466. {
  467. unsigned long addr;
  468. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  469. return PCIBIOS_DEVICE_NOT_FOUND;
  470. switch (size) {
  471. case 1:
  472. __kernel_stb(value, *(vucp)addr);
  473. mb();
  474. __kernel_ldbu(*(vucp)addr);
  475. break;
  476. case 2:
  477. __kernel_stw(value, *(vusp)addr);
  478. mb();
  479. __kernel_ldwu(*(vusp)addr);
  480. break;
  481. case 4:
  482. *(vuip)addr = value;
  483. mb();
  484. *(vuip)addr;
  485. break;
  486. default:
  487. return PCIBIOS_FUNC_NOT_SUPPORTED;
  488. }
  489. return PCIBIOS_SUCCESSFUL;
  490. }
  491. struct pci_ops marvel_pci_ops =
  492. {
  493. .read = marvel_read_config,
  494. .write = marvel_write_config,
  495. };
  496. /*
  497. * Other PCI helper functions.
  498. */
  499. void
  500. marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  501. {
  502. io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
  503. wmb();
  504. csrs->POx_SG_TBIA.csr = 0;
  505. mb();
  506. csrs->POx_SG_TBIA.csr;
  507. }
  508. /*
  509. * RTC Support
  510. */
  511. struct marvel_rtc_access_info {
  512. unsigned long function;
  513. unsigned long index;
  514. unsigned long data;
  515. };
  516. static void
  517. __marvel_access_rtc(void *info)
  518. {
  519. struct marvel_rtc_access_info *rtc_access = info;
  520. register unsigned long __r0 __asm__("$0");
  521. register unsigned long __r16 __asm__("$16") = rtc_access->function;
  522. register unsigned long __r17 __asm__("$17") = rtc_access->index;
  523. register unsigned long __r18 __asm__("$18") = rtc_access->data;
  524. __asm__ __volatile__(
  525. "call_pal %4 # cserve rtc"
  526. : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
  527. : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
  528. : "$1", "$22", "$23", "$24", "$25");
  529. rtc_access->data = __r0;
  530. }
  531. static u8
  532. __marvel_rtc_io(u8 b, unsigned long addr, int write)
  533. {
  534. static u8 index = 0;
  535. struct marvel_rtc_access_info rtc_access;
  536. u8 ret = 0;
  537. switch(addr) {
  538. case 0x70: /* RTC_PORT(0) */
  539. if (write) index = b;
  540. ret = index;
  541. break;
  542. case 0x71: /* RTC_PORT(1) */
  543. rtc_access.index = index;
  544. rtc_access.data = bcd2bin(b);
  545. rtc_access.function = 0x48 + !write; /* GET/PUT_TOY */
  546. __marvel_access_rtc(&rtc_access);
  547. ret = bin2bcd(rtc_access.data);
  548. break;
  549. default:
  550. printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
  551. break;
  552. }
  553. return ret;
  554. }
  555. /*
  556. * IO map support.
  557. */
  558. void __iomem *
  559. marvel_ioremap(unsigned long addr, unsigned long size)
  560. {
  561. struct pci_controller *hose;
  562. unsigned long baddr, last;
  563. struct vm_struct *area;
  564. unsigned long vaddr;
  565. unsigned long *ptes;
  566. unsigned long pfn;
  567. /*
  568. * Adjust the address.
  569. */
  570. FIXUP_MEMADDR_VGA(addr);
  571. /*
  572. * Find the hose.
  573. */
  574. for (hose = hose_head; hose; hose = hose->next) {
  575. if ((addr >> 32) == (hose->mem_space->start >> 32))
  576. break;
  577. }
  578. if (!hose)
  579. return NULL;
  580. /*
  581. * We have the hose - calculate the bus limits.
  582. */
  583. baddr = addr - hose->mem_space->start;
  584. last = baddr + size - 1;
  585. /*
  586. * Is it direct-mapped?
  587. */
  588. if ((baddr >= __direct_map_base) &&
  589. ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
  590. addr = IDENT_ADDR | (baddr - __direct_map_base);
  591. return (void __iomem *) addr;
  592. }
  593. /*
  594. * Check the scatter-gather arena.
  595. */
  596. if (hose->sg_pci &&
  597. baddr >= (unsigned long)hose->sg_pci->dma_base &&
  598. last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size) {
  599. /*
  600. * Adjust the limits (mappings must be page aligned)
  601. */
  602. baddr -= hose->sg_pci->dma_base;
  603. last -= hose->sg_pci->dma_base;
  604. baddr &= PAGE_MASK;
  605. size = PAGE_ALIGN(last) - baddr;
  606. /*
  607. * Map it.
  608. */
  609. area = get_vm_area(size, VM_IOREMAP);
  610. if (!area)
  611. return NULL;
  612. ptes = hose->sg_pci->ptes;
  613. for (vaddr = (unsigned long)area->addr;
  614. baddr <= last;
  615. baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
  616. pfn = ptes[baddr >> PAGE_SHIFT];
  617. if (!(pfn & 1)) {
  618. printk("ioremap failed... pte not valid...\n");
  619. vfree(area->addr);
  620. return NULL;
  621. }
  622. pfn >>= 1; /* make it a true pfn */
  623. if (__alpha_remap_area_pages(vaddr,
  624. pfn << PAGE_SHIFT,
  625. PAGE_SIZE, 0)) {
  626. printk("FAILED to map...\n");
  627. vfree(area->addr);
  628. return NULL;
  629. }
  630. }
  631. flush_tlb_all();
  632. vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
  633. return (void __iomem *) vaddr;
  634. }
  635. /* Assume it was already a reasonable address */
  636. vaddr = baddr + hose->mem_space->start;
  637. return (void __iomem *) vaddr;
  638. }
  639. void
  640. marvel_iounmap(volatile void __iomem *xaddr)
  641. {
  642. unsigned long addr = (unsigned long) xaddr;
  643. if (addr >= VMALLOC_START)
  644. vfree((void *)(PAGE_MASK & addr));
  645. }
  646. int
  647. marvel_is_mmio(const volatile void __iomem *xaddr)
  648. {
  649. unsigned long addr = (unsigned long) xaddr;
  650. if (addr >= VMALLOC_START)
  651. return 1;
  652. else
  653. return (addr & 0xFF000000UL) == 0;
  654. }
  655. #define __marvel_is_port_kbd(a) (((a) == 0x60) || ((a) == 0x64))
  656. #define __marvel_is_port_rtc(a) (((a) == 0x70) || ((a) == 0x71))
  657. void __iomem *marvel_ioportmap (unsigned long addr)
  658. {
  659. FIXUP_IOADDR_VGA(addr);
  660. return (void __iomem *)addr;
  661. }
  662. unsigned int
  663. marvel_ioread8(void __iomem *xaddr)
  664. {
  665. unsigned long addr = (unsigned long) xaddr;
  666. if (__marvel_is_port_kbd(addr))
  667. return 0;
  668. else if (__marvel_is_port_rtc(addr))
  669. return __marvel_rtc_io(0, addr, 0);
  670. else if (marvel_is_ioaddr(addr))
  671. return __kernel_ldbu(*(vucp)addr);
  672. else
  673. /* this should catch other legacy addresses
  674. that would normally fail on MARVEL,
  675. because there really is nothing there...
  676. */
  677. return ~0;
  678. }
  679. void
  680. marvel_iowrite8(u8 b, void __iomem *xaddr)
  681. {
  682. unsigned long addr = (unsigned long) xaddr;
  683. if (__marvel_is_port_kbd(addr))
  684. return;
  685. else if (__marvel_is_port_rtc(addr))
  686. __marvel_rtc_io(b, addr, 1);
  687. else if (marvel_is_ioaddr(addr))
  688. __kernel_stb(b, *(vucp)addr);
  689. }
  690. #ifndef CONFIG_ALPHA_GENERIC
  691. EXPORT_SYMBOL(marvel_ioremap);
  692. EXPORT_SYMBOL(marvel_iounmap);
  693. EXPORT_SYMBOL(marvel_is_mmio);
  694. EXPORT_SYMBOL(marvel_ioportmap);
  695. EXPORT_SYMBOL(marvel_ioread8);
  696. EXPORT_SYMBOL(marvel_iowrite8);
  697. #endif
  698. /*
  699. * NUMA Support
  700. */
  701. /**********
  702. * FIXME - for now each cpu is a node by itself
  703. * -- no real support for striped mode
  704. **********
  705. */
  706. int
  707. marvel_pa_to_nid(unsigned long pa)
  708. {
  709. int cpuid;
  710. if ((pa >> 43) & 1) /* I/O */
  711. cpuid = (~(pa >> 35) & 0xff);
  712. else /* mem */
  713. cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
  714. return marvel_cpuid_to_nid(cpuid);
  715. }
  716. int
  717. marvel_cpuid_to_nid(int cpuid)
  718. {
  719. return cpuid;
  720. }
  721. unsigned long
  722. marvel_node_mem_start(int nid)
  723. {
  724. unsigned long pa;
  725. pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
  726. pa <<= 34;
  727. return pa;
  728. }
  729. unsigned long
  730. marvel_node_mem_size(int nid)
  731. {
  732. return 16UL * 1024 * 1024 * 1024; /* 16GB */
  733. }
  734. /*
  735. * AGP GART Support.
  736. */
  737. #include <linux/agp_backend.h>
  738. #include <asm/agp_backend.h>
  739. #include <linux/slab.h>
  740. #include <linux/delay.h>
  741. struct marvel_agp_aperture {
  742. struct pci_iommu_arena *arena;
  743. long pg_start;
  744. long pg_count;
  745. };
  746. static int
  747. marvel_agp_setup(alpha_agp_info *agp)
  748. {
  749. struct marvel_agp_aperture *aper;
  750. if (!alpha_agpgart_size)
  751. return -ENOMEM;
  752. aper = kmalloc(sizeof(*aper), GFP_KERNEL);
  753. if (aper == NULL) return -ENOMEM;
  754. aper->arena = agp->hose->sg_pci;
  755. aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
  756. aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
  757. aper->pg_count - 1);
  758. if (aper->pg_start < 0) {
  759. printk(KERN_ERR "Failed to reserve AGP memory\n");
  760. kfree(aper);
  761. return -ENOMEM;
  762. }
  763. agp->aperture.bus_base =
  764. aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
  765. agp->aperture.size = aper->pg_count * PAGE_SIZE;
  766. agp->aperture.sysdata = aper;
  767. return 0;
  768. }
  769. static void
  770. marvel_agp_cleanup(alpha_agp_info *agp)
  771. {
  772. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  773. int status;
  774. status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
  775. if (status == -EBUSY) {
  776. printk(KERN_WARNING
  777. "Attempted to release bound AGP memory - unbinding\n");
  778. iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
  779. status = iommu_release(aper->arena, aper->pg_start,
  780. aper->pg_count);
  781. }
  782. if (status < 0)
  783. printk(KERN_ERR "Failed to release AGP memory\n");
  784. kfree(aper);
  785. kfree(agp);
  786. }
  787. static int
  788. marvel_agp_configure(alpha_agp_info *agp)
  789. {
  790. io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
  791. struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
  792. unsigned int new_rate = 0;
  793. unsigned long agp_pll;
  794. /*
  795. * Check the requested mode against the PLL setting.
  796. * The agpgart_be code has not programmed the card yet,
  797. * so we can still tweak mode here.
  798. */
  799. agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
  800. switch(IO7_PLL_RNGB(agp_pll)) {
  801. case 0x4: /* 2x only */
  802. /*
  803. * The PLL is only programmed for 2x, so adjust the
  804. * rate to 2x, if necessary.
  805. */
  806. if (agp->mode.bits.rate != 2)
  807. new_rate = 2;
  808. break;
  809. case 0x6: /* 1x / 4x */
  810. /*
  811. * The PLL is programmed for 1x or 4x. Don't go faster
  812. * than requested, so if the requested rate is 2x, use 1x.
  813. */
  814. if (agp->mode.bits.rate == 2)
  815. new_rate = 1;
  816. break;
  817. default: /* ??????? */
  818. /*
  819. * Don't know what this PLL setting is, take the requested
  820. * rate, but warn the user.
  821. */
  822. printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
  823. __func__, IO7_PLL_RNGB(agp_pll), agp_pll);
  824. break;
  825. }
  826. /*
  827. * Set the new rate, if necessary.
  828. */
  829. if (new_rate) {
  830. printk("Requested AGP Rate %dX not compatible "
  831. "with PLL setting - using %dX\n",
  832. agp->mode.bits.rate,
  833. new_rate);
  834. agp->mode.bits.rate = new_rate;
  835. }
  836. printk("Enabling AGP on hose %d: %dX%s RQ %d\n",
  837. agp->hose->index, agp->mode.bits.rate,
  838. agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
  839. csrs->AGP_CMD.csr = agp->mode.lw;
  840. return 0;
  841. }
  842. static int
  843. marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  844. {
  845. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  846. return iommu_bind(aper->arena, aper->pg_start + pg_start,
  847. mem->page_count, mem->pages);
  848. }
  849. static int
  850. marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  851. {
  852. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  853. return iommu_unbind(aper->arena, aper->pg_start + pg_start,
  854. mem->page_count);
  855. }
  856. static unsigned long
  857. marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
  858. {
  859. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  860. unsigned long baddr = addr - aper->arena->dma_base;
  861. unsigned long pte;
  862. if (addr < agp->aperture.bus_base ||
  863. addr >= agp->aperture.bus_base + agp->aperture.size) {
  864. printk("%s: addr out of range\n", __func__);
  865. return -EINVAL;
  866. }
  867. pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
  868. if (!(pte & 1)) {
  869. printk("%s: pte not valid\n", __func__);
  870. return -EINVAL;
  871. }
  872. return (pte >> 1) << PAGE_SHIFT;
  873. }
  874. struct alpha_agp_ops marvel_agp_ops =
  875. {
  876. .setup = marvel_agp_setup,
  877. .cleanup = marvel_agp_cleanup,
  878. .configure = marvel_agp_configure,
  879. .bind = marvel_agp_bind_memory,
  880. .unbind = marvel_agp_unbind_memory,
  881. .translate = marvel_agp_translate
  882. };
  883. alpha_agp_info *
  884. marvel_agp_info(void)
  885. {
  886. struct pci_controller *hose;
  887. io7_ioport_csrs *csrs;
  888. alpha_agp_info *agp;
  889. struct io7 *io7;
  890. /*
  891. * Find the first IO7 with an AGP card.
  892. *
  893. * FIXME -- there should be a better way (we want to be able to
  894. * specify and what if the agp card is not video???)
  895. */
  896. hose = NULL;
  897. for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
  898. struct pci_controller *h;
  899. vuip addr;