summaryrefslogtreecommitdiffstats
path: root/drivers/misc/mods/mods_dma.c
blob: 2fd581468684c9989020a1075a8e8b46ce33ff1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 * mods_dma.c - This file is part of NVIDIA MODS kernel driver.
 *
 * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA MODS kernel driver is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * NVIDIA MODS kernel driver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NVIDIA MODS kernel driver.
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/uaccess.h>
#include <linux/dmaengine.h>
#include "mods_internal.h"

#define MODS_DMA_MAX_CHANNEL 32
struct mods_dma_chan_info {
	rwlock_t lock;
	bool    in_use;
	u32     id;
	struct  dma_chan *pch;
	bool    should_notify;
};

static DECLARE_BITMAP(dma_info_mask, MODS_DMA_MAX_CHANNEL);
static struct mods_dma_chan_info dma_info_chan_list[MODS_DMA_MAX_CHANNEL];
static DEFINE_SPINLOCK(dma_info_lock);

int mods_get_dma_id(u32 *p_id)
{
	u32 id;

	spin_lock(&dma_info_lock);
	id = find_first_zero_bit(dma_info_mask, MODS_DMA_MAX_CHANNEL);
	if (id >= MODS_DMA_MAX_CHANNEL) {
		spin_unlock(&dma_info_lock);
		return -ENOSPC;
	}
	set_bit(id, dma_info_mask);
	spin_unlock(&dma_info_lock);

	*p_id = id;
	return OK;
}

void mods_release_dma_id(u32 id)
{
	spin_lock(&dma_info_lock);
	clear_bit(id, dma_info_mask);
	spin_unlock(&dma_info_lock);
}

int mods_get_chan_by_id(u32 id, struct mods_dma_chan_info **p_dma_chan)
{
	if (id > MODS_DMA_MAX_CHANNEL)
		return	-ERANGE;

	*p_dma_chan = &dma_info_chan_list[id];

	return OK;
}

int mods_init_dma(void)
{
	struct mods_dma_chan_info *p_chan_info;
	int i;

	for (i = 0; i < MODS_DMA_MAX_CHANNEL; i++) {
		p_chan_info = &dma_info_chan_list[i];
		rwlock_init(&(p_chan_info->lock));
		p_chan_info->in_use = false;
	}

	return OK;
}

void mods_release_channel(u32 id)
{
	struct mods_dma_chan_info *p_mods_chan;
	struct dma_chan *pch = 0;

	if (mods_get_chan_by_id(id, &p_mods_chan) != OK) {
		mods_error_printk("get dma channel failed, id %d\n", id);
		return;
	}

	write_lock(&(p_mods_chan->lock));
	if (p_mods_chan->in_use) {
		pch = p_mods_chan->pch;
		p_mods_chan->pch = NULL;
		mods_release_dma_id(id);
		p_mods_chan->in_use = false;
	}
	write_unlock(&(p_mods_chan->lock));

	/* do not call this when hold spin lock */
	if (pch) {
		dmaengine_terminate_all(pch);
		dma_release_channel(pch);
	}
}

void mods_exit_dma(void)
{
	int i;

	for (i = 0; i < MODS_DMA_MAX_CHANNEL; i++)
		mods_release_channel(i);
}

bool mods_chan_is_inuse(struct mods_dma_chan_info *p_mods_chan)
{
	bool in_use = false;

	read_lock(&(p_mods_chan->lock));
	if (p_mods_chan->in_use)
		in_use = true;
	read_unlock(&(p_mods_chan->lock));

	return in_use;
}

int mods_get_inuse_chan_by_handle(struct MODS_DMA_HANDLE *p_handle,
				  struct mods_dma_chan_info **p_mods_chan)
{
	int ret;
	bool in_use;
	struct mods_dma_chan_info *p_mods_ch;

	ret = mods_get_chan_by_id(p_handle->dma_id, &p_mods_ch);
	if (ret != OK) {
		mods_error_printk("get dma channel failed, id %d\n",
				  p_handle->dma_id);
		return -ENODEV;
	}

	in_use = mods_chan_is_inuse(p_mods_ch);
	if (!in_use) {
		mods_error_printk("invalid dma channel: %d, not in use\n",
				  p_handle->dma_id);
		return -EINVAL;
	}
	*p_mods_chan = p_mods_ch;
	return OK;
}

int mods_dma_sync_wait(struct MODS_DMA_HANDLE *p_handle,
		       mods_dma_cookie_t cookie)
{
	int ret = OK;
	struct mods_dma_chan_info *p_mods_chan;

	ret = mods_get_inuse_chan_by_handle(p_handle, &p_mods_chan);
	if (ret != OK)
		return ret;

	mods_debug_printk(DEBUG_TEGRADMA,
			  "Wait on chan: %p\n", p_mods_chan->pch);
	read_lock(&(p_mods_chan->lock));
	if (dma_sync_wait(p_mods_chan->pch, cookie) != DMA_COMPLETE)
		ret = -1;
	read_unlock(&(p_mods_chan->lock));

	return ret;
}

