1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Cryptographic Coprocessor (CCP) driver
5 * Copyright (C) 2016,2019 Advanced Micro Devices, Inc.
7 * Author: Gary R Hook <gary.hook@amd.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/dmaengine.h>
14 #include <linux/spinlock.h>
15 #include <linux/mutex.h>
16 #include <linux/ccp.h>
19 #include "../../dma/dmaengine.h"
21 #define CCP_DMA_WIDTH(_mask) \
23 u64 mask = _mask + 1; \
24 (mask == 0) ? 64 : fls64(mask); \
27 /* The CCP as a DMA provider can be configured for public or private
28 * channels. Default is specified in the vdata for the device (PCI ID).
29 * This module parameter will override for all channels on all devices:
30 * dma_chan_attr = 0x2 to force all channels public
31 * = 0x1 to force all channels private
32 * = 0x0 to defer to the vdata setting
33 * = any other value: warning, revert to 0x0
35 static unsigned int dma_chan_attr
= CCP_DMA_DFLT
;
36 module_param(dma_chan_attr
, uint
, 0444);
37 MODULE_PARM_DESC(dma_chan_attr
, "Set DMA channel visibility: 0 (default) = device defaults, 1 = make private, 2 = make public");
39 static unsigned int dmaengine
= 1;
40 module_param(dmaengine
, uint
, 0444);
41 MODULE_PARM_DESC(dmaengine
, "Register services with the DMA subsystem (any non-zero value, default: 1)");
43 static unsigned int ccp_get_dma_chan_attr(struct ccp_device
*ccp
)
45 switch (dma_chan_attr
) {
47 return ccp
->vdata
->dma_chan_attr
;
56 dev_info_once(ccp
->dev
, "Invalid value for dma_chan_attr: %d\n",
58 return ccp
->vdata
->dma_chan_attr
;
62 static void ccp_free_cmd_resources(struct ccp_device
*ccp
,
63 struct list_head
*list
)
65 struct ccp_dma_cmd
*cmd
, *ctmp
;
67 list_for_each_entry_safe(cmd
, ctmp
, list
, entry
) {
68 list_del(&cmd
->entry
);
69 kmem_cache_free(ccp
->dma_cmd_cache
, cmd
);
73 static void ccp_free_desc_resources(struct ccp_device
*ccp
,
74 struct list_head
*list
)
76 struct ccp_dma_desc
*desc
, *dtmp
;
78 list_for_each_entry_safe(desc
, dtmp
, list
, entry
) {
79 ccp_free_cmd_resources(ccp
, &desc
->active
);
80 ccp_free_cmd_resources(ccp
, &desc
->pending
);
82 list_del(&desc
->entry
);
83 kmem_cache_free(ccp
->dma_desc_cache
, desc
);
87 static void ccp_free_chan_resources(struct dma_chan
*dma_chan
)
89 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
93 dev_dbg(chan
->ccp
->dev
, "%s - chan=%p\n", __func__
, chan
);
95 spin_lock_irqsave(&chan
->lock
, flags
);
97 ccp_free_desc_resources(chan
->ccp
, &chan
->complete
);
98 ccp_free_desc_resources(chan
->ccp
, &chan
->active
);
99 ccp_free_desc_resources(chan
->ccp
, &chan
->pending
);
100 ccp_free_desc_resources(chan
->ccp
, &chan
->created
);
102 spin_unlock_irqrestore(&chan
->lock
, flags
);
105 static void ccp_cleanup_desc_resources(struct ccp_device
*ccp
,
106 struct list_head
*list
)
108 struct ccp_dma_desc
*desc
, *dtmp
;
110 list_for_each_entry_safe_reverse(desc
, dtmp
, list
, entry
) {
111 if (!async_tx_test_ack(&desc
->tx_desc
))
114 dev_dbg(ccp
->dev
, "%s - desc=%p\n", __func__
, desc
);
116 ccp_free_cmd_resources(ccp
, &desc
->active
);
117 ccp_free_cmd_resources(ccp
, &desc
->pending
);
119 list_del(&desc
->entry
);
120 kmem_cache_free(ccp
->dma_desc_cache
, desc
);
124 static void ccp_do_cleanup(unsigned long data
)
126 struct ccp_dma_chan
*chan
= (struct ccp_dma_chan
*)data
;
129 dev_dbg(chan
->ccp
->dev
, "%s - chan=%s\n", __func__
,
130 dma_chan_name(&chan
->dma_chan
));
132 spin_lock_irqsave(&chan
->lock
, flags
);
134 ccp_cleanup_desc_resources(chan
->ccp
, &chan
->complete
);
136 spin_unlock_irqrestore(&chan
->lock
, flags
);
139 static int ccp_issue_next_cmd(struct ccp_dma_desc
*desc
)
141 struct ccp_dma_cmd
*cmd
;
144 cmd
= list_first_entry(&desc
->pending
, struct ccp_dma_cmd
, entry
);
145 list_move(&cmd
->entry
, &desc
->active
);
147 dev_dbg(desc
->ccp
->dev
, "%s - tx %d, cmd=%p\n", __func__
,
148 desc
->tx_desc
.cookie
, cmd
);
150 ret
= ccp_enqueue_cmd(&cmd
->ccp_cmd
);
151 if (!ret
|| (ret
== -EINPROGRESS
) || (ret
== -EBUSY
))
154 dev_dbg(desc
->ccp
->dev
, "%s - error: ret=%d, tx %d, cmd=%p\n", __func__
,
155 ret
, desc
->tx_desc
.cookie
, cmd
);
160 static void ccp_free_active_cmd(struct ccp_dma_desc
*desc
)
162 struct ccp_dma_cmd
*cmd
;
164 cmd
= list_first_entry_or_null(&desc
->active
, struct ccp_dma_cmd
,
169 dev_dbg(desc
->ccp
->dev
, "%s - freeing tx %d cmd=%p\n",
170 __func__
, desc
->tx_desc
.cookie
, cmd
);
172 list_del(&cmd
->entry
);
173 kmem_cache_free(desc
->ccp
->dma_cmd_cache
, cmd
);
176 static struct ccp_dma_desc
*__ccp_next_dma_desc(struct ccp_dma_chan
*chan
,
177 struct ccp_dma_desc
*desc
)
179 /* Move current DMA descriptor to the complete list */
181 list_move(&desc
->entry
, &chan
->complete
);
183 /* Get the next DMA descriptor on the active list */
184 desc
= list_first_entry_or_null(&chan
->active
, struct ccp_dma_desc
,
190 static struct ccp_dma_desc
*ccp_handle_active_desc(struct ccp_dma_chan
*chan
,
191 struct ccp_dma_desc
*desc
)
193 struct dma_async_tx_descriptor
*tx_desc
;
196 /* Loop over descriptors until one is found with commands */
199 /* Remove the DMA command from the list and free it */
200 ccp_free_active_cmd(desc
);
202 if (!list_empty(&desc
->pending
)) {
203 /* No errors, keep going */
204 if (desc
->status
!= DMA_ERROR
)
207 /* Error, free remaining commands and move on */
208 ccp_free_cmd_resources(desc
->ccp
,
212 tx_desc
= &desc
->tx_desc
;
217 spin_lock_irqsave(&chan
->lock
, flags
);
220 if (desc
->status
!= DMA_ERROR
)
221 desc
->status
= DMA_COMPLETE
;
223 dev_dbg(desc
->ccp
->dev
,
224 "%s - tx %d complete, status=%u\n", __func__
,
225 desc
->tx_desc
.cookie
, desc
->status
);
227 dma_cookie_complete(tx_desc
);
228 dma_descriptor_unmap(tx_desc
);
231 desc
= __ccp_next_dma_desc(chan
, desc
);
233 spin_unlock_irqrestore(&chan
->lock
, flags
);
236 dmaengine_desc_get_callback_invoke(tx_desc
, NULL
);
238 dma_run_dependencies(tx_desc
);
245 static struct ccp_dma_desc
*__ccp_pending_to_active(struct ccp_dma_chan
*chan
)
247 struct ccp_dma_desc
*desc
;
249 if (list_empty(&chan
->pending
))
252 desc
= list_empty(&chan
->active
)
253 ? list_first_entry(&chan
->pending
, struct ccp_dma_desc
, entry
)
256 list_splice_tail_init(&chan
->pending
, &chan
->active
);
261 static void ccp_cmd_callback(void *data
, int err
)
263 struct ccp_dma_desc
*desc
= data
;
264 struct ccp_dma_chan
*chan
;
267 if (err
== -EINPROGRESS
)
270 chan
= container_of(desc
->tx_desc
.chan
, struct ccp_dma_chan
,
273 dev_dbg(chan
->ccp
->dev
, "%s - tx %d callback, err=%d\n",
274 __func__
, desc
->tx_desc
.cookie
, err
);
277 desc
->status
= DMA_ERROR
;
280 /* Check for DMA descriptor completion */
281 desc
= ccp_handle_active_desc(chan
, desc
);
283 /* Don't submit cmd if no descriptor or DMA is paused */
284 if (!desc
|| (chan
->status
== DMA_PAUSED
))
287 ret
= ccp_issue_next_cmd(desc
);
291 desc
->status
= DMA_ERROR
;
294 tasklet_schedule(&chan
->cleanup_tasklet
);
297 static dma_cookie_t
ccp_tx_submit(struct dma_async_tx_descriptor
*tx_desc
)
299 struct ccp_dma_desc
*desc
= container_of(tx_desc
, struct ccp_dma_desc
,
301 struct ccp_dma_chan
*chan
;
305 chan
= container_of(tx_desc
->chan
, struct ccp_dma_chan
, dma_chan
);
307 spin_lock_irqsave(&chan
->lock
, flags
);
309 cookie
= dma_cookie_assign(tx_desc
);
310 list_del(&desc
->entry
);
311 list_add_tail(&desc
->entry
, &chan
->pending
);
313 spin_unlock_irqrestore(&chan
->lock
, flags
);
315 dev_dbg(chan
->ccp
->dev
, "%s - added tx descriptor %d to pending list\n",
321 static struct ccp_dma_cmd
*ccp_alloc_dma_cmd(struct ccp_dma_chan
*chan
)
323 struct ccp_dma_cmd
*cmd
;
325 cmd
= kmem_cache_alloc(chan
->ccp
->dma_cmd_cache
, GFP_NOWAIT
);
327 memset(cmd
, 0, sizeof(*cmd
));
332 static struct ccp_dma_desc
*ccp_alloc_dma_desc(struct ccp_dma_chan
*chan
,
335 struct ccp_dma_desc
*desc
;
337 desc
= kmem_cache_zalloc(chan
->ccp
->dma_desc_cache
, GFP_NOWAIT
);
341 dma_async_tx_descriptor_init(&desc
->tx_desc
, &chan
->dma_chan
);
342 desc
->tx_desc
.flags
= flags
;
343 desc
->tx_desc
.tx_submit
= ccp_tx_submit
;
344 desc
->ccp
= chan
->ccp
;
345 INIT_LIST_HEAD(&desc
->entry
);
346 INIT_LIST_HEAD(&desc
->pending
);
347 INIT_LIST_HEAD(&desc
->active
);
348 desc
->status
= DMA_IN_PROGRESS
;
353 static struct ccp_dma_desc
*ccp_create_desc(struct dma_chan
*dma_chan
,
354 struct scatterlist
*dst_sg
,
355 unsigned int dst_nents
,
356 struct scatterlist
*src_sg
,
357 unsigned int src_nents
,
360 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
362 struct ccp_device
*ccp
= chan
->ccp
;
363 struct ccp_dma_desc
*desc
;
364 struct ccp_dma_cmd
*cmd
;
365 struct ccp_cmd
*ccp_cmd
;
366 struct ccp_passthru_nomap_engine
*ccp_pt
;
367 unsigned int src_offset
, src_len
;
368 unsigned int dst_offset
, dst_len
;
370 unsigned long sflags
;
373 if (!dst_sg
|| !src_sg
)
376 if (!dst_nents
|| !src_nents
)
379 desc
= ccp_alloc_dma_desc(chan
, flags
);
385 src_len
= sg_dma_len(src_sg
);
388 dst_len
= sg_dma_len(dst_sg
);
397 src_sg
= sg_next(src_sg
);
401 src_len
= sg_dma_len(src_sg
);
411 dst_sg
= sg_next(dst_sg
);
415 dst_len
= sg_dma_len(dst_sg
);
420 len
= min(dst_len
, src_len
);
422 cmd
= ccp_alloc_dma_cmd(chan
);
426 ccp_cmd
= &cmd
->ccp_cmd
;
427 ccp_cmd
->ccp
= chan
->ccp
;
428 ccp_pt
= &ccp_cmd
->u
.passthru_nomap
;
429 ccp_cmd
->flags
= CCP_CMD_MAY_BACKLOG
;
430 ccp_cmd
->flags
|= CCP_CMD_PASSTHRU_NO_DMA_MAP
;
431 ccp_cmd
->engine
= CCP_ENGINE_PASSTHRU
;
432 ccp_pt
->bit_mod
= CCP_PASSTHRU_BITWISE_NOOP
;
433 ccp_pt
->byte_swap
= CCP_PASSTHRU_BYTESWAP_NOOP
;
434 ccp_pt
->src_dma
= sg_dma_address(src_sg
) + src_offset
;
435 ccp_pt
->dst_dma
= sg_dma_address(dst_sg
) + dst_offset
;
436 ccp_pt
->src_len
= len
;
438 ccp_cmd
->callback
= ccp_cmd_callback
;
439 ccp_cmd
->data
= desc
;
441 list_add_tail(&cmd
->entry
, &desc
->pending
);
444 "%s - cmd=%p, src=%pad, dst=%pad, len=%llu\n", __func__
,
445 cmd
, &ccp_pt
->src_dma
,
446 &ccp_pt
->dst_dma
, ccp_pt
->src_len
);
457 desc
->len
= total_len
;
459 if (list_empty(&desc
->pending
))
462 dev_dbg(ccp
->dev
, "%s - desc=%p\n", __func__
, desc
);
464 spin_lock_irqsave(&chan
->lock
, sflags
);
466 list_add_tail(&desc
->entry
, &chan
->created
);
468 spin_unlock_irqrestore(&chan
->lock
, sflags
);
473 ccp_free_cmd_resources(ccp
, &desc
->pending
);
474 kmem_cache_free(ccp
->dma_desc_cache
, desc
);
479 static struct dma_async_tx_descriptor
*ccp_prep_dma_memcpy(
480 struct dma_chan
*dma_chan
, dma_addr_t dst
, dma_addr_t src
, size_t len
,
483 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
485 struct ccp_dma_desc
*desc
;
486 struct scatterlist dst_sg
, src_sg
;
488 dev_dbg(chan
->ccp
->dev
,
489 "%s - src=%pad, dst=%pad, len=%zu, flags=%#lx\n",
490 __func__
, &src
, &dst
, len
, flags
);
492 sg_init_table(&dst_sg
, 1);
493 sg_dma_address(&dst_sg
) = dst
;
494 sg_dma_len(&dst_sg
) = len
;
496 sg_init_table(&src_sg
, 1);
497 sg_dma_address(&src_sg
) = src
;
498 sg_dma_len(&src_sg
) = len
;
500 desc
= ccp_create_desc(dma_chan
, &dst_sg
, 1, &src_sg
, 1, flags
);
504 return &desc
->tx_desc
;
507 static struct dma_async_tx_descriptor
*ccp_prep_dma_interrupt(
508 struct dma_chan
*dma_chan
, unsigned long flags
)
510 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
512 struct ccp_dma_desc
*desc
;
514 desc
= ccp_alloc_dma_desc(chan
, flags
);
518 return &desc
->tx_desc
;
521 static void ccp_issue_pending(struct dma_chan
*dma_chan
)
523 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
525 struct ccp_dma_desc
*desc
;
528 dev_dbg(chan
->ccp
->dev
, "%s\n", __func__
);
530 spin_lock_irqsave(&chan
->lock
, flags
);
532 desc
= __ccp_pending_to_active(chan
);
534 spin_unlock_irqrestore(&chan
->lock
, flags
);
536 /* If there was nothing active, start processing */
538 ccp_cmd_callback(desc
, 0);
541 static enum dma_status
ccp_tx_status(struct dma_chan
*dma_chan
,
543 struct dma_tx_state
*state
)
545 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
547 struct ccp_dma_desc
*desc
;
551 if (chan
->status
== DMA_PAUSED
) {
556 ret
= dma_cookie_status(dma_chan
, cookie
, state
);
557 if (ret
== DMA_COMPLETE
) {
558 spin_lock_irqsave(&chan
->lock
, flags
);
560 /* Get status from complete chain, if still there */
561 list_for_each_entry(desc
, &chan
->complete
, entry
) {
562 if (desc
->tx_desc
.cookie
!= cookie
)
569 spin_unlock_irqrestore(&chan
->lock
, flags
);
573 dev_dbg(chan
->ccp
->dev
, "%s - %u\n", __func__
, ret
);
578 static int ccp_pause(struct dma_chan
*dma_chan
)
580 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
583 chan
->status
= DMA_PAUSED
;
585 /*TODO: Wait for active DMA to complete before returning? */
590 static int ccp_resume(struct dma_chan
*dma_chan
)
592 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
594 struct ccp_dma_desc
*desc
;
597 spin_lock_irqsave(&chan
->lock
, flags
);
599 desc
= list_first_entry_or_null(&chan
->active
, struct ccp_dma_desc
,
602 spin_unlock_irqrestore(&chan
->lock
, flags
);
604 /* Indicate the channel is running again */
605 chan
->status
= DMA_IN_PROGRESS
;
607 /* If there was something active, re-start */
609 ccp_cmd_callback(desc
, 0);
614 static int ccp_terminate_all(struct dma_chan
*dma_chan
)
616 struct ccp_dma_chan
*chan
= container_of(dma_chan
, struct ccp_dma_chan
,
620 dev_dbg(chan
->ccp
->dev
, "%s\n", __func__
);
622 /*TODO: Wait for active DMA to complete before continuing */
624 spin_lock_irqsave(&chan
->lock
, flags
);
626 /*TODO: Purge the complete list? */
627 ccp_free_desc_resources(chan
->ccp
, &chan
->active
);
628 ccp_free_desc_resources(chan
->ccp
, &chan
->pending
);
629 ccp_free_desc_resources(chan
->ccp
, &chan
->created
);
631 spin_unlock_irqrestore(&chan
->lock
, flags
);
636 int ccp_dmaengine_register(struct ccp_device
*ccp
)
638 struct ccp_dma_chan
*chan
;
639 struct dma_device
*dma_dev
= &ccp
->dma_dev
;
640 struct dma_chan
*dma_chan
;
641 char *dma_cmd_cache_name
;
642 char *dma_desc_cache_name
;
649 ccp
->ccp_dma_chan
= devm_kcalloc(ccp
->dev
, ccp
->cmd_q_count
,
650 sizeof(*(ccp
->ccp_dma_chan
)),
652 if (!ccp
->ccp_dma_chan
)
655 dma_cmd_cache_name
= devm_kasprintf(ccp
->dev
, GFP_KERNEL
,
656 "%s-dmaengine-cmd-cache",
658 if (!dma_cmd_cache_name
)
661 ccp
->dma_cmd_cache
= kmem_cache_create(dma_cmd_cache_name
,
662 sizeof(struct ccp_dma_cmd
),
664 SLAB_HWCACHE_ALIGN
, NULL
);
665 if (!ccp
->dma_cmd_cache
)
668 dma_desc_cache_name
= devm_kasprintf(ccp
->dev
, GFP_KERNEL
,
669 "%s-dmaengine-desc-cache",
671 if (!dma_desc_cache_name
) {
676 ccp
->dma_desc_cache
= kmem_cache_create(dma_desc_cache_name
,
677 sizeof(struct ccp_dma_desc
),
679 SLAB_HWCACHE_ALIGN
, NULL
);
680 if (!ccp
->dma_desc_cache
) {
685 dma_dev
->dev
= ccp
->dev
;
686 dma_dev
->src_addr_widths
= CCP_DMA_WIDTH(dma_get_mask(ccp
->dev
));
687 dma_dev
->dst_addr_widths
= CCP_DMA_WIDTH(dma_get_mask(ccp
->dev
));
688 dma_dev
->directions
= DMA_MEM_TO_MEM
;
689 dma_dev
->residue_granularity
= DMA_RESIDUE_GRANULARITY_DESCRIPTOR
;
690 dma_cap_set(DMA_MEMCPY
, dma_dev
->cap_mask
);
691 dma_cap_set(DMA_INTERRUPT
, dma_dev
->cap_mask
);
693 /* The DMA channels for this device can be set to public or private,
694 * and overridden by the module parameter dma_chan_attr.
695 * Default: according to the value in vdata (dma_chan_attr=0)
696 * dma_chan_attr=0x1: all channels private (override vdata)
697 * dma_chan_attr=0x2: all channels public (override vdata)
699 if (ccp_get_dma_chan_attr(ccp
) == DMA_PRIVATE
)
700 dma_cap_set(DMA_PRIVATE
, dma_dev
->cap_mask
);
702 INIT_LIST_HEAD(&dma_dev
->channels
);
703 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
704 chan
= ccp
->ccp_dma_chan
+ i
;
705 dma_chan
= &chan
->dma_chan
;
709 spin_lock_init(&chan
->lock
);
710 INIT_LIST_HEAD(&chan
->created
);
711 INIT_LIST_HEAD(&chan
->pending
);
712 INIT_LIST_HEAD(&chan
->active
);
713 INIT_LIST_HEAD(&chan
->complete
);
715 tasklet_init(&chan
->cleanup_tasklet
, ccp_do_cleanup
,
716 (unsigned long)chan
);
718 dma_chan
->device
= dma_dev
;
719 dma_cookie_init(dma_chan
);
721 list_add_tail(&dma_chan
->device_node
, &dma_dev
->channels
);
724 dma_dev
->device_free_chan_resources
= ccp_free_chan_resources
;
725 dma_dev
->device_prep_dma_memcpy
= ccp_prep_dma_memcpy
;
726 dma_dev
->device_prep_dma_interrupt
= ccp_prep_dma_interrupt
;
727 dma_dev
->device_issue_pending
= ccp_issue_pending
;
728 dma_dev
->device_tx_status
= ccp_tx_status
;
729 dma_dev
->device_pause
= ccp_pause
;
730 dma_dev
->device_resume
= ccp_resume
;
731 dma_dev
->device_terminate_all
= ccp_terminate_all
;
733 ret
= dma_async_device_register(dma_dev
);
740 kmem_cache_destroy(ccp
->dma_desc_cache
);
743 kmem_cache_destroy(ccp
->dma_cmd_cache
);
748 void ccp_dmaengine_unregister(struct ccp_device
*ccp
)
750 struct dma_device
*dma_dev
= &ccp
->dma_dev
;
755 dma_async_device_unregister(dma_dev
);
757 kmem_cache_destroy(ccp
->dma_desc_cache
);
758 kmem_cache_destroy(ccp
->dma_cmd_cache
);