summaryrefslogtreecommitdiff
path: root/gxp-mapping.c
blob: 5db126b79826b8dd825335b38033fa42ac676165 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Records the mapped device addresses.
 *
 * Copyright (C) 2021 Google LLC
 */

#include <linux/dma-mapping.h>
#include <linux/mm.h>
#include <linux/slab.h>

#include "gxp-dma.h"
#include "gxp-internal.h"
#include "gxp-mapping.h"
#include "mm-backport.h"

int gxp_mapping_init(struct gxp_dev *gxp)
{
	gxp->mappings =
		devm_kzalloc(gxp->dev, sizeof(*gxp->mappings), GFP_KERNEL);
	if (!gxp->mappings)
		return -ENOMEM;

	gxp->mappings->rb = RB_ROOT;
	mutex_init(&gxp->mappings->lock);

	return 0;
}

struct gxp_mapping *gxp_mapping_create(struct gxp_dev *gxp, uint core_list,
				       u64 user_address, size_t size, u32 flags,
				       enum dma_data_direction dir)
{
	struct gxp_mapping *mapping = NULL;
	uint num_pages = 0;
	struct page **pages;
	ulong offset;
	int ret, i;
	struct vm_area_struct *vma;
	unsigned int foll_flags = FOLL_LONGTERM | FOLL_WRITE;

	/*
	 * The host pages might be read-only and could fail if we attempt to pin
	 * it with FOLL_WRITE.
	 * default to read/write if find_extend_vma returns NULL
	 */
	vma = find_extend_vma(current->mm, user_address & PAGE_MASK);
	if (vma && !(vma->vm_flags & VM_WRITE)) {
		foll_flags &= ~FOLL_WRITE;
		if (dir != DMA_TO_DEVICE) {
			dev_err(gxp->dev,
				"Unable to map read-only pages as anything but DMA_TO_DEVICE\n");
			return ERR_PTR(-EINVAL);
		}
	}

	/* Pin the user pages */
	offset = user_address & (PAGE_SIZE - 1);
	if (unlikely((size + offset) / PAGE_SIZE >= UINT_MAX - 1 ||
		     size + offset < size))
		return ERR_PTR(-ENOMEM);
	num_pages = (size + offset) / PAGE_SIZE;
	if ((size + offset) % PAGE_SIZE)
		num_pages++;

	pages = kcalloc(num_pages, sizeof(*pages), GFP_KERNEL);
	if (!pages)
		return ERR_PTR(-ENOMEM);

	/* Provide protection around `pin_user_pages_fast` since it fails if
	 * called by more than one thread simultaneously.
	 */
	mutex_lock(&gxp->mappings->lock);
	ret = pin_user_pages_fast(user_address & PAGE_MASK, num_pages,
				  foll_flags, pages);
	mutex_unlock(&gxp->mappings->lock);
	if (ret < 0 || ret < num_pages) {
		dev_dbg(gxp->dev,
			"Get user pages failed: user_add=%pK, num_pages=%u, ret=%d\n",
			(void *)user_address, num_pages, ret);
		num_pages = ret < 0 ? 0 : ret;
		ret = ret >= 0 ? -EFAULT : ret;
		goto error_unpin_pages;
	}

	/* Initialize mapping book-keeping */
	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
	if (!mapping) {
		ret = -ENOMEM;
		goto error_unpin_pages;
	}
	mapping->host_address = user_address;
	mapping->core_list = core_list;
	mapping->size = size;
	mapping->map_count = 1;
	mapping->gxp_dma_flags = flags;
	mapping->dir = dir;
	ret = sg_alloc_table_from_pages(&mapping->sgt, pages, num_pages, 0,
					num_pages * PAGE_SIZE, GFP_KERNEL);
	if (ret) {
		dev_dbg(gxp->dev, "Failed to alloc sgt for mapping (ret=%d)\n",
			ret);
		goto error_free_sgt;
	}

