PageRenderTime 36ms CodeModel.GetById 23ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/nouveau/nouveau_grctx.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 160 lines | 116 code | 21 blank | 23 comment | 20 complexity | 5e4edc7da8815354410ab9e5fa83761e MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1/*
  2 * Copyright 2009 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 * Authors: Ben Skeggs
 23 */
 24
 25#include <linux/firmware.h>
 26#include <linux/slab.h>
 27
 28#include "drmP.h"
 29#include "nouveau_drv.h"
 30
 31struct nouveau_ctxprog {
 32	uint32_t signature;
 33	uint8_t  version;
 34	uint16_t length;
 35	uint32_t data[];
 36} __attribute__ ((packed));
 37
 38struct nouveau_ctxvals {
 39	uint32_t signature;
 40	uint8_t  version;
 41	uint32_t length;
 42	struct {
 43		uint32_t offset;
 44		uint32_t value;
 45	} data[];
 46} __attribute__ ((packed));
 47
 48int
 49nouveau_grctx_prog_load(struct drm_device *dev)
 50{
 51	struct drm_nouveau_private *dev_priv = dev->dev_private;
 52	struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
 53	const int chipset = dev_priv->chipset;
 54	const struct firmware *fw;
 55	const struct nouveau_ctxprog *cp;
 56	const struct nouveau_ctxvals *cv;
 57	char name[32];
 58	int ret, i;
 59
 60	if (pgraph->accel_blocked)
 61		return -ENODEV;
 62
 63	if (!pgraph->ctxprog) {
 64		sprintf(name, "nouveau/nv%02x.ctxprog", chipset);
 65		ret = request_firmware(&fw, name, &dev->pdev->dev);
 66		if (ret) {
 67			NV_ERROR(dev, "No ctxprog for NV%02x\n", chipset);
 68			return ret;
 69		}
 70
 71		pgraph->ctxprog = kmemdup(fw->data, fw->size, GFP_KERNEL);
 72		if (!pgraph->ctxprog) {
 73			NV_ERROR(dev, "OOM copying ctxprog\n");
 74			release_firmware(fw);
 75			return -ENOMEM;
 76		}
 77
 78		cp = pgraph->ctxprog;
 79		if (le32_to_cpu(cp->signature) != 0x5043564e ||
 80		    cp->version != 0 ||
 81		    le16_to_cpu(cp->length) != ((fw->size - 7) / 4)) {
 82			NV_ERROR(dev, "ctxprog invalid\n");
 83			release_firmware(fw);
 84			nouveau_grctx_fini(dev);
 85			return -EINVAL;
 86		}
 87		release_firmware(fw);
 88	}
 89
 90	if (!pgraph->ctxvals) {
 91		sprintf(name, "nouveau/nv%02x.ctxvals", chipset);
 92		ret = request_firmware(&fw, name, &dev->pdev->dev);
 93		if (ret) {
 94			NV_ERROR(dev, "No ctxvals for NV%02x\n", chipset);
 95			nouveau_grctx_fini(dev);
 96			return ret;
 97		}
 98
 99		pgraph->ctxvals = kmemdup(fw->data, fw->size, GFP_KERNEL);
100		if (!pgraph->ctxvals) {
101			NV_ERROR(dev, "OOM copying ctxvals\n");
102			release_firmware(fw);
103			nouveau_grctx_fini(dev);
104			return -ENOMEM;
105		}
106
107		cv = (void *)pgraph->ctxvals;
108		if (le32_to_cpu(cv->signature) != 0x5643564e ||
109		    cv->version != 0 ||
110		    le32_to_cpu(cv->length) != ((fw->size - 9) / 8)) {
111			NV_ERROR(dev, "ctxvals invalid\n");
112			release_firmware(fw);
113			nouveau_grctx_fini(dev);
114			return -EINVAL;
115		}
116		release_firmware(fw);
117	}
118
119	cp = pgraph->ctxprog;
120
121	nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_INDEX, 0);
122	for (i = 0; i < le16_to_cpu(cp->length); i++)
123		nv_wr32(dev, NV40_PGRAPH_CTXCTL_UCODE_DATA,
124			le32_to_cpu(cp->data[i]));
125
126	return 0;
127}
128
129void
130nouveau_grctx_fini(struct drm_device *dev)
131{
132	struct drm_nouveau_private *dev_priv = dev->dev_private;
133	struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
134
135	if (pgraph->ctxprog) {
136		kfree(pgraph->ctxprog);
137		pgraph->ctxprog = NULL;
138	}
139
140	if (pgraph->ctxvals) {
141		kfree(pgraph->ctxprog);
142		pgraph->ctxvals = NULL;
143	}
144}
145
146void
147nouveau_grctx_vals_load(struct drm_device *dev, struct nouveau_gpuobj *ctx)
148{
149	struct drm_nouveau_private *dev_priv = dev->dev_private;
150	struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
151	struct nouveau_ctxvals *cv = pgraph->ctxvals;
152	int i;
153
154	if (!cv)
155		return;
156
157	for (i = 0; i < le32_to_cpu(cv->length); i++)
158		nv_wo32(dev, ctx, le32_to_cpu(cv->data[i].offset),
159			le32_to_cpu(cv->data[i].value));
160}