summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nouveau/compote/compote-memory.c
blob: 8435ec0a8d0307f167bd07f1a2b700783f3d5ae2 (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
/*
 * Copyright 2017 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * Authors: Jérôme Glisse <jglisse@redhat.com>
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <uapi/drm/compote-uapi.h>
#include "compote.h"

#include <nvif/driver.h>
#include <nvif/class.h>

#include "nouveau_ttm.h"
#include "nouveau_usif.h"
#include "nouveau_abi16.h"
#include "nouveau_bo.h"

static struct compote_mo *compote_file_mo_find(struct compote_file *cfile,
					       uint64_t foffset)
{
	struct ttm_bo_device *bdev = &cfile->cdevice->nvdrm->ttm.bdev;
	unsigned long pgoffset = foffset >> PAGE_SHIFT;
	struct drm_vma_offset_node *node;
	struct compote_mo *mo = NULL;

	drm_vma_offset_lock_lookup(&bdev->vma_manager);
	node = drm_vma_offset_lookup_locked(&bdev->vma_manager, pgoffset, 1);
	if (likely(node)) {
		struct ttm_buffer_object *bo;
		struct nouveau_bo *nvbo;

		bo = container_of(node, struct ttm_buffer_object, vma_node);
		nvbo = nouveau_bo(bo);
		mo = nvbo->mo;
		if (!mo || mo->cfile != cfile || !kref_get_unless_zero(&mo->kref))
			mo = NULL;
	}
	drm_vma_offset_unlock_lookup(&bdev->vma_manager);

	return mo;
}

static int compote_mo_new(struct compote_mo **mop,
			  struct compote_file *cfile,
			  uint64_t nbytes)
{
	uint32_t flags, tile_mode, tile_flags;
	struct compote_mo *mo;
	int align;
	int ret;

	mo = kzalloc(sizeof(*mo), GFP_KERNEL);
	if (mo == NULL)
		return -ENOMEM;

	align = 0;
	tile_mode = 0;
	tile_flags = 0;
	flags = TTM_PL_FLAG_TT;
	ret = nouveau_bo_new(&cfile->nvclient, nbytes, align, flags, tile_mode,
			     tile_flags, NULL, NULL, &mo->nvbo);
	if (ret)
		goto error_bo;

	mo->cfile = cfile;
	mo->nvbo->mo = mo;
	kref_init(&mo->kref);
	INIT_LIST_HEAD(&mo->vas);
	mo->npages = PAGE_ALIGN(nbytes);
	mo->foffset = drm_vma_node_offset_addr(&mo->nvbo->bo.vma_node);

	*mop = mo;
	return 0;

error_bo:
	kfree(mo);
	*mop = NULL;
	return ret;
}

static void compote_mo_kref_put(struct kref *kref)
{
	struct compote_mo *mo = container_of(kref, struct compote_mo, kref);
	struct ttm_buffer_object *bo = &mo->nvbo->bo;

	ttm_bo_unref(&bo);
	kfree(mo);
}

void compote_mo_ref(struct compote_mo *mo)
{
	kref_get(&mo->kref);
}

void compote_mo_unref(struct compote_mo *mo)
{
	kref_put(&mo->kref, compote_mo_kref_put);
}

long compote_ioctl_mem_alloc(struct compote_file *cfile, void __user *uarg)
{
	struct compote_ioctl_mem_alloc arg;
	struct compote_mo *mo = NULL;
	int ret;

	ret = copy_from_user(&arg, uarg, sizeof(arg));
	if (ret)
		return ret;

	ret = compote_mo_new(&mo, cfile, arg.nbytes);

	arg.foffset = ret ? -1UL : mo->foffset;
	ret = copy_to_user(uarg, &arg, sizeof(arg));
	if (ret) {
		compote_mo_unref(mo);
		return ret;
	}

	return 0;
}

long compote_ioctl_mem_free(struct compote_file *cfile, void __user *uarg)
{
	struct compote_ioctl_mem_free arg;
	struct compote_mo *mo = NULL;
	int ret;

	ret = copy_from_user(&arg, uarg, sizeof(arg));
	if (ret)
		return ret;

	mo = compote_file_mo_find(cfile, arg.foffset);
	if (mo == NULL)
		return -EINVAL;

	compote_mo_unref(mo);
	compote_mo_unref(mo);
	return 0;
}

static int compote_vm_fault(struct vm_fault *vmf)
{
	struct compote_mo_va *mo_va = vmf->vma->vm_private_data;
	struct ttm_buffer_object *bo;
	int ret;

	bo = &mo_va->mo->nvbo->bo;
	vmf->vma->vm_private_data = bo;
	ret = mo_va->ttm_vm_ops->fault(vmf);
	vmf->vma->vm_private_data = mo_va;

	return ret;
}

static void compote_vm_open(struct vm_area_struct *vma)
{
	struct compote_mo_va *mo_va = vma->vm_private_data;
	struct ttm_buffer_object *bo;

	compote_mo_ref(mo_va->mo);

	bo = &mo_va->mo->nvbo->bo;
	vma->vm_private_data = bo;
	mo_va->ttm_vm_ops->open(vma);
	vma->vm_private_data = mo_va;
}

static void compote_vm_close(struct vm_area_struct *vma)
{
	struct compote_mo_va *mo_va = vma->vm_private_data;
	struct ttm_buffer_object *bo;

	bo = &mo_va->mo->nvbo->bo;
	vma->vm_private_data = bo;
	mo_va->ttm_vm_ops->close(vma);

	list_del_init(&mo_va->list);
	vma->vm_private_data = NULL;
	compote_mo_unref(mo_va->mo);
	kfree(mo_va);
}

static const struct vm_operations_struct compote_vm_ops = {
	.fault	= compote_vm_fault,
	.open	= compote_vm_open,
	.close	= compote_vm_close
};

int compote_mo_mmap(struct compote_file *cfile,
		    struct vm_area_struct *vma,
		    struct file *file)
{
	struct ttm_bo_device *bdev = &cfile->cdevice->nvdrm->ttm.bdev;
	struct compote_mo_va *mo_va;
	struct compote_mo *mo;
	int ret;

	mo = compote_file_mo_find(cfile, vma->vm_pgoff << PAGE_SHIFT);
	if (mo == NULL)
		return -EINVAL;

	mo_va = kzalloc(sizeof(*mo_va), GFP_KERNEL);
	if (mo_va == NULL)
		return -ENOMEM;

	mo_va->mo = mo;
	mo_va->start = vma->vm_start;
	mo_va->end = vma->vm_end;
	mo_va->pgoffset = vma->vm_pgoff - (mo->foffset >> PAGE_SHIFT);

	ret = ttm_bo_mmap(file, vma, bdev);
	if (unlikely(ret != 0)) {
		compote_mo_unref(mo);
		kfree(mo_va);
		return ret;
	}

	list_add_tail(&mo_va->list, &mo->vas);
	mo_va->ttm_vm_ops = vma->vm_ops;
	vma->vm_ops = &compote_vm_ops;
	vma->vm_private_data = mo_va;

	return 0;
}