vas-window.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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 void vas_release_window_id(struct ida *ida, int winid)
  434. {
  435. ida_free(ida, winid);
  436. }
  437. static int vas_assign_window_id(struct ida *ida)
  438. {
  439. int winid = ida_alloc_max(ida, VAS_WINDOWS_PER_CHIP - 1, GFP_KERNEL);
  440. if (winid == -ENOSPC) {
  441. pr_err("Too many (%d) open windows\n", VAS_WINDOWS_PER_CHIP);
  442. return -EAGAIN;
  443. }
  444. return winid;
  445. }
  446. static void vas_window_free(struct vas_window *window)
  447. {
  448. int winid = window->winid;
  449. struct vas_instance *vinst = window->vinst;
  450. unmap_winctx_mmio_bars(window);
  451. vas_window_free_dbgdir(window);
  452. kfree(window);
  453. vas_release_window_id(&vinst->ida, winid);
  454. }
  455. static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
  456. {
  457. int winid;
  458. struct vas_window *window;
  459. winid = vas_assign_window_id(&vinst->ida);
  460. if (winid < 0)
  461. return ERR_PTR(winid);
  462. window = kzalloc(sizeof(*window), GFP_KERNEL);
  463. if (!window)
  464. goto out_free;
  465. window->vinst = vinst;
  466. window->winid = winid;
  467. if (map_winctx_mmio_bars(window))
  468. goto out_free;
  469. vas_window_init_dbgdir(window);
  470. return window;
  471. out_free:
  472. kfree(window);
  473. vas_release_window_id(&vinst->ida, winid);
  474. return ERR_PTR(-ENOMEM);
  475. }
  476. static void put_rx_win(struct vas_window *rxwin)
  477. {
  478. /* Better not be a send window! */
  479. WARN_ON_ONCE(rxwin->tx_win);
  480. atomic_dec(&rxwin->num_txwins);
  481. }
  482. /*
  483. * Find the user space receive window given the @pswid.
  484. * - We must have a valid vasid and it must belong to this instance.
  485. * (so both send and receive windows are on the same VAS instance)
  486. * - The window must refer to an OPEN, FTW, RECEIVE window.
  487. *
  488. * NOTE: We access ->windows[] table and assume that vinst->mutex is held.
  489. */
  490. static struct vas_window *get_user_rxwin(struct vas_instance *vinst, u32 pswid)
  491. {
  492. int vasid, winid;
  493. struct vas_window *rxwin;
  494. decode_pswid(pswid, &vasid, &winid);
  495. if (vinst->vas_id != vasid)
  496. return ERR_PTR(-EINVAL);
  497. rxwin = vinst->windows[winid];
  498. if (!rxwin || rxwin->tx_win || rxwin->cop != VAS_COP_TYPE_FTW)
  499. return ERR_PTR(-EINVAL);
  500. return rxwin;
  501. }
  502. /*
  503. * Get the VAS receive window associated with NX engine identified
  504. * by @cop and if applicable, @pswid.
  505. *
  506. * See also function header of set_vinst_win().
  507. */
  508. static struct vas_window *get_vinst_rxwin(struct vas_instance *vinst,
  509. enum vas_cop_type cop, u32 pswid)
  510. {
  511. struct vas_window *rxwin;
  512. mutex_lock(&vinst->mutex);
  513. if (cop == VAS_COP_TYPE_FTW)
  514. rxwin = get_user_rxwin(vinst, pswid);
  515. else
  516. rxwin = vinst->rxwin[cop] ?: ERR_PTR(-EINVAL);
  517. if (!IS_ERR(rxwin))
  518. atomic_inc(&rxwin->num_txwins);
  519. mutex_unlock(&vinst->mutex);
  520. return rxwin;
  521. }
  522. /*
  523. * We have two tables of windows in a VAS instance. The first one,
  524. * ->windows[], contains all the windows in the instance and allows
  525. * looking up a window by its id. It is used to look up send windows
  526. * during fault handling and receive windows when pairing user space
  527. * send/receive windows.
  528. *
  529. * The second table, ->rxwin[], contains receive windows that are
  530. * associated with NX engines. This table has VAS_COP_TYPE_MAX
  531. * entries and is used to look up a receive window by its
  532. * coprocessor type.
  533. *
  534. * Here, we save @window in the ->windows[] table. If it is a receive
  535. * window, we also save the window in the ->rxwin[] table.
  536. */
  537. static void set_vinst_win(struct vas_instance *vinst,
  538. struct vas_window *window)
  539. {
  540. int id = window->winid;
  541. mutex_lock(&vinst->mutex);
  542. /*
  543. * There should only be one receive window for a coprocessor type
  544. * unless its a user (FTW) window.
  545. */
  546. if (!window->user_win && !window->tx_win) {
  547. WARN_ON_ONCE(vinst->rxwin[window->cop]);
  548. vinst->rxwin[window->cop] = window;
  549. }
  550. WARN_ON_ONCE(vinst->windows[id] != NULL);
  551. vinst->windows[id] = window;
  552. mutex_unlock(&vinst->mutex);
  553. }
  554. /*
  555. * Clear this window from the table(s) of windows for this VAS instance.
  556. * See also function header of set_vinst_win().
  557. */
  558. static void clear_vinst_win(struct vas_window *window)
  559. {
  560. int id = window->winid;
  561. struct vas_instance *vinst = window->vinst;
  562. mutex_lock(&vinst->mutex);
  563. if (!window->user_win && !window->tx_win) {
  564. WARN_ON_ONCE(!vinst->rxwin[window->cop]);
  565. vinst->rxwin[window->cop] = NULL;
  566. }
  567. WARN_ON_ONCE(vinst->windows[id] != window);
  568. vinst->windows[id] = NULL;
  569. mutex_unlock(&vinst->mutex);
  570. }
  571. static void init_winctx_for_rxwin(struct vas_window *rxwin,
  572. struct vas_rx_win_attr *rxattr,
  573. struct vas_winctx *winctx)
  574. {
  575. /*
  576. * We first zero (memset()) all fields and only set non-zero fields.
  577. * Following fields are 0/false but maybe deserve a comment:
  578. *
  579. * ->notify_os_intr_reg In powerNV, send intrs to HV
  580. * ->notify_disable False for NX windows
  581. * ->intr_disable False for Fault Windows
  582. * ->xtra_write False for NX windows
  583. * ->notify_early NA for NX windows
  584. * ->rsvd_txbuf_count NA for Rx windows
  585. * ->lpid, ->pid, ->tid NA for Rx windows
  586. */
  587. memset(winctx, 0, sizeof(struct vas_winctx));
  588. winctx->rx_fifo = rxattr->rx_fifo;
  589. winctx->rx_fifo_size = rxattr->rx_fifo_size;
  590. winctx->wcreds_max = rxwin->wcreds_max;
  591. winctx->pin_win = rxattr->pin_win;
  592. winctx->nx_win = rxattr->nx_win;
  593. winctx->fault_win = rxattr->fault_win;
  594. winctx->user_win = rxattr->user_win;
  595. winctx->rej_no_credit = rxattr->rej_no_credit;
  596. winctx->rx_word_mode = rxattr->rx_win_ord_mode;
  597. winctx->tx_word_mode = rxattr->tx_win_ord_mode;
  598. winctx->rx_wcred_mode = rxattr->rx_wcred_mode;
  599. winctx->tx_wcred_mode = rxattr->tx_wcred_mode;
  600. winctx->notify_early = rxattr->notify_early;
  601. if (winctx->nx_win) {
  602. winctx->data_stamp = true;
  603. winctx->intr_disable = true;
  604. winctx->pin_win = true;
  605. WARN_ON_ONCE(winctx->fault_win);
  606. WARN_ON_ONCE(!winctx->rx_word_mode);
  607. WARN_ON_ONCE(!winctx->tx_word_mode);
  608. WARN_ON_ONCE(winctx->notify_after_count);
  609. } else if (winctx->fault_win) {
  610. winctx->notify_disable = true;
  611. } else if (winctx->user_win) {
  612. /*
  613. * Section 1.8.1 Low Latency Core-Core Wake up of
  614. * the VAS workbook:
  615. *
  616. * - disable credit checks ([tr]x_wcred_mode = false)
  617. * - disable FIFO writes
  618. * - enable ASB_Notify, disable interrupt
  619. */
  620. winctx->fifo_disable = true;
  621. winctx->intr_disable = true;
  622. winctx->rx_fifo = NULL;
  623. }
  624. winctx->lnotify_lpid = rxattr->lnotify_lpid;
  625. winctx->lnotify_pid = rxattr->lnotify_pid;
  626. winctx->lnotify_tid = rxattr->lnotify_tid;
  627. winctx->pswid = rxattr->pswid;
  628. winctx->dma_type = VAS_DMA_TYPE_INJECT;
  629. winctx->tc_mode = rxattr->tc_mode;
  630. winctx->min_scope = VAS_SCOPE_LOCAL;
  631. winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
  632. }
  633. static bool rx_win_args_valid(enum vas_cop_type cop,
  634. struct vas_rx_win_attr *attr)
  635. {
  636. pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
  637. attr->fault_win, attr->notify_disable,
  638. attr->intr_disable, attr->notify_early,
  639. attr->rx_fifo_size);
  640. if (cop >= VAS_COP_TYPE_MAX)
  641. return false;
  642. if (cop != VAS_COP_TYPE_FTW &&
  643. attr->rx_fifo_size < VAS_RX_FIFO_SIZE_MIN)
  644. return false;
  645. if (attr->rx_fifo_size > VAS_RX_FIFO_SIZE_MAX)
  646. return false;
  647. if (attr->wcreds_max > VAS_RX_WCREDS_MAX)
  648. return false;
  649. if (attr->nx_win) {
  650. /* cannot be fault or user window if it is nx */
  651. if (attr->fault_win || attr->user_win)
  652. return false;
  653. /*
  654. * Section 3.1.4.32: NX Windows must not disable notification,
  655. * and must not enable interrupts or early notification.
  656. */
  657. if (attr->notify_disable || !attr->intr_disable ||
  658. attr->notify_early)
  659. return false;
  660. } else if (attr->fault_win) {
  661. /* cannot be both fault and user window */
  662. if (attr->user_win)
  663. return false;
  664. /*
  665. * Section 3.1.4.32: Fault windows must disable notification
  666. * but not interrupts.
  667. */
  668. if (!attr->notify_disable || attr->intr_disable)
  669. return false;
  670. } else if (attr->user_win) {
  671. /*
  672. * User receive windows are only for fast-thread-wakeup
  673. * (FTW). They don't need a FIFO and must disable interrupts
  674. */
  675. if (attr->rx_fifo || attr->rx_fifo_size || !attr->intr_disable)
  676. return false;
  677. } else {
  678. /* Rx window must be one of NX or Fault or User window. */
  679. return false;
  680. }
  681. return true;
  682. }
  683. void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop)
  684. {
  685. memset(rxattr, 0, sizeof(*rxattr));
  686. if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
  687. rxattr->pin_win = true;
  688. rxattr->nx_win = true;
  689. rxattr->fault_win = false;
  690. rxattr->intr_disable = true;
  691. rxattr->rx_wcred_mode = true;
  692. rxattr->tx_wcred_mode = true;
  693. rxattr->rx_win_ord_mode = true;
  694. rxattr->tx_win_ord_mode = true;
  695. } else if (cop == VAS_COP_TYPE_FAULT) {
  696. rxattr->pin_win = true;
  697. rxattr->fault_win = true;
  698. rxattr->notify_disable = true;
  699. rxattr->rx_wcred_mode = true;
  700. rxattr->tx_wcred_mode = true;
  701. rxattr->rx_win_ord_mode = true;
  702. rxattr->tx_win_ord_mode = true;
  703. } else if (cop == VAS_COP_TYPE_FTW) {
  704. rxattr->user_win = true;
  705. rxattr->intr_disable = true;
  706. /*
  707. * As noted in the VAS Workbook we disable credit checks.
  708. * If we enable credit checks in the future, we must also
  709. * implement a mechanism to return the user credits or new
  710. * paste operations will fail.
  711. */
  712. }
  713. }
  714. EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
  715. struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
  716. struct vas_rx_win_attr *rxattr)
  717. {
  718. struct vas_window *rxwin;
  719. struct vas_winctx winctx;
  720. struct vas_instance *vinst;
  721. trace_vas_rx_win_open(current, vasid, cop, rxattr);
  722. if (!rx_win_args_valid(cop, rxattr))
  723. return ERR_PTR(-EINVAL);
  724. vinst = find_vas_instance(vasid);
  725. if (!vinst) {
  726. pr_devel("vasid %d not found!\n", vasid);
  727. return ERR_PTR(-EINVAL);
  728. }
  729. pr_devel("Found instance %d\n", vasid);
  730. rxwin = vas_window_alloc(vinst);
  731. if (IS_ERR(rxwin)) {
  732. pr_devel("Unable to allocate memory for Rx window\n");
  733. return rxwin;
  734. }
  735. rxwin->tx_win = false;
  736. rxwin->nx_win = rxattr->nx_win;
  737. rxwin->user_win = rxattr->user_win;
  738. rxwin->cop = cop;
  739. rxwin->wcreds_max = rxattr->wcreds_max ?: VAS_WCREDS_DEFAULT;
  740. if (rxattr->user_win)
  741. rxwin->pid = task_pid_vnr(current);
  742. init_winctx_for_rxwin(rxwin, rxattr, &winctx);
  743. init_winctx_regs(rxwin, &winctx);
  744. set_vinst_win(vinst, rxwin);
  745. return rxwin;
  746. }
  747. EXPORT_SYMBOL_GPL(vas_rx_win_open);
  748. void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
  749. {
  750. memset(txattr, 0, sizeof(*txattr));
  751. if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
  752. txattr->rej_no_credit = false;
  753. txattr->rx_wcred_mode = true;
  754. txattr->tx_wcred_mode = true;
  755. txattr->rx_win_ord_mode = true;
  756. txattr->tx_win_ord_mode = true;
  757. } else if (cop == VAS_COP_TYPE_FTW) {
  758. txattr->user_win = true;
  759. }
  760. }
  761. EXPORT_SYMBOL_GPL(vas_init_tx_win_attr);
  762. static void init_winctx_for_txwin(struct vas_window *txwin,
  763. struct vas_tx_win_attr *txattr,
  764. struct vas_winctx *winctx)
  765. {
  766. /*
  767. * We first zero all fields and only set non-zero ones. Following
  768. * are some fields set to 0/false for the stated reason:
  769. *
  770. * ->notify_os_intr_reg In powernv, send intrs to HV
  771. * ->rsvd_txbuf_count Not supported yet.
  772. * ->notify_disable False for NX windows
  773. * ->xtra_write False for NX windows
  774. * ->notify_early NA for NX windows
  775. * ->lnotify_lpid NA for Tx windows
  776. * ->lnotify_pid NA for Tx windows
  777. * ->lnotify_tid NA for Tx windows
  778. * ->tx_win_cred_mode Ignore for now for NX windows
  779. * ->rx_win_cred_mode Ignore for now for NX windows
  780. */
  781. memset(winctx, 0, sizeof(struct vas_winctx));
  782. winctx->wcreds_max = txwin->wcreds_max;
  783. winctx->user_win = txattr->user_win;
  784. winctx->nx_win = txwin->rxwin->nx_win;
  785. winctx->pin_win = txattr->pin_win;
  786. winctx->rej_no_credit = txattr->rej_no_credit;
  787. winctx->rsvd_txbuf_enable = txattr->rsvd_txbuf_enable;
  788. winctx->rx_wcred_mode = txattr->rx_wcred_mode;
  789. winctx->tx_wcred_mode = txattr->tx_wcred_mode;
  790. winctx->rx_word_mode = txattr->rx_win_ord_mode;
  791. winctx->tx_word_mode = txattr->tx_win_ord_mode;
  792. winctx->rsvd_txbuf_count = txattr->rsvd_txbuf_count;
  793. winctx->intr_disable = true;
  794. if (winctx->nx_win)
  795. winctx->data_stamp = true;
  796. winctx->lpid = txattr->lpid;
  797. winctx->pidr = txattr->pidr;
  798. winctx->rx_win_id = txwin->rxwin->winid;
  799. winctx->dma_type = VAS_DMA_TYPE_INJECT;
  800. winctx->tc_mode = txattr->tc_mode;
  801. winctx->min_scope = VAS_SCOPE_LOCAL;
  802. winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
  803. winctx->pswid = 0;
  804. }
  805. static bool tx_win_args_valid(enum vas_cop_type cop,
  806. struct vas_tx_win_attr *attr)
  807. {
  808. if (attr->tc_mode != VAS_THRESH_DISABLED)
  809. return false;
  810. if (cop > VAS_COP_TYPE_MAX)
  811. return false;
  812. if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
  813. return false;
  814. if (attr->user_win &&
  815. (cop != VAS_COP_TYPE_FTW || attr->rsvd_txbuf_count))
  816. return false;
  817. return true;
  818. }
  819. struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
  820. struct vas_tx_win_attr *attr)
  821. {
  822. int rc;
  823. struct vas_window *txwin;
  824. struct vas_window *rxwin;
  825. struct vas_winctx winctx;
  826. struct vas_instance *vinst;
  827. trace_vas_tx_win_open(current, vasid, cop, attr);
  828. if (!tx_win_args_valid(cop, attr))
  829. return ERR_PTR(-EINVAL);
  830. /*
  831. * If caller did not specify a vasid but specified the PSWID of a
  832. * receive window (applicable only to FTW windows), use the vasid
  833. * from that receive window.
  834. */
  835. if (vasid == -1 && attr->pswid)
  836. decode_pswid(attr->pswid, &vasid, NULL);
  837. vinst = find_vas_instance(vasid);
  838. if (!vinst) {
  839. pr_devel("vasid %d not found!\n", vasid);
  840. return ERR_PTR(-EINVAL);
  841. }
  842. rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
  843. if (IS_ERR(rxwin)) {
  844. pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
  845. return rxwin;
  846. }
  847. txwin = vas_window_alloc(vinst);
  848. if (IS_ERR(txwin)) {
  849. rc = PTR_ERR(txwin);
  850. goto put_rxwin;
  851. }
  852. txwin->cop = cop;
  853. txwin->tx_win = 1;
  854. txwin->rxwin = rxwin;
  855. txwin->nx_win = txwin->rxwin->nx_win;
  856. txwin->pid = attr->pid;
  857. txwin->user_win = attr->user_win;
  858. txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
  859. init_winctx_for_txwin(txwin, attr, &winctx);
  860. init_winctx_regs(txwin, &winctx);
  861. /*
  862. * If its a kernel send window, map the window address into the
  863. * kernel's address space. For user windows, user must issue an
  864. * mmap() to map the window into their address space.
  865. *
  866. * NOTE: If kernel ever resubmits a user CRB after handling a page
  867. * fault, we will need to map this into kernel as well.
  868. */
  869. if (!txwin->user_win) {
  870. txwin->paste_kaddr = map_paste_region(txwin);
  871. if (IS_ERR(txwin->paste_kaddr)) {
  872. rc = PTR_ERR(txwin->paste_kaddr);
  873. goto free_window;
  874. }
  875. } else {
  876. /*
  877. * A user mapping must ensure that context switch issues
  878. * CP_ABORT for this thread.
  879. */
  880. rc = set_thread_uses_vas();
  881. if (rc)
  882. goto free_window;
  883. }
  884. set_vinst_win(vinst, txwin);
  885. return txwin;
  886. free_window:
  887. vas_window_free(txwin);
  888. put_rxwin:
  889. put_rx_win(rxwin);
  890. return ERR_PTR(rc);
  891. }
  892. EXPORT_SYMBOL_GPL(vas_tx_win_open);
  893. int vas_copy_crb(void *crb, int offset)
  894. {
  895. return vas_copy(crb, offset);
  896. }
  897. EXPORT_SYMBOL_GPL(vas_copy_crb);
  898. #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
  899. int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
  900. {
  901. int rc;
  902. void *addr;
  903. uint64_t val;
  904. trace_vas_paste_crb(current, txwin);
  905. /*
  906. * Only NX windows are supported for now and hardware assumes
  907. * report-enable flag is set for NX windows. Ensure software
  908. * complies too.
  909. */
  910. WARN_ON_ONCE(txwin->nx_win && !re);
  911. addr = txwin->paste_kaddr;
  912. if (re) {
  913. /*
  914. * Set the REPORT_ENABLE bit (equivalent to writing
  915. * to 1K offset of the paste address)
  916. */
  917. val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
  918. addr += val;
  919. }
  920. /*
  921. * Map the raw CR value from vas_paste() to an error code (there
  922. * is just pass or fail for now though).
  923. */
  924. rc = vas_paste(addr, offset);
  925. if (rc == 2)
  926. rc = 0;
  927. else
  928. rc = -EINVAL;
  929. pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
  930. read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
  931. return rc;
  932. }
  933. EXPORT_SYMBOL_GPL(vas_paste_crb);
  934. /*
  935. * If credit checking is enabled for this window, poll for the return
  936. * of window credits (i.e for NX engines to process any outstanding CRBs).
  937. * Since NX-842 waits for the CRBs to be processed before closing the
  938. * window, we should not have to wait for too long.
  939. *
  940. * TODO: We retry in 10ms intervals now. We could/should probably peek at
  941. * the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
  942. * CRBs on the FIFO and compute the delay dynamically on each retry.
  943. * But that is not really needed until we support NX-GZIP access from
  944. * user space. (NX-842 driver waits for CSB and Fast thread-wakeup
  945. * doesn't use credit checking).
  946. */
  947. static void poll_window_credits(struct vas_window *window)
  948. {
  949. u64 val;
  950. int creds, mode;
  951. val = read_hvwc_reg(window, VREG(WINCTL));
  952. if (window->tx_win)
  953. mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
  954. else
  955. mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
  956. if (!mode)
  957. return;
  958. retry:
  959. if (window->tx_win) {
  960. val = read_hvwc_reg(window, VREG(TX_WCRED));
  961. creds = GET_FIELD(VAS_TX_WCRED, val);
  962. } else {
  963. val = read_hvwc_reg(window, VREG(LRX_WCRED));
  964. creds = GET_FIELD(VAS_LRX_WCRED, val);
  965. }
  966. if (creds < window->wcreds_max) {
  967. val = 0;
  968. set_current_state(TASK_UNINTERRUPTIBLE);
  969. schedule_timeout(msecs_to_jiffies(10));
  970. goto retry;
  971. }
  972. }
  973. /*
  974. * Wait for the window to go to "not-busy" state. It should only take a
  975. * short time to queue a CRB, so window should not be busy for too long.
  976. * Trying 5ms intervals.
  977. */
  978. static void poll_window_busy_state(struct vas_window *window)
  979. {
  980. int busy;
  981. u64 val;
  982. retry:
  983. val = read_hvwc_reg(window, VREG(WIN_STATUS));
  984. busy = GET_FIELD(VAS_WIN_BUSY, val);
  985. if (busy) {
  986. val = 0;
  987. set_current_state(TASK_UNINTERRUPTIBLE);
  988. schedule_timeout(msecs_to_jiffies(5));
  989. goto retry;
  990. }
  991. }
  992. /*
  993. * Have the hardware cast a window out of cache and wait for it to
  994. * be completed.
  995. *
  996. * NOTE: It can take a relatively long time to cast the window context
  997. * out of the cache. It is not strictly necessary to cast out if:
  998. *
  999. * - we clear the "Pin Window" bit (so hardware is free to evict)
  1000. *
  1001. * - we re-initialize the window context when it is reassigned.
  1002. *
  1003. * We do the former in vas_win_close() and latter in vas_win_open().
  1004. * So, ignoring the cast-out for now. We can add it as needed. If
  1005. * casting out becomes necessary we should consider offloading the
  1006. * job to a worker thread, so the window close can proceed quickly.
  1007. */
  1008. static void poll_window_castout(struct vas_window *window)
  1009. {
  1010. /* stub for now */
  1011. }
  1012. /*
  1013. * Unpin and close a window so no new requests are accepted and the
  1014. * hardware can evict this window from cache if necessary.
  1015. */
  1016. static void unpin_close_window(struct vas_window *window)
  1017. {
  1018. u64 val;
  1019. val = read_hvwc_reg(window, VREG(WINCTL));
  1020. val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
  1021. val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
  1022. write_hvwc_reg(window, VREG(WINCTL), val);
  1023. }
  1024. /*
  1025. * Close a window.
  1026. *
  1027. * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
  1028. * - Disable new paste operations (unmap paste address)
  1029. * - Poll for the "Window Busy" bit to be cleared
  1030. * - Clear the Open/Enable bit for the Window.
  1031. * - Poll for return of window Credits (implies FIFO empty for Rx win?)
  1032. * - Unpin and cast window context out of cache
  1033. *
  1034. * Besides the hardware, kernel has some bookkeeping of course.
  1035. */
  1036. int vas_win_close(struct vas_window *window)
  1037. {
  1038. if (!window)
  1039. return 0;
  1040. if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
  1041. pr_devel("Attempting to close an active Rx window!\n");
  1042. WARN_ON_ONCE(1);
  1043. return -EBUSY;
  1044. }
  1045. unmap_paste_region(window);
  1046. clear_vinst_win(window);
  1047. poll_window_busy_state(window);
  1048. unpin_close_window(window);
  1049. poll_window_credits(window);
  1050. poll_window_castout(window);
  1051. /* if send window, drop reference to matching receive window */
  1052. if (window->tx_win)
  1053. put_rx_win(window->rxwin);
  1054. vas_window_free(window);
  1055. return 0;
  1056. }
  1057. EXPORT_SYMBOL_GPL(vas_win_close);
  1058. /*
  1059. * Return a system-wide unique window id for the window @win.
  1060. */
  1061. u32 vas_win_id(struct vas_window *win)
  1062. {
  1063. return encode_pswid(win->vinst->vas_id, win->winid);
  1064. }
  1065. EXPORT_SYMBOL_GPL(vas_win_id);