2 * Copyright (C) 2017 Etnaviv Project
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.
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
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/>.
17 #include <drm/drm_mm.h>
19 #include "etnaviv_cmdbuf.h"
20 #include "etnaviv_gpu.h"
21 #include "etnaviv_mmu.h"
23 #define SUBALLOC_SIZE SZ_256K
24 #define SUBALLOC_GRANULE SZ_4K
25 #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
27 struct etnaviv_cmdbuf_suballoc {
28 /* suballocated dma buffer properties */
29 struct etnaviv_gpu *gpu;
35 struct drm_mm_node vram_node; /* only used on MMUv2 */
37 /* allocation management */
39 DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
41 wait_queue_head_t free_event;
44 struct etnaviv_cmdbuf_suballoc *
45 etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
47 struct etnaviv_cmdbuf_suballoc *suballoc;
50 suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
52 return ERR_PTR(-ENOMEM);
55 mutex_init(&suballoc->lock);
56 init_waitqueue_head(&suballoc->free_event);
58 suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
59 &suballoc->paddr, GFP_KERNEL);
63 ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
64 &suballoc->vram_node, SUBALLOC_SIZE,
72 dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
79 void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
81 etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
82 SUBALLOC_SIZE, suballoc->iova);
83 dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
88 struct etnaviv_cmdbuf *
89 etnaviv_cmdbuf_new(struct etnaviv_cmdbuf_suballoc *suballoc, u32 size,
92 struct etnaviv_cmdbuf *cmdbuf;
93 size_t sz = size_vstruct(nr_bos, sizeof(cmdbuf->bo_map[0]),
95 int granule_offs, order, ret;
97 cmdbuf = kzalloc(sz, GFP_KERNEL);
101 cmdbuf->suballoc = suballoc;
104 order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
106 mutex_lock(&suballoc->lock);
107 granule_offs = bitmap_find_free_region(suballoc->granule_map,
108 SUBALLOC_GRANULES, order);
109 if (granule_offs < 0) {
110 suballoc->free_space = 0;
111 mutex_unlock(&suballoc->lock);
112 ret = wait_event_interruptible_timeout(suballoc->free_event,
113 suballoc->free_space,
114 msecs_to_jiffies(10 * 1000));
116 dev_err(suballoc->gpu->dev,
117 "Timeout waiting for cmdbuf space\n");
122 mutex_unlock(&suballoc->lock);
123 cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
124 cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
129 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
131 struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
132 int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
135 mutex_lock(&suballoc->lock);
136 bitmap_release_region(suballoc->granule_map,
137 cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
139 suballoc->free_space = 1;
140 mutex_unlock(&suballoc->lock);
141 wake_up_all(&suballoc->free_event);
145 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
147 return buf->suballoc->iova + buf->suballoc_offset;
150 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
152 return buf->suballoc->paddr + buf->suballoc_offset;