m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/surface.c
blob: 29530b792bcdfbc7494a69522ca87912ca602784 (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
#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 copy_rects(struct file *filp, unsigned long arg)
{
	struct surface_data *dst_data;
	struct surface_data *src_data;
	struct doomdev_surf_ioctl_copy_rects *param;
	struct doomdev_copy_rect *rects;
	struct fd src_fds;
	int i;

	dst_data = filp->private_data;
	param = (struct doomdev_surf_ioctl_copy_rects *) arg;
	rects = (struct doomdev_copy_rect *) param->rects_ptr;

	src_fds = fdget(param->surf_src_fd);
	src_data = src_fds.file->private_data;

	if (dst_data->doom_data != src_data->doom_data) {
		return -EINVAL;
	}

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

	for (i = 0; i < param->rects_num; i++) {
		copy_rect(dst_data, src_data, rects[i]);
	}

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

	fdput(src_fds);

	return param->rects_num;
}

long draw_columns(struct file *filp, unsigned long arg)
{
	struct doomdev_surf_ioctl_draw_columns *param;
	struct surface_data *surface_data;
	struct texture_data *texture_data;
	struct doomdev_column *columns;
	struct fd texture_fds;
	int i;

	surface_data = filp->private_data;

	param = (struct doomdev_surf_ioctl_draw_columns *) arg;

	// temp
	if (param->draw_flags & DOOMDEV_DRAW_FLAGS_FUZZ) {
		return 0x400;
	}

	texture_fds = fdget(param->texture_fd);
	texture_data = texture_fds.file->private_data;

	if (surface_data->doom_data != texture_data->doom_data) {
		return -EINVAL;
	}

	columns = (struct doomdev_column *) param->columns_ptr;

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

	for (i = 0; i < param->columns_num; i++) {
		draw_column(surface_data, texture_data, columns[i]);
	}

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

	fdput(texture_fds);

	return param->columns_num;
}

long draw_spans(struct file *filp, unsigned long arg)
{
	struct doomdev_surf_ioctl_draw_spans *param;
	struct surface_data *surface_data;
	struct flat_data *flat_data;
	struct doomdev_span *spans;
	struct fd flat_fds;
	int i;

	surface_data = filp->private_data;

	param = (struct doomdev_surf_ioctl_draw_spans *) arg;

	flat_fds = fdget(param->flat_fd);
	flat_data = flat_fds.file->private_data;

	if (surface_data->doom_data != flat_data->doom_data) {
		return -EINVAL;
	}

	spans = (struct doomdev_span *) param->spans_ptr;

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

	for (i = 0; i < param->spans_num; i++) {
		draw_span(surface_data, flat_data, spans[i]);
	}

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

	fdput(flat_fds);

	return param->spans_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);
	case DOOMDEV_SURF_IOCTL_COPY_RECTS:
		return copy_rects(filp, arg);
	case DOOMDEV_SURF_IOCTL_DRAW_COLUMNS:
		return draw_columns(filp, arg);
	case DOOMDEV_SURF_IOCTL_DRAW_SPANS:
		return draw_spans(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->cmd_mutex);
	ping_sync(surface_data->doom_data);
	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->cmd_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;

	mutex_lock(&surface_data->doom_data->ping_mutex);
	ping_sync(surface_data->doom_data);
	down(&surface_data->doom_data->pong_sem);
	mutex_unlock(&surface_data->doom_data->ping_mutex);

	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_surface);

	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:
	dma_free_coherent(surface_data->doom_data->pci_device,
			surface_data->surface_size,
			surface_data->surface_cpu, surface_data->surface_dev);
error_surface:
	return err;
}

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

	dma_free_coherent(surface_data->doom_data->pci_device,
			surface_data->pages * 4,
			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;

	fdput(fd_s);

	return fd;

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