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