fix printf format
[ps3freebsd_ps3gpu_test.git] / display_buffer.c
blobcbdb09f1b8fc5b81d64a991ecf303661eaffdae9
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 #define PS3GPU_DEV_PATH "/dev/ps3gpu"
51 #define DISPLAY_WIDTH 1920
52 #define DISPLAY_HEIGHT 1080
53 #define DISPLAY_BPP 4
54 #define DISPLAY_PITCH (DISPLAY_WIDTH * DISPLAY_BPP)
56 int
57 main(int argc, char **argv)
59 struct ps3gpu_ctl_context_allocate context_allocate;
60 struct ps3gpu_ctl_context_free context_free;
61 struct ps3gpu_ctl_memory_allocate memory_allocate;
62 struct ps3gpu_ctl_setup_control setup_control;
63 struct ps3gpu_ctl_display_buffer_set display_buffer_set;
64 int context_id;
65 volatile uint32_t *control;
66 volatile uint8_t *driver_info;
67 uint32_t *fifo, *reset_gpu, *db[2];
68 unsigned long fifo_handle, db_handle[2];
69 unsigned int fifo_gaddr, reset_gpu_gaddr, db_gaddr[2];
70 int fd = -1;
71 int err;
73 /* Open GPU device */
75 fd = open(PS3GPU_DEV_PATH, O_RDWR);
76 if (fd < 0) {
77 perror("open");
78 goto done;
81 /* Create GPU context */
83 context_allocate.vram_size = 64; /* MB */
85 err = ioctl(fd, PS3GPU_CTL_CONTEXT_ALLOCATE, &context_allocate);
86 if (err < 0) {
87 perror("ioctl");
88 goto done;
91 context_id = context_allocate.context_id;
93 printf("context id %d\n", context_id);
94 printf("control handle 0x%lx size %d\n",
95 context_allocate.control_handle, context_allocate.control_size);
96 printf("driver_info handle 0x%lx size %d\n",
97 context_allocate.driver_info_handle, context_allocate.driver_info_size);
99 /* Map control registers */
101 control = mmap(NULL, context_allocate.control_size,
102 PROT_READ | PROT_WRITE, MAP_SHARED, fd, context_allocate.control_handle);
103 if (control == (void *) MAP_FAILED) {
104 perror("mmap");
105 goto done;
108 /* Map driver info */
110 driver_info = mmap(NULL, context_allocate.driver_info_size,
111 PROT_READ | PROT_WRITE, MAP_SHARED, fd, context_allocate.driver_info_handle);
112 if (driver_info == (void *) MAP_FAILED) {
113 perror("mmap");
114 goto done;
117 printf("channel id %d\n", get_channel_id(driver_info));
119 /* Allocate FIFO */
121 memory_allocate.context_id = context_id;
122 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_GART;
123 memory_allocate.size = 64 * 1024;
124 memory_allocate.align = 12;
126 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
127 if (err < 0) {
128 perror("ioctl");
129 goto done;
132 fifo_handle = memory_allocate.handle;
133 fifo_gaddr = memory_allocate.gpu_addr;
135 printf("fifo handle 0x%lx gpu addr 0x%08x\n",
136 fifo_handle, fifo_gaddr);
138 /* Map FIFO */
140 fifo = mmap(NULL, memory_allocate.size,
141 PROT_READ | PROT_WRITE, MAP_SHARED, fd, fifo_handle);
142 if (fifo == (void *) MAP_FAILED) {
143 perror("mmap");
144 goto done;
147 /* Setup FIFO */
149 setup_control.context_id = context_id;
150 setup_control.put = fifo_handle;
151 setup_control.get = fifo_handle;
152 setup_control.ref = 0xdeadbabe;
154 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
155 if (err < 0) {
156 perror("ioctl");
157 goto done;
160 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
161 control[0x10], control[0x11], control[0x12]);
163 /* Allocate FIFO for resetting GPU state */
165 memory_allocate.context_id = context_id;
166 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_GART;
167 memory_allocate.size = 4 * 1024;
168 memory_allocate.align = 12;
170 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
171 if (err < 0) {
172 perror("ioctl");
173 goto done;
176 reset_gpu_gaddr = memory_allocate.gpu_addr;
178 printf("reset GPU state handle 0x%lx gpu addr 0x%08x\n",
179 memory_allocate.handle, reset_gpu_gaddr);
181 /* Map FIFO for resetting GPU state */
183 reset_gpu = mmap(NULL, memory_allocate.size,
184 PROT_READ | PROT_WRITE, MAP_SHARED, fd, memory_allocate.handle);
185 if (reset_gpu == (void *) MAP_FAILED) {
186 perror("mmap");
187 goto done;
190 memcpy(reset_gpu, reset_gpu_state_3d, reset_gpu_state_3d_size);
192 /* Kick FIFO */
194 fifo[0] = PS3GPU_MTH_HDR(0, 0, reset_gpu_gaddr | PS3GPU_MTH_ADDR_CALL);
195 fifo[1] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF);
196 fifo[2] = 0xcafef00d;
198 control[0x10] = fifo_gaddr + 3 * sizeof(uint32_t);
200 while (control[0x10] != control[0x11])
201 usleep(1000);
203 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
204 control[0x10], control[0x11], control[0x12]);
206 /* Allocate display buffers */
208 memory_allocate.context_id = context_id;
209 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_VIDEO;
210 memory_allocate.size = 9 * 1024 * 1024;
211 memory_allocate.align = 12;
213 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
214 if (err < 0) {
215 perror("ioctl");
216 goto done;
219 db_handle[0] = memory_allocate.handle;
220 db_gaddr[0] = memory_allocate.gpu_addr;
222 printf("DB0 handle 0x%lx gpu addr 0x%08x\n",
223 db_handle[0], db_gaddr[0]);
225 db[0] = mmap(NULL, memory_allocate.size,
226 PROT_READ | PROT_WRITE, MAP_SHARED, fd, db_handle[0]);
227 if (db[0] == (void *) MAP_FAILED) {
228 perror("mmap");
229 goto done;
232 memset32(db[0], 0xff00ff00, DISPLAY_HEIGHT * DISPLAY_WIDTH);
234 memory_allocate.context_id = context_id;
235 memory_allocate.type = PS3GPU_CTL_MEMORY_TYPE_VIDEO;
236 memory_allocate.size = 9 * 1024 * 1024;
237 memory_allocate.align = 12;
239 err = ioctl(fd, PS3GPU_CTL_MEMORY_ALLOCATE, &memory_allocate);
240 if (err < 0) {
241 perror("ioctl");
242 goto done;
245 db_handle[1] = memory_allocate.handle;
246 db_gaddr[1] = memory_allocate.gpu_addr;
248 printf("DB1 handle 0x%lx gpu addr 0x%08x\n",
249 db_handle[1], db_gaddr[1]);
251 db[1] = mmap(NULL, memory_allocate.size,
252 PROT_READ | PROT_WRITE, MAP_SHARED, fd, db_handle[1]);
253 if (db[1] == (void *) MAP_FAILED) {
254 perror("mmap");
255 goto done;
258 memset32(db[1], 0xffffff00, DISPLAY_HEIGHT * DISPLAY_WIDTH);
260 /* Set display buffers */
262 display_buffer_set.context_id = context_id;
263 display_buffer_set.buffer_id = 0;
264 display_buffer_set.width = DISPLAY_WIDTH;
265 display_buffer_set.height = DISPLAY_HEIGHT;
266 display_buffer_set.pitch = DISPLAY_PITCH;
267 display_buffer_set.offset = db_handle[0];
269 err = ioctl(fd, PS3GPU_CTL_DISPLAY_BUFFER_SET, &display_buffer_set);
270 if (err < 0) {
271 perror("ioctl");
272 goto done;
275 display_buffer_set.context_id = context_id;
276 display_buffer_set.buffer_id = 1;
277 display_buffer_set.width = DISPLAY_WIDTH;
278 display_buffer_set.height = DISPLAY_HEIGHT;
279 display_buffer_set.pitch = DISPLAY_PITCH;
280 display_buffer_set.offset = db_handle[1];
282 err = ioctl(fd, PS3GPU_CTL_DISPLAY_BUFFER_SET, &display_buffer_set);
283 if (err < 0) {
284 perror("ioctl");
285 goto done;
288 /* Flip display buffer 0 */
290 setup_control.context_id = context_id;
291 setup_control.put = fifo_handle;
292 setup_control.get = fifo_handle;
293 setup_control.ref = 0xdeadbabe;
295 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
296 if (err < 0) {
297 perror("ioctl");
298 goto done;
301 err = flip_display_buffer(fifo, get_channel_id(driver_info), 0, 0);
303 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
305 while (control[0x10] != control[0x11])
306 usleep(1000);
308 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
309 control[0x10], control[0x11], control[0x12]);
311 usleep(3000000);
313 /* Flip display buffer 1 */
315 setup_control.context_id = context_id;
316 setup_control.put = fifo_handle;
317 setup_control.get = fifo_handle;
318 setup_control.ref = 0xdeadbabe;
320 err = ioctl(fd, PS3GPU_CTL_SETUP_CONTROL, &setup_control);
321 if (err < 0) {
322 perror("ioctl");
323 goto done;
326 err = flip_display_buffer(fifo, get_channel_id(driver_info), 1, 0);
328 control[0x10] = fifo_gaddr + err * sizeof(uint32_t);
330 while (control[0x10] != control[0x11])
331 usleep(1000);
333 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
334 control[0x10], control[0x11], control[0x12]);
336 /* Destroy GPU context */
338 context_free.context_id = context_id;
340 err = ioctl(fd, PS3GPU_CTL_CONTEXT_FREE, &context_free);
341 if (err < 0) {
342 perror("ioctl");
343 goto done;
346 done:
348 if (fd >= 0)
349 close(fd);
351 /* Restore console */
353 ioctl(0, SW_TEXT_80x25, NULL);
355 exit(0);