vas-window.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * Copyright 2016-17 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) "vas: " fmt
  10. #include <linux/types.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/log2.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/cred.h>
  17. #include <asm/switch_to.h>
  18. #include <asm/ppc-opcode.h>
  19. #include "vas.h"
  20. #include "copy-paste.h"
  21. #define CREATE_TRACE_POINTS
  22. #include "vas-trace.h"
  23. /*
  24. * Compute the paste address region for the window @window using the
  25. * ->paste_base_addr and ->paste_win_id_shift we got from device tree.
  26. */
  27. static void compute_paste_address(struct vas_window *window, u64 *addr, int *len)
  28. {
  29. int winid;
  30. u64 base, shift;
  31. base = window->vinst->paste_base_addr;
  32. shift = window->vinst->paste_win_id_shift;
  33. winid = window->winid;
  34. *addr = base + (winid << shift);
  35. if (len)
  36. *len = PAGE_SIZE;
  37. pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid, *addr);
  38. }
  39. u64 vas_win_paste_addr(struct vas_window *win)
  40. {
  41. u64 addr;
  42. compute_paste_address(win, &addr, NULL);
  43. return addr;
  44. }
  45. EXPORT_SYMBOL(vas_win_paste_addr);
  46. static inline void get_hvwc_mmio_bar(struct vas_window *window,
  47. u64 *start, int *len)
  48. {
  49. u64 pbaddr;
  50. pbaddr = window->vinst->hvwc_bar_start;
  51. *start = pbaddr + window->winid * VAS_HVWC_SIZE;
  52. *len = VAS_HVWC_SIZE;
  53. }
  54. static inline void get_uwc_mmio_bar(struct vas_window *window,
  55. u64 *start, int *len)
  56. {
  57. u64 pbaddr;
  58. pbaddr = window->vinst->uwc_bar_start;
  59. *start = pbaddr + window->winid * VAS_UWC_SIZE;
  60. *len = VAS_UWC_SIZE;
  61. }
  62. /*
  63. * Map the paste bus address of the given send window into kernel address
  64. * space. Unlike MMIO regions (map_mmio_region() below), paste region must
  65. * be mapped cache-able and is only applicable to send windows.
  66. */
  67. static void *map_paste_region(struct vas_window *txwin)
  68. {
  69. int len;
  70. void *map;
  71. char *name;
  72. u64 start;
  73. name = kasprintf(GFP_KERNEL, "window-v%d-w%d", txwin->vinst->vas_id,
  74. txwin->winid);
  75. if (!name)
  76. goto free_name;
  77. txwin->paste_addr_name = name;
  78. compute_paste_address(txwin, &start, &len);
  79. if (!request_mem_region(start, len, name)) {
  80. pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
  81. __func__, start, len);
  82. goto free_name;
  83. }
  84. map = ioremap_cache(start, len);
  85. if (!map) {
  86. pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__,
  87. start, len);
  88. goto free_name;
  89. }
  90. pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start, map);
  91. return map;
  92. free_name:
  93. kfree(name);
  94. return ERR_PTR(-ENOMEM);
  95. }
  96. static void *map_mmio_region(char *name, u64 start, int len)
  97. {
  98. void *map;
  99. if (!request_mem_region(start, len, name)) {
  100. pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
  101. __func__, start, len);
  102. return NULL;
  103. }
  104. map = ioremap(start, len);
  105. if (!map) {
  106. pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__, start,
  107. len);
  108. return NULL;
  109. }
  110. return map;
  111. }
  112. static void unmap_region(void *addr, u64 start, int len)
  113. {
  114. iounmap(addr);
  115. release_mem_region((phys_addr_t)start, len);
  116. }
  117. /*
  118. * Unmap the paste address region for a window.
  119. */
  120. static void unmap_paste_region(struct vas_window *window)
  121. {
  122. int len;
  123. u64 busaddr_start;
  124. if (window->paste_kaddr) {
  125. compute_paste_address(window, &busaddr_start, &len);
  126. unmap_region(window->paste_kaddr, busaddr_start, len);
  127. window->paste_kaddr = NULL;
  128. kfree(window->paste_addr_name);
  129. window->paste_addr_name = NULL;
  130. }
  131. }
  132. /*
  133. * Unmap the MMIO regions for a window. Hold the vas_mutex so we don't
  134. * unmap when the window's debugfs dir is in use. This serializes close
  135. * of a window even on another VAS instance but since its not a critical
  136. * path, just minimize the time we hold the mutex for now. We can add
  137. * a per-instance mutex later if necessary.
  138. */
  139. static void unmap_winctx_mmio_bars(struct vas_window *window)
  140. {
  141. int len;
  142. void *uwc_map;
  143. void *hvwc_map;
  144. u64 busaddr_start;
  145. mutex_lock(&vas_mutex);
  146. hvwc_map = window->hvwc_map;
  147. window->hvwc_map = NULL;
  148. uwc_map = window->uwc_map;
  149. window->uwc_map = NULL;
  150. mutex_unlock(&vas_mutex);
  151. if (hvwc_map) {
  152. get_hvwc_mmio_bar(window, &busaddr_start, &len);
  153. unmap_region(hvwc_map, busaddr_start, len);
  154. }
  155. if (uwc_map) {
  156. get_uwc_mmio_bar(window, &busaddr_start, &len);
  157. unmap_region(uwc_map, busaddr_start, len);
  158. }
  159. }
  160. /*
  161. * Find the Hypervisor Window Context (HVWC) MMIO Base Address Region and the
  162. * OS/User Window Context (UWC) MMIO Base Address Region for the given window.
  163. * Map these bus addresses and save the mapped kernel addresses in @window.
  164. */
  165. int map_winctx_mmio_bars(struct vas_window *window)
  166. {
  167. int len;
  168. u64 start;
  169. get_hvwc_mmio_bar(window, &start, &len);
  170. window->hvwc_map = map_mmio_region("HVWCM_Window", start, len);
  171. get_uwc_mmio_bar(window, &start, &len);
  172. window->uwc_map = map_mmio_region("UWCM_Window", start, len);
  173. if (!window->hvwc_map || !window->uwc_map) {
  174. unmap_winctx_mmio_bars(window);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * Reset all valid registers in the HV and OS/User Window Contexts for
  181. * the window identified by @window.
  182. *
  183. * NOTE: We cannot really use a for loop to reset window context. Not all
  184. * offsets in a window context are valid registers and the valid
  185. * registers are not sequential. And, we can only write to offsets
  186. * with valid registers.
  187. */
  188. void reset_window_regs(struct vas_window *window)
  189. {
  190. write_hvwc_reg(window, VREG(LPID), 0ULL);
  191. write_hvwc_reg(window, VREG(PID), 0ULL);
  192. write_hvwc_reg(window, VREG(XLATE_MSR), 0ULL);
  193. write_hvwc_reg(window, VREG(XLATE_LPCR), 0ULL);
  194. write_hvwc_reg(window, VREG(XLATE_CTL), 0ULL);
  195. write_hvwc_reg(window, VREG(AMR), 0ULL);
  196. write_hvwc_reg(window, VREG(SEIDR), 0ULL);
  197. write_hvwc_reg(window, VREG(FAULT_TX_WIN), 0ULL);
  198. write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
  199. write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), 0ULL);
  200. write_hvwc_reg(window, VREG(PSWID), 0ULL);
  201. write_hvwc_reg(window, VREG(LFIFO_BAR), 0ULL);
  202. write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), 0ULL);
  203. write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), 0ULL);
  204. write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
  205. write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
  206. write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
  207. write_hvwc_reg(window, VREG(LRX_WCRED), 0ULL);
  208. write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
  209. write_hvwc_reg(window, VREG(TX_WCRED), 0ULL);
  210. write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
  211. write_hvwc_reg(window, VREG(LFIFO_SIZE), 0ULL);
  212. write_hvwc_reg(window, VREG(WINCTL), 0ULL);
  213. write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
  214. write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), 0ULL);
  215. write_hvwc_reg(window, VREG(TX_RSVD_BUF_COUNT), 0ULL);
  216. write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), 0ULL);
  217. write_hvwc_reg(window, VREG(LNOTIFY_CTL), 0ULL);
  218. write_hvwc_reg(window, VREG(LNOTIFY_PID), 0ULL);
  219. write_hvwc_reg(window, VREG(LNOTIFY_LPID), 0ULL);
  220. write_hvwc_reg(window, VREG(LNOTIFY_TID), 0ULL);
  221. write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), 0ULL);
  222. write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
  223. /* Skip read-only registers: NX_UTIL and NX_UTIL_SE */
  224. /*
  225. * The send and receive window credit adder registers are also
  226. * accessible from HVWC and have been initialized above. We don't
  227. * need to initialize from the OS/User Window Context, so skip
  228. * following calls:
  229. *
  230. * write_uwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
  231. * write_uwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
  232. */
  233. }
  234. /*
  235. * Initialize window context registers related to Address Translation.
  236. * These registers are common to send/receive windows although they
  237. * differ for user/kernel windows. As we resolve the TODOs we may
  238. * want to add fields to vas_winctx and move the initialization to
  239. * init_vas_winctx_regs().
  240. */
  241. static void init_xlate_regs(struct vas_window *window, bool user_win)
  242. {
  243. u64 lpcr, val;
  244. /*
  245. * MSR_TA, MSR_US are false for both kernel and user.
  246. * MSR_DR and MSR_PR are false for kernel.
  247. */
  248. val = 0ULL;
  249. val = SET_FIELD(VAS_XLATE_MSR_HV, val, 1);
  250. val = SET_FIELD(VAS_XLATE_MSR_SF, val, 1);
  251. if (user_win) {
  252. val = SET_FIELD(VAS_XLATE_MSR_DR, val, 1);
  253. val = SET_FIELD(VAS_XLATE_MSR_PR, val, 1);
  254. }
  255. write_hvwc_reg(window, VREG(XLATE_MSR), val);
  256. lpcr = mfspr(SPRN_LPCR);
  257. val = 0ULL;
  258. /*
  259. * NOTE: From Section 5.7.8.1 Segment Lookaside Buffer of the
  260. * Power ISA, v3.0B, Page size encoding is 0 = 4KB, 5 = 64KB.
  261. *
  262. * NOTE: From Section 1.3.1, Address Translation Context of the
  263. * Nest MMU Workbook, LPCR_SC should be 0 for Power9.
  264. */
  265. val = SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE, val, 5);
  266. val = SET_FIELD(VAS_XLATE_LPCR_ISL, val, lpcr & LPCR_ISL);
  267. val = SET_FIELD(VAS_XLATE_LPCR_TC, val, lpcr & LPCR_TC);
  268. val = SET_FIELD(VAS_XLATE_LPCR_SC, val, 0);
  269. write_hvwc_reg(window, VREG(XLATE_LPCR), val);
  270. /*
  271. * Section 1.3.1 (Address translation Context) of NMMU workbook.
  272. * 0b00 Hashed Page Table mode
  273. * 0b01 Reserved
  274. * 0b10 Radix on HPT
  275. * 0b11 Radix on Radix
  276. */
  277. val = 0ULL;
  278. val = SET_FIELD(VAS_XLATE_MODE, val, radix_enabled() ? 3 : 2);
  279. write_hvwc_reg(window, VREG(XLATE_CTL), val);
  280. /*
  281. * TODO: Can we mfspr(AMR) even for user windows?
  282. */
  283. val = 0ULL;
  284. val = SET_FIELD(VAS_AMR, val, mfspr(SPRN_AMR));
  285. write_hvwc_reg(window, VREG(AMR), val);
  286. val = 0ULL;
  287. val = SET_FIELD(VAS_SEIDR, val, 0);
  288. write_hvwc_reg(window, VREG(SEIDR), val);
  289. }
  290. /*
  291. * Initialize Reserved Send Buffer Count for the send window. It involves
  292. * writing to the register, reading it back to confirm that the hardware
  293. * has enough buffers to reserve. See section 1.3.1.2.1 of VAS workbook.
  294. *
  295. * Since we can only make a best-effort attempt to fulfill the request,
  296. * we don't return any errors if we cannot.
  297. *
  298. * TODO: Reserved (aka dedicated) send buffers are not supported yet.
  299. */
  300. static void init_rsvd_tx_buf_count(struct vas_window *txwin,
  301. struct vas_winctx *winctx)
  302. {
  303. write_hvwc_reg(txwin, VREG(TX_RSVD_BUF_COUNT), 0ULL);
  304. }
  305. /*
  306. * init_winctx_regs()
  307. * Initialize window context registers for a receive window.
  308. * Except for caching control and marking window open, the registers
  309. * are initialized in the order listed in Section 3.1.4 (Window Context
  310. * Cache Register Details) of the VAS workbook although they don't need
  311. * to be.
  312. *
  313. * Design note: For NX receive windows, NX allocates the FIFO buffer in OPAL
  314. * (so that it can get a large contiguous area) and passes that buffer
  315. * to kernel via device tree. We now write that buffer address to the
  316. * FIFO BAR. Would it make sense to do this all in OPAL? i.e have OPAL
  317. * write the per-chip RX FIFO addresses to the windows during boot-up
  318. * as a one-time task? That could work for NX but what about other
  319. * receivers? Let the receivers tell us the rx-fifo buffers for now.
  320. */
  321. int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
  322. {
  323. u64 val;
  324. int fifo_size;
  325. reset_window_regs(window);
  326. val = 0ULL;
  327. val = SET_FIELD(VAS_LPID, val, winctx->lpid);
  328. write_hvwc_reg(window, VREG(LPID), val);
  329. val = 0ULL;
  330. val = SET_FIELD(VAS_PID_ID, val, winctx->pidr);
  331. write_hvwc_reg(window, VREG(PID), val);
  332. init_xlate_regs(window, winctx->user_win);
  333. val = 0ULL;
  334. val = SET_FIELD(VAS_FAULT_TX_WIN, val, 0);
  335. write_hvwc_reg(window, VREG(FAULT_TX_WIN), val);
  336. /* In PowerNV, interrupts go to HV. */
  337. write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
  338. val = 0ULL;
  339. val = SET_FIELD(VAS_HV_INTR_SRC_RA, val, winctx->irq_port);
  340. write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), val);
  341. val = 0ULL;
  342. val = SET_FIELD(VAS_PSWID_EA_HANDLE, val, winctx->pswid);
  343. write_hvwc_reg(window, VREG(PSWID), val);
  344. write_hvwc_reg(window, VREG(SPARE1), 0ULL);
  345. write_hvwc_reg(window, VREG(SPARE2), 0ULL);
  346. write_hvwc_reg(window, VREG(SPARE3), 0ULL);
  347. /*
  348. * NOTE: VAS expects the FIFO address to be copied into the LFIFO_BAR
  349. * register as is - do NOT shift the address into VAS_LFIFO_BAR
  350. * bit fields! Ok to set the page migration select fields -
  351. * VAS ignores the lower 10+ bits in the address anyway, because
  352. * the minimum FIFO size is 1K?
  353. *
  354. * See also: Design note in function header.
  355. */
  356. val = __pa(winctx->rx_fifo);
  357. val = SET_FIELD(VAS_PAGE_MIGRATION_SELECT, val, 0);
  358. write_hvwc_reg(window, VREG(LFIFO_BAR), val);
  359. val = 0ULL;
  360. val = SET_FIELD(VAS_LDATA_STAMP, val, winctx->data_stamp);
  361. write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), val);
  362. val = 0ULL;
  363. val = SET_FIELD(VAS_LDMA_TYPE, val, winctx->dma_type);
  364. val = SET_FIELD(VAS_LDMA_FIFO_DISABLE, val, winctx->fifo_disable);
  365. write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), val);
  366. write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
  367. write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
  368. write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
  369. val = 0ULL;
  370. val = SET_FIELD(VAS_LRX_WCRED, val, winctx->wcreds_max);
  371. write_hvwc_reg(window, VREG(LRX_WCRED), val);
  372. val = 0ULL;
  373. val = SET_FIELD(VAS_TX_WCRED, val, winctx->wcreds_max);
  374. write_hvwc_reg(window, VREG(TX_WCRED), val);
  375. write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
  376. write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
  377. fifo_size = winctx->rx_fifo_size / 1024;
  378. val = 0ULL;
  379. val = SET_FIELD(VAS_LFIFO_SIZE, val, ilog2(fifo_size));
  380. write_hvwc_reg(window, VREG(LFIFO_SIZE), val);
  381. /* Update window control and caching control registers last so
  382. * we mark the window open only after fully initializing it and
  383. * pushing context to cache.
  384. */
  385. write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
  386. init_rsvd_tx_buf_count(window, winctx);
  387. /* for a send window, point to the matching receive window */
  388. val = 0ULL;
  389. val = SET_FIELD(VAS_LRX_WIN_ID, val, winctx->rx_win_id);
  390. write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), val);
  391. write_hvwc_reg(window, VREG(SPARE4), 0ULL);
  392. val = 0ULL;
  393. val = SET_FIELD(VAS_NOTIFY_DISABLE, val, winctx->notify_disable);
  394. val = SET_FIELD(VAS_INTR_DISABLE, val, winctx->intr_disable);
  395. val = SET_FIELD(VAS_NOTIFY_EARLY, val, winctx->notify_early);
  396. val = SET_FIELD(VAS_NOTIFY_OSU_INTR, val, winctx->notify_os_intr_reg);
  397. write_hvwc_reg(window, VREG(LNOTIFY_CTL), val);
  398. val = 0ULL;
  399. val = SET_FIELD(VAS_LNOTIFY_PID, val, winctx->lnotify_pid);
  400. write_hvwc_reg(window, VREG(LNOTIFY_PID), val);
  401. val = 0ULL;
  402. val = SET_FIELD(VAS_LNOTIFY_LPID, val, winctx->lnotify_lpid);
  403. write_hvwc_reg(window, VREG(LNOTIFY_LPID), val);
  404. val = 0ULL;
  405. val = SET_FIELD(VAS_LNOTIFY_TID, val, winctx->lnotify_tid);
  406. write_hvwc_reg(window, VREG(LNOTIFY_TID), val);
  407. val = 0ULL;
  408. val = SET_FIELD(VAS_LNOTIFY_MIN_SCOPE, val, winctx->min_scope);
  409. val = SET_FIELD(VAS_LNOTIFY_MAX_SCOPE, val, winctx->max_scope);
  410. write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), val);
  411. /* Skip read-only registers NX_UTIL and NX_UTIL_SE */
  412. write_hvwc_reg(window, VREG(SPARE5), 0ULL);
  413. write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
  414. write_hvwc_reg(window, VREG(SPARE6), 0ULL);
  415. /* Finally, push window context to memory and... */
  416. val = 0ULL;
  417. val = SET_FIELD(VAS_PUSH_TO_MEM, val, 1);
  418. write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), val);
  419. /* ... mark the window open for business */
  420. val = 0ULL;
  421. val = SET_FIELD(VAS_WINCTL_REJ_NO_CREDIT, val, winctx->rej_no_credit);
  422. val = SET_FIELD(VAS_WINCTL_PIN, val, winctx->pin_win);
  423. val = SET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val, winctx->tx_wcred_mode);
  424. val = SET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val, winctx->rx_wcred_mode);
  425. val = SET_FIELD(VAS_WINCTL_TX_WORD_MODE, val, winctx->tx_word_mode);
  426. val = SET_FIELD(VAS_WINCTL_RX_WORD_MODE, val, winctx->rx_word_mode);
  427. val = SET_FIELD(VAS_WINCTL_FAULT_WIN, val, winctx->fault_win);
  428. val = SET_FIELD(VAS_WINCTL_NX_WIN, val, winctx->nx_win);
  429. val = SET_FIELD(VAS_WINCTL_OPEN, val, 1);
  430. write_hvwc_reg(window, VREG(WINCTL), val);
  431. return 0;
  432. }
  433. static DEFINE_SPINLOCK(vas_ida_lock);
  434. static void vas_release_window_id(struct ida *ida, int winid)
  435. {
  436. spin_lock(&vas_ida_lock);
  437. ida_remove(ida, winid);
  438. spin_unlock(&vas_ida_lock);
  439. }
  440. static int vas_assign_window_id(struct ida *ida)
  441. {
  442. int rc, winid;
  443. do {
  444. rc = ida_pre_get(ida, GFP_KERNEL);
  445. if (!rc)
  446. return -EAGAIN;
  447. spin_lock(&vas_ida_lock);
  448. rc = ida_get_new(ida, &winid);
  449. spin_unlock(&vas_ida_lock);
  450. } while (rc == -EAGAIN);
  451. if (rc)
  452. return rc;
  453. if (winid > VAS_WINDOWS_PER_CHIP) {
  454. pr_err("Too many (%d) open windows\n", winid);
  455. vas_release_window_id(ida, winid);
  456. return -EAGAIN;
  457. }
  458. return winid;
  459. }
  460. static void vas_window_free(struct vas_window *window)
  461. {
  462. int winid = window->winid;
  463. struct vas_instance *vinst = window->vinst;
  464. unmap_winctx_mmio_bars(window);
  465. vas_window_free_dbgdir(window);
  466. kfree(window);
  467. vas_release_window_id(&vinst->ida, winid);
  468. }
  469. static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
  470. {
  471. int winid;
  472. struct vas_window *window;
  473. winid = vas_assign_window_id(&vinst->ida);
  474. if (winid < 0)
  475. return ERR_PTR(winid);
  476. window = kzalloc(sizeof(*window), GFP_KERNEL);
  477. if (!window)
  478. goto out_free;
  479. window->vinst = vinst;
  480. window->winid = winid;
  481. if (map_winctx_mmio_bars(window))
  482. goto out_free;
  483. vas_window_init_dbgdir(window);
  484. return window;
  485. out_free:
  486. kfree(window);
  487. vas_release_window_id(&vinst->ida, winid);
  488. return ERR_PTR(-ENOMEM);
  489. }
  490. static void put_rx_win(struct vas_window *rxwin)
  491. {
  492. /* Better not be a send window! */
  493. WARN_ON_ONCE(rxwin->tx_win);
  494. atomic_dec(&rxwin->num_txwins);
  495. }
  496. /*
  497. * Find the user space receive window given the @pswid.
  498. * - We must have a valid vasid and it must belong to this instance.
  499. * (so both send and receive windows are on the same VAS instance)
  500. * - The window must refer to an OPEN, FTW, RECEIVE window.
  501. *
  502. * NOTE: We access ->windows[] table and assume that vinst->mutex is held.
  503. */
  504. static struct vas_window *get_user_rxwin(struct vas_instance *vinst, u32 pswid)
  505. {
  506. int vasid, winid;
  507. struct vas_window *rxwin;
  508. decode_pswid(pswid, &vasid, &winid);
  509. if (vinst->vas_id != vasid)
  510. return ERR_PTR(-EINVAL);
  511. rxwin = vinst->windows[winid];
  512. if (!rxwin || rxwin->tx_win || rxwin->cop != VAS_COP_TYPE_FTW)
  513. return ERR_PTR(-EINVAL);
  514. return rxwin;
  515. }
  516. /*
  517. * Get the VAS receive window associated with NX engine identified
  518. * by @cop and if applicable, @pswid.
  519. *
  520. * See also function header of set_vinst_win().
  521. */
  522. static struct vas_window *get_vinst_rxwin(struct vas_instance *vinst,
  523. enum vas_cop_type cop, u32 pswid)
  524. {
  525. struct vas_window *rxwin;
  526. mutex_lock(&vinst->mutex);
  527. if (cop == VAS_COP_TYPE_FTW)
  528. rxwin = get_user_rxwin(vinst, pswid);
  529. else
  530. rxwin = vinst->rxwin[cop] ?: ERR_PTR(-EINVAL);
  531. if (!IS_ERR(rxwin))
  532. atomic_inc(&rxwin->num_txwins);
  533. mutex_unlock(&vinst->mutex);
  534. return rxwin;
  535. }
  536. /*
  537. * We have two tables of windows in a VAS instance. The first one,
  538. * ->windows[], contains all the windows in the instance and allows
  539. * looking up a window by its id. It is used to look up send windows
  540. * during fault handling and receive windows when pairing user space
  541. * send/receive windows.
  542. *
  543. * The second table, ->rxwin[], contains receive windows that are
  544. * associated with NX engines. This table has VAS_COP_TYPE_MAX
  545. * entries and is used to look up a receive window by its
  546. * coprocessor type.
  547. *
  548. * Here, we save @window in the ->windows[] table. If it is a receive
  549. * window, we also save the window in the ->rxwin[] table.
  550. */
  551. static void set_vinst_win(struct vas_instance *vinst,
  552. struct vas_window *window)
  553. {
  554. int id = window->winid;
  555. mutex_lock(&vinst->mutex);
  556. /*
  557. * There should only be one receive window for a coprocessor type
  558. * unless its a user (FTW) window.
  559. */
  560. if (!window->user_win && !window->tx_win) {
  561. WARN_ON_ONCE(vinst->rxwin[window->cop]);
  562. vinst->rxwin[window->cop] = window;
  563. }
  564. WARN_ON_ONCE(vinst->windows[id] != NULL);
  565. vinst->windows[id] = window;
  566. mutex_unlock(&vinst->mutex);
  567. }
  568. /*
  569. * Clear this window from the table(s) of windows for this VAS instance.
  570. * See also function header of set_vinst_win().
  571. */
  572. static void clear_vinst_win(struct vas_window *window)
  573. {
  574. int id = window->winid;
  575. struct vas_instance *vinst = window->vinst;
  576. mutex_lock(&vinst->mutex);
  577. if (!window->user_win && !window->tx_win) {
  578. WARN_ON_ONCE(!vinst->rxwin[window->cop]);
  579. vinst->rxwin[window->cop] = NULL;
  580. }
  581. WARN_ON_ONCE(vinst->windows[id] != window);
  582. vinst->windows[id] = NULL;
  583. mutex_unlock(&vinst->mutex);
  584. }
  585. static void init_winctx_for_rxwin(struct vas_window *rxwin,
  586. struct vas_rx_win_attr *rxattr,
  587. struct vas_winctx *winctx)
  588. {
  589. /*
  590. * We first zero (memset()) all fields and only set non-zero fields.
  591. * Following fields are 0/false but maybe deserve a comment:
  592. *
  593. * ->notify_os_intr_reg In powerNV, send intrs to HV
  594. * ->notify_disable False for NX windows
  595. * ->intr_disable False for Fault Windows
  596. * ->xtra_write False for NX windows
  597. * ->notify_early NA for NX windows
  598. * ->rsvd_txbuf_count NA for Rx windows
  599. * ->lpid, ->pid, ->tid NA for Rx windows
  600. */
  601. memset(winctx, 0, sizeof(struct vas_winctx));
  602. winctx->rx_fifo = rxattr->rx_fifo;
  603. winctx->rx_fifo_size = rxattr->rx_fifo_size;
  604. winctx->wcreds_max = rxwin->wcreds_max;
  605. winctx->pin_win = rxattr->pin_win;
  606. winctx->nx_win = rxattr->nx_win;
  607. winctx->fault_win = rxattr->fault_win;
  608. winctx->user_win = rxattr->user_win;
  609. winctx->rej_no_credit = rxattr->rej_no_credit;
  610. winctx->rx_word_mode = rxattr->rx_win_ord_mode;
  611. winctx->tx_word_mode = rxattr->tx_win_ord_mode;
  612. winctx->rx_wcred_mode = rxattr->rx_wcred_mode;
  613. winctx->tx_wcred_mode = rxattr->tx_wcred_mode;
  614. winctx->notify_early = rxattr->notify_early;
  615. if (winctx->nx_win) {
  616. winctx->data_stamp = true;
  617. winctx->intr_disable = true;
  618. winctx->pin_win = true;
  619. WARN_ON_ONCE(winctx->fault_win);
  620. WARN_ON_ONCE(!winctx->rx_word_mode);
  621. WARN_ON_ONCE(!winctx->tx_word_mode);
  622. WARN_ON_ONCE(winctx->notify_after_count);
  623. } else if (winctx->fault_win) {
  624. winctx->notify_disable = true;
  625. } else if (winctx->user_win) {
  626. /*
  627. * Section 1.8.1 Low Latency Core-Core Wake up of
  628. * the VAS workbook:
  629. *
  630. * - disable credit checks ([tr]x_wcred_mode = false)
  631. * - disable FIFO writes
  632. * - enable ASB_Notify, disable interrupt
  633. */
  634. winctx->fifo_disable = true;
  635. winctx->intr_disable = true;
  636. winctx->rx_fifo = NULL;
  637. }
  638. winctx->lnotify_lpid = rxattr->lnotify_lpid;
  639. winctx->lnotify_pid = rxattr->lnotify_pid;
  640. winctx->lnotify_tid = rxattr->lnotify_tid;
  641. winctx->pswid = rxattr->pswid;
  642. winctx->dma_type = VAS_DMA_TYPE_INJECT;
  643. winctx->tc_mode = rxattr->tc_mode;
  644. winctx->min_scope = VAS_SCOPE_LOCAL;
  645. winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
  646. }
  647. static bool rx_win_args_valid(enum vas_cop_type cop,
  648. struct vas_rx_win_attr *attr)
  649. {
  650. pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
  651. attr->fault_win, attr->notify_disable,
  652. attr->intr_disable, attr->notify_early,
  653. attr->rx_fifo_size);
  654. if (cop >= VAS_COP_TYPE_MAX)
  655. return false;
  656. if (cop != VAS_COP_TYPE_FTW &&
  657. attr->rx_fifo_size < VAS_RX_FIFO_SIZE_MIN)
  658. return false;
  659. if (attr->rx_fifo_size > VAS_RX_FIFO_SIZE_MAX)
  660. return false;
  661. if (attr->wcreds_max > VAS_RX_WCREDS_MAX)
  662. return false;
  663. if (attr->nx_win) {
  664. /* cannot be fault or user window if it is nx */
  665. if (attr->fault_win || attr->user_win)
  666. return false;
  667. /*
  668. * Section 3.1.4.32: NX Windows must not disable notification,
  669. * and must not enable interrupts or early notification.
  670. */
  671. if (attr->notify_disable || !attr->intr_disable ||
  672. attr->notify_early)
  673. return false;
  674. } else if (attr->fault_win) {
  675. /* cannot be both fault and user window */
  676. if (attr->user_win)
  677. return false;
  678. /*
  679. * Section 3.1.4.32: Fault windows must disable notification
  680. * but not interrupts.
  681. */
  682. if (!attr->notify_disable || attr->intr_disable)
  683. return false;
  684. } else if (attr->user_win) {
  685. /*
  686. * User receive windows are only for fast-thread-wakeup
  687. * (FTW). They don't need a FIFO and must disable interrupts
  688. */
  689. if (attr->rx_fifo || attr->rx_fifo_size || !attr->intr_disable)
  690. return false;
  691. } else {
  692. /* Rx window must be one of NX or Fault or User window. */
  693. return false;
  694. }
  695. return true;
  696. }
  697. void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop)
  698. {
  699. memset(rxattr, 0, sizeof(*rxattr));
  700. if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
  701. rxattr->pin_win = true;
  702. rxattr->nx_win = true;
  703. rxattr->fault_win = false;
  704. rxattr->intr_disable = true;
  705. rxattr->rx_wcred_mode = true;
  706. rxattr->tx_wcred_mode = true;
  707. rxattr->rx_win_ord_mode = true;
  708. rxattr->tx_win_ord_mode = true;
  709. } else if (cop == VAS_COP_TYPE_FAULT) {
  710. rxattr->pin_win = true;
  711. rxattr->fault_win = true;
  712. rxattr->notify_disable = true;
  713. rxattr->rx_wcred_mode = true;
  714. rxattr->tx_wcred_mode = true;
  715. rxattr->rx_win_ord_mode = true;
  716. rxattr->tx_win_ord_mode = true;
  717. } else if (cop == VAS_COP_TYPE_FTW) {
  718. rxattr->user_win = true;
  719. rxattr->intr_disable = true;
  720. /*
  721. * As noted in the VAS Workbook we disable credit checks.
  722. * If we enable credit checks in the future, we must also
  723. * implement a mechanism to return the user credits or new
  724. * paste operations will fail.
  725. */
  726. }
  727. }
  728. EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
  729. struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
  730. struct vas_rx_win_attr *rxattr)
  731. {
  732. struct vas_window *rxwin;
  733. struct vas_winctx winctx;
  734. struct vas_instance *vinst;
  735. trace_vas_rx_win_open(current, vasid, cop, rxattr);
  736. if (!rx_win_args_valid(cop, rxattr))
  737. return ERR_PTR(-EINVAL);
  738. vinst = find_vas_instance(vasid);
  739. if (!vinst) {
  740. pr_devel("vasid %d not found!\n", vasid);
  741. return ERR_PTR(-EINVAL);
  742. }
  743. pr_devel("Found instance %d\n", vasid);
  744. rxwin = vas_window_alloc(vinst);
  745. if (IS_ERR(rxwin)) {
  746. pr_devel("Unable to allocate memory for Rx window\n");
  747. return rxwin;
  748. }
  749. rxwin->tx_win = false;
  750. rxwin->nx_win = rxattr->nx_win;
  751. rxwin->user_win = rxattr->user_win;
  752. rxwin->cop = cop;
  753. rxwin->wcreds_max = rxattr->wcreds_max ?: VAS_WCREDS_DEFAULT;
  754. if (rxattr->user_win)
  755. rxwin->pid = task_pid_vnr(current);
  756. init_winctx_for_rxwin(rxwin, rxattr, &winctx);
  757. init_winctx_regs(rxwin, &winctx);
  758. set_vinst_win(vinst, rxwin);
  759. return rxwin;
  760. }
  761. EXPORT_SYMBOL_GPL(vas_rx_win_open);
  762. void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
  763. {
  764. memset(txattr, 0, sizeof(*txattr));
  765. if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
  766. txattr->rej_no_credit = false;
  767. txattr->rx_wcred_mode = true;
  768. txattr->tx_wcred_mode = true;
  769. txattr->rx_win_ord_mode = true;
  770. txattr->tx_win_ord_mode = true;
  771. } else if (cop == VAS_COP_TYPE_FTW) {
  772. txattr->user_win = true;
  773. }
  774. }
  775. EXPORT_SYMBOL_GPL(vas_init_tx_win_attr);
  776. static void init_winctx_for_txwin(struct vas_window *txwin,
  777. struct vas_tx_win_attr *txattr,
  778. struct vas_winctx *winctx)
  779. {
  780. /*
  781. * We first zero all fields and only set non-zero ones. Following
  782. * are some fields set to 0/false for the stated reason:
  783. *
  784. * ->notify_os_intr_reg In powernv, send intrs to HV
  785. * ->rsvd_txbuf_count Not supported yet.
  786. * ->notify_disable False for NX windows
  787. * ->xtra_write False for NX windows
  788. * ->notify_early NA for NX windows
  789. * ->lnotify_lpid NA for Tx windows
  790. * ->lnotify_pid NA for Tx windows
  791. * ->lnotify_tid NA for Tx windows
  792. * ->tx_win_cred_mode Ignore for now for NX windows
  793. * ->rx_win_cred_mode Ignore for now for NX windows
  794. */
  795. memset(winctx, 0, sizeof(struct vas_winctx));
  796. winctx->wcreds_max = txwin->wcreds_max;
  797. winctx->user_win = txattr->user_win;
  798. winctx->nx_win = txwin->rxwin->nx_win;
  799. winctx->pin_win = txattr->pin_win;
  800. winctx->rej_no_credit = txattr->rej_no_credit;
  801. winctx->rsvd_txbuf_enable = txattr->rsvd_txbuf_enable;
  802. winctx->rx_wcred_mode = txattr->rx_wcred_mode;
  803. winctx->tx_wcred_mode = txattr->tx_wcred_mode;
  804. winctx->rx_word_mode = txattr->rx_win_ord_mode;
  805. winctx->tx_word_mode = txattr->tx_win_ord_mode;
  806. winctx->rsvd_txbuf_count = txattr->rsvd_txbuf_count;
  807. winctx->intr_disable = true;
  808. if (winctx->nx_win)
  809. winctx->data_stamp = true;
  810. winctx->lpid = txattr->lpid;
  811. winctx->pidr = txattr->pidr;
  812. winctx->rx_win_id = txwin->rxwin->winid;
  813. winctx->dma_type = VAS_DMA_TYPE_INJECT;
  814. winctx->tc_mode = txattr->tc_mode;
  815. winctx->min_scope = VAS_SCOPE_LOCAL;
  816. winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
  817. winctx->pswid = 0;
  818. }
  819. static bool tx_win_args_valid(enum vas_cop_type cop,
  820. struct vas_tx_win_attr *attr)
  821. {
  822. if (attr->tc_mode != VAS_THRESH_DISABLED)
  823. return false;
  824. if (cop > VAS_COP_TYPE_MAX)
  825. return false;
  826. if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
  827. return false;
  828. if (attr->user_win &&
  829. (cop != VAS_COP_TYPE_FTW || attr->rsvd_txbuf_count))
  830. return false;
  831. return true;
  832. }
  833. struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
  834. struct vas_tx_win_attr *attr)
  835. {
  836. int rc;
  837. struct vas_window *txwin;
  838. struct vas_window *rxwin;
  839. struct vas_winctx winctx;
  840. struct vas_instance *vinst;
  841. trace_vas_tx_win_open(current, vasid, cop, attr);
  842. if (!tx_win_args_valid(cop, attr))
  843. return ERR_PTR(-EINVAL);
  844. /*
  845. * If caller did not specify a vasid but specified the PSWID of a
  846. * receive window (applicable only to FTW windows), use the vasid
  847. * from that receive window.
  848. */
  849. if (vasid == -1 && attr->pswid)
  850. decode_pswid(attr->pswid, &vasid, NULL);
  851. vinst = find_vas_instance(vasid);
  852. if (!vinst) {
  853. pr_devel("vasid %d not found!\n", vasid);
  854. return ERR_PTR(-EINVAL);
  855. }
  856. rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
  857. if (IS_ERR(rxwin)) {
  858. pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
  859. return rxwin;
  860. }
  861. txwin = vas_window_alloc(vinst);
  862. if (IS_ERR(txwin)) {
  863. rc = PTR_ERR(txwin);
  864. goto put_rxwin;
  865. }
  866. txwin->cop = cop;
  867. txwin->tx_win = 1;
  868. txwin->rxwin = rxwin;
  869. txwin->nx_win = txwin->rxwin->nx_win;
  870. txwin->pid = attr->pid;
  871. txwin->user_win = attr->user_win;
  872. txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
  873. init_winctx_for_txwin(txwin, attr, &winctx);
  874. init_winctx_regs(txwin, &winctx);
  875. /*
  876. * If its a kernel send window, map the window address into the
  877. * kernel's address space. For user windows, user must issue an
  878. * mmap() to map the window into their address space.
  879. *
  880. * NOTE: If kernel ever resubmits a user CRB after handling a page
  881. * fault, we will need to map this into kernel as well.
  882. */
  883. if (!txwin->user_win) {
  884. txwin->paste_kaddr = map_paste_region(txwin);
  885. if (IS_ERR(txwin->paste_kaddr)) {
  886. rc = PTR_ERR(txwin->paste_kaddr);
  887. goto free_window;
  888. }
  889. } else {
  890. /*
  891. * A user mapping must ensure that context switch issues
  892. * CP_ABORT for this thread.
  893. */
  894. rc = set_thread_uses_vas();
  895. if (rc)
  896. goto free_window;
  897. }
  898. set_vinst_win(vinst, txwin);
  899. return txwin;
  900. free_window:
  901. vas_window_free(txwin);
  902. put_rxwin:
  903. put_rx_win(rxwin);
  904. return ERR_PTR(rc);
  905. }
  906. EXPORT_SYMBOL_GPL(vas_tx_win_open);
  907. int vas_copy_crb(void *crb, int offset)
  908. {
  909. return vas_copy(crb, offset);
  910. }
  911. EXPORT_SYMBOL_GPL(vas_copy_crb);
  912. #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
  913. int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
  914. {
  915. int rc;
  916. void *addr;
  917. uint64_t val;
  918. trace_vas_paste_crb(current, txwin);
  919. /*
  920. * Only NX windows are supported for now and hardware assumes
  921. * report-enable flag is set for NX windows. Ensure software
  922. * complies too.
  923. */
  924. WARN_ON_ONCE(txwin->nx_win && !re);
  925. addr = txwin->paste_kaddr;
  926. if (re) {
  927. /*
  928. * Set the REPORT_ENABLE bit (equivalent to writing
  929. * to 1K offset of the paste address)
  930. */
  931. val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
  932. addr += val;
  933. }
  934. /*
  935. * Map the raw CR value from vas_paste() to an error code (there
  936. * is just pass or fail for now though).
  937. */
  938. rc = vas_paste(addr, offset);
  939. if (rc == 2)
  940. rc = 0;
  941. else
  942. rc = -EINVAL;
  943. pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
  944. read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
  945. return rc;
  946. }
  947. EXPORT_SYMBOL_GPL(vas_paste_crb);
  948. /*
  949. * If credit checking is enabled for this window, poll for the return
  950. * of window credits (i.e for NX engines to process any outstanding CRBs).
  951. * Since NX-842 waits for the CRBs to be processed before closing the
  952. * window, we should not have to wait for too long.
  953. *
  954. * TODO: We retry in 10ms intervals now. We could/should probably peek at
  955. * the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
  956. * CRBs on the FIFO and compute the delay dynamically on each retry.
  957. * But that is not really needed until we support NX-GZIP access from
  958. * user space. (NX-842 driver waits for CSB and Fast thread-wakeup
  959. * doesn't use credit checking).
  960. */
  961. static void poll_window_credits(struct vas_window *window)
  962. {
  963. u64 val;
  964. int creds, mode;
  965. val = read_hvwc_reg(window, VREG(WINCTL));
  966. if (window->tx_win)
  967. mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
  968. else
  969. mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
  970. if (!mode)
  971. return;
  972. retry:
  973. if (window->tx_win) {
  974. val = read_hvwc_reg(window, VREG(TX_WCRED));
  975. creds = GET_FIELD(VAS_TX_WCRED, val);
  976. } else {
  977. val = read_hvwc_reg(window, VREG(LRX_WCRED));
  978. creds = GET_FIELD(VAS_LRX_WCRED, val);
  979. }
  980. if (creds < window->wcreds_max) {
  981. val = 0;
  982. set_current_state(TASK_UNINTERRUPTIBLE);
  983. schedule_timeout(msecs_to_jiffies(10));
  984. goto retry;
  985. }
  986. }
  987. /*
  988. * Wait for the window to go to "not-busy" state. It should only take a
  989. * short time to queue a CRB, so window should not be busy for too long.
  990. * Trying 5ms intervals.
  991. */
  992. static void poll_window_busy_state(struct vas_window *window)
  993. {
  994. int busy;
  995. u64 val;
  996. retry:
  997. val = read_hvwc_reg(window, VREG(WIN_STATUS));
  998. busy = GET_FIELD(VAS_WIN_BUSY, val);
  999. if (busy) {
  1000. val = 0;
  1001. set_current_state(TASK_UNINTERRUPTIBLE);
  1002. schedule_timeout(msecs_to_jiffies(5));
  1003. goto retry;
  1004. }
  1005. }
  1006. /*
  1007. * Have the hardware cast a window out of cache and wait for it to
  1008. * be completed.
  1009. *
  1010. * NOTE: It can take a relatively long time to cast the window context
  1011. * out of the cache. It is not strictly necessary to cast out if:
  1012. *
  1013. * - we clear the "Pin Window" bit (so hardware is free to evict)
  1014. *
  1015. * - we re-initialize the window context when it is reassigned.
  1016. *
  1017. * We do the former in vas_win_close() and latter in vas_win_open().
  1018. * So, ignoring the cast-out for now. We can add it as needed. If
  1019. * casting out becomes necessary we should consider offloading the
  1020. * job to a worker thread, so the window close can proceed quickly.
  1021. */
  1022. static void poll_window_castout(struct vas_window *window)
  1023. {
  1024. /* stub for now */
  1025. }
  1026. /*
  1027. * Unpin and close a window so no new requests are accepted and the
  1028. * hardware can evict this window from cache if necessary.
  1029. */
  1030. static void unpin_close_window(struct vas_window *window)
  1031. {
  1032. u64 val;
  1033. val = read_hvwc_reg(window, VREG(WINCTL));
  1034. val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
  1035. val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
  1036. write_hvwc_reg(window, VREG(WINCTL), val);
  1037. }
  1038. /*
  1039. * Close a window.
  1040. *
  1041. * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
  1042. * - Disable new paste operations (unmap paste address)
  1043. * - Poll for the "Window Busy" bit to be cleared
  1044. * - Clear the Open/Enable bit for the Window.
  1045. * - Poll for return of window Credits (implies FIFO empty for Rx win?)
  1046. * - Unpin and cast window context out of cache
  1047. *
  1048. * Besides the hardware, kernel has some bookkeeping of course.
  1049. */
  1050. int vas_win_close(struct vas_window *window)
  1051. {
  1052. if (!window)
  1053. return 0;
  1054. if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
  1055. pr_devel("Attempting to close an active Rx window!\n");
  1056. WARN_ON_ONCE(1);
  1057. return -EBUSY;
  1058. }
  1059. unmap_paste_region(window);
  1060. clear_vinst_win(window);
  1061. poll_window_busy_state(window);
  1062. unpin_close_window(window);
  1063. poll_window_credits(window);
  1064. poll_window_castout(window);
  1065. /* if send window, drop reference to matching receive window */
  1066. if (window->tx_win)
  1067. put_rx_win(window->rxwin);
  1068. vas_window_free(window);
  1069. return 0;
  1070. }
  1071. EXPORT_SYMBOL_GPL(vas_win_close);
  1072. /*
  1073. * Return a system-wide unique window id for the window @win.
  1074. */
  1075. u32 vas_win_id(struct vas_window *win)
  1076. {
  1077. return encode_pswid(win->vinst->vas_id, win->winid);
  1078. }
  1079. EXPORT_SYMBOL_GPL(vas_win_id);