m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/surface.c
blob: f4fa64f8a4302e623874e5916d808b59ae645377 (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#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"

void free_texture(struct texture_data *texture_data);
int texture_release(struct inode *inode, struct file *filp)
{
	struct texture_data *texture_data;

	texture_data = filp->private_data;

	free_texture(texture_data);

	kfree(texture_data);

	return 0;
}

struct file_operations texture_fops = {
	.owner = THIS_MODULE,
	.release = texture_release

};

int verify_texture_params(struct doomdev_ioctl_create_texture *params)
{
	if (params->size > 4 * 1024 * 1024) {
		return -EOVERFLOW;
	}

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

	return 0;
}

int alloc_texture(struct doomdev_ioctl_create_texture *params,
		struct texture_data *texture_data)
{
	int err;
	int i;
	int not_written;
	int pages_needed;

	texture_data->size = params->size;
	texture_data->height = params->height;

	pages_needed = (params->size / HARDDOOM_PAGE_SIZE);
	if (params->size % HARDDOOM_PAGE_SIZE != 0) {
		pages_needed += 1;
	}

	texture_data->pages = pages_needed;
	texture_data->texture_cpu =
		dma_alloc_coherent(texture_data->doom_data->pci_device,
				params->size, &texture_data->texture_dev,
				GFP_KERNEL);
	ORFAIL_NULL(texture_data->texture_cpu, -ENOMEM, error_texture);

	texture_data->page_table_cpu =
		dma_alloc_coherent(texture_data->doom_data->pci_device,
				pages_needed * 4, &texture_data->page_table_dev,
				GFP_KERNEL);
	ORFAIL_NULL(texture_data->page_table_cpu, -ENOMEM, error_pt);

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

	not_written = copy_from_user(texture_data->texture_cpu,
			(void __user *) params->data_ptr,
			params->size);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->pages * 4, texture_data->page_table_cpu,
			texture_data->page_table_dev);
error_pt:
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->size, texture_data->texture_cpu,
			texture_data->texture_dev);
error_texture:
	return err;
}

void free_texture(struct texture_data *texture_data)
{
	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->pages * 4, texture_data->page_table_cpu,
			texture_data->page_table_dev);

	dma_free_coherent(texture_data->doom_data->pci_device,
			texture_data->size, texture_data->texture_cpu,
			texture_data->texture_dev);
}

int new_texture(struct file *filp, struct doomdev_ioctl_create_texture *arg)
{
	int err;
	struct doomdev_ioctl_create_texture *params;
	struct texture_data *texture_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_texture *) arg;

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

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

	ORFAIL(alloc_texture(params, texture_data), error_texture);

	fd = anon_inode_getfd("doom_texture", &texture_fops, texture_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_texture(texture_data);
error_texture:
	kfree(texture_data);
error_data:
	return err;
}

void free_flat(struct flat_data *flat_data);
int flat_release(struct inode *inode, struct file *filp)
{
	struct flat_data *flat_data;

	flat_data = filp->private_data;

	free_flat(flat_data);

	kfree(flat_data);

	return 0;
}

struct file_operations flat_fops = {
	.owner = THIS_MODULE,
	.release = flat_release

};

int alloc_flat(struct doomdev_ioctl_create_flat *params,
		struct flat_data *flat_data)
{
	int err;
	int not_written;

	flat_data->flat_cpu =
		dma_alloc_coherent(flat_data->doom_data->pci_device,
				HARDDOOM_FLAT_SIZE, &flat_data->flat_dev,
				GFP_KERNEL);
	ORFAIL_NULL(flat_data->flat_cpu, -ENOMEM, error_flat);

	not_written = copy_from_user(flat_data->flat_cpu,
			(void __user *) params->data_ptr, HARDDOOM_FLAT_SIZE);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(flat_data->doom_data->pci_device, HARDDOOM_FLAT_SIZE,
			flat_data->flat_cpu, flat_data->flat_dev);
error_flat:
	return err;

}

void free_flat(struct flat_data *flat_data)
{
	dma_free_coherent(flat_data->doom_data->pci_device, HARDDOOM_FLAT_SIZE,
			flat_data->flat_cpu, flat_data->flat_dev);
}

int new_flat(struct file *filp, struct doomdev_ioctl_create_flat *arg)
{
	int err;
	struct doomdev_ioctl_create_flat *params;
	struct flat_data *flat_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_flat *) arg;

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

	ORFAIL(alloc_flat(params, flat_data), error_flat);

	fd = anon_inode_getfd("doom_flat", &flat_fops, flat_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_flat(flat_data);
error_flat:
	kfree(flat_data);
error_data:
	return err;
}

void free_colors(struct colors_data *colors_data);
int colors_release(struct inode *inode, struct file *filp)
{
	struct colors_data *colors_data;

	colors_data = filp->private_data;

	free_colors(colors_data);

	kfree(colors_data);

	return 0;
}

struct file_operations colors_fops = {
	.owner = THIS_MODULE,
	.release = colors_release

};

int alloc_colors(struct doomdev_ioctl_create_colormaps *params,
		struct colors_data *colors_data)
{
	int err;
	int not_written;

	colors_data->number = params->num;

	colors_data->colors_cpu =
		dma_alloc_coherent(colors_data->doom_data->pci_device,
				HARDDOOM_COLORMAP_SIZE * params->num,
				&colors_data->colors_dev,
				GFP_KERNEL);
	ORFAIL_NULL(colors_data->colors_cpu, -ENOMEM, error_colors);

	not_written = copy_from_user(colors_data->colors_cpu,
			(void __user *) params->data_ptr,
			HARDDOOM_COLORMAP_SIZE * params->num);

