change back to gcc
[ps3freebsd_ps3gpu_test.git] / label.c
blob472787826b23d7bcd8137f1d96fd347adecb5e93
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 <stddef.h>
33 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/uio.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <sys/fbio.h>
41 #include <sys/consio.h>
42 #include <fcntl.h>
43 #include <unistd.h>
45 #include "ps3gpu_ctl.h"
46 #include "ps3gpu_mth.h"
47 #include "reset_gpu_state.h"
48 #include "util.h"
50 int
51 main(int argc, char **argv)
53 struct ps3gpu_ctl_context_allocate context_allocate;
54 struct ps3gpu_ctl_context_free context_free;
55 int context_id;
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;
62 uint32_t label_index;
63 int fd = -1;
64 int err;
66 /* Open GPU device */
68 fd = open(PS3GPU_DEV_PATH, O_RDWR);
69 if (fd < 0) {
70 perror("open");
71 goto done;
74 /* Create GPU context */
76 context_allocate.vram_size = 64; /* MB */
78 err = ioctl(fd, PS3GPU_CTL_CONTEXT_ALLOCATE, &context_allocate);
79 if (err < 0) {
80 perror("ioctl");
81 goto done;
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) {
99 perror("mmap");
100 goto done;
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) {
108 perror("mmap");
109 goto done;
112 /* Map reports */
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) {
117 perror("mmap");
118 goto done;
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));
125 /* Allocate FIFO */
127 err = memory_allocate(fd, context_id, PS3GPU_CTL_MEMORY_TYPE_GART,
128 64 * 1024, 12, &fifo_handle, &fifo_gaddr, (void **) &fifo);
129 if (err < 0) {
130 perror("memory_allocate");
131 goto done;
134 printf("FIFO handle 0x%lx gpu addr 0x%08x\n",
135 fifo_handle, fifo_gaddr);
137 /* Setup FIFO */
139 err = setup_control(fd, context_id, fifo_handle, fifo_handle, 0xdeadbabe);
140 if (err < 0) {
141 perror("setup_control");
142 goto done;
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);
152 if (err < 0) {
153 perror("memory_allocate");
154 goto done;
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);
162 /* Kick FIFO */
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);
171 if (err < 0) {
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);
175 goto done;
178 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
179 control[0x10], control[0x11], control[0x12]);
181 /* Test label */
183 err = setup_control(fd, context_id, fifo_handle, fifo_handle, 0xdeadbabe);
184 if (err < 0) {
185 perror("setup_control");
186 goto done;
189 printf("FIFO put 0x%08x get 0x%08x ref 0x%08x\n",
190 control[0x10], control[0x11], control[0x12]);
192 label_index = 128;
194 /* Reset label */
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));
207 /* Write label */
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);
216 if (err < 0) {
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);
220 goto done;
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);
238 if (err < 0) {
239 perror("ioctl");
240 goto done;
243 done:
245 if (fd >= 0)
246 close(fd);
248 /* Restore console */
250 ioctl(0, SW_TEXT_80x25, NULL);
252 exit(0);