/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_SYS_FSS_H
#define	_SYS_FSS_H

#pragma ident	"@(#)fss.h	1.4	05/11/18 SMI"

#include <sys/types.h>
#include <sys/thread.h>
#include <sys/project.h>

#ifdef	__cplusplus
extern "C" {
#endif

typedef uint64_t fsspri_t;
typedef	uint64_t fssusage_t;
struct cpupart;
struct zone;

/*
 * Valid arg1's for fss_allocbuf()
 */
#define	FSS_NPSET_BUF	1
#define	FSS_NPROJ_BUF	2
#define	FSS_ONE_BUF	3

/*
 * Valid arg2's for fss_allocbuf()
 */
#define	FSS_ALLOC_PROJ	1
#define	FSS_ALLOC_ZONE	2

#define	FSS_MAXSHARES	65535

typedef struct fssbuf {
	int	fssb_size;
	void	**fssb_list;
} fssbuf_t;

void *fss_allocbuf(int, int);
void fss_freebuf(fssbuf_t *, int);
void fss_changeproj(kthread_id_t, void *, void *, fssbuf_t *, fssbuf_t *);
void fss_changepset(kthread_id_t, void *, fssbuf_t *, fssbuf_t *);

/*
 * Fair share scheduling class specific cpu partition structure
 */
typedef struct fsspset {
	kmutex_t	fssps_lock;	/* lock to protect per-pset	*/
					/* list of fssproj structures	*/
	disp_lock_t	fssps_displock;	/* lock for fsps_maxfspri	*/
	struct cpupart	*fssps_cpupart;	/* ptr to our cpu partition	*/
					/* protected by fsspsets_lock	*/
	fsspri_t	fssps_maxfsspri; /* maximum fsspri value among	*/
					/* all projects on this pset	*/
	uint32_t	fssps_shares;	/* number of active shares	*/
	uint32_t	fssps_nproj;	/* number of fssproj structures */
					/* on the list			*/
	struct fssproj	*fssps_list;	/* list of project parts	*/
	struct fsszone	*fssps_zones;	/* list of fsszone_t's in pset	*/
} fsspset_t;

/*
 * One of these structures is allocated to each project running within each
 * active cpu partition.
 */
typedef struct fssproj {
	kproject_t	*fssp_proj;	/* ptr to our project structure	*/
	fsspset_t	*fssp_pset;	/* ptr to our fsspset structure	*/
	uint32_t	fssp_threads;	/* total number of threads here */
					/* protected by fssps_lock	*/
	uint32_t	fssp_runnable;	/* number of runnable threads	*/
					/* protected by fssps_lock	*/
	uint32_t	fssp_shares;	/* copy of our kpj_shares	*/
					/* protected by fssps_displock	*/
	uint32_t	fssp_ticks;	/* total of all ticks		*/
					/* protected by fssps_displock	*/
	fssusage_t	fssp_usage;	/* this project's decayed usage */
	fssusage_t	fssp_shusage;	/* normalized usage		*/
	struct fssproj	*fssp_next;	/* next project on this pset	*/
	struct fssproj	*fssp_prev;	/* prev project on this pset	*/
	struct fsszone	*fssp_fsszone;	/* fsszone_t for this fssproj	*/
} fssproj_t;

/*
 * Fair share scheduling class specific thread structure
 */
typedef struct fssproc {
	kthread_t *fss_tp;	/* pointer back to our thread		*/
	fssproj_t *fss_proj;	/* pointer to our project FS data	*/
	uchar_t fss_flags;	/* flags defined below			*/
	int	fss_timeleft;	/* time remaining in procs quantum	*/
	uint32_t fss_ticks;	/* ticks accumulated by this thread	*/
	pri_t	fss_upri;	/* user supplied priority (to priocntl)	*/
	pri_t	fss_uprilim;	/* user priority limit			*/
	pri_t	fss_umdpri;	/* user mode priority within fs class	*/
	pri_t	fss_scpri;	/* remembered priority, for schedctl	*/
	int	fss_nice;	/* nice value for compatibility with ts	*/
	fsspri_t fss_fsspri;	/* internal fair share priority		*/
	int	fss_runnable;	/* to indicate runnable/sleeping thread	*/
	struct fssproc *fss_next; /* pointer to next fssproc_t struct	*/
	struct fssproc *fss_prev; /* pointer to prev fssproc_t sturct	*/
} fssproc_t;

/*
 * One of these structures is allocated to each zone running within each active
 * cpu partition.
 */
typedef struct fsszone {
	struct zone 	*fssz_zone;	/* ptr to our zone structure */
	struct fsszone	*fssz_next;	/* ptr to next fsszone in fsspset */
	struct fsszone	*fssz_prev;	/* ptr to prev fsszone in fsspset */
	uint32_t	fssz_shares;	/* total #shares for projs in zone */
	uint32_t	fssz_nproj;	/* # fssproj_t's in this fsszone */
	uint32_t	fssz_rshares;	/* "real" shares given to zone */
	uint32_t	fssz_runnable;	/* # projects with runnable threads */
} fsszone_t;

#define	FSSPROC(tx)		((fssproc_t *)(tx->t_cldata))
#define	FSSPROC2FSSPROJ(fssx)	((fssx)->fss_proj);
#define	FSSPROC2FSSPSET(fssx)	(FSSPROC2FSSPROJ(fssx)->fssp_pset)
#define	FSSPROJ(tx)		(FSSPROC(tx)->fss_proj)
#define	FSSPROJ2FSSPSET(fssx)	((fssx)->fssp_pset)
#define	FSSPROJ2KPROJ(fssx)	((fssx)->fssp_proj)
#define	FSSPROJ2FSSZONE(fssx)	((fssx)->fssp_fsszone)

/*
 * fss_flags
 */
#define	FSSKPRI		0x01	/* the thread is in kernel mode	*/
#define	FSSBACKQ	0x02	/* thread should be placed at the back of */
				/* the dispatch queue if preempted */
#define	FSSRESTORE	0x04	/* thread was not preempted, due to schedctl */
				/* restore priority from fss_scpri */
#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_FSS_H */
