summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/nvhost_channel.c
blob: fd309ee9917ba4e5cc6d0da45bb288c3880ccd16 (plain)
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
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * drivers/video/tegra/host/nvhost_channel.c
 *
 * Tegra Graphics Host Channel
 *
 * Copyright (c) 2010-2012, 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 "nvhost_channel.h"
#include "dev.h"
#include "nvhost_acm.h"
#include "nvhost_job.h"
#include "chip_support.h"

#include <trace/events/nvhost.h>
#include <linux/nvhost_ioctl.h>
#include <linux/slab.h>

#define NVHOST_CHANNEL_LOW_PRIO_MAX_WAIT 50

int nvhost_channel_init(struct nvhost_channel *ch,
		struct nvhost_master *dev, int index)
{
	int err;
	struct nvhost_device *ndev;

	/* Link nvhost_device to nvhost_channel */
	err = channel_op().init(ch, dev, index);
	if (err < 0) {
		dev_err(&dev->dev->dev, "failed to init channel %d\n",
				index);
		return err;
	}
	ndev = ch->dev;
	ndev->channel = ch;

	return 0;
}

int nvhost_channel_submit(struct nvhost_job *job)
{
	/*
	 * Check if queue has higher priority jobs running. If so, wait until
	 * queue is empty. Ignores result from nvhost_cdma_flush, as we submit
	 * either when push buffer is empty or when we reach the timeout.
	 */
	int higher_count = 0;

	switch (job->priority) {
	case NVHOST_PRIORITY_HIGH:
		higher_count = 0;
		break;
	case NVHOST_PRIORITY_MEDIUM:
		higher_count = job->ch->cdma.high_prio_count;
		break;
	case NVHOST_PRIORITY_LOW:
		higher_count = job->ch->cdma.high_prio_count
			+ job->ch->cdma.med_prio_count;
		break;
	}
	if (higher_count > 0)
		(void)nvhost_cdma_flush(&job->ch->cdma,
				NVHOST_CHANNEL_LOW_PRIO_MAX_WAIT);

	return channel_op().submit(job);
}

struct nvhost_channel *nvhost_getchannel(struct nvhost_channel *ch)
{
	int err = 0;
	struct nvhost_driver *drv = to_nvhost_driver(ch->dev->dev.driver);

	mutex_lock(&ch->reflock);
	if (ch->refcount == 0) {
		if (drv->init)
			drv->init(ch->dev);
		err = nvhost_cdma_init(&ch->cdma);
	} else if (ch->dev->exclusive) {
		err = -EBUSY;
	}
	if (!err)
		ch->refcount++;

	mutex_unlock(&ch->reflock);

	/* Keep alive modules that needs to be when a channel is open */
	if (!err && ch->dev->keepalive)
		nvhost_module_busy(ch->dev);

	return err ? NULL : ch;
}

void nvhost_putchannel(struct nvhost_channel *ch, struct nvhost_hwctx *ctx)
{
	BUG_ON(!channel_cdma_op().stop);

	if (ctx) {
		mutex_lock(&ch->submitlock);
		if (ch->cur_ctx == ctx)
			ch->cur_ctx = NULL;
		mutex_unlock(&ch->submitlock);
	}

	/* Allow keep-alive'd module to be turned off */
	if (ch->dev->keepalive)
		nvhost_module_idle(ch->dev);

	mutex_lock(&ch->reflock);
	if (ch->refcount == 1) {
		channel_cdma_op().stop(&ch->cdma);
		nvhost_cdma_deinit(&ch->cdma);
		nvhost_module_suspend(ch->dev);
	}
	ch->refcount--;
	mutex_unlock(&ch->reflock);
}

int nvhost_channel_suspend(struct nvhost_channel *ch)
{
	int ret = 0;

	mutex_lock(&ch->reflock);
	BUG_ON(!channel_cdma_op().stop);

	if (ch->refcount) {
		ret = nvhost_module_suspend(ch->dev);
		if (!ret)
			channel_cdma_op().stop(&ch->cdma);
	}
	mutex_unlock(&ch->reflock);

	return ret;
}

struct nvhost_channel *nvhost_alloc_channel_internal(int chindex,
	int max_channels, int *current_channel_count)
{
	struct nvhost_channel *ch = NULL;

	if ( (chindex > max_channels) ||
	     ( (*current_channel_count + 1) > max_channels) )
		return NULL;
	else {
		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
		if (ch == NULL)
			return NULL;
		else {
			(*current_channel_count)++;
			return ch;
		}
	}
}

void nvhost_free_channel_internal(struct nvhost_channel *ch,
	int *current_channel_count)
{
	kfree(ch);
	(*current_channel_count)--;
}

int nvhost_channel_save_context(struct nvhost_channel *ch)
{
	struct nvhost_hwctx *cur_ctx = ch->cur_ctx;
	int err = 0;
	if (cur_ctx)
		err = channel_op().save_context(ch);

	return err;

}

int nvhost_channel_drain_read_fifo(struct nvhost_channel *ch,
			u32 *ptr, unsigned int count, unsigned int *pending)
{
	return channel_op().drain_read_fifo(ch, ptr, count, pending);
}