m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/harddoomdev.c
blob: 4a170012236e1bb974998bb69ed0fd276e916e08 (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
#include "harddoomdev.h"

#include "harddoom.h"
#include "doomcode.h"
#include "pci.h"
#include "private_data.h"
#include "util.h"

#define PING_FREQUENCY 0x80

void doomdev_write(void __iomem *iomem, size_t addr, uint32_t data)
{
	iowrite32(data, iomem + addr);
}

uint32_t doomdev_read(void __iomem *iomem, size_t addr)
{
	return ioread32(iomem + addr);
}

void write_command(void __iomem *iomem, uint32_t command)
{
	doomdev_write(iomem, HARDDOOM_FIFO_SEND, command);
}

void enable_intr(void __iomem *iomem, uint32_t intr)
{
	uint32_t interrupts;

	interrupts = doomdev_read(iomem, HARDDOOM_INTR_ENABLE);
	doomdev_write(iomem, HARDDOOM_INTR_ENABLE, interrupts | intr);
}

void disable_intr(void __iomem *iomem, uint32_t intr)
{
	uint32_t interrupts;

	interrupts = doomdev_read(iomem, HARDDOOM_INTR_ENABLE);
	doomdev_write(iomem, HARDDOOM_INTR_ENABLE, interrupts & (~intr));
}

void send_command(struct doom_data *doom_data, uint32_t command)
{
	uint32_t free;

	free = doomdev_read(doom_data->iomem, HARDDOOM_FIFO_FREE);
	if (free == 0) {
		deactivate_intr(doom_data->iomem, HARDDOOM_INTR_PONG_ASYNC);
		free = doomdev_read(doom_data->iomem, HARDDOOM_FIFO_FREE);
	}

	if (free == 0) {
		enable_intr(doom_data->iomem, HARDDOOM_INTR_PONG_ASYNC);
		down(&doom_data->pong_async_sem);
	}

	if (doom_data->cmd_counter > PING_FREQUENCY) {
		doom_data->cmd_counter = 0;
		write_command(doom_data->iomem, HARDDOOM_CMD_PING_ASYNC);
	} else {
		doom_data->cmd_counter++;
	}

	write_command(doom_data->iomem, command);
}

uint32_t get_interrupts(void __iomem *iomem)
{
	return doomdev_read(iomem, HARDDOOM_INTR);
}

uint32_t get_enabled_interrupts(void __iomem *iomem)
{
	return doomdev_read(iomem, HARDDOOM_INTR_ENABLE);
}

void deactivate_intr(void __iomem *iomem, uint32_t intr)
{
	doomdev_write(iomem, HARDDOOM_INTR, intr);
}

void ping_sync(struct doom_data *doom_data)
{
	send_command(doom_data, HARDDOOM_CMD_PING_SYNC);
}

void set_surf_src_pt(struct doom_data *doom_data, dma_addr_t page_table)
{
	if (doom_data->surf_src_pt != page_table) {
		doom_data->surf_src_pt = page_table;
		send_command(doom_data, HARDDOOM_CMD_SURF_SRC_PT(page_table));
	}
}

void set_surf_dst_pt(struct doom_data *doom_data, dma_addr_t page_table)
{
	if (doom_data->surf_dst_pt != page_table) {
		doom_data->surf_dst_pt = page_table;
		send_command(doom_data, HARDDOOM_CMD_SURF_DST_PT(page_table));
	}
}

void set_surf_dims(struct doom_data *doom_data, uint32_t width, uint32_t height)
{
	if (doom_data->surf_dims_w != width || doom_data->surf_dims_h != height) {
		doom_data->surf_dims_w = width;
		doom_data->surf_dims_h = height;
		send_command(doom_data, HARDDOOM_CMD_SURF_DIMS(width, height));
	}
}

void set_xy_a(struct doom_data *doom_data, uint16_t x, uint16_t y)
{
	send_command(doom_data, HARDDOOM_CMD_XY_A(x, y));
}

void set_xy_b(struct doom_data *doom_data, uint16_t x, uint16_t y)
{
	send_command(doom_data, HARDDOOM_CMD_XY_B(x, y));
}

void set_fill_color(struct doom_data *doom_data, uint8_t color)
{
	send_command(doom_data, HARDDOOM_CMD_FILL_COLOR(color));
}

void set_texture_pt(struct doom_data *doom_data, dma_addr_t page_table_dev)
{
	if (doom_data->texture_pt != page_table_dev) {
		doom_data->texture_pt = page_table_dev;
		send_command(doom_data, HARDDOOM_CMD_TEXTURE_PT(page_table_dev));
	}
}

void set_texture_dims(struct doom_data *doom_data, uint32_t size, uint16_t height)
{
	if (doom_data->texture_dims_s != size ||
			doom_data->texture_dims_h != height) {
		doom_data->texture_dims_s = size;
		doom_data->texture_dims_h = height;
		send_command(doom_data, HARDDOOM_CMD_TEXTURE_DIMS(size,height));
	}
}

void set_flat(struct doom_data *doom_data, dma_addr_t flat_dev)
{
	if (doom_data->flat_addr != flat_dev) {
		doom_data->flat_addr = flat_dev;
		send_command(doom_data, HARDDOOM_CMD_FLAT_ADDR(flat_dev));
	}
}

void set_colormap(struct doom_data *doom_data, dma_addr_t colors_dev)
{
	if (doom_data->colors_addr != colors_dev) {
		doom_data->colors_addr = colors_dev;
		send_command(doom_data, HARDDOOM_CMD_COLORMAP_ADDR(colors_dev));
	}
}

void set_trans(struct doom_data *doom_data, dma_addr_t colors_dev)
{
	if (doom_data->trans_addr != colors_dev) {
		doom_data->trans_addr = colors_dev;
		send_command(doom_data, HARDDOOM_CMD_TRANSLATION_ADDR(colors_dev));
	}
}

void set_ustart(struct doom_data *doom_data, uint32_t ustart)
{
	send_command(doom_data, HARDDOOM_CMD_USTART(ustart));
}

void set_ustep(struct doom_data *doom_data, uint32_t ustep)
{
	send_command(doom_data, HARDDOOM_CMD_USTEP(ustep));
}

void set_vstart(struct doom_data *doom_data, uint32_t vstart)
{
	send_command(doom_data, HARDDOOM_CMD_VSTART(vstart));
}

void set_vstep(struct doom_data *doom_data, uint32_t vstep)
{
	send_command(doom_data, HARDDOOM_CMD_VSTEP(vstep));
}

void set_draw_params(struct doom_data *doom_data, uint8_t flags)
{
	if (doom_data->draw_params != flags) {
		doom_data->draw_params = flags;
		send_command(doom_data, HARDDOOM_CMD_DRAW_PARAMS(flags));
	}
}

void fill_rect(struct surface_data *surface_data, struct doomdev_fill_rect *rect)
{
	set_surf_dst_pt(surface_data->doom_data, surface_data->page_table_dev);
	set_surf_dims(surface_data->doom_data, surface_data->width, surface_data->height);
	set_xy_a(surface_data->doom_data, rect->pos_dst_x, rect->pos_dst_y);
	set_fill_color(surface_data->doom_data, rect->color);

	send_command(surface_data->doom_data, HARDDOOM_CMD_FILL_RECT(rect->width, rect->height));
}

void draw_line(struct surface_data *surface_data, struct doomdev_line *line)
{
	set_surf_dst_pt(surface_data->doom_data, surface_data->page_table_dev);
	set_surf_dims(surface_data->doom_data, surface_data->width, surface_data->height);
	set_xy_a(surface_data->doom_data, line->pos_a_x, line->pos_a_y);
	set_xy_b(surface_data->doom_data, line->pos_b_x, line->pos_b_y);
	set_fill_color(surface_data->doom_data, line->color);

	send_command(surface_data->doom_data, HARDDOOM_CMD_DRAW_LINE);
}

void copy_rect(struct surface_data *dst_data, struct surface_data *src_data,
		struct doomdev_copy_rect *rect)
{
	set_surf_dst_pt(dst_data->doom_data, dst_data->page_table_dev);
	set_surf_src_pt(dst_data->doom_data, src_data->page_table_dev);
	set_surf_dims(dst_data->doom_data, dst_data->width, dst_data->height);
	set_xy_a(dst_data->doom_data, rect->pos_dst_x, rect->pos_dst_y);
	set_xy_b(dst_data->doom_data, rect->pos_src_x, rect->pos_src_y);

	send_command(dst_data->doom_data, HARDDOOM_CMD_COPY_RECT(rect->width, rect->height));
}

void draw_column(struct surface_data *surface_data,
		struct texture_data *texture_data, struct doomdev_column *column,
		struct colors_data *colors_data, struct colors_data *trans_data,
		uint8_t flags, uint8_t trans_idx)
{
	uint16_t y1, y2;

	y1 = min(column->y1, column->y2);
	y2 = max(column->y1, column->y2);

	set_surf_dst_pt(surface_data->doom_data, surface_data->page_table_dev);
	set_surf_dims(surface_data->doom_data, surface_data->width, surface_data->height);
	set_draw_params(surface_data->doom_data, flags);
	set_xy_a(surface_data->doom_data, column->x, y1);
	set_xy_b(surface_data->doom_data, column->x, y2);

	if (!(flags & HARDDOOM_DRAW_PARAMS_FUZZ)) {
		set_texture_pt(surface_data->doom_data,
				texture_data->page_table_dev);
		set_texture_dims(surface_data->doom_data,
				texture_data->size, texture_data->height);
		set_ustart(surface_data->doom_data, column->ustart);
		set_ustep(surface_data->doom_data, column->ustep);
	}

	if (flags & HARDDOOM_DRAW_PARAMS_FUZZ ||
			flags & HARDDOOM_DRAW_PARAMS_COLORMAP) {
		set_colormap(surface_data->doom_data, colors_data->colors_dev +
				HARDDOOM_COLORMAP_SIZE * column->colormap_idx);
	}

	if (flags & HARDDOOM_DRAW_PARAMS_TRANSLATE) {
		set_trans(surface_data->doom_data, trans_data->colors_dev +
				HARDDOOM_COLORMAP_SIZE * trans_idx);
	}

	send_command(surface_data->doom_data,
			HARDDOOM_CMD_DRAW_COLUMN(column->texture_offset));
}

void draw_span(struct surface_data *surface_data, struct flat_data *flat_data,
		struct doomdev_span *span)
{
	uint16_t x1, x2;

	x1 = min(span->x1, span->x2);
	x2 = max(span->x1, span->x2);

	set_surf_dst_pt(surface_data->doom_data, surface_data->page_table_dev);
	set_surf_dims(surface_data->doom_data, surface_data->width,
			surface_data->height);
	set_flat(surface_data->doom_data, flat_data->flat_dev);
	set_xy_a(surface_data->doom_data, x1, span->y);
	set_xy_b(surface_data->doom_data, x2, span->y);
	set_ustart(surface_data->doom_data, span->ustart);
	set_ustep(surface_data->doom_data, span->ustep);
	set_vstart(surface_data->doom_data, span->vstart);
	set_vstep(surface_data->doom_data, span->vstep);
	set_draw_params(surface_data->doom_data, 0);

	send_command(surface_data->doom_data, HARDDOOM_CMD_DRAW_SPAN);
}

void draw_background(struct surface_data *surface_data,
		struct flat_data *flat_data)
{
	set_surf_dst_pt(surface_data->doom_data, surface_data->page_table_dev);
	set_flat(surface_data->doom_data, flat_data->flat_dev);

	send_command(surface_data->doom_data, HARDDOOM_CMD_DRAW_BACKGROUND);
}

uint32_t doomdev_read_stat(void __iomem *iomem, size_t stat)
{
	return doomdev_read(iomem, HARDDOOM_STATS(stat));
}

void load_microcode(void __iomem *iomem)
{
	int i;

	doomdev_write(iomem, HARDDOOM_FE_CODE_ADDR, 0);
	for (i = 0; i < ARRAY_SIZE(doomcode); i++) {
		doomdev_write(iomem, HARDDOOM_FE_CODE_WINDOW, doomcode[i]);
	}
}

void start_dev(struct pci_dev *dev)
{
	struct doom_data *data;
	void __iomem *iomem;

	data = pci_get_drvdata(dev);
	iomem = data->iomem;

	load_microcode(iomem);
	doomdev_write(iomem, HARDDOOM_RESET, 0xffffffe);
	doomdev_write(iomem, HARDDOOM_INTR,
			HARDDOOM_INTR_MASK ^ HARDDOOM_INTR_PONG_ASYNC);
	doomdev_write(iomem, HARDDOOM_INTR_ENABLE, HARDDOOM_INTR_MASK);
	doomdev_write(iomem, HARDDOOM_ENABLE,
			HARDDOOM_ENABLE_ALL ^ HARDDOOM_ENABLE_FETCH_CMD);

	set_surf_dst_pt(data, 0);
	set_surf_src_pt(data, 0);
	set_texture_pt(data, 0);
	set_flat(data, 0);
	set_colormap(data, 0);
	set_surf_dims(data, 0, 0);
	set_texture_dims(data, 0, 0);
	set_draw_params(data, 0);
}

void shutdown_dev(struct pci_dev *dev)
{
	struct doom_data *data;
	void __iomem *iomem;

	data = pci_get_drvdata(dev);
	iomem = data->iomem;

	doomdev_write(iomem, HARDDOOM_ENABLE, 0);
	doomdev_write(iomem, HARDDOOM_INTR_ENABLE, 0);
	doomdev_read(iomem, 0);
}