falcon.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
22
#include <engine/falcon.h>
23

24
#include <core/gpuobj.h>
25
#include <subdev/timer.h>
26
#include <engine/fifo.h>
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static int
nvkm_falcon_oclass_get(struct nvkm_oclass *oclass, int index)
{
	struct nvkm_falcon *falcon = nvkm_falcon(oclass->engine);
	int c = 0;

	while (falcon->func->sclass[c].oclass) {
		if (c++ == index) {
			oclass->base = falcon->func->sclass[index];
			return index;
		}
	}

	return c;
}

static int
nvkm_falcon_cclass_bind(struct nvkm_object *object, struct nvkm_gpuobj *parent,
			int align, struct nvkm_gpuobj **pgpuobj)
{
	return nvkm_gpuobj_new(object->engine->subdev.device, 256,
			       align, true, parent, pgpuobj);
}

static const struct nvkm_object_func
nvkm_falcon_cclass = {
	.bind = nvkm_falcon_cclass_bind,
};

57
static void
58
nvkm_falcon_intr(struct nvkm_engine *engine)
59
{
60 61 62
	struct nvkm_falcon *falcon = nvkm_falcon(engine);
	struct nvkm_subdev *subdev = &falcon->engine.subdev;
	struct nvkm_device *device = subdev->device;
63
	const u32 base = falcon->addr;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	u32 dest = nvkm_rd32(device, base + 0x01c);
	u32 intr = nvkm_rd32(device, base + 0x008) & dest & ~(dest >> 16);
	u32 inst = nvkm_rd32(device, base + 0x050) & 0x3fffffff;
	struct nvkm_fifo_chan *chan;
	unsigned long flags;

	chan = nvkm_fifo_chan_inst(device->fifo, (u64)inst << 12, &flags);

	if (intr & 0x00000040) {
		if (falcon->func->intr) {
			falcon->func->intr(falcon, chan);
			nvkm_wr32(device, base + 0x004, 0x00000040);
			intr &= ~0x00000040;
		}
	}
79 80

	if (intr & 0x00000010) {
81
		nvkm_debug(subdev, "ucode halted\n");
82
		nvkm_wr32(device, base + 0x004, 0x00000010);
83 84 85 86
		intr &= ~0x00000010;
	}

	if (intr)  {
87
		nvkm_error(subdev, "intr %08x\n", intr);
88
		nvkm_wr32(device, base + 0x004, intr);
89
	}
90 91

	nvkm_fifo_chan_put(device->fifo, flags, &chan);
92 93
}

94 95 96 97 98 99 100 101
static int
nvkm_falcon_fini(struct nvkm_engine *engine, bool suspend)
{
	struct nvkm_falcon *falcon = nvkm_falcon(engine);
	struct nvkm_device *device = falcon->engine.subdev.device;
	const u32 base = falcon->addr;

	if (!suspend) {
102
		nvkm_memory_unref(&falcon->core);
103 104 105 106 107 108 109 110 111 112 113 114
		if (falcon->external) {
			vfree(falcon->data.data);
			vfree(falcon->code.data);
			falcon->code.data = NULL;
		}
	}

	nvkm_mask(device, base + 0x048, 0x00000003, 0x00000000);
	nvkm_wr32(device, base + 0x014, 0xffffffff);
	return 0;
}

115 116 117 118 119 120 121 122 123 124
static void *
vmemdup(const void *src, size_t len)
{
	void *p = vmalloc(len);

	if (p)
		memcpy(p, src, len);
	return p;
}

125 126
static int
nvkm_falcon_oneinit(struct nvkm_engine *engine)
127
{
128
	struct nvkm_falcon *falcon = nvkm_falcon(engine);
129 130
	struct nvkm_subdev *subdev = &falcon->engine.subdev;
	struct nvkm_device *device = subdev->device;
131
	const u32 base = falcon->addr;
132 133
	u32 caps;

134
	/* determine falcon capabilities */
135 136 137 138 139
	if (device->chipset <  0xa3 ||
	    device->chipset == 0xaa || device->chipset == 0xac) {
		falcon->version = 0;
		falcon->secret  = (falcon->addr == 0x087000) ? 1 : 0;
	} else {
140
		caps = nvkm_rd32(device, base + 0x12c);
141 142 143 144
		falcon->version = (caps & 0x0000000f);
		falcon->secret  = (caps & 0x00000030) >> 4;
	}

145
	caps = nvkm_rd32(device, base + 0x108);
146 147 148
	falcon->code.limit = (caps & 0x000001ff) << 8;
	falcon->data.limit = (caps & 0x0003fe00) >> 1;

149 150 151 152
	nvkm_debug(subdev, "falcon version: %d\n", falcon->version);
	nvkm_debug(subdev, "secret level: %d\n", falcon->secret);
	nvkm_debug(subdev, "code limit: %d\n", falcon->code.limit);
	nvkm_debug(subdev, "data limit: %d\n", falcon->data.limit);
153 154 155 156 157 158 159 160 161 162 163 164 165
	return 0;
}

