1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
5  */
6 
7 #include "delta.h"
8 #include "delta-mem.h"
9 
10 int hw_alloc(struct delta_ctx *ctx, u32 size, const char *name,
11 	     struct delta_buf *buf)
12 {
13 	struct delta_dev *delta = ctx->dev;
14 	dma_addr_t dma_addr;
15 	void *addr;
16 	unsigned long attrs = DMA_ATTR_WRITE_COMBINE;
17 
18 	addr = dma_alloc_attrs(delta->dev, size, &dma_addr,
19 			       GFP_KERNEL | __GFP_NOWARN, attrs);
20 	if (!addr) {
21 		dev_err(delta->dev,
22 			"%s hw_alloc:dma_alloc_coherent failed for %s (size=%d)\n",
23 			ctx->name, name, size);
24 		ctx->sys_errors++;
25 		return -ENOMEM;
26 	}
27 
28 	buf->size = size;
29 	buf->paddr = dma_addr;
30 	buf->vaddr = addr;
31 	buf->name = name;
32 	buf->attrs = attrs;
33 
34 	dev_dbg(delta->dev,
35 		"%s allocate %d bytes of HW memory @(virt=0x%p, phy=0x%pad): %s\n",
36 		ctx->name, size, buf->vaddr, &buf->paddr, buf->name);
37 
38 	return 0;
39 }
40 
41 void hw_free(struct delta_ctx *ctx, struct delta_buf *buf)
42 {
43 	struct delta_dev *delta = ctx->dev;
44 
45 	dev_dbg(delta->dev,
46 		"%s     free %d bytes of HW memory @(virt=0x%p, phy=0x%pad): %s\n",
47 		ctx->name, buf->size, buf->vaddr, &buf->paddr, buf->name);
48 
49 	dma_free_attrs(delta->dev, buf->size,
50 		       buf->vaddr, buf->paddr, buf->attrs);
51 }
52