GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / gpu / drm / msm / msm_submitqueue.c
1 /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  */
13
14 #include <linux/kref.h>
15 #include "msm_gpu.h"
16
17 void msm_submitqueue_destroy(struct kref *kref)
18 {
19         struct msm_gpu_submitqueue *queue = container_of(kref,
20                 struct msm_gpu_submitqueue, ref);
21
22         kfree(queue);
23 }
24
25 struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
26                 u32 id)
27 {
28         struct msm_gpu_submitqueue *entry;
29
30         if (!ctx)
31                 return NULL;
32
33         read_lock(&ctx->queuelock);
34
35         list_for_each_entry(entry, &ctx->submitqueues, node) {
36                 if (entry->id == id) {
37                         kref_get(&entry->ref);
38                         read_unlock(&ctx->queuelock);
39
40                         return entry;
41                 }
42         }
43
44         read_unlock(&ctx->queuelock);
45         return NULL;
46 }
47
48 void msm_submitqueue_close(struct msm_file_private *ctx)
49 {
50         struct msm_gpu_submitqueue *entry, *tmp;
51
52         if (!ctx)
53                 return;
54
55         /*
56          * No lock needed in close and there won't
57          * be any more user ioctls coming our way
58          */
59         list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node)
60                 msm_submitqueue_put(entry);
61 }
62
63 int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
64                 u32 prio, u32 flags, u32 *id)
65 {
66         struct msm_drm_private *priv = drm->dev_private;
67         struct msm_gpu_submitqueue *queue;
68
69         if (!ctx)
70                 return -ENODEV;
71
72         queue = kzalloc(sizeof(*queue), GFP_KERNEL);
73
74         if (!queue)
75                 return -ENOMEM;
76
77         kref_init(&queue->ref);
78         queue->flags = flags;
79
80         if (priv->gpu) {
81                 if (prio >= priv->gpu->nr_rings) {
82                         kfree(queue);
83                         return -EINVAL;
84                 }
85
86                 queue->prio = prio;
87         }
88
89         write_lock(&ctx->queuelock);
90
91         queue->id = ctx->queueid++;
92
93         if (id)
94                 *id = queue->id;
95
96         list_add_tail(&queue->node, &ctx->submitqueues);
97
98         write_unlock(&ctx->queuelock);
99
100         return 0;
101 }
102
103 int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
104 {
105         struct msm_drm_private *priv = drm->dev_private;
106         int default_prio;
107
108         if (!ctx)
109                 return 0;
110
111         /*
112          * Select priority 2 as the "default priority" unless nr_rings is less
113          * than 2 and then pick the lowest pirority
114          */
115         default_prio = priv->gpu ?
116                 clamp_t(uint32_t, 2, 0, priv->gpu->nr_rings - 1) : 0;
117
118         INIT_LIST_HEAD(&ctx->submitqueues);
119
120         rwlock_init(&ctx->queuelock);
121
122         return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
123 }
124
125 int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
126 {
127         struct msm_gpu_submitqueue *entry;
128
129         if (!ctx)
130                 return 0;
131
132         /*
133          * id 0 is the "default" queue and can't be destroyed
134          * by the user
135          */
136         if (!id)
137                 return -ENOENT;
138
139         write_lock(&ctx->queuelock);
140
141         list_for_each_entry(entry, &ctx->submitqueues, node) {
142                 if (entry->id == id) {
143                         list_del(&entry->node);
144                         write_unlock(&ctx->queuelock);
145
146                         msm_submitqueue_put(entry);
147                         return 0;
148                 }
149         }
150
151         write_unlock(&ctx->queuelock);
152         return -ENOENT;
153 }
154