xref: /linux/drivers/gpu/drm/etnaviv/etnaviv_cmdbuf.c (revision 866c9b94ef968445c52214b3748ecc52a8491bca)
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 <drm/drm_mm.h>
18 
19 #include "etnaviv_cmdbuf.h"
20 #include "etnaviv_gpu.h"
21 #include "etnaviv_mmu.h"
22 #include "etnaviv_perfmon.h"
23 
24 #define SUBALLOC_SIZE		SZ_256K
25 #define SUBALLOC_GRANULE	SZ_4K
26 #define SUBALLOC_GRANULES	(SUBALLOC_SIZE / SUBALLOC_GRANULE)
27 
28 struct etnaviv_cmdbuf_suballoc {
29 	/* suballocated dma buffer properties */
30 	struct etnaviv_gpu *gpu;
31 	void *vaddr;
32 	dma_addr_t paddr;
33 
34 	/* GPU mapping */
35 	u32 iova;
36 	struct drm_mm_node vram_node; /* only used on MMUv2 */
37 
38 	/* allocation management */
39 	struct mutex lock;
40 	DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
41 	int free_space;
42 	wait_queue_head_t free_event;
43 };
44 
45 struct etnaviv_cmdbuf_suballoc *
46 etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
47 {
48 	struct etnaviv_cmdbuf_suballoc *suballoc;
49 	int ret;
50 
51 	suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
52 	if (!suballoc)
53 		return ERR_PTR(-ENOMEM);
54 
55 	suballoc->gpu = gpu;
56 	mutex_init(&suballoc->lock);
57 	init_waitqueue_head(&suballoc->free_event);
58 
59 	suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
60 				       &suballoc->paddr, GFP_KERNEL);
61 	if (!suballoc->vaddr)
62 		goto free_suballoc;
63 
64 	ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
65 					    &suballoc->vram_node, SUBALLOC_SIZE,
66 					    &suballoc->iova);
67 	if (ret)
68 		goto free_dma;
69 
70 	return suballoc;
71 
72 free_dma:
73 	dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
74 free_suballoc:
75 	kfree(suballoc);
76 
77 	return NULL;
78 }
79 
80 void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
81 {
82 	etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
83 				      SUBALLOC_SIZE, suballoc->iova);
84 	dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
85 		    suballoc->paddr);
86 	kfree(suballoc);
87 }
88 
89 struct etnaviv_cmdbuf *
90 etnaviv_cmdbuf_new(struct etnaviv_cmdbuf_suballoc *suballoc, u32 size,
91 		   size_t nr_bos, size_t nr_pmrs)
92 {
93 	struct etnaviv_cmdbuf *cmdbuf;
94 	struct etnaviv_perfmon_request *pmrs;
95 	size_t sz = size_vstruct(nr_bos, sizeof(cmdbuf->bo_map[0]),
96 				 sizeof(*cmdbuf));
97 	int granule_offs, order, ret;
98 
99 	cmdbuf = kzalloc(sz, GFP_KERNEL);
100 	if (!cmdbuf)
101 		return NULL;
102 
103 	sz = sizeof(*pmrs) * nr_pmrs;
104 	pmrs = kzalloc(sz, GFP_KERNEL);
105 	if (!pmrs)
106 		goto out_free_cmdbuf;
107 
108 	cmdbuf->pmrs = pmrs;
109 	cmdbuf->suballoc = suballoc;
110 	cmdbuf->size = size;
111 
112 	order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
113 retry:
114 	mutex_lock(&suballoc->lock);
115 	granule_offs = bitmap_find_free_region(suballoc->granule_map,
116 					SUBALLOC_GRANULES, order);
117 	if (granule_offs < 0) {
118 		suballoc->free_space = 0;
119 		mutex_unlock(&suballoc->lock);
120 		ret = wait_event_interruptible_timeout(suballoc->free_event,
121 						       suballoc->free_space,
122 						       msecs_to_jiffies(10 * 1000));
123 		if (!ret) {
124 			dev_err(suballoc->gpu->dev,
125 				"Timeout waiting for cmdbuf space\n");
126 			return NULL;
127 		}
128 		goto retry;
129 	}
130 	mutex_unlock(&suballoc->lock);
131 	cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
132 	cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
133 
134 	return cmdbuf;
135 
136 out_free_cmdbuf:
137 	kfree(cmdbuf);
138 	return NULL;
139 }
140 
141 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
142 {
143 	struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
144 	int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
145 				 SUBALLOC_GRANULE);
146 
147 	mutex_lock(&suballoc->lock);
148 	bitmap_release_region(suballoc->granule_map,
149 			      cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
150 			      order);
151 	suballoc->free_space = 1;
152 	mutex_unlock(&suballoc->lock);
153 	wake_up_all(&suballoc->free_event);
154 	kfree(cmdbuf->pmrs);
155 	kfree(cmdbuf);
156 }
157 
158 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
159 {
160 	return buf->suballoc->iova + buf->suballoc_offset;
161 }
162 
163 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
164 {
165 	return buf->suballoc->paddr + buf->suballoc_offset;
166 }
167