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