summaryrefslogtreecommitdiff
path: root/drivers/staging/omaprpc/omap_rpc_dmabuf.c
blob: 86a7fd515bc9a95337b3caf56956f3b684695b03 (plain)
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
/*
 * OMAP Remote Procedure Call Driver.
 *
 * Copyright(c) 2012 Texas Instruments. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *	 notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *	 notice, this list of conditions and the following disclaimer in
 *	 the documentation and/or other materials provided with the
 *	 distribution.
 * * Neither the name Texas Instruments nor the names of its
 *	 contributors may be used to endorse or promote products derived
 *	 from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "omap_rpc_internal.h"

static struct dma_info_t *omaprpc_dma_sub(struct omaprpc_instance_t *rpc,
					 int fd)
{
	struct dma_info_t *pos, *n;
	mutex_lock(&rpc->lock);
	list_for_each_entry_safe(pos, n, &rpc->dma_list, list) {
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
			"Looking for FD %u, found FD %u\n", fd, pos->fd);
		if (pos->fd == fd) {
			list_del((struct list_head *)pos);
			break;
		} else
			pos = NULL;
	}
	mutex_unlock(&rpc->lock);
	return pos;
}

static int omaprpc_dma_add(struct omaprpc_instance_t *rpc,
			struct dma_info_t *dma)
{
	if (dma) {
		mutex_lock(&rpc->lock);
		list_add(&dma->list, &rpc->dma_list);
		mutex_unlock(&rpc->lock);
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
			rpc->rpcserv->dev,
			"Added FD %u to list",
			dma->fd);
	}
	return 0;
}

phys_addr_t omaprpc_pin_buffer(struct omaprpc_instance_t *rpc, void *reserved)
{
	struct dma_info_t *dma = kmalloc(sizeof(struct dma_info_t), GFP_KERNEL);
	if (dma == NULL)
		return 0;

	dma->fd = (int)reserved;
	OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
		rpc->rpcserv->dev,
		"Pining with FD %u\n",
		dma->fd);
	dma->dbuf = dma_buf_get((int)reserved);
	if (!(IS_ERR(dma->dbuf))) {
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
			rpc->rpcserv->dev,
			"DMA_BUF=%p\n",
			dma->dbuf);
		dma->attach = dma_buf_attach(dma->dbuf, rpc->rpcserv->dev);
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
			rpc->rpcserv->dev,
			"attach=%p\n",
			dma->attach);
		dma->sgt = dma_buf_map_attachment(dma->attach,
			DMA_BIDIRECTIONAL);
		omaprpc_dma_add(rpc, dma);
		return sg_dma_address(dma->sgt->sgl);
	} else
		kfree(dma);
	return 0;
}

phys_addr_t omaprpc_dma_find(struct omaprpc_instance_t *rpc, void *reserved)
{
	phys_addr_t addr = 0;
	struct list_head *pos = NULL;
	struct dma_info_t *node = NULL;
	int fd = (int)reserved;
	mutex_lock(&rpc->lock);
	list_for_each(pos, &rpc->dma_list) {
		node = (struct dma_info_t *)pos;
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
			"Looking for FD %u, found FD %u\n", fd, node->fd);
		if (node->fd == fd) {
			addr = sg_dma_address(node->sgt->sgl);
			break;
		}
	}
	OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
		"Returning Addr %p for FD %u\n", (void *)addr, fd);
	mutex_unlock(&rpc->lock);
	return addr;
}

void omaprpc_unpin_buffer(struct omaprpc_instance_t *rpc, void *reserved)
{
	struct dma_info_t *dma = omaprpc_dma_sub(rpc, (int)reserved);
	if (dma == NULL)
		return;
	dma_buf_unmap_attachment(dma->attach, dma->sgt, DMA_BIDIRECTIONAL);
	dma_buf_detach(dma->dbuf, dma->attach);
	dma_buf_put(dma->dbuf);
	kfree(dma);
}

phys_addr_t omaprpc_buffer_lookup(struct omaprpc_instance_t *rpc,
				uint32_t core,
				virt_addr_t uva,
				virt_addr_t buva,
				void *reserved)
{
	phys_addr_t lpa = 0, rpa = 0;
	/* User VA - Base User VA = User Offset assuming not tiler 2D*/
	/* For Tiler2D offset is corrected later*/
	long uoff = uva - buva;

	OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
		"CORE=%u BUVA=%p UVA=%p Uoff=%ld [0x%016lx] Hdl=%p\n",
		core, (void *)buva, (void *)uva, uoff, (ulong)uoff, reserved);

	if (uoff < 0) {
		OMAPRPC_ERR(rpc->rpcserv->dev,
			"Offsets calculation for BUVA=%p from UVA=%p is a "
			"negative number. Bad parameters!\n",
			(void *)buva, (void *)uva);
		rpa = 0;
	} else {
		/* find the base of the dam buf from the list */
		lpa = omaprpc_dma_find(rpc, reserved);
		if (lpa == 0) {
			/* wasn't in the list, convert the pointer */
			lpa = omaprpc_pin_buffer(rpc, reserved);
		}

		/* recalculate the offset in the user buffer
		 (accounts for tiler 2D) */
		uoff = omaprpc_recalc_off(lpa, uoff);

		/* offset the lpa by the offset in the user buffer */
		lpa += uoff;

		/* convert the local physical address to remote physical
		 address */
		rpa = rpmsg_local_to_remote_pa(core, lpa);
	}
	OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
		"ARM VA %p == ARM PA %p => REMOTE[%u] PA %p (RESV %p)\n",
		(void *)uva, (void *)lpa, core, (void *)rpa, reserved);
	return rpa;
}

