OSDN Git Service

27bc50e80a56b49c40f43ede1e9ace9b0d5c44fa
[uclinux-h8/linux.git] / drivers / gpu / drm / nouveau / nvkm / engine / falcon.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  */
22 #include <engine/falcon.h>
23
24 #include <subdev/timer.h>
25
26 void
27 nvkm_falcon_intr(struct nvkm_subdev *subdev)
28 {
29         struct nvkm_falcon *falcon = (void *)subdev;
30         struct nvkm_device *device = falcon->engine.subdev.device;
31         const u32 base = falcon->addr;
32         u32 dispatch = nvkm_rd32(device, base + 0x01c);
33         u32 intr = nvkm_rd32(device, base + 0x008) & dispatch & ~(dispatch >> 16);
34
35         if (intr & 0x00000010) {
36                 nvkm_debug(subdev, "ucode halted\n");
37                 nvkm_wr32(device, base + 0x004, 0x00000010);
38                 intr &= ~0x00000010;
39         }
40
41         if (intr)  {
42                 nvkm_error(subdev, "intr %08x\n", intr);
43                 nvkm_wr32(device, base + 0x004, intr);
44         }
45 }
46
47 static void *
48 vmemdup(const void *src, size_t len)
49 {
50         void *p = vmalloc(len);
51
52         if (p)
53                 memcpy(p, src, len);
54         return p;
55 }
56
57 int
58 _nvkm_falcon_init(struct nvkm_object *object)
59 {
60         struct nvkm_falcon *falcon = (void *)object;
61         struct nvkm_subdev *subdev = &falcon->engine.subdev;
62         struct nvkm_device *device = subdev->device;
63         const struct firmware *fw;
64         char name[32] = "internal";
65         const u32 base = falcon->addr;
66         int ret, i;
67         u32 caps;
68
69         /* enable engine, and determine its capabilities */
70         ret = nvkm_engine_init_old(&falcon->engine);
71         if (ret)
72                 return ret;
73
74         if (device->chipset <  0xa3 ||
75             device->chipset == 0xaa || device->chipset == 0xac) {
76                 falcon->version = 0;
77                 falcon->secret  = (falcon->addr == 0x087000) ? 1 : 0;
78         } else {
79                 caps = nvkm_rd32(device, base + 0x12c);
80                 falcon->version = (caps & 0x0000000f);
81                 falcon->secret  = (caps & 0x00000030) >> 4;
82         }
83
84         caps = nvkm_rd32(device, base + 0x108);
85         falcon->code.limit = (caps & 0x000001ff) << 8;
86         falcon->data.limit = (caps & 0x0003fe00) >> 1;
87
88         nvkm_debug(subdev, "falcon version: %d\n", falcon->version);
89         nvkm_debug(subdev, "secret level: %d\n", falcon->secret);
90         nvkm_debug(subdev, "code limit: %d\n", falcon->code.limit);
91         nvkm_debug(subdev, "data limit: %d\n", falcon->data.limit);
92
93         /* wait for 'uc halted' to be signalled before continuing */
94         if (falcon->secret && falcon->version < 4) {
95                 if (!falcon->version) {
96                         nvkm_msec(device, 2000,
97                                 if (nvkm_rd32(device, base + 0x008) & 0x00000010)
98                                         break;
99                         );
100                 } else {
101                         nvkm_msec(device, 2000,
102                                 if (!(nvkm_rd32(device, base + 0x180) & 0x80000000))
103                                         break;
104                         );
105                 }
106                 nvkm_wr32(device, base + 0x004, 0x00000010);
107         }
108
109         /* disable all interrupts */
110         nvkm_wr32(device, base + 0x014, 0xffffffff);
111
112         /* no default ucode provided by the engine implementation, try and
113          * locate a "self-bootstrapping" firmware image for the engine
114          */
115         if (!falcon->code.data) {
116                 snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03x",
117                          device->chipset, falcon->addr >> 12);
118
119                 ret = request_firmware(&fw, name, nv_device_base(device));
120                 if (ret == 0) {
121                         falcon->code.data = vmemdup(fw->data, fw->size);
122                         falcon->code.size = fw->size;
123                         falcon->data.data = NULL;
124                         falcon->data.size = 0;
125                         release_firmware(fw);
126                 }
127
128                 falcon->external = true;
129         }
130
131         /* next step is to try and load "static code/data segment" firmware
132          * images for the engine
133          */
134         if (!falcon->code.data) {
135                 snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xd",
136                          device->chipset, falcon->addr >> 12);
137
138                 ret = request_firmware(&fw, name, nv_device_base(device));
139                 if (ret) {
140                         nvkm_error(subdev, "unable to load firmware data\n");
141                         return -ENODEV;
142                 }
143
144                 falcon->data.data = vmemdup(fw->data, fw->size);
145                 falcon->data.size = fw->size;
146                 release_firmware(fw);
147                 if (!falcon->data.data)
148                         return -ENOMEM;
149
150                 snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xc",
151                          device->chipset, falcon->addr >> 12);
152
153                 ret = request_firmware(&fw, name, nv_device_base(device));
154                 if (ret) {
155                         nvkm_error(subdev, "unable to load firmware code\n");
156                         return -ENODEV;
157                 }
158
159                 falcon->code.data = vmemdup(fw->data, fw->size);
160                 falcon->code.size = fw->size;
161                 release_firmware(fw);
162                 if (!falcon->code.data)
163                         return -ENOMEM;
164         }
165
166         nvkm_debug(subdev, "firmware: %s (%s)\n", name, falcon->data.data ?
167                    "static code/data segments" : "self-bootstrapping");
168
169         /* ensure any "self-bootstrapping" firmware image is in vram */
170         if (!falcon->data.data && !falcon->core) {
171                 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
172                                       falcon->code.size, 256, false,
173                                       &falcon->core);
174                 if (ret) {
175                         nvkm_error(subdev, "core allocation failed, %d\n", ret);
176                         return ret;
177                 }
178
179                 nvkm_kmap(falcon->core);
180                 for (i = 0; i < falcon->code.size; i += 4)
181                         nvkm_wo32(falcon->core, i, falcon->code.data[i / 4]);
182                 nvkm_done(falcon->core);
183         }
184
185         /* upload firmware bootloader (or the full code segments) */
186         if (falcon->core) {
187                 u64 addr = nvkm_memory_addr(falcon->core);
188                 if (device->card_type < NV_C0)
189                         nvkm_wr32(device, base + 0x618, 0x04000000);
190                 else
191                         nvkm_wr32(device, base + 0x618, 0x00000114);
192                 nvkm_wr32(device, base + 0x11c, 0);
193                 nvkm_wr32(device, base + 0x110, addr >> 8);
194                 nvkm_wr32(device, base + 0x114, 0);
195                 nvkm_wr32(device, base + 0x118, 0x00006610);
196         } else {
197                 if (falcon->code.size > falcon->code.limit ||
198                     falcon->data.size > falcon->data.limit) {
199                         nvkm_error(subdev, "ucode exceeds falcon limit(s)\n");
200                         return -EINVAL;
201                 }
202
203                 if (falcon->version < 3) {
204                         nvkm_wr32(device, base + 0xff8, 0x00100000);
205                         for (i = 0; i < falcon->code.size / 4; i++)
206                                 nvkm_wr32(device, base + 0xff4, falcon->code.data[i]);
207                 } else {
208                         nvkm_wr32(device, base + 0x180, 0x01000000);
209                         for (i = 0; i < falcon->code.size / 4; i++) {
210                                 if ((i & 0x3f) == 0)
211                                         nvkm_wr32(device, base + 0x188, i >> 6);
212                                 nvkm_wr32(device, base + 0x184, falcon->code.data[i]);
213                         }
214                 }
215         }
216
217         /* upload data segment (if necessary), zeroing the remainder */
218         if (falcon->version < 3) {
219                 nvkm_wr32(device, base + 0xff8, 0x00000000);
220                 for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
221                         nvkm_wr32(device, base + 0xff4, falcon->data.data[i]);
222                 for (; i < falcon->data.limit; i += 4)
223                         nvkm_wr32(device, base + 0xff4, 0x00000000);
224         } else {
225                 nvkm_wr32(device, base + 0x1c0, 0x01000000);
226                 for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
227                         nvkm_wr32(device, base + 0x1c4, falcon->data.data[i]);
228                 for (; i < falcon->data.limit / 4; i++)
229                         nvkm_wr32(device, base + 0x1c4, 0x00000000);
230         }
231
232         /* start it running */
233         nvkm_wr32(device, base + 0x10c, 0x00000001); /* BLOCK_ON_FIFO */
234         nvkm_wr32(device, base + 0x104, 0x00000000); /* ENTRY */
235         nvkm_wr32(device, base + 0x100, 0x00000002); /* TRIGGER */
236         nvkm_wr32(device, base + 0x048, 0x00000003); /* FIFO | CHSW */
237         return 0;
238 }
239
240 int
241 _nvkm_falcon_fini(struct nvkm_object *object, bool suspend)
242 {
243         struct nvkm_falcon *falcon = (void *)object;
244         struct nvkm_device *device = falcon->engine.subdev.device;
245         const u32 base = falcon->addr;
246
247         if (!suspend) {
248                 nvkm_memory_del(&falcon->core);
249                 if (falcon->external) {
250                         vfree(falcon->data.data);
251                         vfree(falcon->code.data);
252                         falcon->code.data = NULL;
253                 }
254         }
255
256         nvkm_mask(device, base + 0x048, 0x00000003, 0x00000000);
257         nvkm_wr32(device, base + 0x014, 0xffffffff);
258
259         return nvkm_engine_fini_old(&falcon->engine, suspend);
260 }
261
262 int
263 nvkm_falcon_create_(struct nvkm_object *parent, struct nvkm_object *engine,
264                     struct nvkm_oclass *oclass, u32 addr, bool enable,
265                     const char *iname, const char *fname,
266                     int length, void **pobject)
267 {
268         struct nvkm_falcon *falcon;
269         int ret;
270
271         ret = nvkm_engine_create_(parent, engine, oclass, enable, iname,
272                                   fname, length, pobject);
273         falcon = *pobject;
274         if (ret)
275                 return ret;
276
277         falcon->addr = addr;
278         return 0;
279 }