1 /*
2  * Copyright (C) 2017 Etnaviv Project
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "etnaviv_cmdbuf.h"
18 #include "etnaviv_gpu.h"
19 #include "etnaviv_mmu.h"
20 
21 struct etnaviv_cmdbuf *etnaviv_cmdbuf_new(struct etnaviv_gpu *gpu, u32 size,
22 	size_t nr_bos)
23 {
24 	struct etnaviv_cmdbuf *cmdbuf;
25 	size_t sz = size_vstruct(nr_bos, sizeof(cmdbuf->bo_map[0]),
26 				 sizeof(*cmdbuf));
27 
28 	cmdbuf = kzalloc(sz, GFP_KERNEL);
29 	if (!cmdbuf)
30 		return NULL;
31 
32 	if (gpu->mmu->version == ETNAVIV_IOMMU_V2)
33 		size = ALIGN(size, SZ_4K);
34 
35 	cmdbuf->vaddr = dma_alloc_wc(gpu->dev, size, &cmdbuf->paddr,
36 				     GFP_KERNEL);
37 	if (!cmdbuf->vaddr) {
38 		kfree(cmdbuf);
39 		return NULL;
40 	}
41 
42 	cmdbuf->gpu = gpu;
43 	cmdbuf->size = size;
44 
45 	return cmdbuf;
46 }
47 
48 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
49 {
50 	etnaviv_iommu_put_cmdbuf_va(cmdbuf->gpu, cmdbuf);
51 	dma_free_wc(cmdbuf->gpu->dev, cmdbuf->size, cmdbuf->vaddr,
52 		    cmdbuf->paddr);
53 	kfree(cmdbuf);
54 }
55 
56 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
57 {
58 	return etnaviv_iommu_get_cmdbuf_va(buf->gpu, buf);
59 }
60 
61 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
62 {
63 	return buf->paddr;
64 }
65