18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * viking.h:  Defines specific to the GNU/Viking MBUS module.
48c2ecf20Sopenharmony_ci *            This is SRMMU stuff.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#ifndef _SPARC_VIKING_H
98c2ecf20Sopenharmony_ci#define _SPARC_VIKING_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/asi.h>
128c2ecf20Sopenharmony_ci#include <asm/mxcc.h>
138c2ecf20Sopenharmony_ci#include <asm/pgtable.h>
148c2ecf20Sopenharmony_ci#include <asm/pgtsrmmu.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* Bits in the SRMMU control register for GNU/Viking modules.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * -----------------------------------------------------------
198c2ecf20Sopenharmony_ci * |impl-vers| RSV |TC|AC|SP|BM|PC|MBM|SB|IC|DC|PSO|RSV|NF|ME|
208c2ecf20Sopenharmony_ci * -----------------------------------------------------------
218c2ecf20Sopenharmony_ci *  31     24 23-17 16 15 14 13 12 11  10  9  8  7  6-2  1  0
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * TC: Tablewalk Cacheable -- 0 = Twalks are not cacheable in E-cache
248c2ecf20Sopenharmony_ci *                            1 = Twalks are cacheable in E-cache
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * GNU/Viking will only cache tablewalks in the E-cache (mxcc) if present
278c2ecf20Sopenharmony_ci * and never caches them internally (or so states the docs).  Therefore
288c2ecf20Sopenharmony_ci * for machines lacking an E-cache (ie. in MBUS mode) this bit must
298c2ecf20Sopenharmony_ci * remain cleared.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * AC: Alternate Cacheable -- 0 = Passthru physical accesses not cacheable
328c2ecf20Sopenharmony_ci *                            1 = Passthru physical accesses cacheable
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * This indicates whether accesses are cacheable when no cachable bit
358c2ecf20Sopenharmony_ci * is present in the pte when the processor is in boot-mode or the
368c2ecf20Sopenharmony_ci * access does not need pte's for translation (ie. pass-thru ASI's).
378c2ecf20Sopenharmony_ci * "Cachable" is only referring to E-cache (if present) and not the
388c2ecf20Sopenharmony_ci * on chip split I/D caches of the GNU/Viking.
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci * SP: SnooP Enable -- 0 = bus snooping off, 1 = bus snooping on
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * This enables snooping on the GNU/Viking bus.  This must be on
438c2ecf20Sopenharmony_ci * for the hardware cache consistency mechanisms of the GNU/Viking
448c2ecf20Sopenharmony_ci * to work at all.  On non-mxcc GNU/Viking modules the split I/D
458c2ecf20Sopenharmony_ci * caches will snoop regardless of whether they are enabled, this
468c2ecf20Sopenharmony_ci * takes care of the case where the I or D or both caches are turned
478c2ecf20Sopenharmony_ci * off yet still contain valid data.  Note also that this bit does
488c2ecf20Sopenharmony_ci * not affect GNU/Viking store-buffer snoops, those happen if the
498c2ecf20Sopenharmony_ci * store-buffer is enabled no matter what.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * BM: Boot Mode -- 0 = not in boot mode, 1 = in boot mode
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * This indicates whether the GNU/Viking is in boot-mode or not,
548c2ecf20Sopenharmony_ci * if it is then all instruction fetch physical addresses are
558c2ecf20Sopenharmony_ci * computed as 0xff0000000 + low 28 bits of requested address.
568c2ecf20Sopenharmony_ci * GNU/Viking boot-mode does not affect data accesses.  Also,
578c2ecf20Sopenharmony_ci * in boot mode instruction accesses bypass the split on chip I/D
588c2ecf20Sopenharmony_ci * caches, they may be cached by the GNU/MXCC if present and enabled.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * MBM: MBus Mode -- 0 = not in MBus mode, 1 = in MBus mode
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * This indicated the GNU/Viking configuration present.  If in
638c2ecf20Sopenharmony_ci * MBUS mode, the GNU/Viking lacks a GNU/MXCC E-cache.  If it is
648c2ecf20Sopenharmony_ci * not then the GNU/Viking is on a module VBUS connected directly
658c2ecf20Sopenharmony_ci * to a GNU/MXCC cache controller.  The GNU/MXCC can be thus connected
668c2ecf20Sopenharmony_ci * to either an GNU/MBUS (sun4m) or the packet-switched GNU/XBus (sun4d).
678c2ecf20Sopenharmony_ci *
688c2ecf20Sopenharmony_ci * SB: StoreBuffer enable -- 0 = store buffer off, 1 = store buffer on
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci * The GNU/Viking store buffer allows the chip to continue execution
718c2ecf20Sopenharmony_ci * after a store even if the data cannot be placed in one of the
728c2ecf20Sopenharmony_ci * caches during that cycle.  If disabled, all stores operations
738c2ecf20Sopenharmony_ci * occur synchronously.
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * IC: Instruction Cache -- 0 = off, 1 = on
768c2ecf20Sopenharmony_ci * DC: Data Cache -- 0 = off, 1 = 0n
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * These bits enable the on-cpu GNU/Viking split I/D caches.  Note,
798c2ecf20Sopenharmony_ci * as mentioned above, these caches will snoop the bus in GNU/MBUS
808c2ecf20Sopenharmony_ci * configurations even when disabled to avoid data corruption.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci * NF: No Fault -- 0 = faults generate traps, 1 = faults don't trap
838c2ecf20Sopenharmony_ci * ME: MMU enable -- 0 = mmu not translating, 1 = mmu translating
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define VIKING_MMUENABLE    0x00000001
888c2ecf20Sopenharmony_ci#define VIKING_NOFAULT      0x00000002
898c2ecf20Sopenharmony_ci#define VIKING_PSO          0x00000080
908c2ecf20Sopenharmony_ci#define VIKING_DCENABLE     0x00000100   /* Enable data cache */
918c2ecf20Sopenharmony_ci#define VIKING_ICENABLE     0x00000200   /* Enable instruction cache */
928c2ecf20Sopenharmony_ci#define VIKING_SBENABLE     0x00000400   /* Enable store buffer */
938c2ecf20Sopenharmony_ci#define VIKING_MMODE        0x00000800   /* MBUS mode */
948c2ecf20Sopenharmony_ci#define VIKING_PCENABLE     0x00001000   /* Enable parity checking */
958c2ecf20Sopenharmony_ci#define VIKING_BMODE        0x00002000
968c2ecf20Sopenharmony_ci#define VIKING_SPENABLE     0x00004000   /* Enable bus cache snooping */
978c2ecf20Sopenharmony_ci#define VIKING_ACENABLE     0x00008000   /* Enable alternate caching */
988c2ecf20Sopenharmony_ci#define VIKING_TCENABLE     0x00010000   /* Enable table-walks to be cached */
998c2ecf20Sopenharmony_ci#define VIKING_DPENABLE     0x00040000   /* Enable the data prefetcher */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * GNU/Viking Breakpoint Action Register fields.
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ci#define VIKING_ACTION_MIX   0x00001000   /* Enable multiple instructions */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/*
1078c2ecf20Sopenharmony_ci * GNU/Viking Cache Tags.
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_ci#define VIKING_PTAG_VALID   0x01000000   /* Cache block is valid */
1108c2ecf20Sopenharmony_ci#define VIKING_PTAG_DIRTY   0x00010000   /* Block has been modified */
1118c2ecf20Sopenharmony_ci#define VIKING_PTAG_SHARED  0x00000100   /* Shared with some other cache */
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline void viking_flush_icache(void)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	__asm__ __volatile__("sta %%g0, [%%g0] %0\n\t"
1188c2ecf20Sopenharmony_ci			     : /* no outputs */
1198c2ecf20Sopenharmony_ci			     : "i" (ASI_M_IC_FLCLEAR)
1208c2ecf20Sopenharmony_ci			     : "memory");
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline void viking_flush_dcache(void)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	__asm__ __volatile__("sta %%g0, [%%g0] %0\n\t"
1268c2ecf20Sopenharmony_ci			     : /* no outputs */
1278c2ecf20Sopenharmony_ci			     : "i" (ASI_M_DC_FLCLEAR)
1288c2ecf20Sopenharmony_ci			     : "memory");
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline void viking_unlock_icache(void)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	__asm__ __volatile__("sta %%g0, [%0] %1\n\t"
1348c2ecf20Sopenharmony_ci			     : /* no outputs */
1358c2ecf20Sopenharmony_ci			     : "r" (0x80000000), "i" (ASI_M_IC_FLCLEAR)
1368c2ecf20Sopenharmony_ci			     : "memory");
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline void viking_unlock_dcache(void)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	__asm__ __volatile__("sta %%g0, [%0] %1\n\t"
1428c2ecf20Sopenharmony_ci			     : /* no outputs */
1438c2ecf20Sopenharmony_ci			     : "r" (0x80000000), "i" (ASI_M_DC_FLCLEAR)
1448c2ecf20Sopenharmony_ci			     : "memory");
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic inline void viking_set_bpreg(unsigned long regval)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	__asm__ __volatile__("sta %0, [%%g0] %1\n\t"
1508c2ecf20Sopenharmony_ci			     : /* no outputs */
1518c2ecf20Sopenharmony_ci			     : "r" (regval), "i" (ASI_M_ACTION)
1528c2ecf20Sopenharmony_ci			     : "memory");
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic inline unsigned long viking_get_bpreg(void)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	unsigned long regval;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	__asm__ __volatile__("lda [%%g0] %1, %0\n\t"
1608c2ecf20Sopenharmony_ci			     : "=r" (regval)
1618c2ecf20Sopenharmony_ci			     : "i" (ASI_M_ACTION));
1628c2ecf20Sopenharmony_ci	return regval;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline void viking_get_dcache_ptag(int set, int block,
1668c2ecf20Sopenharmony_ci					      unsigned long *data)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	unsigned long ptag = ((set & 0x7f) << 5) | ((block & 0x3) << 26) |
1698c2ecf20Sopenharmony_ci			     0x80000000;
1708c2ecf20Sopenharmony_ci	unsigned long info, page;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	__asm__ __volatile__ ("ldda [%2] %3, %%g2\n\t"
1738c2ecf20Sopenharmony_ci			      "or %%g0, %%g2, %0\n\t"
1748c2ecf20Sopenharmony_ci			      "or %%g0, %%g3, %1\n\t"
1758c2ecf20Sopenharmony_ci			      : "=r" (info), "=r" (page)
1768c2ecf20Sopenharmony_ci			      : "r" (ptag), "i" (ASI_M_DATAC_TAG)
1778c2ecf20Sopenharmony_ci			      : "g2", "g3");
1788c2ecf20Sopenharmony_ci	data[0] = info;
1798c2ecf20Sopenharmony_ci	data[1] = page;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline void viking_mxcc_turn_off_parity(unsigned long *mregp,
1838c2ecf20Sopenharmony_ci						   unsigned long *mxcc_cregp)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	unsigned long mreg = *mregp;
1868c2ecf20Sopenharmony_ci	unsigned long mxcc_creg = *mxcc_cregp;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	mreg &= ~(VIKING_PCENABLE);
1898c2ecf20Sopenharmony_ci	mxcc_creg &= ~(MXCC_CTL_PARE);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	__asm__ __volatile__ ("set 1f, %%g2\n\t"
1928c2ecf20Sopenharmony_ci			      "andcc %%g2, 4, %%g0\n\t"
1938c2ecf20Sopenharmony_ci			      "bne 2f\n\t"
1948c2ecf20Sopenharmony_ci			      " nop\n"
1958c2ecf20Sopenharmony_ci			      "1:\n\t"
1968c2ecf20Sopenharmony_ci			      "sta %0, [%%g0] %3\n\t"
1978c2ecf20Sopenharmony_ci			      "sta %1, [%2] %4\n\t"
1988c2ecf20Sopenharmony_ci			      "b 1f\n\t"
1998c2ecf20Sopenharmony_ci			      " nop\n\t"
2008c2ecf20Sopenharmony_ci			      "nop\n"
2018c2ecf20Sopenharmony_ci			      "2:\n\t"
2028c2ecf20Sopenharmony_ci			      "sta %0, [%%g0] %3\n\t"
2038c2ecf20Sopenharmony_ci			      "sta %1, [%2] %4\n"
2048c2ecf20Sopenharmony_ci			      "1:\n\t"
2058c2ecf20Sopenharmony_ci			      : /* no output */
2068c2ecf20Sopenharmony_ci			      : "r" (mreg), "r" (mxcc_creg),
2078c2ecf20Sopenharmony_ci			        "r" (MXCC_CREG), "i" (ASI_M_MMUREGS),
2088c2ecf20Sopenharmony_ci			        "i" (ASI_M_MXCC)
2098c2ecf20Sopenharmony_ci			      : "g2", "memory", "cc");
2108c2ecf20Sopenharmony_ci	*mregp = mreg;
2118c2ecf20Sopenharmony_ci	*mxcc_cregp = mxcc_creg;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline unsigned long viking_hwprobe(unsigned long vaddr)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	unsigned long val;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	vaddr &= PAGE_MASK;
2198c2ecf20Sopenharmony_ci	/* Probe all MMU entries. */
2208c2ecf20Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t"
2218c2ecf20Sopenharmony_ci			     : "=r" (val)
2228c2ecf20Sopenharmony_ci			     : "r" (vaddr | 0x400), "i" (ASI_M_FLUSH_PROBE));
2238c2ecf20Sopenharmony_ci	if (!val)
2248c2ecf20Sopenharmony_ci		return 0;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* Probe region. */
2278c2ecf20Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t"
2288c2ecf20Sopenharmony_ci			     : "=r" (val)
2298c2ecf20Sopenharmony_ci			     : "r" (vaddr | 0x200), "i" (ASI_M_FLUSH_PROBE));
2308c2ecf20Sopenharmony_ci	if ((val & SRMMU_ET_MASK) == SRMMU_ET_PTE) {
2318c2ecf20Sopenharmony_ci		vaddr &= ~PGDIR_MASK;
2328c2ecf20Sopenharmony_ci		vaddr >>= PAGE_SHIFT;
2338c2ecf20Sopenharmony_ci		return val | (vaddr << 8);
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* Probe segment. */
2378c2ecf20Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t"
2388c2ecf20Sopenharmony_ci			     : "=r" (val)
2398c2ecf20Sopenharmony_ci			     : "r" (vaddr | 0x100), "i" (ASI_M_FLUSH_PROBE));
2408c2ecf20Sopenharmony_ci	if ((val & SRMMU_ET_MASK) == SRMMU_ET_PTE) {
2418c2ecf20Sopenharmony_ci		vaddr &= ~PMD_MASK;
2428c2ecf20Sopenharmony_ci		vaddr >>= PAGE_SHIFT;
2438c2ecf20Sopenharmony_ci		return val | (vaddr << 8);
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	/* Probe page. */
2478c2ecf20Sopenharmony_ci	__asm__ __volatile__("lda [%1] %2, %0\n\t"
2488c2ecf20Sopenharmony_ci			     : "=r" (val)
2498c2ecf20Sopenharmony_ci			     : "r" (vaddr), "i" (ASI_M_FLUSH_PROBE));
2508c2ecf20Sopenharmony_ci	return val;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci#endif /* !__ASSEMBLY__ */
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#endif /* !(_SPARC_VIKING_H) */
256