m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/surface.c
blob: d37a468ea0bc706f550bc7eedf5629a746181890 (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
#include <linux/anon_inodes.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <asm/uaccess.h>

#include "private_data.h"
#include "harddoom.h"
#include "surface.h"
#include "util.h"
#include "harddoomdev.h"

long draw_lines(struct file *filp, unsigned long arg)
{
	struct surface_data *surface_data;
	struct doomdev_surf_ioctl_draw_lines *param;
	struct doomdev_line *lines;
	int i;

	surface_data = filp->private_data;
	param = (struct doomdev_surf_ioctl_draw_lines *) arg;
	lines = (struct doomdev_line *) param->lines_ptr;

	mutex_lock(&surface_data->doom_data->cmd_mutex);

	for (i = 0; i < param->lines_num; i++) {
		draw_line(surface_data, lines[i]);
	}

	mutex_unlock(&surface_data->doom_data->cmd_mutex);

	return param->lines_num;
}

long fill_rects(struct file *filp, unsigned long arg)
{
	struct surface_data *surface_data;
	struct doomdev_surf_ioctl_fill_rects *param;
	struct doomdev_fill_rect *rects;
	int i;

	surface_data = filp->private_data;
	param = (struct doomdev_surf_ioctl_fill_rects *) arg;
	rects = (struct doomdev_fill_rect *) param->rects_ptr;

	mutex_lock(&surface_data->doom_data->cmd_mutex);

	for (i = 0; i < param->rects_num; i++) {
		fill_rect(surface_data, rects[i]);
	}

	mutex_unlock(&surface_data->doom_data->cmd_mutex);

	return param->rects_num;
}

long surface_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case DOOMDEV_SURF_IOCTL_DRAW_LINES:
		return draw_lines(filp, arg);
	case DOOMDEV_SURF_IOCTL_FILL_RECTS:
		return fill_rects(filp, arg);
	default:
		return -1;
	}
}

ssize_t surface_read(struct file *filp, char __user *buf, size_t count,
		loff_t *offset)
{
	struct surface_data *surface_data;
	unsigned long not_written;

	surface_data = (struct surface_data *) filp->private_data;
	if (*offset >= surface_data->surface_size || *offset < 0) {
		return 0;
	}

	if (*offset + count > surface_data->surface_size) {
		count = surface_data->surface_size - *offset;
	}

	mutex_lock(&surface_data->doom_data->ping_mutex);
	ping_sync(surface_data->doom_data->iomem);

	down(&surface_data->doom_data->pong_sem);

	not_written = copy_to_user(buf, surface_data->surface_cpu + (*offset),
			count);

	*offset += count - not_written;

	mutex_unlock(&surface_data->doom_data->ping_mutex);

	return count - not_written;
}

loff_t surface_llseek(struct file *filp, loff_t offset, int origin)
{
	struct surface_data *surface_data;
	loff_t new_pos;

	surface_data = filp->private_data;

	switch (origin) {
	case SEEK_SET:
		new_pos = offset;
		break;
	case SEEK_CUR:
		new_pos = filp->f_pos + offset;
		break;
	case SEEK_END:
		new_pos = surface_data->surface_size + offset;
		break;
	default:
		return -EINVAL;
	}

	if (new_pos < 0) {
		return -EINVAL;
	}

	filp->f_pos = new_pos;

	return new_pos;
}

void free_surface_buffer(struct surface_data *surface_data);

int surface_release (struct inode *inode, struct file *filp)
{
	struct surface_data *surface_data;

	surface_data = filp->private_data;

	free_surface_buffer(surface_data);

	kfree(surface_data);

	return 0;
}

struct file_operations surface_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = surface_ioctl,
	.compat_ioctl = surface_ioctl,
	.llseek = surface_llseek,
	.read = surface_read,
	.release = surface_release

};

int verify_params(struct doomdev_ioctl_create_surface *params)
{
	if (params->width < 64) {
		return -EINVAL;
	}

	if (params->width > 2048) {
		return -EOVERFLOW;
	}

	if (params->width % 64 != 0) {
		return -EINVAL;
	}

	if (params->height < 1) {
		return -EINVAL;
	}

	if (params->height > 2048) {
		return -EOVERFLOW;
	}

	return 0;
}

int alloc_surface_buffer(struct doomdev_ioctl_create_surface *params,
		struct surface_data *surface_data)
{
	int bytes_needed;
	int pages_needed;
	int i;
	int err;

	bytes_needed = params->width * params->height;
	surface_data->surface_size = bytes_needed;
	surface_data->width = params->width;
	surface_data->height = params->height;
	pages_needed = (bytes_needed / HARDDOOM_PAGE_SIZE);
	if (bytes_needed % HARDDOOM_PAGE_SIZE != 0) {
		pages_needed += 1;
	}

	if (pages_needed > 1024) {
		return -ENOMEM;
	}

	surface_data->pages = pages_needed;

	surface_data->surface_cpu =
		dma_alloc_coherent(surface_data->doom_data->pci_device,
			bytes_needed, &surface_data->surface_dev, GFP_KERNEL);

	ORFAIL_NULL(surface_data->surface_cpu, -ENOMEM, error_pt);

	surface_data->page_table_cpu =
		dma_alloc_coherent(surface_data->doom_data->pci_device,
			pages_needed * 4, &surface_data->page_table_dev, GFP_KERNEL);

	ORFAIL_NULL(surface_data->page_table_cpu, -ENOMEM, error_pt);

	for (i = 0; i < pages_needed; i++) {
		surface_data->page_table_cpu[i] =
			(HARDDOOM_PTE_PHYS_MASK &
			 (surface_data->surface_dev + HARDDOOM_PAGE_SIZE * i)) |
			HARDDOOM_PTE_VALID;
	}

	return 0;

error_pt:
	return err;
}

void free_surface_buffer(struct surface_data *surface_data)
{
	dma_free_coherent(surface_data->doom_data->device,
			surface_data->surface_size,
			surface_data->surface_cpu, surface_data->surface_dev);

	dma_free_coherent(surface_data->doom_data->device,
			surface_data->pages,
			surface_data->page_table_cpu, surface_data->page_table_dev);
}

int new_surface(struct file *filp, struct doomdev_ioctl_create_surface *params)
{
	int err;
	int fd;
	struct fd fd_s;
	struct surface_data *surface_data;
	struct doom_data *doom_data;

	err = verify_params(params);
	if (err < 0) {
		return err;
	}

	surface_data = kmalloc(sizeof(*surface_data), GFP_KERNEL);
	ORFAIL_NULL(surface_data, -ENOMEM, error_data);
	doom_data = container_of(filp->f_inode->i_cdev, struct doom_data, cdev);
	surface_data->doom_data = doom_data;

	ORFAIL(alloc_surface_buffer(params, surface_data), error_buffer);

	fd = anon_inode_getfd("doom_surface", &surface_fops, surface_data, 0);
	fd_s = fdget(fd);
	if (fd_s.file->f_op != &surface_fops) {
		err = -ENOENT;
		goto error_fdget;
	}

	fd_s.file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;

	return fd;

error_fdget:
	free_surface_buffer(surface_data);
error_buffer:
	kfree(surface_data);
error_data:
	return err;
}