int mods_dma_async_is_tx_complete(struct MODS_DMA_HANDLE *p_handle,
				  mods_dma_cookie_t cookie,
				  __u32 *p_is_complete)
{
	int ret = OK;
	struct mods_dma_chan_info *p_mods_chan;
	enum dma_status status;

	ret = mods_get_inuse_chan_by_handle(p_handle, &p_mods_chan);
	if (ret != OK)
		return ret;

	read_lock(&(p_mods_chan->lock));
	status = dma_async_is_tx_complete(p_mods_chan->pch, cookie, NULL, NULL);
	read_unlock(&(p_mods_chan->lock));

	if (status == DMA_COMPLETE)
		*p_is_complete = true;
	else if (status == DMA_IN_PROGRESS)
		*p_is_complete = false;
	else
		ret = -EINVAL;

	return ret;
}

int esc_mods_dma_request_channel(struct file *pfile,
				 struct MODS_DMA_HANDLE *p_handle)
{
	struct dma_chan *chan;
	struct mods_dma_chan_info *p_mods_chan;
	dma_cap_mask_t mask;
	u32 id;
	int ret;

	LOG_ENT();

	ret = mods_get_dma_id(&id);
	if (ret != OK) {
		mods_error_printk("no dma handle available\n");
		return ret;
	}

	ret = mods_get_chan_by_id(id, &p_mods_chan);
	if (ret != OK) {
		mods_error_printk("get dma channel failed\n");
		return ret;
	}

	read_lock(&(p_mods_chan->lock));
	if (p_mods_chan->in_use) {
		mods_error_printk("mods dma channel in use\n");
		read_unlock(&(p_mods_chan->lock));
		return -EBUSY;
	}
	read_unlock(&(p_mods_chan->lock));

	dma_cap_zero(mask);
	dma_cap_set(p_handle->dma_type, mask);
	chan = dma_request_channel(mask, NULL, NULL);
	if (!chan) {
		mods_error_printk("Dma channel is not available\n");
		mods_release_dma_id(id);
		return -EBUSY;
	}

	write_lock(&(p_mods_chan->lock));
	p_mods_chan->pch = chan;
	p_mods_chan->in_use = true;
	write_unlock(&(p_mods_chan->lock));

	p_handle->dma_id = id;
	mods_debug_printk(DEBUG_TEGRADMA,
			  "request get dma id: %d\n", id);
	LOG_EXT();

	return 0;
}

int esc_mods_dma_release_channel(struct file *pfile,
				 struct MODS_DMA_HANDLE *p_handle)
{
	mods_release_channel(p_handle->dma_id);
	return OK;
}

int esc_mods_dma_set_config(struct file *pfile,
			    struct MODS_DMA_CHANNEL_CONFIG *p_config)

{
	struct dma_slave_config config;
	struct mods_dma_chan_info *p_mods_chan;
	int ret;

	LOG_ENT();
	ret = mods_get_inuse_chan_by_handle(&p_config->handle, &p_mods_chan);
	if (ret != OK)
		return ret;

	config.direction = p_config->direction;
	config.src_addr = p_config->src_addr;
	config.dst_addr = p_config->dst_addr;
	config.src_addr_width = p_config->src_addr_width;
	config.dst_addr_width = p_config->dst_addr_width;
	config.src_maxburst = p_config->src_maxburst;
	config.dst_maxburst = p_config->dst_maxburst;
	config.device_fc = (p_config->device_fc == 0) ? false : true;
	config.slave_id = p_config->slave_id;

	mods_debug_printk(DEBUG_TEGRADMA,
			  "ch: %d dir [%d], addr[%p -> %p], burst [%d %d]",
			  p_config->handle.dma_id,
			  config.direction,
			  (void *)config.src_addr, (void *)config.dst_addr,
			  config.src_maxburst, config.dst_maxburst);
	mods_debug_printk(DEBUG_TEGRADMA,
			  "width [%d %d] slave id %d\n",
			  config.src_addr_width, config.dst_addr_width,
			  config.slave_id);

	write_lock(&(p_mods_chan->lock));
	ret = dmaengine_slave_config(p_mods_chan->pch, &config);
	write_unlock(&(p_mods_chan->lock));

	LOG_EXT();

	return ret;
}


int esc_mods_dma_submit_request(struct file *pfile,
				struct MODS_DMA_TX_DESC *p_mods_desc)
{
	int ret = OK;
	struct mods_dma_chan_info *p_mods_chan;
	struct dma_async_tx_descriptor *desc;
	struct dma_device       *dev;
	enum dma_ctrl_flags     flags;
	mods_dma_cookie_t cookie = 0;

