1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017-2018 Etnaviv Project
4  */
5 
6 #include <linux/dma-mapping.h>
7 
8 #include <drm/drm_mm.h>
9 
10 #include "etnaviv_cmdbuf.h"
11 #include "etnaviv_gpu.h"
12 #include "etnaviv_mmu.h"
13 #include "etnaviv_perfmon.h"
14 
15 #define SUBALLOC_SIZE		SZ_256K
16 #define SUBALLOC_GRANULE	SZ_4K
17 #define SUBALLOC_GRANULES	(SUBALLOC_SIZE / SUBALLOC_GRANULE)
18 
19 struct etnaviv_cmdbuf_suballoc {
20 	/* suballocated dma buffer properties */
21 	struct etnaviv_gpu *gpu;
22 	void *vaddr;
23 	dma_addr_t paddr;
24 
25 	/* GPU mapping */
26 	u32 iova;
27 	struct drm_mm_node vram_node; /* only used on MMUv2 */
28 
29 	/* allocation management */
30 	struct mutex lock;
31 	DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
32 	int free_space;
33 	wait_queue_head_t free_event;
34 };
35 
36 struct etnaviv_cmdbuf_suballoc *
37 etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
38 {
39 	struct etnaviv_cmdbuf_suballoc *suballoc;
40 	int ret;
41 
42 	suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
43 	if (!suballoc)
44 		return ERR_PTR(-ENOMEM);
45 
46 	suballoc->gpu = gpu;
47 	mutex_init(&suballoc->lock);
48 	init_waitqueue_head(&suballoc->free_event);
49 
50 	suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
51 				       &suballoc->paddr, GFP_KERNEL);
52 	if (!suballoc->vaddr) {
53 		ret = -ENOMEM;
54 		goto free_suballoc;
55 	}
56 
57 	ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
58 					    &suballoc->vram_node, SUBALLOC_SIZE,
59 					    &suballoc->iova);
60 	if (ret)
61 		goto free_dma;
62 
63 	return suballoc;
64 
65 free_dma:
66 	dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
67 free_suballoc:
68 	kfree(suballoc);
69 
70 	return ERR_PTR(ret);
71 }
72 
73 void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
74 {
75 	etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
76 				      SUBALLOC_SIZE, suballoc->iova);
77 	dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
78 		    suballoc->paddr);
79 	kfree(suballoc);
80 }
81 
82 int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
83 			struct etnaviv_cmdbuf *cmdbuf, u32 size)
84 {
85 	int granule_offs, order, ret;
86 
87 	cmdbuf->suballoc = suballoc;
88 	cmdbuf->size = size;
89 
90 	order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
91 retry:
92 	mutex_lock(&suballoc->lock);
93 	granule_offs = bitmap_find_free_region(suballoc->granule_map,
94 					SUBALLOC_GRANULES, order);
95 	if (granule_offs < 0) {
96 		suballoc->free_space = 0;
97 		mutex_unlock(&suballoc->lock);
98 		ret = wait_event_interruptible_timeout(suballoc->free_event,
99 						       suballoc->free_space,
100 						       msecs_to_jiffies(10 * 1000));
101 		if (!ret) {
102 			dev_err(suballoc->gpu->dev,
103 				"Timeout waiting for cmdbuf space\n");
104 			return -ETIMEDOUT;
105 		}
106 		goto retry;
107 	}
108 	mutex_unlock(&suballoc->lock);
109 	cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
110 	cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
111 
112 	return 0;
113 }
114 
115 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
116 {
117 	struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
118 	int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
119 				 SUBALLOC_GRANULE);
120 
121 	mutex_lock(&suballoc->lock);
122 	bitmap_release_region(suballoc->granule_map,
123 			      cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
124 			      order);
125 	suballoc->free_space = 1;
126 	mutex_unlock(&suballoc->lock);
127 	wake_up_all(&suballoc->free_event);
128 }
129 
130 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
131 {
132 	return buf->suballoc->iova + buf->suballoc_offset;
133 }
134 
135 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
136 {
137 	return buf->suballoc->paddr + buf->suballoc_offset;
138 }
139