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