grutlbpurge.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * MMUOPS callbacks + TLB flushing
  5. *
  6. * This file handles emu notifier callbacks from the core kernel. The callbacks
  7. * are used to update the TLB in the GRU as a result of changes in the
  8. * state of a process address space. This file also handles TLB invalidates
  9. * from the GRU driver.
  10. *
  11. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/device.h>
  33. #include <linux/hugetlb.h>
  34. #include <linux/delay.h>
  35. #include <linux/timex.h>
  36. #include <linux/srcu.h>
  37. #include <asm/processor.h>
  38. #include "gru.h"
  39. #include "grutables.h"
  40. #include <asm/uv/uv_hub.h>
  41. #define gru_random() get_cycles()
  42. /* ---------------------------------- TLB Invalidation functions --------
  43. * get_tgh_handle
  44. *
  45. * Find a TGH to use for issuing a TLB invalidate. For GRUs that are on the
  46. * local blade, use a fixed TGH that is a function of the blade-local cpu
  47. * number. Normally, this TGH is private to the cpu & no contention occurs for
  48. * the TGH. For offblade GRUs, select a random TGH in the range above the
  49. * private TGHs. A spinlock is required to access this TGH & the lock must be
  50. * released when the invalidate is completes. This sucks, but it is the best we
  51. * can do.
  52. *
  53. * Note that the spinlock is IN the TGH handle so locking does not involve
  54. * additional cache lines.
  55. *
  56. */
  57. static inline int get_off_blade_tgh(struct gru_state *gru)
  58. {
  59. int n;
  60. n = GRU_NUM_TGH - gru->gs_tgh_first_remote;
  61. n = gru_random() % n;
  62. n += gru->gs_tgh_first_remote;
  63. return n;
  64. }
  65. static inline int get_on_blade_tgh(struct gru_state *gru)
  66. {
  67. return uv_blade_processor_id() >> gru->gs_tgh_local_shift;
  68. }
  69. static struct gru_tlb_global_handle *get_lock_tgh_handle(struct gru_state
  70. *gru)
  71. {
  72. struct gru_tlb_global_handle *tgh;
  73. int n;
  74. preempt_disable();
  75. if (uv_numa_blade_id() == gru->gs_blade_id)
  76. n = get_on_blade_tgh(gru);
  77. else
  78. n = get_off_blade_tgh(gru);
  79. tgh = get_tgh_by_index(gru, n);
  80. lock_tgh_handle(tgh);
  81. return tgh;
  82. }
  83. static void get_unlock_tgh_handle(struct gru_tlb_global_handle *tgh)
  84. {
  85. unlock_tgh_handle(tgh);
  86. preempt_enable();
  87. }
  88. /*
  89. * gru_flush_tlb_range
  90. *
  91. * General purpose TLB invalidation function. This function scans every GRU in
  92. * the ENTIRE system (partition) looking for GRUs where the specified MM has
  93. * been accessed by the GRU. For each GRU found, the TLB must be invalidated OR
  94. * the ASID invalidated. Invalidating an ASID causes a new ASID to be assigned
  95. * on the next fault. This effectively flushes the ENTIRE TLB for the MM at the
  96. * cost of (possibly) a large number of future TLBmisses.
  97. *
  98. * The current algorithm is optimized based on the following (somewhat true)
  99. * assumptions:
  100. * - GRU contexts are not loaded into a GRU unless a reference is made to
  101. * the data segment or control block (this is true, not an assumption).
  102. * If a DS/CB is referenced, the user will also issue instructions that
  103. * cause TLBmisses. It is not necessary to optimize for the case where
  104. * contexts are loaded but no instructions cause TLB misses. (I know
  105. * this will happen but I'm not optimizing for it).
  106. * - GRU instructions to invalidate TLB entries are SLOOOOWWW - normally
  107. * a few usec but in unusual cases, it could be longer. Avoid if
  108. * possible.
  109. * - intrablade process migration between cpus is not frequent but is
  110. * common.
  111. * - a GRU context is not typically migrated to a different GRU on the
  112. * blade because of intrablade migration
  113. * - interblade migration is rare. Processes migrate their GRU context to
  114. * the new blade.
  115. * - if interblade migration occurs, migration back to the original blade
  116. * is very very rare (ie., no optimization for this case)
  117. * - most GRU instruction operate on a subset of the user REGIONS. Code
  118. * & shared library regions are not likely targets of GRU instructions.
  119. *
  120. * To help improve the efficiency of TLB invalidation, the GMS data
  121. * structure is maintained for EACH address space (MM struct). The GMS is
  122. * also the structure that contains the pointer to the mmu callout
  123. * functions. This structure is linked to the mm_struct for the address space
  124. * using the mmu "register" function. The mmu interfaces are used to
  125. * provide the callbacks for TLB invalidation. The GMS contains:
  126. *
  127. * - asid[maxgrus] array. ASIDs are assigned to a GRU when a context is
  128. * loaded into the GRU.
  129. * - asidmap[maxgrus]. bitmap to make it easier to find non-zero asids in
  130. * the above array
  131. * - ctxbitmap[maxgrus]. Indicates the contexts that are currently active
  132. * in the GRU for the address space. This bitmap must be passed to the
  133. * GRU to do an invalidate.
  134. *
  135. * The current algorithm for invalidating TLBs is:
  136. * - scan the asidmap for GRUs where the context has been loaded, ie,
  137. * asid is non-zero.
  138. * - for each gru found:
  139. * - if the ctxtmap is non-zero, there are active contexts in the
  140. * GRU. TLB invalidate instructions must be issued to the GRU.
  141. * - if the ctxtmap is zero, no context is active. Set the ASID to
  142. * zero to force a full TLB invalidation. This is fast but will
  143. * cause a lot of TLB misses if the context is reloaded onto the
  144. * GRU
  145. *
  146. */
  147. void gru_flush_tlb_range(struct gru_mm_struct *gms, unsigned long start,
  148. unsigned long len)
  149. {
  150. struct gru_state *gru;
  151. struct gru_mm_tracker *asids;
  152. struct gru_tlb_global_handle *tgh;
  153. unsigned long num;
  154. int grupagesize, pagesize, pageshift, gid, asid;
  155. /* ZZZ TODO - handle huge pages */
  156. pageshift = PAGE_SHIFT;
  157. pagesize = (1UL << pageshift);
  158. grupagesize = GRU_PAGESIZE(pageshift);
  159. num = min(((len + pagesize - 1) >> pageshift), GRUMAXINVAL);
  160. STAT(flush_tlb);
  161. gru_dbg(grudev, "gms %p, start 0x%lx, len 0x%lx, asidmap 0x%lx\n", gms,
  162. start, len, gms->ms_asidmap[0]);
  163. spin_lock(&gms->ms_asid_lock);
  164. for_each_gru_in_bitmap(gid, gms->ms_asidmap) {
  165. STAT(flush_tlb_gru);
  166. gru = GID_TO_GRU(gid);
  167. asids = gms->ms_asids + gid;
  168. asid = asids->mt_asid;
  169. if (asids->mt_ctxbitmap && asid) {
  170. STAT(flush_tlb_gru_tgh);
  171. asid = GRUASID(asid, start);
  172. gru_dbg(grudev,
  173. " FLUSH gruid %d, asid 0x%x, vaddr 0x%lx, vamask 0x%x, num %ld, cbmap 0x%x\n",
  174. gid, asid, start, grupagesize, num, asids->mt_ctxbitmap);
  175. tgh = get_lock_tgh_handle(gru);
  176. tgh_invalidate(tgh, start, ~0, asid, grupagesize, 0,
  177. num - 1, asids->mt_ctxbitmap);
  178. get_unlock_tgh_handle(tgh);
  179. } else {
  180. STAT(flush_tlb_gru_zero_asid);
  181. asids->mt_asid = 0;
  182. __clear_bit(gru->gs_gid, gms->ms_asidmap);
  183. gru_dbg(grudev,
  184. " CLEARASID gruid %d, asid 0x%x, cbtmap 0x%x, asidmap 0x%lx\n",
  185. gid, asid, asids->mt_ctxbitmap,
  186. gms->ms_asidmap[0]);
  187. }
  188. }
  189. spin_unlock(&gms->ms_asid_lock);
  190. }
  191. /*
  192. * Flush the entire TLB on a chiplet.
  193. */
  194. void gru_flush_all_tlb(struct gru_state *gru)
  195. {
  196. struct gru_tlb_global_handle *tgh;
  197. gru_dbg(grudev, "gid %d\n", gru->gs_gid);
  198. tgh = get_lock_tgh_handle(gru);
  199. tgh_invalidate(tgh, 0, ~0, 0, 1, 1, GRUMAXINVAL - 1, 0xffff);
  200. get_unlock_tgh_handle(tgh);
  201. }
  202. /*
  203. * MMUOPS notifier callout functions
  204. */
  205. static void gru_invalidate_range_start(struct mmu_notifier *mn,
  206. struct mm_struct *mm,
  207. unsigned long start, unsigned long end)
  208. {
  209. struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
  210. ms_notifier);
  211. STAT(mmu_invalidate_range);
  212. atomic_inc(&gms->ms_range_active);
  213. gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx, act %d\n", gms,
  214. start, end, atomic_read(&gms->ms_range_active));
  215. gru_flush_tlb_range(gms, start, end - start);
  216. }
  217. static void gru_invalidate_range_end(struct mmu_notifier *mn,
  218. struct mm_struct *mm, unsigned long start,
  219. unsigned long end)
  220. {
  221. struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
  222. ms_notifier);
  223. /* ..._and_test() provides needed barrier */
  224. (void)atomic_dec_and_test(&gms->ms_range_active);
  225. wake_up_all(&gms->ms_wait_queue);
  226. gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx\n", gms, start, end);
  227. }
  228. static void gru_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm,
  229. unsigned long address)
  230. {
  231. struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
  232. ms_notifier);
  233. STAT(mmu_invalidate_page);
  234. gru_flush_tlb_range(gms, address, PAGE_SIZE);
  235. gru_dbg(grudev, "gms %p, address 0x%lx\n", gms, address);
  236. }
  237. static void gru_release(struct mmu_notifier *mn, struct mm_struct *mm)
  238. {
  239. struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,
  240. ms_notifier);
  241. gms->ms_released = 1;
  242. gru_dbg(grudev, "gms %p\n", gms);
  243. }
  244. static const struct mmu_notifier_ops gru_mmuops = {
  245. .invalidate_page = gru_invalidate_page,
  246. .invalidate_range_start = gru_invalidate_range_start,
  247. .invalidate_range_end = gru_invalidate_range_end,
  248. .release = gru_release,
  249. };
  250. /* Move this to the basic mmu_notifier file. But for now... */
  251. static struct mmu_notifier *mmu_find_ops(struct mm_struct *mm,
  252. const struct mmu_notifier_ops *ops)
  253. {
  254. struct mmu_notifier *mn, *gru_mn = NULL;
  255. if (mm->mmu_notifier_mm) {
  256. rcu_read_lock();
  257. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list,
  258. hlist)
  259. if (mn->ops == ops) {
  260. gru_mn = mn;
  261. break;
  262. }
  263. rcu_read_unlock();
  264. }
  265. return gru_mn;
  266. }
  267. struct gru_mm_struct *gru_register_mmu_notifier(void)
  268. {
  269. struct gru_mm_struct *gms;
  270. struct mmu_notifier *mn;
  271. int err;
  272. mn = mmu_find_ops(current->mm, &gru_mmuops);
  273. if (mn) {
  274. gms = container_of(mn, struct gru_mm_struct, ms_notifier);
  275. atomic_inc(&gms->ms_refcnt);
  276. } else {
  277. gms = kzalloc(sizeof(*gms), GFP_KERNEL);
  278. if (!gms)
  279. return ERR_PTR(-ENOMEM);
  280. STAT(gms_alloc);
  281. spin_lock_init(&gms->ms_asid_lock);
  282. gms->ms_notifier.ops = &gru_mmuops;
  283. atomic_set(&gms->ms_refcnt, 1);
  284. init_waitqueue_head(&gms->ms_wait_queue);
  285. err = __mmu_notifier_register(&gms->ms_notifier, current->mm);
  286. if (err)
  287. goto error;
  288. }
  289. if (gms)
  290. gru_dbg(grudev, "gms %p, refcnt %d\n", gms,
  291. atomic_read(&gms->ms_refcnt));
  292. return gms;
  293. error:
  294. kfree(gms);
  295. return ERR_PTR(err);
  296. }
  297. void gru_drop_mmu_notifier(struct gru_mm_struct *gms)
  298. {
  299. gru_dbg(grudev, "gms %p, refcnt %d, released %d\n", gms,
  300. atomic_read(&gms->ms_refcnt), gms->ms_released);
  301. if (atomic_dec_return(&gms->ms_refcnt) == 0) {
  302. if (!gms->ms_released)
  303. mmu_notifier_unregister(&gms->ms_notifier, current->mm);
  304. kfree(gms);
  305. STAT(gms_free);
  306. }
  307. }
  308. /*
  309. * Setup TGH parameters. There are:
  310. * - 24 TGH handles per GRU chiplet
  311. * - a portion (MAX_LOCAL_TGH) of the handles are reserved for
  312. * use by blade-local cpus
  313. * - the rest are used by off-blade cpus. This usage is
  314. * less frequent than blade-local usage.
  315. *
  316. * For now, use 16 handles for local flushes, 8 for remote flushes. If the blade
  317. * has less tan or equal to 16 cpus, each cpu has a unique handle that it can
  318. * use.
  319. */
  320. #define MAX_LOCAL_TGH 16
  321. void gru_tgh_flush_init(struct gru_state *gru)
  322. {
  323. int cpus, shift = 0, n;
  324. cpus = uv_blade_nr_possible_cpus(gru->gs_blade_id);
  325. /* n = cpus rounded up to next power of 2 */
  326. if (cpus) {
  327. n = 1 << fls(cpus - 1);
  328. /*
  329. * shift count for converting local cpu# to TGH index
  330. * 0 if cpus <= MAX_LOCAL_TGH,
  331. * 1 if cpus <= 2*MAX_LOCAL_TGH,
  332. * etc
  333. */
  334. shift = max(0, fls(n - 1) - fls(MAX_LOCAL_TGH - 1));
  335. }
  336. gru->gs_tgh_local_shift = shift;
  337. /* first starting TGH index to use for remote purges */
  338. gru->gs_tgh_first_remote = (cpus + (1 << shift) - 1) >> shift;
  339. }