blob: d3d9675375d8da5dc1a0af1a22eb3d11194643b3 [file] [log] [blame]
Harshitha Sai Neelatib29aa132020-09-25 15:14:37 +05301/* Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
Shrenuj Bansala419c792016-10-20 14:05:11 -07002 *
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
Venkateswara Rao Tadikonda435bad32018-05-09 16:21:34 +0530211 if (!adreno_rb_empty(adreno_dev->cur_rb))
212 return false;
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +0530213
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
Harshdeep Dhatt358ad222018-02-02 11:48:39 -0700568 memset(&time, 0x0, sizeof(time));
569
Shrenuj Bansala419c792016-10-20 14:05:11 -0700570 dispatcher->inflight++;
571 dispatch_q->inflight++;
572
573 if (dispatcher->inflight == 1 &&
574 !test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
575 /* Time to make the donuts. Turn on the GPU */
576 ret = kgsl_active_count_get(device);
577 if (ret) {
578 dispatcher->inflight--;
579 dispatch_q->inflight--;
580 mutex_unlock(&device->mutex);
581 return ret;
582 }
583
584 set_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
585 }
586
587 if (test_bit(ADRENO_DEVICE_DRAWOBJ_PROFILE, &adreno_dev->priv)) {
588 set_bit(CMDOBJ_PROFILE, &cmdobj->priv);
589 cmdobj->profile_index = adreno_dev->profile_index;
590 adreno_dev->profile_index =
591 (adreno_dev->profile_index + 1) %
592 ADRENO_DRAWOBJ_PROFILE_COUNT;
593 }
594
595 ret = adreno_ringbuffer_submitcmd(adreno_dev, cmdobj, &time);
596
597 /*
598 * On the first command, if the submission was successful, then read the
599 * fault registers. If it failed then turn off the GPU. Sad face.
600 */
601
602 if (dispatcher->inflight == 1) {
603 if (ret == 0) {
604
605 /* Stop fault timer before reading fault registers */
606 del_timer_sync(&dispatcher->fault_timer);
607
608 fault_detect_read(adreno_dev);
609
610 /* Start the fault timer on first submission */
611 start_fault_timer(adreno_dev);
612
613 if (!test_and_set_bit(ADRENO_DISPATCHER_ACTIVE,
614 &dispatcher->priv))
615 reinit_completion(&dispatcher->idle_gate);
Prakash Kamliyac9b7da02016-12-20 17:45:41 +0530616
617 /*
618 * We update power stats generally at the expire of
619 * cmdbatch. In cases where the cmdbatch takes a long
620 * time to finish, it will delay power stats update,
621 * in effect it will delay DCVS decision. Start a
622 * timer to update power state on expire of this timer.
623 */
624 kgsl_pwrscale_midframe_timer_restart(device);
625
Shrenuj Bansala419c792016-10-20 14:05:11 -0700626 } else {
627 kgsl_active_count_put(device);
628 clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
629 }
630 }
631
632
633 if (ret) {
634 dispatcher->inflight--;
635 dispatch_q->inflight--;
636
637 mutex_unlock(&device->mutex);
638
639 /*
640 * Don't log a message in case of:
641 * -ENOENT means that the context was detached before the
642 * command was submitted
643 * -ENOSPC means that there temporarily isn't any room in the
644 * ringbuffer
645 * -PROTO means that a fault is currently being worked
646 */
647
648 if (ret != -ENOENT && ret != -ENOSPC && ret != -EPROTO)
649 KGSL_DRV_ERR(device,
650 "Unable to submit command to the ringbuffer %d\n",
651 ret);
652 return ret;
653 }
654
655 secs = time.ktime;
656 nsecs = do_div(secs, 1000000000);
657
658 trace_adreno_cmdbatch_submitted(drawobj, (int) dispatcher->inflight,
659 time.ticks, (unsigned long) secs, nsecs / 1000, drawctxt->rb,
660 adreno_get_rptr(drawctxt->rb));
661
662 mutex_unlock(&device->mutex);
663
664 cmdobj->submit_ticks = time.ticks;
665
666 dispatch_q->cmd_q[dispatch_q->tail] = cmdobj;
667 dispatch_q->tail = (dispatch_q->tail + 1) %
668 ADRENO_DISPATCH_DRAWQUEUE_SIZE;
669
670 /*
671 * For the first submission in any given command queue update the
672 * expected expire time - this won't actually be used / updated until
673 * the command queue in question goes current, but universally setting
674 * it here avoids the possibilty of some race conditions with preempt
675 */
676
677 if (dispatch_q->inflight == 1)
678 dispatch_q->expires = jiffies +
679 msecs_to_jiffies(adreno_drawobj_timeout);
680
681 /*
682 * If we believe ourselves to be current and preemption isn't a thing,
683 * then set up the timer. If this misses, then preemption is indeed a
684 * thing and the timer will be set up in due time
685 */
Lynus Vaz9c096da2017-10-24 15:47:44 +0530686 if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -0700687 if (drawqueue_is_current(dispatch_q))
688 mod_timer(&dispatcher->timer, dispatch_q->expires);
689 }
690
691
692 /*
693 * we just submitted something, readjust ringbuffer
694 * execution level
695 */
696 if (gpudev->preemption_schedule)
697 gpudev->preemption_schedule(adreno_dev);
698 return 0;
699}
700
Tarun Karra2b8b3632016-11-14 16:38:27 -0800701
702/*
703 * Retires all sync objs from the sparse context
704 * queue and returns one of the below
705 * a) next sparseobj
706 * b) -EAGAIN for syncobj with syncpoints pending
707 * c) -EINVAL for unexpected drawobj
708 * d) NULL for no sparseobj
709 */
710static struct kgsl_drawobj_sparse *_get_next_sparseobj(
711 struct adreno_context *drawctxt)
712{
713 struct kgsl_drawobj *drawobj;
714 unsigned int i = drawctxt->drawqueue_head;
715 int ret = 0;
716
717 if (drawctxt->drawqueue_head == drawctxt->drawqueue_tail)
718 return NULL;
719
720 for (i = drawctxt->drawqueue_head; i != drawctxt->drawqueue_tail;
721 i = DRAWQUEUE_NEXT(i, ADRENO_CONTEXT_DRAWQUEUE_SIZE)) {
722
723 drawobj = drawctxt->drawqueue[i];
724
725 if (drawobj == NULL)
726 return NULL;
727
728 if (drawobj->type == SYNCOBJ_TYPE)
729 ret = _retire_syncobj(SYNCOBJ(drawobj), drawctxt);
730 else if (drawobj->type == SPARSEOBJ_TYPE)
731 return SPARSEOBJ(drawobj);
732 else
733 return ERR_PTR(-EINVAL);
734
735 if (ret == -EAGAIN)
736 return ERR_PTR(-EAGAIN);
737
738 continue;
739 }
740
741 return NULL;
742}
743
744static int _process_drawqueue_sparse(
745 struct adreno_context *drawctxt)
746{
747 struct kgsl_drawobj_sparse *sparseobj;
748 int ret = 0;
749 unsigned int i;
750
751 for (i = 0; i < ADRENO_CONTEXT_DRAWQUEUE_SIZE; i++) {
752
753 spin_lock(&drawctxt->lock);
754 sparseobj = _get_next_sparseobj(drawctxt);
755 if (IS_ERR_OR_NULL(sparseobj)) {
756 if (IS_ERR(sparseobj))
757 ret = PTR_ERR(sparseobj);
758 spin_unlock(&drawctxt->lock);
759 return ret;
760 }
761
762 _pop_drawobj(drawctxt);
763 spin_unlock(&drawctxt->lock);
764
765 _retire_sparseobj(sparseobj, drawctxt);
766 }
767
768 return 0;
769}
770
Shrenuj Bansala419c792016-10-20 14:05:11 -0700771/**
772 * dispatcher_context_sendcmds() - Send commands from a context to the GPU
773 * @adreno_dev: Pointer to the adreno device struct
774 * @drawctxt: Pointer to the adreno context to dispatch commands from
775 *
776 * Dequeue and send a burst of commands from the specified context to the GPU
777 * Returns postive if the context needs to be put back on the pending queue
778 * 0 if the context is empty or detached and negative on error
779 */
780static int dispatcher_context_sendcmds(struct adreno_device *adreno_dev,
781 struct adreno_context *drawctxt)
782{
783 struct adreno_dispatcher_drawqueue *dispatch_q =
784 &(drawctxt->rb->dispatch_q);
785 int count = 0;
786 int ret = 0;
787 int inflight = _drawqueue_inflight(dispatch_q);
788 unsigned int timestamp;
789
Tarun Karra2b8b3632016-11-14 16:38:27 -0800790 if (drawctxt->base.flags & KGSL_CONTEXT_SPARSE)
791 return _process_drawqueue_sparse(drawctxt);
792
Shrenuj Bansala419c792016-10-20 14:05:11 -0700793 if (dispatch_q->inflight >= inflight) {
794 spin_lock(&drawctxt->lock);
795 _process_drawqueue_get_next_drawobj(drawctxt);
796 spin_unlock(&drawctxt->lock);
797 return -EBUSY;
798 }
799
800 /*
801 * Each context can send a specific number of drawobjs per cycle
802 */
803 while ((count < _context_drawobj_burst) &&
804 (dispatch_q->inflight < inflight)) {
805 struct kgsl_drawobj *drawobj;
806 struct kgsl_drawobj_cmd *cmdobj;
807
808 if (adreno_gpu_fault(adreno_dev) != 0)
809 break;
810
811 spin_lock(&drawctxt->lock);
812 drawobj = _process_drawqueue_get_next_drawobj(drawctxt);
813
814 /*
815 * adreno_context_get_drawobj returns -EAGAIN if the current
816 * drawobj has pending sync points so no more to do here.
817 * When the sync points are satisfied then the context will get
818 * reqeueued
819 */
820
821 if (IS_ERR_OR_NULL(drawobj)) {
822 if (IS_ERR(drawobj))
823 ret = PTR_ERR(drawobj);
824 spin_unlock(&drawctxt->lock);
825 break;
826 }
827 _pop_drawobj(drawctxt);
828 spin_unlock(&drawctxt->lock);
829
830 timestamp = drawobj->timestamp;
831 cmdobj = CMDOBJ(drawobj);
832 ret = sendcmd(adreno_dev, cmdobj);
833
834 /*
835 * On error from sendcmd() try to requeue the cmdobj
836 * unless we got back -ENOENT which means that the context has
837 * been detached and there will be no more deliveries from here
838 */
839 if (ret != 0) {
840 /* Destroy the cmdobj on -ENOENT */
841 if (ret == -ENOENT)
842 kgsl_drawobj_destroy(drawobj);
843 else {
844 /*
845 * If the requeue returns an error, return that
846 * instead of whatever sendcmd() sent us
847 */
848 int r = adreno_dispatcher_requeue_cmdobj(
849 drawctxt, cmdobj);
850 if (r)
851 ret = r;
852 }
853
854 break;
855 }
856
857 drawctxt->submitted_timestamp = timestamp;
858
859 count++;
860 }
861
862 /*
863 * Wake up any snoozing threads if we have consumed any real commands
864 * or marker commands and we have room in the context queue.
865 */
866
867 if (_check_context_queue(drawctxt))
868 wake_up_all(&drawctxt->wq);
869
870 if (!ret)
871 ret = count;
872
873 /* Return error or the number of commands queued */
874 return ret;
875}
876
877/**
878 * _adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
879 * @adreno_dev: Pointer to the adreno device struct
880 *
881 * Issue as many commands as possible (up to inflight) from the pending contexts
882 * This function assumes the dispatcher mutex has been locked.
883 */
884static void _adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
885{
886 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
887 struct adreno_context *drawctxt, *next;
888 struct plist_head requeue, busy_list;
889 int ret;
890
891 /* Leave early if the dispatcher isn't in a happy state */
892 if (adreno_gpu_fault(adreno_dev) != 0)
893 return;
894
895 plist_head_init(&requeue);
896 plist_head_init(&busy_list);
897
898 /* Try to fill the ringbuffers as much as possible */
899 while (1) {
900
901 /* Stop doing things if the dispatcher is paused or faulted */
902 if (adreno_gpu_fault(adreno_dev) != 0)
903 break;
904
905 if (adreno_gpu_halt(adreno_dev) != 0)
906 break;
907
908 spin_lock(&dispatcher->plist_lock);
909
910 if (plist_head_empty(&dispatcher->pending)) {
911 spin_unlock(&dispatcher->plist_lock);
912 break;
913 }
914
915 /* Get the next entry on the list */
916 drawctxt = plist_first_entry(&dispatcher->pending,
917 struct adreno_context, pending);
918
919 plist_del(&drawctxt->pending, &dispatcher->pending);
920
921 spin_unlock(&dispatcher->plist_lock);
922
923 if (kgsl_context_detached(&drawctxt->base) ||
924 kgsl_context_invalid(&drawctxt->base)) {
925 kgsl_context_put(&drawctxt->base);
926 continue;
927 }
928
929 ret = dispatcher_context_sendcmds(adreno_dev, drawctxt);
930
931 /* Don't bother requeuing on -ENOENT - context is detached */
932 if (ret != 0 && ret != -ENOENT) {
933 spin_lock(&dispatcher->plist_lock);
934
935 /*
936 * Check to seen if the context had been requeued while
937 * we were processing it (probably by another thread
938 * pushing commands). If it has then shift it to the
939 * requeue list if it was not able to submit commands
940 * due to the dispatch_q being full. Also, do a put to
941 * make sure the reference counting stays accurate.
942 * If the node is empty then we will put it on the
943 * requeue list and not touch the refcount since we
944 * already hold it from the first time it went on the
945 * list.
946 */
947
948 if (!plist_node_empty(&drawctxt->pending)) {
949 plist_del(&drawctxt->pending,
950 &dispatcher->pending);
951 kgsl_context_put(&drawctxt->base);
952 }
953
954 if (ret == -EBUSY)
955 /* Inflight queue is full */
956 plist_add(&drawctxt->pending, &busy_list);
957 else
958 plist_add(&drawctxt->pending, &requeue);
959
960 spin_unlock(&dispatcher->plist_lock);
961 } else {
962 /*
963 * If the context doesn't need be requeued put back the
964 * refcount
965 */
966
967 kgsl_context_put(&drawctxt->base);
968 }
969 }
970
971 spin_lock(&dispatcher->plist_lock);
972
973 /* Put the contexts that couldn't submit back on the pending list */
974 plist_for_each_entry_safe(drawctxt, next, &busy_list, pending) {
975 plist_del(&drawctxt->pending, &busy_list);
976 plist_add(&drawctxt->pending, &dispatcher->pending);
977 }
978
979 /* Now put the contexts that need to be requeued back on the list */
980 plist_for_each_entry_safe(drawctxt, next, &requeue, pending) {
981 plist_del(&drawctxt->pending, &requeue);
982 plist_add(&drawctxt->pending, &dispatcher->pending);
983 }
984
985 spin_unlock(&dispatcher->plist_lock);
986}
987
Hareesh Gundu2eb74d72017-06-07 14:50:15 +0530988static inline void _decrement_submit_now(struct kgsl_device *device)
989{
990 spin_lock(&device->submit_lock);
991 device->submit_now--;
992 spin_unlock(&device->submit_lock);
993}
994
Shrenuj Bansala419c792016-10-20 14:05:11 -0700995/**
996 * adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
997 * @adreno_dev: Pointer to the adreno device struct
998 *
999 * Lock the dispatcher and call _adreno_dispatcher_issueibcmds
1000 */
1001static void adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
1002{
1003 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301004 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1005
1006 spin_lock(&device->submit_lock);
1007 /* If state transition to SLUMBER, schedule the work for later */
1008 if (device->slumber == true) {
1009 spin_unlock(&device->submit_lock);
1010 goto done;
1011 }
1012 device->submit_now++;
1013 spin_unlock(&device->submit_lock);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001014
1015 /* If the dispatcher is busy then schedule the work for later */
1016 if (!mutex_trylock(&dispatcher->mutex)) {
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301017 _decrement_submit_now(device);
1018 goto done;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001019 }
1020
1021 _adreno_dispatcher_issuecmds(adreno_dev);
1022 mutex_unlock(&dispatcher->mutex);
Hareesh Gundu2eb74d72017-06-07 14:50:15 +05301023 _decrement_submit_now(device);
1024 return;
1025done:
1026 adreno_dispatcher_schedule(device);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001027}
1028
1029/**
1030 * get_timestamp() - Return the next timestamp for the context
1031 * @drawctxt - Pointer to an adreno draw context struct
1032 * @drawobj - Pointer to a drawobj
1033 * @timestamp - Pointer to a timestamp value possibly passed from the user
1034 * @user_ts - user generated timestamp
1035 *
1036 * Assign a timestamp based on the settings of the draw context and the command
1037 * batch.
1038 */
1039static int get_timestamp(struct adreno_context *drawctxt,
1040 struct kgsl_drawobj *drawobj, unsigned int *timestamp,
1041 unsigned int user_ts)
1042{
1043
1044 if (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS) {
1045 /*
1046 * User specified timestamps need to be greater than the last
1047 * issued timestamp in the context
1048 */
1049 if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0)
1050 return -ERANGE;
1051
1052 drawctxt->timestamp = user_ts;
1053 } else
1054 drawctxt->timestamp++;
1055
1056 *timestamp = drawctxt->timestamp;
1057 drawobj->timestamp = *timestamp;
1058 return 0;
1059}
1060
1061static void _set_ft_policy(struct adreno_device *adreno_dev,
1062 struct adreno_context *drawctxt,
1063 struct kgsl_drawobj_cmd *cmdobj)
1064{
1065 /*
1066 * Set the fault tolerance policy for the command batch - assuming the
1067 * context hasn't disabled FT use the current device policy
1068 */
1069 if (drawctxt->base.flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
1070 set_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy);
Hareesh Gunduccfb89b2017-04-14 18:36:20 +05301071 /*
1072 * Set the fault tolerance policy to FT_REPLAY - As context wants
1073 * to invalidate it after a replay attempt fails. This doesn't
1074 * require to execute the default FT policy.
1075 */
1076 else if (drawctxt->base.flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT)
1077 set_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001078 else
1079 cmdobj->fault_policy = adreno_dev->ft_policy;
1080}
1081
1082static void _cmdobj_set_flags(struct adreno_context *drawctxt,
1083 struct kgsl_drawobj_cmd *cmdobj)
1084{
1085 /*
1086 * Force the preamble for this submission only - this is usually
1087 * requested by the dispatcher as part of fault recovery
1088 */
1089 if (test_and_clear_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
1090 &drawctxt->base.priv))
1091 set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
1092
1093 /*
1094 * Force the premable if set from userspace in the context or
1095 * command obj flags
1096 */
1097 if ((drawctxt->base.flags & KGSL_CONTEXT_CTX_SWITCH) ||
1098 (cmdobj->base.flags & KGSL_DRAWOBJ_CTX_SWITCH))
1099 set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
1100
1101 /* Skip this ib if IFH_NOP is enabled */
1102 if (drawctxt->base.flags & KGSL_CONTEXT_IFH_NOP)
1103 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1104
1105 /*
1106 * If we are waiting for the end of frame and it hasn't appeared yet,
1107 * then mark the command obj as skipped. It will still progress
1108 * through the pipeline but it won't actually send any commands
1109 */
1110
1111 if (test_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv)) {
1112 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1113
1114 /*
1115 * If this command obj represents the EOF then clear the way
1116 * for the dispatcher to continue submitting
1117 */
1118
1119 if (cmdobj->base.flags & KGSL_DRAWOBJ_END_OF_FRAME) {
1120 clear_bit(ADRENO_CONTEXT_SKIP_EOF,
1121 &drawctxt->base.priv);
1122
1123 /*
1124 * Force the preamble on the next command to ensure that
1125 * the state is correct
1126 */
1127 set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
1128 &drawctxt->base.priv);
1129 }
1130 }
1131}
1132
1133static inline int _check_context_state(struct kgsl_context *context)
1134{
1135 if (kgsl_context_invalid(context))
1136 return -EDEADLK;
1137
1138 if (kgsl_context_detached(context))
1139 return -ENOENT;
1140
1141 return 0;
1142}
1143
1144static inline bool _verify_ib(struct kgsl_device_private *dev_priv,
1145 struct kgsl_context *context, struct kgsl_memobj_node *ib)
1146{
1147 struct kgsl_device *device = dev_priv->device;
1148 struct kgsl_process_private *private = dev_priv->process_priv;
1149
1150 /* The maximum allowable size for an IB in the CP is 0xFFFFF dwords */
1151 if (ib->size == 0 || ((ib->size >> 2) > 0xFFFFF)) {
1152 pr_context(device, context, "ctxt %d invalid ib size %lld\n",
1153 context->id, ib->size);
1154 return false;
1155 }
1156
1157 /* Make sure that the address is mapped */
Abhishek Barman4e80eb02021-12-07 12:50:34 +05301158 if (!kgsl_mmu_gpuaddr_in_range(private->pagetable, ib->gpuaddr,
1159 ib->size)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001160 pr_context(device, context, "ctxt %d invalid ib gpuaddr %llX\n",
1161 context->id, ib->gpuaddr);
1162 return false;
1163 }
1164
1165 return true;
1166}
1167
1168static inline int _verify_cmdobj(struct kgsl_device_private *dev_priv,
1169 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
1170 uint32_t count)
1171{
1172 struct kgsl_device *device = dev_priv->device;
1173 struct kgsl_memobj_node *ib;
1174 unsigned int i;
1175
1176 for (i = 0; i < count; i++) {
1177 /* Verify the IBs before they get queued */
1178 if (drawobj[i]->type == CMDOBJ_TYPE) {
1179 struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj[i]);
1180
1181 list_for_each_entry(ib, &cmdobj->cmdlist, node)
1182 if (_verify_ib(dev_priv,
1183 &ADRENO_CONTEXT(context)->base, ib)
1184 == false)
1185 return -EINVAL;
1186 /*
1187 * Clear the wake on touch bit to indicate an IB has
1188 * been submitted since the last time we set it.
1189 * But only clear it when we have rendering commands.
1190 */
1191 device->flags &= ~KGSL_FLAG_WAKE_ON_TOUCH;
1192 }
1193
1194 /* A3XX does not have support for drawobj profiling */
1195 if (adreno_is_a3xx(ADRENO_DEVICE(device)) &&
1196 (drawobj[i]->flags & KGSL_DRAWOBJ_PROFILING))
1197 return -EOPNOTSUPP;
1198 }
1199
1200 return 0;
1201}
1202
1203static inline int _wait_for_room_in_context_queue(
1204 struct adreno_context *drawctxt)
1205{
1206 int ret = 0;
1207
1208 /* Wait for room in the context queue */
1209 while (drawctxt->queued >= _context_drawqueue_size) {
1210 trace_adreno_drawctxt_sleep(drawctxt);
1211 spin_unlock(&drawctxt->lock);
1212
1213 ret = wait_event_interruptible_timeout(drawctxt->wq,
1214 _check_context_queue(drawctxt),
1215 msecs_to_jiffies(_context_queue_wait));
1216
1217 spin_lock(&drawctxt->lock);
1218 trace_adreno_drawctxt_wake(drawctxt);
1219
Harshitha Sai Neelatib29aa132020-09-25 15:14:37 +05301220 /*
1221 * Account for the possibility that the context got invalidated
1222 * while we were sleeping
1223 */
1224
1225 if (ret > 0) {
1226 ret = _check_context_state(&drawctxt->base);
1227 if (ret)
1228 return ret;
1229 } else
Shrenuj Bansala419c792016-10-20 14:05:11 -07001230 return (ret == 0) ? -ETIMEDOUT : (int) ret;
1231 }
1232
1233 return 0;
1234}
1235
1236static unsigned int _check_context_state_to_queue_cmds(
1237 struct adreno_context *drawctxt)
1238{
1239 int ret = _check_context_state(&drawctxt->base);
1240
1241 if (ret)
1242 return ret;
1243
Harshitha Sai Neelatib29aa132020-09-25 15:14:37 +05301244 return _wait_for_room_in_context_queue(drawctxt);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001245}
1246
1247static void _queue_drawobj(struct adreno_context *drawctxt,
1248 struct kgsl_drawobj *drawobj)
1249{
1250 /* Put the command into the queue */
1251 drawctxt->drawqueue[drawctxt->drawqueue_tail] = drawobj;
1252 drawctxt->drawqueue_tail = (drawctxt->drawqueue_tail + 1) %
1253 ADRENO_CONTEXT_DRAWQUEUE_SIZE;
1254 drawctxt->queued++;
1255 trace_adreno_cmdbatch_queued(drawobj, drawctxt->queued);
1256}
1257
Tarun Karra2b8b3632016-11-14 16:38:27 -08001258static int _queue_sparseobj(struct adreno_device *adreno_dev,
1259 struct adreno_context *drawctxt, struct kgsl_drawobj_sparse *sparseobj,
1260 uint32_t *timestamp, unsigned int user_ts)
1261{
1262 struct kgsl_drawobj *drawobj = DRAWOBJ(sparseobj);
1263 int ret;
1264
1265 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1266 if (ret)
1267 return ret;
1268
1269 /*
1270 * See if we can fastpath this thing - if nothing is
1271 * queued bind/unbind without queueing the context
1272 */
1273 if (!drawctxt->queued)
1274 return 1;
1275
1276 drawctxt->queued_timestamp = *timestamp;
1277 _queue_drawobj(drawctxt, drawobj);
1278
1279 return 0;
1280}
1281
1282
Shrenuj Bansala419c792016-10-20 14:05:11 -07001283static int _queue_markerobj(struct adreno_device *adreno_dev,
1284 struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *markerobj,
1285 uint32_t *timestamp, unsigned int user_ts)
1286{
1287 struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
1288 int ret;
1289
1290 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1291 if (ret)
1292 return ret;
1293
1294 /*
1295 * See if we can fastpath this thing - if nothing is queued
1296 * and nothing is inflight retire without bothering the GPU
1297 */
1298 if (!drawctxt->queued && kgsl_check_timestamp(drawobj->device,
1299 drawobj->context, drawctxt->queued_timestamp)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001300 _retire_timestamp(drawobj);
1301 return 1;
1302 }
1303
1304 /*
1305 * Remember the last queued timestamp - the marker will block
1306 * until that timestamp is expired (unless another command
1307 * comes along and forces the marker to execute)
1308 */
1309
1310 markerobj->marker_timestamp = drawctxt->queued_timestamp;
1311 drawctxt->queued_timestamp = *timestamp;
1312 _set_ft_policy(adreno_dev, drawctxt, markerobj);
1313 _cmdobj_set_flags(drawctxt, markerobj);
1314
1315 _queue_drawobj(drawctxt, drawobj);
1316
1317 return 0;
1318}
1319
1320static int _queue_cmdobj(struct adreno_device *adreno_dev,
1321 struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *cmdobj,
1322 uint32_t *timestamp, unsigned int user_ts)
1323{
1324 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1325 unsigned int j;
1326 int ret;
1327
1328 ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
1329 if (ret)
1330 return ret;
1331
1332 /*
1333 * If this is a real command then we need to force any markers
1334 * queued before it to dispatch to keep time linear - set the
1335 * skip bit so the commands get NOPed.
1336 */
1337 j = drawctxt->drawqueue_head;
1338
1339 while (j != drawctxt->drawqueue_tail) {
1340 if (drawctxt->drawqueue[j]->type == MARKEROBJ_TYPE) {
1341 struct kgsl_drawobj_cmd *markerobj =
1342 CMDOBJ(drawctxt->drawqueue[j]);
1343 set_bit(CMDOBJ_SKIP, &markerobj->priv);
1344 }
1345
1346 j = DRAWQUEUE_NEXT(j, ADRENO_CONTEXT_DRAWQUEUE_SIZE);
1347 }
1348
1349 drawctxt->queued_timestamp = *timestamp;
1350 _set_ft_policy(adreno_dev, drawctxt, cmdobj);
1351 _cmdobj_set_flags(drawctxt, cmdobj);
1352
1353 _queue_drawobj(drawctxt, drawobj);
1354
1355 return 0;
1356}
1357
1358static void _queue_syncobj(struct adreno_context *drawctxt,
1359 struct kgsl_drawobj_sync *syncobj, uint32_t *timestamp)
1360{
1361 struct kgsl_drawobj *drawobj = DRAWOBJ(syncobj);
1362
1363 *timestamp = 0;
1364 drawobj->timestamp = 0;
1365
1366 _queue_drawobj(drawctxt, drawobj);
1367}
1368
1369/**
Tarun Karra2b8b3632016-11-14 16:38:27 -08001370 * adreno_dispactcher_queue_cmds() - Queue a new draw object in the context
Shrenuj Bansala419c792016-10-20 14:05:11 -07001371 * @dev_priv: Pointer to the device private struct
1372 * @context: Pointer to the kgsl draw context
1373 * @drawobj: Pointer to the array of drawobj's being submitted
1374 * @count: Number of drawobj's being submitted
1375 * @timestamp: Pointer to the requested timestamp
1376 *
1377 * Queue a command in the context - if there isn't any room in the queue, then
1378 * block until there is
1379 */
1380int adreno_dispatcher_queue_cmds(struct kgsl_device_private *dev_priv,
1381 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
1382 uint32_t count, uint32_t *timestamp)
1383
1384{
1385 struct kgsl_device *device = dev_priv->device;
1386 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1387 struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
1388 struct adreno_dispatcher_drawqueue *dispatch_q;
1389 int ret;
1390 unsigned int i, user_ts;
1391
Tarun Karra2b8b3632016-11-14 16:38:27 -08001392 if (!count)
1393 return -EINVAL;
1394
Shrenuj Bansala419c792016-10-20 14:05:11 -07001395 ret = _check_context_state(&drawctxt->base);
1396 if (ret)
1397 return ret;
1398
1399 ret = _verify_cmdobj(dev_priv, context, drawobj, count);
1400 if (ret)
1401 return ret;
1402
1403 /* wait for the suspend gate */
1404 wait_for_completion(&device->halt_gate);
1405
1406 spin_lock(&drawctxt->lock);
1407
1408 ret = _check_context_state_to_queue_cmds(drawctxt);
1409 if (ret) {
1410 spin_unlock(&drawctxt->lock);
1411 return ret;
1412 }
1413
1414 user_ts = *timestamp;
1415
Archana Sriram960cfdbc2019-10-15 10:36:26 +05301416 /*
1417 * If there is only one drawobj in the array and it is of
1418 * type SYNCOBJ_TYPE, skip comparing user_ts as it can be 0
1419 */
1420 if (!(count == 1 && drawobj[0]->type == SYNCOBJ_TYPE) &&
1421 (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS)) {
1422 /*
1423 * User specified timestamps need to be greater than the last
1424 * issued timestamp in the context
1425 */
1426 if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0) {
1427 spin_unlock(&drawctxt->lock);
1428 return -ERANGE;
1429 }
1430 }
1431
Shrenuj Bansala419c792016-10-20 14:05:11 -07001432 for (i = 0; i < count; i++) {
1433
1434 switch (drawobj[i]->type) {
1435 case MARKEROBJ_TYPE:
1436 ret = _queue_markerobj(adreno_dev, drawctxt,
1437 CMDOBJ(drawobj[i]),
1438 timestamp, user_ts);
1439 if (ret == 1) {
1440 spin_unlock(&drawctxt->lock);
1441 goto done;
1442 } else if (ret) {
1443 spin_unlock(&drawctxt->lock);
1444 return ret;
1445 }
1446 break;
1447 case CMDOBJ_TYPE:
1448 ret = _queue_cmdobj(adreno_dev, drawctxt,
1449 CMDOBJ(drawobj[i]),
1450 timestamp, user_ts);
1451 if (ret) {
1452 spin_unlock(&drawctxt->lock);
1453 return ret;
1454 }
1455 break;
1456 case SYNCOBJ_TYPE:
1457 _queue_syncobj(drawctxt, SYNCOBJ(drawobj[i]),
1458 timestamp);
1459 break;
Tarun Karra2b8b3632016-11-14 16:38:27 -08001460 case SPARSEOBJ_TYPE:
1461 ret = _queue_sparseobj(adreno_dev, drawctxt,
1462 SPARSEOBJ(drawobj[i]),
1463 timestamp, user_ts);
1464 if (ret == 1) {
1465 spin_unlock(&drawctxt->lock);
1466 _retire_sparseobj(SPARSEOBJ(drawobj[i]),
1467 drawctxt);
1468 return 0;
1469 } else if (ret) {
1470 spin_unlock(&drawctxt->lock);
1471 return ret;
1472 }
1473 break;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001474 default:
1475 spin_unlock(&drawctxt->lock);
1476 return -EINVAL;
1477 }
1478
1479 }
1480
1481 dispatch_q = ADRENO_DRAWOBJ_DISPATCH_DRAWQUEUE(drawobj[0]);
1482
1483 _track_context(adreno_dev, dispatch_q, drawctxt);
1484
1485 spin_unlock(&drawctxt->lock);
1486
Gaurav Sonwanic169c322017-06-15 14:11:23 +05301487 if (device->pwrctrl.l2pc_update_queue)
1488 kgsl_pwrctrl_update_l2pc(&adreno_dev->dev,
1489 KGSL_L2PC_QUEUE_TIMEOUT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001490
1491 /* Add the context to the dispatcher pending list */
1492 dispatcher_queue_context(adreno_dev, drawctxt);
1493
1494 /*
1495 * Only issue commands if inflight is less than burst -this prevents us
1496 * from sitting around waiting for the mutex on a busy system - the work
1497 * loop will schedule it for us. Inflight is mutex protected but the
1498 * worse that can happen is that it will go to 0 after we check and if
1499 * it goes to 0 it is because the work loop decremented it and the work
1500 * queue will try to schedule new commands anyway.
1501 */
1502
1503 if (dispatch_q->inflight < _context_drawobj_burst)
1504 adreno_dispatcher_issuecmds(adreno_dev);
1505done:
1506 if (test_and_clear_bit(ADRENO_CONTEXT_FAULT, &context->priv))
1507 return -EPROTO;
1508
1509 return 0;
1510}
1511
1512static int _mark_context(int id, void *ptr, void *data)
1513{
1514 unsigned int guilty = *((unsigned int *) data);
1515 struct kgsl_context *context = ptr;
1516
1517 /*
1518 * If the context is guilty mark it as such. Otherwise mark it as
1519 * innocent if it had not already been marked as guilty. If id is
1520 * passed as 0 then mark EVERYBODY guilty (recovery failed)
1521 */
1522
1523 if (guilty == 0 || guilty == context->id)
1524 context->reset_status =
1525 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT;
1526 else if (context->reset_status !=
1527 KGSL_CTX_STAT_GUILTY_CONTEXT_RESET_EXT)
1528 context->reset_status =
1529 KGSL_CTX_STAT_INNOCENT_CONTEXT_RESET_EXT;
1530
1531 return 0;
1532}
1533
1534/**
1535 * mark_guilty_context() - Mark the given context as guilty (failed recovery)
1536 * @device: Pointer to a KGSL device structure
1537 * @id: Context ID of the guilty context (or 0 to mark all as guilty)
1538 *
1539 * Mark the given (or all) context(s) as guilty (failed recovery)
1540 */
1541static void mark_guilty_context(struct kgsl_device *device, unsigned int id)
1542{
1543 /* Mark the status for all the contexts in the device */
1544
1545 read_lock(&device->context_lock);
1546 idr_for_each(&device->context_idr, _mark_context, &id);
1547 read_unlock(&device->context_lock);
1548}
1549
1550/*
1551 * If an IB inside of the drawobj has a gpuaddr that matches the base
1552 * passed in then zero the size which effectively skips it when it is submitted
1553 * in the ringbuffer.
1554 */
1555static void _skip_ib(struct kgsl_drawobj_cmd *cmdobj, uint64_t base)
1556{
1557 struct kgsl_memobj_node *ib;
1558
1559 list_for_each_entry(ib, &cmdobj->cmdlist, node) {
1560 if (ib->gpuaddr == base) {
1561 ib->priv |= MEMOBJ_SKIP;
1562 if (base)
1563 return;
1564 }
1565 }
1566}
1567
1568static void _skip_cmd(struct kgsl_drawobj_cmd *cmdobj,
1569 struct kgsl_drawobj_cmd **replay, int count)
1570{
1571 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1572 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
1573 int i;
1574
1575 /*
1576 * SKIPCMD policy: next IB issued for this context is tentative
1577 * if it fails we assume that GFT failed and if it succeeds
1578 * we mark GFT as a success.
1579 *
1580 * Find next commandbatch for the faulting context
1581 * If commandbatch is found
1582 * a) store the current commandbatch fault_policy in context's next
1583 * commandbatch fault_policy
1584 * b) force preamble for next commandbatch
1585 */
1586 for (i = 1; i < count; i++) {
1587 if (DRAWOBJ(replay[i])->context->id == drawobj->context->id) {
1588 replay[i]->fault_policy = replay[0]->fault_policy;
1589 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
1590 set_bit(KGSL_FT_SKIPCMD, &replay[i]->fault_recovery);
1591 break;
1592 }
1593 }
1594
1595 /*
1596 * If we did not find the next cmd then
1597 * a) set a flag for next command issued in this context
1598 * b) store the fault_policy, this fault_policy becomes the policy of
1599 * next command issued in this context
1600 */
1601 if ((i == count) && drawctxt) {
1602 set_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
1603 drawctxt->fault_policy = replay[0]->fault_policy;
1604 }
1605
1606 /* set the flags to skip this cmdobj */
1607 set_bit(CMDOBJ_SKIP, &cmdobj->priv);
1608 cmdobj->fault_recovery = 0;
1609}
1610
1611static void _skip_frame(struct kgsl_drawobj_cmd *cmdobj,
1612 struct kgsl_drawobj_cmd **replay, int count)
1613{
1614 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1615 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
1616 int skip = 1;
1617 int i;
1618
1619 for (i = 0; i < count; i++) {
1620
1621 struct kgsl_drawobj *replay_obj = DRAWOBJ(replay[i]);
1622
1623 /*
1624 * Only operate on drawobj's that belong to the
1625 * faulting context
1626 */
1627
1628 if (replay_obj->context->id != drawobj->context->id)
1629 continue;
1630
1631 /*
1632 * Skip all the drawobjs in this context until
1633 * the EOF flag is seen. If the EOF flag is seen then
1634 * force the preamble for the next command.
1635 */
1636
1637 if (skip) {
1638 set_bit(CMDOBJ_SKIP, &replay[i]->priv);
1639
1640 if (replay_obj->flags & KGSL_DRAWOBJ_END_OF_FRAME)
1641 skip = 0;
1642 } else {
1643 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
1644 return;
1645 }
1646 }
1647
1648 /*
1649 * If the EOF flag hasn't been seen yet then set the flag in the
1650 * drawctxt to keep looking for it
1651 */
1652
1653 if (skip && drawctxt)
1654 set_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv);
1655
1656 /*
1657 * If we did see the EOF flag then force the preamble on for the
1658 * next command issued on this context
1659 */
1660
1661 if (!skip && drawctxt)
1662 set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE, &drawctxt->base.priv);
1663}
1664
1665static void remove_invalidated_cmdobjs(struct kgsl_device *device,
1666 struct kgsl_drawobj_cmd **replay, int count)
1667{
1668 int i;
1669
1670 for (i = 0; i < count; i++) {
1671 struct kgsl_drawobj_cmd *cmdobj = replay[i];
1672 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1673
1674 if (cmdobj == NULL)
1675 continue;
1676
1677 if (kgsl_context_detached(drawobj->context) ||
1678 kgsl_context_invalid(drawobj->context)) {
1679 replay[i] = NULL;
1680
1681 mutex_lock(&device->mutex);
1682 kgsl_cancel_events_timestamp(device,
1683 &drawobj->context->events, drawobj->timestamp);
1684 mutex_unlock(&device->mutex);
1685
1686 kgsl_drawobj_destroy(drawobj);
1687 }
1688 }
1689}
1690
1691static char _pidname[TASK_COMM_LEN];
1692
1693static inline const char *_kgsl_context_comm(struct kgsl_context *context)
1694{
1695 if (context && context->proc_priv)
1696 strlcpy(_pidname, context->proc_priv->comm, sizeof(_pidname));
1697 else
1698 snprintf(_pidname, TASK_COMM_LEN, "unknown");
1699
1700 return _pidname;
1701}
1702
1703#define pr_fault(_d, _c, fmt, args...) \
1704 dev_err((_d)->dev, "%s[%d]: " fmt, \
1705 _kgsl_context_comm((_c)->context), \
Archana Srirama9c943d2020-10-18 23:34:04 +05301706 pid_nr((_c)->context->proc_priv->pid), ##args)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001707
1708
1709static void adreno_fault_header(struct kgsl_device *device,
1710 struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj)
1711{
1712 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1713 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1714 unsigned int status, rptr, wptr, ib1sz, ib2sz;
1715 uint64_t ib1base, ib2base;
1716
1717 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &status);
1718 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
1719 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_WPTR, &wptr);
1720 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
1721 ADRENO_REG_CP_IB1_BASE_HI, &ib1base);
1722 adreno_readreg(adreno_dev, ADRENO_REG_CP_IB1_BUFSZ, &ib1sz);
1723 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB2_BASE,
1724 ADRENO_REG_CP_IB2_BASE_HI, &ib2base);
1725 adreno_readreg(adreno_dev, ADRENO_REG_CP_IB2_BUFSZ, &ib2sz);
1726
1727 if (drawobj != NULL) {
1728 struct adreno_context *drawctxt =
1729 ADRENO_CONTEXT(drawobj->context);
1730
1731 trace_adreno_gpu_fault(drawobj->context->id,
1732 drawobj->timestamp,
1733 status, rptr, wptr, ib1base, ib1sz,
1734 ib2base, ib2sz, drawctxt->rb->id);
1735
1736 pr_fault(device, drawobj,
Rajesh Kemisettic05883a2018-09-17 11:34:08 +05301737 "gpu fault ctx %d ctx_type %s ts %d status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
1738 drawobj->context->id, get_api_type_str(drawctxt->type),
1739 drawobj->timestamp, status,
Shrenuj Bansala419c792016-10-20 14:05:11 -07001740 rptr, wptr, ib1base, ib1sz, ib2base, ib2sz);
1741
1742 if (rb != NULL)
1743 pr_fault(device, drawobj,
1744 "gpu fault rb %d rb sw r/w %4.4x/%4.4x\n",
1745 rb->id, rptr, rb->wptr);
1746 } else {
1747 int id = (rb != NULL) ? rb->id : -1;
1748
1749 dev_err(device->dev,
1750 "RB[%d]: gpu fault status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
1751 id, status, rptr, wptr, ib1base, ib1sz, ib2base,
1752 ib2sz);
1753 if (rb != NULL)
1754 dev_err(device->dev,
1755 "RB[%d] gpu fault rb sw r/w %4.4x/%4.4x\n",
1756 rb->id, rptr, rb->wptr);
1757 }
1758}
1759
1760void adreno_fault_skipcmd_detached(struct adreno_device *adreno_dev,
1761 struct adreno_context *drawctxt,
1762 struct kgsl_drawobj *drawobj)
1763{
1764 if (test_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv) &&
1765 kgsl_context_detached(&drawctxt->base)) {
1766 pr_context(KGSL_DEVICE(adreno_dev), drawobj->context,
1767 "gpu detached context %d\n", drawobj->context->id);
1768 clear_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
1769 }
1770}
1771
1772/**
1773 * process_cmdobj_fault() - Process a cmdobj for fault policies
1774 * @device: Device on which the cmdobj caused a fault
1775 * @replay: List of cmdobj's that are to be replayed on the device. The
1776 * first command in the replay list is the faulting command and the remaining
1777 * cmdobj's in the list are commands that were submitted to the same queue
1778 * as the faulting one.
1779 * @count: Number of cmdobj's in replay
1780 * @base: The IB1 base at the time of fault
1781 * @fault: The fault type
1782 */
1783static void process_cmdobj_fault(struct kgsl_device *device,
1784 struct kgsl_drawobj_cmd **replay, int count,
1785 unsigned int base,
1786 int fault)
1787{
1788 struct kgsl_drawobj_cmd *cmdobj = replay[0];
1789 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1790 int i;
1791 char *state = "failed";
1792
1793 /*
1794 * If GFT recovered more than X times in Y ms invalidate the context
1795 * and do not attempt recovery.
1796 * Example: X==3 and Y==3000 ms, GPU hung at 500ms, 1700ms, 25000ms and
1797 * 3000ms for the same context, we will not try FT and invalidate the
1798 * context @3000ms because context triggered GFT more than 3 times in
1799 * last 3 seconds. If a context caused recoverable GPU hangs
1800 * where 1st and 4th gpu hang are more than 3 seconds apart we
1801 * won't disable GFT and invalidate the context.
1802 */
1803 if (test_bit(KGSL_FT_THROTTLE, &cmdobj->fault_policy)) {
1804 if (time_after(jiffies, (drawobj->context->fault_time
1805 + msecs_to_jiffies(_fault_throttle_time)))) {
1806 drawobj->context->fault_time = jiffies;
1807 drawobj->context->fault_count = 1;
1808 } else {
1809 drawobj->context->fault_count++;
1810 if (drawobj->context->fault_count >
1811 _fault_throttle_burst) {
1812 set_bit(KGSL_FT_DISABLE,
1813 &cmdobj->fault_policy);
1814 pr_context(device, drawobj->context,
1815 "gpu fault threshold exceeded %d faults in %d msecs\n",
1816 _fault_throttle_burst,
1817 _fault_throttle_time);
1818 }
1819 }
1820 }
1821
1822 /*
1823 * If FT is disabled for this cmdobj invalidate immediately
1824 */
1825
1826 if (test_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy) ||
1827 test_bit(KGSL_FT_TEMP_DISABLE, &cmdobj->fault_policy)) {
1828 state = "skipped";
1829 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1830 }
1831
1832 /* If the context is detached do not run FT on context */
1833 if (kgsl_context_detached(drawobj->context)) {
1834 state = "detached";
1835 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1836 }
1837
1838 /*
1839 * Set a flag so we don't print another PM dump if the cmdobj fails
1840 * again on replay
1841 */
1842
1843 set_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy);
1844
1845 /*
1846 * A hardware fault generally means something was deterministically
1847 * wrong with the cmdobj - no point in trying to replay it
1848 * Clear the replay bit and move on to the next policy level
1849 */
1850
1851 if (fault & ADRENO_HARD_FAULT)
1852 clear_bit(KGSL_FT_REPLAY, &(cmdobj->fault_policy));
1853
1854 /*
1855 * A timeout fault means the IB timed out - clear the policy and
1856 * invalidate - this will clear the FT_SKIP_PMDUMP bit but that is okay
1857 * because we won't see this cmdobj again
1858 */
1859
Hareesh Gundu28b9efd2017-08-24 23:11:09 +05301860 if ((fault & ADRENO_TIMEOUT_FAULT) ||
1861 (fault & ADRENO_CTX_DETATCH_TIMEOUT_FAULT))
Shrenuj Bansala419c792016-10-20 14:05:11 -07001862 bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
1863
1864 /*
1865 * If the context had a GPU page fault then it is likely it would fault
1866 * again if replayed
1867 */
1868
1869 if (test_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
1870 &drawobj->context->priv)) {
1871 /* we'll need to resume the mmu later... */
1872 clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
1873 clear_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
1874 &drawobj->context->priv);
1875 }
1876
1877 /*
1878 * Execute the fault tolerance policy. Each cmdobj stores the
1879 * current fault policy that was set when it was queued.
1880 * As the options are tried in descending priority
1881 * (REPLAY -> SKIPIBS -> SKIPFRAME -> NOTHING) the bits are cleared
1882 * from the cmdobj policy so the next thing can be tried if the
1883 * change comes around again
1884 */
1885
1886 /* Replay the hanging cmdobj again */
1887 if (test_and_clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy)) {
1888 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_REPLAY));
1889 set_bit(KGSL_FT_REPLAY, &cmdobj->fault_recovery);
1890 return;
1891 }
1892
1893 /*
1894 * Skip the last IB1 that was played but replay everything else.
1895 * Note that the last IB1 might not be in the "hung" cmdobj
1896 * because the CP may have caused a page-fault while it was prefetching
1897 * the next IB1/IB2. walk all outstanding commands and zap the
1898 * supposedly bad IB1 where ever it lurks.
1899 */
1900
1901 if (test_and_clear_bit(KGSL_FT_SKIPIB, &cmdobj->fault_policy)) {
1902 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPIB));
1903 set_bit(KGSL_FT_SKIPIB, &cmdobj->fault_recovery);
1904
1905 for (i = 0; i < count; i++) {
1906 if (replay[i] != NULL &&
1907 DRAWOBJ(replay[i])->context->id ==
1908 drawobj->context->id)
1909 _skip_ib(replay[i], base);
1910 }
1911
1912 return;
1913 }
1914
1915 /* Skip the faulted cmdobj submission */
1916 if (test_and_clear_bit(KGSL_FT_SKIPCMD, &cmdobj->fault_policy)) {
1917 trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPCMD));
1918
1919 /* Skip faulting cmdobj */
1920 _skip_cmd(cmdobj, replay, count);
1921
1922 return;
1923 }
1924
1925 if (test_and_clear_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_policy)) {
1926 trace_adreno_cmdbatch_recovery(cmdobj,
1927 BIT(KGSL_FT_SKIPFRAME));
1928 set_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_recovery);
1929
1930 /*
1931 * Skip all the pending cmdobj's for this context until
1932 * the EOF frame is seen
1933 */
1934 _skip_frame(cmdobj, replay, count);
1935 return;
1936 }
1937
1938 /* If we get here then all the policies failed */
1939
1940 pr_context(device, drawobj->context, "gpu %s ctx %d ts %d\n",
1941 state, drawobj->context->id, drawobj->timestamp);
1942
1943 /* Mark the context as failed */
1944 mark_guilty_context(device, drawobj->context->id);
1945
1946 /* Invalidate the context */
1947 adreno_drawctxt_invalidate(device, drawobj->context);
1948}
1949
1950/**
1951 * recover_dispatch_q() - Recover all commands in a dispatch queue by
1952 * resubmitting the commands
1953 * @device: Device on which recovery is performed
1954 * @dispatch_q: The command queue to recover
1955 * @fault: Faults caused by the command in the dispatch q
1956 * @base: The IB1 base during the fault
1957 */
1958static void recover_dispatch_q(struct kgsl_device *device,
1959 struct adreno_dispatcher_drawqueue *dispatch_q,
1960 int fault,
1961 unsigned int base)
1962{
1963 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1964 struct kgsl_drawobj_cmd **replay;
1965 unsigned int ptr;
1966 int first = 0;
1967 int count = 0;
1968 int i;
1969
1970 /* Allocate memory to store the inflight commands */
1971 replay = kcalloc(dispatch_q->inflight, sizeof(*replay), GFP_KERNEL);
1972
1973 if (replay == NULL) {
1974 unsigned int ptr = dispatch_q->head;
1975
1976 /* Recovery failed - mark everybody on this q guilty */
1977 while (ptr != dispatch_q->tail) {
1978 struct kgsl_drawobj_cmd *cmdobj =
1979 dispatch_q->cmd_q[ptr];
1980 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
1981
1982 mark_guilty_context(device, drawobj->context->id);
1983 adreno_drawctxt_invalidate(device, drawobj->context);
1984 kgsl_drawobj_destroy(drawobj);
1985
1986 ptr = DRAWQUEUE_NEXT(ptr,
1987 ADRENO_DISPATCH_DRAWQUEUE_SIZE);
1988 }
1989
1990 /*
1991 * Set the replay count to zero - this will ensure that the
1992 * hardware gets reset but nothing else gets played
1993 */
1994
1995 count = 0;
1996 goto replay;
1997 }
1998
1999 /* Copy the inflight cmdobj's into the temporary storage */
2000 ptr = dispatch_q->head;
2001
2002 while (ptr != dispatch_q->tail) {
2003 replay[count++] = dispatch_q->cmd_q[ptr];
2004 ptr = DRAWQUEUE_NEXT(ptr, ADRENO_DISPATCH_DRAWQUEUE_SIZE);
2005 }
2006
2007 if (fault && count)
2008 process_cmdobj_fault(device, replay,
2009 count, base, fault);
2010replay:
2011 dispatch_q->inflight = 0;
2012 dispatch_q->head = dispatch_q->tail = 0;
2013 /* Remove any pending cmdobj's that have been invalidated */
2014 remove_invalidated_cmdobjs(device, replay, count);
2015
2016 /* Replay the pending command buffers */
2017 for (i = 0; i < count; i++) {
2018
2019 int ret;
2020
2021 if (replay[i] == NULL)
2022 continue;
2023
2024 /*
2025 * Force the preamble on the first command (if applicable) to
2026 * avoid any strange stage issues
2027 */
2028
2029 if (first == 0) {
2030 set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
2031 first = 1;
2032 }
2033
2034 /*
2035 * Force each cmdobj to wait for idle - this avoids weird
2036 * CP parse issues
2037 */
2038
2039 set_bit(CMDOBJ_WFI, &replay[i]->priv);
2040
2041 ret = sendcmd(adreno_dev, replay[i]);
2042
2043 /*
2044 * If sending the command fails, then try to recover by
2045 * invalidating the context
2046 */
2047
2048 if (ret) {
2049 pr_context(device, replay[i]->base.context,
2050 "gpu reset failed ctx %d ts %d\n",
2051 replay[i]->base.context->id,
2052 replay[i]->base.timestamp);
2053
2054 /* Mark this context as guilty (failed recovery) */
2055 mark_guilty_context(device,
2056 replay[i]->base.context->id);
2057
2058 adreno_drawctxt_invalidate(device,
2059 replay[i]->base.context);
2060 remove_invalidated_cmdobjs(device, &replay[i],
2061 count - i);
2062 }
2063 }
2064
2065 /* Clear the fault bit */
2066 clear_bit(ADRENO_DEVICE_FAULT, &adreno_dev->priv);
2067
2068 kfree(replay);
2069}
2070
Lynus Vaz43695aa2017-09-01 21:55:23 +05302071static void do_header_and_snapshot(struct kgsl_device *device, int fault,
Shrenuj Bansala419c792016-10-20 14:05:11 -07002072 struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj)
2073{
2074 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2075
2076 /* Always dump the snapshot on a non-drawobj failure */
2077 if (cmdobj == NULL) {
2078 adreno_fault_header(device, rb, NULL);
Lynus Vaz43695aa2017-09-01 21:55:23 +05302079 kgsl_device_snapshot(device, NULL, fault & ADRENO_GMU_FAULT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002080 return;
2081 }
2082
2083 /* Skip everything if the PMDUMP flag is set */
2084 if (test_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy))
2085 return;
2086
2087 /* Print the fault header */
2088 adreno_fault_header(device, rb, cmdobj);
2089
2090 if (!(drawobj->context->flags & KGSL_CONTEXT_NO_SNAPSHOT))
Lynus Vazbb99bd72017-09-18 11:44:37 +05302091 kgsl_device_snapshot(device, drawobj->context,
2092 fault & ADRENO_GMU_FAULT);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002093}
2094
2095static int dispatcher_do_fault(struct adreno_device *adreno_dev)
2096{
2097 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
Carter Cooperdf7ba702017-03-20 11:28:04 -06002098 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002099 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2100 struct adreno_dispatcher_drawqueue *dispatch_q = NULL, *dispatch_q_temp;
2101 struct adreno_ringbuffer *rb;
2102 struct adreno_ringbuffer *hung_rb = NULL;
2103 unsigned int reg;
Lynus Vazdbf77f32017-09-21 16:42:16 +05302104 uint64_t base = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002105 struct kgsl_drawobj_cmd *cmdobj = NULL;
2106 int ret, i;
2107 int fault;
2108 int halt;
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002109 bool gx_on = true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002110
2111 fault = atomic_xchg(&dispatcher->fault, 0);
2112 if (fault == 0)
2113 return 0;
2114
Puranam V G Tejaswi3d42a452019-02-07 11:15:31 +05302115 mutex_lock(&device->mutex);
2116
2117 /*
2118 * In the very unlikely case that the power is off, do nothing - the
2119 * state will be reset on power up and everybody will be happy
2120 */
2121 if (!kgsl_state_is_awake(device)) {
2122 mutex_unlock(&device->mutex);
2123 if (fault & ADRENO_SOFT_FAULT) {
2124 /* Clear the existing register values */
2125 memset(adreno_ft_regs_val, 0,
2126 adreno_ft_regs_num * sizeof(unsigned int));
2127 }
2128 return 0;
2129 }
2130
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002131 /* Mask all GMU interrupts */
2132 if (kgsl_gmu_isenabled(device)) {
2133 adreno_write_gmureg(adreno_dev,
2134 ADRENO_REG_GMU_AO_HOST_INTERRUPT_MASK,
2135 0xFFFFFFFF);
2136 adreno_write_gmureg(adreno_dev,
2137 ADRENO_REG_GMU_GMU2HOST_INTR_MASK,
2138 0xFFFFFFFF);
2139 }
2140
2141 if (gpudev->gx_is_on)
2142 gx_on = gpudev->gx_is_on(adreno_dev);
2143
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302144
2145 /*
Lynus Vaz84020732017-03-27 18:12:24 +05302146 * On A5xx and A6xx, read RBBM_STATUS3:SMMU_STALLED_ON_FAULT (BIT 24)
2147 * to tell if this function was entered after a pagefault. If so, only
Shrenuj Bansala419c792016-10-20 14:05:11 -07002148 * proceed if the fault handler has already run in the IRQ thread,
2149 * else return early to give the fault handler a chance to run.
2150 */
Lynus Vaz84020732017-03-27 18:12:24 +05302151 if (!(fault & ADRENO_IOMMU_PAGE_FAULT) &&
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002152 (adreno_is_a5xx(adreno_dev) || adreno_is_a6xx(adreno_dev)) &&
2153 gx_on) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002154 unsigned int val;
2155
Shrenuj Bansala419c792016-10-20 14:05:11 -07002156 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS3, &val);
Puranam V G Tejaswi3d42a452019-02-07 11:15:31 +05302157 if (val & BIT(24)) {
2158 mutex_unlock(&device->mutex);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002159 return 0;
Puranam V G Tejaswi3d42a452019-02-07 11:15:31 +05302160 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002161 }
2162
2163 /* Turn off all the timers */
2164 del_timer_sync(&dispatcher->timer);
2165 del_timer_sync(&dispatcher->fault_timer);
Wenbin Wang93615132017-03-30 14:10:19 +08002166 /*
2167 * Deleting uninitialized timer will block for ever on kernel debug
2168 * disable build. Hence skip del timer if it is not initialized.
2169 */
Harshdeep Dhatt7ee8a862017-11-20 17:51:54 -07002170 if (adreno_is_preemption_enabled(adreno_dev))
Wenbin Wang93615132017-03-30 14:10:19 +08002171 del_timer_sync(&adreno_dev->preempt.timer);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002172
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002173 if (gx_on)
2174 adreno_readreg64(adreno_dev, ADRENO_REG_CP_RB_BASE,
2175 ADRENO_REG_CP_RB_BASE_HI, &base);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002176
2177 /*
2178 * Force the CP off for anything but a hard fault to make sure it is
2179 * good and stopped
2180 */
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002181 if (!(fault & ADRENO_HARD_FAULT) && gx_on) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002182 adreno_readreg(adreno_dev, ADRENO_REG_CP_ME_CNTL, &reg);
Lynus Vaz84020732017-03-27 18:12:24 +05302183 if (adreno_is_a5xx(adreno_dev) || adreno_is_a6xx(adreno_dev))
Shrenuj Bansala419c792016-10-20 14:05:11 -07002184 reg |= 1 | (1 << 1);
2185 else
2186 reg |= (1 << 27) | (1 << 28);
2187 adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_CNTL, reg);
2188 }
2189 /*
2190 * retire cmdobj's from all the dispatch_q's before starting recovery
2191 */
2192 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2193 adreno_dispatch_retire_drawqueue(adreno_dev,
2194 &(rb->dispatch_q));
2195 /* Select the active dispatch_q */
2196 if (base == rb->buffer_desc.gpuaddr) {
2197 dispatch_q = &(rb->dispatch_q);
2198 hung_rb = rb;
2199 if (adreno_dev->cur_rb != hung_rb) {
2200 adreno_dev->prev_rb = adreno_dev->cur_rb;
2201 adreno_dev->cur_rb = hung_rb;
2202 }
2203 }
2204 if (ADRENO_DISPATCHER_RB_STARVE_TIMER_ELAPSED ==
2205 rb->starve_timer_state) {
2206 adreno_put_gpu_halt(adreno_dev);
2207 rb->starve_timer_state =
2208 ADRENO_DISPATCHER_RB_STARVE_TIMER_UNINIT;
2209 }
2210 }
2211
2212 if (dispatch_q && !adreno_drawqueue_is_empty(dispatch_q)) {
2213 cmdobj = dispatch_q->cmd_q[dispatch_q->head];
2214 trace_adreno_cmdbatch_fault(cmdobj, fault);
2215 }
2216
Shrenuj Bansald197bf62017-04-07 11:00:09 -07002217 if (gx_on)
2218 adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
2219 ADRENO_REG_CP_IB1_BASE_HI, &base);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002220
Lynus Vaz43695aa2017-09-01 21:55:23 +05302221 do_header_and_snapshot(device, fault, hung_rb, cmdobj);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002222
Carter Cooperdf7ba702017-03-20 11:28:04 -06002223 /* Turn off the KEEPALIVE vote from the ISR for hard fault */
2224 if (gpudev->gpu_keepalive && fault & ADRENO_HARD_FAULT)
2225 gpudev->gpu_keepalive(adreno_dev, false);
2226
Shrenuj Bansala419c792016-10-20 14:05:11 -07002227 /* Terminate the stalled transaction and resume the IOMMU */
2228 if (fault & ADRENO_IOMMU_PAGE_FAULT)
2229 kgsl_mmu_pagefault_resume(&device->mmu);
2230
2231 /* Reset the dispatcher queue */
2232 dispatcher->inflight = 0;
2233
2234 /* Reset the GPU and make sure halt is not set during recovery */
2235 halt = adreno_gpu_halt(adreno_dev);
2236 adreno_clear_gpu_halt(adreno_dev);
2237
2238 /*
2239 * If there is a stall in the ringbuffer after all commands have been
2240 * retired then we could hit problems if contexts are waiting for
2241 * internal timestamps that will never retire
2242 */
2243
2244 if (hung_rb != NULL) {
2245 kgsl_sharedmem_writel(device, &device->memstore,
2246 MEMSTORE_RB_OFFSET(hung_rb, soptimestamp),
2247 hung_rb->timestamp);
2248
2249 kgsl_sharedmem_writel(device, &device->memstore,
2250 MEMSTORE_RB_OFFSET(hung_rb, eoptimestamp),
2251 hung_rb->timestamp);
2252
2253 /* Schedule any pending events to be run */
2254 kgsl_process_event_group(device, &hung_rb->events);
2255 }
2256
Shrenuj Bansald0fe7462017-05-08 16:11:19 -07002257 if (gpudev->reset)
2258 ret = gpudev->reset(device, fault);
2259 else
2260 ret = adreno_reset(device, fault);
2261
Shrenuj Bansala419c792016-10-20 14:05:11 -07002262 mutex_unlock(&device->mutex);
2263 /* if any other fault got in until reset then ignore */
2264 atomic_set(&dispatcher->fault, 0);
2265
2266 /* If adreno_reset() fails then what hope do we have for the future? */
2267 BUG_ON(ret);
2268
2269 /* recover all the dispatch_q's starting with the one that hung */
2270 if (dispatch_q)
2271 recover_dispatch_q(device, dispatch_q, fault, base);
2272 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2273 dispatch_q_temp = &(rb->dispatch_q);
2274 if (dispatch_q_temp != dispatch_q)
2275 recover_dispatch_q(device, dispatch_q_temp, 0, base);
2276 }
2277
2278 atomic_add(halt, &adreno_dev->halt);
2279
Kyle Pieferf698a7f2018-01-10 10:19:03 -08002280 /*
2281 * At this point it is safe to assume that we recovered. Setting
2282 * this field allows us to take a new snapshot for the next failure
2283 * if we are prioritizing the first unrecoverable snapshot.
2284 */
2285 if (device->snapshot)
2286 device->snapshot->recovered = true;
2287
Shrenuj Bansala419c792016-10-20 14:05:11 -07002288 return 1;
2289}
2290
2291static inline int drawobj_consumed(struct kgsl_drawobj *drawobj,
2292 unsigned int consumed, unsigned int retired)
2293{
2294 return ((timestamp_cmp(drawobj->timestamp, consumed) >= 0) &&
2295 (timestamp_cmp(retired, drawobj->timestamp) < 0));
2296}
2297
2298static void _print_recovery(struct kgsl_device *device,
2299 struct kgsl_drawobj_cmd *cmdobj)
2300{
2301 static struct {
2302 unsigned int mask;
2303 const char *str;
2304 } flags[] = { ADRENO_FT_TYPES };
2305
2306 int i, nr = find_first_bit(&cmdobj->fault_recovery, BITS_PER_LONG);
2307 char *result = "unknown";
2308 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2309
2310 for (i = 0; i < ARRAY_SIZE(flags); i++) {
2311 if (flags[i].mask == BIT(nr)) {
2312 result = (char *) flags[i].str;
2313 break;
2314 }
2315 }
2316
2317 pr_context(device, drawobj->context,
2318 "gpu %s ctx %d ts %d policy %lX\n",
2319 result, drawobj->context->id, drawobj->timestamp,
2320 cmdobj->fault_recovery);
2321}
2322
2323static void cmdobj_profile_ticks(struct adreno_device *adreno_dev,
2324 struct kgsl_drawobj_cmd *cmdobj, uint64_t *start, uint64_t *retire)
2325{
2326 void *ptr = adreno_dev->profile_buffer.hostptr;
2327 struct adreno_drawobj_profile_entry *entry;
2328
2329 entry = (struct adreno_drawobj_profile_entry *)
2330 (ptr + (cmdobj->profile_index * sizeof(*entry)));
2331
2332 /* get updated values of started and retired */
2333 rmb();
2334 *start = entry->started;
2335 *retire = entry->retired;
2336}
2337
2338static void retire_cmdobj(struct adreno_device *adreno_dev,
2339 struct kgsl_drawobj_cmd *cmdobj)
2340{
2341 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2342 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2343 struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
2344 uint64_t start = 0, end = 0;
2345
2346 if (cmdobj->fault_recovery != 0) {
2347 set_bit(ADRENO_CONTEXT_FAULT, &drawobj->context->priv);
2348 _print_recovery(KGSL_DEVICE(adreno_dev), cmdobj);
2349 }
2350
2351 if (test_bit(CMDOBJ_PROFILE, &cmdobj->priv))
2352 cmdobj_profile_ticks(adreno_dev, cmdobj, &start, &end);
2353
2354 /*
2355 * For A3xx we still get the rptr from the CP_RB_RPTR instead of
2356 * rptr scratch out address. At this point GPU clocks turned off.
2357 * So avoid reading GPU register directly for A3xx.
2358 */
2359 if (adreno_is_a3xx(adreno_dev))
2360 trace_adreno_cmdbatch_retired(drawobj,
2361 (int) dispatcher->inflight, start, end,
2362 ADRENO_DRAWOBJ_RB(drawobj), 0, cmdobj->fault_recovery);
2363 else
2364 trace_adreno_cmdbatch_retired(drawobj,
2365 (int) dispatcher->inflight, start, end,
2366 ADRENO_DRAWOBJ_RB(drawobj),
2367 adreno_get_rptr(drawctxt->rb), cmdobj->fault_recovery);
2368
2369 drawctxt->submit_retire_ticks[drawctxt->ticks_index] =
2370 end - cmdobj->submit_ticks;
2371
2372 drawctxt->ticks_index = (drawctxt->ticks_index + 1) %
2373 SUBMIT_RETIRE_TICKS_SIZE;
2374
2375 kgsl_drawobj_destroy(drawobj);
2376}
2377
2378static int adreno_dispatch_retire_drawqueue(struct adreno_device *adreno_dev,
2379 struct adreno_dispatcher_drawqueue *drawqueue)
2380{
2381 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2382 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2383 int count = 0;
2384
2385 while (!adreno_drawqueue_is_empty(drawqueue)) {
2386 struct kgsl_drawobj_cmd *cmdobj =
2387 drawqueue->cmd_q[drawqueue->head];
2388 struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
2389
2390 if (!kgsl_check_timestamp(device, drawobj->context,
2391 drawobj->timestamp))
2392 break;
2393
2394 retire_cmdobj(adreno_dev, cmdobj);
2395
2396 dispatcher->inflight--;
2397 drawqueue->inflight--;
2398
2399 drawqueue->cmd_q[drawqueue->head] = NULL;
2400
2401 drawqueue->head = DRAWQUEUE_NEXT(drawqueue->head,
2402 ADRENO_DISPATCH_DRAWQUEUE_SIZE);
2403
2404 count++;
2405 }
2406
2407 return count;
2408}
2409
2410static void _adreno_dispatch_check_timeout(struct adreno_device *adreno_dev,
2411 struct adreno_dispatcher_drawqueue *drawqueue)
2412{
2413 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2414 struct kgsl_drawobj *drawobj =
2415 DRAWOBJ(drawqueue->cmd_q[drawqueue->head]);
2416
2417 /* Don't timeout if the timer hasn't expired yet (duh) */
2418 if (time_is_after_jiffies(drawqueue->expires))
2419 return;
2420
2421 /* Don't timeout if the IB timeout is disabled globally */
2422 if (!adreno_long_ib_detect(adreno_dev))
2423 return;
2424
2425 /* Don't time out if the context has disabled it */
2426 if (drawobj->context->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
2427 return;
2428
2429 pr_context(device, drawobj->context, "gpu timeout ctx %d ts %d\n",
2430 drawobj->context->id, drawobj->timestamp);
2431
2432 adreno_set_gpu_fault(adreno_dev, ADRENO_TIMEOUT_FAULT);
2433}
2434
2435static int adreno_dispatch_process_drawqueue(struct adreno_device *adreno_dev,
2436 struct adreno_dispatcher_drawqueue *drawqueue)
2437{
2438 int count = adreno_dispatch_retire_drawqueue(adreno_dev, drawqueue);
2439
2440 /* Nothing to do if there are no pending commands */
2441 if (adreno_drawqueue_is_empty(drawqueue))
2442 return count;
2443
Shrenuj Bansala419c792016-10-20 14:05:11 -07002444 /* Don't update the drawqueue timeout if it isn't active */
2445 if (!drawqueue_is_current(drawqueue))
2446 return count;
2447
2448 /*
2449 * If the current ringbuffer retired any commands then universally
2450 * reset the timeout
2451 */
2452
2453 if (count) {
2454 drawqueue->expires = jiffies +
2455 msecs_to_jiffies(adreno_drawobj_timeout);
2456 return count;
2457 }
2458
2459 /*
2460 * If we get here then 1) the ringbuffer is current and 2) we haven't
2461 * retired anything. Check to see if the timeout if valid for the
2462 * current drawobj and fault if it has expired
2463 */
2464 _adreno_dispatch_check_timeout(adreno_dev, drawqueue);
2465 return 0;
2466}
2467
2468/* Update the dispatcher timers */
2469static void _dispatcher_update_timers(struct adreno_device *adreno_dev)
2470{
2471 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2472 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2473
2474 /* Kick the idle timer */
2475 mutex_lock(&device->mutex);
2476 kgsl_pwrscale_update(device);
2477 mod_timer(&device->idle_timer,
2478 jiffies + device->pwrctrl.interval_timeout);
2479 mutex_unlock(&device->mutex);
2480
2481 /* Check to see if we need to update the command timer */
2482 if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
2483 struct adreno_dispatcher_drawqueue *drawqueue =
2484 DRAWQUEUE(adreno_dev->cur_rb);
2485
2486 if (!adreno_drawqueue_is_empty(drawqueue))
2487 mod_timer(&dispatcher->timer, drawqueue->expires);
2488 }
2489}
2490
2491/* Take down the dispatcher and release any power states */
2492static void _dispatcher_power_down(struct adreno_device *adreno_dev)
2493{
2494 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2495 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2496
2497 mutex_lock(&device->mutex);
2498
2499 if (test_and_clear_bit(ADRENO_DISPATCHER_ACTIVE, &dispatcher->priv))
2500 complete_all(&dispatcher->idle_gate);
2501
2502 del_timer_sync(&dispatcher->fault_timer);
2503
2504 if (test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
2505 kgsl_active_count_put(device);
2506 clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
2507 }
2508
2509 mutex_unlock(&device->mutex);
2510}
2511
Tim Murray85040432017-02-20 15:59:32 +05302512static void adreno_dispatcher_work(struct kthread_work *work)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002513{
2514 struct adreno_dispatcher *dispatcher =
2515 container_of(work, struct adreno_dispatcher, work);
2516 struct adreno_device *adreno_dev =
2517 container_of(dispatcher, struct adreno_device, dispatcher);
2518 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2519 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
2520 int count = 0;
2521 unsigned int i = 0;
2522
2523 mutex_lock(&dispatcher->mutex);
2524
2525 /*
2526 * As long as there are inflight commands, process retired comamnds from
2527 * all drawqueues
2528 */
2529 for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
2530 struct adreno_dispatcher_drawqueue *drawqueue =
2531 DRAWQUEUE(&adreno_dev->ringbuffers[i]);
2532
2533 count += adreno_dispatch_process_drawqueue(adreno_dev,
2534 drawqueue);
2535 if (dispatcher->inflight == 0)
2536 break;
2537 }
2538
2539 kgsl_process_event_groups(device);
2540
2541 /*
2542 * dispatcher_do_fault() returns 0 if no faults occurred. If that is the
2543 * case, then clean up preemption and try to schedule more work
2544 */
2545 if (dispatcher_do_fault(adreno_dev) == 0) {
2546
2547 /* Clean up after preemption */
2548 if (gpudev->preemption_schedule)
2549 gpudev->preemption_schedule(adreno_dev);
2550
2551 /* Run the scheduler for to dispatch new commands */
2552 _adreno_dispatcher_issuecmds(adreno_dev);
2553 }
2554
2555 /*
2556 * If there are commands pending, update the timers, otherwise release
2557 * the power state to prepare for power down
2558 */
2559 if (dispatcher->inflight > 0)
2560 _dispatcher_update_timers(adreno_dev);
2561 else
2562 _dispatcher_power_down(adreno_dev);
2563
2564 mutex_unlock(&dispatcher->mutex);
2565}
2566
2567void adreno_dispatcher_schedule(struct kgsl_device *device)
2568{
2569 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2570 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2571
Tim Murray85040432017-02-20 15:59:32 +05302572 kthread_queue_work(&kgsl_driver.worker, &dispatcher->work);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002573}
2574
2575/**
2576 * adreno_dispatcher_queue_context() - schedule a drawctxt in the dispatcher
2577 * device: pointer to the KGSL device
2578 * drawctxt: pointer to the drawctxt to schedule
2579 *
2580 * Put a draw context on the dispatcher pending queue and schedule the
2581 * dispatcher. This is used to reschedule changes that might have been blocked
2582 * for sync points or other concerns
2583 */
2584void adreno_dispatcher_queue_context(struct kgsl_device *device,
2585 struct adreno_context *drawctxt)
2586{
2587 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2588
2589 dispatcher_queue_context(adreno_dev, drawctxt);
2590 adreno_dispatcher_schedule(device);
2591}
2592
2593/*
2594 * This is called on a regular basis while cmdobj's are inflight. Fault
2595 * detection registers are read and compared to the existing values - if they
2596 * changed then the GPU is still running. If they are the same between
2597 * subsequent calls then the GPU may have faulted
2598 */
2599
2600static void adreno_dispatcher_fault_timer(unsigned long data)
2601{
2602 struct adreno_device *adreno_dev = (struct adreno_device *) data;
2603 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2604
2605 /* Leave if the user decided to turn off fast hang detection */
2606 if (!adreno_soft_fault_detect(adreno_dev))
2607 return;
2608
2609 if (adreno_gpu_fault(adreno_dev)) {
2610 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
2611 return;
2612 }
2613
2614 /*
2615 * Read the fault registers - if it returns 0 then they haven't changed
2616 * so mark the dispatcher as faulted and schedule the work loop.
2617 */
2618
2619 if (!fault_detect_read_compare(adreno_dev)) {
2620 adreno_set_gpu_fault(adreno_dev, ADRENO_SOFT_FAULT);
2621 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302622 } else if (dispatcher->inflight > 0) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07002623 mod_timer(&dispatcher->fault_timer,
2624 jiffies + msecs_to_jiffies(_fault_timer_interval));
2625 }
2626}
2627
2628/*
2629 * This is called when the timer expires - it either means the GPU is hung or
2630 * the IB is taking too long to execute
2631 */
2632static void adreno_dispatcher_timer(unsigned long data)
2633{
2634 struct adreno_device *adreno_dev = (struct adreno_device *) data;
2635
2636 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
2637}
2638
2639/**
2640 * adreno_dispatcher_start() - activate the dispatcher
2641 * @adreno_dev: pointer to the adreno device structure
2642 *
2643 */
2644void adreno_dispatcher_start(struct kgsl_device *device)
2645{
2646 complete_all(&device->halt_gate);
2647
2648 /* Schedule the work loop to get things going */
2649 adreno_dispatcher_schedule(device);
2650}
2651
2652/**
2653 * adreno_dispatcher_stop() - stop the dispatcher
2654 * @adreno_dev: pointer to the adreno device structure
2655 *
2656 * Stop the dispatcher and close all the timers
2657 */
2658void adreno_dispatcher_stop(struct adreno_device *adreno_dev)
2659{
2660 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2661
2662 del_timer_sync(&dispatcher->timer);
2663 del_timer_sync(&dispatcher->fault_timer);
2664}
2665
2666/**
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05302667 * adreno_dispatcher_stop_fault_timer() - stop the dispatcher fault timer
2668 * @device: pointer to the KGSL device structure
2669 *
2670 * Stop the dispatcher fault timer
2671 */
2672void adreno_dispatcher_stop_fault_timer(struct kgsl_device *device)
2673{
2674 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2675 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2676
2677 del_timer_sync(&dispatcher->fault_timer);
2678}
2679
2680/**
Shrenuj Bansala419c792016-10-20 14:05:11 -07002681 * adreno_dispatcher_close() - close the dispatcher
2682 * @adreno_dev: pointer to the adreno device structure
2683 *
2684 * Close the dispatcher and free all the oustanding commands and memory
2685 */
2686void adreno_dispatcher_close(struct adreno_device *adreno_dev)
2687{
2688 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2689 int i;
2690 struct adreno_ringbuffer *rb;
2691
2692 mutex_lock(&dispatcher->mutex);
2693 del_timer_sync(&dispatcher->timer);
2694 del_timer_sync(&dispatcher->fault_timer);
2695
2696 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2697 struct adreno_dispatcher_drawqueue *dispatch_q =
2698 &(rb->dispatch_q);
2699 while (!adreno_drawqueue_is_empty(dispatch_q)) {
2700 kgsl_drawobj_destroy(
2701 DRAWOBJ(dispatch_q->cmd_q[dispatch_q->head]));
2702 dispatch_q->head = (dispatch_q->head + 1)
2703 % ADRENO_DISPATCH_DRAWQUEUE_SIZE;
2704 }
2705 }
2706
2707 mutex_unlock(&dispatcher->mutex);
2708
2709 kobject_put(&dispatcher->kobj);
2710}
2711
2712struct dispatcher_attribute {
2713 struct attribute attr;
2714 ssize_t (*show)(struct adreno_dispatcher *,
2715 struct dispatcher_attribute *, char *);
2716 ssize_t (*store)(struct adreno_dispatcher *,
2717 struct dispatcher_attribute *, const char *buf,
2718 size_t count);
2719 unsigned int max;
2720 unsigned int *value;
2721};
2722
2723#define DISPATCHER_UINT_ATTR(_name, _mode, _max, _value) \
2724 struct dispatcher_attribute dispatcher_attr_##_name = { \
2725 .attr = { .name = __stringify(_name), .mode = _mode }, \
2726 .show = _show_uint, \
2727 .store = _store_uint, \
2728 .max = _max, \
2729 .value = &(_value), \
2730 }
2731
2732#define to_dispatcher_attr(_a) \
2733 container_of((_a), struct dispatcher_attribute, attr)
2734#define to_dispatcher(k) container_of(k, struct adreno_dispatcher, kobj)
2735
2736static ssize_t _store_uint(struct adreno_dispatcher *dispatcher,
2737 struct dispatcher_attribute *attr,
2738 const char *buf, size_t size)
2739{
2740 unsigned int val = 0;
2741 int ret;
2742
2743 ret = kgsl_sysfs_store(buf, &val);
2744 if (ret)
2745 return ret;
2746
2747 if (!val || (attr->max && (val > attr->max)))
2748 return -EINVAL;
2749
2750 *((unsigned int *) attr->value) = val;
2751 return size;
2752}
2753
2754static ssize_t _show_uint(struct adreno_dispatcher *dispatcher,
2755 struct dispatcher_attribute *attr,
2756 char *buf)
2757{
2758 return snprintf(buf, PAGE_SIZE, "%u\n",
2759 *((unsigned int *) attr->value));
2760}
2761
2762static DISPATCHER_UINT_ATTR(inflight, 0644, ADRENO_DISPATCH_DRAWQUEUE_SIZE,
2763 _dispatcher_q_inflight_hi);
2764
2765static DISPATCHER_UINT_ATTR(inflight_low_latency, 0644,
2766 ADRENO_DISPATCH_DRAWQUEUE_SIZE, _dispatcher_q_inflight_lo);
2767/*
2768 * Our code that "puts back" a command from the context is much cleaner
2769 * if we are sure that there will always be enough room in the
2770 * ringbuffer so restrict the maximum size of the context queue to
2771 * ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1
2772 */
2773static DISPATCHER_UINT_ATTR(context_drawqueue_size, 0644,
2774 ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1, _context_drawqueue_size);
2775static DISPATCHER_UINT_ATTR(context_burst_count, 0644, 0,
2776 _context_drawobj_burst);
2777static DISPATCHER_UINT_ATTR(drawobj_timeout, 0644, 0,
2778 adreno_drawobj_timeout);
2779static DISPATCHER_UINT_ATTR(context_queue_wait, 0644, 0, _context_queue_wait);
2780static DISPATCHER_UINT_ATTR(fault_detect_interval, 0644, 0,
2781 _fault_timer_interval);
2782static DISPATCHER_UINT_ATTR(fault_throttle_time, 0644, 0,
2783 _fault_throttle_time);
2784static DISPATCHER_UINT_ATTR(fault_throttle_burst, 0644, 0,
2785 _fault_throttle_burst);
2786static DISPATCHER_UINT_ATTR(disp_preempt_fair_sched, 0644, 0,
2787 adreno_disp_preempt_fair_sched);
2788static DISPATCHER_UINT_ATTR(dispatch_time_slice, 0644, 0,
2789 adreno_dispatch_time_slice);
2790static DISPATCHER_UINT_ATTR(dispatch_starvation_time, 0644, 0,
2791 adreno_dispatch_starvation_time);
2792
2793static struct attribute *dispatcher_attrs[] = {
2794 &dispatcher_attr_inflight.attr,
2795 &dispatcher_attr_inflight_low_latency.attr,
2796 &dispatcher_attr_context_drawqueue_size.attr,
2797 &dispatcher_attr_context_burst_count.attr,
2798 &dispatcher_attr_drawobj_timeout.attr,
2799 &dispatcher_attr_context_queue_wait.attr,
2800 &dispatcher_attr_fault_detect_interval.attr,
2801 &dispatcher_attr_fault_throttle_time.attr,
2802 &dispatcher_attr_fault_throttle_burst.attr,
2803 &dispatcher_attr_disp_preempt_fair_sched.attr,
2804 &dispatcher_attr_dispatch_time_slice.attr,
2805 &dispatcher_attr_dispatch_starvation_time.attr,
2806 NULL,
2807};
2808
2809static ssize_t dispatcher_sysfs_show(struct kobject *kobj,
2810 struct attribute *attr, char *buf)
2811{
2812 struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
2813 struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
2814 ssize_t ret = -EIO;
2815
2816 if (pattr->show)
2817 ret = pattr->show(dispatcher, pattr, buf);
2818
2819 return ret;
2820}
2821
2822static ssize_t dispatcher_sysfs_store(struct kobject *kobj,
2823 struct attribute *attr,
2824 const char *buf, size_t count)
2825{
2826 struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
2827 struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
2828 ssize_t ret = -EIO;
2829
2830 if (pattr->store)
2831 ret = pattr->store(dispatcher, pattr, buf, count);
2832
2833 return ret;
2834}
2835
2836static const struct sysfs_ops dispatcher_sysfs_ops = {
2837 .show = dispatcher_sysfs_show,
2838 .store = dispatcher_sysfs_store
2839};
2840
2841static struct kobj_type ktype_dispatcher = {
2842 .sysfs_ops = &dispatcher_sysfs_ops,
2843 .default_attrs = dispatcher_attrs,
2844};
2845
2846/**
2847 * adreno_dispatcher_init() - Initialize the dispatcher
2848 * @adreno_dev: pointer to the adreno device structure
2849 *
2850 * Initialize the dispatcher
2851 */
2852int adreno_dispatcher_init(struct adreno_device *adreno_dev)
2853{
2854 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2855 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2856 int ret;
2857
2858 memset(dispatcher, 0, sizeof(*dispatcher));
2859
2860 mutex_init(&dispatcher->mutex);
2861
2862 setup_timer(&dispatcher->timer, adreno_dispatcher_timer,
2863 (unsigned long) adreno_dev);
2864
2865 setup_timer(&dispatcher->fault_timer, adreno_dispatcher_fault_timer,
2866 (unsigned long) adreno_dev);
2867
Tim Murray85040432017-02-20 15:59:32 +05302868 kthread_init_work(&dispatcher->work, adreno_dispatcher_work);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002869
2870 init_completion(&dispatcher->idle_gate);
2871 complete_all(&dispatcher->idle_gate);
2872
2873 plist_head_init(&dispatcher->pending);
2874 spin_lock_init(&dispatcher->plist_lock);
2875
2876 ret = kobject_init_and_add(&dispatcher->kobj, &ktype_dispatcher,
2877 &device->dev->kobj, "dispatch");
2878
2879 return ret;
2880}
2881
Deepak Kumar79908f52018-02-28 11:06:38 +05302882void adreno_dispatcher_halt(struct kgsl_device *device)
2883{
2884 adreno_get_gpu_halt(ADRENO_DEVICE(device));
2885}
2886
2887void adreno_dispatcher_unhalt(struct kgsl_device *device)
2888{
2889 adreno_put_gpu_halt(ADRENO_DEVICE(device));
2890}
2891
Shrenuj Bansala419c792016-10-20 14:05:11 -07002892/*
2893 * adreno_dispatcher_idle() - Wait for dispatcher to idle
2894 * @adreno_dev: Adreno device whose dispatcher needs to idle
2895 *
2896 * Signal dispatcher to stop sending more commands and complete
2897 * the commands that have already been submitted. This function
2898 * should not be called when dispatcher mutex is held.
2899 * The caller must hold the device mutex.
2900 */
2901int adreno_dispatcher_idle(struct adreno_device *adreno_dev)
2902{
2903 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
2904 struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
2905 int ret;
2906
2907 if (!test_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv))
2908 return 0;
2909
2910 /*
2911 * Ensure that this function is not called when dispatcher
2912 * mutex is held and device is started
2913 */
2914 if (mutex_is_locked(&dispatcher->mutex) &&
2915 dispatcher->mutex.owner == current)
2916 return -EDEADLK;
2917
2918 adreno_get_gpu_halt(adreno_dev);
2919
2920 mutex_unlock(&device->mutex);
2921
Deepak Kumar79908f52018-02-28 11:06:38 +05302922 /*
2923 * Flush the worker to make sure all executing
2924 * or pending dispatcher works on worker are
2925 * finished
2926 */
2927 kthread_flush_worker(&kgsl_driver.worker);
2928
Shrenuj Bansala419c792016-10-20 14:05:11 -07002929 ret = wait_for_completion_timeout(&dispatcher->idle_gate,
2930 msecs_to_jiffies(ADRENO_IDLE_TIMEOUT));
2931 if (ret == 0) {
2932 ret = -ETIMEDOUT;
2933 WARN(1, "Dispatcher halt timeout ");
2934 } else if (ret < 0) {
2935 KGSL_DRV_ERR(device, "Dispatcher halt failed %d\n", ret);
2936 } else {
2937 ret = 0;
2938 }
2939
2940 mutex_lock(&device->mutex);
2941 adreno_put_gpu_halt(adreno_dev);
2942 /*
2943 * requeue dispatcher work to resubmit pending commands
2944 * that may have been blocked due to this idling request
2945 */
2946 adreno_dispatcher_schedule(device);
2947 return ret;
2948}