video: tegra: host: Syncpoints deliver waitbases
[linux-3.10.git] / drivers / video / tegra / host / bus_client.c
1 /*
2  * drivers/video/tegra/host/bus_client.c
3  *
4  * Tegra Graphics Host Client Module
5  *
6  * Copyright (c) 2010-2013, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/spinlock.h>
24 #include <linux/fs.h>
25 #include <linux/cdev.h>
26 #include <linux/uaccess.h>
27 #include <linux/file.h>
28 #include <linux/clk.h>
29 #include <linux/hrtimer.h>
30 #include <linux/export.h>
31 #include <linux/firmware.h>
32
33 #include <trace/events/nvhost.h>
34
35 #include <linux/io.h>
36 #include <linux/string.h>
37
38 #include <linux/nvhost.h>
39 #include <linux/nvhost_ioctl.h>
40
41 #include <mach/gpufuse.h>
42 #include <mach/hardware.h>
43
44 #include "debug.h"
45 #include "bus_client.h"
46 #include "dev.h"
47 #include "nvhost_memmgr.h"
48 #include "chip_support.h"
49 #include "nvhost_acm.h"
50
51 #include "nvhost_syncpt.h"
52 #include "nvhost_channel.h"
53 #include "nvhost_job.h"
54 #include "nvhost_hwctx.h"
55 #include "user_hwctx.h"
56
57 static int validate_reg(struct platform_device *ndev, u32 offset, int count)
58 {
59         struct resource *r = platform_get_resource(ndev, IORESOURCE_MEM, 0);
60         int err = 0;
61
62         if (offset + 4 * count > resource_size(r)
63                         || (offset + 4 * count < offset))
64                 err = -EPERM;
65
66         return err;
67 }
68
69 int nvhost_read_module_regs(struct platform_device *ndev,
70                         u32 offset, int count, u32 *values)
71 {
72         struct nvhost_device_data *pdata = platform_get_drvdata(ndev);
73         void __iomem *p = pdata->aperture[0] + offset;
74         int err;
75
76         /* verify offset */
77         err = validate_reg(ndev, offset, count);
78         if (err)
79                 return err;
80
81         nvhost_module_busy(ndev);
82         while (count--) {
83                 *(values++) = readl(p);
84                 p += 4;
85         }
86         rmb();
87         nvhost_module_idle(ndev);
88
89         return 0;
90 }
91
92 int nvhost_write_module_regs(struct platform_device *ndev,
93                         u32 offset, int count, const u32 *values)
94 {
95         void __iomem *p;
96         int err;
97         struct nvhost_device_data *pdata = platform_get_drvdata(ndev);
98
99         p = pdata->aperture[0] + offset;
100
101         /* verify offset */
102         err = validate_reg(ndev, offset, count);
103         if (err)
104                 return err;
105
106         nvhost_module_busy(ndev);
107         while (count--) {
108                 writel(*(values++), p);
109                 p += 4;
110         }
111         wmb();
112         nvhost_module_idle(ndev);
113
114         return 0;
115 }
116
117 struct nvhost_channel_userctx {
118         struct nvhost_channel *ch;
119         struct nvhost_hwctx *hwctx;
120         struct nvhost_submit_hdr_ext hdr;
121         int num_relocshifts;
122         struct nvhost_job *job;
123         struct mem_mgr *memmgr;
124         u32 timeout;
125         u32 priority;
126         int clientid;
127         bool timeout_debug_dump;
128 };
129
130 static int nvhost_channelrelease(struct inode *inode, struct file *filp)
131 {
132         struct nvhost_channel_userctx *priv = filp->private_data;
133
134         trace_nvhost_channel_release(dev_name(&priv->ch->dev->dev));
135
136         filp->private_data = NULL;
137
138         nvhost_module_remove_client(priv->ch->dev, priv);
139         nvhost_putchannel(priv->ch, priv->hwctx);
140
141         if (priv->hwctx)
142                 priv->hwctx->h->put(priv->hwctx);
143
144         if (priv->job)
145                 nvhost_job_put(priv->job);
146
147         nvhost_memmgr_put_mgr(priv->memmgr);
148         kfree(priv);
149         return 0;
150 }
151
152 static int nvhost_channelopen(struct inode *inode, struct file *filp)
153 {
154         struct nvhost_channel_userctx *priv;
155         struct nvhost_channel *ch;
156
157         ch = container_of(inode->i_cdev, struct nvhost_channel, cdev);
158         ch = nvhost_getchannel(ch);
159         if (!ch)
160                 return -ENOMEM;
161         trace_nvhost_channel_open(dev_name(&ch->dev->dev));
162
163         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
164         if (!priv) {
165                 nvhost_putchannel(ch, NULL);
166                 return -ENOMEM;
167         }
168         filp->private_data = priv;
169         priv->ch = ch;
170         if(nvhost_module_add_client(ch->dev, priv))
171                 goto fail;
172
173         if (ch->ctxhandler && ch->ctxhandler->alloc) {
174                 priv->hwctx = ch->ctxhandler->alloc(ch->ctxhandler, ch);
175                 if (!priv->hwctx)
176                         goto fail;
177         }
178         priv->priority = NVHOST_PRIORITY_MEDIUM;
179         priv->clientid = atomic_add_return(1,
180                         &nvhost_get_host(ch->dev)->clientid);
181         priv->timeout = CONFIG_TEGRA_GRHOST_DEFAULT_TIMEOUT;
182         priv->timeout_debug_dump = true;
183         if (tegra_platform_is_linsim())
184                 priv->timeout = 0;
185
186         return 0;
187 fail:
188         nvhost_channelrelease(inode, filp);
189         return -ENOMEM;
190 }
191
192 static int set_submit(struct nvhost_channel_userctx *ctx)
193 {
194         struct platform_device *ndev = ctx->ch->dev;
195         struct nvhost_master *host = nvhost_get_host(ndev);
196
197         /* submit should have at least 1 cmdbuf */
198         if (!ctx->hdr.num_cmdbufs ||
199                         !nvhost_syncpt_is_valid(&host->syncpt,
200                                 ctx->hdr.syncpt_id))
201                 return -EIO;
202
203         if (!ctx->memmgr) {
204                 dev_err(&ndev->dev, "no nvmap context set\n");
205                 return -EFAULT;
206         }
207
208         if (ctx->job) {
209                 dev_warn(&ndev->dev, "performing channel submit when a job already exists\n");
210                 nvhost_job_put(ctx->job);
211         }
212         ctx->job = nvhost_job_alloc(ctx->ch,
213                         ctx->hwctx,
214                         ctx->hdr.num_cmdbufs,
215                         ctx->hdr.num_relocs,
216                         ctx->hdr.num_waitchks,
217                         1,
218                         ctx->memmgr);
219         if (!ctx->job)
220                 return -ENOMEM;
221         ctx->job->timeout = ctx->timeout;
222         ctx->job->sp->id = ctx->hdr.syncpt_id;
223         ctx->job->sp->incrs = ctx->hdr.syncpt_incrs;
224         ctx->job->hwctx_syncpt_idx = 0;
225         ctx->job->num_syncpts = 1;
226         ctx->job->priority = ctx->priority;
227         ctx->job->clientid = ctx->clientid;
228         ctx->job->timeout_debug_dump = ctx->timeout_debug_dump;
229
230         if (ctx->hdr.submit_version >= NVHOST_SUBMIT_VERSION_V2)
231                 ctx->num_relocshifts = ctx->hdr.num_relocs;
232
233         return 0;
234 }
235
236 static void reset_submit(struct nvhost_channel_userctx *ctx)
237 {
238         ctx->hdr.num_cmdbufs = 0;
239         ctx->hdr.num_relocs = 0;
240         ctx->num_relocshifts = 0;
241         ctx->hdr.num_waitchks = 0;
242
243         if (ctx->job) {
244                 nvhost_job_put(ctx->job);
245                 ctx->job = NULL;
246         }
247 }
248
249 static ssize_t nvhost_channelwrite(struct file *filp, const char __user *buf,
250                                 size_t count, loff_t *offp)
251 {
252         struct nvhost_channel_userctx *priv = filp->private_data;
253         size_t remaining = count;
254         int err = 0;
255         struct nvhost_job *job = priv->job;
256         struct nvhost_submit_hdr_ext *hdr = &priv->hdr;
257         const char *chname = priv->ch->dev->name;
258
259         if (!job)
260                 return -EIO;
261
262         while (remaining) {
263                 size_t consumed;
264                 if (!hdr->num_relocs &&
265                     !priv->num_relocshifts &&
266                     !hdr->num_cmdbufs &&
267                     !hdr->num_waitchks) {
268                         consumed = sizeof(struct nvhost_submit_hdr);
269                         if (remaining < consumed)
270                                 break;
271                         if (copy_from_user(hdr, buf, consumed)) {
272                                 err = -EFAULT;
273                                 break;
274                         }
275                         hdr->submit_version = NVHOST_SUBMIT_VERSION_V0;
276                         err = set_submit(priv);
277                         if (err)
278                                 break;
279                         trace_nvhost_channel_write_submit(chname,
280                           count, hdr->num_cmdbufs, hdr->num_relocs,
281                           hdr->syncpt_id, hdr->syncpt_incrs);
282                 } else if (hdr->num_cmdbufs) {
283                         struct nvhost_cmdbuf cmdbuf;
284                         consumed = sizeof(cmdbuf);
285                         if (remaining < consumed)
286                                 break;
287                         if (copy_from_user(&cmdbuf, buf, consumed)) {
288                                 err = -EFAULT;
289                                 break;
290                         }
291                         trace_nvhost_channel_write_cmdbuf(chname,
292                                 cmdbuf.mem, cmdbuf.words, cmdbuf.offset);
293                         nvhost_job_add_gather(job,
294                                 cmdbuf.mem, cmdbuf.words, cmdbuf.offset);
295                         hdr->num_cmdbufs--;
296                 } else if (hdr->num_relocs) {
297                         int numrelocs = remaining / sizeof(struct nvhost_reloc);
298                         if (!numrelocs)
299                                 break;
300                         numrelocs = min_t(int, numrelocs, priv->hdr.num_relocs);
301                         consumed = numrelocs * sizeof(struct nvhost_reloc);
302                         if (copy_from_user(&job->relocarray[job->num_relocs],
303                                         buf, consumed)) {
304                                 err = -EFAULT;
305                                 break;
306                         }
307                         while (numrelocs) {
308                                 struct nvhost_reloc *reloc =
309                                         &job->relocarray[job->num_relocs];
310                                 trace_nvhost_channel_write_reloc(chname,
311                                         reloc->cmdbuf_mem,
312                                         reloc->cmdbuf_offset,
313                                         reloc->target,
314                                         reloc->target_offset);
315                                 job->num_relocs++;
316                                 hdr->num_relocs--;
317                                 numrelocs--;
318                         }
319                 } else if (hdr->num_waitchks) {
320                         int numwaitchks =
321                                 (remaining / sizeof(struct nvhost_waitchk));
322                         if (!numwaitchks)
323                                 break;
324                         numwaitchks = min_t(int,
325                                 numwaitchks, hdr->num_waitchks);
326                         consumed = numwaitchks * sizeof(struct nvhost_waitchk);
327                         if (copy_from_user(&job->waitchk[job->num_waitchk],
328                                         buf, consumed)) {
329                                 err = -EFAULT;
330                                 break;
331                         }
332                         trace_nvhost_channel_write_waitchks(
333                           chname, numwaitchks);
334                         job->num_waitchk += numwaitchks;
335                         hdr->num_waitchks -= numwaitchks;
336                 } else if (priv->num_relocshifts) {
337                         int next_shift =
338                                 job->num_relocs - priv->num_relocshifts;
339                         int num =
340                                 (remaining / sizeof(struct nvhost_reloc_shift));
341                         if (!num)
342                                 break;
343                         num = min_t(int, num, priv->num_relocshifts);
344                         consumed = num * sizeof(struct nvhost_reloc_shift);
345                         if (copy_from_user(&job->relocshiftarray[next_shift],
346                                         buf, consumed)) {
347                                 err = -EFAULT;
348                                 break;
349                         }
350                         priv->num_relocshifts -= num;
351                 } else {
352                         err = -EFAULT;
353                         break;
354                 }
355                 remaining -= consumed;
356                 buf += consumed;
357         }
358
359         if (err < 0) {
360                 dev_err(&priv->ch->dev->dev, "channel write error\n");
361                 reset_submit(priv);
362                 return err;
363         }
364
365         return count - remaining;
366 }
367
368 static int nvhost_ioctl_channel_flush(
369         struct nvhost_channel_userctx *ctx,
370         struct nvhost_get_param_args *args,
371         int null_kickoff)
372 {
373         struct platform_device *ndev = to_platform_device(&ctx->ch->dev->dev);
374         int err;
375
376         trace_nvhost_ioctl_channel_flush(ctx->ch->dev->name);
377
378         if (!ctx->job ||
379             ctx->hdr.num_relocs ||
380             ctx->hdr.num_cmdbufs ||
381             ctx->hdr.num_waitchks) {
382                 reset_submit(ctx);
383                 dev_err(&ndev->dev, "channel submit out of sync\n");
384                 return -EFAULT;
385         }
386
387         err = nvhost_job_pin(ctx->job, &nvhost_get_host(ndev)->syncpt);
388         if (err) {
389                 dev_warn(&ndev->dev, "nvhost_job_pin failed: %d\n", err);
390                 goto fail;
391         }
392
393         if (nvhost_debug_null_kickoff_pid == current->tgid)
394                 null_kickoff = 1;
395         ctx->job->null_kickoff = null_kickoff;
396
397         if ((nvhost_debug_force_timeout_pid == current->tgid) &&
398             (nvhost_debug_force_timeout_channel == ctx->ch->chid)) {
399                 ctx->timeout = nvhost_debug_force_timeout_val;
400         }
401
402         /* context switch if needed, and submit user's gathers to the channel */
403         err = nvhost_channel_submit(ctx->job);
404         args->value = ctx->job->sp->fence;
405
406 fail:
407         if (err)
408                 nvhost_job_unpin(ctx->job);
409
410         nvhost_job_put(ctx->job);
411         ctx->job = NULL;
412
413         return err;
414 }
415
416 static int nvhost_ioctl_channel_submit(struct nvhost_channel_userctx *ctx,
417                 struct nvhost_submit_args *args)
418 {
419         struct nvhost_job *job;
420         int num_cmdbufs = args->num_cmdbufs;
421         int num_relocs = args->num_relocs;
422         int num_waitchks = args->num_waitchks;
423         struct nvhost_cmdbuf __user *cmdbufs = args->cmdbufs;
424         struct nvhost_reloc __user *relocs = args->relocs;
425         struct nvhost_reloc_shift __user *reloc_shifts = args->reloc_shifts;
426         struct nvhost_waitchk __user *waitchks = args->waitchks;
427         struct nvhost_syncpt_incr syncpt_incr;
428         struct nvhost_master *host = nvhost_get_host(ctx->ch->dev);
429         int err;
430
431         /* We don't yet support other than one nvhost_syncpt_incrs per submit */
432         if (args->num_syncpt_incrs != 1)
433                 return -EINVAL;
434
435         job = nvhost_job_alloc(ctx->ch,
436                         ctx->hwctx,
437                         args->num_cmdbufs,
438                         args->num_relocs,
439                         args->num_waitchks,
440                         1,
441                         ctx->memmgr);
442         if (!job)
443                 return -ENOMEM;
444
445         job->num_relocs = args->num_relocs;
446         job->num_waitchk = args->num_waitchks;
447         job->num_syncpts = args->num_syncpt_incrs;
448         job->priority = ctx->priority;
449         job->clientid = ctx->clientid;
450
451         while (num_cmdbufs) {
452                 struct nvhost_cmdbuf cmdbuf;
453                 err = copy_from_user(&cmdbuf, cmdbufs, sizeof(cmdbuf));
454                 if (err)
455                         goto fail;
456                 nvhost_job_add_gather(job,
457                                 cmdbuf.mem, cmdbuf.words, cmdbuf.offset);
458                 num_cmdbufs--;
459                 cmdbufs++;
460         }
461
462         err = copy_from_user(job->relocarray,
463                         relocs, sizeof(*relocs) * num_relocs);
464         if (err)
465                 goto fail;
466
467         err = copy_from_user(job->relocshiftarray,
468                         reloc_shifts, sizeof(*reloc_shifts) * num_relocs);
469         if (err)
470                 goto fail;
471
472         err = copy_from_user(job->waitchk,
473                         waitchks, sizeof(*waitchks) * num_waitchks);
474         if (err)
475                 goto fail;
476
477         err = copy_from_user(&syncpt_incr,
478                         args->syncpt_incrs, sizeof(syncpt_incr));
479         if (err)
480                 goto fail;
481         job->sp->id = syncpt_incr.syncpt_id;
482         if (job->sp->id > host->info.nb_pts) {
483                 err = -EINVAL;
484                 goto fail;
485         }
486         job->sp->incrs = syncpt_incr.syncpt_incrs;
487         job->hwctx_syncpt_idx = 0;
488         job->sp->waitbase = nvhost_syncpt_get_waitbase(job->ch, job->sp->id);
489
490         trace_nvhost_channel_submit(ctx->ch->dev->name,
491                 job->num_gathers, job->num_relocs, job->num_waitchk,
492                 job->sp->id, job->sp->incrs);
493
494         err = nvhost_job_pin(job, &nvhost_get_host(ctx->ch->dev)->syncpt);
495         if (err)
496                 goto fail;
497
498         if (args->timeout)
499                 job->timeout = min(ctx->timeout, args->timeout);
500         else
501                 job->timeout = ctx->timeout;
502
503         job->timeout_debug_dump = ctx->timeout_debug_dump;
504
505         err = nvhost_channel_submit(job);
506         if (err)
507                 goto fail_submit;
508
509         args->fence = job->sp->fence;
510
511         nvhost_job_put(job);
512
513         return 0;
514
515 fail_submit:
516         nvhost_job_unpin(job);
517 fail:
518         nvhost_job_put(job);
519         return err;
520 }
521
522 static int nvhost_ioctl_channel_set_ctxswitch(
523                 struct nvhost_channel_userctx *ctx,
524                 struct nvhost_set_ctxswitch_args *args)
525 {
526         struct nvhost_cmdbuf cmdbuf_save;
527         struct nvhost_cmdbuf cmdbuf_restore;
528         struct nvhost_syncpt_incr save_incr, restore_incr;
529         u32 save_waitbase, restore_waitbase;
530         struct nvhost_reloc reloc;
531         struct nvhost_hwctx_handler *ctxhandler = NULL;
532         struct nvhost_hwctx *nhwctx = NULL;
533         struct user_hwctx *hwctx;
534         struct nvhost_device_data *pdata = platform_get_drvdata(ctx->ch->dev);
535         int err;
536
537         /* Only channels with context support */
538         if (!ctx->hwctx)
539                 return -EFAULT;
540
541         /* We don't yet support other than one nvhost_syncpt_incrs per submit */
542         if (args->num_cmdbufs_save != 1
543                         || args->num_cmdbufs_restore != 1
544                         || args->num_save_incrs != 1
545                         || args->num_restore_incrs != 1
546                         || args->num_relocs != 1)
547                 return -EINVAL;
548
549         err = copy_from_user(&cmdbuf_save,
550                         args->cmdbuf_save, sizeof(cmdbuf_save));
551         if (err)
552                 goto fail;
553
554         err = copy_from_user(&cmdbuf_restore,
555                         args->cmdbuf_restore, sizeof(cmdbuf_restore));
556         if (err)
557                 goto fail;
558
559         err = copy_from_user(&reloc, args->relocs, sizeof(reloc));
560         if (err)
561                 goto fail;
562
563         err = copy_from_user(&save_incr,
564                         args->save_incrs, sizeof(save_incr));
565         if (err)
566                 goto fail;
567         err = copy_from_user(&save_waitbase,
568                         args->save_waitbases, sizeof(save_waitbase));
569
570         err = copy_from_user(&restore_incr,
571                         args->restore_incrs, sizeof(restore_incr));
572         if (err)
573                 goto fail;
574         err = copy_from_user(&restore_waitbase,
575                         args->restore_waitbases, sizeof(restore_waitbase));
576
577         if (save_incr.syncpt_id != pdata->syncpts[0]
578                         || restore_incr.syncpt_id != pdata->syncpts[0]
579                         || save_waitbase != pdata->waitbases[0]
580                         || restore_waitbase != pdata->waitbases[0]) {
581                 err = -EINVAL;
582                 goto fail;
583         }
584         ctxhandler = user_ctxhandler_init(save_incr.syncpt_id,
585                         save_waitbase, ctx->ch);
586         if (!ctxhandler) {
587                 err = -ENOMEM;
588                 goto fail;
589         }
590
591         nhwctx = ctxhandler->alloc(ctxhandler, ctx->ch);
592         if (!nhwctx) {
593                 err = -ENOMEM;
594                 goto fail_hwctx;
595         }
596         hwctx = to_user_hwctx(nhwctx);
597
598         trace_nvhost_ioctl_channel_set_ctxswitch(ctx->ch->dev->name, nhwctx,
599                         cmdbuf_save.mem, cmdbuf_save.offset, cmdbuf_save.words,
600                         cmdbuf_restore.mem, cmdbuf_restore.offset,
601                         cmdbuf_restore.words,
602                         pdata->syncpts[0], pdata->waitbases[0],
603                         save_incr.syncpt_incrs, restore_incr.syncpt_incrs);
604
605         nhwctx->memmgr = ctx->hwctx->memmgr;
606         err = user_hwctx_set_restore(hwctx, cmdbuf_restore.mem,
607                         cmdbuf_restore.offset, cmdbuf_restore.words);
608         if (err)
609                 goto fail_set_restore;
610
611         err = user_hwctx_set_save(hwctx, cmdbuf_save.mem,
612                         cmdbuf_save.offset, cmdbuf_save.words, &reloc);
613         if (err)
614                 goto fail_set_save;
615
616         hwctx->hwctx.save_incrs = save_incr.syncpt_incrs;
617         hwctx->hwctx.restore_incrs = restore_incr.syncpt_incrs;
618
619         /* Free old context */
620         ctx->hwctx->h->put(ctx->hwctx);
621         ctx->hwctx = nhwctx;
622
623         return 0;
624
625 fail_set_save:
626 fail_set_restore:
627         ctxhandler->put(&hwctx->hwctx);
628 fail_hwctx:
629         user_ctxhandler_free(ctxhandler);
630 fail:
631         return err;
632 }
633
634 static int nvhost_ioctl_channel_read_3d_reg(struct nvhost_channel_userctx *ctx,
635         struct nvhost_read_3d_reg_args *args)
636 {
637         return nvhost_channel_read_reg(ctx->ch, ctx->hwctx,
638                         args->offset, &args->value);
639 }
640
641 static int moduleid_to_index(struct platform_device *dev, u32 moduleid)
642 {
643         int i;
644         struct nvhost_device_data *pdata = platform_get_drvdata(dev);
645
646         for (i = 0; i < NVHOST_MODULE_MAX_CLOCKS; i++) {
647                 if (pdata->clocks[i].moduleid == moduleid)
648                         return i;
649         }
650
651         /* Old user space is sending a random number in args. Return clock
652          * zero in these cases. */
653         return 0;
654 }
655
656 static int nvhost_ioctl_channel_set_rate(struct nvhost_channel_userctx *ctx,
657         struct nvhost_clk_rate_args *arg)
658 {
659         u32 moduleid = (arg->moduleid >> NVHOST_MODULE_ID_BIT_POS)
660                         & ((1 << NVHOST_MODULE_ID_BIT_WIDTH) - 1);
661         u32 attr = (arg->moduleid >> NVHOST_CLOCK_ATTR_BIT_POS)
662                         & ((1 << NVHOST_CLOCK_ATTR_BIT_WIDTH) - 1);
663         int index = moduleid ?
664                         moduleid_to_index(ctx->ch->dev, moduleid) : 0;
665
666         return nvhost_module_set_rate(ctx->ch->dev,
667                         ctx, arg->rate, index, attr);
668 }
669
670 static int nvhost_ioctl_channel_get_rate(struct nvhost_channel_userctx *ctx,
671         u32 moduleid, u32 *rate)
672 {
673         int index = moduleid ? moduleid_to_index(ctx->ch->dev, moduleid) : 0;
674
675         return nvhost_module_get_rate(ctx->ch->dev,
676                         (unsigned long *)rate, index);
677 }
678
679 static int nvhost_ioctl_channel_module_regrdwr(
680         struct nvhost_channel_userctx *ctx,
681         struct nvhost_ctrl_module_regrdwr_args *args)
682 {
683         u32 num_offsets = args->num_offsets;
684         u32 *offsets = args->offsets;
685         u32 *values = args->values;
686         u32 vals[64];
687         struct platform_device *ndev;
688
689         trace_nvhost_ioctl_channel_module_regrdwr(args->id,
690                 args->num_offsets, args->write);
691
692         /* Check that there is something to read and that block size is
693          * u32 aligned */
694         if (num_offsets == 0 || args->block_size & 3)
695                 return -EINVAL;
696
697         ndev = ctx->ch->dev;
698
699         while (num_offsets--) {
700                 int err;
701                 u32 offs;
702                 int remaining = args->block_size >> 2;
703
704                 if (get_user(offs, offsets))
705                         return -EFAULT;
706
707                 offsets++;
708                 while (remaining) {
709                         int batch = min(remaining, 64);
710                         if (args->write) {
711                                 if (copy_from_user(vals, values,
712                                                 batch * sizeof(u32)))
713                                         return -EFAULT;
714
715                                 err = nvhost_write_module_regs(ndev,
716                                         offs, batch, vals);
717                                 if (err)
718                                         return err;
719                         } else {
720                                 err = nvhost_read_module_regs(ndev,
721                                                 offs, batch, vals);
722                                 if (err)
723                                         return err;
724
725                                 if (copy_to_user(values, vals,
726                                                 batch * sizeof(u32)))
727                                         return -EFAULT;
728                         }
729
730                         remaining -= batch;
731                         offs += batch * sizeof(u32);
732                         values += batch;
733                 }
734         }
735
736         return 0;
737 }
738
739 static u32 create_mask(u32 *words, int num)
740 {
741         int i;
742         u32 word = 0;
743         for (i = 0; i < num && words[i] && words[i] < BITS_PER_LONG; i++)
744                 word |= BIT(words[i]);
745
746         return word;
747 }
748
749 static long nvhost_channelctl(struct file *filp,
750         unsigned int cmd, unsigned long arg)
751 {
752         struct nvhost_channel_userctx *priv = filp->private_data;
753         u8 buf[NVHOST_IOCTL_CHANNEL_MAX_ARG_SIZE];
754         int err = 0;
755
756         if ((_IOC_TYPE(cmd) != NVHOST_IOCTL_MAGIC) ||
757                 (_IOC_NR(cmd) == 0) ||
758                 (_IOC_NR(cmd) > NVHOST_IOCTL_CHANNEL_LAST) ||
759                 (_IOC_SIZE(cmd) > NVHOST_IOCTL_CHANNEL_MAX_ARG_SIZE))
760                 return -EFAULT;
761
762         if (_IOC_DIR(cmd) & _IOC_WRITE) {
763                 if (copy_from_user(buf, (void __user *)arg, _IOC_SIZE(cmd)))
764                         return -EFAULT;
765         }
766
767         switch (cmd) {
768         case NVHOST_IOCTL_CHANNEL_FLUSH:
769                 err = nvhost_ioctl_channel_flush(priv, (void *)buf, 0);
770                 break;
771         case NVHOST_IOCTL_CHANNEL_NULL_KICKOFF:
772                 err = nvhost_ioctl_channel_flush(priv, (void *)buf, 1);
773                 break;
774         case NVHOST_IOCTL_CHANNEL_SUBMIT_EXT:
775         {
776                 struct nvhost_submit_hdr_ext *hdr;
777
778                 if (priv->hdr.num_relocs ||
779                     priv->num_relocshifts ||
780                     priv->hdr.num_cmdbufs ||
781                     priv->hdr.num_waitchks) {
782                         reset_submit(priv);
783                         dev_err(&priv->ch->dev->dev,
784                                 "channel submit out of sync\n");
785                         err = -EIO;
786                         break;
787                 }
788
789                 hdr = (struct nvhost_submit_hdr_ext *)buf;
790                 if (hdr->submit_version > NVHOST_SUBMIT_VERSION_MAX_SUPPORTED) {
791                         dev_err(&priv->ch->dev->dev,
792                                 "submit version %d > max supported %d\n",
793                                 hdr->submit_version,
794                                 NVHOST_SUBMIT_VERSION_MAX_SUPPORTED);
795                         err = -EINVAL;
796                         break;
797                 }
798                 memcpy(&priv->hdr, hdr, sizeof(struct nvhost_submit_hdr_ext));
799                 err = set_submit(priv);
800                 trace_nvhost_ioctl_channel_submit(priv->ch->dev->name,
801                         priv->hdr.submit_version,
802                         priv->hdr.num_cmdbufs, priv->hdr.num_relocs,
803                         priv->hdr.num_waitchks,
804                         priv->hdr.syncpt_id, priv->hdr.syncpt_incrs);
805                 break;
806         }
807         case NVHOST_IOCTL_CHANNEL_GET_SYNCPOINTS:
808         {
809                 struct nvhost_device_data *pdata = \
810                         platform_get_drvdata(priv->ch->dev);
811                 ((struct nvhost_get_param_args *)buf)->value =
812                         create_mask(pdata->syncpts, NVHOST_MODULE_MAX_SYNCPTS);
813                 break;
814         }
815         case NVHOST_IOCTL_CHANNEL_GET_SYNCPOINT:
816         {
817                 struct nvhost_device_data *pdata = \
818                         platform_get_drvdata(priv->ch->dev);
819                 struct nvhost_get_param_arg *arg =
820                         (struct nvhost_get_param_arg *)buf;
821                 if (arg->param >= NVHOST_MODULE_MAX_SYNCPTS
822                                 || !pdata->syncpts[arg->param])
823                         return -EINVAL;
824                 arg->value = pdata->syncpts[arg->param];
825                 break;
826         }
827         case NVHOST_IOCTL_CHANNEL_GET_WAITBASES:
828         {
829                 struct nvhost_device_data *pdata = \
830                         platform_get_drvdata(priv->ch->dev);
831                 ((struct nvhost_get_param_args *)buf)->value =
832                         create_mask(pdata->waitbases,
833                                         NVHOST_MODULE_MAX_WAITBASES);
834                 break;
835         }
836         case NVHOST_IOCTL_CHANNEL_GET_WAITBASE:
837         {
838                 struct nvhost_device_data *pdata = \
839                         platform_get_drvdata(priv->ch->dev);
840                 struct nvhost_get_param_arg *arg =
841                         (struct nvhost_get_param_arg *)buf;
842                 if (arg->param >= NVHOST_MODULE_MAX_WAITBASES
843                                 || !pdata->waitbases[arg->param])
844                         return -EINVAL;
845                 arg->value = pdata->waitbases[arg->param];
846                 break;
847         }
848         case NVHOST_IOCTL_CHANNEL_GET_MODMUTEXES:
849         {
850                 struct nvhost_device_data *pdata = \
851                         platform_get_drvdata(priv->ch->dev);
852                 ((struct nvhost_get_param_args *)buf)->value =
853                         create_mask(pdata->modulemutexes,
854                                         NVHOST_MODULE_MAX_MODMUTEXES);
855                 break;
856         }
857         case NVHOST_IOCTL_CHANNEL_GET_MODMUTEX:
858         {
859                 struct nvhost_device_data *pdata = \
860                         platform_get_drvdata(priv->ch->dev);
861                 struct nvhost_get_param_arg *arg =
862                         (struct nvhost_get_param_arg *)buf;
863                 if (arg->param >= NVHOST_MODULE_MAX_MODMUTEXES
864                                 || !pdata->modulemutexes[arg->param])
865                         return -EINVAL;
866                 arg->value = pdata->modulemutexes[arg->param];
867                 break;
868         }
869         case NVHOST_IOCTL_CHANNEL_SET_NVMAP_FD:
870         {
871                 int fd = (int)((struct nvhost_set_nvmap_fd_args *)buf)->fd;
872                 struct mem_mgr *new_client = nvhost_memmgr_get_mgr_file(fd);
873
874                 if (IS_ERR(new_client)) {
875                         err = PTR_ERR(new_client);
876                         break;
877                 }
878
879                 if (priv->memmgr)
880                         nvhost_memmgr_put_mgr(priv->memmgr);
881
882                 priv->memmgr = new_client;
883
884                 if (priv->hwctx)
885                         priv->hwctx->memmgr = new_client;
886
887                 break;
888         }
889         case NVHOST_IOCTL_CHANNEL_READ_3D_REG:
890                 err = nvhost_ioctl_channel_read_3d_reg(priv, (void *)buf);
891                 break;
892         case NVHOST_IOCTL_CHANNEL_GET_CLK_RATE:
893         {
894                 struct nvhost_clk_rate_args *arg =
895                                 (struct nvhost_clk_rate_args *)buf;
896
897                 err = nvhost_ioctl_channel_get_rate(priv,
898                                 arg->moduleid, &arg->rate);
899                 break;
900         }
901         case NVHOST_IOCTL_CHANNEL_SET_CLK_RATE:
902         {
903                 struct nvhost_clk_rate_args *arg =
904                                 (struct nvhost_clk_rate_args *)buf;
905
906                 err = nvhost_ioctl_channel_set_rate(priv, arg);
907                 break;
908         }
909         case NVHOST_IOCTL_CHANNEL_SET_TIMEOUT:
910                 priv->timeout =
911                         (u32)((struct nvhost_set_timeout_args *)buf)->timeout;
912                 dev_dbg(&priv->ch->dev->dev,
913                         "%s: setting buffer timeout (%d ms) for userctx 0x%p\n",
914                         __func__, priv->timeout, priv);
915                 break;
916         case NVHOST_IOCTL_CHANNEL_GET_TIMEDOUT:
917                 ((struct nvhost_get_param_args *)buf)->value =
918                                 priv->hwctx->has_timedout;
919                 break;
920         case NVHOST_IOCTL_CHANNEL_SET_PRIORITY:
921                 priv->priority =
922                         (u32)((struct nvhost_set_priority_args *)buf)->priority;
923                 break;
924         case NVHOST_IOCTL_CHANNEL_MODULE_REGRDWR:
925                 err = nvhost_ioctl_channel_module_regrdwr(priv, (void *)buf);
926                 break;
927         case NVHOST_IOCTL_CHANNEL_SUBMIT:
928                 err = nvhost_ioctl_channel_submit(priv, (void *)buf);
929                 break;
930         case NVHOST_IOCTL_CHANNEL_SET_TIMEOUT_EX:
931                 priv->timeout = (u32)
932                         ((struct nvhost_set_timeout_ex_args *)buf)->timeout;
933                 priv->timeout_debug_dump = !((u32)
934                         ((struct nvhost_set_timeout_ex_args *)buf)->flags &
935                         (1 << NVHOST_TIMEOUT_FLAG_DISABLE_DUMP));
936                 dev_dbg(&priv->ch->dev->dev,
937                         "%s: setting buffer timeout (%d ms) for userctx 0x%p\n",
938                         __func__, priv->timeout, priv);
939                 break;
940         case NVHOST_IOCTL_CHANNEL_SET_CTXSWITCH:
941                 err = nvhost_ioctl_channel_set_ctxswitch(priv, (void *)buf);
942                 break;
943         default:
944                 err = -ENOTTY;
945                 break;
946         }
947
948         if ((err == 0) && (_IOC_DIR(cmd) & _IOC_READ))
949                 err = copy_to_user((void __user *)arg, buf, _IOC_SIZE(cmd));
950
951         return err;
952 }
953
954 static const struct file_operations nvhost_channelops = {
955         .owner = THIS_MODULE,
956         .release = nvhost_channelrelease,
957         .open = nvhost_channelopen,
958         .write = nvhost_channelwrite,
959         .unlocked_ioctl = nvhost_channelctl
960 };
961
962 int nvhost_client_user_init(struct platform_device *dev)
963 {
964         int err, devno;
965         struct nvhost_device_data *pdata = platform_get_drvdata(dev);
966
967         struct nvhost_channel *ch = pdata->channel;
968         err = alloc_chrdev_region(&devno, 0, 1, IFACE_NAME);
969         if (err < 0) {
970                 dev_err(&dev->dev, "failed to allocate devno\n");
971                 goto fail;
972         }
973
974         cdev_init(&ch->cdev, &nvhost_channelops);
975         ch->cdev.owner = THIS_MODULE;
976
977         err = cdev_add(&ch->cdev, devno, 1);
978         if (err < 0) {
979                 dev_err(&dev->dev,
980                         "failed to add chan %i cdev\n", pdata->index);
981                 goto fail;
982         }
983         ch->node = device_create(nvhost_get_host(dev)->nvhost_class,
984                         NULL, devno, NULL,
985                         IFACE_NAME "-%s", dev_name(&dev->dev));
986         if (IS_ERR(ch->node)) {
987                 err = PTR_ERR(ch->node);
988                 dev_err(&dev->dev,
989                         "failed to create %s channel device\n",
990                         dev_name(&dev->dev));
991                 goto fail;
992         }
993
994         return 0;
995 fail:
996         return err;
997 }
998
999 int nvhost_client_device_init(struct platform_device *dev)
1000 {
1001         int err;
1002         struct nvhost_master *nvhost_master = nvhost_get_host(dev);
1003         struct nvhost_channel *ch;
1004         struct nvhost_device_data *pdata = platform_get_drvdata(dev);
1005
1006         ch = nvhost_alloc_channel(dev);
1007         if (ch == NULL)
1008                 return -ENODEV;
1009
1010         /* store the pointer to this device for channel */
1011         ch->dev = dev;
1012
1013         err = nvhost_channel_init(ch, nvhost_master, pdata->index);
1014         if (err)
1015                 goto fail;
1016
1017         err = nvhost_client_user_init(dev);
1018         if (err)
1019                 goto fail;
1020
1021         err = nvhost_module_init(dev);
1022         if (err)
1023                 goto fail;
1024
1025         if (tickctrl_op().init_channel)
1026                 tickctrl_op().init_channel(dev);
1027
1028         err = nvhost_device_list_add(dev);
1029         if (err)
1030                 goto fail;
1031
1032         if (pdata->scaling_init)
1033                 pdata->scaling_init(dev);
1034
1035         nvhost_device_debug_init(dev);
1036
1037         /* reset syncpoint values for this unit */
1038         nvhost_module_busy(nvhost_master->dev);
1039         nvhost_syncpt_reset_client(dev);
1040         nvhost_module_idle(nvhost_master->dev);
1041
1042         dev_info(&dev->dev, "initialized\n");
1043
1044         return 0;
1045
1046 fail:
1047         /* Add clean-up */
1048         nvhost_free_channel(ch);
1049         return err;
1050 }
1051
1052 int nvhost_client_device_suspend(struct platform_device *dev)
1053 {
1054         int ret = 0;
1055         struct nvhost_device_data *pdata = platform_get_drvdata(dev);
1056
1057         ret = nvhost_channel_suspend(pdata->channel);
1058         if (ret)
1059                 return ret;
1060
1061         dev_info(&dev->dev, "suspend status: %d\n", ret);
1062
1063         return ret;
1064 }
1065
1066 int nvhost_client_device_get_resources(struct platform_device *dev)
1067 {
1068         int i;
1069         void __iomem *regs = NULL;
1070         struct nvhost_device_data *pdata = platform_get_drvdata(dev);
1071
1072         for (i = 0; i < dev->num_resources; i++) {
1073                 struct resource *r = NULL;
1074
1075                 r = platform_get_resource(dev, IORESOURCE_MEM, i);
1076                 /* We've run out of mem resources */
1077                 if (!r)
1078                         break;
1079
1080                 regs = devm_request_and_ioremap(&dev->dev, r);
1081                 if (!regs)
1082                         goto fail;
1083
1084                 pdata->aperture[i] = regs;
1085         }
1086
1087         return 0;
1088
1089 fail:
1090         dev_err(&dev->dev, "failed to get register memory\n");
1091
1092         return -ENXIO;
1093 }
1094
1095 /* This is a simple wrapper around request_firmware that takes
1096  * 'fw_name' and if available applies a SOC relative path prefix to it.
1097  * The caller is responsible for calling release_firmware later.
1098  */
1099 const struct firmware *
1100 nvhost_client_request_firmware(struct platform_device *dev, const char *fw_name)
1101 {
1102         struct nvhost_chip_support *op = nvhost_get_chip_ops();
1103         const struct firmware *fw;
1104         char *fw_path = NULL;
1105         int path_len, err;
1106
1107         if (!fw_name)
1108                 return NULL;
1109
1110         if (op->soc_name) {
1111                 path_len = strlen(fw_name) + strlen(op->soc_name);
1112                 path_len += 2; /* for the path separator and zero terminator*/
1113
1114                 fw_path = kzalloc(sizeof(*fw_path) * path_len,
1115                                      GFP_KERNEL);
1116                 if (!fw_path)
1117                         return NULL;
1118
1119                 sprintf(fw_path, "%s/%s", op->soc_name, fw_name);
1120                 fw_name = fw_path;
1121         }
1122
1123         err = request_firmware(&fw, fw_name, &dev->dev);
1124         kfree(fw_path);
1125         if (err) {
1126                 dev_err(&dev->dev, "failed to get firmware\n");
1127                 return NULL;
1128         }
1129
1130         /* note: caller must release_firmware */
1131         return fw;
1132 }