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