Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 1 | /* |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 2 | * Copyright (c) 2012-2013, NVIDIA Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | |
| 19 | #include "channel.h" |
| 20 | #include "drm.h" |
| 21 | #include "gem.h" |
| 22 | #include "job.h" |
| 23 | #include "host1x.h" |
| 24 | #include "host1x_bo.h" |
| 25 | #include "host1x_client.h" |
| 26 | #include "syncpt.h" |
| 27 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 28 | #define GR2D_NUM_REGS 0x4d |
| 29 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 30 | struct gr2d { |
| 31 | struct host1x_client client; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 32 | struct host1x_channel *channel; |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 33 | struct clk *clk; |
| 34 | |
| 35 | DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static inline struct gr2d *to_gr2d(struct host1x_client *client) |
| 39 | { |
| 40 | return container_of(client, struct gr2d, client); |
| 41 | } |
| 42 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 43 | static int gr2d_client_init(struct host1x_client *client, |
| 44 | struct drm_device *drm) |
| 45 | { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int gr2d_client_exit(struct host1x_client *client) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int gr2d_open_channel(struct host1x_client *client, |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 55 | struct tegra_drm_context *context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 56 | { |
| 57 | struct gr2d *gr2d = to_gr2d(client); |
| 58 | |
| 59 | context->channel = host1x_channel_get(gr2d->channel); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 60 | if (!context->channel) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 66 | static void gr2d_close_channel(struct tegra_drm_context *context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 67 | { |
| 68 | host1x_channel_put(context->channel); |
| 69 | } |
| 70 | |
| 71 | static struct host1x_bo *host1x_bo_lookup(struct drm_device *drm, |
| 72 | struct drm_file *file, |
| 73 | u32 handle) |
| 74 | { |
| 75 | struct drm_gem_object *gem; |
| 76 | struct tegra_bo *bo; |
| 77 | |
| 78 | gem = drm_gem_object_lookup(drm, file, handle); |
| 79 | if (!gem) |
Thierry Reding | dc618db | 2013-05-19 14:21:22 +0200 | [diff] [blame] | 80 | return NULL; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 81 | |
| 82 | mutex_lock(&drm->struct_mutex); |
| 83 | drm_gem_object_unreference(gem); |
| 84 | mutex_unlock(&drm->struct_mutex); |
| 85 | |
| 86 | bo = to_tegra_bo(gem); |
| 87 | return &bo->base; |
| 88 | } |
| 89 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 90 | static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset) |
| 91 | { |
| 92 | struct gr2d *gr2d = dev_get_drvdata(dev); |
| 93 | |
| 94 | switch (class) { |
| 95 | case HOST1X_CLASS_HOST1X: |
| 96 | if (offset == 0x2b) |
| 97 | return 1; |
| 98 | |
| 99 | break; |
| 100 | |
| 101 | case HOST1X_CLASS_GR2D: |
| 102 | case HOST1X_CLASS_GR2D_SB: |
| 103 | if (offset >= GR2D_NUM_REGS) |
| 104 | break; |
| 105 | |
| 106 | if (test_bit(offset, gr2d->addr_regs)) |
| 107 | return 1; |
| 108 | |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 115 | static int gr2d_submit(struct tegra_drm_context *context, |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 116 | struct drm_tegra_submit *args, struct drm_device *drm, |
| 117 | struct drm_file *file) |
| 118 | { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 119 | unsigned int num_cmdbufs = args->num_cmdbufs; |
| 120 | unsigned int num_relocs = args->num_relocs; |
| 121 | unsigned int num_waitchks = args->num_waitchks; |
| 122 | struct drm_tegra_cmdbuf __user *cmdbufs = |
| 123 | (void * __user)(uintptr_t)args->cmdbufs; |
| 124 | struct drm_tegra_reloc __user *relocs = |
| 125 | (void * __user)(uintptr_t)args->relocs; |
| 126 | struct drm_tegra_waitchk __user *waitchks = |
| 127 | (void * __user)(uintptr_t)args->waitchks; |
| 128 | struct drm_tegra_syncpt syncpt; |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 129 | struct host1x_job *job; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 130 | int err; |
| 131 | |
| 132 | /* We don't yet support other than one syncpt_incr struct per submit */ |
| 133 | if (args->num_syncpts != 1) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | job = host1x_job_alloc(context->channel, args->num_cmdbufs, |
| 137 | args->num_relocs, args->num_waitchks); |
| 138 | if (!job) |
| 139 | return -ENOMEM; |
| 140 | |
| 141 | job->num_relocs = args->num_relocs; |
| 142 | job->num_waitchk = args->num_waitchks; |
| 143 | job->client = (u32)args->context; |
| 144 | job->class = context->client->class; |
| 145 | job->serialize = true; |
| 146 | |
| 147 | while (num_cmdbufs) { |
| 148 | struct drm_tegra_cmdbuf cmdbuf; |
| 149 | struct host1x_bo *bo; |
| 150 | |
| 151 | err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf)); |
| 152 | if (err) |
| 153 | goto fail; |
| 154 | |
| 155 | bo = host1x_bo_lookup(drm, file, cmdbuf.handle); |
Wei Yongjun | 2639f2b | 2013-04-24 14:50:51 +0800 | [diff] [blame] | 156 | if (!bo) { |
| 157 | err = -ENOENT; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 158 | goto fail; |
Wei Yongjun | 2639f2b | 2013-04-24 14:50:51 +0800 | [diff] [blame] | 159 | } |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 160 | |
| 161 | host1x_job_add_gather(job, bo, cmdbuf.words, cmdbuf.offset); |
| 162 | num_cmdbufs--; |
| 163 | cmdbufs++; |
| 164 | } |
| 165 | |
| 166 | err = copy_from_user(job->relocarray, relocs, |
| 167 | sizeof(*relocs) * num_relocs); |
| 168 | if (err) |
| 169 | goto fail; |
| 170 | |
| 171 | while (num_relocs--) { |
| 172 | struct host1x_reloc *reloc = &job->relocarray[num_relocs]; |
| 173 | struct host1x_bo *cmdbuf, *target; |
| 174 | |
| 175 | cmdbuf = host1x_bo_lookup(drm, file, (u32)reloc->cmdbuf); |
| 176 | target = host1x_bo_lookup(drm, file, (u32)reloc->target); |
| 177 | |
| 178 | reloc->cmdbuf = cmdbuf; |
| 179 | reloc->target = target; |
| 180 | |
Wei Yongjun | 2639f2b | 2013-04-24 14:50:51 +0800 | [diff] [blame] | 181 | if (!reloc->target || !reloc->cmdbuf) { |
| 182 | err = -ENOENT; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 183 | goto fail; |
Wei Yongjun | 2639f2b | 2013-04-24 14:50:51 +0800 | [diff] [blame] | 184 | } |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | err = copy_from_user(job->waitchk, waitchks, |
| 188 | sizeof(*waitchks) * num_waitchks); |
| 189 | if (err) |
| 190 | goto fail; |
| 191 | |
| 192 | err = copy_from_user(&syncpt, (void * __user)(uintptr_t)args->syncpts, |
| 193 | sizeof(syncpt)); |
| 194 | if (err) |
| 195 | goto fail; |
| 196 | |
| 197 | job->syncpt_id = syncpt.id; |
| 198 | job->syncpt_incrs = syncpt.incrs; |
| 199 | job->timeout = 10000; |
| 200 | job->is_addr_reg = gr2d_is_addr_reg; |
| 201 | |
| 202 | if (args->timeout && args->timeout < 10000) |
| 203 | job->timeout = args->timeout; |
| 204 | |
| 205 | err = host1x_job_pin(job, context->client->dev); |
| 206 | if (err) |
| 207 | goto fail; |
| 208 | |
| 209 | err = host1x_job_submit(job); |
| 210 | if (err) |
| 211 | goto fail_submit; |
| 212 | |
| 213 | args->fence = job->syncpt_end; |
| 214 | |
| 215 | host1x_job_put(job); |
| 216 | return 0; |
| 217 | |
| 218 | fail_submit: |
| 219 | host1x_job_unpin(job); |
| 220 | fail: |
| 221 | host1x_job_put(job); |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | static struct host1x_client_ops gr2d_client_ops = { |
| 226 | .drm_init = gr2d_client_init, |
| 227 | .drm_exit = gr2d_client_exit, |
| 228 | .open_channel = gr2d_open_channel, |
| 229 | .close_channel = gr2d_close_channel, |
| 230 | .submit = gr2d_submit, |
| 231 | }; |
| 232 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 233 | static const struct of_device_id gr2d_match[] = { |
| 234 | { .compatible = "nvidia,tegra30-gr2d" }, |
| 235 | { .compatible = "nvidia,tegra20-gr2d" }, |
| 236 | { }, |
| 237 | }; |
| 238 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 239 | static const u32 gr2d_addr_regs[] = { |
| 240 | 0x1a, 0x1b, 0x26, 0x2b, 0x2c, 0x2d, 0x31, 0x32, |
| 241 | 0x48, 0x49, 0x4a, 0x4b, 0x4c |
| 242 | }; |
| 243 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 244 | static int gr2d_probe(struct platform_device *pdev) |
| 245 | { |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 246 | struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 247 | struct device *dev = &pdev->dev; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 248 | struct host1x_syncpt **syncpts; |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 249 | struct gr2d *gr2d; |
| 250 | unsigned int i; |
| 251 | int err; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 252 | |
| 253 | gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL); |
| 254 | if (!gr2d) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL); |
| 258 | if (!syncpts) |
| 259 | return -ENOMEM; |
| 260 | |
| 261 | gr2d->clk = devm_clk_get(dev, NULL); |
| 262 | if (IS_ERR(gr2d->clk)) { |
| 263 | dev_err(dev, "cannot get clock\n"); |
| 264 | return PTR_ERR(gr2d->clk); |
| 265 | } |
| 266 | |
| 267 | err = clk_prepare_enable(gr2d->clk); |
| 268 | if (err) { |
| 269 | dev_err(dev, "cannot turn on clock\n"); |
| 270 | return err; |
| 271 | } |
| 272 | |
| 273 | gr2d->channel = host1x_channel_request(dev); |
| 274 | if (!gr2d->channel) |
| 275 | return -ENOMEM; |
| 276 | |
Arto Merilainen | ece6689 | 2013-05-29 13:26:07 +0300 | [diff] [blame] | 277 | *syncpts = host1x_syncpt_request(dev, false); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 278 | if (!(*syncpts)) { |
| 279 | host1x_channel_free(gr2d->channel); |
| 280 | return -ENOMEM; |
| 281 | } |
| 282 | |
| 283 | gr2d->client.ops = &gr2d_client_ops; |
| 284 | gr2d->client.dev = dev; |
| 285 | gr2d->client.class = HOST1X_CLASS_GR2D; |
| 286 | gr2d->client.syncpts = syncpts; |
| 287 | gr2d->client.num_syncpts = 1; |
| 288 | |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 289 | err = host1x_register_client(tegra, &gr2d->client); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 290 | if (err < 0) { |
| 291 | dev_err(dev, "failed to register host1x client: %d\n", err); |
| 292 | return err; |
| 293 | } |
| 294 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 295 | /* initialize address register map */ |
| 296 | for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++) |
| 297 | set_bit(gr2d_addr_regs[i], gr2d->addr_regs); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 298 | |
| 299 | platform_set_drvdata(pdev, gr2d); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 304 | static int gr2d_remove(struct platform_device *pdev) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 305 | { |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 306 | struct tegra_drm *tegra = host1x_get_drm_data(pdev->dev.parent); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 307 | struct gr2d *gr2d = platform_get_drvdata(pdev); |
| 308 | unsigned int i; |
| 309 | int err; |
| 310 | |
Thierry Reding | 386a2a7 | 2013-09-24 13:22:17 +0200 | [diff] [blame] | 311 | err = host1x_unregister_client(tegra, &gr2d->client); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 312 | if (err < 0) { |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 313 | dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", |
| 314 | err); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 315 | return err; |
| 316 | } |
| 317 | |
| 318 | for (i = 0; i < gr2d->client.num_syncpts; i++) |
| 319 | host1x_syncpt_free(gr2d->client.syncpts[i]); |
| 320 | |
| 321 | host1x_channel_free(gr2d->channel); |
| 322 | clk_disable_unprepare(gr2d->clk); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | struct platform_driver tegra_gr2d_driver = { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 328 | .driver = { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 329 | .name = "gr2d", |
| 330 | .of_match_table = gr2d_match, |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame^] | 331 | }, |
| 332 | .probe = gr2d_probe, |
| 333 | .remove = gr2d_remove, |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 334 | }; |