gr2d.c 7.6 KB
Newer Older
T
Terje Bergstrom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2012-2013, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/clk.h>

#include "channel.h"
#include "drm.h"
#include "gem.h"
#include "job.h"
#include "host1x_bo.h"
#include "host1x_client.h"
#include "syncpt.h"

27 28
#define GR2D_NUM_REGS 0x4d

T
Terje Bergstrom 已提交
29
struct gr2d {
30
	struct tegra_drm_client client;
T
Terje Bergstrom 已提交
31
	struct host1x_channel *channel;
32 33 34
	struct clk *clk;

	DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
T
Terje Bergstrom 已提交
35 36
};

37
static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
T
Terje Bergstrom 已提交
38 39 40 41
{
	return container_of(client, struct gr2d, client);
}

42
static int gr2d_client_init(struct host1x_client *client)
T
Terje Bergstrom 已提交
43 44 45 46 47 48 49 50 51
{
	return 0;
}

static int gr2d_client_exit(struct host1x_client *client)
{
	return 0;
}

52 53 54 55 56 57
static const struct host1x_client_ops gr2d_client_ops = {
	.init = gr2d_client_init,
	.exit = gr2d_client_exit,
};

static int gr2d_open_channel(struct tegra_drm_client *client,
58
			     struct tegra_drm_context *context)
T
Terje Bergstrom 已提交
59 60 61 62 63 64 65 66 67 68
{
	struct gr2d *gr2d = to_gr2d(client);

	context->channel = host1x_channel_get(gr2d->channel);
	if (!context->channel)
		return -ENOMEM;

	return 0;
}

69
static void gr2d_close_channel(struct tegra_drm_context *context)
T
Terje Bergstrom 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
{
	host1x_channel_put(context->channel);
}

static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm,
					  struct drm_file *file,
					  u32 handle)
{
	struct drm_gem_object *gem;
	struct tegra_bo *bo;

	gem = drm_gem_object_lookup(drm, file, handle);
	if (!gem)
T
Thierry Reding 已提交
83
		return NULL;
T
Terje Bergstrom 已提交
84 85 86 87 88 89 90 91 92

	mutex_lock(&drm->struct_mutex);
	drm_gem_object_unreference(gem);
	mutex_unlock(&drm->struct_mutex);

	bo = to_tegra_bo(gem);
	return &bo->base;
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
{
	struct gr2d *gr2d = dev_get_drvdata(dev);

	switch (class) {
	case HOST1X_CLASS_HOST1X:
		if (offset == 0x2b)
			return 1;

		break;

	case HOST1X_CLASS_GR2D:
	case HOST1X_CLASS_GR2D_SB:
		if (offset >= GR2D_NUM_REGS)
			break;

		if (test_bit(offset, gr2d->addr_regs))
			return 1;

		break;
	}

	return 0;
}

118
static int gr2d_submit(struct tegra_drm_context *context,
T
Terje Bergstrom 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131
		       struct drm_tegra_submit *args, struct drm_device *drm,
		       struct drm_file *file)
{
	unsigned int num_cmdbufs = args->num_cmdbufs;
	unsigned int num_relocs = args->num_relocs;
	unsigned int num_waitchks = args->num_waitchks;
	struct drm_tegra_cmdbuf __user *cmdbufs =
		(void * __user)(uintptr_t)args->cmdbufs;
	struct drm_tegra_reloc __user *relocs =
		(void * __user)(uintptr_t)args->relocs;
	struct drm_tegra_waitchk __user *waitchks =
		(void * __user)(uintptr_t)args->waitchks;
	struct drm_tegra_syncpt syncpt;
132
	struct host1x_job *job;
T
Terje Bergstrom 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
	int err;

	/* We don't yet support other than one syncpt_incr struct per submit */
	if (args->num_syncpts != 1)
		return -EINVAL;

	job = host1x_job_alloc(context->channel, args->num_cmdbufs,
			       args->num_relocs, args->num_waitchks);
	if (!job)
		return -ENOMEM;

	job->num_relocs = args->num_relocs;
	job->num_waitchk = args->num_waitchks;
	job->client = (u32)args->context;
147
	job->class = context->client->base.class;
T
Terje Bergstrom 已提交
148 149 150 151 152 153 154 155 156 157 158
	job->serialize = true;

	while (num_cmdbufs) {
		struct drm_tegra_cmdbuf cmdbuf;
		struct host1x_bo *bo;

		err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
		if (err)
			goto fail;

		bo = host1x_bo_lookup(drm, file, cmdbuf.handle);
159 160
		if (!bo) {
			err = -ENOENT;
T
Terje Bergstrom 已提交
161
			goto fail;
162
		}
T
Terje Bergstrom 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

		host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset);
		num_cmdbufs--;
		cmdbufs++;
	}

	err = copy_from_user(job->relocarray, relocs,
			     sizeof(*relocs) * num_relocs);
	if (err)
		goto fail;

	while (num_relocs--) {
		struct host1x_reloc *reloc = &job->relocarray[num_relocs];
		struct host1x_bo *cmdbuf, *target;

		cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf);
		target = host1x_bo_lookup(drm, file, (u32)reloc->target);

		reloc->cmdbuf = cmdbuf;
		reloc->target = target;

184 185
		if (!reloc->target || !reloc->cmdbuf) {
			err = -ENOENT;
T
Terje Bergstrom 已提交
186
			goto fail;
187
		}
T
Terje Bergstrom 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	}

	err = copy_from_user(job->waitchk, waitchks,
			     sizeof(*waitchks) * num_waitchks);
	if (err)
		goto fail;

	err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts,
			     sizeof(syncpt));
	if (err)
		goto fail;

	job->syncpt_id = syncpt.id;
	job->syncpt_incrs = syncpt.incrs;
	job->timeout = 10000;
	job->is_addr_reg = gr2d_is_addr_reg;

	if (args->timeout && args->timeout < 10000)
		job->timeout = args->timeout;

