hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
kernel/drivers/gpu/host1x/cdma.c
....@@ -1,19 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Tegra host1x Command DMA
34 *
45 * Copyright (c) 2010-2013, NVIDIA Corporation.
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms and conditions of the GNU General Public License,
8
- * version 2, as published by the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
176 */
187
198
....@@ -41,7 +30,17 @@
4130 * means that the push buffer is full, not empty.
4231 */
4332
44
-#define HOST1X_PUSHBUFFER_SLOTS 512
33
+/*
34
+ * Typically the commands written into the push buffer are a pair of words. We
35
+ * use slots to represent each of these pairs and to simplify things. Note the
36
+ * strange number of slots allocated here. 512 slots will fit exactly within a
37
+ * single memory page. We also need one additional word at the end of the push
38
+ * buffer for the RESTART opcode that will instruct the CDMA to jump back to
39
+ * the beginning of the push buffer. With 512 slots, this means that we'll use
40
+ * 2 memory pages and waste 4092 bytes of the second page that will never be
41
+ * used.
42
+ */
43
+#define HOST1X_PUSHBUFFER_SLOTS 511
4544
4645 /*
4746 * Clean up push buffer resources
....@@ -143,7 +142,10 @@
143142 WARN_ON(pb->pos == pb->fence);
144143 *(p++) = op1;
145144 *(p++) = op2;
146
- pb->pos = (pb->pos + 8) & (pb->size - 1);
145
+ pb->pos += 8;
146
+
147
+ if (pb->pos >= pb->size)
148
+ pb->pos -= pb->size;
147149 }
148150
149151 /*
....@@ -153,7 +155,10 @@
153155 static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots)
154156 {
155157 /* Advance the next write position */
156
- pb->fence = (pb->fence + slots * 8) & (pb->size - 1);
158
+ pb->fence += slots * 8;
159
+
160
+ if (pb->fence >= pb->size)
161
+ pb->fence -= pb->size;
157162 }
158163
159164 /*
....@@ -161,7 +166,12 @@
161166 */
162167 static u32 host1x_pushbuffer_space(struct push_buffer *pb)
163168 {
164
- return ((pb->fence - pb->pos) & (pb->size - 1)) / 8;
169
+ unsigned int fence = pb->fence;
170
+
171
+ if (pb->fence < pb->pos)
172
+ fence += pb->size;
173
+
174
+ return (fence - pb->pos) / 8;
165175 }
166176
167177 /*
....@@ -210,13 +220,52 @@
210220 cdma->event = event;
211221
212222 mutex_unlock(&cdma->lock);
213
- down(&cdma->sem);
223
+ wait_for_completion(&cdma->complete);
214224 mutex_lock(&cdma->lock);
215225 }
216226
217227 return 0;
218228 }
219229
230
+/*
231
+ * Sleep (if necessary) until the push buffer has enough free space.
232
+ *
233
+ * Must be called with the cdma lock held.
234
+ */
235
+static int host1x_cdma_wait_pushbuffer_space(struct host1x *host1x,
236
+ struct host1x_cdma *cdma,
237
+ unsigned int needed)
238
+{
239
+ while (true) {
240
+ struct push_buffer *pb = &cdma->push_buffer;
241
+ unsigned int space;
242
+
243
+ space = host1x_pushbuffer_space(pb);
244
+ if (space >= needed)
245
+ break;
246
+
247
+ trace_host1x_wait_cdma(dev_name(cdma_to_channel(cdma)->dev),
248
+ CDMA_EVENT_PUSH_BUFFER_SPACE);
249
+
250
+ host1x_hw_cdma_flush(host1x, cdma);
251
+
252
+ /* If somebody has managed to already start waiting, yield */
253
+ if (cdma->event != CDMA_EVENT_NONE) {
254
+ mutex_unlock(&cdma->lock);
255
+ schedule();
256
+ mutex_lock(&cdma->lock);
257
+ continue;
258
+ }
259
+
260
+ cdma->event = CDMA_EVENT_PUSH_BUFFER_SPACE;
261
+
262
+ mutex_unlock(&cdma->lock);
263
+ wait_for_completion(&cdma->complete);
264
+ mutex_lock(&cdma->lock);
265
+ }
266
+
267
+ return 0;
268
+}
220269 /*
221270 * Start timer that tracks the time spent by the job.
222271 * Must be called with the cdma lock held.
....@@ -314,7 +363,7 @@
314363
315364 if (signal) {
316365 cdma->event = CDMA_EVENT_NONE;
317
- up(&cdma->sem);
366
+ complete(&cdma->complete);
318367 }
319368 }
320369
....@@ -323,7 +372,7 @@
323372 {
324373 struct host1x *host1x = cdma_to_host1x(cdma);
325374 u32 restart_addr, syncpt_incrs, syncpt_val;
326
- struct host1x_job *job = NULL;
375
+ struct host1x_job *job, *next_job = NULL;
327376
328377 syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt);
329378
....@@ -341,40 +390,37 @@
341390 __func__);
342391
343392 list_for_each_entry(job, &cdma->sync_queue, list) {
344
- if (syncpt_val < job->syncpt_end)
345
- break;
393
+ if (syncpt_val < job->syncpt_end) {
394
+
395
+ if (!list_is_last(&job->list, &cdma->sync_queue))
396
+ next_job = list_next_entry(job, list);
397
+
398
+ goto syncpt_incr;
399
+ }
346400
347401 host1x_job_dump(dev, job);
348402 }
349403
404
+ /* all jobs have been completed */
405
+ job = NULL;
406
+
407
+syncpt_incr:
408
+
350409 /*
351
- * Walk the sync_queue, first incrementing with the CPU syncpts that
352
- * are partially executed (the first buffer) or fully skipped while
353
- * still in the current context (slots are also NOP-ed).
410
+ * Increment with CPU the remaining syncpts of a partially executed job.
354411 *
355
- * At the point contexts are interleaved, syncpt increments must be
356
- * done inline with the pushbuffer from a GATHER buffer to maintain
357
- * the order (slots are modified to be a GATHER of syncpt incrs).
358
- *
359
- * Note: save in restart_addr the location where the timed out buffer
360
- * started in the PB, so we can start the refetch from there (with the
361
- * modified NOP-ed PB slots). This lets things appear to have completed
362
- * properly for this buffer and resources are freed.
412
+ * CDMA will continue execution starting with the next job or will get
413
+ * into idle state.
363414 */
364
-
365
- dev_dbg(dev, "%s: perform CPU incr on pending same ctx buffers\n",
366
- __func__);
367
-
368
- if (!list_empty(&cdma->sync_queue))
369
- restart_addr = job->first_get;
415
+ if (next_job)
416
+ restart_addr = next_job->first_get;
370417 else
371418 restart_addr = cdma->last_pos;
372419
373
- /* do CPU increments as long as this context continues */
374
- list_for_each_entry_from(job, &cdma->sync_queue, list) {
375
- /* different context, gets us out of this loop */
376
- if (job->client != cdma->timeout.client)
377
- break;
420
+ /* do CPU increments for the remaining syncpts */
421
+ if (job) {
422
+ dev_dbg(dev, "%s: perform CPU incr on pending buffers\n",
423
+ __func__);
378424
379425 /* won't need a timeout when replayed */
380426 job->timeout = 0;
....@@ -389,20 +435,9 @@
389435 syncpt_incrs, job->syncpt_end,
390436 job->num_slots);
391437
392
- syncpt_val += syncpt_incrs;
438
+ dev_dbg(dev, "%s: finished sync_queue modification\n",
439
+ __func__);
393440 }
394
-
395
- /*
396
- * The following sumbits from the same client may be dependent on the
397
- * failed submit and therefore they may fail. Force a small timeout
398
- * to make the queue cleanup faster.
399
- */
400
-
401
- list_for_each_entry_from(job, &cdma->sync_queue, list)
402
- if (job->client == cdma->timeout.client)
403
- job->timeout = min_t(unsigned int, job->timeout, 500);
404
-
405
- dev_dbg(dev, "%s: finished sync_queue modification\n", __func__);
406441
407442 /* roll back DMAGET and start up channel again */
408443 host1x_hw_cdma_resume(host1x, cdma, restart_addr);
....@@ -416,7 +451,7 @@
416451 int err;
417452
418453 mutex_init(&cdma->lock);
419
- sema_init(&cdma->sem, 0);
454
+ init_completion(&cdma->complete);
420455
421456 INIT_LIST_HEAD(&cdma->sync_queue);
422457
....@@ -510,6 +545,59 @@
510545 }
511546
512547 /*
548
+ * Push four words into two consecutive push buffer slots. Note that extra
549
+ * care needs to be taken not to split the two slots across the end of the
550
+ * push buffer. Otherwise the RESTART opcode at the end of the push buffer
551
+ * that ensures processing will restart at the beginning will break up the
552
+ * four words.
553
+ *
554
+ * Blocks as necessary if the push buffer is full.
555
+ */
556
+void host1x_cdma_push_wide(struct host1x_cdma *cdma, u32 op1, u32 op2,
557
+ u32 op3, u32 op4)
558
+{
559
+ struct host1x_channel *channel = cdma_to_channel(cdma);
560
+ struct host1x *host1x = cdma_to_host1x(cdma);
561
+ struct push_buffer *pb = &cdma->push_buffer;
562
+ unsigned int needed = 2, extra = 0, i;
563
+ unsigned int space = cdma->slots_free;
564
+
565
+ if (host1x_debug_trace_cmdbuf)
566
+ trace_host1x_cdma_push_wide(dev_name(channel->dev), op1, op2,
567
+ op3, op4);
568
+
569
+ /* compute number of extra slots needed for padding */
570
+ if (pb->pos + 16 > pb->size) {
571
+ extra = (pb->size - pb->pos) / 8;
572
+ needed += extra;
573
+ }
574
+
575
+ host1x_cdma_wait_pushbuffer_space(host1x, cdma, needed);
576
+ space = host1x_pushbuffer_space(pb);
577
+
578
+ cdma->slots_free = space - needed;
579
+ cdma->slots_used += needed;
580
+
581
+ /*
582
+ * Note that we rely on the fact that this is only used to submit wide
583
+ * gather opcodes, which consist of 3 words, and they are padded with
584
+ * a NOP to avoid having to deal with fractional slots (a slot always
585
+ * represents 2 words). The fourth opcode passed to this function will
586
+ * therefore always be a NOP.
587
+ *
588
+ * This works around a slight ambiguity when it comes to opcodes. For
589
+ * all current host1x incarnations the NOP opcode uses the exact same
590
+ * encoding (0x20000000), so we could hard-code the value here, but a
591
+ * new incarnation may change it and break that assumption.
592
+ */
593
+ for (i = 0; i < extra; i++)
594
+ host1x_pushbuffer_push(pb, op4, op4);
595
+
596
+ host1x_pushbuffer_push(pb, op1, op2);
597
+ host1x_pushbuffer_push(pb, op3, op4);
598
+}
599
+
600
+/*
513601 * End a cdma submit
514602 * Kick off DMA, add job to the sync queue, and a number of slots to be freed
515603 * from the pushbuffer. The handles for a submit must all be pinned at the same