	LOG_ENT();
	flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;

	ret = mods_get_inuse_chan_by_handle(&p_mods_desc->handle, &p_mods_chan);
	if (ret != OK)
		return ret;

	if (p_mods_desc->mode != MODS_DMA_SINGLE) {
		mods_error_printk("Unsupported Mode: %d\n", p_mods_desc->mode);
		return -EINVAL;
	}

	mods_debug_printk(DEBUG_TEGRADMA,
			  "Submit on chan %p\n", p_mods_chan->pch);

	write_lock(&(p_mods_chan->lock));
	if (p_mods_desc->data_dir == MODS_DMA_MEM_TO_MEM) {
		dev = p_mods_chan->pch->device;
		desc = dev->device_prep_dma_memcpy(
						p_mods_chan->pch,
						p_mods_desc->phys,
						p_mods_desc->phys_2,
						p_mods_desc->length,
						flags);
	} else {
		mods_debug_printk(DEBUG_TEGRADMA,
				  "Phys Addr [%p], len [%d], dir [%d]\n",
					(void *)p_mods_desc->phys,
					p_mods_desc->length,
					p_mods_desc->data_dir);
		desc = dmaengine_prep_slave_single(
							p_mods_chan->pch,
							p_mods_desc->phys,
							p_mods_desc->length,
							p_mods_desc->data_dir,
							flags);
	}

	if (desc == NULL) {
		mods_error_printk("Not able to get desc for Tx\n");
		ret = -EIO;
		goto failed;
	}

	desc->callback = NULL;
	desc->callback_param = NULL;
	cookie = dmaengine_submit(desc);

failed:
	write_unlock(&(p_mods_chan->lock));
	if (dma_submit_error(cookie)) {
		mods_info_printk("Submit cookie: %x Error\n", cookie);
		return -EIO;
	}

	p_mods_desc->cookie = cookie;

	LOG_EXT();
	return ret;
}

int esc_mods_dma_async_issue_pending(struct file *pfile,
				     struct MODS_DMA_HANDLE *p_handle)
{
	int ret = OK;
	struct mods_dma_chan_info *p_mods_chan;

	LOG_ENT();
	ret = mods_get_inuse_chan_by_handle(p_handle, &p_mods_chan);
	if (ret != OK)
		return ret;

	mods_debug_printk(DEBUG_TEGRADMA,
			  "Issue pending on chan: %p\n", p_mods_chan->pch);
	read_lock(&(p_mods_chan->lock));
	dma_async_issue_pending(p_mods_chan->pch);
	read_unlock(&(p_mods_chan->lock));
	LOG_EXT();

	return ret;
}

int esc_mods_dma_wait(struct file *pfile,
		      struct MODS_DMA_WAIT_DESC *p_wait_desc)
{
	int ret;

	LOG_ENT();
	if (p_wait_desc->type == MODS_DMA_SYNC_WAIT)
		ret = mods_dma_sync_wait(&p_wait_desc->handle,
					 p_wait_desc->cookie);
	else if (p_wait_desc->type == MODS_DMA_ASYNC_WAIT)
		ret = mods_dma_async_is_tx_complete(&p_wait_desc->handle,
						    p_wait_desc->cookie,
						    &p_wait_desc->tx_complete);
	else
		ret = -EINVAL;

	LOG_EXT();
	return  ret;
}

int esc_mods_dma_alloc_coherent(struct file                    *fp,
				struct MODS_DMA_COHERENT_MEM_HANDLE *p)
{
	dma_addr_t    p_phys_addr;
	void          *p_cpu_addr;

	LOG_ENT();

	p_cpu_addr = dma_alloc_coherent(NULL,
					p->num_bytes,
					&p_phys_addr,
					GFP_KERNEL);

	mods_debug_printk(DEBUG_MEM,
		"%s:num_bytes=%d, p_cpu_addr=%p, p_phys_addr=%p\n",
		__func__,
		p->num_bytes,
		(void *)p_cpu_addr,
		(void *)p_phys_addr);

	if (!p_cpu_addr) {
		mods_error_printk(
		"%s:FAILED!!!num_bytes=%d, p_cpu_addr=%p, p_phys_addr=%p\n",
		__func__,
		p->num_bytes,
		(void *)p_cpu_addr,
		(void *)p_phys_addr);
		LOG_EXT();
		return -1;
	}

	memset(p_cpu_addr, 0x00, p->num_bytes);

	p->memory_handle_phys = (u64)p_phys_addr;
	p->memory_handle_virt = (u64)p_cpu_addr;

	LOG_EXT();
	return 0;
}

int esc_mods_dma_free_coherent(struct file                    *fp,
				struct MODS_DMA_COHERENT_MEM_HANDLE *p)
{