#ifndef _M68K_SEGMENT_H
#define _M68K_SEGMENT_H

/* define constants */
/* Address spaces (FC0-FC2) */
#ifndef USER_DS
#define USER_DS       (1)
#endif
#ifndef KERNEL_DS
#define KERNEL_DS     (5)
#endif

/* This selects whether to use two supervisor stacks (master/interrupt) or
 * only one. Two stacks reduce the chances of kernel stack overflows, but the
 * '060 has only one SSP and thus is forced to one stack mode. So this is also
 * the default... The two stack mode is left in more or less for reference. */
#undef MASTER_STACK

/* This is currently a bad hack for the '060: It seems that sometimes page
 * table contents are dirty in the cache. This shouldn't be, but happens...
 * The macro below does a CPUSHA only on the '060 and is inserted in some
 * places in memory management for the time being. */
#define CPUSHA060()										\
	do {												\
		if (m68k_is040or060 == 6)						\
			__asm__ __volatile__ (".word 0xf478\n" ::);	\
	} while(0)
 

#ifndef __ASSEMBLY__

static inline unsigned char get_user_byte(const char * addr)
{
	register unsigned char _v;

	__asm__ __volatile__ ("movesb %1,%0":"=r" (_v):"m" (*addr));
	return _v;
}

#define get_fs_byte(addr) get_user_byte((char *)(addr))

static inline unsigned short get_user_word(const short *addr)
{
	unsigned short _v;

	__asm__ __volatile__ ("movesw %1,%0":"=r" (_v):"m" (*addr));
	return _v;
}

#define get_fs_word(addr) get_user_word((short *)(addr))

static inline unsigned long get_user_long(const int *addr)
{
	unsigned long _v;

	__asm__ __volatile__ ("movesl %1,%0":"=r" (_v):"m" (*addr)); \
	return _v;
}

#define get_fs_long(addr) get_user_long((int *)(addr))

static inline void put_user_byte(char val,char *addr)
{
	__asm__ __volatile__ ("movesb %0,%1": /* no outputs */ :"r" (val),"m" (*addr) : "memory");
}

#define put_fs_byte(x,addr) put_user_byte((x),(char *)(addr))

static inline void put_user_word(short val,short * addr)
{
	__asm__ __volatile__ ("movesw %0,%1": /* no outputs */ :"r" (val),"m" (*addr) : "memory");
}

#define put_fs_word(x,addr) put_user_word((x),(short *)(addr))

static inline void put_user_long(unsigned long val,int * addr)
{
	__asm__ __volatile__ ("movesl %0,%1": /* no outputs */ :"r" (val),"m" (*addr) : "memory");
}

#define put_fs_long(x,addr) put_user_long((x),(int *)(addr))

static inline void __generic_memcpy_tofs(void * to, const void * from, unsigned long n)
{
	unsigned long tmp;
	if (n == 0) return;
	tmp = n;
	n >>= 2;
	if (n != 0)
		__asm__ __volatile__ ("1:\t"
			 "movel %1@+,%/d0\n\t"
			 "movesl %/d0,%2@+\n\t"
			      "dbra %0,1b\n\t"
			      "clrw %0\n\t"
			      "subql #1,%0\n\t"
			 "bccs 1b\n\t"
			      : "=d" (n), "=a" (from), "=a" (to)
			 : "0" (n-1), "1" (from), "2" (to)
			 : "d0", "memory");
	if (tmp & 2)
		__asm__ __volatile__ ("movew %0@+,%/d0\n\t"
			 "movesw %/d0,%1@+\n\t"
			 : "=a" (from), "=a" (to)
			 : "0" (from), "1" (to)
			 : "d0", "memory");
	if (tmp & 1)
		__asm__ __volatile__ ("moveb %0@,%/d0\n\t"
			 "movesb %/d0,%1@\n\t"
			 : /* no outputs */
			 : "a" (from), "a" (to)
			      : "d0", "memory");
}

static inline void __constant_memcpy_tofs(void * to, const void * from, unsigned long n)
{
	if (n == 0) {
			return;
	} else if (n == 1) {
			put_user_byte(*(const char *) from, (char *) to);
			return;
	} else if (n == 2) {
			put_user_word(*(const short *) from, (short *) to);
			return;
	} else if (n == 3) {
			put_user_word(*(const short *) from, (short *) to);
			put_user_byte(*(2+(const char *) from), 2+(char *) to);
			return;
	} else if (n == 4) {
			put_user_long(*(const int *) from, (int *) to);
			return;
	}
#define COMMON(x)                     \
__asm__ __volatile__ ("1:\n\t"                    \
            "movel %1@+,%/d0\n\t"           \
            "movesl %/d0,%2@+\n\t"          \
            "dbra %0,1b\n\t"              \
            "clrw %0\n\t"             \
            "subql #1,%0\n\t"             \
            "bccs 1b\n\t"             \
            x                     \
            : "=d" (n), "=a" (from), "=a" (to)    \
            : "1" (from), "2" (to), "0" (n/4-1)   \
            : "d0", "memory");

  switch (n % 4) {
      case 0:
          COMMON("");
          return;
      case 1:
          COMMON("moveb %1@+,%/d0; movesb %/d0,%2@+");
          return;
      case 2:
          COMMON("movew %1@+,%/d0; movesw %/d0,%2@+");
          return;
      case 3:
          COMMON("movew %1@+,%/d0; movesw %/d0,%2@+\n\t"
                 "moveb %1@+,%/d0; movesb %/d0,%2@+");
          return;
  }
#undef COMMON
}

