vlib: introduce DMA infrastructure
[vpp.git] / src / plugins / dma_intel / dsa.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright(c) 2022 Cisco Systems, Inc.
3  * Copyright (c) 2022 Intel and/or its affiliates.
4  */
5
6 #include <vlib/vlib.h>
7 #include <vlib/pci/pci.h>
8 #include <vlib/dma/dma.h>
9 #include <vppinfra/heap.h>
10 #include <vppinfra/atomics.h>
11 #include <vnet/plugin/plugin.h>
12 #include <vpp/app/version.h>
13 #include <dma_intel/dsa_intel.h>
14
15 extern vlib_node_registration_t intel_dsa_node;
16
17 VLIB_REGISTER_LOG_CLASS (intel_dsa_log, static) = {
18   .class_name = "intel_dsa",
19   .subclass_name = "dsa",
20 };
21
22 static void
23 intel_dsa_channel_lock (intel_dsa_channel_t *ch)
24 {
25   u8 expected = 0;
26   if (ch->n_threads < 2)
27     return;
28
29   /* channel is used by multiple threads so we need to lock it */
30   while (!__atomic_compare_exchange_n (&ch->lock, &expected,
31                                        /* desired */ 1, /* weak */ 0,
32                                        __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
33     {
34       while (__atomic_load_n (&ch->lock, __ATOMIC_RELAXED))
35         CLIB_PAUSE ();
36       expected = 0;
37     }
38 }
39
40 static void
41 intel_dsa_channel_unlock (intel_dsa_channel_t *ch)
42 {
43   if (ch->n_threads < 2)
44     return;
45
46   __atomic_store_n (&ch->lock, 0, __ATOMIC_RELEASE);
47 }
48
49 static vlib_dma_batch_t *
50 intel_dsa_batch_new (vlib_main_t *vm, struct vlib_dma_config_data *cd)
51 {
52   intel_dsa_main_t *idm = &intel_dsa_main;
53   intel_dsa_config_t *idc;
54   intel_dsa_batch_t *b;
55
56   idc = vec_elt_at_index (idm->dsa_config_heap,
57                           cd->private_data + vm->thread_index);
58
59   if (vec_len (idc->freelist) > 0)
60     b = vec_pop (idc->freelist);
61   else
62     {
63       clib_spinlock_lock (&idm->lock);
64       b = vlib_physmem_alloc (vm, idc->alloc_size);
65       clib_spinlock_unlock (&idm->lock);
66       /* if no free space in physmem, force quit */
67       ASSERT (b != NULL);
68       *b = idc->batch_template;
69       b->max_transfers = idc->max_transfers;
70
71       u32 def_flags = (INTEL_DSA_OP_MEMMOVE << INTEL_DSA_OP_SHIFT) |
72                       INTEL_DSA_FLAG_CACHE_CONTROL;
73       if (b->ch->block_on_fault)
74         def_flags |= INTEL_DSA_FLAG_BLOCK_ON_FAULT;
75       for (int i = 0; i < idc->max_transfers; i++)
76         {
77           intel_dsa_desc_t *dsa_desc = b->descs + i;
78           dsa_desc->op_flags = def_flags;
79         }
80     }
81
82   return &b->batch;
83 }
84
85 #if defined(__x86_64__) || defined(i386)
86 static_always_inline void
87 __movdir64b (volatile void *dst, const void *src)
88 {
89   asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
90                :
91                : "a"(dst), "d"(src)
92                : "memory");
93 }
94 #endif
95
96 static_always_inline void
97 intel_dsa_batch_fallback (vlib_main_t *vm, intel_dsa_batch_t *b,
98                           intel_dsa_channel_t *ch)
99 {
100   for (u16 i = 0; i < b->batch.n_enq; i++)
101     {
102       intel_dsa_desc_t *desc = &b->descs[i];
103       clib_memcpy_fast (desc->dst, desc->src, desc->size);
104     }
105   b->status = INTEL_DSA_STATUS_CPU_SUCCESS;
106   ch->submitted++;
107   return;
108 }
109
110 int
111 intel_dsa_batch_submit (vlib_main_t *vm, struct vlib_dma_batch *vb)
112 {
113   intel_dsa_main_t *idm = &intel_dsa_main;
114   intel_dsa_batch_t *b = (intel_dsa_batch_t *) vb;
115   intel_dsa_channel_t *ch = b->ch;
116   if (PREDICT_FALSE (vb->n_enq == 0))
117     {
118       vec_add1 (idm->dsa_config_heap[b->config_heap_index].freelist, b);
119       return 0;
120     }
121
122   intel_dsa_channel_lock (ch);
123   if (ch->n_enq >= ch->size)
124     {
125       if (!b->sw_fallback)
126         {
127           intel_dsa_channel_unlock (ch);
128           return 0;
129         }
130       /* skip channel limitation if first pending finished */
131       intel_dsa_batch_t *lb = NULL;
132       u32 n_pendings =
133         vec_len (idm->dsa_threads[vm->thread_index].pending_batches);
134       if (n_pendings)
135         lb =
136           idm->dsa_threads[vm->thread_index].pending_batches[n_pendings - 1];
137
138       if (!lb || lb->status != INTEL_DSA_STATUS_SUCCESS)
139         {
140           intel_dsa_batch_fallback (vm, b, ch);
141           goto done;
142         }
143     }
144
145   b->status = INTEL_DSA_STATUS_BUSY;
146   if (PREDICT_FALSE (vb->n_enq == 1))
147     {
148       intel_dsa_desc_t *desc = &b->descs[0];
149       desc->completion = (u64) &b->completion_cl;
150       desc->op_flags |= INTEL_DSA_FLAG_COMPLETION_ADDR_VALID |
151                         INTEL_DSA_FLAG_REQUEST_COMPLETION;
152 #if defined(__x86_64__) || defined(i386)
153       _mm_sfence (); /* fence before writing desc to device */
154       __movdir64b (ch->portal, (void *) desc);
155 #endif
156     }
157   else
158     {
159       intel_dsa_desc_t *batch_desc = &b->descs[b->max_transfers];
160       batch_desc->op_flags = (INTEL_DSA_OP_BATCH << INTEL_DSA_OP_SHIFT) |
161                              INTEL_DSA_FLAG_COMPLETION_ADDR_VALID |
162                              INTEL_DSA_FLAG_REQUEST_COMPLETION;
163       batch_desc->desc_addr = (void *) (b->descs);
164       batch_desc->size = vb->n_enq;
165       batch_desc->completion = (u64) &b->completion_cl;
166 #if defined(__x86_64__) || defined(i386)
167       _mm_sfence (); /* fence before writing desc to device */
168       __movdir64b (ch->portal, (void *) batch_desc);
169 #endif
170     }
171
172   ch->submitted++;
173   ch->n_enq++;
174
175 done:
176   intel_dsa_channel_unlock (ch);
177   vec_add1 (idm->dsa_threads[vm->thread_index].pending_batches, b);
178   vlib_node_set_interrupt_pending (vm, intel_dsa_node.index);
179   return 1;
180 }
181
182 static int
183 intel_dsa_check_channel (intel_dsa_channel_t *ch, vlib_dma_config_data_t *cd)
184 {
185   if (!ch)
186     {
187       dsa_log_error ("no available dsa channel");
188       return 1;
189     }
190   vlib_dma_config_t supported_cfg = {
191     .barrier_before_last = 1,
192     .sw_fallback = 1,
193   };
194
195   if (cd->cfg.features & ~supported_cfg.features)
196     {
197       dsa_log_error ("unsupported feature requested");
198       return 1;
199     }
200
201   if (cd->cfg.max_transfers > ch->max_transfers)
202     {
203       dsa_log_error ("transfer number (%u) too big", cd->cfg.max_transfers);
204       return 1;
205     }
206
207   if (cd->cfg.max_transfer_size > ch->max_transfer_size)
208     {
209       dsa_log_error ("transfer size (%u) too big", cd->cfg.max_transfer_size);
210       return 1;
211     }
212   return 0;
213 }
214
215 static int
216 intel_dsa_config_add_fn (vlib_main_t *vm, vlib_dma_config_data_t *cd)
217 {
218   intel_dsa_main_t *idm = &intel_dsa_main;
219   intel_dsa_config_t *idc;
220   u32 index, n_threads = vlib_get_n_threads ();
221
222   vec_validate (idm->dsa_config_heap_handle_by_config_index, cd->config_index);
223   index = heap_alloc_aligned (
224     idm->dsa_config_heap, n_threads, CLIB_CACHE_LINE_BYTES,
225     idm->dsa_config_heap_handle_by_config_index[cd->config_index]);
226
227   cd->batch_new_fn = intel_dsa_batch_new;
228   cd->private_data = index;
229
230   for (u32 thread = 0; thread < n_threads; thread++)
231     {
232       intel_dsa_batch_t *idb;
233       vlib_dma_batch_t *b;
234       idc = vec_elt_at_index (idm->dsa_config_heap, index + thread);
235
236       /* size of physmem allocation for this config */
237       idc->max_transfers = cd->cfg.max_transfers;
238       idc->alloc_size = sizeof (intel_dsa_batch_t) +
239                         sizeof (intel_dsa_desc_t) * (idc->max_transfers + 1);
240       /* fill batch template */
241       idb = &idc->batch_template;
242       idb->ch = idm->dsa_threads[thread].ch;
243       if (intel_dsa_check_channel (idb->ch, cd))
244         return 0;
245
246       dsa_log_debug ("config %d in thread %d using channel %u/%u",
247                      cd->config_index, thread, idb->ch->did, idb->ch->qid);
248       idb->config_heap_index = index + thread;
249       idb->config_index = cd->config_index;
250       idb->batch.callback_fn = cd->cfg.callback_fn;
251       idb->features = cd->cfg.features;
252       b = &idb->batch;
253       b->stride = sizeof (intel_dsa_desc_t);
254       b->src_ptr_off = STRUCT_OFFSET_OF (intel_dsa_batch_t, descs[0].src);
255       b->dst_ptr_off = STRUCT_OFFSET_OF (intel_dsa_batch_t, descs[0].dst);
256       b->size_off = STRUCT_OFFSET_OF (intel_dsa_batch_t, descs[0].size);
257       b->submit_fn = intel_dsa_batch_submit;
258       dsa_log_debug (
259         "config %d in thread %d stride %d src/dst/size offset %d-%d-%d",
260         cd->config_index, thread, b->stride, b->src_ptr_off, b->dst_ptr_off,
261         b->size_off);
262     }
263
264   dsa_log_info ("config %u added", cd->private_data);
265
266   return 1;
267 }
268
269 static void
270 intel_dsa_config_del_fn (vlib_main_t *vm, vlib_dma_config_data_t *cd)
271 {
272   intel_dsa_main_t *idm = &intel_dsa_main;
273   intel_dsa_thread_t *t =
274     vec_elt_at_index (idm->dsa_threads, vm->thread_index);
275   u32 n_pending, n_threads, config_heap_index, n = 0;
276   n_threads = vlib_get_n_threads ();
277
278   if (!t->pending_batches)
279     goto free_heap;
280
281   n_pending = vec_len (t->pending_batches);
282   intel_dsa_batch_t *b;
283
284   /* clean pending list and free list */
285   for (u32 i = 0; i < n_pending; i++)
286     {
287       b = t->pending_batches[i];
288       if (b->config_index == cd->config_index)
289         {
290           vec_add1 (idm->dsa_config_heap[b->config_heap_index].freelist, b);
291           if (b->status == INTEL_DSA_STATUS_SUCCESS ||
292               b->status == INTEL_DSA_STATUS_BUSY)
293             b->ch->n_enq--;
294         }
295       else
296         t->pending_batches[n++] = b;
297     }
298
299   vec_set_len (t->pending_batches, n);
300
301 free_heap:
302   for (u32 thread = 0; thread < n_threads; thread++)
303     {
304       config_heap_index = cd->private_data + thread;
305       while (vec_len (idm->dsa_config_heap[config_heap_index].freelist) > 0)
306         {
307           b = vec_pop (idm->dsa_config_heap[config_heap_index].freelist);
308           vlib_physmem_free (vm, b);
309         }
310     }
311
312   heap_dealloc (idm->dsa_config_heap,
313                 idm->dsa_config_heap_handle_by_config_index[cd->config_index]);
314
315   dsa_log_debug ("config %u removed", cd->private_data);
316 }
317
318 static uword
319 intel_dsa_node_fn (vlib_main_t *vm, vlib_node_runtime_t *node,
320                    vlib_frame_t *frame)
321 {
322   intel_dsa_main_t *idm = &intel_dsa_main;
323   intel_dsa_thread_t *t =
324     vec_elt_at_index (idm->dsa_threads, vm->thread_index);
325   u32 n_pending = 0, n = 0;
326   u8 glitch = 0;
327
328   if (!t->pending_batches)
329     return 0;
330
331   n_pending = vec_len (t->pending_batches);
332
333   for (u32 i = 0; i < n_pending; i++)
334     {
335       intel_dsa_batch_t *b = t->pending_batches[i];
336       intel_dsa_channel_t *ch = b->ch;
337
338       if ((b->status == INTEL_DSA_STATUS_SUCCESS ||
339            b->status == INTEL_DSA_STATUS_CPU_SUCCESS) &&
340           !glitch)
341         {
342           /* callback */
343           if (b->batch.callback_fn)
344             b->batch.callback_fn (vm, &b->batch);
345
346           /* restore last descriptor fields */
347           if (b->batch.n_enq == 1)
348             {
349               b->descs[0].completion = 0;
350               b->descs[0].op_flags =
351                 (INTEL_DSA_OP_MEMMOVE << INTEL_DSA_OP_SHIFT) |
352                 INTEL_DSA_FLAG_CACHE_CONTROL;
353               if (b->ch->block_on_fault)
354                 b->descs[0].op_flags |= INTEL_DSA_FLAG_BLOCK_ON_FAULT;
355             }
356           /* add to freelist */
357           vec_add1 (idm->dsa_config_heap[b->config_heap_index].freelist, b);
358
359           intel_dsa_channel_lock (ch);
360           if (b->status == INTEL_DSA_STATUS_SUCCESS)
361             {
362               ch->n_enq--;
363               ch->completed++;
364             }
365           else
366             ch->sw_fallback++;
367           intel_dsa_channel_unlock (ch);
368
369           b->batch.n_enq = 0;
370           b->status = INTEL_DSA_STATUS_IDLE;
371         }
372       else if (b->status == INTEL_DSA_STATUS_BUSY)
373         {
374           glitch = 1 & b->barrier_before_last;
375           t->pending_batches[n++] = b;
376         }
377       else if (!glitch)
378         {
379           /* fallback to software if exception happened */
380           intel_dsa_batch_fallback (vm, b, ch);
381           glitch = 1 & b->barrier_before_last;
382         }
383       else
384         {
385           t->pending_batches[n++] = b;
386         }
387     }
388   vec_set_len (t->pending_batches, n);
389
390   if (n)
391     {
392       vlib_node_set_interrupt_pending (vm, intel_dsa_node.index);
393     }
394
395   return n_pending - n;
396 }
397
398 u8 *
399 format_dsa_info (u8 *s, va_list *args)
400 {
401   intel_dsa_main_t *idm = &intel_dsa_main;
402   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
403   intel_dsa_channel_t *ch;
404   ch = idm->dsa_threads[vm->thread_index].ch;
405   s = format (s, "thread %d dma %u/%u request %-16lld hw %-16lld cpu %-16lld",
406               vm->thread_index, ch->did, ch->qid, ch->submitted, ch->completed,
407               ch->sw_fallback);
408   return s;
409 }
410
411 VLIB_REGISTER_NODE (intel_dsa_node) = {
412   .function = intel_dsa_node_fn,
413   .name = "intel-dsa",
414   .type = VLIB_NODE_TYPE_INPUT,
415   .state = VLIB_NODE_STATE_INTERRUPT,
416   .vector_size = 4,
417 };
418
419 vlib_dma_backend_t intel_dsa_backend = {
420   .name = "Intel DSA",
421   .config_add_fn = intel_dsa_config_add_fn,
422   .config_del_fn = intel_dsa_config_del_fn,
423   .info_fn = format_dsa_info,
424 };