]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/gpu/nvgpu/gk20a/channel_gk20a.h
gpu: nvgpu: fix use-after-free in case of error notifier
[sojka/nv-tegra/linux-3.10.git] / drivers / gpu / nvgpu / gk20a / channel_gk20a.h
1 /*
2  * GK20A graphics channel
3  *
4  * Copyright (c) 2011-2016, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef CHANNEL_GK20A_H
19 #define CHANNEL_GK20A_H
20
21 #include <linux/log2.h>
22 #include <linux/mutex.h>
23 #include <linux/poll.h>
24 #include <linux/semaphore.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/wait.h>
28 #include <uapi/linux/nvgpu.h>
29
30 struct gk20a;
31 struct gr_gk20a;
32 struct dbg_session_gk20a;
33 struct gk20a_fence;
34
35 #include "channel_sync_gk20a.h"
36
37 #include "mm_gk20a.h"
38 #include "gr_gk20a.h"
39 #include "fence_gk20a.h"
40
41 struct gpfifo {
42         u32 entry0;
43         u32 entry1;
44 };
45
46 struct notification {
47         struct {
48                 u32 nanoseconds[2];
49         } timestamp;
50         u32 info32;
51         u16 info16;
52         u16 status;
53 };
54
55 struct fence {
56         u32 hw_chid;
57         u32 syncpt_val;
58 };
59
60 /* contexts associated with a channel */
61 struct channel_ctx_gk20a {
62         struct gr_ctx_desc      *gr_ctx;
63         struct patch_desc       patch_ctx;
64         struct zcull_ctx_desc   zcull_ctx;
65         u64     global_ctx_buffer_va[NR_GLOBAL_CTX_BUF_VA];
66         u64     global_ctx_buffer_size[NR_GLOBAL_CTX_BUF_VA];
67         bool    global_ctx_buffer_mapped;
68 };
69
70 struct channel_gk20a_job {
71         struct mapped_buffer_node **mapped_buffers;
72         int num_mapped_buffers;
73         struct gk20a_fence *pre_fence;
74         struct gk20a_fence *post_fence;
75         struct priv_cmd_entry *wait_cmd;
76         struct priv_cmd_entry *incr_cmd;
77         struct list_head list;
78 };
79
80 struct channel_gk20a_poll_events {
81         struct mutex lock;
82         bool events_enabled;
83         int num_pending_events;
84 };
85
86 struct channel_gk20a_clean_up {
87         struct mutex lock;
88         bool scheduled;
89         struct delayed_work wq;
90 };
91
92 /* this is the priv element of struct nvhost_channel */
93 struct channel_gk20a {
94         struct gk20a *g; /* set only when channel is active */
95
96         struct list_head free_chs;
97
98         spinlock_t ref_obtain_lock;
99         bool referenceable;
100         atomic_t ref_count;
101         wait_queue_head_t ref_count_dec_wq;
102
103         int hw_chid;
104         bool bound;
105         bool first_init;
106         bool vpr;
107         bool cde;
108         pid_t pid;
109         struct mutex ioctl_lock;
110
111         int tsgid;
112         struct list_head ch_entry; /* channel's entry in TSG */
113
114         struct list_head jobs;
115         spinlock_t jobs_lock;
116
117         struct vm_gk20a *vm;
118
119         struct gpfifo_desc gpfifo;
120
121         struct channel_ctx_gk20a ch_ctx;
122
123         struct mem_desc inst_block;
124         struct mem_desc_sub ramfc;
125
126         void *userd_cpu_va;
127         u64 userd_iova;
128         u64 userd_gpu_va;
129
130         s32 num_objects;
131         u32 obj_class;  /* we support only one obj per channel */
132
133         struct priv_cmd_queue priv_cmd_q;
134
135         wait_queue_head_t notifier_wq;
136         wait_queue_head_t semaphore_wq;
137         wait_queue_head_t submit_wq;
138
139         u32 timeout_accumulated_ms;
140         u32 timeout_gpfifo_get;
141
142         struct channel_gk20a_clean_up clean_up;
143
144         bool cmds_pending;
145         struct {
146                 struct gk20a_fence *pre_fence;
147                 struct gk20a_fence *post_fence;
148                 struct mutex fence_lock;
149         } last_submit;
150
151         void (*remove_support)(struct channel_gk20a *);
152 #if defined(CONFIG_GK20A_CYCLE_STATS)
153         struct {
154         void *cyclestate_buffer;
155         u32 cyclestate_buffer_size;
156         struct dma_buf *cyclestate_buffer_handler;
157         struct mutex cyclestate_buffer_mutex;
158         } cyclestate;
159
160         struct mutex cs_client_mutex;
161         struct gk20a_cs_snapshot_client *cs_client;
162 #endif
163         struct mutex dbg_s_lock;
164         struct list_head dbg_s_list;
165
166         bool has_timedout;
167         u32 timeout_ms_max;
168         bool timeout_debug_dump;
169
170         struct dma_buf *error_notifier_ref;
171         struct nvgpu_notification *error_notifier;
172         void *error_notifier_va;
173         struct mutex error_notifier_mutex;
174
175         struct mutex sync_lock;
176         struct gk20a_channel_sync *sync;
177
178 #ifdef CONFIG_TEGRA_GR_VIRTUALIZATION
179         u64 virt_ctx;
180 #endif
181
182         /* event support */
183         struct channel_gk20a_poll_events poll_events;
184
185         /* signal channel owner via a callback, if set, in gk20a_channel_update
186          * via schedule_work */
187         void (*update_fn)(struct channel_gk20a *, void *);
188         void *update_fn_data;
189         spinlock_t update_fn_lock; /* make access to the two above atomic */
190         struct work_struct update_fn_work;
191 };
192
193 static inline bool gk20a_channel_as_bound(struct channel_gk20a *ch)
194 {
195         return !!ch->vm;
196 }
197 int channel_gk20a_commit_va(struct channel_gk20a *c);
198 int gk20a_init_channel_support(struct gk20a *, u32 chid);
199
200 /* must be inside gk20a_busy()..gk20a_idle() */
201 void gk20a_channel_close(struct channel_gk20a *ch);
202
203 bool gk20a_channel_update_and_check_timeout(struct channel_gk20a *ch,
204                                             u32 timeout_delta_ms);
205 void gk20a_disable_channel(struct channel_gk20a *ch);
206 void gk20a_channel_abort(struct channel_gk20a *ch, bool channel_preempt);
207 int gk20a_channel_finish(struct channel_gk20a *ch, unsigned long timeout);
208 void gk20a_set_error_notifier(struct channel_gk20a *ch, __u32 error);
209 void gk20a_channel_semaphore_wakeup(struct gk20a *g);
210 int gk20a_channel_alloc_priv_cmdbuf(struct channel_gk20a *c, u32 size,
211                              struct priv_cmd_entry **entry);
212
213 int gk20a_channel_suspend(struct gk20a *g);
214 int gk20a_channel_resume(struct gk20a *g);
215
216 /* Channel file operations */
217 int gk20a_channel_open(struct inode *inode, struct file *filp);
218 int gk20a_channel_open_ioctl(struct gk20a *g,
219                 struct nvgpu_channel_open_args *args);
220 long gk20a_channel_ioctl(struct file *filp,
221                          unsigned int cmd,
222                          unsigned long arg);
223 int gk20a_channel_release(struct inode *inode, struct file *filp);
224 struct channel_gk20a *gk20a_get_channel_from_file(int fd);
225 void gk20a_channel_update(struct channel_gk20a *c, int nr_completed);
226 unsigned int gk20a_channel_poll(struct file *filep, poll_table *wait);
227 void gk20a_channel_event(struct channel_gk20a *ch);
228
229 void gk20a_init_channel(struct gpu_ops *gops);
230
231 /* returns ch if reference was obtained */
232 struct channel_gk20a *__must_check _gk20a_channel_get(struct channel_gk20a *ch,
233                                                       const char *caller);
234 #define gk20a_channel_get(ch) _gk20a_channel_get(ch, __func__)
235
236
237 void _gk20a_channel_put(struct channel_gk20a *ch, const char *caller);
238 #define gk20a_channel_put(ch) _gk20a_channel_put(ch, __func__)
239
240 int gk20a_wait_channel_idle(struct channel_gk20a *ch);
241 struct channel_gk20a *gk20a_open_new_channel(struct gk20a *g);
242 struct channel_gk20a *gk20a_open_new_channel_with_cb(struct gk20a *g,
243                 void (*update_fn)(struct channel_gk20a *, void *),
244                 void *update_fn_data);
245 void channel_gk20a_unbind(struct channel_gk20a *ch_gk20a);
246
247 void gk20a_channel_cancel_job_clean_up(struct channel_gk20a *c,
248                                 bool wait_for_completion);
249
250 int gk20a_submit_channel_gpfifo(struct channel_gk20a *c,
251                                 struct nvgpu_gpfifo *gpfifo,
252                                 struct nvgpu_submit_gpfifo_args *args,
253                                 u32 num_entries,
254                                 u32 flags,
255                                 struct nvgpu_fence *fence,
256                                 struct gk20a_fence **fence_out,
257                                 bool force_need_sync_fence);
258
259 int gk20a_alloc_channel_gpfifo(struct channel_gk20a *c,
260                                struct nvgpu_alloc_gpfifo_args *args);
261
262 void channel_gk20a_unbind(struct channel_gk20a *ch_gk20a);
263 void channel_gk20a_disable(struct channel_gk20a *ch);
264 int channel_gk20a_alloc_inst(struct gk20a *g, struct channel_gk20a *ch);
265 void channel_gk20a_free_inst(struct gk20a *g, struct channel_gk20a *ch);
266 int channel_gk20a_setup_ramfc(struct channel_gk20a *c,
267                         u64 gpfifo_base, u32 gpfifo_entries, u32 flags);
268 void channel_gk20a_enable(struct channel_gk20a *ch);
269 #endif /* CHANNEL_GK20A_H */