| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Tegra host1x Command DMA |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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/>. |
|---|
| 17 | 6 | */ |
|---|
| 18 | 7 | |
|---|
| 19 | 8 | |
|---|
| .. | .. |
|---|
| 41 | 30 | * means that the push buffer is full, not empty. |
|---|
| 42 | 31 | */ |
|---|
| 43 | 32 | |
|---|
| 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 |
|---|
| 45 | 44 | |
|---|
| 46 | 45 | /* |
|---|
| 47 | 46 | * Clean up push buffer resources |
|---|
| .. | .. |
|---|
| 143 | 142 | WARN_ON(pb->pos == pb->fence); |
|---|
| 144 | 143 | *(p++) = op1; |
|---|
| 145 | 144 | *(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; |
|---|
| 147 | 149 | } |
|---|
| 148 | 150 | |
|---|
| 149 | 151 | /* |
|---|
| .. | .. |
|---|
| 153 | 155 | static void host1x_pushbuffer_pop(struct push_buffer *pb, unsigned int slots) |
|---|
| 154 | 156 | { |
|---|
| 155 | 157 | /* 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; |
|---|
| 157 | 162 | } |
|---|
| 158 | 163 | |
|---|
| 159 | 164 | /* |
|---|
| .. | .. |
|---|
| 161 | 166 | */ |
|---|
| 162 | 167 | static u32 host1x_pushbuffer_space(struct push_buffer *pb) |
|---|
| 163 | 168 | { |
|---|
| 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; |
|---|
| 165 | 175 | } |
|---|
| 166 | 176 | |
|---|
| 167 | 177 | /* |
|---|
| .. | .. |
|---|
| 210 | 220 | cdma->event = event; |
|---|
| 211 | 221 | |
|---|
| 212 | 222 | mutex_unlock(&cdma->lock); |
|---|
| 213 | | - down(&cdma->sem); |
|---|
| 223 | + wait_for_completion(&cdma->complete); |
|---|
| 214 | 224 | mutex_lock(&cdma->lock); |
|---|
| 215 | 225 | } |
|---|
| 216 | 226 | |
|---|
| 217 | 227 | return 0; |
|---|
| 218 | 228 | } |
|---|
| 219 | 229 | |
|---|
| 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 | +} |
|---|
| 220 | 269 | /* |
|---|
| 221 | 270 | * Start timer that tracks the time spent by the job. |
|---|
| 222 | 271 | * Must be called with the cdma lock held. |
|---|
| .. | .. |
|---|
| 314 | 363 | |
|---|
| 315 | 364 | if (signal) { |
|---|
| 316 | 365 | cdma->event = CDMA_EVENT_NONE; |
|---|
| 317 | | - up(&cdma->sem); |
|---|
| 366 | + complete(&cdma->complete); |
|---|
| 318 | 367 | } |
|---|
| 319 | 368 | } |
|---|
| 320 | 369 | |
|---|
| .. | .. |
|---|
| 323 | 372 | { |
|---|
| 324 | 373 | struct host1x *host1x = cdma_to_host1x(cdma); |
|---|
| 325 | 374 | u32 restart_addr, syncpt_incrs, syncpt_val; |
|---|
| 326 | | - struct host1x_job *job = NULL; |
|---|
| 375 | + struct host1x_job *job, *next_job = NULL; |
|---|
| 327 | 376 | |
|---|
| 328 | 377 | syncpt_val = host1x_syncpt_load(cdma->timeout.syncpt); |
|---|
| 329 | 378 | |
|---|
| .. | .. |
|---|
| 341 | 390 | __func__); |
|---|
| 342 | 391 | |
|---|
| 343 | 392 | 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 | + } |
|---|
| 346 | 400 | |
|---|
| 347 | 401 | host1x_job_dump(dev, job); |
|---|
| 348 | 402 | } |
|---|
| 349 | 403 | |
|---|
| 404 | + /* all jobs have been completed */ |
|---|
| 405 | + job = NULL; |
|---|
| 406 | + |
|---|
| 407 | +syncpt_incr: |
|---|
| 408 | + |
|---|
| 350 | 409 | /* |
|---|
| 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. |
|---|
| 354 | 411 | * |
|---|
| 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. |
|---|
| 363 | 414 | */ |
|---|
| 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; |
|---|
| 370 | 417 | else |
|---|
| 371 | 418 | restart_addr = cdma->last_pos; |
|---|
| 372 | 419 | |
|---|
| 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__); |
|---|
| 378 | 424 | |
|---|
| 379 | 425 | /* won't need a timeout when replayed */ |
|---|
| 380 | 426 | job->timeout = 0; |
|---|
| .. | .. |
|---|
| 389 | 435 | syncpt_incrs, job->syncpt_end, |
|---|
| 390 | 436 | job->num_slots); |
|---|
| 391 | 437 | |
|---|
| 392 | | - syncpt_val += syncpt_incrs; |
|---|
| 438 | + dev_dbg(dev, "%s: finished sync_queue modification\n", |
|---|
| 439 | + __func__); |
|---|
| 393 | 440 | } |
|---|
| 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__); |
|---|
| 406 | 441 | |
|---|
| 407 | 442 | /* roll back DMAGET and start up channel again */ |
|---|
| 408 | 443 | host1x_hw_cdma_resume(host1x, cdma, restart_addr); |
|---|
| .. | .. |
|---|
| 416 | 451 | int err; |
|---|
| 417 | 452 | |
|---|
| 418 | 453 | mutex_init(&cdma->lock); |
|---|
| 419 | | - sema_init(&cdma->sem, 0); |
|---|
| 454 | + init_completion(&cdma->complete); |
|---|
| 420 | 455 | |
|---|
| 421 | 456 | INIT_LIST_HEAD(&cdma->sync_queue); |
|---|
| 422 | 457 | |
|---|
| .. | .. |
|---|
| 510 | 545 | } |
|---|
| 511 | 546 | |
|---|
| 512 | 547 | /* |
|---|
| 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 | +/* |
|---|
| 513 | 601 | * End a cdma submit |
|---|
| 514 | 602 | * Kick off DMA, add job to the sync queue, and a number of slots to be freed |
|---|
| 515 | 603 | * from the pushbuffer. The handles for a submit must all be pinned at the same |
|---|