	/* map the user pages */
	ret = gxp_dma_map_sg(gxp, mapping->core_list, mapping->sgt.sgl,
			     mapping->sgt.nents, mapping->dir,
			     DMA_ATTR_SKIP_CPU_SYNC, mapping->gxp_dma_flags);
	if (!ret) {
		dev_dbg(gxp->dev, "Failed to map sgt (ret=%d)\n", ret);
		ret = -EINVAL;
		goto error_free_sgt;
	}
	mapping->sgt.nents = ret;
	mapping->device_address =
		sg_dma_address(mapping->sgt.sgl) + offset;

	kfree(pages);
	return mapping;

error_free_sgt:
	sg_free_table(&mapping->sgt);
	kfree(mapping);
error_unpin_pages:
	for (i = 0; i < num_pages; i++)
		unpin_user_page(pages[i]);
	kfree(pages);

	return ERR_PTR(ret);
}

void gxp_mapping_destroy(struct gxp_dev *gxp, struct gxp_mapping *mapping)
{
	struct sg_page_iter sg_iter;
	struct page *page;

	/*
	 * Unmap the user pages
	 *
	 * Normally on unmap, the entire mapping is synced back to the CPU.
	 * Since mappings are made at a page granularity regardless of the
	 * underlying buffer's size, they can cover other data as well. If a
	 * user requires a mapping be synced before unmapping, they are
	 * responsible for calling `gxp_mapping_sync()` before hand.
	 */
	gxp_dma_unmap_sg(gxp, mapping->core_list, mapping->sgt.sgl,
			 mapping->sgt.orig_nents, mapping->dir,
			 DMA_ATTR_SKIP_CPU_SYNC);

	/* Unpin the user pages */
	for_each_sg_page(mapping->sgt.sgl, &sg_iter, mapping->sgt.orig_nents,
			 0) {
		page = sg_page_iter_page(&sg_iter);
		if (mapping->dir == DMA_FROM_DEVICE ||
		    mapping->dir == DMA_BIDIRECTIONAL) {
			set_page_dirty(page);
		}

		unpin_user_page(page);
	}

	/* Free the mapping book-keeping */
	sg_free_table(&mapping->sgt);
	kfree(mapping);
}

int gxp_mapping_sync(struct gxp_dev *gxp, struct gxp_mapping *mapping,
		     u32 offset, u32 size, bool for_cpu)
{
	struct scatterlist *sg, *start_sg = NULL, *end_sg = NULL;
	int nelems = 0, cur_offset = 0, ret = 0, i;
	u64 start, end;
	unsigned int start_diff = 0, end_diff = 0;

	/*
	 * Valid input requires
	 * - size > 0 (offset + size != offset)
	 * - offset + size does not overflow (offset + size > offset)
	 * - the mapped range falls within [0 : mapping->size]
	 */
	if (offset + size <= offset ||
	    offset + size > mapping->size)
		return -EINVAL;

	/*
	 * Mappings are created at a PAGE_SIZE granularity, however other data
	 * which is not part of the mapped buffer may be present in the first
	 * and last pages of the buffer's scattergather list.
	 *
	 * To ensure only the intended data is actually synced, iterate through
	 * the scattergather list, to find the first and last `scatterlist`s
	 * which contain the range of the buffer to sync.
	 *
	 * After those links are found, change their offset/lengths so that
	 * `dma_map_sg_for_*()` will only sync the requested region.
	 */
	start = (mapping->host_address & ~PAGE_MASK) + offset;
	end = start + size;
	for_each_sg(mapping->sgt.sgl, sg, mapping->sgt.orig_nents, i) {
		if (end <= cur_offset)
			break;
		if (cur_offset <= start && start < cur_offset + sg->length) {
			start_sg = sg;
			start_diff = start - cur_offset;
		}
		if (start_sg)
			nelems++;
		cur_offset += sg->length;
		end_sg = sg;
	}
	end_diff = cur_offset - end;

	/* Make sure a valid starting scatterlist was found for the start */
	if (!start_sg)
		return -EINVAL;

	/*
	 * Since the scatter-gather list of the mapping is modified while it is
	 * being synced, only one sync for a given mapping can occur at a time.
	 * Rather than maintain a mutex for every mapping, lock the mapping list
	 * mutex, making all syncs mutually exclusive.
	 */
	mutex_lock(&gxp->mappings->lock);

	start_sg->offset += start_diff;
	start_sg->dma_address += start_diff;
	start_sg->length -= start_diff;
	start_sg->dma_length -= start_diff;
	end_sg->length -= end_diff;
	end_sg->dma_length -= end_diff;

	if (for_cpu)
		gxp_dma_sync_sg_for_cpu(gxp, start_sg, nelems, mapping->dir);
	else
		gxp_dma_sync_sg_for_device(gxp, start_sg, nelems, mapping->dir);

	/*
	 * Return the start and end scatterlists' offset/lengths to their
	 * original values for the next time they need to be synced/unmapped.
	 */
	end_sg->length += end_diff;
	end_sg->dma_length += end_diff;
	start_sg->offset -= start_diff;
	start_sg->dma_address -= start_diff;
	start_sg->length += start_diff;
	start_sg->dma_length += start_diff;

	mutex_unlock(&gxp->mappings->lock);

	return ret;
}

