grutables.h 23 KB

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