208
	err = host1x_job_pin(job, context->client->base.dev);
T
Terje Bergstrom 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	if (err)
		goto fail;

	err = host1x_job_submit(job);
	if (err)
		goto fail_submit;

	args->fence = job->syncpt_end;

	host1x_job_put(job);
	return 0;

fail_submit:
	host1x_job_unpin(job);
fail:
	host1x_job_put(job);
	return err;
}

228
static const struct tegra_drm_client_ops gr2d_ops = {
T
Terje Bergstrom 已提交
229 230 231 232 233 234 235 236 237 238 239
	.open_channel = gr2d_open_channel,
	.close_channel = gr2d_close_channel,
	.submit = gr2d_submit,
};

static const struct of_device_id gr2d_match[] = {
	{ .compatible = "nvidia,tegra30-gr2d" },
	{ .compatible = "nvidia,tegra20-gr2d" },
	{ },
};

240 241 242 243 244
static const u32 gr2d_addr_regs[] = {
	0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31, 0x32,
	0x48, 0x49, 0x4a, 0x4b, 0x4c
};

T
Terje Bergstrom 已提交
245 246
static int gr2d_probe(struct platform_device *pdev)
{
247
	struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
T
Terje Bergstrom 已提交
248 249
	struct device *dev = &pdev->dev;
	struct host1x_syncpt **syncpts;
250 251 252
	struct gr2d *gr2d;
	unsigned int i;
	int err;
T
Terje Bergstrom 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
	if (!gr2d)
		return -ENOMEM;

	syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
	if (!syncpts)
		return -ENOMEM;

	gr2d->clk = devm_clk_get(dev, NULL);
	if (IS_ERR(gr2d->clk)) {
		dev_err(dev, "cannot get clock\n");
		return PTR_ERR(gr2d->clk);
	}

	err = clk_prepare_enable(gr2d->clk);
	if (err) {
		dev_err(dev, "cannot turn on clock\n");
		return err;
	}

	gr2d->channel = host1x_channel_request(dev);
	if (!gr2d->channel)
		return -ENOMEM;

278
	*syncpts = host1x_syncpt_request(dev, false);
T
Terje Bergstrom 已提交
279 280 281 282 283
	if (!(*syncpts)) {
		host1x_channel_free(gr2d->channel);
		return -ENOMEM;
	}

284 285 286 287 288 289 290
	INIT_LIST_HEAD(&gr2d->client.base.list);
	gr2d->client.base.ops = &gr2d_client_ops;
	gr2d->client.base.dev = dev;
	gr2d->client.base.class = HOST1X_CLASS_GR2D;
	gr2d->client.base.syncpts = syncpts;
	gr2d->client.base.num_syncpts = 1;
	gr2d->client.ops = &gr2d_ops;
T
Terje Bergstrom 已提交
291

292
	err = host1x_register_client(tegra, &gr2d->client.base);
T
Terje Bergstrom 已提交
293 294 295 296 297
	if (err < 0) {
		dev_err(dev, "failed to register host1x client: %d\n", err);
		return err;
	}

298 299 300
	/* initialize address register map */
	for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
		set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
T
Terje Bergstrom 已提交
301 302 303 304 305 306

	platform_set_drvdata(pdev, gr2d);

	return 0;
}

307
static int gr2d_remove(struct platform_device *pdev)
T
Terje Bergstrom 已提交
308
{
309
	struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent);
T
Terje Bergstrom 已提交
310 311 312 313
	struct gr2d *gr2d = platform_get_drvdata(pdev);
	unsigned int i;
	int err;

314
	err = host1x_unregister_client(tegra, &gr2d->client.base);
T
Terje Bergstrom 已提交
315
	if (err < 0) {
316 317
		dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
			err);
T
Terje Bergstrom 已提交
318 319 320
		return err;
	}

321 322
	for (i = 0; i < gr2d->client.base.num_syncpts; i++)
		host1x_syncpt_free(gr2d->client.base.syncpts[i]);
T
Terje Bergstrom 已提交
323 324 325 326 327 328 329 330 331

	host1x_channel_free(gr2d->channel);
	clk_disable_unprepare(gr2d->clk);

	return 0;
}

struct platform_driver tegra_gr2d_driver = {
	.driver = {
332
		.name = "tegra-gr2d",
T
Terje Bergstrom 已提交
333
		.of_match_table = gr2d_match,
334 335 336
	},
	.probe = gr2d_probe,
	.remove = gr2d_remove,
T
Terje Bergstrom 已提交
337
};