Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 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 | |
Thierry Reding | e1e9064 | 2013-09-24 13:59:01 +0200 | [diff] [blame] | 19 | #include <linux/host1x.h> |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 20 | #include <linux/slab.h> |
Thierry Reding | e1e9064 | 2013-09-24 13:59:01 +0200 | [diff] [blame] | 21 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 22 | #include <trace/events/host1x.h> |
| 23 | |
Thierry Reding | fc3be3e | 2013-10-09 10:32:54 +0200 | [diff] [blame] | 24 | #include "../channel.h" |
| 25 | #include "../dev.h" |
| 26 | #include "../intr.h" |
| 27 | #include "../job.h" |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 28 | |
| 29 | #define HOST1X_CHANNEL_SIZE 16384 |
| 30 | #define TRACE_MAX_LENGTH 128U |
| 31 | |
Terje Bergstrom | 6236451 | 2013-03-22 16:34:04 +0200 | [diff] [blame] | 32 | static void trace_write_gather(struct host1x_cdma *cdma, struct host1x_bo *bo, |
| 33 | u32 offset, u32 words) |
| 34 | { |
Thierry Reding | b40d02b | 2014-06-12 13:14:50 +0200 | [diff] [blame] | 35 | struct device *dev = cdma_to_channel(cdma)->dev; |
Terje Bergstrom | 6236451 | 2013-03-22 16:34:04 +0200 | [diff] [blame] | 36 | void *mem = NULL; |
| 37 | |
| 38 | if (host1x_debug_trace_cmdbuf) |
| 39 | mem = host1x_bo_mmap(bo); |
| 40 | |
| 41 | if (mem) { |
| 42 | u32 i; |
| 43 | /* |
| 44 | * Write in batches of 128 as there seems to be a limit |
| 45 | * of how much you can output to ftrace at once. |
| 46 | */ |
| 47 | for (i = 0; i < words; i += TRACE_MAX_LENGTH) { |
Thierry Reding | b40d02b | 2014-06-12 13:14:50 +0200 | [diff] [blame] | 48 | u32 num_words = min(words - i, TRACE_MAX_LENGTH); |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 49 | |
Thierry Reding | b40d02b | 2014-06-12 13:14:50 +0200 | [diff] [blame] | 50 | offset += i * sizeof(u32); |
| 51 | |
| 52 | trace_host1x_cdma_push_gather(dev_name(dev), bo, |
| 53 | num_words, offset, |
| 54 | mem); |
Terje Bergstrom | 6236451 | 2013-03-22 16:34:04 +0200 | [diff] [blame] | 55 | } |
Thierry Reding | b40d02b | 2014-06-12 13:14:50 +0200 | [diff] [blame] | 56 | |
Terje Bergstrom | 6236451 | 2013-03-22 16:34:04 +0200 | [diff] [blame] | 57 | host1x_bo_munmap(bo, mem); |
| 58 | } |
| 59 | } |
| 60 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 61 | static void submit_gathers(struct host1x_job *job) |
| 62 | { |
| 63 | struct host1x_cdma *cdma = &job->channel->cdma; |
| 64 | unsigned int i; |
| 65 | |
| 66 | for (i = 0; i < job->num_gathers; i++) { |
| 67 | struct host1x_job_gather *g = &job->gathers[i]; |
| 68 | u32 op1 = host1x_opcode_gather(g->words); |
| 69 | u32 op2 = g->base + g->offset; |
Thierry Reding | 0b8070d1 | 2016-06-23 11:35:50 +0200 | [diff] [blame] | 70 | |
Terje Bergstrom | 6236451 | 2013-03-22 16:34:04 +0200 | [diff] [blame] | 71 | trace_write_gather(cdma, g->bo, g->offset, op1 & 0xffff); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 72 | host1x_cdma_push(cdma, op1, op2); |
| 73 | } |
| 74 | } |
| 75 | |
Arto Merilainen | f5a954f | 2013-10-14 15:21:53 +0300 | [diff] [blame] | 76 | static inline void synchronize_syncpt_base(struct host1x_job *job) |
| 77 | { |
| 78 | struct host1x *host = dev_get_drvdata(job->channel->dev->parent); |
| 79 | struct host1x_syncpt *sp = host->syncpt + job->syncpt_id; |
Thierry Reding | 5c0d8d3 | 2016-06-23 11:19:00 +0200 | [diff] [blame] | 80 | unsigned int id; |
| 81 | u32 value; |
Arto Merilainen | f5a954f | 2013-10-14 15:21:53 +0300 | [diff] [blame] | 82 | |
| 83 | value = host1x_syncpt_read_max(sp); |
| 84 | id = sp->base->id; |
| 85 | |
| 86 | host1x_cdma_push(&job->channel->cdma, |
| 87 | host1x_opcode_setclass(HOST1X_CLASS_HOST1X, |
| 88 | HOST1X_UCLASS_LOAD_SYNCPT_BASE, 1), |
| 89 | HOST1X_UCLASS_LOAD_SYNCPT_BASE_BASE_INDX_F(id) | |
| 90 | HOST1X_UCLASS_LOAD_SYNCPT_BASE_VALUE_F(value)); |
| 91 | } |
| 92 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 93 | static int channel_submit(struct host1x_job *job) |
| 94 | { |
| 95 | struct host1x_channel *ch = job->channel; |
| 96 | struct host1x_syncpt *sp; |
| 97 | u32 user_syncpt_incrs = job->syncpt_incrs; |
| 98 | u32 prev_max = 0; |
| 99 | u32 syncval; |
| 100 | int err; |
| 101 | struct host1x_waitlist *completed_waiter = NULL; |
| 102 | struct host1x *host = dev_get_drvdata(ch->dev->parent); |
| 103 | |
| 104 | sp = host->syncpt + job->syncpt_id; |
| 105 | trace_host1x_channel_submit(dev_name(ch->dev), |
| 106 | job->num_gathers, job->num_relocs, |
| 107 | job->num_waitchk, job->syncpt_id, |
| 108 | job->syncpt_incrs); |
| 109 | |
| 110 | /* before error checks, return current max */ |
| 111 | prev_max = job->syncpt_end = host1x_syncpt_read_max(sp); |
| 112 | |
| 113 | /* get submit lock */ |
| 114 | err = mutex_lock_interruptible(&ch->submitlock); |
| 115 | if (err) |
| 116 | goto error; |
| 117 | |
| 118 | completed_waiter = kzalloc(sizeof(*completed_waiter), GFP_KERNEL); |
| 119 | if (!completed_waiter) { |
| 120 | mutex_unlock(&ch->submitlock); |
| 121 | err = -ENOMEM; |
| 122 | goto error; |
| 123 | } |
| 124 | |
| 125 | /* begin a CDMA submit */ |
| 126 | err = host1x_cdma_begin(&ch->cdma, job); |
| 127 | if (err) { |
| 128 | mutex_unlock(&ch->submitlock); |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | if (job->serialize) { |
| 133 | /* |
| 134 | * Force serialization by inserting a host wait for the |
| 135 | * previous job to finish before this one can commence. |
| 136 | */ |
| 137 | host1x_cdma_push(&ch->cdma, |
| 138 | host1x_opcode_setclass(HOST1X_CLASS_HOST1X, |
| 139 | host1x_uclass_wait_syncpt_r(), 1), |
| 140 | host1x_class_host_wait_syncpt(job->syncpt_id, |
| 141 | host1x_syncpt_read_max(sp))); |
| 142 | } |
| 143 | |
Arto Merilainen | f5a954f | 2013-10-14 15:21:53 +0300 | [diff] [blame] | 144 | /* Synchronize base register to allow using it for relative waiting */ |
| 145 | if (sp->base) |
| 146 | synchronize_syncpt_base(job); |
| 147 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 148 | syncval = host1x_syncpt_incr_max(sp, user_syncpt_incrs); |
| 149 | |
Mikko Perttunen | c3f5222 | 2017-09-28 15:50:39 +0300 | [diff] [blame] | 150 | host1x_hw_syncpt_assign_to_channel(host, sp, ch); |
| 151 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 152 | job->syncpt_end = syncval; |
| 153 | |
| 154 | /* add a setclass for modules that require it */ |
| 155 | if (job->class) |
| 156 | host1x_cdma_push(&ch->cdma, |
| 157 | host1x_opcode_setclass(job->class, 0, 0), |
| 158 | HOST1X_OPCODE_NOP); |
| 159 | |
| 160 | submit_gathers(job); |
| 161 | |
| 162 | /* end CDMA submit & stash pinned hMems into sync queue */ |
| 163 | host1x_cdma_end(&ch->cdma, job); |
| 164 | |
| 165 | trace_host1x_channel_submitted(dev_name(ch->dev), prev_max, syncval); |
| 166 | |
| 167 | /* schedule a submit complete interrupt */ |
| 168 | err = host1x_intr_add_action(host, job->syncpt_id, syncval, |
| 169 | HOST1X_INTR_ACTION_SUBMIT_COMPLETE, ch, |
| 170 | completed_waiter, NULL); |
| 171 | completed_waiter = NULL; |
| 172 | WARN(err, "Failed to set submit complete interrupt"); |
| 173 | |
| 174 | mutex_unlock(&ch->submitlock); |
| 175 | |
| 176 | return 0; |
| 177 | |
| 178 | error: |
| 179 | kfree(completed_waiter); |
| 180 | return err; |
| 181 | } |
| 182 | |
Mikko Perttunen | 2316f29 | 2017-09-28 15:50:40 +0300 | [diff] [blame^] | 183 | static void enable_gather_filter(struct host1x *host, |
| 184 | struct host1x_channel *ch) |
| 185 | { |
| 186 | #if HOST1X_HW >= 6 |
| 187 | u32 val; |
| 188 | |
| 189 | if (!host->hv_regs) |
| 190 | return; |
| 191 | |
| 192 | val = host1x_hypervisor_readl( |
| 193 | host, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32)); |
| 194 | val |= BIT(ch->id % 32); |
| 195 | host1x_hypervisor_writel( |
| 196 | host, val, HOST1X_HV_CH_KERNEL_FILTER_GBUFFER(ch->id / 32)); |
| 197 | #elif HOST1X_HW >= 4 |
| 198 | host1x_ch_writel(ch, |
| 199 | HOST1X_CHANNEL_CHANNELCTRL_KERNEL_FILTER_GBUFFER(1), |
| 200 | HOST1X_CHANNEL_CHANNELCTRL); |
| 201 | #endif |
| 202 | } |
| 203 | |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 204 | static int host1x_channel_init(struct host1x_channel *ch, struct host1x *dev, |
| 205 | unsigned int index) |
| 206 | { |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 207 | ch->regs = dev->regs + index * HOST1X_CHANNEL_SIZE; |
Mikko Perttunen | 2316f29 | 2017-09-28 15:50:40 +0300 | [diff] [blame^] | 208 | enable_gather_filter(dev, ch); |
Terje Bergstrom | 6579324 | 2013-03-22 16:34:03 +0200 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static const struct host1x_channel_ops host1x_channel_ops = { |
| 213 | .init = host1x_channel_init, |
| 214 | .submit = channel_submit, |
| 215 | }; |