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.
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
41 #include <sys/consio.h>
45 #include "ps3gpu_ctl.h"
46 #include "ps3gpu_mth.h"
47 #include "reset_gpu_state.h"
51 main(int argc
, char **argv
)
53 struct ps3gpu_ctl_context_allocate context_allocate
;
54 struct ps3gpu_ctl_context_free context_free
;
56 volatile uint32_t *control
;
57 volatile uint8_t *driver_info
;
58 volatile uint8_t *reports
;
59 uint32_t *fifo
, *reset_gpu
;
60 unsigned long fifo_handle
, reset_gpu_handle
;
61 unsigned int fifo_gaddr
, reset_gpu_gaddr
;
68 fd
= open(PS3GPU_DEV_PATH
, O_RDWR
);
74 /* Create GPU context */
76 context_allocate
.vram_size
= 64; /* MB */
78 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_ALLOCATE
, &context_allocate
);
84 context_id
= context_allocate
.context_id
;
86 printf("context id %d\n", context_id
);
87 printf("control handle 0x%lx size %d\n",
88 context_allocate
.control_handle
, context_allocate
.control_size
);
89 printf("driver_info handle 0x%lx size %d\n",
90 context_allocate
.driver_info_handle
, context_allocate
.driver_info_size
);
91 printf("reports handle 0x%lx size %d\n",
92 context_allocate
.reports_handle
, context_allocate
.reports_size
);
94 /* Map control registers */
96 control
= mmap(NULL
, context_allocate
.control_size
,
97 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, context_allocate
.control_handle
);
98 if (control
== (void *) MAP_FAILED
) {
103 /* Map driver info */
105 driver_info
= mmap(NULL
, context_allocate
.driver_info_size
,
106 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, context_allocate
.driver_info_handle
);
107 if (driver_info
== (void *) MAP_FAILED
) {
114 reports
= mmap(NULL
, context_allocate
.reports_size
,
115 PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, context_allocate
.reports_handle
);
116 if (reports
== (void *) MAP_FAILED
) {
121 printf("channel id %d\n", get_channel_id(driver_info
));
122 printf("label area offset 0x%08x\n", get_label_area_offset(driver_info
));
123 printf("report data area offset 0x%08x\n", get_report_data_area_offset(driver_info
));
127 err
= memory_allocate(fd
, context_id
, PS3GPU_CTL_MEMORY_TYPE_GART
,
128 64 * 1024, 12, &fifo_handle
, &fifo_gaddr
, (void **) &fifo
);
130 perror("memory_allocate");
134 printf("FIFO handle 0x%lx gpu addr 0x%08x\n",
135 fifo_handle
, fifo_gaddr
);
139 err
= setup_control(fd
, context_id
, fifo_handle
, fifo_handle
, 0xdeadbabe);
141 perror("setup_control");
145 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
146 control
[0x10], control
[0x11], control
[0x12]);
148 /* Allocate FIFO for resetting GPU state */
150 err
= memory_allocate(fd
, context_id
, PS3GPU_CTL_MEMORY_TYPE_GART
,
151 4 * 1024, 12, &reset_gpu_handle
, &reset_gpu_gaddr
, (void **)&reset_gpu
);
153 perror("memory_allocate");
157 printf("reset GPU state handle 0x%lx gpu addr 0x%08x\n",
158 reset_gpu_handle
, reset_gpu_gaddr
);
160 memcpy(reset_gpu
, reset_gpu_state_3d
, reset_gpu_state_3d_size
);
164 fifo
[0] = PS3GPU_MTH_HDR(0, 0, reset_gpu_gaddr
| PS3GPU_MTH_ADDR_CALL
);
165 fifo
[1] = PS3GPU_MTH_HDR(1, 0, PS3GPU_MTH_ADDR_REF
);
166 fifo
[2] = 0xcafef00d;
168 control
[0x10] = fifo_gaddr
+ 3 * sizeof(uint32_t);
170 err
= wait_fifo_idle(control
);
172 fprintf(stderr
, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
173 control
[0x10], control
[0x11], control
[0x12]);
174 dump_fifo(stderr
, fifo
, 0x400);
178 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
179 control
[0x10], control
[0x11], control
[0x12]);
183 err
= setup_control(fd
, context_id
, fifo_handle
, fifo_handle
, 0xdeadbabe);
185 perror("setup_control");
189 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
190 control
[0x10], control
[0x11], control
[0x12]);
196 *get_label_addr(driver_info
, reports
, label_index
) = 0;
197 *get_label_addr(driver_info
, reports
, label_index
+ 1) = 0;
198 *get_label_addr(driver_info
, reports
, label_index
+ 2) = 0;
200 printf("label #%d value 0x%08x\n", label_index
,
201 get_label_value(driver_info
, reports
, label_index
));
202 printf("label #%d value 0x%08x\n", label_index
+ 1,
203 get_label_value(driver_info
, reports
, label_index
+ 1));
204 printf("label #%d value 0x%08x\n", label_index
+ 2,
205 get_label_value(driver_info
, reports
, label_index
+ 2));
209 err
+= write_label(fifo
+ err
, label_index
, 0xcafebabe);
210 err
+= write_backend_label(fifo
+ err
, label_index
+ 1, 0xb00bf00d);
211 err
+= write_texture_label(fifo
+ err
, label_index
+ 2, 0xdeadbeef);
213 control
[0x10] = fifo_gaddr
+ err
* sizeof(uint32_t);
215 err
= wait_fifo_idle(control
);
217 fprintf(stderr
, "FIFO timeout: put 0x%08x get 0x%08x ref 0x%08x\n",
218 control
[0x10], control
[0x11], control
[0x12]);
219 dump_fifo(stderr
, fifo
, 0x400);
223 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
224 control
[0x10], control
[0x11], control
[0x12]);
226 printf("label #%d value 0x%08x\n", label_index
,
227 get_label_value(driver_info
, reports
, label_index
));
228 printf("label #%d value 0x%08x\n", label_index
+ 1,
229 get_label_value(driver_info
, reports
, label_index
+ 1));
230 printf("label #%d value 0x%08x\n", label_index
+ 2,
231 get_label_value(driver_info
, reports
, label_index
+ 2));
233 /* Destroy GPU context */
235 context_free
.context_id
= context_id
;
237 err
= ioctl(fd
, PS3GPU_CTL_CONTEXT_FREE
, &context_free
);
248 /* Restore console */
250 ioctl(0, SW_TEXT_80x25
, NULL
);