blob: 8fd8b8813204c929bff0a0c16302c12113d531b8 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/wait.h>
15#include <linux/delay.h>
16#include <linux/sched.h>
17#include <linux/jiffies.h>
18#include <linux/err.h>
19
20#include "kgsl.h"
21#include "kgsl_sharedmem.h"
22#include "adreno.h"
23#include "adreno_ringbuffer.h"
24#include "adreno_trace.h"
25#include "kgsl_sharedmem.h"
26
27#define DRAWQUEUE_NEXT(_i, _s) (((_i) + 1) % (_s))
28
29/* Time in ms after which the dispatcher tries to schedule an unscheduled RB */
30unsigned int adreno_dispatch_starvation_time = 2000;
31
32/* Amount of time in ms that a starved RB is permitted to execute for */
33unsigned int adreno_dispatch_time_slice = 25;
34
35/*
36 * If set then dispatcher tries to schedule lower priority RB's after if they
37 * have commands in their pipe and have been inactive for
38 * _dispatch_starvation_time. Also, once an RB is schduled it will be allowed
39 * to run for _dispatch_time_slice unless it's commands complete before
40 * _dispatch_time_slice
41 */
42unsigned int adreno_disp_preempt_fair_sched;
43
44/* Number of commands that can be queued in a context before it sleeps */
45static unsigned int _context_drawqueue_size = 50;
46
47/* Number of milliseconds to wait for the context queue to clear */
48static unsigned int _context_queue_wait = 10000;
49
50/* Number of drawobjs sent at a time from a single context */
51static unsigned int _context_drawobj_burst = 5;
52
53/*
54 * GFT throttle parameters. If GFT recovered more than
55 * X times in Y ms invalidate the context and do not attempt recovery.
56 * X -> _fault_throttle_burst
57 * Y -> _fault_throttle_time
58 */
59static unsigned int _fault_throttle_time = 3000;
60static unsigned int _fault_throttle_burst = 3;
61
62/*
63 * Maximum ringbuffer inflight for the single submitting context case - this
64 * should be sufficiently high to keep the GPU loaded
65 */
66static unsigned int _dispatcher_q_inflight_hi = 15;
67
68/*
69 * Minimum inflight for the multiple context case - this should sufficiently low
70 * to allow for lower latency context switching
71 */
72static unsigned int _dispatcher_q_inflight_lo = 4;
73
74/* Command batch timeout (in milliseconds) */
75unsigned int adreno_drawobj_timeout = 2000;
76
77/* Interval for reading and comparing fault detection registers */
78static unsigned int _fault_timer_interval = 200;
79
80#define DRAWQUEUE_RB(_drawqueue) \
81 ((struct adreno_ringbuffer *) \
82 container_of((_drawqueue),\
83 struct adreno_ringbuffer, dispatch_q))
84
85#define DRAWQUEUE(_ringbuffer) (&(_ringbuffer)->dispatch_q)
86
87static int adreno_dispatch_retire_drawqueue(struct adreno_device *adreno_dev,
88 struct adreno_dispatcher_drawqueue *drawqueue);
89
90static inline bool drawqueue_is_current(
91 struct adreno_dispatcher_drawqueue *drawqueue)
92{
93 struct adreno_ringbuffer *rb = DRAWQUEUE_RB(drawqueue);
94 struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
95
96 return (adreno_dev->cur_rb == rb);
97}
98
99static void _add_context(struct adreno_device *adreno_dev,
100 struct adreno_context *drawctxt)
101{
102 /* Remove it from the list */
103 list_del_init(&drawctxt->active_node);
104
105 /* And push it to the front */
106 drawctxt->active_time = jiffies;
107 list_add(&drawctxt->active_node, &adreno_dev->active_list);
108}
109
110static int __count_context(struct adreno_context *drawctxt, void *data)
111{
112 unsigned long expires = drawctxt->active_time + msecs_to_jiffies(100);
113
114 return time_after(jiffies, expires) ? 0 : 1;
115}
116
117static int __count_drawqueue_context(struct adreno_context *drawctxt,
118 void *data)
119{
120 unsigned long expires = drawctxt->active_time + msecs_to_jiffies(100);
121
122 if (time_after(jiffies, expires))
123 return 0;
124
125 return (&drawctxt->rb->dispatch_q ==
126 (struct adreno_dispatcher_drawqueue *) data) ? 1 : 0;
127}
128
129static int _adreno_count_active_contexts(struct adreno_device *adreno_dev,
130 int (*func)(struct adreno_context *, void *), void *data)
131{
132 struct adreno_context *ctxt;
133 int count = 0;
134
135 list_for_each_entry(ctxt, &adreno_dev->active_list, active_node) {
136 if (func(ctxt, data) == 0)
137 return count;
138
139 count++;
140 }
141
142 return count;
143}
144
145static void _track_context(struct adreno_device *adreno_dev,
146 struct adreno_dispatcher_drawqueue *drawqueue,
147 struct adreno_context *drawctxt)
148{
149 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
150
151 spin_lock(&adreno_dev->active_list_lock);
152
153 _add_context(adreno_dev, drawctxt);
154
155 device->active_context_count =
156 _adreno_count_active_contexts(adreno_dev,
157 __count_context, NULL);
158 drawqueue->active_context_count =
159 _adreno_count_active_contexts(adreno_dev,
160 __count_drawqueue_context, drawqueue);
161
162 spin_unlock(&adreno_dev->active_list_lock);
163}
164
165/*
166 * If only one context has queued in the last 100 milliseconds increase
167 * inflight to a high number to load up the GPU. If multiple contexts
168 * have queued drop the inflight for better context switch latency.
169 * If no contexts have queued what are you even doing here?
170 */
171
172static inline int
173_drawqueue_inflight(struct adreno_dispatcher_drawqueue *drawqueue)
174{
175 return (drawqueue->active_context_count > 1)
176 ? _dispatcher_q_inflight_lo : _dispatcher_q_inflight_hi;
177}
178
179static void fault_detect_read(struct adreno_device *adreno_dev)
180{
181 int i;
182
183 if (!test_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv))
184 return;
185
186 for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
187 struct adreno_ringbuffer *rb = &(adreno_dev->ringbuffers[i]);
188
189 adreno_rb_readtimestamp(adreno_dev, rb,
190 KGSL_TIMESTAMP_RETIRED, &(rb->fault_detect_ts));
191 }
192
193 for (i = 0; i < adreno_ft_regs_num; i++) {
194 if (adreno_ft_regs[i] != 0)
195 kgsl_regread(KGSL_DEVICE(adreno_dev), adreno_ft_regs[i],
196 &adreno_ft_regs_val[i]);
197 }
198}
199
200/*
201 * Check to see if the device is idle
202 */
203static inline bool _isidle(struct adreno_device *adreno_dev)
204{
205 const struct adreno_gpu_core *gpucore = adreno_dev->gpucore;
206 unsigned int reg_rbbm_status;
207
208 if (!kgsl_state_is_awake(KGSL_DEVICE(adreno_dev)))
209 goto ret;
210
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +0530211 if (adreno_rb_empty(adreno_dev->cur_rb))
212 goto ret;
213
Shrenuj Bansala419c792016-10-20 14:05:11 -0700214 /* only check rbbm status to determine if GPU is idle */
215 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &reg_rbbm_status);
216
217 if (reg_rbbm_status & gpucore->busy_mask)
218 return false;
219
220ret:
221 /* Clear the existing register values */
222 memset(adreno_ft_regs_val, 0,
223 adreno_ft_regs_num * sizeof(unsigned int));
224
225 return true;
226}
227
228/**
229 * fault_detect_read_compare() - Read the fault detect registers and compare
230 * them to the current value
231 * @device: Pointer to the KGSL device struct
232 *
233 * Read the set of fault detect registers and compare them to the current set
234 * of registers. Return 1 if any of the register values changed. Also, compare
235 * if the current RB's timstamp has changed or not.
236 */
237static int fault_detect_read_compare(struct adreno_device *adreno_dev)
238{
239 struct adreno_ringbuffer *rb = ADRENO_CURRENT_RINGBUFFER(adreno_dev);
240 int i, ret = 0;
241 unsigned int ts;
242
Shrenuj Bansalae672812016-02-24 14:17:30 -0800243 if (!test_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv))
244 return 1;
245
Shrenuj Bansala419c792016-10-20 14:05:11 -0700246 /* Check to see if the device is idle - if so report no hang */
247 if (_isidle(adreno_dev) == true)
248 ret = 1;
249
250 for (i = 0; i < adreno_ft_regs_num; i++) {
251 unsigned int val;
252
253 if (adreno_ft_regs[i] == 0)
254 continue;
255 kgsl_regread(KGSL_DEVICE(adreno_dev), adreno_ft_regs[i], &val);
256 if (val != adreno_ft_regs_val[i])
257 ret = 1;
258 adreno_ft_regs_val[i] = val;
259 }
260
261 if (!adreno_rb_readtimestamp(adreno_dev, adreno_dev->cur_rb,
262 KGSL_TIMESTAMP_RETIRED, &ts)) {
263 if (ts != rb->fault_detect_ts)
264 ret = 1;
265
266 rb->fault_detect_ts = ts;
267 }
268
269 return ret;
270}
271
272static void start_fault_timer(struct adreno_device *adreno_dev)
273{
274 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
275
276 if (adreno_soft_fault_detect(adreno_dev))
277 mod_timer(&dispatcher->fault_timer,
278 jiffies + msecs_to_jiffies(_fault_timer_interval));
279}
280
281/**
282 * _retire_timestamp() - Retire object without sending it
283 * to the hardware
284 * @drawobj: Pointer to the object to retire
285 *
286 * In some cases ibs can be retired by the software
287 * without going to the GPU. In those cases, update the
288 * memstore from the CPU, kick off the event engine to handle
289 * expired events and destroy the ib.
290 */
291static void _retire_timestamp(struct kgsl_drawobj *drawobj)
292{
293 struct kgsl_context *context = drawobj->context;
294 struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
295 struct kgsl_device *device = context->device;
296
297 /*
298 * Write the start and end timestamp to the memstore to keep the
299 * accounting sane
300 */
301 kgsl_sharedmem_writel(device, &device->memstore,
302 KGSL_MEMSTORE_OFFSET(context->id, soptimestamp),
303 drawobj->timestamp);
304
305 kgsl_sharedmem_writel(device, &device->memstore,
306 KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp),
307 drawobj->timestamp);
308
309
310 /* Retire pending GPU events for the object */
311 kgsl_process_event_group(device, &context->events);
312
313 /*
314 * For A3xx we still get the rptr from the CP_RB_RPTR instead of
315 * rptr scratch out address. At this point GPU clocks turned off.
316 * So avoid reading GPU register directly for A3xx.
317 */
318 if (adreno_is_a3xx(ADRENO_DEVICE(device)))
319 trace_adreno_cmdbatch_retired(drawobj, -1, 0, 0, drawctxt->rb,
320 0, 0);
321 else
322 trace_adreno_cmdbatch_retired(drawobj, -1, 0, 0, drawctxt->rb,
323 adreno_get_rptr(drawctxt->rb), 0);
324 kgsl_drawobj_destroy(drawobj);
325}
326
327static int _check_context_queue(struct adreno_context *drawctxt)
328{
329 int ret;
330
331 spin_lock(&drawctxt->lock);
332
333 /*
334 * Wake up if there is room in the context or if the whole thing got
335 * invalidated while we were asleep
336 */
337
338 if (kgsl_context_invalid(&drawctxt->base))
339 ret = 1;
340 else
341 ret = drawctxt->queued < _context_drawqueue_size ? 1 : 0;
342
343 spin_unlock(&drawctxt->lock);
344
345 return ret;
346}
347
348/*
349 * return true if this is a marker command and the dependent timestamp has
350 * retired
351 */
352static bool _marker_expired(struct kgsl_drawobj_cmd *markerobj)
353{
354 struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
355
356 return (drawobj->flags & KGSL_DRAWOBJ_MARKER) &&
357 kgsl_check_timestamp(drawobj->device, drawobj->context,
358 markerobj->marker_timestamp);
359}
360
361static inline void _pop_drawobj(struct adreno_context *drawctxt)
362{
363 drawctxt->drawqueue_head = DRAWQUEUE_NEXT(drawctxt->drawqueue_head,
364 ADRENO_CONTEXT_DRAWQUEUE_SIZE);
365 drawctxt->queued--;
366}
367
Tarun Karra2b8b3632016-11-14 16:38:27 -0800368static void _retire_sparseobj(struct kgsl_drawobj_sparse *sparseobj,
369 struct adreno_context *drawctxt)
370{
371 kgsl_sparse_bind(drawctxt->base.proc_priv, sparseobj);
372 _retire_timestamp(DRAWOBJ(sparseobj));
373}
374
Shrenuj Bansala419c792016-10-20 14:05:11 -0700375static int _retire_markerobj(struct kgsl_drawobj_cmd *cmdobj,
376 struct adreno_context *drawctxt)
377{
378 if (_marker_expired(cmdobj)) {
379 _pop_drawobj(drawctxt);
380 _retire_timestamp(DRAWOBJ(cmdobj));
381 return 0;
382 }
383
384 /*
385 * If the marker isn't expired but the SKIP bit
386 * is set then there are real commands following
387 * this one in the queue. This means that we
388 * need to dispatch the command so that we can
389 * keep the timestamp accounting correct. If
390 * skip isn't set then we block this queue
391 * until the dependent timestamp expires
392 */
393 return test_bit(CMDOBJ_SKIP, &cmdobj->priv) ? 1 : -EAGAIN;
394}
395
396static int _retire_syncobj(struct kgsl_drawobj_sync *syncobj,
397 struct adreno_context *drawctxt)
398{
399 if (!kgsl_drawobj_events_pending(syncobj)) {
400 _pop_drawobj(drawctxt);
401 kgsl_drawobj_destroy(DRAWOBJ(syncobj));
402 return 0;
403 }
404
405 /*
406 * If we got here, there are pending events for sync object.
407 * Start the canary timer if it hasnt been started already.
408 */
409 if (!syncobj->timeout_jiffies) {
410 syncobj->timeout_jiffies = jiffies + msecs_to_jiffies(5000);
411 mod_timer(&syncobj->timer, syncobj->timeout_jiffies);
412 }
413
414 return -EAGAIN;
415}
416
417/*
418 * Retires all expired marker and sync objs from the context
419 * queue and returns one of the below
420 * a) next drawobj that needs to be sent to ringbuffer
421 * b) -EAGAIN for syncobj with syncpoints pending.
422 * c) -EAGAIN for markerobj whose marker timestamp has not expired yet.
423 * c) NULL for no commands remaining in drawqueue.
424 */
425static struct kgsl_drawobj *_process_drawqueue_get_next_drawobj(
426 struct adreno_context *drawctxt)
427{
428 struct kgsl_drawobj *drawobj;
429 unsigned int i = drawctxt->drawqueue_head;
430 int ret = 0;
431
432 if (drawctxt->drawqueue_head == drawctxt->drawqueue_tail)
433 return NULL;
434
435 for (i = drawctxt->drawqueue_head; i != drawctxt->drawqueue_tail;
436 i = DRAWQUEUE_NEXT(i, ADRENO_CONTEXT_DRAWQUEUE_SIZE)) {
437
438 drawobj = drawctxt->drawqueue[i];
439
440 if (drawobj == NULL)
441 return NULL;
442
443 if (drawobj->type == CMDOBJ_TYPE)
444 return drawobj;
445 else if (drawobj->type == MARKEROBJ_TYPE) {
446 ret = _retire_markerobj(CMDOBJ(drawobj), drawctxt);
447 /* Special case where marker needs to be sent to GPU */
448 if (ret == 1)
449 return drawobj;
450 } else if (drawobj->type == SYNCOBJ_TYPE)
451 ret = _retire_syncobj(SYNCOBJ(drawobj), drawctxt);
Tarun Karra2b8b3632016-11-14 16:38:27 -0800452 else
453 return ERR_PTR(-EINVAL);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700454
455 if (ret == -EAGAIN)
456 return ERR_PTR(-EAGAIN);
457
458 continue;
459 }
460
461 return NULL;
462}
463
464/**
465 * adreno_dispatcher_requeue_cmdobj() - Put a command back on the context
466 * queue
467 * @drawctxt: Pointer to the adreno draw context
468 * @cmdobj: Pointer to the KGSL command object to requeue
469 *
470 * Failure to submit a command to the ringbuffer isn't the fault of the command
471 * being submitted so if a failure happens, push it back on the head of the the
472 * context queue to be reconsidered again unless the context got detached.
473 */
474static inline int adreno_dispatcher_requeue_cmdobj(
475 struct adreno_context *drawctxt,
476 struct kgsl_drawobj_cmd *cmdobj)
477{
478 unsigned int prev;
479 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
480
481 spin_lock(&drawctxt->lock);
482
483 if (kgsl_context_detached(&drawctxt->base) ||
484 kgsl_context_invalid(&drawctxt->base)) {
485 spin_unlock(&drawctxt->lock);
486 /* get rid of this drawobj since the context is bad */
487 kgsl_drawobj_destroy(drawobj);
488 return -ENOENT;
489 }
490
491 prev = drawctxt->drawqueue_head == 0 ?
492 (ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1) :
493 (drawctxt->drawqueue_head - 1);
494
495 /*
496 * The maximum queue size always needs to be one less then the size of
497 * the ringbuffer queue so there is "room" to put the drawobj back in
498 */
499
500 WARN_ON(prev == drawctxt->drawqueue_tail);
501
502 drawctxt->drawqueue[prev] = drawobj;
503 drawctxt->queued++;
504
505 /* Reset the command queue head to reflect the newly requeued change */
506 drawctxt->drawqueue_head = prev;
507 spin_unlock(&drawctxt->lock);
508 return 0;
509}
510
511/**
512 * dispatcher_queue_context() - Queue a context in the dispatcher pending list
513 * @dispatcher: Pointer to the adreno dispatcher struct
514 * @drawctxt: Pointer to the adreno draw context
515 *
516 * Add a context to the dispatcher pending list.
517 */
518static void dispatcher_queue_context(struct adreno_device *adreno_dev,
519 struct adreno_context *drawctxt)
520{
521 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
522
523 /* Refuse to queue a detached context */
524 if (kgsl_context_detached(&drawctxt->base))
525 return;
526
527 spin_lock(&dispatcher->plist_lock);
528
529 if (plist_node_empty(&drawctxt->pending)) {
530 /* Get a reference to the context while it sits on the list */
531 if (_kgsl_context_get(&drawctxt->base)) {
532 trace_dispatch_queue_context(drawctxt);
533 plist_add(&drawctxt->pending, &dispatcher->pending);
534 }
535 }
536
537 spin_unlock(&dispatcher->plist_lock);
538}
539
540/**
541 * sendcmd() - Send a drawobj to the GPU hardware
542 * @dispatcher: Pointer to the adreno dispatcher struct
543 * @drawobj: Pointer to the KGSL drawobj being sent
544 *
545 * Send a KGSL drawobj to the GPU hardware
546 */
547static int sendcmd(struct adreno_device *adreno_dev,
548 struct kgsl_drawobj_cmd *cmdobj)
549{
550 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
551 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
552 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
553 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
554 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
555 struct adreno_dispatcher_drawqueue *dispatch_q =
556 ADRENO_DRAWOBJ_DISPATCH_DRAWQUEUE(drawobj);
557 struct adreno_submit_time time;
558 uint64_t secs = 0;
559 unsigned long nsecs = 0;
560 int ret;
561
562 mutex_lock(&device->mutex);
563 if (adreno_gpu_halt(adreno_dev) != 0) {
564 mutex_unlock(&device->mutex);
565 return -EBUSY;
566 }
567
568 dispatcher->inflight++;
569 dispatch_q->inflight++;
570
571 if (dispatcher->inflight == 1 &&
572 !test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
573 /* Time to make the donuts. Turn on the GPU */
574 ret = kgsl_active_count_get(device);
575 if (ret) {
576 dispatcher->inflight--;
577 dispatch_q->inflight--;
578 mutex_unlock(&device->mutex);
579 return ret;
580 }
581
582 set_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
583 }
584
585 if (test_bit(ADRENO_DEVICE_DRAWOBJ_PROFILE, &adreno_dev->priv)) {
586 set_bit(CMDOBJ_PROFILE, &cmdobj->priv);
587 cmdobj->profile_index = adreno_dev->profile_index;
588 adreno_dev->profile_index =
589 (adreno_dev->profile_index + 1) %
590 ADRENO_DRAWOBJ_PROFILE_COUNT;
591 }
592
593 ret = adreno_ringbuffer_submitcmd(adreno_dev, cmdobj, &time);
594
595 /*
596 * On the first command, if the submission was successful, then read the
597 * fault registers. If it failed then turn off the GPU. Sad face.
598 */
599
600 if (dispatcher->inflight == 1) {
601 if (ret == 0) {
602
603 /* Stop fault timer before reading fault registers */
604 del_timer_sync(&dispatcher->fault_timer);
605
606 fault_detect_read(adreno_dev);
607
608 /* Start the fault timer on first submission */
609 start_fault_timer(adreno_dev);
610
611 if (!test_and_set_bit(ADRENO_DISPATCHER_ACTIVE,
612 &dispatcher->priv))
613 reinit_completion(&dispatcher->idle_gate);
Prakash Kamliyac9b7da02016-12-20 17:45:41 +0530614
615 /*
616 * We update power stats generally at the expire of
617 * cmdbatch. In cases where the cmdbatch takes a long
618 * time to finish, it will delay power stats update,
619 * in effect it will delay DCVS decision. Start a
620 * timer to update power state on expire of this timer.
621 */
622 kgsl_pwrscale_midframe_timer_restart(device);
623
Shrenuj Bansala419c792016-10-20 14:05:11 -0700624 } else {
625 kgsl_active_count_put(device);
626 clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
627 }
628 }
629
630
631 if (ret) {
632 dispatcher->inflight--;
633 dispatch_q->inflight--;
634
635 mutex_unlock(&device->mutex);
636
637 /*
638 * Don't log a message in case of:
639 * -ENOENT means that the context was detached before the
640 * command was submitted
641 * -ENOSPC means that there temporarily isn't any room in the
642 * ringbuffer
643 * -PROTO means that a fault is currently being worked
644 */
645
646 if (ret != -ENOENT && ret != -ENOSPC && ret != -EPROTO)
647 KGSL_DRV_ERR(device,
648 "Unable to submit command to the ringbuffer %d\n",
649 ret);
650 return ret;
651 }
652
653 secs = time.ktime;
654 nsecs = do_div(secs, 1000000000);
655
656 trace_adreno_cmdbatch_submitted(drawobj, (int) dispatcher->inflight,
657 time.ticks, (unsigned long) secs, nsecs / 1000, drawctxt->rb,
658 adreno_get_rptr(drawctxt->rb));
659
660 mutex_unlock(&device->mutex);
661
662 cmdobj->submit_ticks = time.ticks;
663
664 dispatch_q->cmd_q[dispatch_q->tail] = cmdobj;
665 dispatch_q->tail = (dispatch_q->tail + 1) %
666 ADRENO_DISPATCH_DRAWQUEUE_SIZE;
667
668 /*
669 * For the first submission in any given command queue update the
670 * expected expire time - this won't actually be used / updated until
671 * the command queue in question goes current, but universally setting
672 * it here avoids the possibilty of some race conditions with preempt
673 */
674
675 if (dispatch_q->inflight == 1)
676 dispatch_q->expires = jiffies +
677 msecs_to_jiffies(adreno_drawobj_timeout);
678
679 /*
680 * If we believe ourselves to be current and preemption isn't a thing,
681 * then set up the timer. If this misses, then preemption is indeed a
682 * thing and the timer will be set up in due time
683 */
684 if (!adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
685 if (drawqueue_is_current(dispatch_q))
686 mod_timer(&dispatcher->timer, dispatch_q->expires);
687 }
688
689
690 /*
691 * we just submitted something, readjust ringbuffer
692 * execution level
693 */
694 if (gpudev->preemption_schedule)
695 gpudev->preemption_schedule(adreno_dev);
696 return 0;
697}
698
Tarun Karra2b8b3632016-11-14 16:38:27 -0800699
700/*
701 * Retires all sync objs from the sparse context
702 * queue and returns one of the below
703 * a) next sparseobj
704 * b) -EAGAIN for syncobj with syncpoints pending
705 * c) -EINVAL for unexpected drawobj
706 * d) NULL for no sparseobj
707 */
708static struct kgsl_drawobj_sparse *_get_next_sparseobj(
709 struct adreno_context *drawctxt)
710{
711 struct kgsl_drawobj *drawobj;
712 unsigned int i = drawctxt->drawqueue_head;
713 int ret = 0;
714
715 if (drawctxt->drawqueue_head == drawctxt->drawqueue_tail)
716 return NULL;
717
718 for (i = drawctxt->drawqueue_head; i != drawctxt->drawqueue_tail;
719 i = DRAWQUEUE_NEXT(i, ADRENO_CONTEXT_DRAWQUEUE_SIZE)) {
720
721 drawobj = drawctxt->drawqueue[i];
722
723 if (drawobj == NULL)
724 return NULL;
725
726 if (drawobj->type == SYNCOBJ_TYPE)
727 ret = _retire_syncobj(SYNCOBJ(drawobj), drawctxt);
728 else if (drawobj->type == SPARSEOBJ_TYPE)
729 return SPARSEOBJ(drawobj);
730 else
731 return ERR_PTR(-EINVAL);
732
733 if (ret == -EAGAIN)
734 return ERR_PTR(-EAGAIN);
735
736 continue;
737 }
738
739 return NULL;
740}
741
742static int _process_drawqueue_sparse(
743 struct adreno_context *drawctxt)
744{
745 struct kgsl_drawobj_sparse *sparseobj;
746 int ret = 0;
747 unsigned int i;
748
749 for (i = 0; i < ADRENO_CONTEXT_DRAWQUEUE_SIZE; i++) {
750
751 spin_lock(&drawctxt->lock);
752 sparseobj = _get_next_sparseobj(drawctxt);
753 if (IS_ERR_OR_NULL(sparseobj)) {
754 if (IS_ERR(sparseobj))
755 ret = PTR_ERR(sparseobj);
756 spin_unlock(&drawctxt->lock);
757 return ret;
758 }
759
760 _pop_drawobj(drawctxt);
761 spin_unlock(&drawctxt->lock);
762
763 _retire_sparseobj(sparseobj, drawctxt);
764 }
765
766 return 0;
767}
768
Shrenuj Bansala419c792016-10-20 14:05:11 -0700769/**
770 * dispatcher_context_sendcmds() - Send commands from a context to the GPU
771 * @adreno_dev: Pointer to the adreno device struct
772 * @drawctxt: Pointer to the adreno context to dispatch commands from
773 *
774 * Dequeue and send a burst of commands from the specified context to the GPU
775 * Returns postive if the context needs to be put back on the pending queue
776 * 0 if the context is empty or detached and negative on error
777 */
778static int dispatcher_context_sendcmds(struct adreno_device *adreno_dev,
779 struct adreno_context *drawctxt)
780{
781 struct adreno_dispatcher_drawqueue *dispatch_q =
782 &(drawctxt->rb->dispatch_q);
783 int count = 0;
784 int ret = 0;
785 int inflight = _drawqueue_inflight(dispatch_q);
786 unsigned int timestamp;
787
Tarun Karra2b8b3632016-11-14 16:38:27 -0800788 if (drawctxt->base.flags & KGSL_CONTEXT_SPARSE)
789 return _process_drawqueue_sparse(drawctxt);
790
Shrenuj Bansala419c792016-10-20 14:05:11 -0700791 if (dispatch_q->inflight >= inflight) {
792 spin_lock(&drawctxt->lock);
793 _process_drawqueue_get_next_drawobj(drawctxt);
794 spin_unlock(&drawctxt->lock);
795 return -EBUSY;
796 }
797
798 /*
799 * Each context can send a specific number of drawobjs per cycle
800 */
801 while ((count < _context_drawobj_burst) &&
802 (dispatch_q->inflight < inflight)) {
803 struct kgsl_drawobj *drawobj;
804 struct kgsl_drawobj_cmd *cmdobj;
805
806 if (adreno_gpu_fault(adreno_dev) != 0)
807 break;
808
809 spin_lock(&drawctxt->lock);
810 drawobj = _process_drawqueue_get_next_drawobj(drawctxt);
811
812 /*
813 * adreno_context_get_drawobj returns -EAGAIN if the current
814 * drawobj has pending sync points so no more to do here.
815 * When the sync points are satisfied then the context will get
816 * reqeueued
817 */
818
819 if (IS_ERR_OR_NULL(drawobj)) {
820 if (IS_ERR(drawobj))
821 ret = PTR_ERR(drawobj);
822 spin_unlock(&drawctxt->lock);
823 break;
824 }
825 _pop_drawobj(drawctxt);
826 spin_unlock(&drawctxt->lock);
827
828 timestamp = drawobj->timestamp;
829 cmdobj = CMDOBJ(drawobj);
830 ret = sendcmd(adreno_dev, cmdobj);
831
832 /*
833 * On error from sendcmd() try to requeue the cmdobj
834 * unless we got back -ENOENT which means that the context has
835 * been detached and there will be no more deliveries from here
836 */
837 if (ret != 0) {
838 /* Destroy the cmdobj on -ENOENT */
839 if (ret == -ENOENT)
840 kgsl_drawobj_destroy(drawobj);
841 else {
842 /*
843 * If the requeue returns an error, return that
844 * instead of whatever sendcmd() sent us
845 */
846 int r = adreno_dispatcher_requeue_cmdobj(
847 drawctxt, cmdobj);
848 if (r)
849 ret = r;
850 }
851
852 break;
853 }
854
855 drawctxt->submitted_timestamp = timestamp;
856
857 count++;
858 }
859
860 /*
861 * Wake up any snoozing threads if we have consumed any real commands
862 * or marker commands and we have room in the context queue.
863 */
864
865 if (_check_context_queue(drawctxt))
866 wake_up_all(&drawctxt->wq);
867
868 if (!ret)
869 ret = count;
870
871 /* Return error or the number of commands queued */
872 return ret;
873}
874
875/**
876 * _adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
877 * @adreno_dev: Pointer to the adreno device struct
878 *
879 * Issue as many commands as possible (up to inflight) from the pending contexts
880 * This function assumes the dispatcher mutex has been locked.
881 */
882static void _adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
883{
884 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
885 struct adreno_context *drawctxt, *next;
886 struct plist_head requeue, busy_list;
887 int ret;
888
889 /* Leave early if the dispatcher isn't in a happy state */
890 if (adreno_gpu_fault(adreno_dev) != 0)
891 return;
892
893 plist_head_init(&requeue);
894 plist_head_init(&busy_list);
895
896 /* Try to fill the ringbuffers as much as possible */
897 while (1) {
898
899 /* Stop doing things if the dispatcher is paused or faulted */
900 if (adreno_gpu_fault(adreno_dev) != 0)
901 break;
902
903 if (adreno_gpu_halt(adreno_dev) != 0)
904 break;
905
906 spin_lock(&dispatcher->plist_lock);
907
908 if (plist_head_empty(&dispatcher->pending)) {
909 spin_unlock(&dispatcher->plist_lock);
910 break;
911 }
912
913 /* Get the next entry on the list */
914 drawctxt = plist_first_entry(&dispatcher->pending,
915 struct adreno_context, pending);
916
917 plist_del(&drawctxt->pending, &dispatcher->pending);
918
919 spin_unlock(&dispatcher->plist_lock);
920
921 if (kgsl_context_detached(&drawctxt->base) ||
922 kgsl_context_invalid(&drawctxt->base)) {
923 kgsl_context_put(&drawctxt->base);
924 continue;
925 }
926
927 ret = dispatcher_context_sendcmds(adreno_dev, drawctxt);
928
929 /* Don't bother requeuing on -ENOENT - context is detached */
930 if (ret != 0 && ret != -ENOENT) {
931 spin_lock(&dispatcher->plist_lock);
932
933 /*
934 * Check to seen if the context had been requeued while
935 * we were processing it (probably by another thread
936 * pushing commands). If it has then shift it to the
937 * requeue list if it was not able to submit commands
938 * due to the dispatch_q being full. Also, do a put to
939 * make sure the reference counting stays accurate.
940 * If the node is empty then we will put it on the
941 * requeue list and not touch the refcount since we
942 * already hold it from the first time it went on the
943 * list.
944 */
945
946 if (!plist_node_empty(&drawctxt->pending)) {
947 plist_del(&drawctxt->pending,
948 &dispatcher->pending);
949 kgsl_context_put(&drawctxt->base);
950 }
951
952 if (ret == -EBUSY)
953 /* Inflight queue is full */
954 plist_add(&drawctxt->pending, &busy_list);
955 else
956 plist_add(&drawctxt->pending, &requeue);
957
958 spin_unlock(&dispatcher->plist_lock);
959 } else {
960 /*
961 * If the context doesn't need be requeued put back the
962 * refcount
963 */
964
965 kgsl_context_put(&drawctxt->base);
966 }
967 }
968
969 spin_lock(&dispatcher->plist_lock);
970
971 /* Put the contexts that couldn't submit back on the pending list */
972 plist_for_each_entry_safe(drawctxt, next, &busy_list, pending) {
973 plist_del(&drawctxt->pending, &busy_list);
974 plist_add(&drawctxt->pending, &dispatcher->pending);
975 }
976
977 /* Now put the contexts that need to be requeued back on the list */
978 plist_for_each_entry_safe(drawctxt, next, &requeue, pending) {
979 plist_del(&drawctxt->pending, &requeue);
980 plist_add(&drawctxt->pending, &dispatcher->pending);
981 }
982
983 spin_unlock(&dispatcher->plist_lock);
984}
985
Hareesh Gundu2eb74d72017-06-07 14:50:15 +0530986static inline void _decrement_submit_now(struct kgsl_device *device)
987{
988 spin_lock(&device->submit_lock);
989 device->submit_now--;
990 spin_unlock(&device->submit_lock);
991}
992
Shrenuj Bansala419c792016-10-20 14:05:11 -0700993/**
994 * adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
995 * @adreno_dev: Pointer to the adreno device struct
996 *
997 * Lock the dispatcher and call _adreno_dispatcher_issueibcmds
998 */
999static void adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
1000{
1001 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301002 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1003
1004 spin_lock(&device->submit_lock);
1005 /* If state transition to SLUMBER, schedule the work for later */
1006 if (device->slumber == true) {
1007 spin_unlock(&device->submit_lock);
1008 goto done;
1009 }
1010 device->submit_now++;
1011 spin_unlock(&device->submit_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001012
1013 /* If the dispatcher is busy then schedule the work for later */
1014 if (!mutex_trylock(&dispatcher->mutex)) {
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301015 _decrement_submit_now(device);
1016 goto done;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001017 }
1018
1019 _adreno_dispatcher_issuecmds(adreno_dev);
1020 mutex_unlock(&dispatcher->mutex);
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301021 _decrement_submit_now(device);
1022 return;
1023done:
1024 adreno_dispatcher_schedule(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001025}
1026
1027/**
1028 * get_timestamp() - Return the next timestamp for the context
1029 * @drawctxt - Pointer to an adreno draw context struct
1030 * @drawobj - Pointer to a drawobj
1031 * @timestamp - Pointer to a timestamp value possibly passed from the user
1032 * @user_ts - user generated timestamp
1033 *
1034 * Assign a timestamp based on the settings of the draw context and the command
1035 * batch.
1036 */
1037static int get_timestamp(struct adreno_context *drawctxt,
1038 struct kgsl_drawobj *drawobj, unsigned int *timestamp,
1039 unsigned int user_ts)
1040{
1041
1042 if (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS) {
1043 /*
1044 * User specified timestamps need to be greater than the last
1045 * issued timestamp in the context
1046 */
1047 if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0)
1048 return -ERANGE;
1049
1050 drawctxt->timestamp = user_ts;
1051 } else
1052 drawctxt->timestamp++;
1053
1054 *timestamp = drawctxt->timestamp;
1055 drawobj->timestamp = *timestamp;
1056 return 0;
1057}
1058
1059static void _set_ft_policy(struct adreno_device *adreno_dev,
1060 struct adreno_context *drawctxt,
1061 struct kgsl_drawobj_cmd *cmdobj)
1062{
1063 /*
1064 * Set the fault tolerance policy for the command batch - assuming the
1065 * context hasn't disabled FT use the current device policy
1066 */
1067 if (drawctxt->base.flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
1068 set_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy);
Hareesh Gunduccfb89b2017-04-14 18:36:20 +05301069 /*
1070 * Set the fault tolerance policy to FT_REPLAY - As context wants
1071 * to invalidate it after a replay attempt fails. This doesn't
1072 * require to execute the default FT policy.
1073 */
1074 else if (drawctxt->base.flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT)
1075 set_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001076 else
1077 cmdobj->fault_policy = adreno_dev->ft_policy;
1078}
1079
1080static void _cmdobj_set_flags(struct adreno_context *drawctxt,
1081 struct kgsl_drawobj_cmd *cmdobj)
1082{
1083 /*
1084 * Force the preamble for this submission only - this is usually
1085 * requested by the dispatcher as part of fault recovery
1086 */
1087 if (test_and_clear_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
1088 &drawctxt->base.priv))
1089 set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
1090
1091 /*
1092 * Force the premable if set from userspace in the context or
1093 * command obj flags
1094 */
1095 if ((drawctxt->base.flags & KGSL_CONTEXT_CTX_SWITCH) ||
1096 (cmdobj->base.flags & KGSL_DRAWOBJ_CTX_SWITCH))
1097 set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
1098
1099 /* Skip this ib if IFH_NOP is enabled */
1100 if (drawctxt->base.flags & KGSL_CONTEXT_IFH_NOP)
1101 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1102
1103 /*
1104 * If we are waiting for the end of frame and it hasn't appeared yet,
1105 * then mark the command obj as skipped. It will still progress
1106 * through the pipeline but it won't actually send any commands
1107 */
1108
1109 if (test_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv)) {
1110 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1111
1112 /*
1113 * If this command obj represents the EOF then clear the way
1114 * for the dispatcher to continue submitting
1115 */
1116
1117 if (cmdobj->base.flags & KGSL_DRAWOBJ_END_OF_FRAME) {
1118 clear_bit(ADRENO_CONTEXT_SKIP_EOF,
1119 &drawctxt->base.priv);
1120
1121 /*
1122 * Force the preamble on the next command to ensure that
1123 * the state is correct
1124 */
1125 set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
1126 &drawctxt->base.priv);
1127 }
1128 }
1129}
1130
1131static inline int _check_context_state(struct kgsl_context *context)
1132{
1133 if (kgsl_context_invalid(context))
1134 return -EDEADLK;
1135
1136 if (kgsl_context_detached(context))
1137 return -ENOENT;
1138
1139 return 0;
1140}
1141
1142static inline bool _verify_ib(struct kgsl_device_private *dev_priv,
1143 struct kgsl_context *context, struct kgsl_memobj_node *ib)
1144{
1145 struct kgsl_device *device = dev_priv->device;
1146 struct kgsl_process_private *private = dev_priv->process_priv;
1147
1148 /* The maximum allowable size for an IB in the CP is 0xFFFFF dwords */
1149 if (ib->size == 0 || ((ib->size >> 2) > 0xFFFFF)) {
1150 pr_context(device, context, "ctxt %d invalid ib size %lld\n",
1151 context->id, ib->size);
1152 return false;
1153 }
1154
1155 /* Make sure that the address is mapped */
1156 if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, ib->gpuaddr)) {
1157 pr_context(device, context, "ctxt %d invalid ib gpuaddr %llX\n",
1158 context->id, ib->gpuaddr);
1159 return false;
1160 }
1161
1162 return true;
1163}
1164
1165static inline int _verify_cmdobj(struct kgsl_device_private *dev_priv,
1166 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
1167 uint32_t count)
1168{
1169 struct kgsl_device *device = dev_priv->device;
1170 struct kgsl_memobj_node *ib;
1171 unsigned int i;
1172
1173 for (i = 0; i < count; i++) {
1174 /* Verify the IBs before they get queued */
1175 if (drawobj[i]->type == CMDOBJ_TYPE) {
1176 struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj[i]);
1177
1178 list_for_each_entry(ib, &cmdobj->cmdlist, node)
1179 if (_verify_ib(dev_priv,
1180 &ADRENO_CONTEXT(context)->base, ib)
1181 == false)
1182 return -EINVAL;
1183 /*
1184 * Clear the wake on touch bit to indicate an IB has
1185 * been submitted since the last time we set it.
1186 * But only clear it when we have rendering commands.
1187 */
1188 device->flags &= ~KGSL_FLAG_WAKE_ON_TOUCH;
1189 }
1190
1191 /* A3XX does not have support for drawobj profiling */
1192 if (adreno_is_a3xx(ADRENO_DEVICE(device)) &&
1193 (drawobj[i]->flags & KGSL_DRAWOBJ_PROFILING))
1194 return -EOPNOTSUPP;
1195 }
1196
1197 return 0;
1198}
1199
1200static inline int _wait_for_room_in_context_queue(
1201 struct adreno_context *drawctxt)
1202{
1203 int ret = 0;
1204
1205 /* Wait for room in the context queue */
1206 while (drawctxt->queued >= _context_drawqueue_size) {
1207 trace_adreno_drawctxt_sleep(drawctxt);
1208 spin_unlock(&drawctxt->lock);
1209
1210 ret = wait_event_interruptible_timeout(drawctxt->wq,
1211 _check_context_queue(drawctxt),
1212 msecs_to_jiffies(_context_queue_wait));
1213
1214 spin_lock(&drawctxt->lock);
1215 trace_adreno_drawctxt_wake(drawctxt);
1216
1217 if (ret <= 0)
1218 return (ret == 0) ? -ETIMEDOUT : (int) ret;
1219 }
1220
1221 return 0;
1222}
1223
1224static unsigned int _check_context_state_to_queue_cmds(
1225 struct adreno_context *drawctxt)
1226{
1227 int ret = _check_context_state(&drawctxt->base);
1228
1229 if (ret)
1230 return ret;
1231
1232 ret = _wait_for_room_in_context_queue(drawctxt);
1233 if (ret)
1234 return ret;
1235
1236 /*
1237 * Account for the possiblity that the context got invalidated
1238 * while we were sleeping
1239 */
1240 return _check_context_state(&drawctxt->base);
1241}
1242
1243static void _queue_drawobj(struct adreno_context *drawctxt,
1244 struct kgsl_drawobj *drawobj)
1245{
1246 /* Put the command into the queue */
1247 drawctxt->drawqueue[drawctxt->drawqueue_tail] = drawobj;
1248 drawctxt->drawqueue_tail = (drawctxt->drawqueue_tail + 1) %
1249 ADRENO_CONTEXT_DRAWQUEUE_SIZE;
1250 drawctxt->queued++;
1251 trace_adreno_cmdbatch_queued(drawobj, drawctxt->queued);
1252}
1253
Tarun Karra2b8b3632016-11-14 16:38:27 -08001254static int _queue_sparseobj(struct adreno_device *adreno_dev,
1255 struct adreno_context *drawctxt, struct kgsl_drawobj_sparse *sparseobj,
1256 uint32_t *timestamp, unsigned int user_ts)
1257{
1258 struct kgsl_drawobj *drawobj = DRAWOBJ(sparseobj);
1259 int ret;
1260
1261 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1262 if (ret)
1263 return ret;
1264
1265 /*
1266 * See if we can fastpath this thing - if nothing is
1267 * queued bind/unbind without queueing the context
1268 */
1269 if (!drawctxt->queued)
1270 return 1;
1271
1272 drawctxt->queued_timestamp = *timestamp;
1273 _queue_drawobj(drawctxt, drawobj);
1274
1275 return 0;
1276}
1277
1278
Shrenuj Bansala419c792016-10-20 14:05:11 -07001279static int _queue_markerobj(struct adreno_device *adreno_dev,
1280 struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *markerobj,
1281 uint32_t *timestamp, unsigned int user_ts)
1282{
1283 struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
1284 int ret;
1285
1286 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1287 if (ret)
1288 return ret;
1289
1290 /*
1291 * See if we can fastpath this thing - if nothing is queued
1292 * and nothing is inflight retire without bothering the GPU
1293 */
1294 if (!drawctxt->queued && kgsl_check_timestamp(drawobj->device,
1295 drawobj->context, drawctxt->queued_timestamp)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001296 _retire_timestamp(drawobj);
1297 return 1;
1298 }
1299
1300 /*
1301 * Remember the last queued timestamp - the marker will block
1302 * until that timestamp is expired (unless another command
1303 * comes along and forces the marker to execute)
1304 */
1305
1306 markerobj->marker_timestamp = drawctxt->queued_timestamp;
1307 drawctxt->queued_timestamp = *timestamp;
1308 _set_ft_policy(adreno_dev, drawctxt, markerobj);
1309 _cmdobj_set_flags(drawctxt, markerobj);
1310
1311 _queue_drawobj(drawctxt, drawobj);
1312
1313 return 0;
1314}
1315
1316static int _queue_cmdobj(struct adreno_device *adreno_dev,
1317 struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *cmdobj,
1318 uint32_t *timestamp, unsigned int user_ts)
1319{
1320 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1321 unsigned int j;
1322 int ret;
1323
1324 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1325 if (ret)
1326 return ret;
1327
1328 /*
1329 * If this is a real command then we need to force any markers
1330 * queued before it to dispatch to keep time linear - set the
1331 * skip bit so the commands get NOPed.
1332 */
1333 j = drawctxt->drawqueue_head;
1334
1335 while (j != drawctxt->drawqueue_tail) {
1336 if (drawctxt->drawqueue[j]->type == MARKEROBJ_TYPE) {
1337 struct kgsl_drawobj_cmd *markerobj =
1338 CMDOBJ(drawctxt->drawqueue[j]);
1339 set_bit(CMDOBJ_SKIP, &markerobj->priv);
1340 }
1341
1342 j = DRAWQUEUE_NEXT(j, ADRENO_CONTEXT_DRAWQUEUE_SIZE);
1343 }
1344
1345 drawctxt->queued_timestamp = *timestamp;
1346 _set_ft_policy(adreno_dev, drawctxt, cmdobj);
1347 _cmdobj_set_flags(drawctxt, cmdobj);
1348
1349 _queue_drawobj(drawctxt, drawobj);
1350
1351 return 0;
1352}
1353
1354static void _queue_syncobj(struct adreno_context *drawctxt,
1355 struct kgsl_drawobj_sync *syncobj, uint32_t *timestamp)
1356{
1357 struct kgsl_drawobj *drawobj = DRAWOBJ(syncobj);
1358
1359 *timestamp = 0;
1360 drawobj->timestamp = 0;
1361
1362 _queue_drawobj(drawctxt, drawobj);
1363}
1364
1365/**
Tarun Karra2b8b3632016-11-14 16:38:27 -08001366 * adreno_dispactcher_queue_cmds() - Queue a new draw object in the context
Shrenuj Bansala419c792016-10-20 14:05:11 -07001367 * @dev_priv: Pointer to the device private struct
1368 * @context: Pointer to the kgsl draw context
1369 * @drawobj: Pointer to the array of drawobj's being submitted
1370 * @count: Number of drawobj's being submitted
1371 * @timestamp: Pointer to the requested timestamp
1372 *
1373 * Queue a command in the context - if there isn't any room in the queue, then
1374 * block until there is
1375 */
1376int adreno_dispatcher_queue_cmds(struct kgsl_device_private *dev_priv,
1377 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
1378 uint32_t count, uint32_t *timestamp)
1379
1380{
1381 struct kgsl_device *device = dev_priv->device;
1382 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1383 struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
1384 struct adreno_dispatcher_drawqueue *dispatch_q;
1385 int ret;
1386 unsigned int i, user_ts;
1387
Tarun Karra2b8b3632016-11-14 16:38:27 -08001388 if (!count)
1389 return -EINVAL;
1390
Shrenuj Bansala419c792016-10-20 14:05:11 -07001391 ret = _check_context_state(&drawctxt->base);
1392 if (ret)
1393 return ret;
1394
1395 ret = _verify_cmdobj(dev_priv, context, drawobj, count);
1396 if (ret)
1397 return ret;
1398
1399 /* wait for the suspend gate */
1400 wait_for_completion(&device->halt_gate);
1401
1402 spin_lock(&drawctxt->lock);
1403
1404 ret = _check_context_state_to_queue_cmds(drawctxt);
1405 if (ret) {
1406 spin_unlock(&drawctxt->lock);
1407 return ret;
1408 }
1409
1410 user_ts = *timestamp;
1411
1412 for (i = 0; i < count; i++) {
1413
1414 switch (drawobj[i]->type) {
1415 case MARKEROBJ_TYPE:
1416 ret = _queue_markerobj(adreno_dev, drawctxt,
1417 CMDOBJ(drawobj[i]),
1418 timestamp, user_ts);
1419 if (ret == 1) {
1420 spin_unlock(&drawctxt->lock);
1421 goto done;
1422 } else if (ret) {
1423 spin_unlock(&drawctxt->lock);
1424 return ret;
1425 }
1426 break;
1427 case CMDOBJ_TYPE:
1428 ret = _queue_cmdobj(adreno_dev, drawctxt,
1429 CMDOBJ(drawobj[i]),
1430 timestamp, user_ts);
1431 if (ret) {
1432 spin_unlock(&drawctxt->lock);
1433 return ret;
1434 }
1435 break;
1436 case SYNCOBJ_TYPE:
1437 _queue_syncobj(drawctxt, SYNCOBJ(drawobj[i]),
1438 timestamp);
1439 break;
Tarun Karra2b8b3632016-11-14 16:38:27 -08001440 case SPARSEOBJ_TYPE:
1441 ret = _queue_sparseobj(adreno_dev, drawctxt,
1442 SPARSEOBJ(drawobj[i]),
1443 timestamp, user_ts);
1444 if (ret == 1) {
1445 spin_unlock(&drawctxt->lock);
1446 _retire_sparseobj(SPARSEOBJ(drawobj[i]),
1447 drawctxt);
1448 return 0;
1449 } else if (ret) {
1450 spin_unlock(&drawctxt->lock);
1451 return ret;
1452 }
1453 break;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001454 default:
1455 spin_unlock(&drawctxt->lock);
1456 return -EINVAL;
1457 }
1458
1459 }
1460
1461 dispatch_q = ADRENO_DRAWOBJ_DISPATCH_DRAWQUEUE(drawobj[0]);
1462
1463 _track_context(adreno_dev, dispatch_q, drawctxt);
1464
1465 spin_unlock(&drawctxt->lock);
1466
Gaurav Sonwanic169c322017-06-15 14:11:23 +05301467 if (device->pwrctrl.l2pc_update_queue)
1468 kgsl_pwrctrl_update_l2pc(&adreno_dev->dev,
1469 KGSL_L2PC_QUEUE_TIMEOUT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001470
1471 /* Add the context to the dispatcher pending list */
1472 dispatcher_queue_context(adreno_dev, drawctxt);
1473
1474 /*
1475 * Only issue commands if inflight is less than burst -this prevents us
1476 * from sitting around waiting for the mutex on a busy system - the work
1477 * loop will schedule it for us. Inflight is mutex protected but the
1478 * worse that can happen is that it will go to 0 after we check and if
1479 * it goes to 0 it is because the work loop decremented it and the work
1480 * queue will try to schedule new commands anyway.
1481 */
1482
1483 if (dispatch_q->inflight < _context_drawobj_burst)
1484 adreno_dispatcher_issuecmds(adreno_dev);
1485done:
1486 if (test_and_clear_bit(ADRENO_CONTEXT_FAULT, &context->priv))
1487 return -EPROTO;
1488
1489 return 0;
1490}
1491
1492static int _mark_context(int id, void *ptr, void *data)
1493{
1494 unsigned int guilty = *((unsigned int *) data);
1495 struct kgsl_context *context = ptr;
1496
1497 /*
1498 * If the context is guilty mark it as such. Otherwise mark it as
1499 * innocent if it had not already been marked as guilty. If id is
1500 * passed as 0 then mark EVERYBODY guilty (recovery failed)
1501 */
1502
1503 if (guilty == 0 || guilty == context->id)
1504 context->reset_status =
1505 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1506 else if (context->reset_status !=
1507 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT)
1508 context->reset_status =
1509 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1510
1511 return 0;
1512}
1513
1514/**
1515 * mark_guilty_context() - Mark the given context as guilty (failed recovery)
1516 * @device: Pointer to a KGSL device structure
1517 * @id: Context ID of the guilty context (or 0 to mark all as guilty)
1518 *
1519 * Mark the given (or all) context(s) as guilty (failed recovery)
1520 */
1521static void mark_guilty_context(struct kgsl_device *device, unsigned int id)
1522{
1523 /* Mark the status for all the contexts in the device */
1524
1525 read_lock(&device->context_lock);
1526 idr_for_each(&device->context_idr, _mark_context, &id);
1527 read_unlock(&device->context_lock);
1528}
1529
1530/*
1531 * If an IB inside of the drawobj has a gpuaddr that matches the base
1532 * passed in then zero the size which effectively skips it when it is submitted
1533 * in the ringbuffer.
1534 */
1535static void _skip_ib(struct kgsl_drawobj_cmd *cmdobj, uint64_t base)
1536{
1537 struct kgsl_memobj_node *ib;
1538
1539 list_for_each_entry(ib, &cmdobj->cmdlist, node) {
1540 if (ib->gpuaddr == base) {
1541 ib->priv |= MEMOBJ_SKIP;
1542 if (base)
1543 return;
1544 }
1545 }
1546}
1547
1548static void _skip_cmd(struct kgsl_drawobj_cmd *cmdobj,
1549 struct kgsl_drawobj_cmd **replay, int count)
1550{
1551 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1552 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
1553 int i;
1554
1555 /*
1556 * SKIPCMD policy: next IB issued for this context is tentative
1557 * if it fails we assume that GFT failed and if it succeeds
1558 * we mark GFT as a success.
1559 *
1560 * Find next commandbatch for the faulting context
1561 * If commandbatch is found
1562 * a) store the current commandbatch fault_policy in context's next
1563 * commandbatch fault_policy
1564 * b) force preamble for next commandbatch
1565 */
1566 for (i = 1; i < count; i++) {
1567 if (DRAWOBJ(replay[i])->context->id == drawobj->context->id) {
1568 replay[i]->fault_policy = replay[0]->fault_policy;
1569 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
1570 set_bit(KGSL_FT_SKIPCMD, &replay[i]->fault_recovery);
1571 break;
1572 }
1573 }
1574
1575 /*
1576 * If we did not find the next cmd then
1577 * a) set a flag for next command issued in this context
1578 * b) store the fault_policy, this fault_policy becomes the policy of
1579 * next command issued in this context
1580 */
1581 if ((i == count) && drawctxt) {
1582 set_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
1583 drawctxt->fault_policy = replay[0]->fault_policy;
1584 }
1585
1586 /* set the flags to skip this cmdobj */
1587 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1588 cmdobj->fault_recovery = 0;
1589}
1590
1591static void _skip_frame(struct kgsl_drawobj_cmd *cmdobj,
1592 struct kgsl_drawobj_cmd **replay, int count)
1593{
1594 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1595 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
1596 int skip = 1;
1597 int i;
1598
1599 for (i = 0; i < count; i++) {
1600
1601 struct kgsl_drawobj *replay_obj = DRAWOBJ(replay[i]);
1602
1603 /*
1604 * Only operate on drawobj's that belong to the
1605 * faulting context
1606 */
1607
1608 if (replay_obj->context->id != drawobj->context->id)
1609 continue;
1610
1611 /*
1612 * Skip all the drawobjs in this context until
1613 * the EOF flag is seen. If the EOF flag is seen then
1614 * force the preamble for the next command.
1615 */
1616
1617 if (skip) {
1618 set_bit(CMDOBJ_SKIP, &replay[i]->priv);
1619
1620 if (replay_obj->flags & KGSL_DRAWOBJ_END_OF_FRAME)
1621 skip = 0;
1622 } else {
1623 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
1624 return;
1625 }
1626 }
1627
1628 /*
1629 * If the EOF flag hasn't been seen yet then set the flag in the
1630 * drawctxt to keep looking for it
1631 */
1632
1633 if (skip && drawctxt)
1634 set_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv);
1635
1636 /*
1637 * If we did see the EOF flag then force the preamble on for the
1638 * next command issued on this context
1639 */
1640
1641 if (!skip && drawctxt)
1642 set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE, &drawctxt->base.priv);
1643}
1644
1645static void remove_invalidated_cmdobjs(struct kgsl_device *device,
1646 struct kgsl_drawobj_cmd **replay, int count)
1647{
1648 int i;
1649
1650 for (i = 0; i < count; i++) {
1651 struct kgsl_drawobj_cmd *cmdobj = replay[i];
1652 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1653
1654 if (cmdobj == NULL)
1655 continue;
1656
1657 if (kgsl_context_detached(drawobj->context) ||
1658 kgsl_context_invalid(drawobj->context)) {
1659 replay[i] = NULL;
1660
1661 mutex_lock(&device->mutex);
1662 kgsl_cancel_events_timestamp(device,
1663 &drawobj->context->events, drawobj->timestamp);
1664 mutex_unlock(&device->mutex);
1665
1666 kgsl_drawobj_destroy(drawobj);
1667 }
1668 }
1669}
1670
1671static char _pidname[TASK_COMM_LEN];
1672
1673static inline const char *_kgsl_context_comm(struct kgsl_context *context)
1674{
1675 if (context && context->proc_priv)
1676 strlcpy(_pidname, context->proc_priv->comm, sizeof(_pidname));
1677 else
1678 snprintf(_pidname, TASK_COMM_LEN, "unknown");
1679
1680 return _pidname;
1681}
1682
1683#define pr_fault(_d, _c, fmt, args...) \
1684 dev_err((_d)->dev, "%s[%d]: " fmt, \
1685 _kgsl_context_comm((_c)->context), \
1686 (_c)->context->proc_priv->pid, ##args)
1687
1688
1689static void adreno_fault_header(struct kgsl_device *device,
1690 struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj)
1691{
1692 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1693 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1694 unsigned int status, rptr, wptr, ib1sz, ib2sz;
1695 uint64_t ib1base, ib2base;
1696
1697 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &status);
1698 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
1699 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_WPTR, &wptr);
1700 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
1701 ADRENO_REG_CP_IB1_BASE_HI, &ib1base);
1702 adreno_readreg(adreno_dev, ADRENO_REG_CP_IB1_BUFSZ, &ib1sz);
1703 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB2_BASE,
1704 ADRENO_REG_CP_IB2_BASE_HI, &ib2base);
1705 adreno_readreg(adreno_dev, ADRENO_REG_CP_IB2_BUFSZ, &ib2sz);
1706
1707 if (drawobj != NULL) {
1708 struct adreno_context *drawctxt =
1709 ADRENO_CONTEXT(drawobj->context);
1710
1711 trace_adreno_gpu_fault(drawobj->context->id,
1712 drawobj->timestamp,
1713 status, rptr, wptr, ib1base, ib1sz,
1714 ib2base, ib2sz, drawctxt->rb->id);
1715
1716 pr_fault(device, drawobj,
1717 "gpu fault ctx %d ts %d status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
1718 drawobj->context->id, drawobj->timestamp, status,
1719 rptr, wptr, ib1base, ib1sz, ib2base, ib2sz);
1720
1721 if (rb != NULL)
1722 pr_fault(device, drawobj,
1723 "gpu fault rb %d rb sw r/w %4.4x/%4.4x\n",
1724 rb->id, rptr, rb->wptr);
1725 } else {
1726 int id = (rb != NULL) ? rb->id : -1;
1727
1728 dev_err(device->dev,
1729 "RB[%d]: gpu fault status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
1730 id, status, rptr, wptr, ib1base, ib1sz, ib2base,
1731 ib2sz);
1732 if (rb != NULL)
1733 dev_err(device->dev,
1734 "RB[%d] gpu fault rb sw r/w %4.4x/%4.4x\n",
1735 rb->id, rptr, rb->wptr);
1736 }
1737}
1738
1739void adreno_fault_skipcmd_detached(struct adreno_device *adreno_dev,
1740 struct adreno_context *drawctxt,
1741 struct kgsl_drawobj *drawobj)
1742{
1743 if (test_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv) &&
1744 kgsl_context_detached(&drawctxt->base)) {
1745 pr_context(KGSL_DEVICE(adreno_dev), drawobj->context,
1746 "gpu detached context %d\n", drawobj->context->id);
1747 clear_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
1748 }
1749}
1750
1751/**
1752 * process_cmdobj_fault() - Process a cmdobj for fault policies
1753 * @device: Device on which the cmdobj caused a fault
1754 * @replay: List of cmdobj's that are to be replayed on the device. The
1755 * first command in the replay list is the faulting command and the remaining
1756 * cmdobj's in the list are commands that were submitted to the same queue
1757 * as the faulting one.
1758 * @count: Number of cmdobj's in replay
1759 * @base: The IB1 base at the time of fault
1760 * @fault: The fault type
1761 */
1762static void process_cmdobj_fault(struct kgsl_device *device,
1763 struct kgsl_drawobj_cmd **replay, int count,
1764 unsigned int base,
1765 int fault)
1766{
1767 struct kgsl_drawobj_cmd *cmdobj = replay[0];
1768 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1769 int i;
1770 char *state = "failed";
1771
1772 /*
1773 * If GFT recovered more than X times in Y ms invalidate the context
1774 * and do not attempt recovery.
1775 * Example: X==3 and Y==3000 ms, GPU hung at 500ms, 1700ms, 25000ms and
1776 * 3000ms for the same context, we will not try FT and invalidate the
1777 * context @3000ms because context triggered GFT more than 3 times in
1778 * last 3 seconds. If a context caused recoverable GPU hangs
1779 * where 1st and 4th gpu hang are more than 3 seconds apart we
1780 * won't disable GFT and invalidate the context.
1781 */
1782 if (test_bit(KGSL_FT_THROTTLE, &cmdobj->fault_policy)) {
1783 if (time_after(jiffies, (drawobj->context->fault_time
1784 + msecs_to_jiffies(_fault_throttle_time)))) {
1785 drawobj->context->fault_time = jiffies;
1786 drawobj->context->fault_count = 1;
1787 } else {
1788 drawobj->context->fault_count++;
1789 if (drawobj->context->fault_count >
1790 _fault_throttle_burst) {
1791 set_bit(KGSL_FT_DISABLE,
1792 &cmdobj->fault_policy);
1793 pr_context(device, drawobj->context,
1794 "gpu fault threshold exceeded %d faults in %d msecs\n",
1795 _fault_throttle_burst,
1796 _fault_throttle_time);
1797 }
1798 }
1799 }
1800
1801 /*
1802 * If FT is disabled for this cmdobj invalidate immediately
1803 */
1804
1805 if (test_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy) ||
1806 test_bit(KGSL_FT_TEMP_DISABLE, &cmdobj->fault_policy)) {
1807 state = "skipped";
1808 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1809 }
1810
1811 /* If the context is detached do not run FT on context */
1812 if (kgsl_context_detached(drawobj->context)) {
1813 state = "detached";
1814 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1815 }
1816
1817 /*
1818 * Set a flag so we don't print another PM dump if the cmdobj fails
1819 * again on replay
1820 */
1821
1822 set_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy);
1823
1824 /*
1825 * A hardware fault generally means something was deterministically
1826 * wrong with the cmdobj - no point in trying to replay it
1827 * Clear the replay bit and move on to the next policy level
1828 */
1829
1830 if (fault & ADRENO_HARD_FAULT)
1831 clear_bit(KGSL_FT_REPLAY, &(cmdobj->fault_policy));
1832
1833 /*
1834 * A timeout fault means the IB timed out - clear the policy and
1835 * invalidate - this will clear the FT_SKIP_PMDUMP bit but that is okay
1836 * because we won't see this cmdobj again
1837 */
1838
1839 if (fault & ADRENO_TIMEOUT_FAULT)
1840 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1841
1842 /*
1843 * If the context had a GPU page fault then it is likely it would fault
1844 * again if replayed
1845 */
1846
1847 if (test_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
1848 &drawobj->context->priv)) {
1849 /* we'll need to resume the mmu later... */
1850 clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
1851 clear_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
1852 &drawobj->context->priv);
1853 }
1854
1855 /*
1856 * Execute the fault tolerance policy. Each cmdobj stores the
1857 * current fault policy that was set when it was queued.
1858 * As the options are tried in descending priority
1859 * (REPLAY -> SKIPIBS -> SKIPFRAME -> NOTHING) the bits are cleared
1860 * from the cmdobj policy so the next thing can be tried if the
1861 * change comes around again
1862 */
1863
1864 /* Replay the hanging cmdobj again */
1865 if (test_and_clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy)) {
1866 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_REPLAY));
1867 set_bit(KGSL_FT_REPLAY, &cmdobj->fault_recovery);
1868 return;
1869 }
1870
1871 /*
1872 * Skip the last IB1 that was played but replay everything else.
1873 * Note that the last IB1 might not be in the "hung" cmdobj
1874 * because the CP may have caused a page-fault while it was prefetching
1875 * the next IB1/IB2. walk all outstanding commands and zap the
1876 * supposedly bad IB1 where ever it lurks.
1877 */
1878
1879 if (test_and_clear_bit(KGSL_FT_SKIPIB, &cmdobj->fault_policy)) {
1880 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPIB));
1881 set_bit(KGSL_FT_SKIPIB, &cmdobj->fault_recovery);
1882
1883 for (i = 0; i < count; i++) {
1884 if (replay[i] != NULL &&
1885 DRAWOBJ(replay[i])->context->id ==
1886 drawobj->context->id)
1887 _skip_ib(replay[i], base);
1888 }
1889
1890 return;
1891 }
1892
1893 /* Skip the faulted cmdobj submission */
1894 if (test_and_clear_bit(KGSL_FT_SKIPCMD, &cmdobj->fault_policy)) {
1895 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPCMD));
1896
1897 /* Skip faulting cmdobj */
1898 _skip_cmd(cmdobj, replay, count);
1899
1900 return;
1901 }
1902
1903 if (test_and_clear_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_policy)) {
1904 trace_adreno_cmdbatch_recovery(cmdobj,
1905 BIT(KGSL_FT_SKIPFRAME));
1906 set_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_recovery);
1907
1908 /*
1909 * Skip all the pending cmdobj's for this context until
1910 * the EOF frame is seen
1911 */
1912 _skip_frame(cmdobj, replay, count);
1913 return;
1914 }
1915
1916 /* If we get here then all the policies failed */
1917
1918 pr_context(device, drawobj->context, "gpu %s ctx %d ts %d\n",
1919 state, drawobj->context->id, drawobj->timestamp);
1920
1921 /* Mark the context as failed */
1922 mark_guilty_context(device, drawobj->context->id);
1923
1924 /* Invalidate the context */
1925 adreno_drawctxt_invalidate(device, drawobj->context);
1926}
1927
1928/**
1929 * recover_dispatch_q() - Recover all commands in a dispatch queue by
1930 * resubmitting the commands
1931 * @device: Device on which recovery is performed
1932 * @dispatch_q: The command queue to recover
1933 * @fault: Faults caused by the command in the dispatch q
1934 * @base: The IB1 base during the fault
1935 */
1936static void recover_dispatch_q(struct kgsl_device *device,
1937 struct adreno_dispatcher_drawqueue *dispatch_q,
1938 int fault,
1939 unsigned int base)
1940{
1941 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1942 struct kgsl_drawobj_cmd **replay;
1943 unsigned int ptr;
1944 int first = 0;
1945 int count = 0;
1946 int i;
1947
1948 /* Allocate memory to store the inflight commands */
1949 replay = kcalloc(dispatch_q->inflight, sizeof(*replay), GFP_KERNEL);
1950
1951 if (replay == NULL) {
1952 unsigned int ptr = dispatch_q->head;
1953
1954 /* Recovery failed - mark everybody on this q guilty */
1955 while (ptr != dispatch_q->tail) {
1956 struct kgsl_drawobj_cmd *cmdobj =
1957 dispatch_q->cmd_q[ptr];
1958 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1959
1960 mark_guilty_context(device, drawobj->context->id);
1961 adreno_drawctxt_invalidate(device, drawobj->context);
1962 kgsl_drawobj_destroy(drawobj);
1963
1964 ptr = DRAWQUEUE_NEXT(ptr,
1965 ADRENO_DISPATCH_DRAWQUEUE_SIZE);
1966 }
1967
1968 /*
1969 * Set the replay count to zero - this will ensure that the
1970 * hardware gets reset but nothing else gets played
1971 */
1972
1973 count = 0;
1974 goto replay;
1975 }
1976
1977 /* Copy the inflight cmdobj's into the temporary storage */
1978 ptr = dispatch_q->head;
1979
1980 while (ptr != dispatch_q->tail) {
1981 replay[count++] = dispatch_q->cmd_q[ptr];
1982 ptr = DRAWQUEUE_NEXT(ptr, ADRENO_DISPATCH_DRAWQUEUE_SIZE);
1983 }
1984
1985 if (fault && count)
1986 process_cmdobj_fault(device, replay,
1987 count, base, fault);
1988replay:
1989 dispatch_q->inflight = 0;
1990 dispatch_q->head = dispatch_q->tail = 0;
1991 /* Remove any pending cmdobj's that have been invalidated */
1992 remove_invalidated_cmdobjs(device, replay, count);
1993
1994 /* Replay the pending command buffers */
1995 for (i = 0; i < count; i++) {
1996
1997 int ret;
1998
1999 if (replay[i] == NULL)
2000 continue;
2001
2002 /*
2003 * Force the preamble on the first command (if applicable) to
2004 * avoid any strange stage issues
2005 */
2006
2007 if (first == 0) {
2008 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
2009 first = 1;
2010 }
2011
2012 /*
2013 * Force each cmdobj to wait for idle - this avoids weird
2014 * CP parse issues
2015 */
2016
2017 set_bit(CMDOBJ_WFI, &replay[i]->priv);
2018
2019 ret = sendcmd(adreno_dev, replay[i]);
2020
2021 /*
2022 * If sending the command fails, then try to recover by
2023 * invalidating the context
2024 */
2025
2026 if (ret) {
2027 pr_context(device, replay[i]->base.context,
2028 "gpu reset failed ctx %d ts %d\n",
2029 replay[i]->base.context->id,
2030 replay[i]->base.timestamp);
2031
2032 /* Mark this context as guilty (failed recovery) */
2033 mark_guilty_context(device,
2034 replay[i]->base.context->id);
2035
2036 adreno_drawctxt_invalidate(device,
2037 replay[i]->base.context);
2038 remove_invalidated_cmdobjs(device, &replay[i],
2039 count - i);
2040 }
2041 }
2042
2043 /* Clear the fault bit */
2044 clear_bit(ADRENO_DEVICE_FAULT, &adreno_dev->priv);
2045
2046 kfree(replay);
2047}
2048
2049static void do_header_and_snapshot(struct kgsl_device *device,
2050 struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj)
2051{
2052 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2053
2054 /* Always dump the snapshot on a non-drawobj failure */
2055 if (cmdobj == NULL) {
2056 adreno_fault_header(device, rb, NULL);
2057 kgsl_device_snapshot(device, NULL);
2058 return;
2059 }
2060
2061 /* Skip everything if the PMDUMP flag is set */
2062 if (test_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy))
2063 return;
2064
2065 /* Print the fault header */
2066 adreno_fault_header(device, rb, cmdobj);
2067
2068 if (!(drawobj->context->flags & KGSL_CONTEXT_NO_SNAPSHOT))
2069 kgsl_device_snapshot(device, drawobj->context);
2070}
2071
2072static int dispatcher_do_fault(struct adreno_device *adreno_dev)
2073{
2074 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
Carter Cooperdf7ba702017-03-20 11:28:04 -06002075 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002076 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2077 struct adreno_dispatcher_drawqueue *dispatch_q = NULL, *dispatch_q_temp;
2078 struct adreno_ringbuffer *rb;
2079 struct adreno_ringbuffer *hung_rb = NULL;
2080 unsigned int reg;
2081 uint64_t base;
2082 struct kgsl_drawobj_cmd *cmdobj = NULL;
2083 int ret, i;
2084 int fault;
2085 int halt;
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002086 bool gx_on = true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002087
2088 fault = atomic_xchg(&dispatcher->fault, 0);
2089 if (fault == 0)
2090 return 0;
2091
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002092 /* Mask all GMU interrupts */
2093 if (kgsl_gmu_isenabled(device)) {
2094 adreno_write_gmureg(adreno_dev,
2095 ADRENO_REG_GMU_AO_HOST_INTERRUPT_MASK,
2096 0xFFFFFFFF);
2097 adreno_write_gmureg(adreno_dev,
2098 ADRENO_REG_GMU_GMU2HOST_INTR_MASK,
2099 0xFFFFFFFF);
2100 }
2101
2102 if (gpudev->gx_is_on)
2103 gx_on = gpudev->gx_is_on(adreno_dev);
2104
Shrenuj Bansala419c792016-10-20 14:05:11 -07002105 /*
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302106 * In the very unlikely case that the power is off, do nothing - the
2107 * state will be reset on power up and everybody will be happy
2108 */
2109
2110 if (!kgsl_state_is_awake(device) && (fault & ADRENO_SOFT_FAULT)) {
2111 /* Clear the existing register values */
2112 memset(adreno_ft_regs_val, 0,
2113 adreno_ft_regs_num * sizeof(unsigned int));
2114 return 0;
2115 }
2116
2117 /*
Lynus Vaz84020732017-03-27 18:12:24 +05302118 * On A5xx and A6xx, read RBBM_STATUS3:SMMU_STALLED_ON_FAULT (BIT 24)
2119 * to tell if this function was entered after a pagefault. If so, only
Shrenuj Bansala419c792016-10-20 14:05:11 -07002120 * proceed if the fault handler has already run in the IRQ thread,
2121 * else return early to give the fault handler a chance to run.
2122 */
Lynus Vaz84020732017-03-27 18:12:24 +05302123 if (!(fault & ADRENO_IOMMU_PAGE_FAULT) &&
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002124 (adreno_is_a5xx(adreno_dev) || adreno_is_a6xx(adreno_dev)) &&
2125 gx_on) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002126 unsigned int val;
2127
2128 mutex_lock(&device->mutex);
2129 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS3, &val);
2130 mutex_unlock(&device->mutex);
2131 if (val & BIT(24))
2132 return 0;
2133 }
2134
2135 /* Turn off all the timers */
2136 del_timer_sync(&dispatcher->timer);
2137 del_timer_sync(&dispatcher->fault_timer);
Wenbin Wang93615132017-03-30 14:10:19 +08002138 /*
2139 * Deleting uninitialized timer will block for ever on kernel debug
2140 * disable build. Hence skip del timer if it is not initialized.
2141 */
2142 if (adreno_is_preemption_enabled(adreno_dev))
2143 del_timer_sync(&adreno_dev->preempt.timer);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002144
2145 mutex_lock(&device->mutex);
2146
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002147 if (gx_on)
2148 adreno_readreg64(adreno_dev, ADRENO_REG_CP_RB_BASE,
2149 ADRENO_REG_CP_RB_BASE_HI, &base);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002150
2151 /*
2152 * Force the CP off for anything but a hard fault to make sure it is
2153 * good and stopped
2154 */
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002155 if (!(fault & ADRENO_HARD_FAULT) && gx_on) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002156 adreno_readreg(adreno_dev, ADRENO_REG_CP_ME_CNTL, &reg);
Lynus Vaz84020732017-03-27 18:12:24 +05302157 if (adreno_is_a5xx(adreno_dev) || adreno_is_a6xx(adreno_dev))
Shrenuj Bansala419c792016-10-20 14:05:11 -07002158 reg |= 1 | (1 << 1);
2159 else
2160 reg |= (1 << 27) | (1 << 28);
2161 adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_CNTL, reg);
2162 }
2163 /*
2164 * retire cmdobj's from all the dispatch_q's before starting recovery
2165 */
2166 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2167 adreno_dispatch_retire_drawqueue(adreno_dev,
2168 &(rb->dispatch_q));
2169 /* Select the active dispatch_q */
2170 if (base == rb->buffer_desc.gpuaddr) {
2171 dispatch_q = &(rb->dispatch_q);
2172 hung_rb = rb;
2173 if (adreno_dev->cur_rb != hung_rb) {
2174 adreno_dev->prev_rb = adreno_dev->cur_rb;
2175 adreno_dev->cur_rb = hung_rb;
2176 }
2177 }
2178 if (ADRENO_DISPATCHER_RB_STARVE_TIMER_ELAPSED ==
2179 rb->starve_timer_state) {
2180 adreno_put_gpu_halt(adreno_dev);
2181 rb->starve_timer_state =
2182 ADRENO_DISPATCHER_RB_STARVE_TIMER_UNINIT;
2183 }
2184 }
2185
2186 if (dispatch_q && !adreno_drawqueue_is_empty(dispatch_q)) {
2187 cmdobj = dispatch_q->cmd_q[dispatch_q->head];
2188 trace_adreno_cmdbatch_fault(cmdobj, fault);
2189 }
2190
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002191 if (gx_on)
2192 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
2193 ADRENO_REG_CP_IB1_BASE_HI, &base);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002194
2195 do_header_and_snapshot(device, hung_rb, cmdobj);
2196
Carter Cooperdf7ba702017-03-20 11:28:04 -06002197 /* Turn off the KEEPALIVE vote from the ISR for hard fault */
2198 if (gpudev->gpu_keepalive && fault & ADRENO_HARD_FAULT)
2199 gpudev->gpu_keepalive(adreno_dev, false);
2200
Shrenuj Bansala419c792016-10-20 14:05:11 -07002201 /* Terminate the stalled transaction and resume the IOMMU */
2202 if (fault & ADRENO_IOMMU_PAGE_FAULT)
2203 kgsl_mmu_pagefault_resume(&device->mmu);
2204
2205 /* Reset the dispatcher queue */
2206 dispatcher->inflight = 0;
2207
2208 /* Reset the GPU and make sure halt is not set during recovery */
2209 halt = adreno_gpu_halt(adreno_dev);
2210 adreno_clear_gpu_halt(adreno_dev);
2211
2212 /*
2213 * If there is a stall in the ringbuffer after all commands have been
2214 * retired then we could hit problems if contexts are waiting for
2215 * internal timestamps that will never retire
2216 */
2217
2218 if (hung_rb != NULL) {
2219 kgsl_sharedmem_writel(device, &device->memstore,
2220 MEMSTORE_RB_OFFSET(hung_rb, soptimestamp),
2221 hung_rb->timestamp);
2222
2223 kgsl_sharedmem_writel(device, &device->memstore,
2224 MEMSTORE_RB_OFFSET(hung_rb, eoptimestamp),
2225 hung_rb->timestamp);
2226
2227 /* Schedule any pending events to be run */
2228 kgsl_process_event_group(device, &hung_rb->events);
2229 }
2230
Shrenuj Bansald0fe7462017-05-08 16:11:19 -07002231 if (gpudev->reset)
2232 ret = gpudev->reset(device, fault);
2233 else
2234 ret = adreno_reset(device, fault);
2235
Shrenuj Bansala419c792016-10-20 14:05:11 -07002236 mutex_unlock(&device->mutex);
2237 /* if any other fault got in until reset then ignore */
2238 atomic_set(&dispatcher->fault, 0);
2239
2240 /* If adreno_reset() fails then what hope do we have for the future? */
2241 BUG_ON(ret);
2242
2243 /* recover all the dispatch_q's starting with the one that hung */
2244 if (dispatch_q)
2245 recover_dispatch_q(device, dispatch_q, fault, base);
2246 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2247 dispatch_q_temp = &(rb->dispatch_q);
2248 if (dispatch_q_temp != dispatch_q)
2249 recover_dispatch_q(device, dispatch_q_temp, 0, base);
2250 }
2251
2252 atomic_add(halt, &adreno_dev->halt);
2253
2254 return 1;
2255}
2256
2257static inline int drawobj_consumed(struct kgsl_drawobj *drawobj,
2258 unsigned int consumed, unsigned int retired)
2259{
2260 return ((timestamp_cmp(drawobj->timestamp, consumed) >= 0) &&
2261 (timestamp_cmp(retired, drawobj->timestamp) < 0));
2262}
2263
2264static void _print_recovery(struct kgsl_device *device,
2265 struct kgsl_drawobj_cmd *cmdobj)
2266{
2267 static struct {
2268 unsigned int mask;
2269 const char *str;
2270 } flags[] = { ADRENO_FT_TYPES };
2271
2272 int i, nr = find_first_bit(&cmdobj->fault_recovery, BITS_PER_LONG);
2273 char *result = "unknown";
2274 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2275
2276 for (i = 0; i < ARRAY_SIZE(flags); i++) {
2277 if (flags[i].mask == BIT(nr)) {
2278 result = (char *) flags[i].str;
2279 break;
2280 }
2281 }
2282
2283 pr_context(device, drawobj->context,
2284 "gpu %s ctx %d ts %d policy %lX\n",
2285 result, drawobj->context->id, drawobj->timestamp,
2286 cmdobj->fault_recovery);
2287}
2288
2289static void cmdobj_profile_ticks(struct adreno_device *adreno_dev,
2290 struct kgsl_drawobj_cmd *cmdobj, uint64_t *start, uint64_t *retire)
2291{
2292 void *ptr = adreno_dev->profile_buffer.hostptr;
2293 struct adreno_drawobj_profile_entry *entry;
2294
2295 entry = (struct adreno_drawobj_profile_entry *)
2296 (ptr + (cmdobj->profile_index * sizeof(*entry)));
2297
2298 /* get updated values of started and retired */
2299 rmb();
2300 *start = entry->started;
2301 *retire = entry->retired;
2302}
2303
2304static void retire_cmdobj(struct adreno_device *adreno_dev,
2305 struct kgsl_drawobj_cmd *cmdobj)
2306{
2307 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2308 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2309 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
2310 uint64_t start = 0, end = 0;
2311
2312 if (cmdobj->fault_recovery != 0) {
2313 set_bit(ADRENO_CONTEXT_FAULT, &drawobj->context->priv);
2314 _print_recovery(KGSL_DEVICE(adreno_dev), cmdobj);
2315 }
2316
2317 if (test_bit(CMDOBJ_PROFILE, &cmdobj->priv))
2318 cmdobj_profile_ticks(adreno_dev, cmdobj, &start, &end);
2319
2320 /*
2321 * For A3xx we still get the rptr from the CP_RB_RPTR instead of
2322 * rptr scratch out address. At this point GPU clocks turned off.
2323 * So avoid reading GPU register directly for A3xx.
2324 */
2325 if (adreno_is_a3xx(adreno_dev))
2326 trace_adreno_cmdbatch_retired(drawobj,
2327 (int) dispatcher->inflight, start, end,
2328 ADRENO_DRAWOBJ_RB(drawobj), 0, cmdobj->fault_recovery);
2329 else
2330 trace_adreno_cmdbatch_retired(drawobj,
2331 (int) dispatcher->inflight, start, end,
2332 ADRENO_DRAWOBJ_RB(drawobj),
2333 adreno_get_rptr(drawctxt->rb), cmdobj->fault_recovery);
2334
2335 drawctxt->submit_retire_ticks[drawctxt->ticks_index] =
2336 end - cmdobj->submit_ticks;
2337
2338 drawctxt->ticks_index = (drawctxt->ticks_index + 1) %
2339 SUBMIT_RETIRE_TICKS_SIZE;
2340
2341 kgsl_drawobj_destroy(drawobj);
2342}
2343
2344static int adreno_dispatch_retire_drawqueue(struct adreno_device *adreno_dev,
2345 struct adreno_dispatcher_drawqueue *drawqueue)
2346{
2347 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2348 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2349 int count = 0;
2350
2351 while (!adreno_drawqueue_is_empty(drawqueue)) {
2352 struct kgsl_drawobj_cmd *cmdobj =
2353 drawqueue->cmd_q[drawqueue->head];
2354 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2355
2356 if (!kgsl_check_timestamp(device, drawobj->context,
2357 drawobj->timestamp))
2358 break;
2359
2360 retire_cmdobj(adreno_dev, cmdobj);
2361
2362 dispatcher->inflight--;
2363 drawqueue->inflight--;
2364
2365 drawqueue->cmd_q[drawqueue->head] = NULL;
2366
2367 drawqueue->head = DRAWQUEUE_NEXT(drawqueue->head,
2368 ADRENO_DISPATCH_DRAWQUEUE_SIZE);
2369
2370 count++;
2371 }
2372
2373 return count;
2374}
2375
2376static void _adreno_dispatch_check_timeout(struct adreno_device *adreno_dev,
2377 struct adreno_dispatcher_drawqueue *drawqueue)
2378{
2379 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2380 struct kgsl_drawobj *drawobj =
2381 DRAWOBJ(drawqueue->cmd_q[drawqueue->head]);
2382
2383 /* Don't timeout if the timer hasn't expired yet (duh) */
2384 if (time_is_after_jiffies(drawqueue->expires))
2385 return;
2386
2387 /* Don't timeout if the IB timeout is disabled globally */
2388 if (!adreno_long_ib_detect(adreno_dev))
2389 return;
2390
2391 /* Don't time out if the context has disabled it */
2392 if (drawobj->context->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
2393 return;
2394
2395 pr_context(device, drawobj->context, "gpu timeout ctx %d ts %d\n",
2396 drawobj->context->id, drawobj->timestamp);
2397
2398 adreno_set_gpu_fault(adreno_dev, ADRENO_TIMEOUT_FAULT);
2399}
2400
2401static int adreno_dispatch_process_drawqueue(struct adreno_device *adreno_dev,
2402 struct adreno_dispatcher_drawqueue *drawqueue)
2403{
2404 int count = adreno_dispatch_retire_drawqueue(adreno_dev, drawqueue);
2405
2406 /* Nothing to do if there are no pending commands */
2407 if (adreno_drawqueue_is_empty(drawqueue))
2408 return count;
2409
Shrenuj Bansala419c792016-10-20 14:05:11 -07002410 /* Don't update the drawqueue timeout if it isn't active */
2411 if (!drawqueue_is_current(drawqueue))
2412 return count;
2413
2414 /*
2415 * If the current ringbuffer retired any commands then universally
2416 * reset the timeout
2417 */
2418
2419 if (count) {
2420 drawqueue->expires = jiffies +
2421 msecs_to_jiffies(adreno_drawobj_timeout);
2422 return count;
2423 }
2424
2425 /*
2426 * If we get here then 1) the ringbuffer is current and 2) we haven't
2427 * retired anything. Check to see if the timeout if valid for the
2428 * current drawobj and fault if it has expired
2429 */
2430 _adreno_dispatch_check_timeout(adreno_dev, drawqueue);
2431 return 0;
2432}
2433
2434/* Update the dispatcher timers */
2435static void _dispatcher_update_timers(struct adreno_device *adreno_dev)
2436{
2437 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2438 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2439
2440 /* Kick the idle timer */
2441 mutex_lock(&device->mutex);
2442 kgsl_pwrscale_update(device);
2443 mod_timer(&device->idle_timer,
2444 jiffies + device->pwrctrl.interval_timeout);
2445 mutex_unlock(&device->mutex);
2446
2447 /* Check to see if we need to update the command timer */
2448 if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
2449 struct adreno_dispatcher_drawqueue *drawqueue =
2450 DRAWQUEUE(adreno_dev->cur_rb);
2451
2452 if (!adreno_drawqueue_is_empty(drawqueue))
2453 mod_timer(&dispatcher->timer, drawqueue->expires);
2454 }
2455}
2456
2457/* Take down the dispatcher and release any power states */
2458static void _dispatcher_power_down(struct adreno_device *adreno_dev)
2459{
2460 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2461 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2462
2463 mutex_lock(&device->mutex);
2464
2465 if (test_and_clear_bit(ADRENO_DISPATCHER_ACTIVE, &dispatcher->priv))
2466 complete_all(&dispatcher->idle_gate);
2467
2468 del_timer_sync(&dispatcher->fault_timer);
2469
2470 if (test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
2471 kgsl_active_count_put(device);
2472 clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
2473 }
2474
2475 mutex_unlock(&device->mutex);
2476}
2477
Tim Murray85040432017-02-20 15:59:32 +05302478static void adreno_dispatcher_work(struct kthread_work *work)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002479{
2480 struct adreno_dispatcher *dispatcher =
2481 container_of(work, struct adreno_dispatcher, work);
2482 struct adreno_device *adreno_dev =
2483 container_of(dispatcher, struct adreno_device, dispatcher);
2484 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2485 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
2486 int count = 0;
2487 unsigned int i = 0;
2488
2489 mutex_lock(&dispatcher->mutex);
2490
2491 /*
2492 * As long as there are inflight commands, process retired comamnds from
2493 * all drawqueues
2494 */
2495 for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
2496 struct adreno_dispatcher_drawqueue *drawqueue =
2497 DRAWQUEUE(&adreno_dev->ringbuffers[i]);
2498
2499 count += adreno_dispatch_process_drawqueue(adreno_dev,
2500 drawqueue);
2501 if (dispatcher->inflight == 0)
2502 break;
2503 }
2504
2505 kgsl_process_event_groups(device);
2506
2507 /*
2508 * dispatcher_do_fault() returns 0 if no faults occurred. If that is the
2509 * case, then clean up preemption and try to schedule more work
2510 */
2511 if (dispatcher_do_fault(adreno_dev) == 0) {
2512
2513 /* Clean up after preemption */
2514 if (gpudev->preemption_schedule)
2515 gpudev->preemption_schedule(adreno_dev);
2516
2517 /* Run the scheduler for to dispatch new commands */
2518 _adreno_dispatcher_issuecmds(adreno_dev);
2519 }
2520
2521 /*
2522 * If there are commands pending, update the timers, otherwise release
2523 * the power state to prepare for power down
2524 */
2525 if (dispatcher->inflight > 0)
2526 _dispatcher_update_timers(adreno_dev);
2527 else
2528 _dispatcher_power_down(adreno_dev);
2529
2530 mutex_unlock(&dispatcher->mutex);
2531}
2532
2533void adreno_dispatcher_schedule(struct kgsl_device *device)
2534{
2535 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2536 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2537
Tim Murray85040432017-02-20 15:59:32 +05302538 kthread_queue_work(&kgsl_driver.worker, &dispatcher->work);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002539}
2540
2541/**
2542 * adreno_dispatcher_queue_context() - schedule a drawctxt in the dispatcher
2543 * device: pointer to the KGSL device
2544 * drawctxt: pointer to the drawctxt to schedule
2545 *
2546 * Put a draw context on the dispatcher pending queue and schedule the
2547 * dispatcher. This is used to reschedule changes that might have been blocked
2548 * for sync points or other concerns
2549 */
2550void adreno_dispatcher_queue_context(struct kgsl_device *device,
2551 struct adreno_context *drawctxt)
2552{
2553 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2554
2555 dispatcher_queue_context(adreno_dev, drawctxt);
2556 adreno_dispatcher_schedule(device);
2557}
2558
2559/*
2560 * This is called on a regular basis while cmdobj's are inflight. Fault
2561 * detection registers are read and compared to the existing values - if they
2562 * changed then the GPU is still running. If they are the same between
2563 * subsequent calls then the GPU may have faulted
2564 */
2565
2566static void adreno_dispatcher_fault_timer(unsigned long data)
2567{
2568 struct adreno_device *adreno_dev = (struct adreno_device *) data;
2569 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2570
2571 /* Leave if the user decided to turn off fast hang detection */
2572 if (!adreno_soft_fault_detect(adreno_dev))
2573 return;
2574
2575 if (adreno_gpu_fault(adreno_dev)) {
2576 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
2577 return;
2578 }
2579
2580 /*
2581 * Read the fault registers - if it returns 0 then they haven't changed
2582 * so mark the dispatcher as faulted and schedule the work loop.
2583 */
2584
2585 if (!fault_detect_read_compare(adreno_dev)) {
2586 adreno_set_gpu_fault(adreno_dev, ADRENO_SOFT_FAULT);
2587 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302588 } else if (dispatcher->inflight > 0) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002589 mod_timer(&dispatcher->fault_timer,
2590 jiffies + msecs_to_jiffies(_fault_timer_interval));
2591 }
2592}
2593
2594/*
2595 * This is called when the timer expires - it either means the GPU is hung or
2596 * the IB is taking too long to execute
2597 */
2598static void adreno_dispatcher_timer(unsigned long data)
2599{
2600 struct adreno_device *adreno_dev = (struct adreno_device *) data;
2601
2602 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
2603}
2604
2605/**
2606 * adreno_dispatcher_start() - activate the dispatcher
2607 * @adreno_dev: pointer to the adreno device structure
2608 *
2609 */
2610void adreno_dispatcher_start(struct kgsl_device *device)
2611{
2612 complete_all(&device->halt_gate);
2613
2614 /* Schedule the work loop to get things going */
2615 adreno_dispatcher_schedule(device);
2616}
2617
2618/**
2619 * adreno_dispatcher_stop() - stop the dispatcher
2620 * @adreno_dev: pointer to the adreno device structure
2621 *
2622 * Stop the dispatcher and close all the timers
2623 */
2624void adreno_dispatcher_stop(struct adreno_device *adreno_dev)
2625{
2626 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2627
2628 del_timer_sync(&dispatcher->timer);
2629 del_timer_sync(&dispatcher->fault_timer);
2630}
2631
2632/**
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302633 * adreno_dispatcher_stop_fault_timer() - stop the dispatcher fault timer
2634 * @device: pointer to the KGSL device structure
2635 *
2636 * Stop the dispatcher fault timer
2637 */
2638void adreno_dispatcher_stop_fault_timer(struct kgsl_device *device)
2639{
2640 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2641 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2642
2643 del_timer_sync(&dispatcher->fault_timer);
2644}
2645
2646/**
Shrenuj Bansala419c792016-10-20 14:05:11 -07002647 * adreno_dispatcher_close() - close the dispatcher
2648 * @adreno_dev: pointer to the adreno device structure
2649 *
2650 * Close the dispatcher and free all the oustanding commands and memory
2651 */
2652void adreno_dispatcher_close(struct adreno_device *adreno_dev)
2653{
2654 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2655 int i;
2656 struct adreno_ringbuffer *rb;
2657
2658 mutex_lock(&dispatcher->mutex);
2659 del_timer_sync(&dispatcher->timer);
2660 del_timer_sync(&dispatcher->fault_timer);
2661
2662 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2663 struct adreno_dispatcher_drawqueue *dispatch_q =
2664 &(rb->dispatch_q);
2665 while (!adreno_drawqueue_is_empty(dispatch_q)) {
2666 kgsl_drawobj_destroy(
2667 DRAWOBJ(dispatch_q->cmd_q[dispatch_q->head]));
2668 dispatch_q->head = (dispatch_q->head + 1)
2669 % ADRENO_DISPATCH_DRAWQUEUE_SIZE;
2670 }
2671 }
2672
2673 mutex_unlock(&dispatcher->mutex);
2674
2675 kobject_put(&dispatcher->kobj);
2676}
2677
2678struct dispatcher_attribute {
2679 struct attribute attr;
2680 ssize_t (*show)(struct adreno_dispatcher *,
2681 struct dispatcher_attribute *, char *);
2682 ssize_t (*store)(struct adreno_dispatcher *,
2683 struct dispatcher_attribute *, const char *buf,
2684 size_t count);
2685 unsigned int max;
2686 unsigned int *value;
2687};
2688
2689#define DISPATCHER_UINT_ATTR(_name, _mode, _max, _value) \
2690 struct dispatcher_attribute dispatcher_attr_##_name = { \
2691 .attr = { .name = __stringify(_name), .mode = _mode }, \
2692 .show = _show_uint, \
2693 .store = _store_uint, \
2694 .max = _max, \
2695 .value = &(_value), \
2696 }
2697
2698#define to_dispatcher_attr(_a) \
2699 container_of((_a), struct dispatcher_attribute, attr)
2700#define to_dispatcher(k) container_of(k, struct adreno_dispatcher, kobj)
2701
2702static ssize_t _store_uint(struct adreno_dispatcher *dispatcher,
2703 struct dispatcher_attribute *attr,
2704 const char *buf, size_t size)
2705{
2706 unsigned int val = 0;
2707 int ret;
2708
2709 ret = kgsl_sysfs_store(buf, &val);
2710 if (ret)
2711 return ret;
2712
2713 if (!val || (attr->max && (val > attr->max)))
2714 return -EINVAL;
2715
2716 *((unsigned int *) attr->value) = val;
2717 return size;
2718}
2719
2720static ssize_t _show_uint(struct adreno_dispatcher *dispatcher,
2721 struct dispatcher_attribute *attr,
2722 char *buf)
2723{
2724 return snprintf(buf, PAGE_SIZE, "%u\n",
2725 *((unsigned int *) attr->value));
2726}
2727
2728static DISPATCHER_UINT_ATTR(inflight, 0644, ADRENO_DISPATCH_DRAWQUEUE_SIZE,
2729 _dispatcher_q_inflight_hi);
2730
2731static DISPATCHER_UINT_ATTR(inflight_low_latency, 0644,
2732 ADRENO_DISPATCH_DRAWQUEUE_SIZE, _dispatcher_q_inflight_lo);
2733/*
2734 * Our code that "puts back" a command from the context is much cleaner
2735 * if we are sure that there will always be enough room in the
2736 * ringbuffer so restrict the maximum size of the context queue to
2737 * ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1
2738 */
2739static DISPATCHER_UINT_ATTR(context_drawqueue_size, 0644,
2740 ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1, _context_drawqueue_size);
2741static DISPATCHER_UINT_ATTR(context_burst_count, 0644, 0,
2742 _context_drawobj_burst);
2743static DISPATCHER_UINT_ATTR(drawobj_timeout, 0644, 0,
2744 adreno_drawobj_timeout);
2745static DISPATCHER_UINT_ATTR(context_queue_wait, 0644, 0, _context_queue_wait);
2746static DISPATCHER_UINT_ATTR(fault_detect_interval, 0644, 0,
2747 _fault_timer_interval);
2748static DISPATCHER_UINT_ATTR(fault_throttle_time, 0644, 0,
2749 _fault_throttle_time);
2750static DISPATCHER_UINT_ATTR(fault_throttle_burst, 0644, 0,
2751 _fault_throttle_burst);
2752static DISPATCHER_UINT_ATTR(disp_preempt_fair_sched, 0644, 0,
2753 adreno_disp_preempt_fair_sched);
2754static DISPATCHER_UINT_ATTR(dispatch_time_slice, 0644, 0,
2755 adreno_dispatch_time_slice);
2756static DISPATCHER_UINT_ATTR(dispatch_starvation_time, 0644, 0,
2757 adreno_dispatch_starvation_time);
2758
2759static struct attribute *dispatcher_attrs[] = {
2760 &dispatcher_attr_inflight.attr,
2761 &dispatcher_attr_inflight_low_latency.attr,
2762 &dispatcher_attr_context_drawqueue_size.attr,
2763 &dispatcher_attr_context_burst_count.attr,
2764 &dispatcher_attr_drawobj_timeout.attr,
2765 &dispatcher_attr_context_queue_wait.attr,
2766 &dispatcher_attr_fault_detect_interval.attr,
2767 &dispatcher_attr_fault_throttle_time.attr,
2768 &dispatcher_attr_fault_throttle_burst.attr,
2769 &dispatcher_attr_disp_preempt_fair_sched.attr,
2770 &dispatcher_attr_dispatch_time_slice.attr,
2771 &dispatcher_attr_dispatch_starvation_time.attr,
2772 NULL,
2773};
2774
2775static ssize_t dispatcher_sysfs_show(struct kobject *kobj,
2776 struct attribute *attr, char *buf)
2777{
2778 struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
2779 struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
2780 ssize_t ret = -EIO;
2781
2782 if (pattr->show)
2783 ret = pattr->show(dispatcher, pattr, buf);
2784
2785 return ret;
2786}
2787
2788static ssize_t dispatcher_sysfs_store(struct kobject *kobj,
2789 struct attribute *attr,
2790 const char *buf, size_t count)
2791{
2792 struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
2793 struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
2794 ssize_t ret = -EIO;
2795
2796 if (pattr->store)
2797 ret = pattr->store(dispatcher, pattr, buf, count);
2798
2799 return ret;
2800}
2801
2802static const struct sysfs_ops dispatcher_sysfs_ops = {
2803 .show = dispatcher_sysfs_show,
2804 .store = dispatcher_sysfs_store
2805};
2806
2807static struct kobj_type ktype_dispatcher = {
2808 .sysfs_ops = &dispatcher_sysfs_ops,
2809 .default_attrs = dispatcher_attrs,
2810};
2811
2812/**
2813 * adreno_dispatcher_init() - Initialize the dispatcher
2814 * @adreno_dev: pointer to the adreno device structure
2815 *
2816 * Initialize the dispatcher
2817 */
2818int adreno_dispatcher_init(struct adreno_device *adreno_dev)
2819{
2820 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2821 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2822 int ret;
2823
2824 memset(dispatcher, 0, sizeof(*dispatcher));
2825
2826 mutex_init(&dispatcher->mutex);
2827
2828 setup_timer(&dispatcher->timer, adreno_dispatcher_timer,
2829 (unsigned long) adreno_dev);
2830
2831 setup_timer(&dispatcher->fault_timer, adreno_dispatcher_fault_timer,
2832 (unsigned long) adreno_dev);
2833
Tim Murray85040432017-02-20 15:59:32 +05302834 kthread_init_work(&dispatcher->work, adreno_dispatcher_work);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002835
2836 init_completion(&dispatcher->idle_gate);
2837 complete_all(&dispatcher->idle_gate);
2838
2839 plist_head_init(&dispatcher->pending);
2840 spin_lock_init(&dispatcher->plist_lock);
2841
2842 ret = kobject_init_and_add(&dispatcher->kobj, &ktype_dispatcher,
2843 &device->dev->kobj, "dispatch");
2844
2845 return ret;
2846}
2847
2848/*
2849 * adreno_dispatcher_idle() - Wait for dispatcher to idle
2850 * @adreno_dev: Adreno device whose dispatcher needs to idle
2851 *
2852 * Signal dispatcher to stop sending more commands and complete
2853 * the commands that have already been submitted. This function
2854 * should not be called when dispatcher mutex is held.
2855 * The caller must hold the device mutex.
2856 */
2857int adreno_dispatcher_idle(struct adreno_device *adreno_dev)
2858{
2859 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2860 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2861 int ret;
2862
2863 if (!test_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv))
2864 return 0;
2865
2866 /*
2867 * Ensure that this function is not called when dispatcher
2868 * mutex is held and device is started
2869 */
2870 if (mutex_is_locked(&dispatcher->mutex) &&
2871 dispatcher->mutex.owner == current)
2872 return -EDEADLK;
2873
2874 adreno_get_gpu_halt(adreno_dev);
2875
2876 mutex_unlock(&device->mutex);
2877
2878 ret = wait_for_completion_timeout(&dispatcher->idle_gate,
2879 msecs_to_jiffies(ADRENO_IDLE_TIMEOUT));
2880 if (ret == 0) {
2881 ret = -ETIMEDOUT;
2882 WARN(1, "Dispatcher halt timeout ");
2883 } else if (ret < 0) {
2884 KGSL_DRV_ERR(device, "Dispatcher halt failed %d\n", ret);
2885 } else {
2886 ret = 0;
2887 }
2888
2889 mutex_lock(&device->mutex);
2890 adreno_put_gpu_halt(adreno_dev);
2891 /*
2892 * requeue dispatcher work to resubmit pending commands
2893 * that may have been blocked due to this idling request
2894 */
2895 adreno_dispatcher_schedule(device);
2896 return ret;
2897}