int omaprpc_xlate_buffers(struct omaprpc_instance_t *rpc,
			  struct omaprpc_call_function_t *function,
			  int direction)
{
	int idx = 0, start = 0, inc = 1, limit = 0, ret = 0;
	uint32_t ptr_idx = 0, pri_offset = 0, sec_offset = 0, pg_offset = 0,
		 size = 0;

	/* @NOTE not all the parameters are pointers so this may be sparse */
	uint8_t *base_ptrs[OMAPRPC_MAX_PARAMETERS];
	struct dma_buf *dbufs[OMAPRPC_MAX_PARAMETERS];

	if (function->num_translations == 0)
		return 0;

	limit = function->num_translations;
	memset(base_ptrs, 0, sizeof(base_ptrs));
	OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
		"Operating on %d pointers\n", function->num_translations);
	/* we may have a failure during translation, in which case we need to
	   unwind the whole operation from here */

	for (idx = start; idx != limit; idx += inc) {
		OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
			"#### Starting Translation %d of %d by %d\n",
			idx, limit, inc);
		/* conveinence variables */
		ptr_idx = function->translations[idx].index;
		sec_offset	= function->translations[idx].offset;

		/* if the pointer index for this translation is invalid */
		if (ptr_idx >= OMAPRPC_MAX_PARAMETERS) {
			OMAPRPC_ERR(rpc->rpcserv->dev,
				"Invalid parameter pointer index %u\n",
				ptr_idx);
			goto unwind;
		} else if (function->params[ptr_idx].type !=
				OMAPRPC_PARAM_TYPE_PTR) {
			OMAPRPC_ERR(rpc->rpcserv->dev,
				"Parameter index %u is not a pointer "
				"(type %u)\n",
				ptr_idx, function->params[ptr_idx].type);
			goto unwind;
		}

		size = function->params[ptr_idx].size;

		if (sec_offset >= (size - sizeof(virt_addr_t))) {
			OMAPRPC_ERR(rpc->rpcserv->dev,
				"Offset is larger than data area! "
				"(sec_offset=%u size=%u)\n", sec_offset, size);
			goto unwind;
		}

		if (function->params[ptr_idx].data == 0) {
			OMAPRPC_ERR(rpc->rpcserv->dev,
				"Supplied user pointer is NULL!\n");
			goto unwind;
		}

		/* if the KVA pointer has not been mapped */
		if (base_ptrs[ptr_idx] == NULL) {
			size_t start = (pri_offset + sec_offset) & PAGE_MASK;
			size_t end = PAGE_SIZE;
			int ret = 0;

			/* compute the secondary offset */
			pri_offset = function->params[ptr_idx].data -
					function->params[ptr_idx].base;

			/* acquire a handle to the dma buf */
			dbufs[ptr_idx] = dma_buf_get(
				(int)function->params[ptr_idx].reserved);

			/* map the dma buf into cpu memory? */
			ret = dma_buf_begin_cpu_access(dbufs[ptr_idx],
							start,
							end,
							DMA_BIDIRECTIONAL);
			if (ret < 0) {
				OMAPRPC_ERR(rpc->rpcserv->dev,
					"OMAPRPC: Failed to acquire cpu access "
					"to the DMA Buf! ret=%d\n", ret);
				dma_buf_put(dbufs[ptr_idx]);
				goto unwind;
			}

			/* caluculate the base pointer for the region. */
			base_ptrs[ptr_idx] = dma_buf_kmap(dbufs[ptr_idx],
				((pri_offset + sec_offset)>>PAGE_SHIFT));

			/* calculate the new offset within that page */
			pg_offset = ((pri_offset + sec_offset) & (PAGE_SIZE-1));

			if (base_ptrs[ptr_idx] != NULL) {
				OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
					rpc->rpcserv->dev,
					"KMap'd base_ptr[%u]=%p dbuf=%p into "
					"kernel from %zu for %zu bytes, "
					"PG_OFFSET=%u\n",
					ptr_idx,
					base_ptrs[ptr_idx],
					dbufs[ptr_idx],
					start,
					end,
					pg_offset);
			}
		}

		/* if the KVA pointer is not NULL */
		if (base_ptrs[ptr_idx] != NULL) {
			if (direction == OMAPRPC_UVA_TO_RPA) {
				/* get the kernel virtual pointer to the
				 pointer to translate */
				virt_addr_t kva =
				(virt_addr_t)&((base_ptrs[ptr_idx])[pg_offset]);
				virt_addr_t uva = 0;
				virt_addr_t buva =
				(virt_addr_t)function->translations[idx].base;
				phys_addr_t rpa = 0;
				void	   *reserved =
				(void *)function->translations[idx].reserved;

				/* make sure we won't cause an unalign mem
				 access */
				if ((kva & 0x3) > 0) {
					OMAPRPC_ERR(rpc->rpcserv->dev,
						"ERROR: KVA %p is unaligned!\n",
						(void *)kva);
					return -EADDRNOTAVAIL;
				}
				/* load the user's VA */
				uva = *(virt_addr_t *)kva;
				if (uva == 0) {
					OMAPRPC_ERR(rpc->rpcserv->dev,
						"ERROR: Failed to access user "
						"buffer to translate pointer"
						"\n");
					print_hex_dump(KERN_DEBUG,
						"OMAPRPC: KMAP: ",
						DUMP_PREFIX_NONE, 16, 1,
						base_ptrs[ptr_idx], PAGE_SIZE,
						true);
					goto unwind;
				}

				OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
					rpc->rpcserv->dev,
					"Replacing UVA %p at KVA %p PTRIDX:%u "
					"PG_OFFSET:%u IDX:%d RESV:%p\n",
					(void *)uva, (void *)kva, ptr_idx,
					pg_offset, idx, reserved);

				/* calc the new RPA (remote physical address) */
				rpa = omaprpc_buffer_lookup(rpc, rpc->core,
							uva, buva, reserved);
				/* save the old value */
				function->translations[idx].reserved =
					(size_t)uva;
				/* replace with new RPA */
				*(phys_addr_t *)kva = rpa;

				OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
					rpc->rpcserv->dev,
					"Replaced UVA %p with RPA %p at KVA %p\n",
					(void *)uva, (void *)rpa, (void *)kva);

				if (rpa == 0) {
					/* need to unwind all operations.. */
					direction = OMAPRPC_RPA_TO_UVA;
					start = idx-1;
					inc = -1;
					limit = -1;
					ret = -ENODATA;
					/* @TODO unmap the parameter base
					pointer */
					goto restart;
				}
			} else if (direction == OMAPRPC_RPA_TO_UVA) {
				/* address of the pointer in memory */
				virt_addr_t kva = 0;
				virt_addr_t uva = 0;
				phys_addr_t rpa = 0;
				kva = (virt_addr_t)
					&((base_ptrs[ptr_idx])[pg_offset]);
				/* make sure we won't cause an unalign mem
				 access */
				if ((kva & 0x3) > 0)
					return -EADDRNOTAVAIL;
				/* get what was there for debugging */
				rpa = *(phys_addr_t *)kva;
				/* convienence value of uva */
				uva = (virt_addr_t)
					function->translations[idx].reserved;
				/* replace the translated value with the
				   remember version */
				*(virt_addr_t *)kva = uva;

				/* @TODO DMA_BUF requires unmapping the data
				from the TILER. */

				OMAPRPC_PRINT(OMAPRPC_ZONE_INFO,
					rpc->rpcserv->dev,
					"Replaced RPA %p with UVA %p at KVA %p\n",
					(void *)rpa, (void *)uva, (void *)kva);

				if (uva == 0) {
					/* need to unwind all operations.. */
					direction = OMAPRPC_RPA_TO_UVA;
					start = idx-1;
					inc = -1;
					limit = -1;
					ret = -ENODATA;
					/* @TODO unmap the parameter base
					 pointer */
					goto restart;
				}
			}
		} else {
			OMAPRPC_ERR(rpc->rpcserv->dev,
				"Failed to map UVA to KVA to do translation!\n");
			/* we can arrive here from multiple points, but the
			   action is the same from everywhere */
unwind:
			if (direction == OMAPRPC_UVA_TO_RPA) {
				/* we've encountered an error which needs to
				  unwind all the operations */
				OMAPRPC_ERR(rpc->rpcserv->dev,
					"Unwinding UVA to RPA translations!\n");
				direction = OMAPRPC_RPA_TO_UVA;
				start = idx-1;
				inc = -1;
				limit = -1;
				ret = -ENOBUFS;
				goto restart;
			} else if (direction == OMAPRPC_RPA_TO_UVA) {
				/* there was a problem restoring the pointer,
				   there's nothing to do but to continue
				   processing */
				continue;
			}
		}
restart:
		if (base_ptrs[ptr_idx]) {
			size_t start = (pri_offset + sec_offset) & PAGE_MASK;
			size_t end	 = PAGE_SIZE;

			OMAPRPC_PRINT(OMAPRPC_ZONE_INFO, rpc->rpcserv->dev,
				"Unkmaping base_ptrs[%u]=%p from dbuf=%p %zu "
				"for %zu bytes\n",
				ptr_idx,
				base_ptrs[ptr_idx],
				dbufs[ptr_idx],
				start,
				end);
			/* unmap the page in case this pointer needs to move to
			  a different adddress */
			dma_buf_kunmap(dbufs[ptr_idx],
				((pri_offset + sec_offset)>>PAGE_SHIFT),
				base_ptrs[ptr_idx]);
			/* end access to this page */
			dma_buf_end_cpu_access(dbufs[ptr_idx],
				start,
				end,
				DMA_BIDIRECTIONAL);
			base_ptrs[ptr_idx] = NULL;
			dma_buf_put(dbufs[ptr_idx]);
			dbufs[ptr_idx] = NULL;
			pg_offset = 0;
		}
	}
	return ret;
}