This source file includes following definitions.
- compute_paste_address
- get_hvwc_mmio_bar
- get_uwc_mmio_bar
- map_paste_region
- map_mmio_region
- unmap_region
- unmap_paste_region
- unmap_winctx_mmio_bars
- map_winctx_mmio_bars
- reset_window_regs
- init_xlate_regs
- init_rsvd_tx_buf_count
- init_winctx_regs
- vas_release_window_id
- vas_assign_window_id
- vas_window_free
- vas_window_alloc
- put_rx_win
- get_user_rxwin
- get_vinst_rxwin
- set_vinst_win
- clear_vinst_win
- init_winctx_for_rxwin
- rx_win_args_valid
- vas_init_rx_win_attr
- vas_rx_win_open
- vas_init_tx_win_attr
- init_winctx_for_txwin
- tx_win_args_valid
- vas_tx_win_open
- vas_copy_crb
- vas_paste_crb
- poll_window_credits
- poll_window_busy_state
- poll_window_castout
- unpin_close_window
- vas_win_close
1
2
3
4
5
6 #define pr_fmt(fmt) "vas: " fmt
7
8 #include <linux/types.h>
9 #include <linux/mutex.h>
10 #include <linux/slab.h>
11 #include <linux/io.h>
12 #include <linux/log2.h>
13 #include <linux/rcupdate.h>
14 #include <linux/cred.h>
15 #include <asm/switch_to.h>
16 #include <asm/ppc-opcode.h>
17 #include "vas.h"
18 #include "copy-paste.h"
19
20 #define CREATE_TRACE_POINTS
21 #include "vas-trace.h"
22
23
24
25
26
27 static void compute_paste_address(struct vas_window *window, u64 *addr, int *len)
28 {
29 int winid;
30 u64 base, shift;
31
32 base = window->vinst->paste_base_addr;
33 shift = window->vinst->paste_win_id_shift;
34 winid = window->winid;
35
36 *addr = base + (winid << shift);
37 if (len)
38 *len = PAGE_SIZE;
39
40 pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid, *addr);
41 }
42
43 static inline void get_hvwc_mmio_bar(struct vas_window *window,
44 u64 *start, int *len)
45 {
46 u64 pbaddr;
47
48 pbaddr = window->vinst->hvwc_bar_start;
49 *start = pbaddr + window->winid * VAS_HVWC_SIZE;
50 *len = VAS_HVWC_SIZE;
51 }
52
53 static inline void get_uwc_mmio_bar(struct vas_window *window,
54 u64 *start, int *len)
55 {
56 u64 pbaddr;
57
58 pbaddr = window->vinst->uwc_bar_start;
59 *start = pbaddr + window->winid * VAS_UWC_SIZE;
60 *len = VAS_UWC_SIZE;
61 }
62
63
64
65
66
67
68 static void *map_paste_region(struct vas_window *txwin)
69 {
70 int len;
71 void *map;
72 char *name;
73 u64 start;
74
75 name = kasprintf(GFP_KERNEL, "window-v%d-w%d", txwin->vinst->vas_id,
76 txwin->winid);
77 if (!name)
78 goto free_name;
79
80 txwin->paste_addr_name = name;
81 compute_paste_address(txwin, &start, &len);
82
83 if (!request_mem_region(start, len, name)) {
84 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
85 __func__, start, len);
86 goto free_name;
87 }
88
89 map = ioremap_cache(start, len);
90 if (!map) {
91 pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__,
92 start, len);
93 goto free_name;
94 }
95
96 pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start, map);
97 return map;
98
99 free_name:
100 kfree(name);
101 return ERR_PTR(-ENOMEM);
102 }
103
104 static void *map_mmio_region(char *name, u64 start, int len)
105 {
106 void *map;
107
108 if (!request_mem_region(start, len, name)) {
109 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
110 __func__, start, len);
111 return NULL;
112 }
113
114 map = ioremap(start, len);
115 if (!map) {
116 pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__, start,
117 len);
118 return NULL;
119 }
120
121 return map;
122 }
123
124 static void unmap_region(void *addr, u64 start, int len)
125 {
126 iounmap(addr);
127 release_mem_region((phys_addr_t)start, len);
128 }
129
130
131
132
133 static void unmap_paste_region(struct vas_window *window)
134 {
135 int len;
136 u64 busaddr_start;
137
138 if (window->paste_kaddr) {
139 compute_paste_address(window, &busaddr_start, &len);
140 unmap_region(window->paste_kaddr, busaddr_start, len);
141 window->paste_kaddr = NULL;
142 kfree(window->paste_addr_name);
143 window->paste_addr_name = NULL;
144 }
145 }
146
147
148
149
150
151
152
153
154 static void unmap_winctx_mmio_bars(struct vas_window *window)
155 {
156 int len;
157 void *uwc_map;
158 void *hvwc_map;
159 u64 busaddr_start;
160
161 mutex_lock(&vas_mutex);
162
163 hvwc_map = window->hvwc_map;
164 window->hvwc_map = NULL;
165
166 uwc_map = window->uwc_map;
167 window->uwc_map = NULL;
168
169 mutex_unlock(&vas_mutex);
170
171 if (hvwc_map) {
172 get_hvwc_mmio_bar(window, &busaddr_start, &len);
173 unmap_region(hvwc_map, busaddr_start, len);
174 }
175
176 if (uwc_map) {
177 get_uwc_mmio_bar(window, &busaddr_start, &len);
178 unmap_region(uwc_map, busaddr_start, len);
179 }
180 }
181
182
183
184
185
186
187 int map_winctx_mmio_bars(struct vas_window *window)
188 {
189 int len;
190 u64 start;
191
192 get_hvwc_mmio_bar(window, &start, &len);
193 window->hvwc_map = map_mmio_region("HVWCM_Window", start, len);
194
195 get_uwc_mmio_bar(window, &start, &len);
196 window->uwc_map = map_mmio_region("UWCM_Window", start, len);
197
198 if (!window->hvwc_map || !window->uwc_map) {
199 unmap_winctx_mmio_bars(window);
200 return -1;
201 }
202
203 return 0;
204 }
205
206
207
208
209
210
211
212
213
214
215 void reset_window_regs(struct vas_window *window)
216 {
217 write_hvwc_reg(window, VREG(LPID), 0ULL);
218 write_hvwc_reg(window, VREG(PID), 0ULL);
219 write_hvwc_reg(window, VREG(XLATE_MSR), 0ULL);
220 write_hvwc_reg(window, VREG(XLATE_LPCR), 0ULL);
221 write_hvwc_reg(window, VREG(XLATE_CTL), 0ULL);
222 write_hvwc_reg(window, VREG(AMR), 0ULL);
223 write_hvwc_reg(window, VREG(SEIDR), 0ULL);
224 write_hvwc_reg(window, VREG(FAULT_TX_WIN), 0ULL);
225 write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
226 write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), 0ULL);
227 write_hvwc_reg(window, VREG(PSWID), 0ULL);
228 write_hvwc_reg(window, VREG(LFIFO_BAR), 0ULL);
229 write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), 0ULL);
230 write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), 0ULL);
231 write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
232 write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
233 write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
234 write_hvwc_reg(window, VREG(LRX_WCRED), 0ULL);
235 write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
236 write_hvwc_reg(window, VREG(TX_WCRED), 0ULL);
237 write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
238 write_hvwc_reg(window, VREG(LFIFO_SIZE), 0ULL);
239 write_hvwc_reg(window, VREG(WINCTL), 0ULL);
240 write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
241 write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), 0ULL);
242 write_hvwc_reg(window, VREG(TX_RSVD_BUF_COUNT), 0ULL);
243 write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), 0ULL);
244 write_hvwc_reg(window, VREG(LNOTIFY_CTL), 0ULL);
245 write_hvwc_reg(window, VREG(LNOTIFY_PID), 0ULL);
246 write_hvwc_reg(window, VREG(LNOTIFY_LPID), 0ULL);
247 write_hvwc_reg(window, VREG(LNOTIFY_TID), 0ULL);
248 write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), 0ULL);
249 write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
250
251
252
253
254
255
256
257
258
259
260
261
262 }
263
264
265
266
267
268
269
270
271 static void init_xlate_regs(struct vas_window *window, bool user_win)
272 {
273 u64 lpcr, val;
274
275
276
277
278
279 val = 0ULL;
280 val = SET_FIELD(VAS_XLATE_MSR_HV, val, 1);
281 val = SET_FIELD(VAS_XLATE_MSR_SF, val, 1);
282 if (user_win) {
283 val = SET_FIELD(VAS_XLATE_MSR_DR, val, 1);
284 val = SET_FIELD(VAS_XLATE_MSR_PR, val, 1);
285 }
286 write_hvwc_reg(window, VREG(XLATE_MSR), val);
287
288 lpcr = mfspr(SPRN_LPCR);
289 val = 0ULL;
290
291
292
293
294
295
296
297 val = SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE, val, 5);
298 val = SET_FIELD(VAS_XLATE_LPCR_ISL, val, lpcr & LPCR_ISL);
299 val = SET_FIELD(VAS_XLATE_LPCR_TC, val, lpcr & LPCR_TC);
300 val = SET_FIELD(VAS_XLATE_LPCR_SC, val, 0);
301 write_hvwc_reg(window, VREG(XLATE_LPCR), val);
302
303
304
305
306
307
308
309
310 val = 0ULL;
311 val = SET_FIELD(VAS_XLATE_MODE, val, radix_enabled() ? 3 : 2);
312 write_hvwc_reg(window, VREG(XLATE_CTL), val);
313
314
315
316
317 val = 0ULL;
318 val = SET_FIELD(VAS_AMR, val, mfspr(SPRN_AMR));
319 write_hvwc_reg(window, VREG(AMR), val);
320
321 val = 0ULL;
322 val = SET_FIELD(VAS_SEIDR, val, 0);
323 write_hvwc_reg(window, VREG(SEIDR), val);
324 }
325
326
327
328
329
330
331
332
333
334
335
336 static void init_rsvd_tx_buf_count(struct vas_window *txwin,
337 struct vas_winctx *winctx)
338 {
339 write_hvwc_reg(txwin, VREG(TX_RSVD_BUF_COUNT), 0ULL);
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
359 {
360 u64 val;
361 int fifo_size;
362
363 reset_window_regs(window);
364
365 val = 0ULL;
366 val = SET_FIELD(VAS_LPID, val, winctx->lpid);
367 write_hvwc_reg(window, VREG(LPID), val);
368
369 val = 0ULL;
370 val = SET_FIELD(VAS_PID_ID, val, winctx->pidr);
371 write_hvwc_reg(window, VREG(PID), val);
372
373 init_xlate_regs(window, winctx->user_win);
374
375 val = 0ULL;
376 val = SET_FIELD(VAS_FAULT_TX_WIN, val, 0);
377 write_hvwc_reg(window, VREG(FAULT_TX_WIN), val);
378
379
380 write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
381
382 val = 0ULL;
383 val = SET_FIELD(VAS_HV_INTR_SRC_RA, val, winctx->irq_port);
384 write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), val);
385
386 val = 0ULL;
387 val = SET_FIELD(VAS_PSWID_EA_HANDLE, val, winctx->pswid);
388 write_hvwc_reg(window, VREG(PSWID), val);
389
390 write_hvwc_reg(window, VREG(SPARE1), 0ULL);
391 write_hvwc_reg(window, VREG(SPARE2), 0ULL);
392 write_hvwc_reg(window, VREG(SPARE3), 0ULL);
393
394
395
396
397
398
399
400
401
402
403 val = __pa(winctx->rx_fifo);
404 val = SET_FIELD(VAS_PAGE_MIGRATION_SELECT, val, 0);
405 write_hvwc_reg(window, VREG(LFIFO_BAR), val);
406
407 val = 0ULL;
408 val = SET_FIELD(VAS_LDATA_STAMP, val, winctx->data_stamp);
409 write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), val);
410
411 val = 0ULL;
412 val = SET_FIELD(VAS_LDMA_TYPE, val, winctx->dma_type);
413 val = SET_FIELD(VAS_LDMA_FIFO_DISABLE, val, winctx->fifo_disable);
414 write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), val);
415
416 write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
417 write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
418 write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
419
420 val = 0ULL;
421 val = SET_FIELD(VAS_LRX_WCRED, val, winctx->wcreds_max);
422 write_hvwc_reg(window, VREG(LRX_WCRED), val);
423
424 val = 0ULL;
425 val = SET_FIELD(VAS_TX_WCRED, val, winctx->wcreds_max);
426 write_hvwc_reg(window, VREG(TX_WCRED), val);
427
428 write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
429 write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
430
431 fifo_size = winctx->rx_fifo_size / 1024;
432
433 val = 0ULL;
434 val = SET_FIELD(VAS_LFIFO_SIZE, val, ilog2(fifo_size));
435 write_hvwc_reg(window, VREG(LFIFO_SIZE), val);
436
437
438
439
440
441
442 write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
443
444 init_rsvd_tx_buf_count(window, winctx);
445
446
447 val = 0ULL;
448 val = SET_FIELD(VAS_LRX_WIN_ID, val, winctx->rx_win_id);
449 write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), val);
450
451 write_hvwc_reg(window, VREG(SPARE4), 0ULL);
452
453 val = 0ULL;
454 val = SET_FIELD(VAS_NOTIFY_DISABLE, val, winctx->notify_disable);
455 val = SET_FIELD(VAS_INTR_DISABLE, val, winctx->intr_disable);
456 val = SET_FIELD(VAS_NOTIFY_EARLY, val, winctx->notify_early);
457 val = SET_FIELD(VAS_NOTIFY_OSU_INTR, val, winctx->notify_os_intr_reg);
458 write_hvwc_reg(window, VREG(LNOTIFY_CTL), val);
459
460 val = 0ULL;
461 val = SET_FIELD(VAS_LNOTIFY_PID, val, winctx->lnotify_pid);
462 write_hvwc_reg(window, VREG(LNOTIFY_PID), val);
463
464 val = 0ULL;
465 val = SET_FIELD(VAS_LNOTIFY_LPID, val, winctx->lnotify_lpid);
466 write_hvwc_reg(window, VREG(LNOTIFY_LPID), val);
467
468 val = 0ULL;
469 val = SET_FIELD(VAS_LNOTIFY_TID, val, winctx->lnotify_tid);
470 write_hvwc_reg(window, VREG(LNOTIFY_TID), val);
471
472 val = 0ULL;
473 val = SET_FIELD(VAS_LNOTIFY_MIN_SCOPE, val, winctx->min_scope);
474 val = SET_FIELD(VAS_LNOTIFY_MAX_SCOPE, val, winctx->max_scope);
475 write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), val);
476
477
478
479 write_hvwc_reg(window, VREG(SPARE5), 0ULL);
480 write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
481 write_hvwc_reg(window, VREG(SPARE6), 0ULL);
482
483
484 val = 0ULL;
485 val = SET_FIELD(VAS_PUSH_TO_MEM, val, 1);
486 write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), val);
487
488
489 val = 0ULL;
490 val = SET_FIELD(VAS_WINCTL_REJ_NO_CREDIT, val, winctx->rej_no_credit);
491 val = SET_FIELD(VAS_WINCTL_PIN, val, winctx->pin_win);
492 val = SET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val, winctx->tx_wcred_mode);
493 val = SET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val, winctx->rx_wcred_mode);
494 val = SET_FIELD(VAS_WINCTL_TX_WORD_MODE, val, winctx->tx_word_mode);
495 val = SET_FIELD(VAS_WINCTL_RX_WORD_MODE, val, winctx->rx_word_mode);
496 val = SET_FIELD(VAS_WINCTL_FAULT_WIN, val, winctx->fault_win);
497 val = SET_FIELD(VAS_WINCTL_NX_WIN, val, winctx->nx_win);
498 val = SET_FIELD(VAS_WINCTL_OPEN, val, 1);
499 write_hvwc_reg(window, VREG(WINCTL), val);
500
501 return 0;
502 }
503
504 static void vas_release_window_id(struct ida *ida, int winid)
505 {
506 ida_free(ida, winid);
507 }
508
509 static int vas_assign_window_id(struct ida *ida)
510 {
511 int winid = ida_alloc_max(ida, VAS_WINDOWS_PER_CHIP - 1, GFP_KERNEL);
512
513 if (winid == -ENOSPC) {
514 pr_err("Too many (%d) open windows\n", VAS_WINDOWS_PER_CHIP);
515 return -EAGAIN;
516 }
517
518 return winid;
519 }
520
521 static void vas_window_free(struct vas_window *window)
522 {
523 int winid = window->winid;
524 struct vas_instance *vinst = window->vinst;
525
526 unmap_winctx_mmio_bars(window);
527
528 vas_window_free_dbgdir(window);
529
530 kfree(window);
531
532 vas_release_window_id(&vinst->ida, winid);
533 }
534
535 static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
536 {
537 int winid;
538 struct vas_window *window;
539
540 winid = vas_assign_window_id(&vinst->ida);
541 if (winid < 0)
542 return ERR_PTR(winid);
543
544 window = kzalloc(sizeof(*window), GFP_KERNEL);
545 if (!window)
546 goto out_free;
547
548 window->vinst = vinst;
549 window->winid = winid;
550
551 if (map_winctx_mmio_bars(window))
552 goto out_free;
553
554 vas_window_init_dbgdir(window);
555
556 return window;
557
558 out_free:
559 kfree(window);
560 vas_release_window_id(&vinst->ida, winid);
561 return ERR_PTR(-ENOMEM);
562 }
563
564 static void put_rx_win(struct vas_window *rxwin)
565 {
566
567 WARN_ON_ONCE(rxwin->tx_win);
568
569 atomic_dec(&rxwin->num_txwins);
570 }
571
572
573
574
575
576
577
578
579
580 static struct vas_window *get_user_rxwin(struct vas_instance *vinst, u32 pswid)
581 {
582 int vasid, winid;
583 struct vas_window *rxwin;
584
585 decode_pswid(pswid, &vasid, &winid);
586
587 if (vinst->vas_id != vasid)
588 return ERR_PTR(-EINVAL);
589
590 rxwin = vinst->windows[winid];
591
592 if (!rxwin || rxwin->tx_win || rxwin->cop != VAS_COP_TYPE_FTW)
593 return ERR_PTR(-EINVAL);
594
595 return rxwin;
596 }
597
598
599
600
601
602
603
604 static struct vas_window *get_vinst_rxwin(struct vas_instance *vinst,
605 enum vas_cop_type cop, u32 pswid)
606 {
607 struct vas_window *rxwin;
608
609 mutex_lock(&vinst->mutex);
610
611 if (cop == VAS_COP_TYPE_FTW)
612 rxwin = get_user_rxwin(vinst, pswid);
613 else
614 rxwin = vinst->rxwin[cop] ?: ERR_PTR(-EINVAL);
615
616 if (!IS_ERR(rxwin))
617 atomic_inc(&rxwin->num_txwins);
618
619 mutex_unlock(&vinst->mutex);
620
621 return rxwin;
622 }
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639 static void set_vinst_win(struct vas_instance *vinst,
640 struct vas_window *window)
641 {
642 int id = window->winid;
643
644 mutex_lock(&vinst->mutex);
645
646
647
648
649
650 if (!window->user_win && !window->tx_win) {
651 WARN_ON_ONCE(vinst->rxwin[window->cop]);
652 vinst->rxwin[window->cop] = window;
653 }
654
655 WARN_ON_ONCE(vinst->windows[id] != NULL);
656 vinst->windows[id] = window;
657
658 mutex_unlock(&vinst->mutex);
659 }
660
661
662
663
664
665 static void clear_vinst_win(struct vas_window *window)
666 {
667 int id = window->winid;
668 struct vas_instance *vinst = window->vinst;
669
670 mutex_lock(&vinst->mutex);
671
672 if (!window->user_win && !window->tx_win) {
673 WARN_ON_ONCE(!vinst->rxwin[window->cop]);
674 vinst->rxwin[window->cop] = NULL;
675 }
676
677 WARN_ON_ONCE(vinst->windows[id] != window);
678 vinst->windows[id] = NULL;
679
680 mutex_unlock(&vinst->mutex);
681 }
682
683 static void init_winctx_for_rxwin(struct vas_window *rxwin,
684 struct vas_rx_win_attr *rxattr,
685 struct vas_winctx *winctx)
686 {
687
688
689
690
691
692
693
694
695
696
697
698
699
700 memset(winctx, 0, sizeof(struct vas_winctx));
701
702 winctx->rx_fifo = rxattr->rx_fifo;
703 winctx->rx_fifo_size = rxattr->rx_fifo_size;
704 winctx->wcreds_max = rxwin->wcreds_max;
705 winctx->pin_win = rxattr->pin_win;
706
707 winctx->nx_win = rxattr->nx_win;
708 winctx->fault_win = rxattr->fault_win;
709 winctx->user_win = rxattr->user_win;
710 winctx->rej_no_credit = rxattr->rej_no_credit;
711 winctx->rx_word_mode = rxattr->rx_win_ord_mode;
712 winctx->tx_word_mode = rxattr->tx_win_ord_mode;
713 winctx->rx_wcred_mode = rxattr->rx_wcred_mode;
714 winctx->tx_wcred_mode = rxattr->tx_wcred_mode;
715 winctx->notify_early = rxattr->notify_early;
716
717 if (winctx->nx_win) {
718 winctx->data_stamp = true;
719 winctx->intr_disable = true;
720 winctx->pin_win = true;
721
722 WARN_ON_ONCE(winctx->fault_win);
723 WARN_ON_ONCE(!winctx->rx_word_mode);
724 WARN_ON_ONCE(!winctx->tx_word_mode);
725 WARN_ON_ONCE(winctx->notify_after_count);
726 } else if (winctx->fault_win) {
727 winctx->notify_disable = true;
728 } else if (winctx->user_win) {
729
730
731
732
733
734
735
736
737 winctx->fifo_disable = true;
738 winctx->intr_disable = true;
739 winctx->rx_fifo = NULL;
740 }
741
742 winctx->lnotify_lpid = rxattr->lnotify_lpid;
743 winctx->lnotify_pid = rxattr->lnotify_pid;
744 winctx->lnotify_tid = rxattr->lnotify_tid;
745 winctx->pswid = rxattr->pswid;
746 winctx->dma_type = VAS_DMA_TYPE_INJECT;
747 winctx->tc_mode = rxattr->tc_mode;
748
749 winctx->min_scope = VAS_SCOPE_LOCAL;
750 winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
751 }
752
753 static bool rx_win_args_valid(enum vas_cop_type cop,
754 struct vas_rx_win_attr *attr)
755 {
756 pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
757 attr->fault_win, attr->notify_disable,
758 attr->intr_disable, attr->notify_early,
759 attr->rx_fifo_size);
760
761 if (cop >= VAS_COP_TYPE_MAX)
762 return false;
763
764 if (cop != VAS_COP_TYPE_FTW &&
765 attr->rx_fifo_size < VAS_RX_FIFO_SIZE_MIN)
766 return false;
767
768 if (attr->rx_fifo_size > VAS_RX_FIFO_SIZE_MAX)
769 return false;
770
771 if (attr->wcreds_max > VAS_RX_WCREDS_MAX)
772 return false;
773
774 if (attr->nx_win) {
775
776 if (attr->fault_win || attr->user_win)
777 return false;
778
779
780
781
782 if (attr->notify_disable || !attr->intr_disable ||
783 attr->notify_early)
784 return false;
785 } else if (attr->fault_win) {
786
787 if (attr->user_win)
788 return false;
789
790
791
792
793
794 if (!attr->notify_disable || attr->intr_disable)
795 return false;
796
797 } else if (attr->user_win) {
798
799
800
801
802 if (attr->rx_fifo || attr->rx_fifo_size || !attr->intr_disable)
803 return false;
804 } else {
805
806 return false;
807 }
808
809 return true;
810 }
811
812 void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop)
813 {
814 memset(rxattr, 0, sizeof(*rxattr));
815
816 if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
817 rxattr->pin_win = true;
818 rxattr->nx_win = true;
819 rxattr->fault_win = false;
820 rxattr->intr_disable = true;
821 rxattr->rx_wcred_mode = true;
822 rxattr->tx_wcred_mode = true;
823 rxattr->rx_win_ord_mode = true;
824 rxattr->tx_win_ord_mode = true;
825 } else if (cop == VAS_COP_TYPE_FAULT) {
826 rxattr->pin_win = true;
827 rxattr->fault_win = true;
828 rxattr->notify_disable = true;
829 rxattr->rx_wcred_mode = true;
830 rxattr->tx_wcred_mode = true;
831 rxattr->rx_win_ord_mode = true;
832 rxattr->tx_win_ord_mode = true;
833 } else if (cop == VAS_COP_TYPE_FTW) {
834 rxattr->user_win = true;
835 rxattr->intr_disable = true;
836
837
838
839
840
841
842
843 }
844 }
845 EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
846
847 struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
848 struct vas_rx_win_attr *rxattr)
849 {
850 struct vas_window *rxwin;
851 struct vas_winctx winctx;
852 struct vas_instance *vinst;
853
854 trace_vas_rx_win_open(current, vasid, cop, rxattr);
855
856 if (!rx_win_args_valid(cop, rxattr))
857 return ERR_PTR(-EINVAL);
858
859 vinst = find_vas_instance(vasid);
860 if (!vinst) {
861 pr_devel("vasid %d not found!\n", vasid);
862 return ERR_PTR(-EINVAL);
863 }
864 pr_devel("Found instance %d\n", vasid);
865
866 rxwin = vas_window_alloc(vinst);
867 if (IS_ERR(rxwin)) {
868 pr_devel("Unable to allocate memory for Rx window\n");
869 return rxwin;
870 }
871
872 rxwin->tx_win = false;
873 rxwin->nx_win = rxattr->nx_win;
874 rxwin->user_win = rxattr->user_win;
875 rxwin->cop = cop;
876 rxwin->wcreds_max = rxattr->wcreds_max ?: VAS_WCREDS_DEFAULT;
877 if (rxattr->user_win)
878 rxwin->pid = task_pid_vnr(current);
879
880 init_winctx_for_rxwin(rxwin, rxattr, &winctx);
881 init_winctx_regs(rxwin, &winctx);
882
883 set_vinst_win(vinst, rxwin);
884
885 return rxwin;
886 }
887 EXPORT_SYMBOL_GPL(vas_rx_win_open);
888
889 void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
890 {
891 memset(txattr, 0, sizeof(*txattr));
892
893 if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
894 txattr->rej_no_credit = false;
895 txattr->rx_wcred_mode = true;
896 txattr->tx_wcred_mode = true;
897 txattr->rx_win_ord_mode = true;
898 txattr->tx_win_ord_mode = true;
899 } else if (cop == VAS_COP_TYPE_FTW) {
900 txattr->user_win = true;
901 }
902 }
903 EXPORT_SYMBOL_GPL(vas_init_tx_win_attr);
904
905 static void init_winctx_for_txwin(struct vas_window *txwin,
906 struct vas_tx_win_attr *txattr,
907 struct vas_winctx *winctx)
908 {
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924 memset(winctx, 0, sizeof(struct vas_winctx));
925
926 winctx->wcreds_max = txwin->wcreds_max;
927
928 winctx->user_win = txattr->user_win;
929 winctx->nx_win = txwin->rxwin->nx_win;
930 winctx->pin_win = txattr->pin_win;
931 winctx->rej_no_credit = txattr->rej_no_credit;
932 winctx->rsvd_txbuf_enable = txattr->rsvd_txbuf_enable;
933
934 winctx->rx_wcred_mode = txattr->rx_wcred_mode;
935 winctx->tx_wcred_mode = txattr->tx_wcred_mode;
936 winctx->rx_word_mode = txattr->rx_win_ord_mode;
937 winctx->tx_word_mode = txattr->tx_win_ord_mode;
938 winctx->rsvd_txbuf_count = txattr->rsvd_txbuf_count;
939
940 winctx->intr_disable = true;
941 if (winctx->nx_win)
942 winctx->data_stamp = true;
943
944 winctx->lpid = txattr->lpid;
945 winctx->pidr = txattr->pidr;
946 winctx->rx_win_id = txwin->rxwin->winid;
947
948 winctx->dma_type = VAS_DMA_TYPE_INJECT;
949 winctx->tc_mode = txattr->tc_mode;
950 winctx->min_scope = VAS_SCOPE_LOCAL;
951 winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
952
953 winctx->pswid = 0;
954 }
955
956 static bool tx_win_args_valid(enum vas_cop_type cop,
957 struct vas_tx_win_attr *attr)
958 {
959 if (attr->tc_mode != VAS_THRESH_DISABLED)
960 return false;
961
962 if (cop > VAS_COP_TYPE_MAX)
963 return false;
964
965 if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
966 return false;
967
968 if (attr->user_win &&
969 (cop != VAS_COP_TYPE_FTW || attr->rsvd_txbuf_count))
970 return false;
971
972 return true;
973 }
974
975 struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
976 struct vas_tx_win_attr *attr)
977 {
978 int rc;
979 struct vas_window *txwin;
980 struct vas_window *rxwin;
981 struct vas_winctx winctx;
982 struct vas_instance *vinst;
983
984 trace_vas_tx_win_open(current, vasid, cop, attr);
985
986 if (!tx_win_args_valid(cop, attr))
987 return ERR_PTR(-EINVAL);
988
989
990
991
992
993
994 if (vasid == -1 && attr->pswid)
995 decode_pswid(attr->pswid, &vasid, NULL);
996
997 vinst = find_vas_instance(vasid);
998 if (!vinst) {
999 pr_devel("vasid %d not found!\n", vasid);
1000 return ERR_PTR(-EINVAL);
1001 }
1002
1003 rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
1004 if (IS_ERR(rxwin)) {
1005 pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
1006 return rxwin;
1007 }
1008
1009 txwin = vas_window_alloc(vinst);
1010 if (IS_ERR(txwin)) {
1011 rc = PTR_ERR(txwin);
1012 goto put_rxwin;
1013 }
1014
1015 txwin->cop = cop;
1016 txwin->tx_win = 1;
1017 txwin->rxwin = rxwin;
1018 txwin->nx_win = txwin->rxwin->nx_win;
1019 txwin->pid = attr->pid;
1020 txwin->user_win = attr->user_win;
1021 txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
1022
1023 init_winctx_for_txwin(txwin, attr, &winctx);
1024
1025 init_winctx_regs(txwin, &winctx);
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 if (!txwin->user_win) {
1036 txwin->paste_kaddr = map_paste_region(txwin);
1037 if (IS_ERR(txwin->paste_kaddr)) {
1038 rc = PTR_ERR(txwin->paste_kaddr);
1039 goto free_window;
1040 }
1041 } else {
1042
1043
1044
1045
1046 rc = set_thread_uses_vas();
1047 if (rc)
1048 goto free_window;
1049 }
1050
1051 set_vinst_win(vinst, txwin);
1052
1053 return txwin;
1054
1055 free_window:
1056 vas_window_free(txwin);
1057
1058 put_rxwin:
1059 put_rx_win(rxwin);
1060 return ERR_PTR(rc);
1061
1062 }
1063 EXPORT_SYMBOL_GPL(vas_tx_win_open);
1064
1065 int vas_copy_crb(void *crb, int offset)
1066 {
1067 return vas_copy(crb, offset);
1068 }
1069 EXPORT_SYMBOL_GPL(vas_copy_crb);
1070
1071 #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
1072 int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
1073 {
1074 int rc;
1075 void *addr;
1076 uint64_t val;
1077
1078 trace_vas_paste_crb(current, txwin);
1079
1080
1081
1082
1083
1084
1085 WARN_ON_ONCE(txwin->nx_win && !re);
1086
1087 addr = txwin->paste_kaddr;
1088 if (re) {
1089
1090
1091
1092
1093 val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
1094 addr += val;
1095 }
1096
1097
1098
1099
1100
1101 rc = vas_paste(addr, offset);
1102 if (rc == 2)
1103 rc = 0;
1104 else
1105 rc = -EINVAL;
1106
1107 pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
1108 read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
1109
1110 return rc;
1111 }
1112 EXPORT_SYMBOL_GPL(vas_paste_crb);
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 static void poll_window_credits(struct vas_window *window)
1128 {
1129 u64 val;
1130 int creds, mode;
1131
1132 val = read_hvwc_reg(window, VREG(WINCTL));
1133 if (window->tx_win)
1134 mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
1135 else
1136 mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
1137
1138 if (!mode)
1139 return;
1140 retry:
1141 if (window->tx_win) {
1142 val = read_hvwc_reg(window, VREG(TX_WCRED));
1143 creds = GET_FIELD(VAS_TX_WCRED, val);
1144 } else {
1145 val = read_hvwc_reg(window, VREG(LRX_WCRED));
1146 creds = GET_FIELD(VAS_LRX_WCRED, val);
1147 }
1148
1149 if (creds < window->wcreds_max) {
1150 val = 0;
1151 set_current_state(TASK_UNINTERRUPTIBLE);
1152 schedule_timeout(msecs_to_jiffies(10));
1153 goto retry;
1154 }
1155 }
1156
1157
1158
1159
1160
1161
1162 static void poll_window_busy_state(struct vas_window *window)
1163 {
1164 int busy;
1165 u64 val;
1166
1167 retry:
1168 val = read_hvwc_reg(window, VREG(WIN_STATUS));
1169 busy = GET_FIELD(VAS_WIN_BUSY, val);
1170 if (busy) {
1171 val = 0;
1172 set_current_state(TASK_UNINTERRUPTIBLE);
1173 schedule_timeout(msecs_to_jiffies(5));
1174 goto retry;
1175 }
1176 }
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194 static void poll_window_castout(struct vas_window *window)
1195 {
1196
1197 }
1198
1199
1200
1201
1202
1203 static void unpin_close_window(struct vas_window *window)
1204 {
1205 u64 val;
1206
1207 val = read_hvwc_reg(window, VREG(WINCTL));
1208 val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
1209 val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
1210 write_hvwc_reg(window, VREG(WINCTL), val);
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225 int vas_win_close(struct vas_window *window)
1226 {
1227 if (!window)
1228 return 0;
1229
1230 if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
1231 pr_devel("Attempting to close an active Rx window!\n");
1232 WARN_ON_ONCE(1);
1233 return -EBUSY;
1234 }
1235
1236 unmap_paste_region(window);
1237
1238 clear_vinst_win(window);
1239
1240 poll_window_busy_state(window);
1241
1242 unpin_close_window(window);
1243
1244 poll_window_credits(window);
1245
1246 poll_window_castout(window);
1247
1248
1249 if (window->tx_win)
1250 put_rx_win(window->rxwin);
1251
1252 vas_window_free(window);
1253
1254 return 0;
1255 }
1256 EXPORT_SYMBOL_GPL(vas_win_close);