2 * Copyright (C) 2011, 2012 glevand <geoffrey.levand@mail.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
34 #include <sys/types.h>
37 #include <sys/ioctl.h>
40 #include <sys/consio.h>
44 #include "ps3gpu_ctl.h"
45 #include "ps3gpu_mth.h"
46 #include "reset_gpu_state.h"
49 #define PS3GPU_DEV_PATH "/dev/ps3gpu"
51 #define DISPLAY_WIDTH 1920
52 #define DISPLAY_HEIGHT 1080
54 #define DISPLAY_PITCH (DISPLAY_WIDTH * DISPLAY_BPP)
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_flip flip
;
65 volatile uint32_t *control
;
66 uint32_t *fifo
, *reset_gpu
, *vram
;
67 unsigned long fifo_handle
, vram_handle
;
68 unsigned int fifo_gaddr
, reset_gpu_gaddr
, vram_gaddr
;
71 int x1
, y1
, x2
, y2
, w
, h
;
76 fd
= open(PS3GPU_DEV_PATH
, O_RDWR
);
82 /* Create GPU context */
84 context_allocate
.vram_size
= 64; /* MB */
86 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_ALLOCATE
, &context_allocate
);
92 context_id
= context_allocate
.context_id
;
94 printf("context id %d\n", context_id
);
95 printf("control handle 0x%lx size %d\n",
96 context_allocate
.control_handle
, context_allocate
.control_size
);
98 /* Map control registers */
100 control
= mmap(NULL
, context_allocate
.control_size
,
101 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, context_allocate
.control_handle
);
102 if (control
== (void *) MAP_FAILED
) {
109 memory_allocate
.context_id
= context_id
;
110 memory_allocate
.type
= PS3GPU_CTL_MEMORY_TYPE_GART
;
111 memory_allocate
.size
= 64 * 1024;
112 memory_allocate
.align
= 12;
114 err
= ioctl(fd
, PS3GPU_CTL_MEMORY_ALLOCATE
, &memory_allocate
);
120 fifo_handle
= memory_allocate
.handle
;
121 fifo_gaddr
= memory_allocate
.gpu_addr
;
123 printf("fifo handle 0x%lx gpu addr 0x%08x\n",
124 fifo_handle
, fifo_gaddr
);
128 fifo
= mmap(NULL
, memory_allocate
.size
,
129 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, fifo_handle
);
130 if (fifo
== (void *) MAP_FAILED
) {
137 setup_control
.context_id
= context_id
;
138 setup_control
.put
= fifo_handle
;
139 setup_control
.get
= fifo_handle
;
140 setup_control
.ref
= 0xdeadbabe;
142 err
= ioctl(fd
, PS3GPU_CTL_SETUP_CONTROL
, &setup_control
);
148 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
149 control
[0x10], control
[0x11], control
[0x12]);
151 /* Allocate FIFO for resetting GPU state */
153 memory_allocate
.context_id
= context_id
;
154 memory_allocate
.type
= PS3GPU_CTL_MEMORY_TYPE_GART
;
155 memory_allocate
.size
= 4 * 1024;
156 memory_allocate
.align
= 12;
158 err
= ioctl(fd
, PS3GPU_CTL_MEMORY_ALLOCATE
, &memory_allocate
);
164 reset_gpu_gaddr
= memory_allocate
.gpu_addr
;
166 printf("reset GPU state handle 0x%lx gpu addr 0x%08x\n",
167 memory_allocate
.handle
, reset_gpu_gaddr
);
169 /* Map FIFO for resetting GPU state */
171 reset_gpu
= mmap(NULL
, memory_allocate
.size
,
172 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, memory_allocate
.handle
);
173 if (reset_gpu
== (void *) MAP_FAILED
) {
178 memcpy(reset_gpu
, reset_gpu_state
, reset_gpu_state_size
);
182 fifo
[0] = PS3GPU_MTH_HDR(0, 0, reset_gpu_gaddr
| PS3GPU_MTH_ADDR_CALL
);
183 fifo
[1] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF
);
184 fifo
[2] = 0xcafef00d;
186 control
[0x10] = fifo_gaddr
+ 3 * sizeof(uint32_t);
188 while (control
[0x10] != control
[0x11])
191 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
192 control
[0x10], control
[0x11], control
[0x12]);
196 memory_allocate
.context_id
= context_id
;
197 memory_allocate
.type
= PS3GPU_CTL_MEMORY_TYPE_VIDEO
;
198 memory_allocate
.size
= 9 * 1024 * 1024;
199 memory_allocate
.align
= 12;
201 err
= ioctl(fd
, PS3GPU_CTL_MEMORY_ALLOCATE
, &memory_allocate
);
207 vram_handle
= memory_allocate
.handle
;
208 vram_gaddr
= memory_allocate
.gpu_addr
;
210 printf("VRAM handle 0x%lx gpu addr 0x%08x\n",
211 vram_handle
, vram_gaddr
);
215 vram
= mmap(NULL
, memory_allocate
.size
,
216 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, vram_handle
);
217 if (vram
== (void *) MAP_FAILED
) {
222 memset32(vram
, 0xff404040, DISPLAY_HEIGHT
* DISPLAY_WIDTH
);
226 flip
.context_id
= context_id
;
227 flip
.head
= PS3GPU_CTL_HEAD_A
;
228 flip
.offset
= vram_handle
;
230 err
= ioctl(fd
, PS3GPU_CTL_FLIP
, &flip
);
236 flip
.context_id
= context_id
;
237 flip
.head
= PS3GPU_CTL_HEAD_B
;
238 flip
.offset
= vram_handle
;
240 err
= ioctl(fd
, PS3GPU_CTL_FLIP
, &flip
);
255 for (y
= y2
; y
< y2
+ h
; y
++) {
256 for (x
= x2
; x
< x2
+ w
; x
++) {
257 if (y
< (y2
+ h
/ 2))
258 vram
[y
* DISPLAY_WIDTH
+ x
] = 0xff00ff00;
260 vram
[y
* DISPLAY_WIDTH
+ x
] = 0xffffff00;
264 /* RSX DMA supports positive and negative pitches */
266 /* Test positive pitch */
268 setup_control
.context_id
= context_id
;
269 setup_control
.put
= fifo_handle
;
270 setup_control
.get
= fifo_handle
;
271 setup_control
.ref
= 0xdeadbabe;
273 err
= ioctl(fd
, PS3GPU_CTL_SETUP_CONTROL
, &setup_control
);
282 err
= transfer_data(fifo
, 0xfeed0000, 0xfeed0000,
283 vram_gaddr
+ y1
* DISPLAY_PITCH
+ x1
* DISPLAY_BPP
, DISPLAY_PITCH
,
284 vram_gaddr
+ y2
* DISPLAY_PITCH
+ x2
* DISPLAY_BPP
, DISPLAY_PITCH
,
287 control
[0x10] = fifo_gaddr
+ err
* sizeof(uint32_t);
289 while (control
[0x10] != control
[0x11])
292 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
293 control
[0x10], control
[0x11], control
[0x12]);
295 /* Test negative pitch */
297 setup_control
.context_id
= context_id
;
298 setup_control
.put
= fifo_handle
;
299 setup_control
.get
= fifo_handle
;
300 setup_control
.ref
= 0xdeadbabe;
302 err
= ioctl(fd
, PS3GPU_CTL_SETUP_CONTROL
, &setup_control
);
311 err
= transfer_data(fifo
, 0xfeed0000, 0xfeed0000,
312 vram_gaddr
+ (y1
+ h
- 1) * DISPLAY_PITCH
+ x1
* DISPLAY_BPP
, -1 * DISPLAY_PITCH
,
313 vram_gaddr
+ (y2
+ h
- 1) * DISPLAY_PITCH
+ x2
* DISPLAY_BPP
, -1 * DISPLAY_PITCH
,
316 control
[0x10] = fifo_gaddr
+ err
* sizeof(uint32_t);
318 while (control
[0x10] != control
[0x11])
321 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
322 control
[0x10], control
[0x11], control
[0x12]);
324 save_image("image.argb", (const char *) vram
, DISPLAY_PITCH
* DISPLAY_HEIGHT
);
326 /* Destroy GPU context */
328 context_free
.context_id
= context_id
;
330 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_FREE
, &context_free
);
341 /* Restore console */
343 ioctl(0, SW_TEXT_80x25
, NULL
);