xref: /openbmc/linux/drivers/misc/sgi-gru/grutables.h (revision 22d55f02)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * SN Platform GRU Driver
4  *
5  *            GRU DRIVER TABLES, MACROS, externs, etc
6  *
7  *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
8  */
9 
10 #ifndef __GRUTABLES_H__
11 #define __GRUTABLES_H__
12 
13 /*
14  * GRU Chiplet:
15  *   The GRU is a user addressible memory accelerator. It provides
16  *   several forms of load, store, memset, bcopy instructions. In addition, it
17  *   contains special instructions for AMOs, sending messages to message
18  *   queues, etc.
19  *
20  *   The GRU is an integral part of the node controller. It connects
21  *   directly to the cpu socket. In its current implementation, there are 2
22  *   GRU chiplets in the node controller on each blade (~node).
23  *
24  *   The entire GRU memory space is fully coherent and cacheable by the cpus.
25  *
26  *   Each GRU chiplet has a physical memory map that looks like the following:
27  *
28  *   	+-----------------+
29  *   	|/////////////////|
30  *   	|/////////////////|
31  *   	|/////////////////|
32  *   	|/////////////////|
33  *   	|/////////////////|
34  *   	|/////////////////|
35  *   	|/////////////////|
36  *   	|/////////////////|
37  *   	+-----------------+
38  *   	|  system control |
39  *   	+-----------------+        _______ +-------------+
40  *   	|/////////////////|       /        |             |
41  *   	|/////////////////|      /         |             |
42  *   	|/////////////////|     /          | instructions|
43  *   	|/////////////////|    /           |             |
44  *   	|/////////////////|   /            |             |
45  *   	|/////////////////|  /             |-------------|
46  *   	|/////////////////| /              |             |
47  *   	+-----------------+                |             |
48  *   	|   context 15    |                |  data       |
49  *   	+-----------------+                |             |
50  *   	|    ......       | \              |             |
51  *   	+-----------------+  \____________ +-------------+
52  *   	|   context 1     |
53  *   	+-----------------+
54  *   	|   context 0     |
55  *   	+-----------------+
56  *
57  *   Each of the "contexts" is a chunk of memory that can be mmaped into user
58  *   space. The context consists of 2 parts:
59  *
60  *  	- an instruction space that can be directly accessed by the user
61  *  	  to issue GRU instructions and to check instruction status.
62  *
63  *  	- a data area that acts as normal RAM.
64  *
65  *   User instructions contain virtual addresses of data to be accessed by the
66  *   GRU. The GRU contains a TLB that is used to convert these user virtual
67  *   addresses to physical addresses.
68  *
69  *   The "system control" area of the GRU chiplet is used by the kernel driver
70  *   to manage user contexts and to perform functions such as TLB dropin and
71  *   purging.
72  *
73  *   One context may be reserved for the kernel and used for cross-partition
74  *   communication. The GRU will also be used to asynchronously zero out
75  *   large blocks of memory (not currently implemented).
76  *
77  *
78  * Tables:
79  *
80  * 	VDATA-VMA Data		- Holds a few parameters. Head of linked list of
81  * 				  GTS tables for threads using the GSEG
82  * 	GTS - Gru Thread State  - contains info for managing a GSEG context. A
83  * 				  GTS is allocated for each thread accessing a
84  * 				  GSEG.
85  *     	GTD - GRU Thread Data   - contains shadow copy of GRU data when GSEG is
86  *     				  not loaded into a GRU
87  *	GMS - GRU Memory Struct - Used to manage TLB shootdowns. Tracks GRUs
88  *				  where a GSEG has been loaded. Similar to
89  *				  an mm_struct but for GRU.
90  *
91  *	GS  - GRU State 	- Used to manage the state of a GRU chiplet
92  *	BS  - Blade State	- Used to manage state of all GRU chiplets
93  *				  on a blade
94  *
95  *
96  *  Normal task tables for task using GRU.
97  *  		- 2 threads in process
98  *  		- 2 GSEGs open in process
99  *  		- GSEG1 is being used by both threads
100  *  		- GSEG2 is used only by thread 2
101  *
102  *       task -->|
103  *       task ---+---> mm ->------ (notifier) -------+-> gms
104  *                     |                             |
105  *                     |--> vma -> vdata ---> gts--->|		GSEG1 (thread1)
106  *                     |                  |          |
107  *                     |                  +-> gts--->|		GSEG1 (thread2)
108  *                     |                             |
109  *                     |--> vma -> vdata ---> gts--->|		GSEG2 (thread2)
110  *                     .
111  *                     .
112  *
113  *  GSEGs are marked DONTCOPY on fork
114  *
115  * At open
116  * 	file.private_data -> NULL
117  *
118  * At mmap,
119  * 	vma -> vdata
120  *
121  * After gseg reference
122  * 	vma -> vdata ->gts
123  *
124  * After fork
125  *   parent
126  * 	vma -> vdata -> gts
127  *   child
128  * 	(vma is not copied)
129  *
130  */
131 
132 #include <linux/rmap.h>
133 #include <linux/interrupt.h>
134 #include <linux/mutex.h>
135 #include <linux/wait.h>
136 #include <linux/mmu_notifier.h>
137 #include <linux/mm_types.h>
138 #include "gru.h"
139 #include "grulib.h"
140 #include "gruhandles.h"
141 
142 extern struct gru_stats_s gru_stats;
143 extern struct gru_blade_state *gru_base[];
144 extern unsigned long gru_start_paddr, gru_end_paddr;
145 extern void *gru_start_vaddr;
146 extern unsigned int gru_max_gids;
147 
148 #define GRU_MAX_BLADES		MAX_NUMNODES
149 #define GRU_MAX_GRUS		(GRU_MAX_BLADES * GRU_CHIPLETS_PER_BLADE)
150 
151 #define GRU_DRIVER_ID_STR	"SGI GRU Device Driver"
152 #define GRU_DRIVER_VERSION_STR	"0.85"
153 
154 /*
155  * GRU statistics.
156  */
157 struct gru_stats_s {
158 	atomic_long_t vdata_alloc;
159 	atomic_long_t vdata_free;
160 	atomic_long_t gts_alloc;
161 	atomic_long_t gts_free;
162 	atomic_long_t gms_alloc;
163 	atomic_long_t gms_free;
164 	atomic_long_t gts_double_allocate;
165 	atomic_long_t assign_context;
166 	atomic_long_t assign_context_failed;
167 	atomic_long_t free_context;
168 	atomic_long_t load_user_context;
169 	atomic_long_t load_kernel_context;
170 	atomic_long_t lock_kernel_context;
171 	atomic_long_t unlock_kernel_context;
172 	atomic_long_t steal_user_context;
173 	atomic_long_t steal_kernel_context;
174 	atomic_long_t steal_context_failed;
175 	atomic_long_t nopfn;
176 	atomic_long_t asid_new;
177 	atomic_long_t asid_next;
178 	atomic_long_t asid_wrap;
179 	atomic_long_t asid_reuse;
180 	atomic_long_t intr;
181 	atomic_long_t intr_cbr;
182 	atomic_long_t intr_tfh;
183 	atomic_long_t intr_spurious;
184 	atomic_long_t intr_mm_lock_failed;
185 	atomic_long_t call_os;
186 	atomic_long_t call_os_wait_queue;
187 	atomic_long_t user_flush_tlb;
188 	atomic_long_t user_unload_context;
189 	atomic_long_t user_exception;
190 	atomic_long_t set_context_option;
191 	atomic_long_t check_context_retarget_intr;
192 	atomic_long_t check_context_unload;
193 	atomic_long_t tlb_dropin;
194 	atomic_long_t tlb_preload_page;
195 	atomic_long_t tlb_dropin_fail_no_asid;
196 	atomic_long_t tlb_dropin_fail_upm;
197 	atomic_long_t tlb_dropin_fail_invalid;
198 	atomic_long_t tlb_dropin_fail_range_active;
199 	atomic_long_t tlb_dropin_fail_idle;
200 	atomic_long_t tlb_dropin_fail_fmm;
201 	atomic_long_t tlb_dropin_fail_no_exception;
202 	atomic_long_t tfh_stale_on_fault;
203 	atomic_long_t mmu_invalidate_range;
204 	atomic_long_t mmu_invalidate_page;
205 	atomic_long_t flush_tlb;
206 	atomic_long_t flush_tlb_gru;
207 	atomic_long_t flush_tlb_gru_tgh;
208 	atomic_long_t flush_tlb_gru_zero_asid;
209 
210 	atomic_long_t copy_gpa;
211 	atomic_long_t read_gpa;
212 
213 	atomic_long_t mesq_receive;
214 	atomic_long_t mesq_receive_none;
215 	atomic_long_t mesq_send;
216 	atomic_long_t mesq_send_failed;
217 	atomic_long_t mesq_noop;
218 	atomic_long_t mesq_send_unexpected_error;
219 	atomic_long_t mesq_send_lb_overflow;
220 	atomic_long_t mesq_send_qlimit_reached;
221 	atomic_long_t mesq_send_amo_nacked;
222 	atomic_long_t mesq_send_put_nacked;
223 	atomic_long_t mesq_page_overflow;
224 	atomic_long_t mesq_qf_locked;
225 	atomic_long_t mesq_qf_noop_not_full;
226 	atomic_long_t mesq_qf_switch_head_failed;
227 	atomic_long_t mesq_qf_unexpected_error;
228 	atomic_long_t mesq_noop_unexpected_error;
229 	atomic_long_t mesq_noop_lb_overflow;
230 	atomic_long_t mesq_noop_qlimit_reached;
231 	atomic_long_t mesq_noop_amo_nacked;
232 	atomic_long_t mesq_noop_put_nacked;
233 	atomic_long_t mesq_noop_page_overflow;
234 
235 };
236 
237 enum mcs_op {cchop_allocate, cchop_start, cchop_interrupt, cchop_interrupt_sync,
238 	cchop_deallocate, tfhop_write_only, tfhop_write_restart,
239 	tghop_invalidate, mcsop_last};
240 
241 struct mcs_op_statistic {
242 	atomic_long_t	count;
243 	atomic_long_t	total;
244 	unsigned long	max;
245 };
246 
247 extern struct mcs_op_statistic mcs_op_statistics[mcsop_last];
248 
249 #define OPT_DPRINT		1
250 #define OPT_STATS		2
251 
252 
253 #define IRQ_GRU			110	/* Starting IRQ number for interrupts */
254 
255 /* Delay in jiffies between attempts to assign a GRU context */
256 #define GRU_ASSIGN_DELAY	((HZ * 20) / 1000)
257 
258 /*
259  * If a process has it's context stolen, min delay in jiffies before trying to
260  * steal a context from another process.
261  */
262 #define GRU_STEAL_DELAY		((HZ * 200) / 1000)
263 
264 #define STAT(id)	do {						\
265 				if (gru_options & OPT_STATS)		\
266 					atomic_long_inc(&gru_stats.id);	\
267 			} while (0)
268 
269 #ifdef CONFIG_SGI_GRU_DEBUG
270 #define gru_dbg(dev, fmt, x...)						\
271 	do {								\
272 		if (gru_options & OPT_DPRINT)				\
273 			printk(KERN_DEBUG "GRU:%d %s: " fmt, smp_processor_id(), __func__, x);\
274 	} while (0)
275 #else
276 #define gru_dbg(x...)
277 #endif
278 
279 /*-----------------------------------------------------------------------------
280  * ASID management
281  */
282 #define MAX_ASID	0xfffff0
283 #define MIN_ASID	8
284 #define ASID_INC	8	/* number of regions */
285 
286 /* Generate a GRU asid value from a GRU base asid & a virtual address. */
287 #define VADDR_HI_BIT		64
288 #define GRUREGION(addr)		((addr) >> (VADDR_HI_BIT - 3) & 3)
289 #define GRUASID(asid, addr)	((asid) + GRUREGION(addr))
290 
291 /*------------------------------------------------------------------------------
292  *  File & VMS Tables
293  */
294 
295 struct gru_state;
296 
297 /*
298  * This structure is pointed to from the mmstruct via the notifier pointer.
299  * There is one of these per address space.
300  */
301 struct gru_mm_tracker {				/* pack to reduce size */
302 	unsigned int		mt_asid_gen:24;	/* ASID wrap count */
303 	unsigned int		mt_asid:24;	/* current base ASID for gru */
304 	unsigned short		mt_ctxbitmap:16;/* bitmap of contexts using
305 						   asid */
306 } __attribute__ ((packed));
307 
308 struct gru_mm_struct {
309 	struct mmu_notifier	ms_notifier;
310 	atomic_t		ms_refcnt;
311 	spinlock_t		ms_asid_lock;	/* protects ASID assignment */
312 	atomic_t		ms_range_active;/* num range_invals active */
313 	char			ms_released;
314 	wait_queue_head_t	ms_wait_queue;
315 	DECLARE_BITMAP(ms_asidmap, GRU_MAX_GRUS);
316 	struct gru_mm_tracker	ms_asids[GRU_MAX_GRUS];
317 };
318 
319 /*
320  * One of these structures is allocated when a GSEG is mmaped. The
321  * structure is pointed to by the vma->vm_private_data field in the vma struct.
322  */
323 struct gru_vma_data {
324 	spinlock_t		vd_lock;	/* Serialize access to vma */
325 	struct list_head	vd_head;	/* head of linked list of gts */
326 	long			vd_user_options;/* misc user option flags */
327 	int			vd_cbr_au_count;
328 	int			vd_dsr_au_count;
329 	unsigned char		vd_tlb_preload_count;
330 };
331 
332 /*
333  * One of these is allocated for each thread accessing a mmaped GRU. A linked
334  * list of these structure is hung off the struct gru_vma_data in the mm_struct.
335  */
336 struct gru_thread_state {
337 	struct list_head	ts_next;	/* list - head at vma-private */
338 	struct mutex		ts_ctxlock;	/* load/unload CTX lock */
339 	struct mm_struct	*ts_mm;		/* mm currently mapped to
340 						   context */
341 	struct vm_area_struct	*ts_vma;	/* vma of GRU context */
342 	struct gru_state	*ts_gru;	/* GRU where the context is
343 						   loaded */
344 	struct gru_mm_struct	*ts_gms;	/* asid & ioproc struct */
345 	unsigned char		ts_tlb_preload_count; /* TLB preload pages */
346 	unsigned long		ts_cbr_map;	/* map of allocated CBRs */
347 	unsigned long		ts_dsr_map;	/* map of allocated DATA
348 						   resources */
349 	unsigned long		ts_steal_jiffies;/* jiffies when context last
350 						    stolen */
351 	long			ts_user_options;/* misc user option flags */
352 	pid_t			ts_tgid_owner;	/* task that is using the
353 						   context - for migration */
354 	short			ts_user_blade_id;/* user selected blade */
355 	char			ts_user_chiplet_id;/* user selected chiplet */
356 	unsigned short		ts_sizeavail;	/* Pagesizes in use */
357 	int			ts_tsid;	/* thread that owns the
358 						   structure */
359 	int			ts_tlb_int_select;/* target cpu if interrupts
360 						     enabled */
361 	int			ts_ctxnum;	/* context number where the
362 						   context is loaded */
363 	atomic_t		ts_refcnt;	/* reference count GTS */
364 	unsigned char		ts_dsr_au_count;/* Number of DSR resources
365 						   required for contest */
366 	unsigned char		ts_cbr_au_count;/* Number of CBR resources
367 						   required for contest */
368 	char			ts_cch_req_slice;/* CCH packet slice */
369 	char			ts_blade;	/* If >= 0, migrate context if
370 						   ref from different blade */
371 	char			ts_force_cch_reload;
372 	char			ts_cbr_idx[GRU_CBR_AU];/* CBR numbers of each
373 							  allocated CB */
374 	int			ts_data_valid;	/* Indicates if ts_gdata has
375 						   valid data */
376 	struct gru_gseg_statistics ustats;	/* User statistics */
377 	unsigned long		ts_gdata[0];	/* save area for GRU data (CB,
378 						   DS, CBE) */
379 };
380 
381 /*
382  * Threaded programs actually allocate an array of GSEGs when a context is
383  * created. Each thread uses a separate GSEG. TSID is the index into the GSEG
384  * array.
385  */
386 #define TSID(a, v)		(((a) - (v)->vm_start) / GRU_GSEG_PAGESIZE)
387 #define UGRUADDR(gts)		((gts)->ts_vma->vm_start +		\
388 					(gts)->ts_tsid * GRU_GSEG_PAGESIZE)
389 
390 #define NULLCTX			(-1)	/* if context not loaded into GRU */
391 
392 /*-----------------------------------------------------------------------------
393  *  GRU State Tables
394  */
395 
396 /*
397  * One of these exists for each GRU chiplet.
398  */
399 struct gru_state {
400 	struct gru_blade_state	*gs_blade;		/* GRU state for entire
401 							   blade */
402 	unsigned long		gs_gru_base_paddr;	/* Physical address of
403 							   gru segments (64) */
404 	void			*gs_gru_base_vaddr;	/* Virtual address of
405 							   gru segments (64) */
406 	unsigned short		gs_gid;			/* unique GRU number */
407 	unsigned short		gs_blade_id;		/* blade of GRU */
408 	unsigned char		gs_chiplet_id;		/* blade chiplet of GRU */
409 	unsigned char		gs_tgh_local_shift;	/* used to pick TGH for
410 							   local flush */
411 	unsigned char		gs_tgh_first_remote;	/* starting TGH# for
412 							   remote flush */
413 	spinlock_t		gs_asid_lock;		/* lock used for
414 							   assigning asids */
415 	spinlock_t		gs_lock;		/* lock used for
416 							   assigning contexts */
417 
418 	/* -- the following are protected by the gs_asid_lock spinlock ---- */
419 	unsigned int		gs_asid;		/* Next availe ASID */
420 	unsigned int		gs_asid_limit;		/* Limit of available
421 							   ASIDs */
422 	unsigned int		gs_asid_gen;		/* asid generation.
423 							   Inc on wrap */
424 
425 	/* --- the following fields are protected by the gs_lock spinlock --- */
426 	unsigned long		gs_context_map;		/* bitmap to manage
427 							   contexts in use */
428 	unsigned long		gs_cbr_map;		/* bitmap to manage CB
429 							   resources */
430 	unsigned long		gs_dsr_map;		/* bitmap used to manage
431 							   DATA resources */
432 	unsigned int		gs_reserved_cbrs;	/* Number of kernel-
433 							   reserved cbrs */
434 	unsigned int		gs_reserved_dsr_bytes;	/* Bytes of kernel-
435 							   reserved dsrs */
436 	unsigned short		gs_active_contexts;	/* number of contexts
437 							   in use */
438 	struct gru_thread_state	*gs_gts[GRU_NUM_CCH];	/* GTS currently using
439 							   the context */
440 	int			gs_irq[GRU_NUM_TFM];	/* Interrupt irqs */
441 };
442 
443 /*
444  * This structure contains the GRU state for all the GRUs on a blade.
445  */
446 struct gru_blade_state {
447 	void			*kernel_cb;		/* First kernel
448 							   reserved cb */
449 	void			*kernel_dsr;		/* First kernel
450 							   reserved DSR */
451 	struct rw_semaphore	bs_kgts_sema;		/* lock for kgts */
452 	struct gru_thread_state *bs_kgts;		/* GTS for kernel use */
453 
454 	/* ---- the following are used for managing kernel async GRU CBRs --- */
455 	int			bs_async_dsr_bytes;	/* DSRs for async */
456 	int			bs_async_cbrs;		/* CBRs AU for async */
457 	struct completion	*bs_async_wq;
458 
459 	/* ---- the following are protected by the bs_lock spinlock ---- */
460 	spinlock_t		bs_lock;		/* lock used for
461 							   stealing contexts */
462 	int			bs_lru_ctxnum;		/* STEAL - last context
463 							   stolen */
464 	struct gru_state	*bs_lru_gru;		/* STEAL - last gru
465 							   stolen */
466 
467 	struct gru_state	bs_grus[GRU_CHIPLETS_PER_BLADE];
468 };
469 
470 /*-----------------------------------------------------------------------------
471  * Address Primitives
472  */
473 #define get_tfm_for_cpu(g, c)						\
474 	((struct gru_tlb_fault_map *)get_tfm((g)->gs_gru_base_vaddr, (c)))
475 #define get_tfh_by_index(g, i)						\
476 	((struct gru_tlb_fault_handle *)get_tfh((g)->gs_gru_base_vaddr, (i)))
477 #define get_tgh_by_index(g, i)						\
478 	((struct gru_tlb_global_handle *)get_tgh((g)->gs_gru_base_vaddr, (i)))
479 #define get_cbe_by_index(g, i)						\
480 	((struct gru_control_block_extended *)get_cbe((g)->gs_gru_base_vaddr,\
481 			(i)))
482 
483 /*-----------------------------------------------------------------------------
484  * Useful Macros
485  */
486 
487 /* Given a blade# & chiplet#, get a pointer to the GRU */
488 #define get_gru(b, c)		(&gru_base[b]->bs_grus[c])
489 
490 /* Number of bytes to save/restore when unloading/loading GRU contexts */
491 #define DSR_BYTES(dsr)		((dsr) * GRU_DSR_AU_BYTES)
492 #define CBR_BYTES(cbr)		((cbr) * GRU_HANDLE_BYTES * GRU_CBR_AU_SIZE * 2)
493 
494 /* Convert a user CB number to the actual CBRNUM */
495 #define thread_cbr_number(gts, n) ((gts)->ts_cbr_idx[(n) / GRU_CBR_AU_SIZE] \
496 				  * GRU_CBR_AU_SIZE + (n) % GRU_CBR_AU_SIZE)
497 
498 /* Convert a gid to a pointer to the GRU */
499 #define GID_TO_GRU(gid)							\
500 	(gru_base[(gid) / GRU_CHIPLETS_PER_BLADE] ?			\
501 		(&gru_base[(gid) / GRU_CHIPLETS_PER_BLADE]->		\
502 			bs_grus[(gid) % GRU_CHIPLETS_PER_BLADE]) :	\
503 	 NULL)
504 
505 /* Scan all active GRUs in a GRU bitmap */
506 #define for_each_gru_in_bitmap(gid, map)				\
507 	for_each_set_bit((gid), (map), GRU_MAX_GRUS)
508 
509 /* Scan all active GRUs on a specific blade */
510 #define for_each_gru_on_blade(gru, nid, i)				\
511 	for ((gru) = gru_base[nid]->bs_grus, (i) = 0;			\
512 			(i) < GRU_CHIPLETS_PER_BLADE;			\
513 			(i)++, (gru)++)
514 
515 /* Scan all GRUs */
516 #define foreach_gid(gid)						\
517 	for ((gid) = 0; (gid) < gru_max_gids; (gid)++)
518 
519 /* Scan all active GTSs on a gru. Note: must hold ss_lock to use this macro. */
520 #define for_each_gts_on_gru(gts, gru, ctxnum)				\
521 	for ((ctxnum) = 0; (ctxnum) < GRU_NUM_CCH; (ctxnum)++)		\
522 		if (((gts) = (gru)->gs_gts[ctxnum]))
523 
524 /* Scan each CBR whose bit is set in a TFM (or copy of) */
525 #define for_each_cbr_in_tfm(i, map)					\
526 	for_each_set_bit((i), (map), GRU_NUM_CBE)
527 
528 /* Scan each CBR in a CBR bitmap. Note: multiple CBRs in an allocation unit */
529 #define for_each_cbr_in_allocation_map(i, map, k)			\
530 	for_each_set_bit((k), (map), GRU_CBR_AU)			\
531 		for ((i) = (k)*GRU_CBR_AU_SIZE;				\
532 				(i) < ((k) + 1) * GRU_CBR_AU_SIZE; (i)++)
533 
534 /* Scan each DSR in a DSR bitmap. Note: multiple DSRs in an allocation unit */
535 #define for_each_dsr_in_allocation_map(i, map, k)			\
536 	for_each_set_bit((k), (const unsigned long *)(map), GRU_DSR_AU)	\
537 		for ((i) = (k) * GRU_DSR_AU_CL;				\
538 				(i) < ((k) + 1) * GRU_DSR_AU_CL; (i)++)
539 
540 #define gseg_physical_address(gru, ctxnum)				\
541 		((gru)->gs_gru_base_paddr + ctxnum * GRU_GSEG_STRIDE)
542 #define gseg_virtual_address(gru, ctxnum)				\
543 		((gru)->gs_gru_base_vaddr + ctxnum * GRU_GSEG_STRIDE)
544 
545 /*-----------------------------------------------------------------------------
546  * Lock / Unlock GRU handles
547  * 	Use the "delresp" bit in the handle as a "lock" bit.
548  */
549 
550 /* Lock hierarchy checking enabled only in emulator */
551 
552 /* 0 = lock failed, 1 = locked */
553 static inline int __trylock_handle(void *h)
554 {
555 	return !test_and_set_bit(1, h);
556 }
557 
558 static inline void __lock_handle(void *h)
559 {
560 	while (test_and_set_bit(1, h))
561 		cpu_relax();
562 }
563 
564 static inline void __unlock_handle(void *h)
565 {
566 	clear_bit(1, h);
567 }
568 
569 static inline int trylock_cch_handle(struct gru_context_configuration_handle *cch)
570 {
571 	return __trylock_handle(cch);
572 }
573 
574 static inline void lock_cch_handle(struct gru_context_configuration_handle *cch)
575 {
576 	__lock_handle(cch);
577 }
578 
579 static inline void unlock_cch_handle(struct gru_context_configuration_handle
580 				     *cch)
581 {
582 	__unlock_handle(cch);
583 }
584 
585 static inline void lock_tgh_handle(struct gru_tlb_global_handle *tgh)
586 {
587 	__lock_handle(tgh);
588 }
589 
590 static inline void unlock_tgh_handle(struct gru_tlb_global_handle *tgh)
591 {
592 	__unlock_handle(tgh);
593 }
594 
595 static inline int is_kernel_context(struct gru_thread_state *gts)
596 {
597 	return !gts->ts_mm;
598 }
599 
600 /*
601  * The following are for Nehelem-EX. A more general scheme is needed for
602  * future processors.
603  */
604 #define UV_MAX_INT_CORES		8
605 #define uv_cpu_socket_number(p)		((cpu_physical_id(p) >> 5) & 1)
606 #define uv_cpu_ht_number(p)		(cpu_physical_id(p) & 1)
607 #define uv_cpu_core_number(p)		(((cpu_physical_id(p) >> 2) & 4) |	\
608 					((cpu_physical_id(p) >> 1) & 3))
609 /*-----------------------------------------------------------------------------
610  * Function prototypes & externs
611  */
612 struct gru_unload_context_req;
613 
614 extern const struct vm_operations_struct gru_vm_ops;
615 extern struct device *grudev;
616 
617 extern struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma,
618 				int tsid);
619 extern struct gru_thread_state *gru_find_thread_state(struct vm_area_struct
620 				*vma, int tsid);
621 extern struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct
622 				*vma, int tsid);
623 extern struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts);
624 extern void gru_load_context(struct gru_thread_state *gts);
625 extern void gru_steal_context(struct gru_thread_state *gts);
626 extern void gru_unload_context(struct gru_thread_state *gts, int savestate);
627 extern int gru_update_cch(struct gru_thread_state *gts);
628 extern void gts_drop(struct gru_thread_state *gts);
629 extern void gru_tgh_flush_init(struct gru_state *gru);
630 extern int gru_kservices_init(void);
631 extern void gru_kservices_exit(void);
632 extern irqreturn_t gru0_intr(int irq, void *dev_id);
633 extern irqreturn_t gru1_intr(int irq, void *dev_id);
634 extern irqreturn_t gru_intr_mblade(int irq, void *dev_id);
635 extern int gru_dump_chiplet_request(unsigned long arg);
636 extern long gru_get_gseg_statistics(unsigned long arg);
637 extern int gru_handle_user_call_os(unsigned long address);
638 extern int gru_user_flush_tlb(unsigned long arg);
639 extern int gru_user_unload_context(unsigned long arg);
640 extern int gru_get_exception_detail(unsigned long arg);
641 extern int gru_set_context_option(unsigned long address);
642 extern void gru_check_context_placement(struct gru_thread_state *gts);
643 extern int gru_cpu_fault_map_id(void);
644 extern struct vm_area_struct *gru_find_vma(unsigned long vaddr);
645 extern void gru_flush_all_tlb(struct gru_state *gru);
646 extern int gru_proc_init(void);
647 extern void gru_proc_exit(void);
648 
649 extern struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
650 		int cbr_au_count, int dsr_au_count,
651 		unsigned char tlb_preload_count, int options, int tsid);
652 extern unsigned long gru_reserve_cb_resources(struct gru_state *gru,
653 		int cbr_au_count, char *cbmap);
654 extern unsigned long gru_reserve_ds_resources(struct gru_state *gru,
655 		int dsr_au_count, char *dsmap);
656 extern vm_fault_t gru_fault(struct vm_fault *vmf);
657 extern struct gru_mm_struct *gru_register_mmu_notifier(void);
658 extern void gru_drop_mmu_notifier(struct gru_mm_struct *gms);
659 
660 extern int gru_ktest(unsigned long arg);
661 extern void gru_flush_tlb_range(struct gru_mm_struct *gms, unsigned long start,
662 					unsigned long len);
663 
664 extern unsigned long gru_options;
665 
666 #endif /* __GRUTABLES_H__ */
667