static inline void __generic_memcpy_fromfs(void * to, const void * from, unsigned long n)
{
	unsigned long tmp;
	if (n == 0) return;
	tmp = n;
	n >>= 2;
	if (n != 0)
		__asm__ __volatile__ ("1:\t"
			 "movesl %1@+,%/d0\n\t"
			 "movel %/d0,%2@+\n\t"
			      "dbra %0,1b\n\t"
			      "clrw %0\n\t"
			      "subql #1,%0\n\t"
			 "bccs 1b\n\t"
			      : "=d" (n), "=a" (from), "=a" (to)
			 : "0" (n-1), "1" (from), "2" (to)
			 : "d0", "memory");
	if (tmp & 2)
		__asm__ __volatile__ ("movesw %0@+,%/d0\n\t"
			 "movew %/d0,%1@+\n\t"
			 : "=a" (from), "=a" (to)
			 : "0" (from), "1" (to)
			 : "d0", "memory");
	if (tmp & 1)
		__asm__ __volatile__ ("movesb %0@,%/d0\n\t"
			 "moveb %/d0,%1@\n\t"
			 : /* no outputs */
			 : "a" (from), "a" (to)
			      : "d0", "memory");
}

static inline void __constant_memcpy_fromfs(void * to, const void * from, unsigned long n)
{
	if (n == 0) {
			return;
	} else if (n == 1) {
			*(char *)to = get_user_byte((const char *) from);
			return;
	} else if (n == 2) {
			*(short *)to = get_user_word((const short *) from);
			return;
	} else if (n == 3) {
			*(short *) to = get_user_word((const short *) from);
			*(2+(char *) to) = get_user_byte(2+(const char *) from);
			return;
	} else if (n == 4) {
			*(int *) to = get_user_long((const int *) from);
			return;
	}
#define COMMON(x)                     \
__asm__ __volatile__ ("1:\n\t"                    \
            "movesl %1@+,%/d0\n\t"          \
            "movel %/d0,%2@+\n\t"           \
            "dbra %0,1b\n\t"              \
            "clrw %0\n\t"             \
            "subql #1,%0\n\t"             \
            "bccs 1b\n\t"             \
            x                     \
            : "=d" (n), "=a" (from), "=a" (to)    \
            : "1" (from), "2" (to), "0" (n/4-1)   \
            : "d0", "memory");

  switch (n % 4) {
      case 0:
          COMMON("");
          return;
      case 1:
          COMMON("movesb %1@+,%/d0; moveb %/d0,%2@+");
          return;
      case 2:
          COMMON("movesw %1@+,%/d0; movew %/d0,%2@+");
          return;
      case 3:
          COMMON("movesw %1@+,%/d0; movew %/d0,%2@+\n\t"
                 "movesb %1@+,%/d0; moveb %/d0,%2@+");
          return;
  }
#undef COMMON
}

#define memcpy_fromfs(to, from, n) \
(__builtin_constant_p(n) ? \
 __constant_memcpy_fromfs((to),(from),(n)) : \
 __generic_memcpy_fromfs((to),(from),(n)))

#define memcpy_tofs(to, from, n) \
(__builtin_constant_p(n) ? \
 __constant_memcpy_tofs((to),(from),(n)) : \
 __generic_memcpy_tofs((to),(from),(n)))

/*
 * Get/set the SFC/DFC registers for MOVES instructions
 */

static inline unsigned long get_fs(void)
{
	unsigned long _v;
	__asm__ ("movec %/dfc,%0":"=r" (_v):);

	return _v;
}

static inline unsigned long get_ds(void)
{
    /* return the supervisor data space code */
    return KERNEL_DS;
}

static inline void set_fs(unsigned long val)
{
	__asm__ __volatile__ ("movec %0,%/sfc\n\t"
			      "movec %0,%/dfc\n\t"
			      : /* no outputs */ : "r" (val) : "memory");
}

#endif /* __ASSEMBLY__ */

#endif /* _M68K_SEGMENT_H */