int gxp_mapping_put(struct gxp_dev *gxp, struct gxp_mapping *map)
{
	struct rb_node **link;
	struct rb_node *parent = NULL;
	u64 device_address = map->device_address;
	struct gxp_mapping *this;

	link = &gxp->mappings->rb.rb_node;

	mutex_lock(&gxp->mappings->lock);

	/* Figure out where to put new node */
	while (*link) {
		parent = *link;
		this = rb_entry(parent, struct gxp_mapping, node);

		if (this->device_address > device_address)
			link = &(*link)->rb_left;
		else if (this->device_address < device_address)
			link = &(*link)->rb_right;
		else
			goto out;
	}

	/* Add new node and rebalance tree. */
	rb_link_node(&map->node, parent, link);
	rb_insert_color(&map->node, &gxp->mappings->rb);

	mutex_unlock(&gxp->mappings->lock);

	return 0;

out:
	mutex_unlock(&gxp->mappings->lock);
	dev_err(gxp->dev, "Duplicate mapping: 0x%llx", map->device_address);
	return -EINVAL;
}

struct gxp_mapping *gxp_mapping_get(struct gxp_dev *gxp, u64 device_address)
{
	struct rb_node *node;
	struct gxp_mapping *this;

	mutex_lock(&gxp->mappings->lock);

	node = gxp->mappings->rb.rb_node;

	while (node) {
		this = rb_entry(node, struct gxp_mapping, node);

		if (this->device_address > device_address) {
			node = node->rb_left;
		} else if (this->device_address < device_address) {
			node = node->rb_right;
		} else {
			mutex_unlock(&gxp->mappings->lock);
			return this;  /* Found it */
		}
	}

	mutex_unlock(&gxp->mappings->lock);

	dev_err(gxp->dev, "Mapping not found: 0x%llx", device_address);
	return NULL;
}

struct gxp_mapping *gxp_mapping_get_host(struct gxp_dev *gxp, u64 host_address)
{
	struct rb_node *node;
	struct gxp_mapping *this;

	mutex_lock(&gxp->mappings->lock);

	if (!host_address) {
		dev_warn(gxp->dev,
			 "Unable to get dma-buf mapping by host address\n");
		return NULL;
	}

	/* Iterate through the elements in the rbtree */
	for (node = rb_first(&gxp->mappings->rb); node; node = rb_next(node)) {
		this = rb_entry(node, struct gxp_mapping, node);
		if (this->host_address == host_address) {
			mutex_unlock(&gxp->mappings->lock);
			return this;
		}
	}

	mutex_unlock(&gxp->mappings->lock);

	return NULL;
}

void gxp_mapping_remove(struct gxp_dev *gxp, struct gxp_mapping *map)
{
	mutex_lock(&gxp->mappings->lock);

	rb_erase(&map->node, &gxp->mappings->rb);

	mutex_unlock(&gxp->mappings->lock);
}