static int
nvkm_falcon_init(struct nvkm_engine *engine)
{
	struct nvkm_falcon *falcon = nvkm_falcon(engine);
	struct nvkm_subdev *subdev = &falcon->engine.subdev;
	struct nvkm_device *device = subdev->device;
	const struct firmware *fw;
	char name[32] = "internal";
	const u32 base = falcon->addr;
	int ret, i;
166 167

	/* wait for 'uc halted' to be signalled before continuing */
168
	if (falcon->secret && falcon->version < 4) {
169 170
		if (!falcon->version) {
			nvkm_msec(device, 2000,
171
				if (nvkm_rd32(device, base + 0x008) & 0x00000010)
172 173 174 175
					break;
			);
		} else {
			nvkm_msec(device, 2000,
176
				if (!(nvkm_rd32(device, base + 0x180) & 0x80000000))
177 178 179
					break;
			);
		}
180
		nvkm_wr32(device, base + 0x004, 0x00000010);
181 182 183
	}

	/* disable all interrupts */
184
	nvkm_wr32(device, base + 0x014, 0xffffffff);
185 186 187 188 189 190 191 192

	/* no default ucode provided by the engine implementation, try and
	 * locate a "self-bootstrapping" firmware image for the engine
	 */
	if (!falcon->code.data) {
		snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03x",
			 device->chipset, falcon->addr >> 12);

193
		ret = request_firmware(&fw, name, device->dev);
194
		if (ret == 0) {
195
			falcon->code.data = vmemdup(fw->data, fw->size);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
			falcon->code.size = fw->size;
			falcon->data.data = NULL;
			falcon->data.size = 0;
			release_firmware(fw);
		}

		falcon->external = true;
	}

	/* next step is to try and load "static code/data segment" firmware
	 * images for the engine
	 */
	if (!falcon->code.data) {
		snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xd",
			 device->chipset, falcon->addr >> 12);

212
		ret = request_firmware(&fw, name, device->dev);
213
		if (ret) {
214
			nvkm_error(subdev, "unable to load firmware data\n");
215
			return -ENODEV;
216 217
		}

218
		falcon->data.data = vmemdup(fw->data, fw->size);
219 220 221 222 223 224 225 226
		falcon->data.size = fw->size;
		release_firmware(fw);
		if (!falcon->data.data)
			return -ENOMEM;

		snprintf(name, sizeof(name), "nouveau/nv%02x_fuc%03xc",
			 device->chipset, falcon->addr >> 12);

227
		ret = request_firmware(&fw, name, device->dev);
228
		if (ret) {
229
			nvkm_error(subdev, "unable to load firmware code\n");
230
			return -ENODEV;
231 232
		}

233
		falcon->code.data = vmemdup(fw->data, fw->size);
234 235 236 237 238 239
		falcon->code.size = fw->size;
		release_firmware(fw);
		if (!falcon->code.data)
			return -ENOMEM;
	}

240 241
	nvkm_debug(subdev, "firmware: %s (%s)\n", name, falcon->data.data ?
		   "static code/data segments" : "self-bootstrapping");
242 243 244

	/* ensure any "self-bootstrapping" firmware image is in vram */
	if (!falcon->data.data && !falcon->core) {
245 246 247
		ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
				      falcon->code.size, 256, false,
				      &falcon->core);
248
		if (ret) {
249
			nvkm_error(subdev, "core allocation failed, %d\n", ret);
250 251 252
			return ret;
		}

253
		nvkm_kmap(falcon->core);
254
		for (i = 0; i < falcon->code.size; i += 4)
255 256
			nvkm_wo32(falcon->core, i, falcon->code.data[i / 4]);
		nvkm_done(falcon->core);
