2 * SN Platform GRU Driver
4 * GRU DRIVER TABLES, MACROS, externs, etc
6 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GRUTABLES_H__
24 #define __GRUTABLES_H__
28 * The GRU is a user addressible memory accelerator. It provides
29 * several forms of load, store, memset, bcopy instructions. In addition, it
30 * contains special instructions for AMOs, sending messages to message
33 * The GRU is an integral part of the node controller. It connects
34 * directly to the cpu socket. In its current implementation, there are 2
35 * GRU chiplets in the node controller on each blade (~node).
37 * The entire GRU memory space is fully coherent and cacheable by the cpus.
39 * Each GRU chiplet has a physical memory map that looks like the following:
52 * +-----------------+ _______ +-------------+
53 * |/////////////////| / | |
54 * |/////////////////| / | |
55 * |/////////////////| / | instructions|
56 * |/////////////////| / | |
57 * |/////////////////| / | |
58 * |/////////////////| / |-------------|
59 * |/////////////////| / | |
60 * +-----------------+ | |
61 * | context 15 | | data |
62 * +-----------------+ | |
64 * +-----------------+ \____________ +-------------+
70 * Each of the "contexts" is a chunk of memory that can be mmaped into user
71 * space. The context consists of 2 parts:
73 * - an instruction space that can be directly accessed by the user
74 * to issue GRU instructions and to check instruction status.
76 * - a data area that acts as normal RAM.
78 * User instructions contain virtual addresses of data to be accessed by the
79 * GRU. The GRU contains a TLB that is used to convert these user virtual
80 * addresses to physical addresses.
82 * The "system control" area of the GRU chiplet is used by the kernel driver
83 * to manage user contexts and to perform functions such as TLB dropin and
86 * One context may be reserved for the kernel and used for cross-partition
87 * communication. The GRU will also be used to asynchronously zero out
88 * large blocks of memory (not currently implemented).
93 * VDATA-VMA Data - Holds a few parameters. Head of linked list of
94 * GTS tables for threads using the GSEG
95 * GTS - Gru Thread State - contains info for managing a GSEG context. A
96 * GTS is allocated for each thread accessing a
98 * GTD - GRU Thread Data - contains shadow copy of GRU data when GSEG is
99 * not loaded into a GRU
100 * GMS - GRU Memory Struct - Used to manage TLB shootdowns. Tracks GRUs
101 * where a GSEG has been loaded. Similar to
102 * an mm_struct but for GRU.
104 * GS - GRU State - Used to manage the state of a GRU chiplet
105 * BS - Blade State - Used to manage state of all GRU chiplets
109 * Normal task tables for task using GRU.
110 * - 2 threads in process
111 * - 2 GSEGs open in process
112 * - GSEG1 is being used by both threads
113 * - GSEG2 is used only by thread 2
116 * task ---+---> mm ->------ (notifier) -------+-> gms
118 * |--> vma -> vdata ---> gts--->| GSEG1 (thread1)
120 * | +-> gts--->| GSEG1 (thread2)
122 * |--> vma -> vdata ---> gts--->| GSEG2 (thread2)
126 * GSEGs are marked DONTCOPY on fork
129 * file.private_data -> NULL
134 * After gseg reference
139 * vma -> vdata -> gts
141 * (vma is not copied)
145 #include <linux/rmap.h>
146 #include <linux/interrupt.h>
147 #include <linux/mutex.h>
148 #include <linux/wait.h>
149 #include <linux/mmu_notifier.h>
151 #include "gruhandles.h"
153 extern struct gru_stats_s gru_stats
;
154 extern struct gru_blade_state
*gru_base
[];
155 extern unsigned long gru_start_paddr
, gru_end_paddr
;
156 extern unsigned int gru_max_gids
;
158 #define GRU_MAX_BLADES MAX_NUMNODES
159 #define GRU_MAX_GRUS (GRU_MAX_BLADES * GRU_CHIPLETS_PER_BLADE)
161 #define GRU_DRIVER_ID_STR "SGI GRU Device Driver"
162 #define GRU_DRIVER_VERSION_STR "0.80"
168 atomic_long_t vdata_alloc
;
169 atomic_long_t vdata_free
;
170 atomic_long_t gts_alloc
;
171 atomic_long_t gts_free
;
172 atomic_long_t vdata_double_alloc
;
173 atomic_long_t gts_double_allocate
;
174 atomic_long_t assign_context
;
175 atomic_long_t assign_context_failed
;
176 atomic_long_t free_context
;
177 atomic_long_t load_context
;
178 atomic_long_t unload_context
;
179 atomic_long_t steal_context
;
180 atomic_long_t steal_context_failed
;
182 atomic_long_t break_cow
;
183 atomic_long_t asid_new
;
184 atomic_long_t asid_next
;
185 atomic_long_t asid_wrap
;
186 atomic_long_t asid_reuse
;
188 atomic_long_t intr_mm_lock_failed
;
189 atomic_long_t call_os
;
190 atomic_long_t call_os_offnode_reference
;
191 atomic_long_t call_os_check_for_bug
;
192 atomic_long_t call_os_wait_queue
;
193 atomic_long_t user_flush_tlb
;
194 atomic_long_t user_unload_context
;
195 atomic_long_t user_exception
;
196 atomic_long_t set_task_slice
;
197 atomic_long_t migrate_check
;
198 atomic_long_t migrated_retarget
;
199 atomic_long_t migrated_unload
;
200 atomic_long_t migrated_unload_delay
;
201 atomic_long_t migrated_nopfn_retarget
;
202 atomic_long_t migrated_nopfn_unload
;
203 atomic_long_t tlb_dropin
;
204 atomic_long_t tlb_dropin_fail_no_asid
;
205 atomic_long_t tlb_dropin_fail_upm
;
206 atomic_long_t tlb_dropin_fail_invalid
;
207 atomic_long_t tlb_dropin_fail_range_active
;
208 atomic_long_t tlb_dropin_fail_idle
;
209 atomic_long_t tlb_dropin_fail_fmm
;
210 atomic_long_t mmu_invalidate_range
;
211 atomic_long_t mmu_invalidate_page
;
212 atomic_long_t mmu_clear_flush_young
;
213 atomic_long_t flush_tlb
;
214 atomic_long_t flush_tlb_gru
;
215 atomic_long_t flush_tlb_gru_tgh
;
216 atomic_long_t flush_tlb_gru_zero_asid
;
218 atomic_long_t copy_gpa
;
220 atomic_long_t mesq_receive
;
221 atomic_long_t mesq_receive_none
;
222 atomic_long_t mesq_send
;
223 atomic_long_t mesq_send_failed
;
224 atomic_long_t mesq_noop
;
225 atomic_long_t mesq_send_unexpected_error
;
226 atomic_long_t mesq_send_lb_overflow
;
227 atomic_long_t mesq_send_qlimit_reached
;
228 atomic_long_t mesq_send_amo_nacked
;
229 atomic_long_t mesq_send_put_nacked
;
230 atomic_long_t mesq_qf_not_full
;
231 atomic_long_t mesq_qf_locked
;
232 atomic_long_t mesq_qf_noop_not_full
;
233 atomic_long_t mesq_qf_switch_head_failed
;
234 atomic_long_t mesq_qf_unexpected_error
;
235 atomic_long_t mesq_noop_unexpected_error
;
236 atomic_long_t mesq_noop_lb_overflow
;
237 atomic_long_t mesq_noop_qlimit_reached
;
238 atomic_long_t mesq_noop_amo_nacked
;
239 atomic_long_t mesq_noop_put_nacked
;
243 enum mcs_op
{cchop_allocate
, cchop_start
, cchop_interrupt
, cchop_interrupt_sync
,
244 cchop_deallocate
, tghop_invalidate
, mcsop_last
};
246 struct mcs_op_statistic
{
252 extern struct mcs_op_statistic mcs_op_statistics
[mcsop_last
];
256 #define GRU_QUICKLOOK 4
259 #define IRQ_GRU 110 /* Starting IRQ number for interrupts */
261 /* Delay in jiffies between attempts to assign a GRU context */
262 #define GRU_ASSIGN_DELAY ((HZ * 20) / 1000)
265 * If a process has it's context stolen, min delay in jiffies before trying to
266 * steal a context from another process.
268 #define GRU_STEAL_DELAY ((HZ * 200) / 1000)
270 #define STAT(id) do { \
271 if (gru_options & OPT_STATS) \
272 atomic_long_inc(&gru_stats.id); \
275 #ifdef CONFIG_SGI_GRU_DEBUG
276 #define gru_dbg(dev, fmt, x...) \
278 if (gru_options & OPT_DPRINT) \
279 dev_dbg(dev, "%s: " fmt, __func__, x); \
282 #define gru_dbg(x...)
285 /*-----------------------------------------------------------------------------
288 #define MAX_ASID 0xfffff0
290 #define ASID_INC 8 /* number of regions */
292 /* Generate a GRU asid value from a GRU base asid & a virtual address. */
293 #if defined CONFIG_IA64
294 #define VADDR_HI_BIT 64
295 #elif defined CONFIG_X86_64
296 #define VADDR_HI_BIT 48
298 #error "Unsupported architecture"
300 #define GRUREGION(addr) ((addr) >> (VADDR_HI_BIT - 3) & 3)
301 #define GRUASID(asid, addr) ((asid) + GRUREGION(addr))
303 /*------------------------------------------------------------------------------
310 * This structure is pointed to from the mmstruct via the notifier pointer.
311 * There is one of these per address space.
313 struct gru_mm_tracker
{ /* pack to reduce size */
314 unsigned int mt_asid_gen
:24; /* ASID wrap count */
315 unsigned int mt_asid
:24; /* current base ASID for gru */
316 unsigned short mt_ctxbitmap
:16;/* bitmap of contexts using
318 } __attribute__ ((packed
));
320 struct gru_mm_struct
{
321 struct mmu_notifier ms_notifier
;
323 spinlock_t ms_asid_lock
; /* protects ASID assignment */
324 atomic_t ms_range_active
;/* num range_invals active */
326 wait_queue_head_t ms_wait_queue
;
327 DECLARE_BITMAP(ms_asidmap
, GRU_MAX_GRUS
);
328 struct gru_mm_tracker ms_asids
[GRU_MAX_GRUS
];
332 * One of these structures is allocated when a GSEG is mmaped. The
333 * structure is pointed to by the vma->vm_private_data field in the vma struct.
335 struct gru_vma_data
{
336 spinlock_t vd_lock
; /* Serialize access to vma */
337 struct list_head vd_head
; /* head of linked list of gts */
338 long vd_user_options
;/* misc user option flags */
344 * One of these is allocated for each thread accessing a mmaped GRU. A linked
345 * list of these structure is hung off the struct gru_vma_data in the mm_struct.
347 struct gru_thread_state
{
348 struct list_head ts_next
; /* list - head at vma-private */
349 struct mutex ts_ctxlock
; /* load/unload CTX lock */
350 struct mm_struct
*ts_mm
; /* mm currently mapped to
352 struct vm_area_struct
*ts_vma
; /* vma of GRU context */
353 struct gru_state
*ts_gru
; /* GRU where the context is
355 struct gru_mm_struct
*ts_gms
; /* asid & ioproc struct */
356 unsigned long ts_cbr_map
; /* map of allocated CBRs */
357 unsigned long ts_dsr_map
; /* map of allocated DATA
359 unsigned long ts_steal_jiffies
;/* jiffies when context last
361 long ts_user_options
;/* misc user option flags */
362 pid_t ts_tgid_owner
; /* task that is using the
363 context - for migration */
364 int ts_tsid
; /* thread that owns the
366 int ts_tlb_int_select
;/* target cpu if interrupts
368 int ts_ctxnum
; /* context number where the
370 atomic_t ts_refcnt
; /* reference count GTS */
371 unsigned char ts_dsr_au_count
;/* Number of DSR resources
372 required for contest */
373 unsigned char ts_cbr_au_count
;/* Number of CBR resources
374 required for contest */
375 char ts_blade
; /* If >= 0, migrate context if
376 ref from diferent blade */
377 char ts_force_unload
;/* force context to be unloaded
379 char ts_cbr_idx
[GRU_CBR_AU
];/* CBR numbers of each
381 unsigned long ts_gdata
[0]; /* save area for GRU data (CB,
386 * Threaded programs actually allocate an array of GSEGs when a context is
387 * created. Each thread uses a separate GSEG. TSID is the index into the GSEG
390 #define TSID(a, v) (((a) - (v)->vm_start) / GRU_GSEG_PAGESIZE)
391 #define UGRUADDR(gts) ((gts)->ts_vma->vm_start + \
392 (gts)->ts_tsid * GRU_GSEG_PAGESIZE)
394 #define NULLCTX (-1) /* if context not loaded into GRU */
396 /*-----------------------------------------------------------------------------
401 * One of these exists for each GRU chiplet.
404 struct gru_blade_state
*gs_blade
; /* GRU state for entire
406 unsigned long gs_gru_base_paddr
; /* Physical address of
408 void *gs_gru_base_vaddr
; /* Virtual address of
410 unsigned short gs_gid
; /* unique GRU number */
411 unsigned short gs_blade_id
; /* blade of GRU */
412 unsigned char gs_tgh_local_shift
; /* used to pick TGH for
414 unsigned char gs_tgh_first_remote
; /* starting TGH# for
416 spinlock_t gs_asid_lock
; /* lock used for
418 spinlock_t gs_lock
; /* lock used for
419 assigning contexts */
421 /* -- the following are protected by the gs_asid_lock spinlock ---- */
422 unsigned int gs_asid
; /* Next availe ASID */
423 unsigned int gs_asid_limit
; /* Limit of available
425 unsigned int gs_asid_gen
; /* asid generation.
428 /* --- the following fields are protected by the gs_lock spinlock --- */
429 unsigned long gs_context_map
; /* bitmap to manage
431 unsigned long gs_cbr_map
; /* bitmap to manage CB
433 unsigned long gs_dsr_map
; /* bitmap used to manage
435 unsigned int gs_reserved_cbrs
; /* Number of kernel-
437 unsigned int gs_reserved_dsr_bytes
; /* Bytes of kernel-
439 unsigned short gs_active_contexts
; /* number of contexts
441 struct gru_thread_state
*gs_gts
[GRU_NUM_CCH
]; /* GTS currently using
446 * This structure contains the GRU state for all the GRUs on a blade.
448 struct gru_blade_state
{
449 void *kernel_cb
; /* First kernel
451 void *kernel_dsr
; /* First kernel
453 /* ---- the following are protected by the bs_lock spinlock ---- */
454 spinlock_t bs_lock
; /* lock used for
456 int bs_lru_ctxnum
; /* STEAL - last context
458 struct gru_state
*bs_lru_gru
; /* STEAL - last gru
461 struct gru_state bs_grus
[GRU_CHIPLETS_PER_BLADE
];
464 /*-----------------------------------------------------------------------------
467 #define get_tfm_for_cpu(g, c) \
468 ((struct gru_tlb_fault_map *)get_tfm((g)->gs_gru_base_vaddr, (c)))
469 #define get_tfh_by_index(g, i) \
470 ((struct gru_tlb_fault_handle *)get_tfh((g)->gs_gru_base_vaddr, (i)))
471 #define get_tgh_by_index(g, i) \
472 ((struct gru_tlb_global_handle *)get_tgh((g)->gs_gru_base_vaddr, (i)))
473 #define get_cbe_by_index(g, i) \
474 ((struct gru_control_block_extended *)get_cbe((g)->gs_gru_base_vaddr,\
477 /*-----------------------------------------------------------------------------
481 /* Given a blade# & chiplet#, get a pointer to the GRU */
482 #define get_gru(b, c) (&gru_base[b]->bs_grus[c])
484 /* Number of bytes to save/restore when unloading/loading GRU contexts */
485 #define DSR_BYTES(dsr) ((dsr) * GRU_DSR_AU_BYTES)
486 #define CBR_BYTES(cbr) ((cbr) * GRU_HANDLE_BYTES * GRU_CBR_AU_SIZE * 2)
488 /* Convert a user CB number to the actual CBRNUM */
489 #define thread_cbr_number(gts, n) ((gts)->ts_cbr_idx[(n) / GRU_CBR_AU_SIZE] \
490 * GRU_CBR_AU_SIZE + (n) % GRU_CBR_AU_SIZE)
492 /* Convert a gid to a pointer to the GRU */
493 #define GID_TO_GRU(gid) \
494 (gru_base[(gid) / GRU_CHIPLETS_PER_BLADE] ? \
495 (&gru_base[(gid) / GRU_CHIPLETS_PER_BLADE]-> \
496 bs_grus[(gid) % GRU_CHIPLETS_PER_BLADE]) : \
499 /* Scan all active GRUs in a GRU bitmap */
500 #define for_each_gru_in_bitmap(gid, map) \
501 for ((gid) = find_first_bit((map), GRU_MAX_GRUS); (gid) < GRU_MAX_GRUS;\
502 (gid)++, (gid) = find_next_bit((map), GRU_MAX_GRUS, (gid)))
504 /* Scan all active GRUs on a specific blade */
505 #define for_each_gru_on_blade(gru, nid, i) \
506 for ((gru) = gru_base[nid]->bs_grus, (i) = 0; \
507 (i) < GRU_CHIPLETS_PER_BLADE; \
511 #define foreach_gid(gid) \
512 for ((gid) = 0; (gid) < gru_max_gids; (gid)++)
514 /* Scan all active GTSs on a gru. Note: must hold ss_lock to use this macro. */
515 #define for_each_gts_on_gru(gts, gru, ctxnum) \
516 for ((ctxnum) = 0; (ctxnum) < GRU_NUM_CCH; (ctxnum)++) \
517 if (((gts) = (gru)->gs_gts[ctxnum]))
519 /* Scan each CBR whose bit is set in a TFM (or copy of) */
520 #define for_each_cbr_in_tfm(i, map) \
521 for ((i) = find_first_bit(map, GRU_NUM_CBE); \
523 (i)++, (i) = find_next_bit(map, GRU_NUM_CBE, i))
525 /* Scan each CBR in a CBR bitmap. Note: multiple CBRs in an allocation unit */
526 #define for_each_cbr_in_allocation_map(i, map, k) \
527 for ((k) = find_first_bit(map, GRU_CBR_AU); (k) < GRU_CBR_AU; \
528 (k) = find_next_bit(map, GRU_CBR_AU, (k) + 1)) \
529 for ((i) = (k)*GRU_CBR_AU_SIZE; \
530 (i) < ((k) + 1) * GRU_CBR_AU_SIZE; (i)++)
532 /* Scan each DSR in a DSR bitmap. Note: multiple DSRs in an allocation unit */
533 #define for_each_dsr_in_allocation_map(i, map, k) \
534 for ((k) = find_first_bit((const unsigned long *)map, GRU_DSR_AU);\
536 (k) = find_next_bit((const unsigned long *)map, \
537 GRU_DSR_AU, (k) + 1)) \
538 for ((i) = (k) * GRU_DSR_AU_CL; \
539 (i) < ((k) + 1) * GRU_DSR_AU_CL; (i)++)
541 #define gseg_physical_address(gru, ctxnum) \
542 ((gru)->gs_gru_base_paddr + ctxnum * GRU_GSEG_STRIDE)
543 #define gseg_virtual_address(gru, ctxnum) \
544 ((gru)->gs_gru_base_vaddr + ctxnum * GRU_GSEG_STRIDE)
546 /*-----------------------------------------------------------------------------
547 * Lock / Unlock GRU handles
548 * Use the "delresp" bit in the handle as a "lock" bit.
551 /* Lock hierarchy checking enabled only in emulator */
553 static inline void __lock_handle(void *h
)
555 while (test_and_set_bit(1, h
))
559 static inline void __unlock_handle(void *h
)
564 static inline void lock_cch_handle(struct gru_context_configuration_handle
*cch
)
569 static inline void unlock_cch_handle(struct gru_context_configuration_handle
572 __unlock_handle(cch
);
575 static inline void lock_tgh_handle(struct gru_tlb_global_handle
*tgh
)
580 static inline void unlock_tgh_handle(struct gru_tlb_global_handle
*tgh
)
582 __unlock_handle(tgh
);
585 /*-----------------------------------------------------------------------------
586 * Function prototypes & externs
588 struct gru_unload_context_req
;
590 extern struct vm_operations_struct gru_vm_ops
;
591 extern struct device
*grudev
;
593 extern struct gru_vma_data
*gru_alloc_vma_data(struct vm_area_struct
*vma
,
595 extern struct gru_thread_state
*gru_find_thread_state(struct vm_area_struct
597 extern struct gru_thread_state
*gru_alloc_thread_state(struct vm_area_struct
599 extern void gru_unload_context(struct gru_thread_state
*gts
, int savestate
);
600 extern void gts_drop(struct gru_thread_state
*gts
);
601 extern void gru_tgh_flush_init(struct gru_state
*gru
);
602 extern int gru_kservices_init(struct gru_state
*gru
);
603 extern void gru_kservices_exit(struct gru_state
*gru
);
604 extern irqreturn_t
gru_intr(int irq
, void *dev_id
);
605 extern int gru_handle_user_call_os(unsigned long address
);
606 extern int gru_user_flush_tlb(unsigned long arg
);
607 extern int gru_user_unload_context(unsigned long arg
);
608 extern int gru_get_exception_detail(unsigned long arg
);
609 extern int gru_set_task_slice(long address
);
610 extern int gru_cpu_fault_map_id(void);
611 extern struct vm_area_struct
*gru_find_vma(unsigned long vaddr
);
612 extern void gru_flush_all_tlb(struct gru_state
*gru
);
613 extern int gru_proc_init(void);
614 extern void gru_proc_exit(void);
616 extern unsigned long gru_reserve_cb_resources(struct gru_state
*gru
,
617 int cbr_au_count
, char *cbmap
);
618 extern unsigned long gru_reserve_ds_resources(struct gru_state
*gru
,
619 int dsr_au_count
, char *dsmap
);
620 extern int gru_fault(struct vm_area_struct
*, struct vm_fault
*vmf
);
621 extern struct gru_mm_struct
*gru_register_mmu_notifier(void);
622 extern void gru_drop_mmu_notifier(struct gru_mm_struct
*gms
);
624 extern void gru_flush_tlb_range(struct gru_mm_struct
*gms
, unsigned long start
,
627 extern unsigned long gru_options
;
629 #endif /* __GRUTABLES_H__ */