vas-window.c 41 KB

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