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