	if (not_written) {
		err = -EFAULT;
		goto error_copy;
	}

	return 0;

error_copy:
	dma_free_coherent(colors_data->doom_data->pci_device,
			HARDDOOM_COLORMAP_SIZE * params->num,
			colors_data->colors_cpu, colors_data->colors_dev);
error_colors:
	return err;

}

void free_colors(struct colors_data *colors_data)
{
	dma_free_coherent(colors_data->doom_data->pci_device,
			HARDDOOM_COLORMAP_SIZE * colors_data->number,
			colors_data->colors_cpu, colors_data->colors_dev);
}

int new_colors(struct file *filp, struct doomdev_ioctl_create_colormaps *arg)
{
	int err;
	struct doomdev_ioctl_create_colormaps *params;
	struct colors_data *colors_data;
	int fd;
	struct doom_data *doom_data;

	params = (struct doomdev_ioctl_create_colormaps *) arg;

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

	ORFAIL(alloc_colors(params, colors_data), error_colors);

	fd = anon_inode_getfd("doom_colors", &colors_fops, colors_data, 0);
	ORFAIL(fd, error_inode);

	return fd;

error_inode:
	free_colors(colors_data);
error_colors:
	kfree(colors_data);
error_data:
	return err;
}

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;
}

struct file_operations surface_fops;

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;
	int err;

	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);
	if (src_fds.file->f_op != &surface_fops) {
		err = -EINVAL;
		goto error_fdget;
	}

	src_data = src_fds.file->private_data;
	if (dst_data->doom_data != src_data->doom_data) {
		err = -EINVAL;
		goto error_fdget;
	}

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

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

	err = param->rects_num;

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

error_fdget:
	fdput(src_fds);

	return err;
}

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 colors_data *colors_data;
	struct colors_data *trans_data;
	struct doomdev_column *columns;
	struct fd texture_fds;
	struct fd colors_fds;
	struct fd trans_fds;
	uint8_t flags;
	bool got_colors = false;
	bool got_trans = false;
	bool got_texture = false;
	int i;
	int err;

	surface_data = filp->private_data;

	param = (struct doomdev_surf_ioctl_draw_columns *) arg;

	flags = param->draw_flags;

	if (flags & DOOMDEV_DRAW_FLAGS_FUZZ ||
			flags & DOOMDEV_DRAW_FLAGS_COLORMAP) {
		if (flags & DOOMDEV_DRAW_FLAGS_FUZZ) {
			flags = DOOMDEV_DRAW_FLAGS_FUZZ;
		}

		colors_fds = fdget(param->colormaps_fd);
		got_colors = true;
		if (colors_fds.file->f_op != &colors_fops) {
			err = -EINVAL;
			goto error_fdget;
		}

		colors_data = colors_fds.file->private_data;
		if (surface_data->doom_data != colors_data->doom_data) {
			err = -EINVAL;
			goto error_fdget;
		}
	}

	if (flags & DOOMDEV_DRAW_FLAGS_TRANSLATE) {
		trans_fds = fdget(param->translations_fd);
		got_trans = true;
		if (trans_fds.file->f_op != &colors_fops) {
			err = -EINVAL;
			goto error_fdget;
		}

		trans_data = trans_fds.file->private_data;
		if (surface_data->doom_data != trans_data->doom_data) {
			err = -EINVAL;
			goto error_fdget;
		}
	}

	texture_fds = fdget(param->texture_fd);
	got_texture = true;
	if (texture_fds.file->f_op != &texture_fops) {
		err = -EINVAL;
		goto error_fdget;
	}

	texture_data = texture_fds.file->private_data;
	if (surface_data->doom_data != texture_data->doom_data) {
		err = -EINVAL;
		goto error_fdget;
	}

	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], colors_data,
				trans_data, flags, param->translation_idx);
	}

	err = param->columns_num;

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

error_fdget:

	if (got_texture) {
		fdput(texture_fds);
	}

	if (got_colors) {
		fdput(colors_fds);
	}

	if (got_trans) {
		fdput(trans_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;
	int err;

	surface_data = filp->private_data;

	param = (struct doomdev_surf_ioctl_draw_spans *) arg;

	flat_fds = fdget(param->flat_fd);
	if (flat_fds.file->f_op != &flat_fops) {
		err = -EINVAL;
		goto error_fdget;
	}

	flat_data = flat_fds.file->private_data;
	if (surface_data->doom_data != flat_data->doom_data) {
		err = -EINVAL;
		goto error_fdget;
	}

	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]);
	}

	err = param->spans_num;

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

error_fdget:
	fdput(flat_fds);

	return err;
}

long do_draw_background(struct file *filp, unsigned long arg)
{
	struct doomdev_surf_ioctl_draw_background *param;
	struct surface_data *surface_data;
	struct flat_data *flat_data;
	struct fd flat_fds;
	int err = 0;

	surface_data = filp->private_data;

	param = (struct doomdev_surf_ioctl_draw_background *) arg;

	flat_fds = fdget(param->flat_fd);
	if (flat_fds.file->f_op != &flat_fops) {
		err = -EINVAL;
		goto error_fdget;
	}

	flat_data = flat_fds.file->private_data;
	if (surface_data->doom_data != flat_data->doom_data) {
		err = -EINVAL;
		goto error_fdget;
	}

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

	draw_background(surface_data, flat_data);

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

error_fdget:
	fdput(flat_fds);

	return err;
}

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);
	case DOOMDEV_SURF_IOCTL_DRAW_BACKGROUND:
		return do_draw_background(filp, arg);
	default:
		return -ENOTTY;
	}
}

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

	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;
}