257 258 259 260
	}

	/* upload firmware bootloader (or the full code segments) */
	if (falcon->core) {
261
		u64 addr = nvkm_memory_addr(falcon->core);
262
		if (device->card_type < NV_C0)
263
			nvkm_wr32(device, base + 0x618, 0x04000000);
264
		else
265 266
			nvkm_wr32(device, base + 0x618, 0x00000114);
		nvkm_wr32(device, base + 0x11c, 0);
267
		nvkm_wr32(device, base + 0x110, addr >> 8);
268 269
		nvkm_wr32(device, base + 0x114, 0);
		nvkm_wr32(device, base + 0x118, 0x00006610);
270 271 272
	} else {
		if (falcon->code.size > falcon->code.limit ||
		    falcon->data.size > falcon->data.limit) {
273
			nvkm_error(subdev, "ucode exceeds falcon limit(s)\n");
274 275 276 277
			return -EINVAL;
		}

		if (falcon->version < 3) {
278
			nvkm_wr32(device, base + 0xff8, 0x00100000);
279
			for (i = 0; i < falcon->code.size / 4; i++)
280
				nvkm_wr32(device, base + 0xff4, falcon->code.data[i]);
281
		} else {
282
			nvkm_wr32(device, base + 0x180, 0x01000000);
283 284
			for (i = 0; i < falcon->code.size / 4; i++) {
				if ((i & 0x3f) == 0)
285 286
					nvkm_wr32(device, base + 0x188, i >> 6);
				nvkm_wr32(device, base + 0x184, falcon->code.data[i]);
287 288 289 290 291 292
			}
		}
	}

	/* upload data segment (if necessary), zeroing the remainder */
	if (falcon->version < 3) {
293
		nvkm_wr32(device, base + 0xff8, 0x00000000);
294
		for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
295
			nvkm_wr32(device, base + 0xff4, falcon->data.data[i]);
296
		for (; i < falcon->data.limit; i += 4)
297
			nvkm_wr32(device, base + 0xff4, 0x00000000);
298
	} else {
299
		nvkm_wr32(device, base + 0x1c0, 0x01000000);
300
		for (i = 0; !falcon->core && i < falcon->data.size / 4; i++)
301
			nvkm_wr32(device, base + 0x1c4, falcon->data.data[i]);
302
		for (; i < falcon->data.limit / 4; i++)
303
			nvkm_wr32(device, base + 0x1c4, 0x00000000);
304 305 306
	}

	/* start it running */
307 308 309 310
	nvkm_wr32(device, base + 0x10c, 0x00000001); /* BLOCK_ON_FIFO */
	nvkm_wr32(device, base + 0x104, 0x00000000); /* ENTRY */
	nvkm_wr32(device, base + 0x100, 0x00000002); /* TRIGGER */
	nvkm_wr32(device, base + 0x048, 0x00000003); /* FIFO | CHSW */
311 312 313

	if (falcon->func->init)
		falcon->func->init(falcon);
314 315 316
	return 0;
}

317 318
static void *
nvkm_falcon_dtor(struct nvkm_engine *engine)
319
{
320
	return nvkm_falcon(engine);
321 322
}

323 324
static const struct nvkm_engine_func
nvkm_falcon = {
325 326 327 328 329
	.dtor = nvkm_falcon_dtor,
	.oneinit = nvkm_falcon_oneinit,
	.init = nvkm_falcon_init,
	.fini = nvkm_falcon_fini,
	.intr = nvkm_falcon_intr,
330 331 332 333
	.fifo.sclass = nvkm_falcon_oclass_get,
	.cclass = &nvkm_falcon_cclass,
};

334
int
335 336 337
nvkm_falcon_new_(const struct nvkm_falcon_func *func,
		 struct nvkm_device *device, int index, bool enable,
		 u32 addr, struct nvkm_engine **pengine)
338
{
339
	struct nvkm_falcon *falcon;
340

341 342
	if (!(falcon = kzalloc(sizeof(*falcon), GFP_KERNEL)))
		return -ENOMEM;
343
	falcon->func = func;
344
	falcon->addr = addr;
345 346 347 348 349 350
	falcon->code.data = func->code.data;
	falcon->code.size = func->code.size;
	falcon->data.data = func->data.data;
	falcon->data.size = func->data.size;
	*pengine = &falcon->engine;

351
	return nvkm_engine_ctor(&nvkm_falcon, device, index,
352
				enable, &falcon->engine);
353
}