PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/arch/sparc/mm/sun4c.c

https://bitbucket.org/ndreys/linux-sunxi
C | 2166 lines | 1678 code | 324 blank | 164 comment | 207 complexity | 85bbe7265e0a735c557fdf1813a2cfbf MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* sun4c.c: Doing in software what should be done in hardware.
  2. *
  3. * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1996 Andrew Tridgell (Andrew.Tridgell@anu.edu.au)
  6. * Copyright (C) 1997-2000 Anton Blanchard (anton@samba.org)
  7. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  8. */
  9. #define NR_TASK_BUCKETS 512
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/highmem.h>
  16. #include <linux/fs.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/bitmap.h>
  20. #include <asm/sections.h>
  21. #include <asm/page.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/vaddrs.h>
  25. #include <asm/idprom.h>
  26. #include <asm/machines.h>
  27. #include <asm/memreg.h>
  28. #include <asm/processor.h>
  29. #include <asm/auxio.h>
  30. #include <asm/io.h>
  31. #include <asm/oplib.h>
  32. #include <asm/openprom.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/highmem.h>
  35. #include <asm/btfixup.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/tlbflush.h>
  38. /* Because of our dynamic kernel TLB miss strategy, and how
  39. * our DVMA mapping allocation works, you _MUST_:
  40. *
  41. * 1) Disable interrupts _and_ not touch any dynamic kernel
  42. * memory while messing with kernel MMU state. By
  43. * dynamic memory I mean any object which is not in
  44. * the kernel image itself or a thread_union (both of
  45. * which are locked into the MMU).
  46. * 2) Disable interrupts while messing with user MMU state.
  47. */
  48. extern int num_segmaps, num_contexts;
  49. extern unsigned long page_kernel;
  50. /* That's it, we prom_halt() on sun4c if the cache size is something other than 65536.
  51. * So let's save some cycles and just use that everywhere except for that bootup
  52. * sanity check.
  53. */
  54. #define SUN4C_VAC_SIZE 65536
  55. #define SUN4C_KERNEL_BUCKETS 32
  56. /* Flushing the cache. */
  57. struct sun4c_vac_props sun4c_vacinfo;
  58. unsigned long sun4c_kernel_faults;
  59. /* Invalidate every sun4c cache line tag. */
  60. static void __init sun4c_flush_all(void)
  61. {
  62. unsigned long begin, end;
  63. if (sun4c_vacinfo.on)
  64. panic("SUN4C: AIEEE, trying to invalidate vac while it is on.");
  65. /* Clear 'valid' bit in all cache line tags */
  66. begin = AC_CACHETAGS;
  67. end = (AC_CACHETAGS + SUN4C_VAC_SIZE);
  68. while (begin < end) {
  69. __asm__ __volatile__("sta %%g0, [%0] %1\n\t" : :
  70. "r" (begin), "i" (ASI_CONTROL));
  71. begin += sun4c_vacinfo.linesize;
  72. }
  73. }
  74. static void sun4c_flush_context_hw(void)
  75. {
  76. unsigned long end = SUN4C_VAC_SIZE;
  77. __asm__ __volatile__(
  78. "1: addcc %0, -4096, %0\n\t"
  79. " bne 1b\n\t"
  80. " sta %%g0, [%0] %2"
  81. : "=&r" (end)
  82. : "0" (end), "i" (ASI_HWFLUSHCONTEXT)
  83. : "cc");
  84. }
  85. /* Must be called minimally with IRQs disabled. */
  86. static void sun4c_flush_segment_hw(unsigned long addr)
  87. {
  88. if (sun4c_get_segmap(addr) != invalid_segment) {
  89. unsigned long vac_size = SUN4C_VAC_SIZE;
  90. __asm__ __volatile__(
  91. "1: addcc %0, -4096, %0\n\t"
  92. " bne 1b\n\t"
  93. " sta %%g0, [%2 + %0] %3"
  94. : "=&r" (vac_size)
  95. : "0" (vac_size), "r" (addr), "i" (ASI_HWFLUSHSEG)
  96. : "cc");
  97. }
  98. }
  99. /* File local boot time fixups. */
  100. BTFIXUPDEF_CALL(void, sun4c_flush_page, unsigned long)
  101. BTFIXUPDEF_CALL(void, sun4c_flush_segment, unsigned long)
  102. BTFIXUPDEF_CALL(void, sun4c_flush_context, void)
  103. #define sun4c_flush_page(addr) BTFIXUP_CALL(sun4c_flush_page)(addr)
  104. #define sun4c_flush_segment(addr) BTFIXUP_CALL(sun4c_flush_segment)(addr)
  105. #define sun4c_flush_context() BTFIXUP_CALL(sun4c_flush_context)()
  106. /* Must be called minimally with interrupts disabled. */
  107. static void sun4c_flush_page_hw(unsigned long addr)
  108. {
  109. addr &= PAGE_MASK;
  110. if ((int)sun4c_get_pte(addr) < 0)
  111. __asm__ __volatile__("sta %%g0, [%0] %1"
  112. : : "r" (addr), "i" (ASI_HWFLUSHPAGE));
  113. }
  114. /* Don't inline the software version as it eats too many cache lines if expanded. */
  115. static void sun4c_flush_context_sw(void)
  116. {
  117. unsigned long nbytes = SUN4C_VAC_SIZE;
  118. unsigned long lsize = sun4c_vacinfo.linesize;
  119. __asm__ __volatile__(
  120. "add %2, %2, %%g1\n\t"
  121. "add %2, %%g1, %%g2\n\t"
  122. "add %2, %%g2, %%g3\n\t"
  123. "add %2, %%g3, %%g4\n\t"
  124. "add %2, %%g4, %%g5\n\t"
  125. "add %2, %%g5, %%o4\n\t"
  126. "add %2, %%o4, %%o5\n"
  127. "1:\n\t"
  128. "subcc %0, %%o5, %0\n\t"
  129. "sta %%g0, [%0] %3\n\t"
  130. "sta %%g0, [%0 + %2] %3\n\t"
  131. "sta %%g0, [%0 + %%g1] %3\n\t"
  132. "sta %%g0, [%0 + %%g2] %3\n\t"
  133. "sta %%g0, [%0 + %%g3] %3\n\t"
  134. "sta %%g0, [%0 + %%g4] %3\n\t"
  135. "sta %%g0, [%0 + %%g5] %3\n\t"
  136. "bg 1b\n\t"
  137. " sta %%g0, [%1 + %%o4] %3\n"
  138. : "=&r" (nbytes)
  139. : "0" (nbytes), "r" (lsize), "i" (ASI_FLUSHCTX)
  140. : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
  141. }
  142. /* Don't inline the software version as it eats too many cache lines if expanded. */
  143. static void sun4c_flush_segment_sw(unsigned long addr)
  144. {
  145. if (sun4c_get_segmap(addr) != invalid_segment) {
  146. unsigned long nbytes = SUN4C_VAC_SIZE;
  147. unsigned long lsize = sun4c_vacinfo.linesize;
  148. __asm__ __volatile__(
  149. "add %2, %2, %%g1\n\t"
  150. "add %2, %%g1, %%g2\n\t"
  151. "add %2, %%g2, %%g3\n\t"
  152. "add %2, %%g3, %%g4\n\t"
  153. "add %2, %%g4, %%g5\n\t"
  154. "add %2, %%g5, %%o4\n\t"
  155. "add %2, %%o4, %%o5\n"
  156. "1:\n\t"
  157. "subcc %1, %%o5, %1\n\t"
  158. "sta %%g0, [%0] %6\n\t"
  159. "sta %%g0, [%0 + %2] %6\n\t"
  160. "sta %%g0, [%0 + %%g1] %6\n\t"
  161. "sta %%g0, [%0 + %%g2] %6\n\t"
  162. "sta %%g0, [%0 + %%g3] %6\n\t"
  163. "sta %%g0, [%0 + %%g4] %6\n\t"
  164. "sta %%g0, [%0 + %%g5] %6\n\t"
  165. "sta %%g0, [%0 + %%o4] %6\n\t"
  166. "bg 1b\n\t"
  167. " add %0, %%o5, %0\n"
  168. : "=&r" (addr), "=&r" (nbytes), "=&r" (lsize)
  169. : "0" (addr), "1" (nbytes), "2" (lsize),
  170. "i" (ASI_FLUSHSEG)
  171. : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
  172. }
  173. }
  174. /* Don't inline the software version as it eats too many cache lines if expanded. */
  175. static void sun4c_flush_page_sw(unsigned long addr)
  176. {
  177. addr &= PAGE_MASK;
  178. if ((sun4c_get_pte(addr) & (_SUN4C_PAGE_NOCACHE | _SUN4C_PAGE_VALID)) ==
  179. _SUN4C_PAGE_VALID) {
  180. unsigned long left = PAGE_SIZE;
  181. unsigned long lsize = sun4c_vacinfo.linesize;
  182. __asm__ __volatile__(
  183. "add %2, %2, %%g1\n\t"
  184. "add %2, %%g1, %%g2\n\t"
  185. "add %2, %%g2, %%g3\n\t"
  186. "add %2, %%g3, %%g4\n\t"
  187. "add %2, %%g4, %%g5\n\t"
  188. "add %2, %%g5, %%o4\n\t"
  189. "add %2, %%o4, %%o5\n"
  190. "1:\n\t"
  191. "subcc %1, %%o5, %1\n\t"
  192. "sta %%g0, [%0] %6\n\t"
  193. "sta %%g0, [%0 + %2] %6\n\t"
  194. "sta %%g0, [%0 + %%g1] %6\n\t"
  195. "sta %%g0, [%0 + %%g2] %6\n\t"
  196. "sta %%g0, [%0 + %%g3] %6\n\t"
  197. "sta %%g0, [%0 + %%g4] %6\n\t"
  198. "sta %%g0, [%0 + %%g5] %6\n\t"
  199. "sta %%g0, [%0 + %%o4] %6\n\t"
  200. "bg 1b\n\t"
  201. " add %0, %%o5, %0\n"
  202. : "=&r" (addr), "=&r" (left), "=&r" (lsize)
  203. : "0" (addr), "1" (left), "2" (lsize),
  204. "i" (ASI_FLUSHPG)
  205. : "g1", "g2", "g3", "g4", "g5", "o4", "o5", "cc");
  206. }
  207. }
  208. /* The sun4c's do have an on chip store buffer. And the way you
  209. * clear them out isn't so obvious. The only way I can think of
  210. * to accomplish this is to read the current context register,
  211. * store the same value there, then read an external hardware
  212. * register.
  213. */
  214. void sun4c_complete_all_stores(void)
  215. {
  216. volatile int _unused;
  217. _unused = sun4c_get_context();
  218. sun4c_set_context(_unused);
  219. _unused = get_auxio();
  220. }
  221. /* Bootup utility functions. */
  222. static inline void sun4c_init_clean_segmap(unsigned char pseg)
  223. {
  224. unsigned long vaddr;
  225. sun4c_put_segmap(0, pseg);
  226. for (vaddr = 0; vaddr < SUN4C_REAL_PGDIR_SIZE; vaddr += PAGE_SIZE)
  227. sun4c_put_pte(vaddr, 0);
  228. sun4c_put_segmap(0, invalid_segment);
  229. }
  230. static inline void sun4c_init_clean_mmu(unsigned long kernel_end)
  231. {
  232. unsigned long vaddr;
  233. unsigned char savectx, ctx;
  234. savectx = sun4c_get_context();
  235. for (ctx = 0; ctx < num_contexts; ctx++) {
  236. sun4c_set_context(ctx);
  237. for (vaddr = 0; vaddr < 0x20000000; vaddr += SUN4C_REAL_PGDIR_SIZE)
  238. sun4c_put_segmap(vaddr, invalid_segment);
  239. for (vaddr = 0xe0000000; vaddr < KERNBASE; vaddr += SUN4C_REAL_PGDIR_SIZE)
  240. sun4c_put_segmap(vaddr, invalid_segment);
  241. for (vaddr = kernel_end; vaddr < KADB_DEBUGGER_BEGVM; vaddr += SUN4C_REAL_PGDIR_SIZE)
  242. sun4c_put_segmap(vaddr, invalid_segment);
  243. for (vaddr = LINUX_OPPROM_ENDVM; vaddr; vaddr += SUN4C_REAL_PGDIR_SIZE)
  244. sun4c_put_segmap(vaddr, invalid_segment);
  245. }
  246. sun4c_set_context(savectx);
  247. }
  248. void __init sun4c_probe_vac(void)
  249. {
  250. sun4c_disable_vac();
  251. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
  252. (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
  253. /* PROM on SS1 lacks this info, to be super safe we
  254. * hard code it here since this arch is cast in stone.
  255. */
  256. sun4c_vacinfo.num_bytes = 65536;
  257. sun4c_vacinfo.linesize = 16;
  258. } else {
  259. sun4c_vacinfo.num_bytes =
  260. prom_getintdefault(prom_root_node, "vac-size", 65536);
  261. sun4c_vacinfo.linesize =
  262. prom_getintdefault(prom_root_node, "vac-linesize", 16);
  263. }
  264. sun4c_vacinfo.do_hwflushes =
  265. prom_getintdefault(prom_root_node, "vac-hwflush", 0);
  266. if (sun4c_vacinfo.do_hwflushes == 0)
  267. sun4c_vacinfo.do_hwflushes =
  268. prom_getintdefault(prom_root_node, "vac_hwflush", 0);
  269. if (sun4c_vacinfo.num_bytes != 65536) {
  270. prom_printf("WEIRD Sun4C VAC cache size, "
  271. "tell sparclinux@vger.kernel.org");
  272. prom_halt();
  273. }
  274. switch (sun4c_vacinfo.linesize) {
  275. case 16:
  276. sun4c_vacinfo.log2lsize = 4;
  277. break;
  278. case 32:
  279. sun4c_vacinfo.log2lsize = 5;
  280. break;
  281. default:
  282. prom_printf("probe_vac: Didn't expect vac-linesize of %d, halting\n",
  283. sun4c_vacinfo.linesize);
  284. prom_halt();
  285. }
  286. sun4c_flush_all();
  287. sun4c_enable_vac();
  288. }
  289. /* Patch instructions for the low level kernel fault handler. */
  290. extern unsigned long invalid_segment_patch1, invalid_segment_patch1_ff;
  291. extern unsigned long invalid_segment_patch2, invalid_segment_patch2_ff;
  292. extern unsigned long invalid_segment_patch1_1ff, invalid_segment_patch2_1ff;
  293. extern unsigned long num_context_patch1, num_context_patch1_16;
  294. extern unsigned long num_context_patch2_16;
  295. extern unsigned long vac_linesize_patch, vac_linesize_patch_32;
  296. extern unsigned long vac_hwflush_patch1, vac_hwflush_patch1_on;
  297. extern unsigned long vac_hwflush_patch2, vac_hwflush_patch2_on;
  298. #define PATCH_INSN(src, dst) do { \
  299. daddr = &(dst); \
  300. iaddr = &(src); \
  301. *daddr = *iaddr; \
  302. } while (0)
  303. static void __init patch_kernel_fault_handler(void)
  304. {
  305. unsigned long *iaddr, *daddr;
  306. switch (num_segmaps) {
  307. case 128:
  308. /* Default, nothing to do. */
  309. break;
  310. case 256:
  311. PATCH_INSN(invalid_segment_patch1_ff,
  312. invalid_segment_patch1);
  313. PATCH_INSN(invalid_segment_patch2_ff,
  314. invalid_segment_patch2);
  315. break;
  316. case 512:
  317. PATCH_INSN(invalid_segment_patch1_1ff,
  318. invalid_segment_patch1);
  319. PATCH_INSN(invalid_segment_patch2_1ff,
  320. invalid_segment_patch2);
  321. break;
  322. default:
  323. prom_printf("Unhandled number of segmaps: %d\n",
  324. num_segmaps);
  325. prom_halt();
  326. }
  327. switch (num_contexts) {
  328. case 8:
  329. /* Default, nothing to do. */
  330. break;
  331. case 16:
  332. PATCH_INSN(num_context_patch1_16,
  333. num_context_patch1);
  334. break;
  335. default:
  336. prom_printf("Unhandled number of contexts: %d\n",
  337. num_contexts);
  338. prom_halt();
  339. }
  340. if (sun4c_vacinfo.do_hwflushes != 0) {
  341. PATCH_INSN(vac_hwflush_patch1_on, vac_hwflush_patch1);
  342. PATCH_INSN(vac_hwflush_patch2_on, vac_hwflush_patch2);
  343. } else {
  344. switch (sun4c_vacinfo.linesize) {
  345. case 16:
  346. /* Default, nothing to do. */
  347. break;
  348. case 32:
  349. PATCH_INSN(vac_linesize_patch_32, vac_linesize_patch);
  350. break;
  351. default:
  352. prom_printf("Impossible VAC linesize %d, halting...\n",
  353. sun4c_vacinfo.linesize);
  354. prom_halt();
  355. }
  356. }
  357. }
  358. static void __init sun4c_probe_mmu(void)
  359. {
  360. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS1)) ||
  361. (idprom->id_machtype == (SM_SUN4C | SM_4C_SS1PLUS))) {
  362. /* Hardcode these just to be safe, PROM on SS1 does
  363. * not have this info available in the root node.
  364. */
  365. num_segmaps = 128;
  366. num_contexts = 8;
  367. } else {
  368. num_segmaps =
  369. prom_getintdefault(prom_root_node, "mmu-npmg", 128);
  370. num_contexts =
  371. prom_getintdefault(prom_root_node, "mmu-nctx", 0x8);
  372. }
  373. patch_kernel_fault_handler();
  374. }
  375. volatile unsigned long __iomem *sun4c_memerr_reg = NULL;
  376. void __init sun4c_probe_memerr_reg(void)
  377. {
  378. phandle node;
  379. struct linux_prom_registers regs[1];
  380. node = prom_getchild(prom_root_node);
  381. node = prom_searchsiblings(prom_root_node, "memory-error");
  382. if (!node)
  383. return;
  384. if (prom_getproperty(node, "reg", (char *)regs, sizeof(regs)) <= 0)
  385. return;
  386. /* hmm I think regs[0].which_io is zero here anyways */
  387. sun4c_memerr_reg = ioremap(regs[0].phys_addr, regs[0].reg_size);
  388. }
  389. static inline void sun4c_init_ss2_cache_bug(void)
  390. {
  391. if ((idprom->id_machtype == (SM_SUN4C | SM_4C_SS2)) ||
  392. (idprom->id_machtype == (SM_SUN4C | SM_4C_IPX)) ||
  393. (idprom->id_machtype == (SM_SUN4C | SM_4C_ELC))) {
  394. /* Whee.. */
  395. printk("SS2 cache bug detected, uncaching trap table page\n");
  396. sun4c_flush_page((unsigned int) &_start);
  397. sun4c_put_pte(((unsigned long) &_start),
  398. (sun4c_get_pte((unsigned long) &_start) | _SUN4C_PAGE_NOCACHE));
  399. }
  400. }
  401. /* Addr is always aligned on a page boundary for us already. */
  402. static int sun4c_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va,
  403. unsigned long addr, int len)
  404. {
  405. unsigned long page, end;
  406. *pba = addr;
  407. end = PAGE_ALIGN((addr + len));
  408. while (addr < end) {
  409. page = va;
  410. sun4c_flush_page(page);
  411. page -= PAGE_OFFSET;
  412. page >>= PAGE_SHIFT;
  413. page |= (_SUN4C_PAGE_VALID | _SUN4C_PAGE_DIRTY |
  414. _SUN4C_PAGE_NOCACHE | _SUN4C_PAGE_PRIV);
  415. sun4c_put_pte(addr, page);
  416. addr += PAGE_SIZE;
  417. va += PAGE_SIZE;
  418. }
  419. return 0;
  420. }
  421. static void sun4c_unmap_dma_area(struct device *dev, unsigned long busa, int len)
  422. {
  423. /* Fortunately for us, bus_addr == uncached_virt in sun4c. */
  424. /* XXX Implement this */
  425. }
  426. /* TLB management. */
  427. /* Don't change this struct without changing entry.S. This is used
  428. * in the in-window kernel fault handler, and you don't want to mess
  429. * with that. (See sun4c_fault in entry.S).
  430. */
  431. struct sun4c_mmu_entry {
  432. struct sun4c_mmu_entry *next;
  433. struct sun4c_mmu_entry *prev;
  434. unsigned long vaddr;
  435. unsigned char pseg;
  436. unsigned char locked;
  437. /* For user mappings only, and completely hidden from kernel
  438. * TLB miss code.
  439. */
  440. unsigned char ctx;
  441. struct sun4c_mmu_entry *lru_next;
  442. struct sun4c_mmu_entry *lru_prev;
  443. };
  444. static struct sun4c_mmu_entry mmu_entry_pool[SUN4C_MAX_SEGMAPS];
  445. static void __init sun4c_init_mmu_entry_pool(void)
  446. {
  447. int i;
  448. for (i=0; i < SUN4C_MAX_SEGMAPS; i++) {
  449. mmu_entry_pool[i].pseg = i;
  450. mmu_entry_pool[i].next = NULL;
  451. mmu_entry_pool[i].prev = NULL;
  452. mmu_entry_pool[i].vaddr = 0;
  453. mmu_entry_pool[i].locked = 0;
  454. mmu_entry_pool[i].ctx = 0;
  455. mmu_entry_pool[i].lru_next = NULL;
  456. mmu_entry_pool[i].lru_prev = NULL;
  457. }
  458. mmu_entry_pool[invalid_segment].locked = 1;
  459. }
  460. static inline void fix_permissions(unsigned long vaddr, unsigned long bits_on,
  461. unsigned long bits_off)
  462. {
  463. unsigned long start, end;
  464. end = vaddr + SUN4C_REAL_PGDIR_SIZE;
  465. for (start = vaddr; start < end; start += PAGE_SIZE)
  466. if (sun4c_get_pte(start) & _SUN4C_PAGE_VALID)
  467. sun4c_put_pte(start, (sun4c_get_pte(start) | bits_on) &
  468. ~bits_off);
  469. }
  470. static inline void sun4c_init_map_kernelprom(unsigned long kernel_end)
  471. {
  472. unsigned long vaddr;
  473. unsigned char pseg, ctx;
  474. for (vaddr = KADB_DEBUGGER_BEGVM;
  475. vaddr < LINUX_OPPROM_ENDVM;
  476. vaddr += SUN4C_REAL_PGDIR_SIZE) {
  477. pseg = sun4c_get_segmap(vaddr);
  478. if (pseg != invalid_segment) {
  479. mmu_entry_pool[pseg].locked = 1;
  480. for (ctx = 0; ctx < num_contexts; ctx++)
  481. prom_putsegment(ctx, vaddr, pseg);
  482. fix_permissions(vaddr, _SUN4C_PAGE_PRIV, 0);
  483. }
  484. }
  485. for (vaddr = KERNBASE; vaddr < kernel_end; vaddr += SUN4C_REAL_PGDIR_SIZE) {
  486. pseg = sun4c_get_segmap(vaddr);
  487. mmu_entry_pool[pseg].locked = 1;
  488. for (ctx = 0; ctx < num_contexts; ctx++)
  489. prom_putsegment(ctx, vaddr, pseg);
  490. fix_permissions(vaddr, _SUN4C_PAGE_PRIV, _SUN4C_PAGE_NOCACHE);
  491. }
  492. }
  493. static void __init sun4c_init_lock_area(unsigned long start, unsigned long end)
  494. {
  495. int i, ctx;
  496. while (start < end) {
  497. for (i = 0; i < invalid_segment; i++)
  498. if (!mmu_entry_pool[i].locked)
  499. break;
  500. mmu_entry_pool[i].locked = 1;
  501. sun4c_init_clean_segmap(i);
  502. for (ctx = 0; ctx < num_contexts; ctx++)
  503. prom_putsegment(ctx, start, mmu_entry_pool[i].pseg);
  504. start += SUN4C_REAL_PGDIR_SIZE;
  505. }
  506. }
  507. /* Don't change this struct without changing entry.S. This is used
  508. * in the in-window kernel fault handler, and you don't want to mess
  509. * with that. (See sun4c_fault in entry.S).
  510. */
  511. struct sun4c_mmu_ring {
  512. struct sun4c_mmu_entry ringhd;
  513. int num_entries;
  514. };
  515. static struct sun4c_mmu_ring sun4c_context_ring[SUN4C_MAX_CONTEXTS]; /* used user entries */
  516. static struct sun4c_mmu_ring sun4c_ufree_ring; /* free user entries */
  517. static struct sun4c_mmu_ring sun4c_ulru_ring; /* LRU user entries */
  518. struct sun4c_mmu_ring sun4c_kernel_ring; /* used kernel entries */
  519. struct sun4c_mmu_ring sun4c_kfree_ring; /* free kernel entries */
  520. static inline void sun4c_init_rings(void)
  521. {
  522. int i;
  523. for (i = 0; i < SUN4C_MAX_CONTEXTS; i++) {
  524. sun4c_context_ring[i].ringhd.next =
  525. sun4c_context_ring[i].ringhd.prev =
  526. &sun4c_context_ring[i].ringhd;
  527. sun4c_context_ring[i].num_entries = 0;
  528. }
  529. sun4c_ufree_ring.ringhd.next = sun4c_ufree_ring.ringhd.prev =
  530. &sun4c_ufree_ring.ringhd;
  531. sun4c_ufree_ring.num_entries = 0;
  532. sun4c_ulru_ring.ringhd.lru_next = sun4c_ulru_ring.ringhd.lru_prev =
  533. &sun4c_ulru_ring.ringhd;
  534. sun4c_ulru_ring.num_entries = 0;
  535. sun4c_kernel_ring.ringhd.next = sun4c_kernel_ring.ringhd.prev =
  536. &sun4c_kernel_ring.ringhd;
  537. sun4c_kernel_ring.num_entries = 0;
  538. sun4c_kfree_ring.ringhd.next = sun4c_kfree_ring.ringhd.prev =
  539. &sun4c_kfree_ring.ringhd;
  540. sun4c_kfree_ring.num_entries = 0;
  541. }
  542. static void add_ring(struct sun4c_mmu_ring *ring,
  543. struct sun4c_mmu_entry *entry)
  544. {
  545. struct sun4c_mmu_entry *head = &ring->ringhd;
  546. entry->prev = head;
  547. (entry->next = head->next)->prev = entry;
  548. head->next = entry;
  549. ring->num_entries++;
  550. }
  551. static inline void add_lru(struct sun4c_mmu_entry *entry)
  552. {
  553. struct sun4c_mmu_ring *ring = &sun4c_ulru_ring;
  554. struct sun4c_mmu_entry *head = &ring->ringhd;
  555. entry->lru_next = head;
  556. (entry->lru_prev = head->lru_prev)->lru_next = entry;
  557. head->lru_prev = entry;
  558. }
  559. static void add_ring_ordered(struct sun4c_mmu_ring *ring,
  560. struct sun4c_mmu_entry *entry)
  561. {
  562. struct sun4c_mmu_entry *head = &ring->ringhd;
  563. unsigned long addr = entry->vaddr;
  564. while ((head->next != &ring->ringhd) && (head->next->vaddr < addr))
  565. head = head->next;
  566. entry->prev = head;
  567. (entry->next = head->next)->prev = entry;
  568. head->next = entry;
  569. ring->num_entries++;
  570. add_lru(entry);
  571. }
  572. static inline void remove_ring(struct sun4c_mmu_ring *ring,
  573. struct sun4c_mmu_entry *entry)
  574. {
  575. struct sun4c_mmu_entry *next = entry->next;
  576. (next->prev = entry->prev)->next = next;
  577. ring->num_entries--;
  578. }
  579. static void remove_lru(struct sun4c_mmu_entry *entry)
  580. {
  581. struct sun4c_mmu_entry *next = entry->lru_next;
  582. (next->lru_prev = entry->lru_prev)->lru_next = next;
  583. }
  584. static void free_user_entry(int ctx, struct sun4c_mmu_entry *entry)
  585. {
  586. remove_ring(sun4c_context_ring+ctx, entry);
  587. remove_lru(entry);
  588. add_ring(&sun4c_ufree_ring, entry);
  589. }
  590. static void free_kernel_entry(struct sun4c_mmu_entry *entry,
  591. struct sun4c_mmu_ring *ring)
  592. {
  593. remove_ring(ring, entry);
  594. add_ring(&sun4c_kfree_ring, entry);
  595. }
  596. static void __init sun4c_init_fill_kernel_ring(int howmany)
  597. {
  598. int i;
  599. while (howmany) {
  600. for (i = 0; i < invalid_segment; i++)
  601. if (!mmu_entry_pool[i].locked)
  602. break;
  603. mmu_entry_pool[i].locked = 1;
  604. sun4c_init_clean_segmap(i);
  605. add_ring(&sun4c_kfree_ring, &mmu_entry_pool[i]);
  606. howmany--;
  607. }
  608. }
  609. static void __init sun4c_init_fill_user_ring(void)
  610. {
  611. int i;
  612. for (i = 0; i < invalid_segment; i++) {
  613. if (mmu_entry_pool[i].locked)
  614. continue;
  615. sun4c_init_clean_segmap(i);
  616. add_ring(&sun4c_ufree_ring, &mmu_entry_pool[i]);
  617. }
  618. }
  619. static void sun4c_kernel_unmap(struct sun4c_mmu_entry *kentry)
  620. {
  621. int savectx, ctx;
  622. savectx = sun4c_get_context();
  623. for (ctx = 0; ctx < num_contexts; ctx++) {
  624. sun4c_set_context(ctx);
  625. sun4c_put_segmap(kentry->vaddr, invalid_segment);
  626. }
  627. sun4c_set_context(savectx);
  628. }
  629. static void sun4c_kernel_map(struct sun4c_mmu_entry *kentry)
  630. {
  631. int savectx, ctx;
  632. savectx = sun4c_get_context();
  633. for (ctx = 0; ctx < num_contexts; ctx++) {
  634. sun4c_set_context(ctx);
  635. sun4c_put_segmap(kentry->vaddr, kentry->pseg);
  636. }
  637. sun4c_set_context(savectx);
  638. }
  639. #define sun4c_user_unmap(__entry) \
  640. sun4c_put_segmap((__entry)->vaddr, invalid_segment)
  641. static void sun4c_demap_context(struct sun4c_mmu_ring *crp, unsigned char ctx)
  642. {
  643. struct sun4c_mmu_entry *head = &crp->ringhd;
  644. unsigned long flags;
  645. local_irq_save(flags);
  646. if (head->next != head) {
  647. struct sun4c_mmu_entry *entry = head->next;
  648. int savectx = sun4c_get_context();
  649. flush_user_windows();
  650. sun4c_set_context(ctx);
  651. sun4c_flush_context();
  652. do {
  653. struct sun4c_mmu_entry *next = entry->next;
  654. sun4c_user_unmap(entry);
  655. free_user_entry(ctx, entry);
  656. entry = next;
  657. } while (entry != head);
  658. sun4c_set_context(savectx);
  659. }
  660. local_irq_restore(flags);
  661. }
  662. static int sun4c_user_taken_entries; /* This is how much we have. */
  663. static int max_user_taken_entries; /* This limits us and prevents deadlock. */
  664. static struct sun4c_mmu_entry *sun4c_kernel_strategy(void)
  665. {
  666. struct sun4c_mmu_entry *this_entry;
  667. /* If some are free, return first one. */
  668. if (sun4c_kfree_ring.num_entries) {
  669. this_entry = sun4c_kfree_ring.ringhd.next;
  670. return this_entry;
  671. }
  672. /* Else free one up. */
  673. this_entry = sun4c_kernel_ring.ringhd.prev;
  674. sun4c_flush_segment(this_entry->vaddr);
  675. sun4c_kernel_unmap(this_entry);
  676. free_kernel_entry(this_entry, &sun4c_kernel_ring);
  677. this_entry = sun4c_kfree_ring.ringhd.next;
  678. return this_entry;
  679. }
  680. /* Using this method to free up mmu entries eliminates a lot of
  681. * potential races since we have a kernel that incurs tlb
  682. * replacement faults. There may be performance penalties.
  683. *
  684. * NOTE: Must be called with interrupts disabled.
  685. */
  686. static struct sun4c_mmu_entry *sun4c_user_strategy(void)
  687. {
  688. struct sun4c_mmu_entry *entry;
  689. unsigned char ctx;
  690. int savectx;
  691. /* If some are free, return first one. */
  692. if (sun4c_ufree_ring.num_entries) {
  693. entry = sun4c_ufree_ring.ringhd.next;
  694. goto unlink_out;
  695. }
  696. if (sun4c_user_taken_entries) {
  697. entry = sun4c_kernel_strategy();
  698. sun4c_user_taken_entries--;
  699. goto kunlink_out;
  700. }
  701. /* Grab from the beginning of the LRU list. */
  702. entry = sun4c_ulru_ring.ringhd.lru_next;
  703. ctx = entry->ctx;
  704. savectx = sun4c_get_context();
  705. flush_user_windows();
  706. sun4c_set_context(ctx);
  707. sun4c_flush_segment(entry->vaddr);
  708. sun4c_user_unmap(entry);
  709. remove_ring(sun4c_context_ring + ctx, entry);
  710. remove_lru(entry);
  711. sun4c_set_context(savectx);
  712. return entry;
  713. unlink_out:
  714. remove_ring(&sun4c_ufree_ring, entry);
  715. return entry;
  716. kunlink_out:
  717. remove_ring(&sun4c_kfree_ring, entry);
  718. return entry;
  719. }
  720. /* NOTE: Must be called with interrupts disabled. */
  721. void sun4c_grow_kernel_ring(void)
  722. {
  723. struct sun4c_mmu_entry *entry;
  724. /* Prevent deadlock condition. */
  725. if (sun4c_user_taken_entries >= max_user_taken_entries)
  726. return;
  727. if (sun4c_ufree_ring.num_entries) {
  728. entry = sun4c_ufree_ring.ringhd.next;
  729. remove_ring(&sun4c_ufree_ring, entry);
  730. add_ring(&sun4c_kfree_ring, entry);
  731. sun4c_user_taken_entries++;
  732. }
  733. }
  734. /* 2 page buckets for task struct and kernel stack allocation.
  735. *
  736. * TASK_STACK_BEGIN
  737. * bucket[0]
  738. * bucket[1]
  739. * [ ... ]
  740. * bucket[NR_TASK_BUCKETS-1]
  741. * TASK_STACK_BEGIN + (sizeof(struct task_bucket) * NR_TASK_BUCKETS)
  742. *
  743. * Each slot looks like:
  744. *
  745. * page 1 -- task struct + beginning of kernel stack
  746. * page 2 -- rest of kernel stack
  747. */
  748. union task_union *sun4c_bucket[NR_TASK_BUCKETS];
  749. static int sun4c_lowbucket_avail;
  750. #define BUCKET_EMPTY ((union task_union *) 0)
  751. #define BUCKET_SHIFT (PAGE_SHIFT + 1) /* log2(sizeof(struct task_bucket)) */
  752. #define BUCKET_SIZE (1 << BUCKET_SHIFT)
  753. #define BUCKET_NUM(addr) ((((addr) - SUN4C_LOCK_VADDR) >> BUCKET_SHIFT))
  754. #define BUCKET_ADDR(num) (((num) << BUCKET_SHIFT) + SUN4C_LOCK_VADDR)
  755. #define BUCKET_PTE(page) \
  756. ((((page) - PAGE_OFFSET) >> PAGE_SHIFT) | pgprot_val(SUN4C_PAGE_KERNEL))
  757. #define BUCKET_PTE_PAGE(pte) \
  758. (PAGE_OFFSET + (((pte) & SUN4C_PFN_MASK) << PAGE_SHIFT))
  759. static void get_locked_segment(unsigned long addr)
  760. {
  761. struct sun4c_mmu_entry *stolen;
  762. unsigned long flags;
  763. local_irq_save(flags);
  764. addr &= SUN4C_REAL_PGDIR_MASK;
  765. stolen = sun4c_user_strategy();
  766. max_user_taken_entries--;
  767. stolen->vaddr = addr;
  768. flush_user_windows();
  769. sun4c_kernel_map(stolen);
  770. local_irq_restore(flags);
  771. }
  772. static void free_locked_segment(unsigned long addr)
  773. {
  774. struct sun4c_mmu_entry *entry;
  775. unsigned long flags;
  776. unsigned char pseg;
  777. local_irq_save(flags);
  778. addr &= SUN4C_REAL_PGDIR_MASK;
  779. pseg = sun4c_get_segmap(addr);
  780. entry = &mmu_entry_pool[pseg];
  781. flush_user_windows();
  782. sun4c_flush_segment(addr);
  783. sun4c_kernel_unmap(entry);
  784. add_ring(&sun4c_ufree_ring, entry);
  785. max_user_taken_entries++;
  786. local_irq_restore(flags);
  787. }
  788. static inline void garbage_collect(int entry)
  789. {
  790. int start, end;
  791. /* 32 buckets per segment... */
  792. entry &= ~31;
  793. start = entry;
  794. for (end = (start + 32); start < end; start++)
  795. if (sun4c_bucket[start] != BUCKET_EMPTY)
  796. return;
  797. /* Entire segment empty, release it. */
  798. free_locked_segment(BUCKET_ADDR(entry));
  799. }
  800. static struct thread_info *sun4c_alloc_thread_info_node(int node)
  801. {
  802. unsigned long addr, pages;
  803. int entry;
  804. pages = __get_free_pages(GFP_KERNEL, THREAD_INFO_ORDER);
  805. if (!pages)
  806. return NULL;
  807. for (entry = sun4c_lowbucket_avail; entry < NR_TASK_BUCKETS; entry++)
  808. if (sun4c_bucket[entry] == BUCKET_EMPTY)
  809. break;
  810. if (entry == NR_TASK_BUCKETS) {
  811. free_pages(pages, THREAD_INFO_ORDER);
  812. return NULL;
  813. }
  814. if (entry >= sun4c_lowbucket_avail)
  815. sun4c_lowbucket_avail = entry + 1;
  816. addr = BUCKET_ADDR(entry);
  817. sun4c_bucket[entry] = (union task_union *) addr;
  818. if(sun4c_get_segmap(addr) == invalid_segment)
  819. get_locked_segment(addr);
  820. /* We are changing the virtual color of the page(s)
  821. * so we must flush the cache to guarantee consistency.
  822. */
  823. sun4c_flush_page(pages);
  824. sun4c_flush_page(pages + PAGE_SIZE);
  825. sun4c_put_pte(addr, BUCKET_PTE(pages));
  826. sun4c_put_pte(addr + PAGE_SIZE, BUCKET_PTE(pages + PAGE_SIZE));
  827. #ifdef CONFIG_DEBUG_STACK_USAGE
  828. memset((void *)addr, 0, PAGE_SIZE << THREAD_INFO_ORDER);
  829. #endif /* DEBUG_STACK_USAGE */
  830. return (struct thread_info *) addr;
  831. }
  832. static void sun4c_free_thread_info(struct thread_info *ti)
  833. {
  834. unsigned long tiaddr = (unsigned long) ti;
  835. unsigned long pages = BUCKET_PTE_PAGE(sun4c_get_pte(tiaddr));
  836. int entry = BUCKET_NUM(tiaddr);
  837. /* We are deleting a mapping, so the flush here is mandatory. */
  838. sun4c_flush_page(tiaddr);
  839. sun4c_flush_page(tiaddr + PAGE_SIZE);
  840. sun4c_put_pte(tiaddr, 0);
  841. sun4c_put_pte(tiaddr + PAGE_SIZE, 0);
  842. sun4c_bucket[entry] = BUCKET_EMPTY;
  843. if (entry < sun4c_lowbucket_avail)
  844. sun4c_lowbucket_avail = entry;
  845. free_pages(pages, THREAD_INFO_ORDER);
  846. garbage_collect(entry);
  847. }
  848. static void __init sun4c_init_buckets(void)
  849. {
  850. int entry;
  851. if (sizeof(union thread_union) != (PAGE_SIZE << THREAD_INFO_ORDER)) {
  852. extern void thread_info_size_is_bolixed_pete(void);
  853. thread_info_size_is_bolixed_pete();
  854. }
  855. for (entry = 0; entry < NR_TASK_BUCKETS; entry++)
  856. sun4c_bucket[entry] = BUCKET_EMPTY;
  857. sun4c_lowbucket_avail = 0;
  858. }
  859. static unsigned long sun4c_iobuffer_start;
  860. static unsigned long sun4c_iobuffer_end;
  861. static unsigned long sun4c_iobuffer_high;
  862. static unsigned long *sun4c_iobuffer_map;
  863. static int iobuffer_map_size;
  864. /*
  865. * Alias our pages so they do not cause a trap.
  866. * Also one page may be aliased into several I/O areas and we may
  867. * finish these I/O separately.
  868. */
  869. static char *sun4c_lockarea(char *vaddr, unsigned long size)
  870. {
  871. unsigned long base, scan;
  872. unsigned long npages;
  873. unsigned long vpage;
  874. unsigned long pte;
  875. unsigned long apage;
  876. unsigned long high;
  877. unsigned long flags;
  878. npages = (((unsigned long)vaddr & ~PAGE_MASK) +
  879. size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  880. local_irq_save(flags);
  881. base = bitmap_find_next_zero_area(sun4c_iobuffer_map, iobuffer_map_size,
  882. 0, npages, 0);
  883. if (base >= iobuffer_map_size)
  884. goto abend;
  885. high = ((base + npages) << PAGE_SHIFT) + sun4c_iobuffer_start;
  886. high = SUN4C_REAL_PGDIR_ALIGN(high);
  887. while (high > sun4c_iobuffer_high) {
  888. get_locked_segment(sun4c_iobuffer_high);
  889. sun4c_iobuffer_high += SUN4C_REAL_PGDIR_SIZE;
  890. }
  891. vpage = ((unsigned long) vaddr) & PAGE_MASK;
  892. for (scan = base; scan < base+npages; scan++) {
  893. pte = ((vpage-PAGE_OFFSET) >> PAGE_SHIFT);
  894. pte |= pgprot_val(SUN4C_PAGE_KERNEL);
  895. pte |= _SUN4C_PAGE_NOCACHE;
  896. set_bit(scan, sun4c_iobuffer_map);
  897. apage = (scan << PAGE_SHIFT) + sun4c_iobuffer_start;
  898. /* Flush original mapping so we see the right things later. */
  899. sun4c_flush_page(vpage);
  900. sun4c_put_pte(apage, pte);
  901. vpage += PAGE_SIZE;
  902. }
  903. local_irq_restore(flags);
  904. return (char *) ((base << PAGE_SHIFT) + sun4c_iobuffer_start +
  905. (((unsigned long) vaddr) & ~PAGE_MASK));
  906. abend:
  907. local_irq_restore(flags);
  908. printk("DMA vaddr=0x%p size=%08lx\n", vaddr, size);
  909. panic("Out of iobuffer table");
  910. return NULL;
  911. }
  912. static void sun4c_unlockarea(char *vaddr, unsigned long size)
  913. {
  914. unsigned long vpage, npages;
  915. unsigned long flags;
  916. int scan, high;
  917. vpage = (unsigned long)vaddr & PAGE_MASK;
  918. npages = (((unsigned long)vaddr & ~PAGE_MASK) +
  919. size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
  920. local_irq_save(flags);
  921. while (npages != 0) {
  922. --npages;
  923. /* This mapping is marked non-cachable, no flush necessary. */
  924. sun4c_put_pte(vpage, 0);
  925. clear_bit((vpage - sun4c_iobuffer_start) >> PAGE_SHIFT,
  926. sun4c_iobuffer_map);
  927. vpage += PAGE_SIZE;
  928. }
  929. /* garbage collect */
  930. scan = (sun4c_iobuffer_high - sun4c_iobuffer_start) >> PAGE_SHIFT;
  931. while (scan >= 0 && !sun4c_iobuffer_map[scan >> 5])
  932. scan -= 32;
  933. scan += 32;
  934. high = sun4c_iobuffer_start + (scan << PAGE_SHIFT);
  935. high = SUN4C_REAL_PGDIR_ALIGN(high) + SUN4C_REAL_PGDIR_SIZE;
  936. while (high < sun4c_iobuffer_high) {
  937. sun4c_iobuffer_high -= SUN4C_REAL_PGDIR_SIZE;
  938. free_locked_segment(sun4c_iobuffer_high);
  939. }
  940. local_irq_restore(flags);
  941. }
  942. /* Note the scsi code at init time passes to here buffers
  943. * which sit on the kernel stack, those are already locked
  944. * by implication and fool the page locking code above
  945. * if passed to by mistake.
  946. */
  947. static __u32 sun4c_get_scsi_one(struct device *dev, char *bufptr, unsigned long len)
  948. {
  949. unsigned long page;
  950. page = ((unsigned long)bufptr) & PAGE_MASK;
  951. if (!virt_addr_valid(page)) {
  952. sun4c_flush_page(page);
  953. return (__u32)bufptr; /* already locked */
  954. }
  955. return (__u32)sun4c_lockarea(bufptr, len);
  956. }
  957. static void sun4c_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  958. {
  959. while (sz != 0) {
  960. --sz;
  961. sg->dma_address = (__u32)sun4c_lockarea(sg_virt(sg), sg->length);
  962. sg->dma_length = sg->length;
  963. sg = sg_next(sg);
  964. }
  965. }
  966. static void sun4c_release_scsi_one(struct device *dev, __u32 bufptr, unsigned long len)
  967. {
  968. if (bufptr < sun4c_iobuffer_start)
  969. return; /* On kernel stack or similar, see above */
  970. sun4c_unlockarea((char *)bufptr, len);
  971. }
  972. static void sun4c_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
  973. {
  974. while (sz != 0) {
  975. --sz;
  976. sun4c_unlockarea((char *)sg->dma_address, sg->length);
  977. sg = sg_next(sg);
  978. }
  979. }
  980. #define TASK_ENTRY_SIZE BUCKET_SIZE /* see above */
  981. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  982. struct vm_area_struct sun4c_kstack_vma;
  983. static void __init sun4c_init_lock_areas(void)
  984. {
  985. unsigned long sun4c_taskstack_start;
  986. unsigned long sun4c_taskstack_end;
  987. int bitmap_size;
  988. sun4c_init_buckets();
  989. sun4c_taskstack_start = SUN4C_LOCK_VADDR;
  990. sun4c_taskstack_end = (sun4c_taskstack_start +
  991. (TASK_ENTRY_SIZE * NR_TASK_BUCKETS));
  992. if (sun4c_taskstack_end >= SUN4C_LOCK_END) {
  993. prom_printf("Too many tasks, decrease NR_TASK_BUCKETS please.\n");
  994. prom_halt();
  995. }
  996. sun4c_iobuffer_start = sun4c_iobuffer_high =
  997. SUN4C_REAL_PGDIR_ALIGN(sun4c_taskstack_end);
  998. sun4c_iobuffer_end = SUN4C_LOCK_END;
  999. bitmap_size = (sun4c_iobuffer_end - sun4c_iobuffer_start) >> PAGE_SHIFT;
  1000. bitmap_size = (bitmap_size + 7) >> 3;
  1001. bitmap_size = LONG_ALIGN(bitmap_size);
  1002. iobuffer_map_size = bitmap_size << 3;
  1003. sun4c_iobuffer_map = __alloc_bootmem(bitmap_size, SMP_CACHE_BYTES, 0UL);
  1004. memset((void *) sun4c_iobuffer_map, 0, bitmap_size);
  1005. sun4c_kstack_vma.vm_mm = &init_mm;
  1006. sun4c_kstack_vma.vm_start = sun4c_taskstack_start;
  1007. sun4c_kstack_vma.vm_end = sun4c_taskstack_end;
  1008. sun4c_kstack_vma.vm_page_prot = PAGE_SHARED;
  1009. sun4c_kstack_vma.vm_flags = VM_READ | VM_WRITE | VM_EXEC;
  1010. insert_vm_struct(&init_mm, &sun4c_kstack_vma);
  1011. }
  1012. /* Cache flushing on the sun4c. */
  1013. static void sun4c_flush_cache_all(void)
  1014. {
  1015. unsigned long begin, end;
  1016. flush_user_windows();
  1017. begin = (KERNBASE + SUN4C_REAL_PGDIR_SIZE);
  1018. end = (begin + SUN4C_VAC_SIZE);
  1019. if (sun4c_vacinfo.linesize == 32) {
  1020. while (begin < end) {
  1021. __asm__ __volatile__(
  1022. "ld [%0 + 0x00], %%g0\n\t"
  1023. "ld [%0 + 0x20], %%g0\n\t"
  1024. "ld [%0 + 0x40], %%g0\n\t"
  1025. "ld [%0 + 0x60], %%g0\n\t"
  1026. "ld [%0 + 0x80], %%g0\n\t"
  1027. "ld [%0 + 0xa0], %%g0\n\t"
  1028. "ld [%0 + 0xc0], %%g0\n\t"
  1029. "ld [%0 + 0xe0], %%g0\n\t"
  1030. "ld [%0 + 0x100], %%g0\n\t"
  1031. "ld [%0 + 0x120], %%g0\n\t"
  1032. "ld [%0 + 0x140], %%g0\n\t"
  1033. "ld [%0 + 0x160], %%g0\n\t"
  1034. "ld [%0 + 0x180], %%g0\n\t"
  1035. "ld [%0 + 0x1a0], %%g0\n\t"
  1036. "ld [%0 + 0x1c0], %%g0\n\t"
  1037. "ld [%0 + 0x1e0], %%g0\n"
  1038. : : "r" (begin));
  1039. begin += 512;
  1040. }
  1041. } else {
  1042. while (begin < end) {
  1043. __asm__ __volatile__(
  1044. "ld [%0 + 0x00], %%g0\n\t"
  1045. "ld [%0 + 0x10], %%g0\n\t"
  1046. "ld [%0 + 0x20], %%g0\n\t"
  1047. "ld [%0 + 0x30], %%g0\n\t"
  1048. "ld [%0 + 0x40], %%g0\n\t"
  1049. "ld [%0 + 0x50], %%g0\n\t"
  1050. "ld [%0 + 0x60], %%g0\n\t"
  1051. "ld [%0 + 0x70], %%g0\n\t"
  1052. "ld [%0 + 0x80], %%g0\n\t"
  1053. "ld [%0 + 0x90], %%g0\n\t"
  1054. "ld [%0 + 0xa0], %%g0\n\t"
  1055. "ld [%0 + 0xb0], %%g0\n\t"
  1056. "ld [%0 + 0xc0], %%g0\n\t"
  1057. "ld [%0 + 0xd0], %%g0\n\t"
  1058. "ld [%0 + 0xe0], %%g0\n\t"
  1059. "ld [%0 + 0xf0], %%g0\n"
  1060. : : "r" (begin));
  1061. begin += 256;
  1062. }
  1063. }
  1064. }
  1065. static void sun4c_flush_cache_mm(struct mm_struct *mm)
  1066. {
  1067. int new_ctx = mm->context;
  1068. if (new_ctx != NO_CONTEXT) {
  1069. flush_user_windows();
  1070. if (sun4c_context_ring[new_ctx].num_entries) {
  1071. struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
  1072. unsigned long flags;
  1073. local_irq_save(flags);
  1074. if (head->next != head) {
  1075. struct sun4c_mmu_entry *entry = head->next;
  1076. int savectx = sun4c_get_context();
  1077. sun4c_set_context(new_ctx);
  1078. sun4c_flush_context();
  1079. do {
  1080. struct sun4c_mmu_entry *next = entry->next;
  1081. sun4c_user_unmap(entry);
  1082. free_user_entry(new_ctx, entry);
  1083. entry = next;
  1084. } while (entry != head);
  1085. sun4c_set_context(savectx);
  1086. }
  1087. local_irq_restore(flags);
  1088. }
  1089. }
  1090. }
  1091. static void sun4c_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1092. {
  1093. struct mm_struct *mm = vma->vm_mm;
  1094. int new_ctx = mm->context;
  1095. if (new_ctx != NO_CONTEXT) {
  1096. struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
  1097. struct sun4c_mmu_entry *entry;
  1098. unsigned long flags;
  1099. flush_user_windows();
  1100. local_irq_save(flags);
  1101. /* All user segmap chains are ordered on entry->vaddr. */
  1102. for (entry = head->next;
  1103. (entry != head) && ((entry->vaddr+SUN4C_REAL_PGDIR_SIZE) < start);
  1104. entry = entry->next)
  1105. ;
  1106. /* Tracing various job mixtures showed that this conditional
  1107. * only passes ~35% of the time for most worse case situations,
  1108. * therefore we avoid all of this gross overhead ~65% of the time.
  1109. */
  1110. if ((entry != head) && (entry->vaddr < end)) {
  1111. int octx = sun4c_get_context();
  1112. sun4c_set_context(new_ctx);
  1113. /* At this point, always, (start >= entry->vaddr) and
  1114. * (entry->vaddr < end), once the latter condition
  1115. * ceases to hold, or we hit the end of the list, we
  1116. * exit the loop. The ordering of all user allocated
  1117. * segmaps makes this all work out so beautifully.
  1118. */
  1119. do {
  1120. struct sun4c_mmu_entry *next = entry->next;
  1121. unsigned long realend;
  1122. /* "realstart" is always >= entry->vaddr */
  1123. realend = entry->vaddr + SUN4C_REAL_PGDIR_SIZE;
  1124. if (end < realend)
  1125. realend = end;
  1126. if ((realend - entry->vaddr) <= (PAGE_SIZE << 3)) {
  1127. unsigned long page = entry->vaddr;
  1128. while (page < realend) {
  1129. sun4c_flush_page(page);
  1130. page += PAGE_SIZE;
  1131. }
  1132. } else {
  1133. sun4c_flush_segment(entry->vaddr);
  1134. sun4c_user_unmap(entry);
  1135. free_user_entry(new_ctx, entry);
  1136. }
  1137. entry = next;
  1138. } while ((entry != head) && (entry->vaddr < end));
  1139. sun4c_set_context(octx);
  1140. }
  1141. local_irq_restore(flags);
  1142. }
  1143. }
  1144. static void sun4c_flush_cache_page(struct vm_area_struct *vma, unsigned long page)
  1145. {
  1146. struct mm_struct *mm = vma->vm_mm;
  1147. int new_ctx = mm->context;
  1148. /* Sun4c has no separate I/D caches so cannot optimize for non
  1149. * text page flushes.
  1150. */
  1151. if (new_ctx != NO_CONTEXT) {
  1152. int octx = sun4c_get_context();
  1153. unsigned long flags;
  1154. flush_user_windows();
  1155. local_irq_save(flags);
  1156. sun4c_set_context(new_ctx);
  1157. sun4c_flush_page(page);
  1158. sun4c_set_context(octx);
  1159. local_irq_restore(flags);
  1160. }
  1161. }
  1162. static void sun4c_flush_page_to_ram(unsigned long page)
  1163. {
  1164. unsigned long flags;
  1165. local_irq_save(flags);
  1166. sun4c_flush_page(page);
  1167. local_irq_restore(flags);
  1168. }
  1169. /* Sun4c cache is unified, both instructions and data live there, so
  1170. * no need to flush the on-stack instructions for new signal handlers.
  1171. */
  1172. static void sun4c_flush_sig_insns(struct mm_struct *mm, unsigned long insn_addr)
  1173. {
  1174. }
  1175. /* TLB flushing on the sun4c. These routines count on the cache
  1176. * flushing code to flush the user register windows so that we need
  1177. * not do so when we get here.
  1178. */
  1179. static void sun4c_flush_tlb_all(void)
  1180. {
  1181. struct sun4c_mmu_entry *this_entry, *next_entry;
  1182. unsigned long flags;
  1183. int savectx, ctx;
  1184. local_irq_save(flags);
  1185. this_entry = sun4c_kernel_ring.ringhd.next;
  1186. savectx = sun4c_get_context();
  1187. flush_user_windows();
  1188. while (sun4c_kernel_ring.num_entries) {
  1189. next_entry = this_entry->next;
  1190. sun4c_flush_segment(this_entry->vaddr);
  1191. for (ctx = 0; ctx < num_contexts; ctx++) {
  1192. sun4c_set_context(ctx);
  1193. sun4c_put_segmap(this_entry->vaddr, invalid_segment);
  1194. }
  1195. free_kernel_entry(this_entry, &sun4c_kernel_ring);
  1196. this_entry = next_entry;
  1197. }
  1198. sun4c_set_context(savectx);
  1199. local_irq_restore(flags);
  1200. }
  1201. static void sun4c_flush_tlb_mm(struct mm_struct *mm)
  1202. {
  1203. int new_ctx = mm->context;
  1204. if (new_ctx != NO_CONTEXT) {
  1205. struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
  1206. unsigned long flags;
  1207. local_irq_save(flags);
  1208. if (head->next != head) {
  1209. struct sun4c_mmu_entry *entry = head->next;
  1210. int savectx = sun4c_get_context();
  1211. sun4c_set_context(new_ctx);
  1212. sun4c_flush_context();
  1213. do {
  1214. struct sun4c_mmu_entry *next = entry->next;
  1215. sun4c_user_unmap(entry);
  1216. free_user_entry(new_ctx, entry);
  1217. entry = next;
  1218. } while (entry != head);
  1219. sun4c_set_context(savectx);
  1220. }
  1221. local_irq_restore(flags);
  1222. }
  1223. }
  1224. static void sun4c_flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  1225. {
  1226. struct mm_struct *mm = vma->vm_mm;
  1227. int new_ctx = mm->context;
  1228. if (new_ctx != NO_CONTEXT) {
  1229. struct sun4c_mmu_entry *head = &sun4c_context_ring[new_ctx].ringhd;
  1230. struct sun4c_mmu_entry *entry;
  1231. unsigned long flags;
  1232. local_irq_save(flags);
  1233. /* See commentary in sun4c_flush_cache_range(). */
  1234. for (entry = head->next;
  1235. (entry != head) && ((entry->vaddr+SUN4C_REAL_PGDIR_SIZE) < start);
  1236. entry = entry->next)
  1237. ;
  1238. if ((entry != head) && (entry->vaddr < end)) {
  1239. int octx = sun4c_get_context();
  1240. sun4c_set_context(new_ctx);
  1241. do {
  1242. struct sun4c_mmu_entry *next = entry->next;
  1243. sun4c_flush_segment(entry->vaddr);
  1244. sun4c_user_unmap(entry);
  1245. free_user_entry(new_ctx, entry);
  1246. entry = next;
  1247. } while ((entry != head) && (entry->vaddr < end));
  1248. sun4c_set_context(octx);
  1249. }
  1250. local_irq_restore(flags);
  1251. }
  1252. }
  1253. static void sun4c_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  1254. {
  1255. struct mm_struct *mm = vma->vm_mm;
  1256. int new_ctx = mm->context;
  1257. if (new_ctx != NO_CONTEXT) {
  1258. int savectx = sun4c_get_context();
  1259. unsigned long flags;
  1260. local_irq_save(flags);
  1261. sun4c_set_context(new_ctx);
  1262. page &= PAGE_MASK;
  1263. sun4c_flush_page(page);
  1264. sun4c_put_pte(page, 0);
  1265. sun4c_set_context(savectx);
  1266. local_irq_restore(flags);
  1267. }
  1268. }
  1269. static inline void sun4c_mapioaddr(unsigned long physaddr, unsigned long virt_addr)
  1270. {
  1271. unsigned long page_entry, pg_iobits;
  1272. pg_iobits = _SUN4C_PAGE_PRESENT | _SUN4C_READABLE | _SUN4C_WRITEABLE |
  1273. _SUN4C_PAGE_IO | _SUN4C_PAGE_NOCACHE;
  1274. page_entry = ((physaddr >> PAGE_SHIFT) & SUN4C_PFN_MASK);
  1275. page_entry |= ((pg_iobits | _SUN4C_PAGE_PRIV) & ~(_SUN4C_PAGE_PRESENT));
  1276. sun4c_put_pte(virt_addr, page_entry);
  1277. }
  1278. static void sun4c_mapiorange(unsigned int bus, unsigned long xpa,
  1279. unsigned long xva, unsigned int len)
  1280. {
  1281. while (len != 0) {
  1282. len -= PAGE_SIZE;
  1283. sun4c_mapioaddr(xpa, xva);
  1284. xva += PAGE_SIZE;
  1285. xpa += PAGE_SIZE;
  1286. }
  1287. }
  1288. static void sun4c_unmapiorange(unsigned long virt_addr, unsigned int len)
  1289. {
  1290. while (len != 0) {
  1291. len -= PAGE_SIZE;
  1292. sun4c_put_pte(virt_addr, 0);
  1293. virt_addr += PAGE_SIZE;
  1294. }
  1295. }
  1296. static void sun4c_alloc_context(struct mm_struct *old_mm, struct mm_struct *mm)
  1297. {
  1298. struct ctx_list *ctxp;
  1299. ctxp = ctx_free.next;
  1300. if (ctxp != &ctx_free) {
  1301. remove_from_ctx_list(ctxp);
  1302. add_to_used_ctxlist(ctxp);
  1303. mm->context = ctxp->ctx_number;
  1304. ctxp->ctx_mm = mm;
  1305. return;
  1306. }
  1307. ctxp = ctx_used.next;
  1308. if (ctxp->ctx_mm == old_mm)
  1309. ctxp = ctxp->next;
  1310. remove_from_ctx_list(ctxp);
  1311. add_to_used_ctxlist(ctxp);
  1312. ctxp->ctx_mm->context = NO_CONTEXT;
  1313. ctxp->ctx_mm = mm;
  1314. mm->context = ctxp->ctx_number;
  1315. sun4c_demap_context(&sun4c_context_ring[ctxp->ctx_number],
  1316. ctxp->ctx_number);
  1317. }
  1318. /* Switch the current MM context. */
  1319. static void sun4c_switch_mm(struct mm_struct *old_mm, struct mm_struct *mm, struct task_struct *tsk, int cpu)
  1320. {
  1321. struct ctx_list *ctx;
  1322. int dirty = 0;
  1323. if (mm->context == NO_CONTEXT) {
  1324. dirty = 1;
  1325. sun4c_alloc_context(old_mm, mm);
  1326. } else {
  1327. /* Update the LRU ring of contexts. */
  1328. ctx = ctx_list_pool + mm->context;
  1329. remove_from_ctx_list(ctx);
  1330. add_to_used_ctxlist(ctx);
  1331. }
  1332. if (dirty || old_mm != mm)
  1333. sun4c_set_context(mm->context);
  1334. }
  1335. static void sun4c_destroy_context(struct mm_struct *mm)
  1336. {
  1337. struct ctx_list *ctx_old;
  1338. if (mm->context != NO_CONTEXT) {
  1339. sun4c_demap_context(&sun4c_context_ring[mm->context], mm->context);
  1340. ctx_old = ctx_list_pool + mm->context;
  1341. remove_from_ctx_list(ctx_old);
  1342. add_to_free_ctxlist(ctx_old);
  1343. mm->context = NO_CONTEXT;
  1344. }
  1345. }
  1346. static void sun4c_mmu_info(struct seq_file *m)
  1347. {
  1348. int used_user_entries, i;
  1349. used_user_entries = 0;
  1350. for (i = 0; i < num_contexts; i++)
  1351. used_user_entries += sun4c_context_ring[i].num_entries;
  1352. seq_printf(m,
  1353. "vacsize\t\t: %d bytes\n"
  1354. "vachwflush\t: %s\n"
  1355. "vaclinesize\t: %d bytes\n"
  1356. "mmuctxs\t\t: %d\n"
  1357. "mmupsegs\t: %d\n"
  1358. "kernelpsegs\t: %d\n"
  1359. "kfreepsegs\t: %d\n"
  1360. "usedpsegs\t: %d\n"
  1361. "ufreepsegs\t: %d\n"
  1362. "user_taken\t: %d\n"
  1363. "max_taken\t: %d\n",
  1364. sun4c_vacinfo.num_bytes,
  1365. (sun4c_vacinfo.do_hwflushes ? "yes" : "no"),
  1366. sun4c_vacinfo.linesize,
  1367. num_contexts,
  1368. (invalid_segment + 1),
  1369. sun4c_kernel_ring.num_entries,
  1370. sun4c_kfree_ring.num_entries,
  1371. used_user_entries,
  1372. sun4c_ufree_ring.num_entries,
  1373. sun4c_user_taken_entries,
  1374. max_user_taken_entries);
  1375. }
  1376. /* Nothing below here should touch the mmu hardware nor the mmu_entry
  1377. * data structures.
  1378. */
  1379. /* First the functions which the mid-level code uses to directly
  1380. * manipulate the software page tables. Some defines since we are
  1381. * emulating the i386 page directory layout.
  1382. */
  1383. #define PGD_PRESENT 0x001
  1384. #define PGD_RW 0x002
  1385. #define PGD_USER 0x004
  1386. #define PGD_ACCESSED 0x020
  1387. #define PGD_DIRTY 0x040
  1388. #define PGD_TABLE (PGD_PRESENT | PGD_RW | PGD_USER | PGD_ACCESSED | PGD_DIRTY)
  1389. static void sun4c_set_pte(pte_t *ptep, pte_t pte)
  1390. {
  1391. *ptep = pte;
  1392. }
  1393. static void sun4c_pgd_set(pgd_t * pgdp, pmd_t * pmdp)
  1394. {
  1395. }
  1396. static void sun4c_pmd_set(pmd_t * pmdp, pte_t * ptep)
  1397. {
  1398. pmdp->pmdv[0] = PGD_TABLE | (unsigned long) ptep;
  1399. }
  1400. static void sun4c_pmd_populate(pmd_t * pmdp, struct page * ptep)
  1401. {
  1402. if (page_address(ptep) == NULL) BUG(); /* No highmem on sun4c */
  1403. pmdp->pmdv[0] = PGD_TABLE | (unsigned long) page_address(ptep);
  1404. }
  1405. static int sun4c_pte_present(pte_t pte)
  1406. {
  1407. return ((pte_val(pte) & (_SUN4C_PAGE_PRESENT | _SUN4C_PAGE_PRIV)) != 0);
  1408. }
  1409. static void sun4c_pte_clear(pte_t *ptep) { *ptep = __pte(0); }
  1410. static int sun4c_pmd_bad(pmd_t pmd)
  1411. {
  1412. return (((pmd_val(pmd) & ~PAGE_MASK) != PGD_TABLE) ||
  1413. (!virt_addr_valid(pmd_val(pmd))));
  1414. }
  1415. static int sun4c_pmd_present(pmd_t pmd)
  1416. {
  1417. return ((pmd_val(pmd) & PGD_PRESENT) != 0);
  1418. }
  1419. #if 0 /* if PMD takes one word */
  1420. static void sun4c_pmd_clear(pmd_t *pmdp) { *pmdp = __pmd(0); }
  1421. #else /* if pmd_t is a longish aggregate */
  1422. static void sun4c_pmd_clear(pmd_t *pmdp) {
  1423. memset((void *)pmdp, 0, sizeof(pmd_t));
  1424. }
  1425. #endif
  1426. static int sun4c_pgd_none(pgd_t pgd) { return 0; }
  1427. static int sun4c_pgd_bad(pgd_t pgd) { return 0; }
  1428. static int sun4c_pgd_present(pgd_t pgd) { return 1; }
  1429. static void sun4c_pgd_clear(pgd_t * pgdp) { }
  1430. /*
  1431. * The following only work if pte_present() is true.
  1432. * Undefined behaviour if not..
  1433. */
  1434. static pte_t sun4c_pte_mkwrite(pte_t pte)
  1435. {
  1436. pte = __pte(pte_val(pte) | _SUN4C_PAGE_WRITE);
  1437. if (pte_val(pte) & _SUN4C_PAGE_MODIFIED)
  1438. pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_WRITE);
  1439. return pte;
  1440. }
  1441. static pte_t sun4c_pte_mkdirty(pte_t pte)
  1442. {
  1443. pte = __pte(pte_val(pte) | _SUN4C_PAGE_MODIFIED);
  1444. if (pte_val(pte) & _SUN4C_PAGE_WRITE)
  1445. pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_WRITE);
  1446. return pte;
  1447. }
  1448. static pte_t sun4c_pte_mkyoung(pte_t pte)
  1449. {
  1450. pte = __pte(pte_val(pte) | _SUN4C_PAGE_ACCESSED);
  1451. if (pte_val(pte) & _SUN4C_PAGE_READ)
  1452. pte = __pte(pte_val(pte) | _SUN4C_PAGE_SILENT_READ);
  1453. return pte;
  1454. }
  1455. /*
  1456. * Conversion functions: convert a page and protection to a page entry,
  1457. * and a page entry and page directory to the page they refer to.
  1458. */
  1459. static pte_t sun4c_mk_pte(struct page *page, pgprot_t pgprot)
  1460. {
  1461. return __pte(page_to_pfn(page) | pgprot_val(pgprot));
  1462. }
  1463. static pte_t sun4c_mk_pte_phys(unsigned long phys_page, pgprot_t pgprot)
  1464. {
  1465. return __pte((phys_page >> PAGE_SHIFT) | pgprot_val(pgprot));
  1466. }
  1467. static pte_t sun4c_mk_pte_io(unsigned long page, pgprot_t pgprot, int space)
  1468. {
  1469. return __pte(((page - PAGE_OFFSET) >> PAGE_SHIFT) | pgprot_val(pgprot));
  1470. }
  1471. static unsigned long sun4c_pte_pfn(pte_t pte)
  1472. {
  1473. return pte_val(pte) & SUN4C_PFN_MASK;
  1474. }
  1475. static pte_t sun4c_pgoff_to_pte(unsigned long pgoff)
  1476. {
  1477. return __pte(pgoff | _SUN4C_PAGE_FILE);
  1478. }
  1479. static unsigned long sun4c_pte_to_pgoff(pte_t pte)
  1480. {
  1481. return pte_val(pte) & ((1UL << PTE_FILE_MAX_BITS) - 1);
  1482. }
  1483. static inline unsigned long sun4c_pmd_page_v(pmd_t pmd)
  1484. {
  1485. return (pmd_val(pmd) & PAGE_MASK);
  1486. }
  1487. static struct page *sun4c_pmd_page(pmd_t pmd)
  1488. {
  1489. return virt_to_page(sun4c_pmd_page_v(pmd));
  1490. }
  1491. static unsigned long sun4c_pgd_page(pgd_t pgd) { return 0; }
  1492. /* to find an entry in a page-table-directory */
  1493. static inline pgd_t *sun4c_pgd_offset(struct mm_struct * mm, unsigned long address)
  1494. {
  1495. return mm->pgd + (address >> SUN4C_PGDIR_SHIFT);
  1496. }
  1497. /* Find an entry in the second-level page table.. */
  1498. static pmd_t *sun4c_pmd_offset(pgd_t * dir, unsigned long address)
  1499. {
  1500. return (pmd_t *) dir;
  1501. }
  1502. /* Find an entry in the third-level page table.. */
  1503. pte_t *sun4c_pte_offset_kernel(pmd_t * dir, unsigned long address)
  1504. {
  1505. return (pte_t *) sun4c_pmd_page_v(*dir) +
  1506. ((address >> PAGE_SHIFT) & (SUN4C_PTRS_PER_PTE - 1));
  1507. }
  1508. static unsigned long sun4c_swp_type(swp_entry_t entry)
  1509. {
  1510. return (entry.val & SUN4C_SWP_TYPE_MASK);
  1511. }
  1512. static unsigned long sun4c_swp_offset(swp_entry_t entry)
  1513. {
  1514. return (entry.val >> SUN4C_SWP_OFF_SHIFT) & SUN4C_SWP_OFF_MASK;
  1515. }
  1516. static swp_entry_t sun4c_swp_entry(unsigned long type, unsigned long offset)
  1517. {
  1518. return (swp_entry_t) {
  1519. (offset & SUN4C_SWP_OFF_MASK) << SUN4C_SWP_OFF_SHIFT
  1520. | (type & SUN4C_SWP_TYPE_MASK) };
  1521. }
  1522. static void sun4c_free_pte_slow(pte_t *pte)
  1523. {
  1524. free_page((unsigned long)pte);
  1525. }
  1526. static void sun4c_free_pgd_slow(pgd_t *pgd)
  1527. {
  1528. free_page((unsigned long)pgd);
  1529. }
  1530. static pgd_t *sun4c_get_pgd_fast(void)
  1531. {
  1532. unsigned long *ret;
  1533. if ((ret = pgd_quicklist) != NULL) {
  1534. pgd_quicklist = (unsigned long *)(*ret);
  1535. ret[0] = ret[1];
  1536. pgtable_cache_size--;
  1537. } else {
  1538. pgd_t *init;
  1539. ret = (unsigned long *)__get_free_page(GFP_KERNEL);
  1540. memset (ret, 0, (KERNBASE / SUN4C_PGDIR_SIZE) * sizeof(pgd_t));
  1541. init = sun4c_pgd_offset(&init_mm, 0);
  1542. memcpy (((pgd_t *)ret) + USER_PTRS_PER_PGD, init + USER_PTRS_PER_PGD,
  1543. (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
  1544. }
  1545. return (pgd_t *)ret;
  1546. }
  1547. static void sun4c_free_pgd_fast(pgd_t *pgd)
  1548. {
  1549. *(unsigned long *)pgd = (unsigned long) pgd_quicklist;
  1550. pgd_quicklist = (unsigned long *) pgd;
  1551. pgtable_cache_size++;
  1552. }
  1553. static inline pte_t *
  1554. sun4c_pte_alloc_one_fast(struct mm_struct *mm, unsigned long address)
  1555. {
  1556. unsigned long *ret;
  1557. if ((ret = (unsigned long *)pte_quicklist) != NULL) {
  1558. pte_quicklist = (unsigned long *)(*ret);
  1559. ret[0] = ret[1];
  1560. pgtable_cache_size--;
  1561. }
  1562. return (pte_t *)ret;
  1563. }
  1564. static pte_t *sun4c_pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
  1565. {
  1566. pte_t *pte;
  1567. if ((pte = sun4c_pte_alloc_one_fast(mm, address)) != NULL)
  1568. return pte;
  1569. pte = (pte_t *)get_zeroed_page(GFP_KERNEL|__GFP_REPEAT);
  1570. return pte;
  1571. }
  1572. static pgtable_t sun4c_pte_alloc_one(struct mm_struct *mm, unsigned long address)
  1573. {
  1574. pte_t *pte;
  1575. struct page *page;
  1576. pte = sun4c_pte_alloc_one_kernel(mm, address);
  1577. if (pte == NULL)
  1578. return NULL;
  1579. page = virt_to_page(pte);
  1580. pgtable_page_ctor(page);
  1581. return page;
  1582. }
  1583. static inline void sun4c_free_pte_fast(pte_t *pte)
  1584. {
  1585. *(unsigned long *)pte = (unsigned long) pte_quicklist;
  1586. pte_quicklist = (unsigned long *) pte;
  1587. pgtable_cache_size++;
  1588. }
  1589. static void sun4c_pte_free(pgtable_t pte)
  1590. {
  1591. pgtable_page_dtor(pte);
  1592. sun4c_free_pte_fast(page_address(pte));
  1593. }
  1594. /*
  1595. * allocating and freeing a pmd is trivial: the 1-entry pmd is
  1596. * inside the pgd, so has no extra memory associated with it.
  1597. */
  1598. static pmd_t *sun4c_pmd_alloc_one(struct mm_struct *mm, unsigned long address)
  1599. {
  1600. BUG();
  1601. return NULL;
  1602. }
  1603. static void sun4c_free_pmd_fast(pmd_t * pmd) { }
  1604. static void sun4c_check_pgt_cache(int low, int high)
  1605. {
  1606. if (pgtable_cache_size > high) {
  1607. do {
  1608. if (pgd_quicklist)
  1609. sun4c_free_pgd_slow(sun4c_get_pgd_fast());
  1610. if (pte_quicklist)
  1611. sun4c_free_pte_slow(sun4c_pte_alloc_one_fast(NULL, 0));
  1612. } while (pgtable_cache_size > low);
  1613. }
  1614. }
  1615. /* An experiment, turn off by default for now... -DaveM */
  1616. #define SUN4C_PRELOAD_PSEG
  1617. void sun4c_update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
  1618. {
  1619. unsigned long flags;
  1620. int pseg;
  1621. if (vma->vm_mm->context == NO_CONTEXT)
  1622. return;
  1623. local_irq_save(flags);
  1624. address &= PAGE_MASK;
  1625. if ((pseg = sun4c_get_segmap(address)) == invalid_segment) {
  1626. struct sun4c_mmu_entry *entry = sun4c_user_strategy();
  1627. struct mm_struct *mm = vma->vm_mm;
  1628. unsigned long start, end;
  1629. entry->vaddr = start = (address & SUN4C_REAL_PGDIR_MASK);
  1630. entry->ctx = mm->context;
  1631. add_ring_ordered(sun4c_context_ring + mm->context, entry);
  1632. sun4c_put_segmap(entry->vaddr, entry->pseg);
  1633. end = start + SUN4C_REAL_PGDIR_SIZE;
  1634. while (start < end) {
  1635. #ifdef SUN4C_PRELOAD_PSEG
  1636. pgd_t *pgdp = sun4c_pgd_offset(mm, start);
  1637. pte_t *ptep;
  1638. if (!pgdp)
  1639. goto no_mapping;
  1640. ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, start);
  1641. if (!ptep || !(pte_val(*ptep) & _SUN4C_PAGE_PRESENT))
  1642. goto no_mapping;
  1643. sun4c_put_pte(start, pte_val(*ptep));
  1644. goto next;
  1645. no_mapping:
  1646. #endif
  1647. sun4c_put_pte(start, 0);
  1648. #ifdef SUN4C_PRELOAD_PSEG
  1649. next:
  1650. #endif
  1651. start += PAGE_SIZE;
  1652. }
  1653. #ifndef SUN4C_PRELOAD_PSEG
  1654. sun4c_put_pte(address, pte_val(*ptep));
  1655. #endif
  1656. local_irq_restore(flags);
  1657. return;
  1658. } else {
  1659. struct sun4c_mmu_entry *entry = &mmu_entry_pool[pseg];
  1660. remove_lru(entry);
  1661. add_lru(entry);
  1662. }
  1663. sun4c_put_pte(address, pte_val(*ptep));
  1664. local_irq_restore(flags);
  1665. }
  1666. extern void sparc_context_init(int);
  1667. extern unsigned long bootmem_init(unsigned long *pages_avail);
  1668. extern unsigned long last_valid_pfn;
  1669. void __init sun4c_paging_init(void)
  1670. {
  1671. int i, cnt;
  1672. unsigned long kernel_end, vaddr;
  1673. extern struct resource sparc_iomap;
  1674. unsigned long end_pfn, pages_avail;
  1675. kernel_end = (unsigned long) &_end;
  1676. kernel_end = SUN4C_REAL_PGDIR_ALIGN(kernel_end);
  1677. pages_avail = 0;
  1678. last_valid_pfn = bootmem_init(&pages_avail);
  1679. end_pfn = last_valid_pfn;
  1680. sun4c_probe_mmu();
  1681. invalid_segment = (num_segmaps - 1);
  1682. sun4c_init_mmu_entry_pool();
  1683. sun4c_init_rings();
  1684. sun4c_init_map_kernelprom(kernel_end);
  1685. sun4c_init_clean_mmu(kernel_end);
  1686. sun4c_init_fill_kernel_ring(SUN4C_KERNEL_BUCKETS);
  1687. sun4c_init_lock_area(sparc_iomap.start, IOBASE_END);
  1688. sun4c_init_lock_area(DVMA_VADDR, DVMA_END);
  1689. sun4c_init_lock_areas();
  1690. sun4c_init_fill_user_ring();
  1691. sun4c_set_context(0);
  1692. memset(swapper_pg_dir, 0, PAGE_SIZE);
  1693. memset(pg0, 0, PAGE_SIZE);
  1694. memset(pg1, 0, PAGE_SIZE);
  1695. memset(pg2, 0, PAGE_SIZE);
  1696. memset(pg3, 0, PAGE_SIZE);
  1697. /* Save work later. */
  1698. vaddr = VMALLOC_START;
  1699. swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg0);
  1700. vaddr += SUN4C_PGDIR_SIZE;
  1701. swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg1);
  1702. vaddr += SUN4C_PGDIR_SIZE;
  1703. swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg2);
  1704. vaddr += SUN4C_PGDIR_SIZE;
  1705. swapper_pg_dir[vaddr>>SUN4C_PGDIR_SHIFT] = __pgd(PGD_TABLE | (unsigned long) pg3);
  1706. sun4c_init_ss2_cache_bug();
  1707. sparc_context_init(num_contexts);
  1708. {
  1709. unsigned long zones_size[MAX_NR_ZONES];
  1710. unsigned long zholes_size[MAX_NR_ZONES];
  1711. unsigned long npages;
  1712. int znum;
  1713. for (znum = 0; znum < MAX_NR_ZONES; znum++)
  1714. zones_size[znum] = zholes_size[znum] = 0;
  1715. npages = max_low_pfn - pfn_base;
  1716. zones_size[ZONE_DMA] = npages;
  1717. zholes_size[ZONE_DMA] = npages - pages_avail;
  1718. npages = highend_pfn - max_low_pfn;
  1719. zones_size[ZONE_HIGHMEM] = npages;
  1720. zholes_size[ZONE_HIGHMEM] = npages - calc_highpages();
  1721. free_area_init_node(0, zones_size, pfn_base, zholes_size);
  1722. }
  1723. cnt = 0;
  1724. for (i = 0; i < num_segmaps; i++)
  1725. if (mmu_entry_pool[i].locked)
  1726. cnt++;
  1727. max_user_taken_entries = num_segmaps - cnt - 40 - 1;
  1728. printk("SUN4C: %d mmu entries for the kernel\n", cnt);
  1729. }
  1730. static pgprot_t sun4c_pgprot_noncached(pgprot_t prot)
  1731. {
  1732. prot |= __pgprot(_SUN4C_PAGE_IO | _SUN4C_PAGE_NOCACHE);
  1733. return prot;
  1734. }
  1735. /* Load up routines and constants for sun4c mmu */
  1736. void __init ld_mmu_sun4c(void)
  1737. {
  1738. extern void ___xchg32_sun4c(void);
  1739. printk("Loading sun4c MMU routines\n");
  1740. /* First the constants */
  1741. BTFIXUPSET_SIMM13(pgdir_shift, SUN4C_PGDIR_SHIFT);
  1742. BTFIXUPSET_SETHI(pgdir_size, SUN4C_PGDIR_SIZE);
  1743. BTFIXUPSET_SETHI(pgdir_mask, SUN4C_PGDIR_MASK);
  1744. BTFIXUPSET_SIMM13(ptrs_per_pmd, SUN4C_PTRS_PER_PMD);
  1745. BTFIXUPSET_SIMM13(ptrs_per_pgd, SUN4C_PTRS_PER_PGD);
  1746. BTFIXUPSET_SIMM13(user_ptrs_per_pgd, KERNBASE / SUN4C_PGDIR_SIZE);
  1747. BTFIXUPSET_INT(page_none, pgprot_val(SUN4C_PAGE_NONE));
  1748. PAGE_SHARED = pgprot_val(SUN4C_PAGE_SHARED);
  1749. BTFIXUPSET_INT(page_copy, pgprot_val(SUN4C_PAGE_COPY));
  1750. BTFIXUPSET_INT(page_readonly, pgprot_val(SUN4C_PAGE_READONLY));
  1751. BTFIXUPSET_INT(page_kernel, pgprot_val(SUN4C_PAGE_KERNEL));
  1752. page_kernel = pgprot_val(SUN4C_PAGE_KERNEL);
  1753. /* Functions */
  1754. BTFIXUPSET_CALL(pgprot_noncached, sun4c_pgprot_noncached, BTFIXUPCALL_NORM);
  1755. BTFIXUPSET_CALL(___xchg32, ___xchg32_sun4c, BTFIXUPCALL_NORM);
  1756. BTFIXUPSET_CALL(do_check_pgt_cache, sun4c_check_pgt_cache, BTFIXUPCALL_NORM);
  1757. BTFIXUPSET_CALL(flush_cache_all, sun4c_flush_cache_all, BTFIXUPCALL_NORM);
  1758. if (sun4c_vacinfo.do_hwflushes) {
  1759. BTFIXUPSET_CALL(sun4c_flush_page, sun4c_flush_page_hw, BTFIXUPCALL_NORM);
  1760. BTFIXUPSET_CALL(sun4c_flush_segment, sun4c_flush_segment_hw, BTFIXUPCALL_NORM);
  1761. BTFIXUPSET_CALL(sun4c_flush_context, sun4c_flush_context_hw, BTFIXUPCALL_NORM);
  1762. } else {
  1763. BTFIXUPSET_CALL(sun4c_flush_page, sun4c_flush_page_sw, BTFIXUPCALL_NORM);
  1764. BTFIXUPSET_CALL(sun4c_flush_segment, sun4c_flush_segment_sw, BTFIXUPCALL_NORM);
  1765. BTFIXUPSET_CALL(sun4c_flush_context, sun4c_flush_context_sw, BTFIXUPCALL_NORM);
  1766. }
  1767. BTFIXUPSET_CALL(flush_tlb_mm, sun4c_flush_tlb_mm, BTFIXUPCALL_NORM);
  1768. BTFIXUPSET_CALL(flush_cache_mm, sun4c_flush_cache_mm, BTFIXUPCALL_NORM);
  1769. BTFIXUPSET_CALL(destroy_context, sun4c_destroy_context, BTFIXUPCALL_NORM);
  1770. BTFIXUPSET_CALL(switch_mm, sun4c_switch_mm, BTFIXUPCALL_NORM);
  1771. BTFIXUPSET_CALL(flush_cache_page, sun4c_flush_cache_page, BTFIXUPCALL_NORM);
  1772. BTFIXUPSET_CALL(flush_tlb_page, sun4c_flush_tlb_page, BTFIXUPCALL_NORM);
  1773. BTFIXUPSET_CALL(flush_tlb_range, sun4c_flush_tlb_range, BTFIXUPCALL_NORM);
  1774. BTFIXUPSET_CALL(flush_cache_range, sun4c_flush_cache_range, BTFIXUPCALL_NORM);
  1775. BTFIXUPSET_CALL(__flush_page_to_ram, sun4c_flush_page_to_ram, BTFIXUPCALL_NORM);
  1776. BTFIXUPSET_CALL(flush_tlb_all, sun4c_flush_tlb_all, BTFIXUPCALL_NORM);
  1777. BTFIXUPSET_CALL(flush_sig_insns, sun4c_flush_sig_insns, BTFIXUPCALL_NOP);
  1778. BTFIXUPSET_CALL(set_pte, sun4c_set_pte, BTFIXUPCALL_STO1O0);
  1779. BTFIXUPSET_CALL(pte_pfn, sun4c_pte_pfn, BTFIXUPCALL_NORM);
  1780. #if 0 /* PAGE_SHIFT <= 12 */ /* Eek. Investigate. XXX */
  1781. BTFIXUPSET_CALL(pmd_page, sun4c_pmd_page, BTFIXUPCALL_ANDNINT(PAGE_SIZE - 1));
  1782. #else
  1783. BTFIXUPSET_CALL(pmd_page, sun4c_pmd_page, BTFIXUPCALL_NORM);
  1784. #endif
  1785. BTFIXUPSET_CALL(pmd_set, sun4c_pmd_set, BTFIXUPCALL_NORM);
  1786. BTFIXUPSET_CALL(pmd_populate, sun4c_pmd_populate, BTFIXUPCALL_NORM);
  1787. BTFIXUPSET_CALL(pte_present, sun4c_pte_present, BTFIXUPCALL_NORM);
  1788. BTFIXUPSET_CALL(pte_clear, sun4c_pte_clear, BTFIXUPCALL_STG0O0);
  1789. BTFIXUPSET_CALL(pmd_bad, sun4c_pmd_bad, BTFIXUPCALL_NORM);
  1790. BTFIXUPSET_CALL(pmd_present, sun4c_pmd_present, BTFIXUPCALL_NORM);
  1791. BTFIXUPSET_CALL(pmd_clear, sun4c_pmd_clear, BTFIXUPCALL_STG0O0);
  1792. BTFIXUPSET_CALL(pgd_none, sun4c_pgd_none, BTFIXUPCALL_RETINT(0));
  1793. BTFIXUPSET_CALL(pgd_bad, sun4c_pgd_bad, BTFIXUPCALL_RETINT(0));
  1794. BTFIXUPSET_CALL(pgd_present, sun4c_pgd_present, BTFIXUPCALL_RETINT(1));
  1795. BTFIXUPSET_CALL(pgd_clear, sun4c_pgd_clear, BTFIXUPCALL_NOP);
  1796. BTFIXUPSET_CALL(mk_pte, sun4c_mk_pte, BTFIXUPCALL_NORM);
  1797. BTFIXUPSET_CALL(mk_pte_phys, sun4c_mk_pte_phys, BTFIXUPCALL_NORM);
  1798. BTFIXUPSET_CALL(mk_pte_io, sun4c_mk_pte_io, BTFIXUPCALL_NORM);
  1799. BTFIXUPSET_INT(pte_modify_mask, _SUN4C_PAGE_CHG_MASK);
  1800. BTFIXUPSET_CALL(pmd_offset, sun4c_pmd_offset, BTFIXUPCALL_NORM);
  1801. BTFIXUPSET_CALL(pte_offset_kernel, sun4c_pte_offset_kernel, BTFIXUPCALL_NORM);
  1802. BTFIXUPSET_CALL(free_pte_fast, sun4c_free_pte_fast, BTFIXUPCALL_NORM);
  1803. BTFIXUPSET_CALL(pte_free, sun4c_pte_free, BTFIXUPCALL_NORM);
  1804. BTFIXUPSET_CALL(pte_alloc_one_kernel, sun4c_pte_alloc_one_kernel, BTFIXUPCALL_NORM);
  1805. BTFIXUPSET_CALL(pte_alloc_one, sun4c_pte_alloc_one, BTFIXUPCALL_NORM);
  1806. BTFIXUPSET_CALL(free_pmd_fast, sun4c_free_pmd_fast, BTFIXUPCALL_NOP);
  1807. BTFIXUPSET_CALL(pmd_alloc_one, sun4c_pmd_alloc_one, BTFIXUPCALL_RETO0);
  1808. BTFIXUPSET_CALL(free_pgd_fast, sun4c_free_pgd_fast, BTFIXUPCALL_NORM);
  1809. BTFIXUPSET_CALL(get_pgd_fast, sun4c_get_pgd_fast, BTFIXUPCALL_NORM);
  1810. BTFIXUPSET_HALF(pte_writei, _SUN4C_PAGE_WRITE);
  1811. BTFIXUPSET_HALF(pte_dirtyi, _SUN4C_PAGE_MODIFIED);
  1812. BTFIXUPSET_HALF(pte_youngi, _SUN4C_PAGE_ACCESSED);
  1813. BTFIXUPSET_HALF(pte_filei, _SUN4C_PAGE_FILE);
  1814. BTFIXUPSET_HALF(pte_wrprotecti, _SUN4C_PAGE_WRITE|_SUN4C_PAGE_SILENT_WRITE);
  1815. BTFIXUPSET_HALF(pte_mkcleani, _SUN4C_PAGE_MODIFIED|_SUN4C_PAGE_SILENT_WRITE);
  1816. BTFIXUPSET_HALF(pte_mkoldi, _SUN4C_PAGE_ACCESSED|_SUN4C_PAGE_SILENT_READ);
  1817. BTFIXUPSET_CALL(pte_mkwrite, sun4c_pte_mkwrite, BTFIXUPCALL_NORM);
  1818. BTFIXUPSET_CALL(pte_mkdirty, sun4c_pte_mkdirty, BTFIXUPCALL_NORM);
  1819. BTFIXUPSET_CALL(pte_mkyoung, sun4c_pte_mkyoung, BTFIXUPCALL_NORM);
  1820. BTFIXUPSET_CALL(update_mmu_cache, sun4c_update_mmu_cache, BTFIXUPCALL_NORM);
  1821. BTFIXUPSET_CALL(pte_to_pgoff, sun4c_pte_to_pgoff, BTFIXUPCALL_NORM);
  1822. BTFIXUPSET_CALL(pgoff_to_pte, sun4c_pgoff_to_pte, BTFIXUPCALL_NORM);
  1823. BTFIXUPSET_CALL(mmu_lockarea, sun4c_lockarea, BTFIXUPCALL_NORM);
  1824. BTFIXUPSET_CALL(mmu_unlockarea, sun4c_unlockarea, BTFIXUPCALL_NORM);
  1825. BTFIXUPSET_CALL(mmu_get_scsi_one, sun4c_get_scsi_one, BTFIXUPCALL_NORM);
  1826. BTFIXUPSET_CALL(mmu_get_scsi_sgl, sun4c_get_scsi_sgl, BTFIXUPCALL_NORM);
  1827. BTFIXUPSET_CALL(mmu_release_scsi_one, sun4c_release_scsi_one, BTFIXUPCALL_NORM);
  1828. BTFIXUPSET_CALL(mmu_release_scsi_sgl, sun4c_release_scsi_sgl, BTFIXUPCALL_NORM);
  1829. BTFIXUPSET_CALL(mmu_map_dma_area, sun4c_map_dma_area, BTFIXUPCALL_NORM);
  1830. BTFIXUPSET_CALL(mmu_unmap_dma_area, sun4c_unmap_dma_area, BTFIXUPCALL_NORM);
  1831. BTFIXUPSET_CALL(sparc_mapiorange, sun4c_mapiorange, BTFIXUPCALL_NORM);
  1832. BTFIXUPSET_CALL(sparc_unmapiorange, sun4c_unmapiorange, BTFIXUPCALL_NORM);
  1833. BTFIXUPSET_CALL(__swp_type, sun4c_swp_type, BTFIXUPCALL_NORM);
  1834. BTFIXUPSET_CALL(__swp_offset, sun4c_swp_offset, BTFIXUPCALL_NORM);
  1835. BTFIXUPSET_CALL(__swp_entry, sun4c_swp_entry, BTFIXUPCALL_NORM);
  1836. BTFIXUPSET_CALL(alloc_thread_info_node, sun4c_alloc_thread_info_node, BTFIXUPCALL_NORM);
  1837. BTFIXUPSET_CALL(free_thread_info, sun4c_free_thread_info, BTFIXUPCALL_NORM);
  1838. BTFIXUPSET_CALL(mmu_info, sun4c_mmu_info, BTFIXUPCALL_NORM);
  1839. /* These should _never_ get called with two level tables. */
  1840. BTFIXUPSET_CALL(pgd_set, sun4c_pgd_set, BTFIXUPCALL_NOP);
  1841. BTFIXUPSET_CALL(pgd_page_vaddr, sun4c_pgd_page, BTFIXUPCALL_RETO0);
  1842. }