summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nouveau/compote/compote-channel.c
blob: 9b9b30e999cb1c1b5c4e985fc8cf0dd63611b7c6 (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
/*
 * 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 <nvif/cla06f.h>

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

int compote_channel_new(struct compote_channel **channelp,
			struct compote_file *cfile)
{
	uint32_t fb_ctxdma_handle, tt_ctxdma_handle;
	struct compote_channel *channel;
	struct nvif_device *nvifdevice;
	struct nouveau_drm *nvdrm;
	int ret;

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

	channel->cfile = cfile;
	kref_init(&channel->kref);

	/* create channel object and initialise dma and fence management */
	tt_ctxdma_handle = 0;
	nvdrm = cfile->cdevice->nvdrm;
	nvifdevice = &cfile->nvclient.device;
	fb_ctxdma_handle = NVA06F_V0_ENGINE_GR;
	ret = nouveau_channel_new(nvdrm, nvifdevice,
				  fb_ctxdma_handle,
				  tt_ctxdma_handle,
				  &channel->nvchan);
	if (ret)
		goto error;

	*channelp = channel;
	return 0;

error:
	kfree(channel);
	return ret;
}

static void compote_channel_kref_put(struct kref *kref)
{
	struct compote_channel *channel;
	struct compote_file *cfile;

	channel = container_of(kref, struct compote_channel, kref);
	cfile = channel->cfile;

	/*
	 * Wait for all activity to stop before releasing notify object, which
	 * may be still in use.
	 */
	nouveau_channel_idle(channel->nvchan);
	nouveau_channel_del(&channel->nvchan);

	down_write(&cfile->rwsem);
	list_del_init(&channel->list);
	up_write(&cfile->rwsem);

	kfree(channel);
}

void compote_channel_ref(struct compote_channel *channel)
{
	kref_get(&channel->kref);
}

void compote_channel_unref(struct compote_channel *channel)
{
	kref_put(&channel->kref, compote_channel_kref_put);
}

int compote_ioctl_channel_alloc(struct compote_file *cfile, void __user *uarg)
{
	struct compote_ioctl_channel_alloc arg;
	struct compote_channel *channel;
	int ret;

	ret = compote_channel_new(&channel, cfile);
	if (ret)
		return ret;

	arg.channel = channel->nvchan->chid;
	ret = copy_to_user(uarg, &arg, sizeof(arg));
	if (ret) {
		compote_channel_unref(channel);
		return ret;
	}

	down_write(&cfile->rwsem);
	list_add_tail(&channel->list, &cfile->channels);
	up_write(&cfile->rwsem);

	return 0;
}

int compote_ioctl_channel_free(struct compote_file *cfile,
			       void __user *uarg)
{
	struct compote_ioctl_channel_free arg;
	struct compote_channel *channel;
	int ret;

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

	down_write(&cfile->rwsem);
	list_for_each_entry (channel, &cfile->channels, list) {
		if (channel->nvchan->chid == arg.channel) {
			compote_channel_ref(channel);
			up_write(&cfile->rwsem);
			compote_channel_unref(channel);
			compote_channel_unref(channel);
			return 0;
		}
	}
	up_write(&cfile->rwsem);

	return -EINVAL;
}