1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018 Etnaviv Project
6 #include <linux/dma-mapping.h>
8 #include <drm/drm_mm.h>
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"
16 #define SUBALLOC_SIZE SZ_512K
17 #define SUBALLOC_GRANULE SZ_4K
18 #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
20 struct etnaviv_cmdbuf_suballoc {
21 /* suballocated dma buffer properties */
26 /* allocation management */
28 DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
30 wait_queue_head_t free_event;
33 struct etnaviv_cmdbuf_suballoc *
34 etnaviv_cmdbuf_suballoc_new(struct device *dev)
36 struct etnaviv_cmdbuf_suballoc *suballoc;
39 suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
41 return ERR_PTR(-ENOMEM);
44 mutex_init(&suballoc->lock);
45 init_waitqueue_head(&suballoc->free_event);
47 BUILD_BUG_ON(ETNAVIV_SOFTPIN_START_ADDRESS < SUBALLOC_SIZE);
48 suballoc->vaddr = dma_alloc_wc(dev, SUBALLOC_SIZE,
49 &suballoc->paddr, GFP_KERNEL);
50 if (!suballoc->vaddr) {
63 int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
64 struct etnaviv_iommu_context *context,
65 struct etnaviv_vram_mapping *mapping,
68 return etnaviv_iommu_get_suballoc_va(context, mapping, memory_base,
69 suballoc->paddr, SUBALLOC_SIZE);
72 void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
73 struct etnaviv_vram_mapping *mapping)
75 etnaviv_iommu_put_suballoc_va(context, mapping);
78 void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
80 dma_free_wc(suballoc->dev, SUBALLOC_SIZE, suballoc->vaddr,
85 int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
86 struct etnaviv_cmdbuf *cmdbuf, u32 size)
88 int granule_offs, order, ret;
90 cmdbuf->suballoc = suballoc;
93 order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
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));
105 dev_err(suballoc->dev,
106 "Timeout waiting for cmdbuf space\n");
111 mutex_unlock(&suballoc->lock);
112 cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
113 cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
118 void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
120 struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
121 int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
127 mutex_lock(&suballoc->lock);
128 bitmap_release_region(suballoc->granule_map,
129 cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
131 suballoc->free_space = 1;
132 mutex_unlock(&suballoc->lock);
133 wake_up_all(&suballoc->free_event);
136 u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf,
137 struct etnaviv_vram_mapping *mapping)
139 return mapping->iova + buf->suballoc_offset;
142 dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
144 return buf->suballoc->paddr + buf->suballoc_offset;