0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <asm/cacheflush.h>
0010 #include <linux/device.h>
0011 #include <linux/dma-mapping.h>
0012 #include <linux/host1x.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/kfifo.h>
0016 #include <linux/slab.h>
0017 #include <trace/events/host1x.h>
0018
0019 #include "cdma.h"
0020 #include "channel.h"
0021 #include "dev.h"
0022 #include "debug.h"
0023 #include "job.h"
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 #define HOST1X_PUSHBUFFER_SLOTS 511
0044
0045
0046
0047
0048 static void host1x_pushbuffer_destroy(struct push_buffer *pb)
0049 {
0050 struct host1x_cdma *cdma = pb_to_cdma(pb);
0051 struct host1x *host1x = cdma_to_host1x(cdma);
0052
0053 if (!pb->mapped)
0054 return;
0055
0056 if (host1x->domain) {
0057 iommu_unmap(host1x->domain, pb->dma, pb->alloc_size);
0058 free_iova(&host1x->iova, iova_pfn(&host1x->iova, pb->dma));
0059 }
0060
0061 dma_free_wc(host1x->dev, pb->alloc_size, pb->mapped, pb->phys);
0062
0063 pb->mapped = NULL;
0064 pb->phys = 0;
0065 }
0066
0067
0068
0069
0070 static int host1x_pushbuffer_init(struct push_buffer *pb)
0071 {
0072 struct host1x_cdma *cdma = pb_to_cdma(pb);
0073 struct host1x *host1x = cdma_to_host1x(cdma);
0074 struct iova *alloc;
0075 u32 size;
0076 int err;
0077
0078 pb->mapped = NULL;
0079 pb->phys = 0;
0080 pb->size = HOST1X_PUSHBUFFER_SLOTS * 8;
0081
0082 size = pb->size + 4;
0083
0084
0085 pb->fence = pb->size - 8;
0086 pb->pos = 0;
0087
0088 if (host1x->domain) {
0089 unsigned long shift;
0090
0091 size = iova_align(&host1x->iova, size);
0092
0093 pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
0094 GFP_KERNEL);
0095 if (!pb->mapped)
0096 return -ENOMEM;
0097
0098 shift = iova_shift(&host1x->iova);
0099 alloc = alloc_iova(&host1x->iova, size >> shift,
0100 host1x->iova_end >> shift, true);
0101 if (!alloc) {
0102 err = -ENOMEM;
0103 goto iommu_free_mem;
0104 }
0105
0106 pb->dma = iova_dma_addr(&host1x->iova, alloc);
0107 err = iommu_map(host1x->domain, pb->dma, pb->phys, size,
0108 IOMMU_READ);
0109 if (err)
0110 goto iommu_free_iova;
0111 } else {
0112 pb->mapped = dma_alloc_wc(host1x->dev, size, &pb->phys,
0113 GFP_KERNEL);
0114 if (!pb->mapped)
0115 return -ENOMEM;
0116
0117 pb->dma = pb->phys;
0118 }
0119
0120 pb->alloc_size = size;
0121
0122 host1x_hw_pushbuffer_init(host1x, pb);
0123
0124 return 0;
0125
0126 iommu_free_iova:
0127 __free_iova(&host1x->iova, alloc);
0128 iommu_free_mem:
0129 dma_free_wc(host1x->dev, size, pb->mapped, pb->phys);
0130
0131 return err;
0132 }
0133
0134
0135
0136
0137
0138 static void host1x_pushbuffer_push(struct push_buffer *pb, u32 op1, u32 op2)
0139 {
0140 u32 *p = (u32 *)((void *)pb->mapped + pb->pos);
0141
0142 WARN_ON(pb->pos == pb->fence);
0143 *(p++) = op1;
0144 *(p++) = op2;
0145 pb->pos += 8;
0146
0147 if (pb->pos >= pb->size)
0148 pb->pos -= pb->size;
0149 }
0150
0151
0152
0153
0154
0155 static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
0156 {
0157
0158 pb->fence += slots * 8;
0159
0160 if (pb->fence >= pb->size)
0161 pb->fence -= pb->size;
0162 }
0163
0164
0165
0166
0167 static u32 host1x_pushbuffer_space(struct push_buffer *pb)
0168 {
0169 unsigned int fence = pb->fence;
0170
0171 if (pb->fence < pb->pos)
0172 fence += pb->size;
0173
0174 return (fence - pb->pos) / 8;
0175 }
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 unsigned int host1x_cdma_wait_locked(struct host1x_cdma *cdma,
0186 enum cdma_event event)
0187 {
0188 for (;;) {
0189 struct push_buffer *pb = &cdma->push_buffer;
0190 unsigned int space;
0191
0192 switch (event) {
0193 case CDMA_EVENT_SYNC_QUEUE_EMPTY:
0194 space = list_empty(&cdma->sync_queue) ? 1 : 0;
0195 break;
0196
0197 case CDMA_EVENT_PUSH_BUFFER_SPACE:
0198 space = host1x_pushbuffer_space(pb);
0199 break;
0200
0201 default:
0202 WARN_ON(1);
0203 return -EINVAL;
0204 }
0205
0206 if (space)
0207 return space;
0208
0209 trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
0210 event);
0211
0212
0213 if (cdma->event != CDMA_EVENT_NONE) {
0214 mutex_unlock(&cdma->lock);
0215 schedule();
0216 mutex_lock(&cdma->lock);
0217 continue;
0218 }
0219
0220 cdma->event = event;
0221
0222 mutex_unlock(&cdma->lock);
0223 wait_for_completion(&cdma->complete);
0224 mutex_lock(&cdma->lock);
0225 }
0226
0227 return 0;
0228 }
0229
0230
0231
0232
0233
0234
0235 static int host1x_cdma_wait_pushbuffer_space(struct host1x *host1x,
0236 struct host1x_cdma *cdma,
0237 unsigned int needed)
0238 {
0239 while (true) {
0240 struct push_buffer *pb = &cdma->push_buffer;
0241 unsigned int space;
0242
0243 space = host1x_pushbuffer_space(pb);
0244 if (space >= needed)
0245 break;
0246
0247 trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
0248 CDMA_EVENT_PUSH_BUFFER_SPACE);
0249
0250 host1x_hw_cdma_flush(host1x, cdma);
0251
0252
0253 if (cdma->event != CDMA_EVENT_NONE) {
0254 mutex_unlock(&cdma->lock);
0255 schedule();
0256 mutex_lock(&cdma->lock);
0257 continue;
0258 }
0259
0260 cdma->event = CDMA_EVENT_PUSH_BUFFER_SPACE;
0261
0262 mutex_unlock(&cdma->lock);
0263 wait_for_completion(&cdma->complete);
0264 mutex_lock(&cdma->lock);
0265 }
0266
0267 return 0;
0268 }
0269
0270
0271
0272
0273 static void cdma_start_timer_locked(struct host1x_cdma *cdma,
0274 struct host1x_job *job)
0275 {
0276 if (cdma->timeout.client) {
0277
0278 return;
0279 }
0280
0281 cdma->timeout.client = job->client;
0282 cdma->timeout.syncpt = job->syncpt;
0283 cdma->timeout.syncpt_val = job->syncpt_end;
0284 cdma->timeout.start_ktime = ktime_get();
0285
0286 schedule_delayed_work(&cdma->timeout.wq,
0287 msecs_to_jiffies(job->timeout));
0288 }
0289
0290
0291
0292
0293
0294 static void stop_cdma_timer_locked(struct host1x_cdma *cdma)
0295 {
0296 cancel_delayed_work(&cdma->timeout.wq);
0297 cdma->timeout.client = NULL;
0298 }
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310 static void update_cdma_locked(struct host1x_cdma *cdma)
0311 {
0312 bool signal = false;
0313 struct host1x_job *job, *n;
0314
0315
0316
0317
0318
0319 list_for_each_entry_safe(job, n, &cdma->sync_queue, list) {
0320 struct host1x_syncpt *sp = job->syncpt;
0321
0322
0323 if (!host1x_syncpt_is_expired(sp, job->syncpt_end) &&
0324 !job->cancelled) {
0325
0326 if (job->timeout)
0327 cdma_start_timer_locked(cdma, job);
0328
0329 break;
0330 }
0331
0332
0333 if (cdma->timeout.client)
0334 stop_cdma_timer_locked(cdma);
0335
0336
0337 host1x_job_unpin(job);
0338
0339
0340 if (job->num_slots) {
0341 struct push_buffer *pb = &cdma->push_buffer;
0342
0343 host1x_pushbuffer_pop(pb, job->num_slots);
0344
0345 if (cdma->event == CDMA_EVENT_PUSH_BUFFER_SPACE)
0346 signal = true;
0347 }
0348
0349 list_del(&job->list);
0350 host1x_job_put(job);
0351 }
0352
0353 if (cdma->event == CDMA_EVENT_SYNC_QUEUE_EMPTY &&
0354 list_empty(&cdma->sync_queue))
0355 signal = true;
0356
0357 if (signal) {
0358 cdma->event = CDMA_EVENT_NONE;
0359 complete(&cdma->complete);
0360 }
0361 }
0362
0363 void host1x_cdma_update_sync_queue(struct host1x_cdma *cdma,
0364 struct device *dev)
0365 {
0366 struct host1x *host1x = cdma_to_host1x(cdma);
0367 u32 restart_addr, syncpt_incrs, syncpt_val;
0368 struct host1x_job *job, *next_job = NULL;
0369
0370 syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
0371
0372 dev_dbg(dev, "%s: starting cleanup (thresh %d)\n",
0373 __func__, syncpt_val);
0374
0375
0376
0377
0378
0379
0380
0381
0382 dev_dbg(dev, "%s: skip completed buffers still in sync_queue\n",
0383 __func__);
0384
0385 list_for_each_entry(job, &cdma->sync_queue, list) {
0386 if (syncpt_val < job->syncpt_end) {
0387
0388 if (!list_is_last(&job->list, &cdma->sync_queue))
0389 next_job = list_next_entry(job, list);
0390
0391 goto syncpt_incr;
0392 }
0393
0394 host1x_job_dump(dev, job);
0395 }
0396
0397
0398 job = NULL;
0399
0400 syncpt_incr:
0401
0402
0403
0404
0405
0406
0407
0408 if (next_job)
0409 restart_addr = next_job->first_get;
0410 else
0411 restart_addr = cdma->last_pos;
0412
0413 if (!job)
0414 goto resume;
0415
0416
0417 if (job->syncpt_recovery) {
0418 dev_dbg(dev, "%s: perform CPU incr on pending buffers\n",
0419 __func__);
0420
0421
0422 job->timeout = 0;
0423
0424 syncpt_incrs = job->syncpt_end - syncpt_val;
0425 dev_dbg(dev, "%s: CPU incr (%d)\n", __func__, syncpt_incrs);
0426
0427 host1x_job_dump(dev, job);
0428
0429
0430 host1x_hw_cdma_timeout_cpu_incr(host1x, cdma, job->first_get,
0431 syncpt_incrs, job->syncpt_end,
0432 job->num_slots);
0433
0434 dev_dbg(dev, "%s: finished sync_queue modification\n",
0435 __func__);
0436 } else {
0437 struct host1x_job *failed_job = job;
0438
0439 host1x_job_dump(dev, job);
0440
0441 host1x_syncpt_set_locked(job->syncpt);
0442 failed_job->cancelled = true;
0443
0444 list_for_each_entry_continue(job, &cdma->sync_queue, list) {
0445 unsigned int i;
0446
0447 if (job->syncpt != failed_job->syncpt)
0448 continue;
0449
0450 for (i = 0; i < job->num_slots; i++) {
0451 unsigned int slot = (job->first_get/8 + i) %
0452 HOST1X_PUSHBUFFER_SLOTS;
0453 u32 *mapped = cdma->push_buffer.mapped;
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469 if (i == 0 && host1x->info->has_wide_gather) {
0470 unsigned int next_job = (job->first_get/8 + job->num_slots)
0471 % HOST1X_PUSHBUFFER_SLOTS;
0472 mapped[2*slot+0] = (0xd << 28) | (next_job * 2);
0473 mapped[2*slot+1] = 0x0;
0474 } else {
0475 mapped[2*slot+0] = 0x1bad0000;
0476 mapped[2*slot+1] = 0x1bad0000;
0477 }
0478 }
0479
0480 job->cancelled = true;
0481 }
0482
0483 wmb();
0484
0485 update_cdma_locked(cdma);
0486 }
0487
0488 resume:
0489
0490 host1x_hw_cdma_resume(host1x, cdma, restart_addr);
0491 }
0492
0493
0494
0495
0496 int host1x_cdma_init(struct host1x_cdma *cdma)
0497 {
0498 int err;
0499
0500 mutex_init(&cdma->lock);
0501 init_completion(&cdma->complete);
0502
0503 INIT_LIST_HEAD(&cdma->sync_queue);
0504
0505 cdma->event = CDMA_EVENT_NONE;
0506 cdma->running = false;
0507 cdma->torndown = false;
0508
0509 err = host1x_pushbuffer_init(&cdma->push_buffer);
0510 if (err)
0511 return err;
0512
0513 return 0;
0514 }
0515
0516
0517
0518
0519 int host1x_cdma_deinit(struct host1x_cdma *cdma)
0520 {
0521 struct push_buffer *pb = &cdma->push_buffer;
0522 struct host1x *host1x = cdma_to_host1x(cdma);
0523
0524 if (cdma->running) {
0525 pr_warn("%s: CDMA still running\n", __func__);
0526 return -EBUSY;
0527 }
0528
0529 host1x_pushbuffer_destroy(pb);
0530 host1x_hw_cdma_timeout_destroy(host1x, cdma);
0531
0532 return 0;
0533 }
0534
0535
0536
0537
0538 int host1x_cdma_begin(struct host1x_cdma *cdma, struct host1x_job *job)
0539 {
0540 struct host1x *host1x = cdma_to_host1x(cdma);
0541
0542 mutex_lock(&cdma->lock);
0543
0544
0545
0546
0547
0548
0549 if (job->syncpt->locked) {
0550 mutex_unlock(&cdma->lock);
0551 return -EPERM;
0552 }
0553
0554 if (job->timeout) {
0555
0556 if (!cdma->timeout.initialized) {
0557 int err;
0558
0559 err = host1x_hw_cdma_timeout_init(host1x, cdma);
0560 if (err) {
0561 mutex_unlock(&cdma->lock);
0562 return err;
0563 }
0564 }
0565 }
0566
0567 if (!cdma->running)
0568 host1x_hw_cdma_start(host1x, cdma);
0569
0570 cdma->slots_free = 0;
0571 cdma->slots_used = 0;
0572 cdma->first_get = cdma->push_buffer.pos;
0573
0574 trace_host1x_cdma_begin(dev_name(job->channel->dev));
0575 return 0;
0576 }
0577
0578
0579
0580
0581
0582 void host1x_cdma_push(struct host1x_cdma *cdma, u32 op1, u32 op2)
0583 {
0584 struct host1x *host1x = cdma_to_host1x(cdma);
0585 struct push_buffer *pb = &cdma->push_buffer;
0586 u32 slots_free = cdma->slots_free;
0587
0588 if (host1x_debug_trace_cmdbuf)
0589 trace_host1x_cdma_push(dev_name(cdma_to_channel(cdma)->dev),
0590 op1, op2);
0591
0592 if (slots_free == 0) {
0593 host1x_hw_cdma_flush(host1x, cdma);
0594 slots_free = host1x_cdma_wait_locked(cdma,
0595 CDMA_EVENT_PUSH_BUFFER_SPACE);
0596 }
0597
0598 cdma->slots_free = slots_free - 1;
0599 cdma->slots_used++;
0600 host1x_pushbuffer_push(pb, op1, op2);
0601 }
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612 void host1x_cdma_push_wide(struct host1x_cdma *cdma, u32 op1, u32 op2,
0613 u32 op3, u32 op4)
0614 {
0615 struct host1x_channel *channel = cdma_to_channel(cdma);
0616 struct host1x *host1x = cdma_to_host1x(cdma);
0617 struct push_buffer *pb = &cdma->push_buffer;
0618 unsigned int space = cdma->slots_free;
0619 unsigned int needed = 2, extra = 0;
0620
0621 if (host1x_debug_trace_cmdbuf)
0622 trace_host1x_cdma_push_wide(dev_name(channel->dev), op1, op2,
0623 op3, op4);
0624
0625
0626 if (pb->pos + 16 > pb->size) {
0627 extra = (pb->size - pb->pos) / 8;
0628 needed += extra;
0629 }
0630
0631 host1x_cdma_wait_pushbuffer_space(host1x, cdma, needed);
0632 space = host1x_pushbuffer_space(pb);
0633
0634 cdma->slots_free = space - needed;
0635 cdma->slots_used += needed;
0636
0637 if (extra > 0) {
0638
0639
0640
0641
0642
0643 host1x_pushbuffer_push(pb, (0x5 << 28), 0xdead0000);
0644 }
0645
0646 host1x_pushbuffer_push(pb, op1, op2);
0647 host1x_pushbuffer_push(pb, op3, op4);
0648 }
0649
0650
0651
0652
0653
0654
0655
0656 void host1x_cdma_end(struct host1x_cdma *cdma,
0657 struct host1x_job *job)
0658 {
0659 struct host1x *host1x = cdma_to_host1x(cdma);
0660 bool idle = list_empty(&cdma->sync_queue);
0661
0662 host1x_hw_cdma_flush(host1x, cdma);
0663
0664 job->first_get = cdma->first_get;
0665 job->num_slots = cdma->slots_used;
0666 host1x_job_get(job);
0667 list_add_tail(&job->list, &cdma->sync_queue);
0668
0669
0670 if (job->timeout && idle)
0671 cdma_start_timer_locked(cdma, job);
0672
0673 trace_host1x_cdma_end(dev_name(job->channel->dev));
0674 mutex_unlock(&cdma->lock);
0675 }
0676
0677
0678
0679
0680 void host1x_cdma_update(struct host1x_cdma *cdma)
0681 {
0682 mutex_lock(&cdma->lock);
0683 update_cdma_locked(cdma);
0684 mutex_unlock(&cdma->lock);
0685 }