xref: /openbmc/linux/drivers/gpu/host1x/hw/channel_hw.c (revision 930beb5a)
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 HOST1X_CHANNEL_SIZE 16384
30 #define TRACE_MAX_LENGTH 128U
31 
32 static void trace_write_gather(struct host1x_cdma *cdma, struct host1x_bo *bo,
33 			       u32 offset, u32 words)
34 {
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 			trace_host1x_cdma_push_gather(
48 				dev_name(cdma_to_channel(cdma)->dev),
49 				(u32)bo, min(words - i, TRACE_MAX_LENGTH),
50 				offset + i * sizeof(u32), mem);
51 		}
52 		host1x_bo_munmap(bo, mem);
53 	}
54 }
55 
56 static void submit_gathers(struct host1x_job *job)
57 {
58 	struct host1x_cdma *cdma = &job->channel->cdma;
59 	unsigned int i;
60 
61 	for (i = 0; i < job->num_gathers; i++) {
62 		struct host1x_job_gather *g = &job->gathers[i];
63 		u32 op1 = host1x_opcode_gather(g->words);
64 		u32 op2 = g->base + g->offset;
65 		trace_write_gather(cdma, g->bo, g->offset, op1 & 0xffff);
66 		host1x_cdma_push(cdma, op1, op2);
67 	}
68 }
69 
70 static inline void synchronize_syncpt_base(struct host1x_job *job)
71 {
72 	struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
73 	struct host1x_syncpt *sp = host->syncpt + job->syncpt_id;
74 	u32 id, value;
75 
76 	value = host1x_syncpt_read_max(sp);
77 	id = sp->base->id;
78 
79 	host1x_cdma_push(&job->channel->cdma,
80 			 host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
81 				HOST1X_UCLASS_LOAD_SYNCPT_BASE, 1),
82 			 HOST1X_UCLASS_LOAD_SYNCPT_BASE_BASE_INDX_F(id) |
83 			 HOST1X_UCLASS_LOAD_SYNCPT_BASE_VALUE_F(value));
84 }
85 
86 static int channel_submit(struct host1x_job *job)
87 {
88 	struct host1x_channel *ch = job->channel;
89 	struct host1x_syncpt *sp;
90 	u32 user_syncpt_incrs = job->syncpt_incrs;
91 	u32 prev_max = 0;
92 	u32 syncval;
93 	int err;
94 	struct host1x_waitlist *completed_waiter = NULL;
95 	struct host1x *host = dev_get_drvdata(ch->dev->parent);
96 
97 	sp = host->syncpt + job->syncpt_id;
98 	trace_host1x_channel_submit(dev_name(ch->dev),
99 				    job->num_gathers, job->num_relocs,
100 				    job->num_waitchk, job->syncpt_id,
101 				    job->syncpt_incrs);
102 
103 	/* before error checks, return current max */
104 	prev_max = job->syncpt_end = host1x_syncpt_read_max(sp);
105 
106 	/* get submit lock */
107 	err = mutex_lock_interruptible(&ch->submitlock);
108 	if (err)
109 		goto error;
110 
111 	completed_waiter = kzalloc(sizeof(*completed_waiter), GFP_KERNEL);
112 	if (!completed_waiter) {
113 		mutex_unlock(&ch->submitlock);
114 		err = -ENOMEM;
115 		goto error;
116 	}
117 
118 	/* begin a CDMA submit */
119 	err = host1x_cdma_begin(&ch->cdma, job);
120 	if (err) {
121 		mutex_unlock(&ch->submitlock);
122 		goto error;
123 	}
124 
125 	if (job->serialize) {
126 		/*
127 		 * Force serialization by inserting a host wait for the
128 		 * previous job to finish before this one can commence.
129 		 */
130 		host1x_cdma_push(&ch->cdma,
131 				 host1x_opcode_setclass(HOST1X_CLASS_HOST1X,
132 					host1x_uclass_wait_syncpt_r(), 1),
133 				 host1x_class_host_wait_syncpt(job->syncpt_id,
134 					host1x_syncpt_read_max(sp)));
135 	}
136 
137 	/* Synchronize base register to allow using it for relative waiting */
138 	if (sp->base)
139 		synchronize_syncpt_base(job);
140 
141 	syncval = host1x_syncpt_incr_max(sp, user_syncpt_incrs);
142 
143 	job->syncpt_end = syncval;
144 
145 	/* add a setclass for modules that require it */
146 	if (job->class)
147 		host1x_cdma_push(&ch->cdma,
148 				 host1x_opcode_setclass(job->class, 0, 0),
149 				 HOST1X_OPCODE_NOP);
150 
151 	submit_gathers(job);
152 
153 	/* end CDMA submit & stash pinned hMems into sync queue */
154 	host1x_cdma_end(&ch->cdma, job);
155 
156 	trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval);
157 
158 	/* schedule a submit complete interrupt */
159 	err = host1x_intr_add_action(host, job->syncpt_id, syncval,
160 				     HOST1X_INTR_ACTION_SUBMIT_COMPLETE, ch,
161 				     completed_waiter, NULL);
162 	completed_waiter = NULL;
163 	WARN(err, "Failed to set submit complete interrupt");
164 
165 	mutex_unlock(&ch->submitlock);
166 
167 	return 0;
168 
169 error:
170 	kfree(completed_waiter);
171 	return err;
172 }
173 
174 static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev,
175 			       unsigned int index)
176 {
177 	ch->id = index;
178 	mutex_init(&ch->reflock);
179 	mutex_init(&ch->submitlock);
180 
181 	ch->regs = dev->regs + index * HOST1X_CHANNEL_SIZE;
182 	return 0;
183 }
184 
185 static const struct host1x_channel_ops host1x_channel_ops = {
186 	.init = host1x_channel_init,
187 	.submit = channel_submit,
188 };
189