xref: /openbmc/linux/drivers/gpu/host1x/hw/channel_hw.c (revision 8631f940b81bf0da3d375fce166d381fa8c47bb2)
1 /*
2  * Tegra host1x Channel
3  *
4  * Copyright (c) 2010-2013, NVIDIA Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <linux/host1x.h>
20 #include <linux/slab.h>
21 
22 #include <trace/events/host1x.h>
23 
24 #include "../channel.h"
25 #include "../dev.h"
26 #include "../intr.h"
27 #include "../job.h"
28 
29 #define TRACE_MAX_LENGTH 128U
30 
31 static void trace_write_gather(struct host1x_cdma *cdma, struct host1x_bo *bo,
32 			       u32 offset, u32 words)
33 {
34 	struct device *dev = cdma_to_channel(cdma)->dev;
35 	void *mem = NULL;
36 
37 	if (host1x_debug_trace_cmdbuf)
38 		mem = host1x_bo_mmap(bo);
39 
40 	if (mem) {
41 		u32 i;
42 		/*
43 		 * Write in batches of 128 as there seems to be a limit
44 		 * of how much you can output to ftrace at once.
45 		 */
46 		for (i = 0; i < words; i += TRACE_MAX_LENGTH) {
47 			u32 num_words = min(words - i, TRACE_MAX_LENGTH);
48 
49 			offset += i * sizeof(u32);
50 
51 			trace_host1x_cdma_push_gather(dev_name(dev), bo,
52 						      num_words, offset,
53 						      mem);
54 		}
55 
56 		host1x_bo_munmap(bo, mem);
57 	}
58 }
59 
60 static void submit_gathers(struct host1x_job *job)
61 {
62 	struct host1x_cdma *cdma = &job->channel->cdma;
63 	unsigned int i;
64 
65 	for (i = 0; i < job->num_gathers; i++) {
66 		struct host1x_job_gather *g = &job->gathers[i];
67 		u32 op1 = host1x_opcode_gather(g->words);
68 		u32 op2 = g->base + g->offset;
69 
70 		trace_write_gather(cdma, g->bo, g->offset, op1 & 0xffff);
71 		host1x_cdma_push(cdma, op1, op2);
72 	}
73 }
74 
75 static inline void synchronize_syncpt_base(struct host1x_job *job)
76 {
77 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
78 	struct host1x_syncpt *sp = host->syncpt + job->syncpt_id;
79 	unsigned int id;
80 	u32 value;
81 
82 	value = host1x_syncpt_read_max(sp);
83 	id = sp->base->id;
84 
85 	host1x_cdma_push(&job->channel->cdma,
86 			 host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
87 				HOST1X_UCLASS_LOAD_SYNCPT_BASE, 1),
88 			 HOST1X_UCLASS_LOAD_SYNCPT_BASE_BASE_INDX_F(id) |
89 			 HOST1X_UCLASS_LOAD_SYNCPT_BASE_VALUE_F(value));
90 }
91 
92 static int channel_submit(struct host1x_job *job)
93 {
94 	struct host1x_channel *ch = job->channel;
95 	struct host1x_syncpt *sp;
96 	u32 user_syncpt_incrs = job->syncpt_incrs;
97 	u32 prev_max = 0;
98 	u32 syncval;
99 	int err;
100 	struct host1x_waitlist *completed_waiter = NULL;
101 	struct host1x *host = dev_get_drvdata(ch->dev->parent);
102 
103 	sp = host->syncpt + job->syncpt_id;
104 	trace_host1x_channel_submit(dev_name(ch->dev),
105 				    job->num_gathers, job->num_relocs,
106 				    job->syncpt_id, job->syncpt_incrs);
107 
108 	/* before error checks, return current max */
109 	prev_max = job->syncpt_end = host1x_syncpt_read_max(sp);
110 
111 	/* get submit lock */
112 	err = mutex_lock_interruptible(&ch->submitlock);
113 	if (err)
114 		goto error;
115 
116 	completed_waiter = kzalloc(sizeof(*completed_waiter), GFP_KERNEL);
117 	if (!completed_waiter) {
118 		mutex_unlock(&ch->submitlock);
119 		err = -ENOMEM;
120 		goto error;
121 	}
122 
123 	/* begin a CDMA submit */
124 	err = host1x_cdma_begin(&ch->cdma, job);
125 	if (err) {
126 		mutex_unlock(&ch->submitlock);
127 		goto error;
128 	}
129 
130 	if (job->serialize) {
131 		/*
132 		 * Force serialization by inserting a host wait for the
133 		 * previous job to finish before this one can commence.
134 		 */
135 		host1x_cdma_push(&ch->cdma,
136 				 host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
137 					host1x_uclass_wait_syncpt_r(), 1),
138 				 host1x_class_host_wait_syncpt(job->syncpt_id,
139 					host1x_syncpt_read_max(sp)));
140 	}
141 
142 	/* Synchronize base register to allow using it for relative waiting */
143 	if (sp->base)
144 		synchronize_syncpt_base(job);
145 
146 	syncval = host1x_syncpt_incr_max(sp, user_syncpt_incrs);
147 
148 	host1x_hw_syncpt_assign_to_channel(host, sp, ch);
149 
150 	job->syncpt_end = syncval;
151 
152 	/* add a setclass for modules that require it */
153 	if (job->class)
154 		host1x_cdma_push(&ch->cdma,
155 				 host1x_opcode_setclass(job->class, 0, 0),
156 				 HOST1X_OPCODE_NOP);
157 
158 	submit_gathers(job);
159 
160 	/* end CDMA submit & stash pinned hMems into sync queue */
161 	host1x_cdma_end(&ch->cdma, job);
162 
163 	trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval);
164 
165 	/* schedule a submit complete interrupt */
166 	err = host1x_intr_add_action(host, sp, syncval,
167 				     HOST1X_INTR_ACTION_SUBMIT_COMPLETE, ch,
168 				     completed_waiter, NULL);
169 	completed_waiter = NULL;
170 	WARN(err, "Failed to set submit complete interrupt");
171 
172 	mutex_unlock(&ch->submitlock);
173 
174 	return 0;
175 
176 error:
177 	kfree(completed_waiter);
178 	return err;
179 }
180 
181 static void enable_gather_filter(struct host1x *host,
182 				 struct host1x_channel *ch)
183 {
184 #if HOST1X_HW >= 6
185 	u32 val;
186 
187 	if (!host->hv_regs)
188 		return;
189 
190 	val = host1x_hypervisor_readl(
191 		host, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
192 	val |= BIT(ch->id % 32);
193 	host1x_hypervisor_writel(
194 		host, val, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32));
195 #elif HOST1X_HW >= 4
196 	host1x_ch_writel(ch,
197 			 HOST1X_CHANNEL_CHANNELCTRL_KERNEL_FILTER_GBUFFER(1),
198 			 HOST1X_CHANNEL_CHANNELCTRL);
199 #endif
200 }
201 
202 static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev,
203 			       unsigned int index)
204 {
205 #if HOST1X_HW < 6
206 	ch->regs = dev->regs + index * 0x4000;
207 #else
208 	ch->regs = dev->regs + index * 0x100;
209 #endif
210 	enable_gather_filter(dev, ch);
211 	return 0;
212 }
213 
214 static const struct host1x_channel_ops host1x_channel_ops = {
215 	.init = host1x_channel_init,
216 	.submit = channel_submit,
217 };
218