2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 #include <engine/falcon.h>
24 #include <core/device.h>
25 #include <subdev/timer.h>
28 nvkm_falcon_intr(struct nvkm_subdev
*subdev
)
30 struct nvkm_falcon
*falcon
= (void *)subdev
;
31 u32 dispatch
= nv_ro32(falcon
, 0x01c);
32 u32 intr
= nv_ro32(falcon
, 0x008) & dispatch
& ~(dispatch
>> 16);
34 if (intr
& 0x00000010) {
35 nv_debug(falcon
, "ucode halted\n");
36 nv_wo32(falcon
, 0x004, 0x00000010);
41 nv_error(falcon
, "unhandled intr 0x%08x\n", intr
);
42 nv_wo32(falcon
, 0x004, intr
);
47 _nvkm_falcon_rd32(struct nvkm_object
*object
, u64 addr
)
49 struct nvkm_falcon
*falcon
= (void *)object
;
50 return nv_rd32(falcon
, falcon
->addr
+ addr
);
54 _nvkm_falcon_wr32(struct nvkm_object
*object
, u64 addr
, u32 data
)
56 struct nvkm_falcon
*falcon
= (void *)object
;
57 nv_wr32(falcon
, falcon
->addr
+ addr
, data
);
61 vmemdup(const void *src
, size_t len
)
63 void *p
= vmalloc(len
);
71 _nvkm_falcon_init(struct nvkm_object
*object
)
73 struct nvkm_device
*device
= nv_device(object
);
74 struct nvkm_falcon
*falcon
= (void *)object
;
75 const struct firmware
*fw
;
76 char name
[32] = "internal";
80 /* enable engine, and determine its capabilities */
81 ret
= nvkm_engine_init(&falcon
->base
);
85 if (device
->chipset
< 0xa3 ||
86 device
->chipset
== 0xaa || device
->chipset
== 0xac) {
88 falcon
->secret
= (falcon
->addr
== 0x087000) ? 1 : 0;
90 caps
= nv_ro32(falcon
, 0x12c);
91 falcon
->version
= (caps
& 0x0000000f);
92 falcon
->secret
= (caps
& 0x00000030) >> 4;
95 caps
= nv_ro32(falcon
, 0x108);
96 falcon
->code
.limit
= (caps
& 0x000001ff) << 8;
97 falcon
->data
.limit
= (caps
& 0x0003fe00) >> 1;
99 nv_debug(falcon
, "falcon version: %d\n", falcon
->version
);
100 nv_debug(falcon
, "secret level: %d\n", falcon
->secret
);
101 nv_debug(falcon
, "code limit: %d\n", falcon
->code
.limit
);
102 nv_debug(falcon
, "data limit: %d\n", falcon
->data
.limit
);
104 /* wait for 'uc halted' to be signalled before continuing */
105 if (falcon
->secret
&& falcon
->version
< 4) {
106 if (!falcon
->version
)
107 nv_wait(falcon
, 0x008, 0x00000010, 0x00000010);
109 nv_wait(falcon
, 0x180, 0x80000000, 0);
110 nv_wo32(falcon
, 0x004, 0x00000010);
113 /* disable all interrupts */
114 nv_wo32(falcon
, 0x014, 0xffffffff);
116 /* no default ucode provided by the engine implementation, try and
117 * locate a "self-bootstrapping" firmware image for the engine
119 if (!falcon
->code
.data
) {
120 snprintf(name
, sizeof(name
), "nouveau/nv%02x_fuc%03x",
121 device
->chipset
, falcon
->addr
>> 12);
123 ret
= request_firmware(&fw
, name
, nv_device_base(device
));
125 falcon
->code
.data
= vmemdup(fw
->data
, fw
->size
);
126 falcon
->code
.size
= fw
->size
;
127 falcon
->data
.data
= NULL
;
128 falcon
->data
.size
= 0;
129 release_firmware(fw
);
132 falcon
->external
= true;
135 /* next step is to try and load "static code/data segment" firmware
136 * images for the engine
138 if (!falcon
->code
.data
) {
139 snprintf(name
, sizeof(name
), "nouveau/nv%02x_fuc%03xd",
140 device
->chipset
, falcon
->addr
>> 12);
142 ret
= request_firmware(&fw
, name
, nv_device_base(device
));
144 nv_error(falcon
, "unable to load firmware data\n");
148 falcon
->data
.data
= vmemdup(fw
->data
, fw
->size
);
149 falcon
->data
.size
= fw
->size
;
150 release_firmware(fw
);
151 if (!falcon
->data
.data
)
154 snprintf(name
, sizeof(name
), "nouveau/nv%02x_fuc%03xc",
155 device
->chipset
, falcon
->addr
>> 12);
157 ret
= request_firmware(&fw
, name
, nv_device_base(device
));
159 nv_error(falcon
, "unable to load firmware code\n");
163 falcon
->code
.data
= vmemdup(fw
->data
, fw
->size
);
164 falcon
->code
.size
= fw
->size
;
165 release_firmware(fw
);
166 if (!falcon
->code
.data
)
170 nv_debug(falcon
, "firmware: %s (%s)\n", name
, falcon
->data
.data
?
171 "static code/data segments" : "self-bootstrapping");
173 /* ensure any "self-bootstrapping" firmware image is in vram */
174 if (!falcon
->data
.data
&& !falcon
->core
) {
175 ret
= nvkm_gpuobj_new(object
->parent
, NULL
, falcon
->code
.size
,
176 256, 0, &falcon
->core
);
178 nv_error(falcon
, "core allocation failed, %d\n", ret
);
182 for (i
= 0; i
< falcon
->code
.size
; i
+= 4)
183 nv_wo32(falcon
->core
, i
, falcon
->code
.data
[i
/ 4]);
186 /* upload firmware bootloader (or the full code segments) */
188 if (device
->card_type
< NV_C0
)
189 nv_wo32(falcon
, 0x618, 0x04000000);
191 nv_wo32(falcon
, 0x618, 0x00000114);
192 nv_wo32(falcon
, 0x11c, 0);
193 nv_wo32(falcon
, 0x110, falcon
->core
->addr
>> 8);
194 nv_wo32(falcon
, 0x114, 0);
195 nv_wo32(falcon
, 0x118, 0x00006610);
197 if (falcon
->code
.size
> falcon
->code
.limit
||
198 falcon
->data
.size
> falcon
->data
.limit
) {
199 nv_error(falcon
, "ucode exceeds falcon limit(s)\n");
203 if (falcon
->version
< 3) {
204 nv_wo32(falcon
, 0xff8, 0x00100000);
205 for (i
= 0; i
< falcon
->code
.size
/ 4; i
++)
206 nv_wo32(falcon
, 0xff4, falcon
->code
.data
[i
]);
208 nv_wo32(falcon
, 0x180, 0x01000000);
209 for (i
= 0; i
< falcon
->code
.size
/ 4; i
++) {
211 nv_wo32(falcon
, 0x188, i
>> 6);
212 nv_wo32(falcon
, 0x184, falcon
->code
.data
[i
]);
217 /* upload data segment (if necessary), zeroing the remainder */
218 if (falcon
->version
< 3) {
219 nv_wo32(falcon
, 0xff8, 0x00000000);
220 for (i
= 0; !falcon
->core
&& i
< falcon
->data
.size
/ 4; i
++)
221 nv_wo32(falcon
, 0xff4, falcon
->data
.data
[i
]);
222 for (; i
< falcon
->data
.limit
; i
+= 4)
223 nv_wo32(falcon
, 0xff4, 0x00000000);
225 nv_wo32(falcon
, 0x1c0, 0x01000000);
226 for (i
= 0; !falcon
->core
&& i
< falcon
->data
.size
/ 4; i
++)
227 nv_wo32(falcon
, 0x1c4, falcon
->data
.data
[i
]);
228 for (; i
< falcon
->data
.limit
/ 4; i
++)
229 nv_wo32(falcon
, 0x1c4, 0x00000000);
232 /* start it running */
233 nv_wo32(falcon
, 0x10c, 0x00000001); /* BLOCK_ON_FIFO */
234 nv_wo32(falcon
, 0x104, 0x00000000); /* ENTRY */
235 nv_wo32(falcon
, 0x100, 0x00000002); /* TRIGGER */
236 nv_wo32(falcon
, 0x048, 0x00000003); /* FIFO | CHSW */
241 _nvkm_falcon_fini(struct nvkm_object
*object
, bool suspend
)
243 struct nvkm_falcon
*falcon
= (void *)object
;
246 nvkm_gpuobj_ref(NULL
, &falcon
->core
);
247 if (falcon
->external
) {
248 vfree(falcon
->data
.data
);
249 vfree(falcon
->code
.data
);
250 falcon
->code
.data
= NULL
;
254 nv_mo32(falcon
, 0x048, 0x00000003, 0x00000000);
255 nv_wo32(falcon
, 0x014, 0xffffffff);
257 return nvkm_engine_fini(&falcon
->base
, suspend
);
261 nvkm_falcon_create_(struct nvkm_object
*parent
, struct nvkm_object
*engine
,
262 struct nvkm_oclass
*oclass
, u32 addr
, bool enable
,
263 const char *iname
, const char *fname
,
264 int length
, void **pobject
)
266 struct nvkm_falcon
*falcon
;
269 ret
= nvkm_engine_create_(parent
, engine
, oclass
, enable
, iname
,
270 fname
, length
, pobject
);