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