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__
29 * VDATA-VMA Data - Holds a few parameters. Head of linked list of
30 * GTS tables for threads using the GSEG
31 * GTS - Gru Thread State - contains info for managing a GSEG context. A
32 * GTS is allocated for each thread accessing a
34 * GTD - GRU Thread Data - contains shadow copy of GRU data when GSEG is
35 * not loaded into a GRU
36 * GMS - GRU Memory Struct - Used to manage TLB shootdowns. Tracks GRUs
37 * where a GSEG has been loaded. Similar to
38 * an mm_struct but for GRU.
40 * GS - GRU State - Used to manage the state of a GRU chiplet
41 * BS - Blade State - Used to manage state of all GRU chiplets
45 * Normal task tables for task using GRU.
46 * - 2 threads in process
47 * - 2 GSEGs open in process
48 * - GSEG1 is being used by both threads
49 * - GSEG2 is used only by thread 2
52 * task ---+---> mm ->------ (notifier) -------+-> gms
54 * |--> vma -> vdata ---> gts--->| GSEG1 (thread1)
56 * | +-> gts--->| GSEG1 (thread2)
58 * |--> vma -> vdata ---> gts--->| GSEG2 (thread2)
62 * GSEGs are marked DONTCOPY on fork
65 * file.private_data -> NULL
70 * After gseg reference
81 #include <linux/rmap.h>
82 #include <linux/interrupt.h>
83 #include <linux/mutex.h>
84 #include <linux/wait.h>
85 #include <linux/mmu_notifier.h>
87 #include "gruhandles.h"
89 extern struct gru_stats_s gru_stats
;
90 extern struct gru_blade_state
*gru_base
[];
91 extern unsigned long gru_start_paddr
, gru_end_paddr
;
93 #define GRU_MAX_BLADES MAX_NUMNODES
94 #define GRU_MAX_GRUS (GRU_MAX_BLADES * GRU_CHIPLETS_PER_BLADE)
96 #define GRU_DRIVER_ID_STR "SGI GRU Device Driver"
97 #define GRU_DRIVER_VERSION_STR "0.80"
103 atomic_long_t vdata_alloc
;
104 atomic_long_t vdata_free
;
105 atomic_long_t gts_alloc
;
106 atomic_long_t gts_free
;
107 atomic_long_t vdata_double_alloc
;
108 atomic_long_t gts_double_allocate
;
109 atomic_long_t assign_context
;
110 atomic_long_t assign_context_failed
;
111 atomic_long_t free_context
;
112 atomic_long_t load_context
;
113 atomic_long_t unload_context
;
114 atomic_long_t steal_context
;
115 atomic_long_t steal_context_failed
;
117 atomic_long_t break_cow
;
118 atomic_long_t asid_new
;
119 atomic_long_t asid_next
;
120 atomic_long_t asid_wrap
;
121 atomic_long_t asid_reuse
;
123 atomic_long_t call_os
;
124 atomic_long_t call_os_check_for_bug
;
125 atomic_long_t call_os_wait_queue
;
126 atomic_long_t user_flush_tlb
;
127 atomic_long_t user_unload_context
;
128 atomic_long_t user_exception
;
129 atomic_long_t set_task_slice
;
130 atomic_long_t migrate_check
;
131 atomic_long_t migrated_retarget
;
132 atomic_long_t migrated_unload
;
133 atomic_long_t migrated_unload_delay
;
134 atomic_long_t migrated_nopfn_retarget
;
135 atomic_long_t migrated_nopfn_unload
;
136 atomic_long_t tlb_dropin
;
137 atomic_long_t tlb_dropin_fail_no_asid
;
138 atomic_long_t tlb_dropin_fail_upm
;
139 atomic_long_t tlb_dropin_fail_invalid
;
140 atomic_long_t tlb_dropin_fail_range_active
;
141 atomic_long_t tlb_dropin_fail_idle
;
142 atomic_long_t tlb_dropin_fail_fmm
;
143 atomic_long_t mmu_invalidate_range
;
144 atomic_long_t mmu_invalidate_page
;
145 atomic_long_t mmu_clear_flush_young
;
146 atomic_long_t flush_tlb
;
147 atomic_long_t flush_tlb_gru
;
148 atomic_long_t flush_tlb_gru_tgh
;
149 atomic_long_t flush_tlb_gru_zero_asid
;
151 atomic_long_t copy_gpa
;
153 atomic_long_t mesq_receive
;
154 atomic_long_t mesq_receive_none
;
155 atomic_long_t mesq_send
;
156 atomic_long_t mesq_send_failed
;
157 atomic_long_t mesq_noop
;
158 atomic_long_t mesq_send_unexpected_error
;
159 atomic_long_t mesq_send_lb_overflow
;
160 atomic_long_t mesq_send_qlimit_reached
;
161 atomic_long_t mesq_send_amo_nacked
;
162 atomic_long_t mesq_send_put_nacked
;
163 atomic_long_t mesq_qf_not_full
;
164 atomic_long_t mesq_qf_locked
;
165 atomic_long_t mesq_qf_noop_not_full
;
166 atomic_long_t mesq_qf_switch_head_failed
;
167 atomic_long_t mesq_qf_unexpected_error
;
168 atomic_long_t mesq_noop_unexpected_error
;
169 atomic_long_t mesq_noop_lb_overflow
;
170 atomic_long_t mesq_noop_qlimit_reached
;
171 atomic_long_t mesq_noop_amo_nacked
;
172 atomic_long_t mesq_noop_put_nacked
;
178 #define GRU_QUICKLOOK 4
181 #define IRQ_GRU 110 /* Starting IRQ number for interrupts */
183 /* Delay in jiffies between attempts to assign a GRU context */
184 #define GRU_ASSIGN_DELAY ((HZ * 20) / 1000)
187 * If a process has it's context stolen, min delay in jiffies before trying to
188 * steal a context from another process.
190 #define GRU_STEAL_DELAY ((HZ * 200) / 1000)
192 #define STAT(id) do { \
193 if (options & OPT_STATS) \
194 atomic_long_inc(&gru_stats.id); \
197 #ifdef CONFIG_SGI_GRU_DEBUG
198 #define gru_dbg(dev, fmt, x...) \
200 if (options & OPT_DPRINT) \
201 dev_dbg(dev, "%s: " fmt, __func__, x); \
204 #define gru_dbg(x...)
207 /*-----------------------------------------------------------------------------
210 #define MAX_ASID 0xfffff0
212 #define ASID_INC 8 /* number of regions */
214 /* Generate a GRU asid value from a GRU base asid & a virtual address. */
215 #if defined CONFIG_IA64
216 #define VADDR_HI_BIT 64
217 #define GRUREGION(addr) ((addr) >> (VADDR_HI_BIT - 3) & 3)
218 #elif defined __x86_64
219 #define VADDR_HI_BIT 48
220 #define GRUREGION(addr) (0) /* ZZZ could do better */
222 #error "Unsupported architecture"
224 #define GRUASID(asid, addr) ((asid) + GRUREGION(addr))
226 /*------------------------------------------------------------------------------
233 * This structure is pointed to from the mmstruct via the notifier pointer.
234 * There is one of these per address space.
236 struct gru_mm_tracker
{
237 unsigned int mt_asid_gen
; /* ASID wrap count */
238 int mt_asid
; /* current base ASID for gru */
239 unsigned short mt_ctxbitmap
; /* bitmap of contexts using
243 struct gru_mm_struct
{
244 struct mmu_notifier ms_notifier
;
246 spinlock_t ms_asid_lock
; /* protects ASID assignment */
247 atomic_t ms_range_active
;/* num range_invals active */
249 wait_queue_head_t ms_wait_queue
;
250 DECLARE_BITMAP(ms_asidmap
, GRU_MAX_GRUS
);
251 struct gru_mm_tracker ms_asids
[GRU_MAX_GRUS
];
255 * One of these structures is allocated when a GSEG is mmaped. The
256 * structure is pointed to by the vma->vm_private_data field in the vma struct.
258 struct gru_vma_data
{
259 spinlock_t vd_lock
; /* Serialize access to vma */
260 struct list_head vd_head
; /* head of linked list of gts */
261 long vd_user_options
;/* misc user option flags */
267 * One of these is allocated for each thread accessing a mmaped GRU. A linked
268 * list of these structure is hung off the struct gru_vma_data in the mm_struct.
270 struct gru_thread_state
{
271 struct list_head ts_next
; /* list - head at vma-private */
272 struct mutex ts_ctxlock
; /* load/unload CTX lock */
273 struct mm_struct
*ts_mm
; /* mm currently mapped to
275 struct vm_area_struct
*ts_vma
; /* vma of GRU context */
276 struct gru_state
*ts_gru
; /* GRU where the context is
278 struct gru_mm_struct
*ts_gms
; /* asid & ioproc struct */
279 unsigned long ts_cbr_map
; /* map of allocated CBRs */
280 unsigned long ts_dsr_map
; /* map of allocated DATA
282 unsigned long ts_steal_jiffies
;/* jiffies when context last
284 long ts_user_options
;/* misc user option flags */
285 pid_t ts_tgid_owner
; /* task that is using the
286 context - for migration */
287 int ts_tsid
; /* thread that owns the
289 int ts_tlb_int_select
;/* target cpu if interrupts
291 int ts_ctxnum
; /* context number where the
293 atomic_t ts_refcnt
; /* reference count GTS */
294 unsigned char ts_dsr_au_count
;/* Number of DSR resources
295 required for contest */
296 unsigned char ts_cbr_au_count
;/* Number of CBR resources
297 required for contest */
298 char ts_force_unload
;/* force context to be unloaded
300 char ts_cbr_idx
[GRU_CBR_AU
];/* CBR numbers of each
302 unsigned long ts_gdata
[0]; /* save area for GRU data (CB,
307 * Threaded programs actually allocate an array of GSEGs when a context is
308 * created. Each thread uses a separate GSEG. TSID is the index into the GSEG
311 #define TSID(a, v) (((a) - (v)->vm_start) / GRU_GSEG_PAGESIZE)
312 #define UGRUADDR(gts) ((gts)->ts_vma->vm_start + \
313 (gts)->ts_tsid * GRU_GSEG_PAGESIZE)
315 #define NULLCTX (-1) /* if context not loaded into GRU */
317 /*-----------------------------------------------------------------------------
322 * One of these exists for each GRU chiplet.
325 struct gru_blade_state
*gs_blade
; /* GRU state for entire
327 unsigned long gs_gru_base_paddr
; /* Physical address of
329 void *gs_gru_base_vaddr
; /* Virtual address of
331 unsigned char gs_gid
; /* unique GRU number */
332 unsigned char gs_tgh_local_shift
; /* used to pick TGH for
334 unsigned char gs_tgh_first_remote
; /* starting TGH# for
336 unsigned short gs_blade_id
; /* blade of GRU */
337 spinlock_t gs_asid_lock
; /* lock used for
339 spinlock_t gs_lock
; /* lock used for
340 assigning contexts */
342 /* -- the following are protected by the gs_asid_lock spinlock ---- */
343 unsigned int gs_asid
; /* Next availe ASID */
344 unsigned int gs_asid_limit
; /* Limit of available
346 unsigned int gs_asid_gen
; /* asid generation.
349 /* --- the following fields are protected by the gs_lock spinlock --- */
350 unsigned long gs_context_map
; /* bitmap to manage
352 unsigned long gs_cbr_map
; /* bitmap to manage CB
354 unsigned long gs_dsr_map
; /* bitmap used to manage
356 unsigned int gs_reserved_cbrs
; /* Number of kernel-
358 unsigned int gs_reserved_dsr_bytes
; /* Bytes of kernel-
360 unsigned short gs_active_contexts
; /* number of contexts
362 struct gru_thread_state
*gs_gts
[GRU_NUM_CCH
]; /* GTS currently using
367 * This structure contains the GRU state for all the GRUs on a blade.
369 struct gru_blade_state
{
370 void *kernel_cb
; /* First kernel
372 void *kernel_dsr
; /* First kernel
374 /* ---- the following are protected by the bs_lock spinlock ---- */
375 spinlock_t bs_lock
; /* lock used for
377 int bs_lru_ctxnum
; /* STEAL - last context
379 struct gru_state
*bs_lru_gru
; /* STEAL - last gru
382 struct gru_state bs_grus
[GRU_CHIPLETS_PER_BLADE
];
385 /*-----------------------------------------------------------------------------
388 #define get_tfm_for_cpu(g, c) \
389 ((struct gru_tlb_fault_map *)get_tfm((g)->gs_gru_base_vaddr, (c)))
390 #define get_tfh_by_index(g, i) \
391 ((struct gru_tlb_fault_handle *)get_tfh((g)->gs_gru_base_vaddr, (i)))
392 #define get_tgh_by_index(g, i) \
393 ((struct gru_tlb_global_handle *)get_tgh((g)->gs_gru_base_vaddr, (i)))
394 #define get_cbe_by_index(g, i) \
395 ((struct gru_control_block_extended *)get_cbe((g)->gs_gru_base_vaddr,\
398 /*-----------------------------------------------------------------------------
402 /* Given a blade# & chiplet#, get a pointer to the GRU */
403 #define get_gru(b, c) (&gru_base[b]->bs_grus[c])
405 /* Number of bytes to save/restore when unloading/loading GRU contexts */
406 #define DSR_BYTES(dsr) ((dsr) * GRU_DSR_AU_BYTES)
407 #define CBR_BYTES(cbr) ((cbr) * GRU_HANDLE_BYTES * GRU_CBR_AU_SIZE * 2)
409 /* Convert a user CB number to the actual CBRNUM */
410 #define thread_cbr_number(gts, n) ((gts)->ts_cbr_idx[(n) / GRU_CBR_AU_SIZE] \
411 * GRU_CBR_AU_SIZE + (n) % GRU_CBR_AU_SIZE)
413 /* Convert a gid to a pointer to the GRU */
414 #define GID_TO_GRU(gid) \
415 (gru_base[(gid) / GRU_CHIPLETS_PER_BLADE] ? \
416 (&gru_base[(gid) / GRU_CHIPLETS_PER_BLADE]-> \
417 bs_grus[(gid) % GRU_CHIPLETS_PER_BLADE]) : \
420 /* Scan all active GRUs in a GRU bitmap */
421 #define for_each_gru_in_bitmap(gid, map) \
422 for ((gid) = find_first_bit((map), GRU_MAX_GRUS); (gid) < GRU_MAX_GRUS;\
423 (gid)++, (gid) = find_next_bit((map), GRU_MAX_GRUS, (gid)))
425 /* Scan all active GRUs on a specific blade */
426 #define for_each_gru_on_blade(gru, nid, i) \
427 for ((gru) = gru_base[nid]->bs_grus, (i) = 0; \
428 (i) < GRU_CHIPLETS_PER_BLADE; \
431 /* Scan all active GTSs on a gru. Note: must hold ss_lock to use this macro. */
432 #define for_each_gts_on_gru(gts, gru, ctxnum) \
433 for ((ctxnum) = 0; (ctxnum) < GRU_NUM_CCH; (ctxnum)++) \
434 if (((gts) = (gru)->gs_gts[ctxnum]))
436 /* Scan each CBR whose bit is set in a TFM (or copy of) */
437 #define for_each_cbr_in_tfm(i, map) \
438 for ((i) = find_first_bit(map, GRU_NUM_CBE); \
440 (i)++, (i) = find_next_bit(map, GRU_NUM_CBE, i))
442 /* Scan each CBR in a CBR bitmap. Note: multiple CBRs in an allocation unit */
443 #define for_each_cbr_in_allocation_map(i, map, k) \
444 for ((k) = find_first_bit(map, GRU_CBR_AU); (k) < GRU_CBR_AU; \
445 (k) = find_next_bit(map, GRU_CBR_AU, (k) + 1)) \
446 for ((i) = (k)*GRU_CBR_AU_SIZE; \
447 (i) < ((k) + 1) * GRU_CBR_AU_SIZE; (i)++)
449 /* Scan each DSR in a DSR bitmap. Note: multiple DSRs in an allocation unit */
450 #define for_each_dsr_in_allocation_map(i, map, k) \
451 for ((k) = find_first_bit((const unsigned long *)map, GRU_DSR_AU);\
453 (k) = find_next_bit((const unsigned long *)map, \
454 GRU_DSR_AU, (k) + 1)) \
455 for ((i) = (k) * GRU_DSR_AU_CL; \
456 (i) < ((k) + 1) * GRU_DSR_AU_CL; (i)++)
458 #define gseg_physical_address(gru, ctxnum) \
459 ((gru)->gs_gru_base_paddr + ctxnum * GRU_GSEG_STRIDE)
460 #define gseg_virtual_address(gru, ctxnum) \
461 ((gru)->gs_gru_base_vaddr + ctxnum * GRU_GSEG_STRIDE)
463 /*-----------------------------------------------------------------------------
464 * Lock / Unlock GRU handles
465 * Use the "delresp" bit in the handle as a "lock" bit.
468 /* Lock hierarchy checking enabled only in emulator */
470 static inline void __lock_handle(void *h
)
472 while (test_and_set_bit(1, h
))
476 static inline void __unlock_handle(void *h
)
481 static inline void lock_cch_handle(struct gru_context_configuration_handle
*cch
)
486 static inline void unlock_cch_handle(struct gru_context_configuration_handle
489 __unlock_handle(cch
);
492 static inline void lock_tgh_handle(struct gru_tlb_global_handle
*tgh
)
497 static inline void unlock_tgh_handle(struct gru_tlb_global_handle
*tgh
)
499 __unlock_handle(tgh
);
502 /*-----------------------------------------------------------------------------
503 * Function prototypes & externs
505 struct gru_unload_context_req
;
507 extern struct vm_operations_struct gru_vm_ops
;
508 extern struct device
*grudev
;
510 extern struct gru_vma_data
*gru_alloc_vma_data(struct vm_area_struct
*vma
,
512 extern struct gru_thread_state
*gru_find_thread_state(struct vm_area_struct
514 extern struct gru_thread_state
*gru_alloc_thread_state(struct vm_area_struct
516 extern void gru_unload_context(struct gru_thread_state
*gts
, int savestate
);
517 extern void gts_drop(struct gru_thread_state
*gts
);
518 extern void gru_tgh_flush_init(struct gru_state
*gru
);
519 extern int gru_kservices_init(struct gru_state
*gru
);
520 extern irqreturn_t
gru_intr(int irq
, void *dev_id
);
521 extern int gru_handle_user_call_os(unsigned long address
);
522 extern int gru_user_flush_tlb(unsigned long arg
);
523 extern int gru_user_unload_context(unsigned long arg
);
524 extern int gru_get_exception_detail(unsigned long arg
);
525 extern int gru_set_task_slice(long address
);
526 extern int gru_cpu_fault_map_id(void);
527 extern struct vm_area_struct
*gru_find_vma(unsigned long vaddr
);
528 extern void gru_flush_all_tlb(struct gru_state
*gru
);
529 extern int gru_proc_init(void);
530 extern void gru_proc_exit(void);
532 extern unsigned long reserve_gru_cb_resources(struct gru_state
*gru
,
533 int cbr_au_count
, char *cbmap
);
534 extern unsigned long reserve_gru_ds_resources(struct gru_state
*gru
,
535 int dsr_au_count
, char *dsmap
);
536 extern int gru_fault(struct vm_area_struct
*, struct vm_fault
*vmf
);
537 extern struct gru_mm_struct
*gru_register_mmu_notifier(void);
538 extern void gru_drop_mmu_notifier(struct gru_mm_struct
*gms
);
540 extern void gru_flush_tlb_range(struct gru_mm_struct
*gms
, unsigned long start
,
543 extern unsigned long options
;
545 #endif /* __GRUTABLES_H__ */