rename vertex_array to vertex_buffer
[ps3freebsd_ps3gpu_test.git] / vram_dma.c
blobd921b87b38eb5bdc6f72b5319fef84305245bb56
1 /*-
2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD$
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/uio.h>
37 #include <sys/ioctl.h>
38 #include <sys/mman.h>
39 #include <sys/fbio.h>
40 #include <sys/consio.h>
41 #include <fcntl.h>
42 #include <unistd.h>
44 #include "ps3gpu_ctl.h"
45 #include "ps3gpu_mth.h"
46 #include "reset_gpu_state.h"
47 #include "util.h"
49 int
50 main(int argc, char **argv)
52 struct ps3gpu_ctl_context_allocate context_allocate;
53 struct ps3gpu_ctl_context_free context_free;
54 struct ps3gpu_ctl_memory_allocate memory_allocate;
55 struct ps3gpu_ctl_setup_control setup_control;
56 struct ps3gpu_ctl_flip flip;
57 int context_id;
58 volatile uint32_t *control;
59 uint32_t *fifo, *reset_gpu, *vram;
60 unsigned long fifo_handle, vram_handle;
61 unsigned int fifo_gaddr, reset_gpu_gaddr, vram_gaddr;
62 int fd = -1;
63 int x, y;
64 int x1, y1, x2, y2, w, h;
65 int err;
67 /* Open GPU device */
69 fd = open(PS3GPU_DEV_PATH, O_RDWR);
70 if (fd < 0) {
71 perror("open");
72 goto done;
75 /* Create GPU context */
77 context_allocate.vram_size = 64; /* MB */
79 err = ioctl(fd, PS3GPU_CTL_CONTEXT_ALLOCATE, &context_allocate);
80 if (err < 0) {
81 perror("ioctl");
82 goto done;
85 context_id = context_allocate.context_id;
87 printf("context id %d\n", context_id);
88 printf("control handle 0x%lx size %d\n",
89 context_allocate.control_handle, context_allocate.control_size);
91 /* Map control registers */
93 control = mmap(NULL, context_allocate.control_size,
94 PROT_READ | PROT_WRITE, MAP_SHARED, fd, context_allocate.control_handle);
95 if (control == (void *) MAP_FAILED) {
96 perror("mmap");
97 goto done;
100 /* Allocate FIFO */
102 memory_allocate.context_id = context_id;
103 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_GART;
104 memory_allocate.size = 64 * 1024;
105 memory_allocate.align = 12;
107 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
108 if (err < 0) {
109 perror("ioctl");
110 goto done;
113 fifo_handle = memory_allocate.handle;
114 fifo_gaddr = memory_allocate.gpu_addr;
116 printf("fifo handle 0x%lx gpu addr 0x%08x\n",
117 fifo_handle, fifo_gaddr);
119 /* Map FIFO */
121 fifo = mmap(NULL, memory_allocate.size,
122 PROT_READ | PROT_WRITE, MAP_SHARED, fd, fifo_handle);
123 if (fifo == (void *) MAP_FAILED) {
124 perror("mmap");
125 goto done;
128 /* Setup FIFO */
130 setup_control.context_id = context_id;
131 setup_control.put = fifo_handle;
132 setup_control.get = fifo_handle;
133 setup_control.ref = 0xdeadbabe;
135 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
136 if (err < 0) {
137 perror("ioctl");
138 goto done;
141 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
142 control[0x10], control[0x11], control[0x12]);
144 /* Allocate FIFO for resetting GPU state */
146 memory_allocate.context_id = context_id;
147 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_GART;
148 memory_allocate.size = 4 * 1024;
149 memory_allocate.align = 12;
151 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
152 if (err < 0) {
153 perror("ioctl");
154 goto done;
157 reset_gpu_gaddr = memory_allocate.gpu_addr;
159 printf("reset GPU state handle 0x%lx gpu addr 0x%08x\n",
160 memory_allocate.handle, reset_gpu_gaddr);
162 /* Map FIFO for resetting GPU state */
164 reset_gpu = mmap(NULL, memory_allocate.size,
165 PROT_READ | PROT_WRITE, MAP_SHARED, fd, memory_allocate.handle);
166 if (reset_gpu == (void *) MAP_FAILED) {
167 perror("mmap");
168 goto done;
171 memcpy(reset_gpu, reset_gpu_state, reset_gpu_state_size);
173 /* Kick FIFO */
175 fifo[0] = PS3GPU_MTH_HDR(0, 0, reset_gpu_gaddr | PS3GPU_MTH_ADDR_CALL);
176 fifo[1] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF);
177 fifo[2] = 0xcafef00d;
179 control[0x10] = fifo_gaddr + 3 * sizeof(uint32_t);
181 err = wait_fifo_idle(control);
182 if (err < 0) {
183 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
184 control[0x10], control[0x11], control[0x12]);
185 dump_fifo(stderr, fifo, 0x400);
186 goto done;
189 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
190 control[0x10], control[0x11], control[0x12]);
192 /* Allocate VRAM */
194 memory_allocate.context_id = context_id;
195 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_VIDEO;
196 memory_allocate.size = ROUNDUP(DISPLAY_HEIGHT * DISPLAY_PITCH, 4 * 1024);
197 memory_allocate.align = 12;
199 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
200 if (err < 0) {
201 perror("ioctl");
202 goto done;
205 vram_handle = memory_allocate.handle;
206 vram_gaddr = memory_allocate.gpu_addr;
208 printf("VRAM handle 0x%lx gpu addr 0x%08x\n",
209 vram_handle, vram_gaddr);
211 /* Map VRAM */
213 vram = mmap(NULL, memory_allocate.size,
214 PROT_READ | PROT_WRITE, MAP_SHARED, fd, vram_handle);
215 if (vram == (void *) MAP_FAILED) {
216 perror("mmap");
217 goto done;
220 memset32(vram, 0xff404040, DISPLAY_HEIGHT * DISPLAY_WIDTH);
222 /* Flip */
224 flip.context_id = context_id;
225 flip.head = PS3GPU_CTL_HEAD_A;
226 flip.offset = vram_handle;
228 err = ioctl(fd, PS3GPU_CTL_FLIP, &flip);
229 if (err < 0) {
230 perror("ioctl");
231 goto done;
234 flip.context_id = context_id;
235 flip.head = PS3GPU_CTL_HEAD_B;
236 flip.offset = vram_handle;
238 err = ioctl(fd, PS3GPU_CTL_FLIP, &flip);
239 if (err < 0) {
240 perror("ioctl");
241 goto done;
244 usleep(2000000);
246 /* Test RSX DMA */
248 x2 = 800;
249 y2 = 400;
250 w = 200;
251 h = 200;
253 for (y = y2; y < y2 + h; y++) {
254 for (x = x2; x < x2 + w; x++) {
255 if (y < (y2 + h / 2))
256 vram[y * DISPLAY_WIDTH + x] = 0xff00ff00;
257 else
258 vram[y * DISPLAY_WIDTH + x] = 0xffffff00;
262 /* RSX DMA supports positive and negative pitches */
264 /* Test positive pitch */
266 setup_control.context_id = context_id;
267 setup_control.put = fifo_handle;
268 setup_control.get = fifo_handle;
269 setup_control.ref = 0xdeadbabe;
271 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
272 if (err < 0) {
273 perror("ioctl");
274 goto done;
277 x1 = 200;
278 y1 = 800;
280 err = transfer_data(fifo, 0xfeed0000, 0xfeed0000,
281 vram_gaddr + y1 * DISPLAY_PITCH + x1 * DISPLAY_BPP, DISPLAY_PITCH,
282 vram_gaddr + y2 * DISPLAY_PITCH + x2 * DISPLAY_BPP, DISPLAY_PITCH,
283 w * DISPLAY_BPP, h);
285 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
287 err = wait_fifo_idle(control);
288 if (err < 0) {
289 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
290 control[0x10], control[0x11], control[0x12]);
291 dump_fifo(stderr, fifo, 0x400);
292 goto done;
295 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
296 control[0x10], control[0x11], control[0x12]);
298 /* Test negative pitch */
300 setup_control.context_id = context_id;
301 setup_control.put = fifo_handle;
302 setup_control.get = fifo_handle;
303 setup_control.ref = 0xdeadbabe;
305 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
306 if (err < 0) {
307 perror("ioctl");
308 goto done;
311 x1 = 1500;
312 y1 = 800;
314 err = transfer_data(fifo, 0xfeed0000, 0xfeed0000,
315 vram_gaddr + (y1 + h - 1) * DISPLAY_PITCH + x1 * DISPLAY_BPP, -1 * DISPLAY_PITCH,
316 vram_gaddr + (y2 + h - 1) * DISPLAY_PITCH + x2 * DISPLAY_BPP, -1 * DISPLAY_PITCH,
317 w * DISPLAY_BPP, h);
319 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
321 err = wait_fifo_idle(control);
322 if (err < 0) {
323 fprintf(stderr, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
324 control[0x10], control[0x11], control[0x12]);
325 dump_fifo(stderr, fifo, 0x400);
326 goto done;
329 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
330 control[0x10], control[0x11], control[0x12]);
332 save_image("image.argb", (const char *) vram, DISPLAY_PITCH * DISPLAY_HEIGHT);
334 /* Destroy GPU context */
336 context_free.context_id = context_id;
338 err = ioctl(fd, PS3GPU_CTL_CONTEXT_FREE, &context_free);
339 if (err < 0) {
340 perror("ioctl");
341 goto done;
344 done:
346 if (fd >= 0)
347 close(fd);
349 /* Restore console */
351 ioctl(0, SW_TEXT_80x25, NULL);
353 exit(0);