1 // SPDX-License-Identifier: GPL-2.0-only 2 3 /* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. */ 4 /* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. */ 5 6 #include <linux/bitfield.h> 7 #include <linux/bits.h> 8 #include <linux/completion.h> 9 #include <linux/delay.h> 10 #include <linux/dma-buf.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/interrupt.h> 13 #include <linux/kref.h> 14 #include <linux/list.h> 15 #include <linux/math64.h> 16 #include <linux/mm.h> 17 #include <linux/moduleparam.h> 18 #include <linux/scatterlist.h> 19 #include <linux/spinlock.h> 20 #include <linux/srcu.h> 21 #include <linux/types.h> 22 #include <linux/uaccess.h> 23 #include <linux/wait.h> 24 #include <drm/drm_file.h> 25 #include <drm/drm_gem.h> 26 #include <drm/drm_prime.h> 27 #include <drm/drm_print.h> 28 #include <uapi/drm/qaic_accel.h> 29 30 #include "qaic.h" 31 32 #define SEM_VAL_MASK GENMASK_ULL(11, 0) 33 #define SEM_INDEX_MASK GENMASK_ULL(4, 0) 34 #define BULK_XFER BIT(3) 35 #define GEN_COMPLETION BIT(4) 36 #define INBOUND_XFER 1 37 #define OUTBOUND_XFER 2 38 #define REQHP_OFF 0x0 /* we read this */ 39 #define REQTP_OFF 0x4 /* we write this */ 40 #define RSPHP_OFF 0x8 /* we write this */ 41 #define RSPTP_OFF 0xc /* we read this */ 42 43 #define ENCODE_SEM(val, index, sync, cmd, flags) \ 44 ({ \ 45 FIELD_PREP(GENMASK(11, 0), (val)) | \ 46 FIELD_PREP(GENMASK(20, 16), (index)) | \ 47 FIELD_PREP(BIT(22), (sync)) | \ 48 FIELD_PREP(GENMASK(26, 24), (cmd)) | \ 49 FIELD_PREP(GENMASK(30, 29), (flags)) | \ 50 FIELD_PREP(BIT(31), (cmd) ? 1 : 0); \ 51 }) 52 #define NUM_EVENTS 128 53 #define NUM_DELAYS 10 54 55 static unsigned int wait_exec_default_timeout_ms = 5000; /* 5 sec default */ 56 module_param(wait_exec_default_timeout_ms, uint, 0600); 57 MODULE_PARM_DESC(wait_exec_default_timeout_ms, "Default timeout for DRM_IOCTL_QAIC_WAIT_BO"); 58 59 static unsigned int datapath_poll_interval_us = 100; /* 100 usec default */ 60 module_param(datapath_poll_interval_us, uint, 0600); 61 MODULE_PARM_DESC(datapath_poll_interval_us, 62 "Amount of time to sleep between activity when datapath polling is enabled"); 63 64 struct dbc_req { 65 /* 66 * A request ID is assigned to each memory handle going in DMA queue. 67 * As a single memory handle can enqueue multiple elements in DMA queue 68 * all of them will have the same request ID. 69 */ 70 __le16 req_id; 71 /* Future use */ 72 __u8 seq_id; 73 /* 74 * Special encoded variable 75 * 7 0 - Do not force to generate MSI after DMA is completed 76 * 1 - Force to generate MSI after DMA is completed 77 * 6:5 Reserved 78 * 4 1 - Generate completion element in the response queue 79 * 0 - No Completion Code 80 * 3 0 - DMA request is a Link list transfer 81 * 1 - DMA request is a Bulk transfer 82 * 2 Reserved 83 * 1:0 00 - No DMA transfer involved 84 * 01 - DMA transfer is part of inbound transfer 85 * 10 - DMA transfer has outbound transfer 86 * 11 - NA 87 */ 88 __u8 cmd; 89 __le32 resv; 90 /* Source address for the transfer */ 91 __le64 src_addr; 92 /* Destination address for the transfer */ 93 __le64 dest_addr; 94 /* Length of transfer request */ 95 __le32 len; 96 __le32 resv2; 97 /* Doorbell address */ 98 __le64 db_addr; 99 /* 100 * Special encoded variable 101 * 7 1 - Doorbell(db) write 102 * 0 - No doorbell write 103 * 6:2 Reserved 104 * 1:0 00 - 32 bit access, db address must be aligned to 32bit-boundary 105 * 01 - 16 bit access, db address must be aligned to 16bit-boundary 106 * 10 - 8 bit access, db address must be aligned to 8bit-boundary 107 * 11 - Reserved 108 */ 109 __u8 db_len; 110 __u8 resv3; 111 __le16 resv4; 112 /* 32 bit data written to doorbell address */ 113 __le32 db_data; 114 /* 115 * Special encoded variable 116 * All the fields of sem_cmdX are passed from user and all are ORed 117 * together to form sem_cmd. 118 * 0:11 Semaphore value 119 * 15:12 Reserved 120 * 20:16 Semaphore index 121 * 21 Reserved 122 * 22 Semaphore Sync 123 * 23 Reserved 124 * 26:24 Semaphore command 125 * 28:27 Reserved 126 * 29 Semaphore DMA out bound sync fence 127 * 30 Semaphore DMA in bound sync fence 128 * 31 Enable semaphore command 129 */ 130 __le32 sem_cmd0; 131 __le32 sem_cmd1; 132 __le32 sem_cmd2; 133 __le32 sem_cmd3; 134 } __packed; 135 136 struct dbc_rsp { 137 /* Request ID of the memory handle whose DMA transaction is completed */ 138 __le16 req_id; 139 /* Status of the DMA transaction. 0 : Success otherwise failure */ 140 __le16 status; 141 } __packed; 142 143 inline int get_dbc_req_elem_size(void) 144 { 145 return sizeof(struct dbc_req); 146 } 147 148 inline int get_dbc_rsp_elem_size(void) 149 { 150 return sizeof(struct dbc_rsp); 151 } 152 153 static void free_slice(struct kref *kref) 154 { 155 struct bo_slice *slice = container_of(kref, struct bo_slice, ref_count); 156 157 slice->bo->total_slice_nents -= slice->nents; 158 list_del(&slice->slice); 159 drm_gem_object_put(&slice->bo->base); 160 sg_free_table(slice->sgt); 161 kfree(slice->sgt); 162 kfree(slice->reqs); 163 kfree(slice); 164 } 165 166 static int clone_range_of_sgt_for_slice(struct qaic_device *qdev, struct sg_table **sgt_out, 167 struct sg_table *sgt_in, u64 size, u64 offset) 168 { 169 int total_len, len, nents, offf = 0, offl = 0; 170 struct scatterlist *sg, *sgn, *sgf, *sgl; 171 struct sg_table *sgt; 172 int ret, j; 173 174 /* find out number of relevant nents needed for this mem */ 175 total_len = 0; 176 sgf = NULL; 177 sgl = NULL; 178 nents = 0; 179 180 size = size ? size : PAGE_SIZE; 181 for (sg = sgt_in->sgl; sg; sg = sg_next(sg)) { 182 len = sg_dma_len(sg); 183 184 if (!len) 185 continue; 186 if (offset >= total_len && offset < total_len + len) { 187 sgf = sg; 188 offf = offset - total_len; 189 } 190 if (sgf) 191 nents++; 192 if (offset + size >= total_len && 193 offset + size <= total_len + len) { 194 sgl = sg; 195 offl = offset + size - total_len; 196 break; 197 } 198 total_len += len; 199 } 200 201 if (!sgf || !sgl) { 202 ret = -EINVAL; 203 goto out; 204 } 205 206 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 207 if (!sgt) { 208 ret = -ENOMEM; 209 goto out; 210 } 211 212 ret = sg_alloc_table(sgt, nents, GFP_KERNEL); 213 if (ret) 214 goto free_sgt; 215 216 /* copy relevant sg node and fix page and length */ 217 sgn = sgf; 218 for_each_sgtable_sg(sgt, sg, j) { 219 memcpy(sg, sgn, sizeof(*sg)); 220 if (sgn == sgf) { 221 sg_dma_address(sg) += offf; 222 sg_dma_len(sg) -= offf; 223 sg_set_page(sg, sg_page(sgn), sg_dma_len(sg), offf); 224 } else { 225 offf = 0; 226 } 227 if (sgn == sgl) { 228 sg_dma_len(sg) = offl - offf; 229 sg_set_page(sg, sg_page(sgn), offl - offf, offf); 230 sg_mark_end(sg); 231 break; 232 } 233 sgn = sg_next(sgn); 234 } 235 236 *sgt_out = sgt; 237 return ret; 238 239 free_sgt: 240 kfree(sgt); 241 out: 242 *sgt_out = NULL; 243 return ret; 244 } 245 246 static int encode_reqs(struct qaic_device *qdev, struct bo_slice *slice, 247 struct qaic_attach_slice_entry *req) 248 { 249 __le64 db_addr = cpu_to_le64(req->db_addr); 250 __le32 db_data = cpu_to_le32(req->db_data); 251 struct scatterlist *sg; 252 __u8 cmd = BULK_XFER; 253 int presync_sem; 254 u64 dev_addr; 255 __u8 db_len; 256 int i; 257 258 if (!slice->no_xfer) 259 cmd |= (slice->dir == DMA_TO_DEVICE ? INBOUND_XFER : OUTBOUND_XFER); 260 261 if (req->db_len && !IS_ALIGNED(req->db_addr, req->db_len / 8)) 262 return -EINVAL; 263 264 presync_sem = req->sem0.presync + req->sem1.presync + req->sem2.presync + req->sem3.presync; 265 if (presync_sem > 1) 266 return -EINVAL; 267 268 presync_sem = req->sem0.presync << 0 | req->sem1.presync << 1 | 269 req->sem2.presync << 2 | req->sem3.presync << 3; 270 271 switch (req->db_len) { 272 case 32: 273 db_len = BIT(7); 274 break; 275 case 16: 276 db_len = BIT(7) | 1; 277 break; 278 case 8: 279 db_len = BIT(7) | 2; 280 break; 281 case 0: 282 db_len = 0; /* doorbell is not active for this command */ 283 break; 284 default: 285 return -EINVAL; /* should never hit this */ 286 } 287 288 /* 289 * When we end up splitting up a single request (ie a buf slice) into 290 * multiple DMA requests, we have to manage the sync data carefully. 291 * There can only be one presync sem. That needs to be on every xfer 292 * so that the DMA engine doesn't transfer data before the receiver is 293 * ready. We only do the doorbell and postsync sems after the xfer. 294 * To guarantee previous xfers for the request are complete, we use a 295 * fence. 296 */ 297 dev_addr = req->dev_addr; 298 for_each_sgtable_sg(slice->sgt, sg, i) { 299 slice->reqs[i].cmd = cmd; 300 slice->reqs[i].src_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ? 301 sg_dma_address(sg) : dev_addr); 302 slice->reqs[i].dest_addr = cpu_to_le64(slice->dir == DMA_TO_DEVICE ? 303 dev_addr : sg_dma_address(sg)); 304 /* 305 * sg_dma_len(sg) returns size of a DMA segment, maximum DMA 306 * segment size is set to UINT_MAX by qaic and hence return 307 * values of sg_dma_len(sg) can never exceed u32 range. So, 308 * by down sizing we are not corrupting the value. 309 */ 310 slice->reqs[i].len = cpu_to_le32((u32)sg_dma_len(sg)); 311 switch (presync_sem) { 312 case BIT(0): 313 slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val, 314 req->sem0.index, 315 req->sem0.presync, 316 req->sem0.cmd, 317 req->sem0.flags)); 318 break; 319 case BIT(1): 320 slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val, 321 req->sem1.index, 322 req->sem1.presync, 323 req->sem1.cmd, 324 req->sem1.flags)); 325 break; 326 case BIT(2): 327 slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val, 328 req->sem2.index, 329 req->sem2.presync, 330 req->sem2.cmd, 331 req->sem2.flags)); 332 break; 333 case BIT(3): 334 slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val, 335 req->sem3.index, 336 req->sem3.presync, 337 req->sem3.cmd, 338 req->sem3.flags)); 339 break; 340 } 341 dev_addr += sg_dma_len(sg); 342 } 343 /* add post transfer stuff to last segment */ 344 i--; 345 slice->reqs[i].cmd |= GEN_COMPLETION; 346 slice->reqs[i].db_addr = db_addr; 347 slice->reqs[i].db_len = db_len; 348 slice->reqs[i].db_data = db_data; 349 /* 350 * Add a fence if we have more than one request going to the hardware 351 * representing the entirety of the user request, and the user request 352 * has no presync condition. 353 * Fences are expensive, so we try to avoid them. We rely on the 354 * hardware behavior to avoid needing one when there is a presync 355 * condition. When a presync exists, all requests for that same 356 * presync will be queued into a fifo. Thus, since we queue the 357 * post xfer activity only on the last request we queue, the hardware 358 * will ensure that the last queued request is processed last, thus 359 * making sure the post xfer activity happens at the right time without 360 * a fence. 361 */ 362 if (i && !presync_sem) 363 req->sem0.flags |= (slice->dir == DMA_TO_DEVICE ? 364 QAIC_SEM_INSYNCFENCE : QAIC_SEM_OUTSYNCFENCE); 365 slice->reqs[i].sem_cmd0 = cpu_to_le32(ENCODE_SEM(req->sem0.val, req->sem0.index, 366 req->sem0.presync, req->sem0.cmd, 367 req->sem0.flags)); 368 slice->reqs[i].sem_cmd1 = cpu_to_le32(ENCODE_SEM(req->sem1.val, req->sem1.index, 369 req->sem1.presync, req->sem1.cmd, 370 req->sem1.flags)); 371 slice->reqs[i].sem_cmd2 = cpu_to_le32(ENCODE_SEM(req->sem2.val, req->sem2.index, 372 req->sem2.presync, req->sem2.cmd, 373 req->sem2.flags)); 374 slice->reqs[i].sem_cmd3 = cpu_to_le32(ENCODE_SEM(req->sem3.val, req->sem3.index, 375 req->sem3.presync, req->sem3.cmd, 376 req->sem3.flags)); 377 378 return 0; 379 } 380 381 static int qaic_map_one_slice(struct qaic_device *qdev, struct qaic_bo *bo, 382 struct qaic_attach_slice_entry *slice_ent) 383 { 384 struct sg_table *sgt = NULL; 385 struct bo_slice *slice; 386 int ret; 387 388 ret = clone_range_of_sgt_for_slice(qdev, &sgt, bo->sgt, slice_ent->size, slice_ent->offset); 389 if (ret) 390 goto out; 391 392 slice = kmalloc(sizeof(*slice), GFP_KERNEL); 393 if (!slice) { 394 ret = -ENOMEM; 395 goto free_sgt; 396 } 397 398 slice->reqs = kcalloc(sgt->nents, sizeof(*slice->reqs), GFP_KERNEL); 399 if (!slice->reqs) { 400 ret = -ENOMEM; 401 goto free_slice; 402 } 403 404 slice->no_xfer = !slice_ent->size; 405 slice->sgt = sgt; 406 slice->nents = sgt->nents; 407 slice->dir = bo->dir; 408 slice->bo = bo; 409 slice->size = slice_ent->size; 410 slice->offset = slice_ent->offset; 411 412 ret = encode_reqs(qdev, slice, slice_ent); 413 if (ret) 414 goto free_req; 415 416 bo->total_slice_nents += sgt->nents; 417 kref_init(&slice->ref_count); 418 drm_gem_object_get(&bo->base); 419 list_add_tail(&slice->slice, &bo->slices); 420 421 return 0; 422 423 free_req: 424 kfree(slice->reqs); 425 free_slice: 426 kfree(slice); 427 free_sgt: 428 sg_free_table(sgt); 429 kfree(sgt); 430 out: 431 return ret; 432 } 433 434 static int create_sgt(struct qaic_device *qdev, struct sg_table **sgt_out, u64 size) 435 { 436 struct scatterlist *sg; 437 struct sg_table *sgt; 438 struct page **pages; 439 int *pages_order; 440 int buf_extra; 441 int max_order; 442 int nr_pages; 443 int ret = 0; 444 int i, j, k; 445 int order; 446 447 if (size) { 448 nr_pages = DIV_ROUND_UP(size, PAGE_SIZE); 449 /* 450 * calculate how much extra we are going to allocate, to remove 451 * later 452 */ 453 buf_extra = (PAGE_SIZE - size % PAGE_SIZE) % PAGE_SIZE; 454 max_order = min(MAX_ORDER - 1, get_order(size)); 455 } else { 456 /* allocate a single page for book keeping */ 457 nr_pages = 1; 458 buf_extra = 0; 459 max_order = 0; 460 } 461 462 pages = kvmalloc_array(nr_pages, sizeof(*pages) + sizeof(*pages_order), GFP_KERNEL); 463 if (!pages) { 464 ret = -ENOMEM; 465 goto out; 466 } 467 pages_order = (void *)pages + sizeof(*pages) * nr_pages; 468 469 /* 470 * Allocate requested memory using alloc_pages. It is possible to allocate 471 * the requested memory in multiple chunks by calling alloc_pages 472 * multiple times. Use SG table to handle multiple allocated pages. 473 */ 474 i = 0; 475 while (nr_pages > 0) { 476 order = min(get_order(nr_pages * PAGE_SIZE), max_order); 477 while (1) { 478 pages[i] = alloc_pages(GFP_KERNEL | GFP_HIGHUSER | 479 __GFP_NOWARN | __GFP_ZERO | 480 (order ? __GFP_NORETRY : __GFP_RETRY_MAYFAIL), 481 order); 482 if (pages[i]) 483 break; 484 if (!order--) { 485 ret = -ENOMEM; 486 goto free_partial_alloc; 487 } 488 } 489 490 max_order = order; 491 pages_order[i] = order; 492 493 nr_pages -= 1 << order; 494 if (nr_pages <= 0) 495 /* account for over allocation */ 496 buf_extra += abs(nr_pages) * PAGE_SIZE; 497 i++; 498 } 499 500 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); 501 if (!sgt) { 502 ret = -ENOMEM; 503 goto free_partial_alloc; 504 } 505 506 if (sg_alloc_table(sgt, i, GFP_KERNEL)) { 507 ret = -ENOMEM; 508 goto free_sgt; 509 } 510 511 /* Populate the SG table with the allocated memory pages */ 512 sg = sgt->sgl; 513 for (k = 0; k < i; k++, sg = sg_next(sg)) { 514 /* Last entry requires special handling */ 515 if (k < i - 1) { 516 sg_set_page(sg, pages[k], PAGE_SIZE << pages_order[k], 0); 517 } else { 518 sg_set_page(sg, pages[k], (PAGE_SIZE << pages_order[k]) - buf_extra, 0); 519 sg_mark_end(sg); 520 } 521 } 522 523 kvfree(pages); 524 *sgt_out = sgt; 525 return ret; 526 527 free_sgt: 528 kfree(sgt); 529 free_partial_alloc: 530 for (j = 0; j < i; j++) 531 __free_pages(pages[j], pages_order[j]); 532 kvfree(pages); 533 out: 534 *sgt_out = NULL; 535 return ret; 536 } 537 538 static bool invalid_sem(struct qaic_sem *sem) 539 { 540 if (sem->val & ~SEM_VAL_MASK || sem->index & ~SEM_INDEX_MASK || 541 !(sem->presync == 0 || sem->presync == 1) || sem->pad || 542 sem->flags & ~(QAIC_SEM_INSYNCFENCE | QAIC_SEM_OUTSYNCFENCE) || 543 sem->cmd > QAIC_SEM_WAIT_GT_0) 544 return true; 545 return false; 546 } 547 548 static int qaic_validate_req(struct qaic_device *qdev, struct qaic_attach_slice_entry *slice_ent, 549 u32 count, u64 total_size) 550 { 551 int i; 552 553 for (i = 0; i < count; i++) { 554 if (!(slice_ent[i].db_len == 32 || slice_ent[i].db_len == 16 || 555 slice_ent[i].db_len == 8 || slice_ent[i].db_len == 0) || 556 invalid_sem(&slice_ent[i].sem0) || invalid_sem(&slice_ent[i].sem1) || 557 invalid_sem(&slice_ent[i].sem2) || invalid_sem(&slice_ent[i].sem3)) 558 return -EINVAL; 559 560 if (slice_ent[i].offset + slice_ent[i].size > total_size) 561 return -EINVAL; 562 } 563 564 return 0; 565 } 566 567 static void qaic_free_sgt(struct sg_table *sgt) 568 { 569 struct scatterlist *sg; 570 571 for (sg = sgt->sgl; sg; sg = sg_next(sg)) 572 if (sg_page(sg)) 573 __free_pages(sg_page(sg), get_order(sg->length)); 574 sg_free_table(sgt); 575 kfree(sgt); 576 } 577 578 static void qaic_gem_print_info(struct drm_printer *p, unsigned int indent, 579 const struct drm_gem_object *obj) 580 { 581 struct qaic_bo *bo = to_qaic_bo(obj); 582 583 drm_printf_indent(p, indent, "BO DMA direction %d\n", bo->dir); 584 } 585 586 static const struct vm_operations_struct drm_vm_ops = { 587 .open = drm_gem_vm_open, 588 .close = drm_gem_vm_close, 589 }; 590 591 static int qaic_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 592 { 593 struct qaic_bo *bo = to_qaic_bo(obj); 594 unsigned long offset = 0; 595 struct scatterlist *sg; 596 int ret = 0; 597 598 if (obj->import_attach) 599 return -EINVAL; 600 601 for (sg = bo->sgt->sgl; sg; sg = sg_next(sg)) { 602 if (sg_page(sg)) { 603 ret = remap_pfn_range(vma, vma->vm_start + offset, page_to_pfn(sg_page(sg)), 604 sg->length, vma->vm_page_prot); 605 if (ret) 606 goto out; 607 offset += sg->length; 608 } 609 } 610 611 out: 612 return ret; 613 } 614 615 static void qaic_free_object(struct drm_gem_object *obj) 616 { 617 struct qaic_bo *bo = to_qaic_bo(obj); 618 619 if (obj->import_attach) { 620 /* DMABUF/PRIME Path */ 621 drm_prime_gem_destroy(obj, NULL); 622 } else { 623 /* Private buffer allocation path */ 624 qaic_free_sgt(bo->sgt); 625 } 626 627 mutex_destroy(&bo->lock); 628 drm_gem_object_release(obj); 629 kfree(bo); 630 } 631 632 static const struct drm_gem_object_funcs qaic_gem_funcs = { 633 .free = qaic_free_object, 634 .print_info = qaic_gem_print_info, 635 .mmap = qaic_gem_object_mmap, 636 .vm_ops = &drm_vm_ops, 637 }; 638 639 static void qaic_init_bo(struct qaic_bo *bo, bool reinit) 640 { 641 if (reinit) { 642 bo->sliced = false; 643 reinit_completion(&bo->xfer_done); 644 } else { 645 mutex_init(&bo->lock); 646 init_completion(&bo->xfer_done); 647 } 648 complete_all(&bo->xfer_done); 649 INIT_LIST_HEAD(&bo->slices); 650 } 651 652 static struct qaic_bo *qaic_alloc_init_bo(void) 653 { 654 struct qaic_bo *bo; 655 656 bo = kzalloc(sizeof(*bo), GFP_KERNEL); 657 if (!bo) 658 return ERR_PTR(-ENOMEM); 659 660 qaic_init_bo(bo, false); 661 662 return bo; 663 } 664 665 int qaic_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 666 { 667 struct qaic_create_bo *args = data; 668 int usr_rcu_id, qdev_rcu_id; 669 struct drm_gem_object *obj; 670 struct qaic_device *qdev; 671 struct qaic_user *usr; 672 struct qaic_bo *bo; 673 size_t size; 674 int ret; 675 676 if (args->pad) 677 return -EINVAL; 678 679 size = PAGE_ALIGN(args->size); 680 if (size == 0) 681 return -EINVAL; 682 683 usr = file_priv->driver_priv; 684 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 685 if (!usr->qddev) { 686 ret = -ENODEV; 687 goto unlock_usr_srcu; 688 } 689 690 qdev = usr->qddev->qdev; 691 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 692 if (qdev->in_reset) { 693 ret = -ENODEV; 694 goto unlock_dev_srcu; 695 } 696 697 bo = qaic_alloc_init_bo(); 698 if (IS_ERR(bo)) { 699 ret = PTR_ERR(bo); 700 goto unlock_dev_srcu; 701 } 702 obj = &bo->base; 703 704 drm_gem_private_object_init(dev, obj, size); 705 706 obj->funcs = &qaic_gem_funcs; 707 ret = create_sgt(qdev, &bo->sgt, size); 708 if (ret) 709 goto free_bo; 710 711 ret = drm_gem_handle_create(file_priv, obj, &args->handle); 712 if (ret) 713 goto free_sgt; 714 715 bo->handle = args->handle; 716 drm_gem_object_put(obj); 717 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 718 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 719 720 return 0; 721 722 free_sgt: 723 qaic_free_sgt(bo->sgt); 724 free_bo: 725 kfree(bo); 726 unlock_dev_srcu: 727 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 728 unlock_usr_srcu: 729 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 730 return ret; 731 } 732 733 int qaic_mmap_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 734 { 735 struct qaic_mmap_bo *args = data; 736 int usr_rcu_id, qdev_rcu_id; 737 struct drm_gem_object *obj; 738 struct qaic_device *qdev; 739 struct qaic_user *usr; 740 int ret; 741 742 usr = file_priv->driver_priv; 743 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 744 if (!usr->qddev) { 745 ret = -ENODEV; 746 goto unlock_usr_srcu; 747 } 748 749 qdev = usr->qddev->qdev; 750 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 751 if (qdev->in_reset) { 752 ret = -ENODEV; 753 goto unlock_dev_srcu; 754 } 755 756 obj = drm_gem_object_lookup(file_priv, args->handle); 757 if (!obj) { 758 ret = -ENOENT; 759 goto unlock_dev_srcu; 760 } 761 762 ret = drm_gem_create_mmap_offset(obj); 763 if (ret == 0) 764 args->offset = drm_vma_node_offset_addr(&obj->vma_node); 765 766 drm_gem_object_put(obj); 767 768 unlock_dev_srcu: 769 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 770 unlock_usr_srcu: 771 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 772 return ret; 773 } 774 775 struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf) 776 { 777 struct dma_buf_attachment *attach; 778 struct drm_gem_object *obj; 779 struct qaic_bo *bo; 780 size_t size; 781 int ret; 782 783 bo = qaic_alloc_init_bo(); 784 if (IS_ERR(bo)) { 785 ret = PTR_ERR(bo); 786 goto out; 787 } 788 789 obj = &bo->base; 790 get_dma_buf(dma_buf); 791 792 attach = dma_buf_attach(dma_buf, dev->dev); 793 if (IS_ERR(attach)) { 794 ret = PTR_ERR(attach); 795 goto attach_fail; 796 } 797 798 size = PAGE_ALIGN(attach->dmabuf->size); 799 if (size == 0) { 800 ret = -EINVAL; 801 goto size_align_fail; 802 } 803 804 drm_gem_private_object_init(dev, obj, size); 805 /* 806 * skipping dma_buf_map_attachment() as we do not know the direction 807 * just yet. Once the direction is known in the subsequent IOCTL to 808 * attach slicing, we can do it then. 809 */ 810 811 obj->funcs = &qaic_gem_funcs; 812 obj->import_attach = attach; 813 obj->resv = dma_buf->resv; 814 815 return obj; 816 817 size_align_fail: 818 dma_buf_detach(dma_buf, attach); 819 attach_fail: 820 dma_buf_put(dma_buf); 821 kfree(bo); 822 out: 823 return ERR_PTR(ret); 824 } 825 826 static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_hdr *hdr) 827 { 828 struct drm_gem_object *obj = &bo->base; 829 struct sg_table *sgt; 830 int ret; 831 832 if (obj->import_attach->dmabuf->size < hdr->size) 833 return -EINVAL; 834 835 sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir); 836 if (IS_ERR(sgt)) { 837 ret = PTR_ERR(sgt); 838 return ret; 839 } 840 841 bo->sgt = sgt; 842 843 return 0; 844 } 845 846 static int qaic_prepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo, 847 struct qaic_attach_slice_hdr *hdr) 848 { 849 int ret; 850 851 if (bo->base.size < hdr->size) 852 return -EINVAL; 853 854 ret = dma_map_sgtable(&qdev->pdev->dev, bo->sgt, hdr->dir, 0); 855 if (ret) 856 return -EFAULT; 857 858 return 0; 859 } 860 861 static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo, 862 struct qaic_attach_slice_hdr *hdr) 863 { 864 int ret; 865 866 if (bo->base.import_attach) 867 ret = qaic_prepare_import_bo(bo, hdr); 868 else 869 ret = qaic_prepare_export_bo(qdev, bo, hdr); 870 bo->dir = hdr->dir; 871 bo->dbc = &qdev->dbc[hdr->dbc_id]; 872 bo->nr_slice = hdr->count; 873 874 return ret; 875 } 876 877 static void qaic_unprepare_import_bo(struct qaic_bo *bo) 878 { 879 dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir); 880 bo->sgt = NULL; 881 } 882 883 static void qaic_unprepare_export_bo(struct qaic_device *qdev, struct qaic_bo *bo) 884 { 885 dma_unmap_sgtable(&qdev->pdev->dev, bo->sgt, bo->dir, 0); 886 } 887 888 static void qaic_unprepare_bo(struct qaic_device *qdev, struct qaic_bo *bo) 889 { 890 if (bo->base.import_attach) 891 qaic_unprepare_import_bo(bo); 892 else 893 qaic_unprepare_export_bo(qdev, bo); 894 895 bo->dir = 0; 896 bo->dbc = NULL; 897 bo->nr_slice = 0; 898 } 899 900 static void qaic_free_slices_bo(struct qaic_bo *bo) 901 { 902 struct bo_slice *slice, *temp; 903 904 list_for_each_entry_safe(slice, temp, &bo->slices, slice) 905 kref_put(&slice->ref_count, free_slice); 906 if (WARN_ON_ONCE(bo->total_slice_nents != 0)) 907 bo->total_slice_nents = 0; 908 bo->nr_slice = 0; 909 } 910 911 static int qaic_attach_slicing_bo(struct qaic_device *qdev, struct qaic_bo *bo, 912 struct qaic_attach_slice_hdr *hdr, 913 struct qaic_attach_slice_entry *slice_ent) 914 { 915 int ret, i; 916 917 for (i = 0; i < hdr->count; i++) { 918 ret = qaic_map_one_slice(qdev, bo, &slice_ent[i]); 919 if (ret) { 920 qaic_free_slices_bo(bo); 921 return ret; 922 } 923 } 924 925 if (bo->total_slice_nents > bo->dbc->nelem) { 926 qaic_free_slices_bo(bo); 927 return -ENOSPC; 928 } 929 930 return 0; 931 } 932 933 int qaic_attach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 934 { 935 struct qaic_attach_slice_entry *slice_ent; 936 struct qaic_attach_slice *args = data; 937 int rcu_id, usr_rcu_id, qdev_rcu_id; 938 struct dma_bridge_chan *dbc; 939 struct drm_gem_object *obj; 940 struct qaic_device *qdev; 941 unsigned long arg_size; 942 struct qaic_user *usr; 943 u8 __user *user_data; 944 struct qaic_bo *bo; 945 int ret; 946 947 if (args->hdr.count == 0) 948 return -EINVAL; 949 950 arg_size = args->hdr.count * sizeof(*slice_ent); 951 if (arg_size / args->hdr.count != sizeof(*slice_ent)) 952 return -EINVAL; 953 954 if (args->hdr.size == 0) 955 return -EINVAL; 956 957 if (!(args->hdr.dir == DMA_TO_DEVICE || args->hdr.dir == DMA_FROM_DEVICE)) 958 return -EINVAL; 959 960 if (args->data == 0) 961 return -EINVAL; 962 963 usr = file_priv->driver_priv; 964 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 965 if (!usr->qddev) { 966 ret = -ENODEV; 967 goto unlock_usr_srcu; 968 } 969 970 qdev = usr->qddev->qdev; 971 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 972 if (qdev->in_reset) { 973 ret = -ENODEV; 974 goto unlock_dev_srcu; 975 } 976 977 if (args->hdr.dbc_id >= qdev->num_dbc) { 978 ret = -EINVAL; 979 goto unlock_dev_srcu; 980 } 981 982 user_data = u64_to_user_ptr(args->data); 983 984 slice_ent = kzalloc(arg_size, GFP_KERNEL); 985 if (!slice_ent) { 986 ret = -EINVAL; 987 goto unlock_dev_srcu; 988 } 989 990 ret = copy_from_user(slice_ent, user_data, arg_size); 991 if (ret) { 992 ret = -EFAULT; 993 goto free_slice_ent; 994 } 995 996 ret = qaic_validate_req(qdev, slice_ent, args->hdr.count, args->hdr.size); 997 if (ret) 998 goto free_slice_ent; 999 1000 obj = drm_gem_object_lookup(file_priv, args->hdr.handle); 1001 if (!obj) { 1002 ret = -ENOENT; 1003 goto free_slice_ent; 1004 } 1005 1006 bo = to_qaic_bo(obj); 1007 ret = mutex_lock_interruptible(&bo->lock); 1008 if (ret) 1009 goto put_bo; 1010 1011 if (bo->sliced) { 1012 ret = -EINVAL; 1013 goto unlock_bo; 1014 } 1015 1016 dbc = &qdev->dbc[args->hdr.dbc_id]; 1017 rcu_id = srcu_read_lock(&dbc->ch_lock); 1018 if (dbc->usr != usr) { 1019 ret = -EINVAL; 1020 goto unlock_ch_srcu; 1021 } 1022 1023 ret = qaic_prepare_bo(qdev, bo, &args->hdr); 1024 if (ret) 1025 goto unlock_ch_srcu; 1026 1027 ret = qaic_attach_slicing_bo(qdev, bo, &args->hdr, slice_ent); 1028 if (ret) 1029 goto unprepare_bo; 1030 1031 if (args->hdr.dir == DMA_TO_DEVICE) 1032 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, args->hdr.dir); 1033 1034 bo->sliced = true; 1035 list_add_tail(&bo->bo_list, &bo->dbc->bo_lists); 1036 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1037 mutex_unlock(&bo->lock); 1038 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1039 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1040 1041 return 0; 1042 1043 unprepare_bo: 1044 qaic_unprepare_bo(qdev, bo); 1045 unlock_ch_srcu: 1046 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1047 unlock_bo: 1048 mutex_unlock(&bo->lock); 1049 put_bo: 1050 drm_gem_object_put(obj); 1051 free_slice_ent: 1052 kfree(slice_ent); 1053 unlock_dev_srcu: 1054 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1055 unlock_usr_srcu: 1056 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1057 return ret; 1058 } 1059 1060 static inline int copy_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice, u32 dbc_id, 1061 u32 head, u32 *ptail) 1062 { 1063 struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id]; 1064 struct dbc_req *reqs = slice->reqs; 1065 u32 tail = *ptail; 1066 u32 avail; 1067 1068 avail = head - tail; 1069 if (head <= tail) 1070 avail += dbc->nelem; 1071 1072 --avail; 1073 1074 if (avail < slice->nents) 1075 return -EAGAIN; 1076 1077 if (tail + slice->nents > dbc->nelem) { 1078 avail = dbc->nelem - tail; 1079 avail = min_t(u32, avail, slice->nents); 1080 memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs, 1081 sizeof(*reqs) * avail); 1082 reqs += avail; 1083 avail = slice->nents - avail; 1084 if (avail) 1085 memcpy(dbc->req_q_base, reqs, sizeof(*reqs) * avail); 1086 } else { 1087 memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs, 1088 sizeof(*reqs) * slice->nents); 1089 } 1090 1091 *ptail = (tail + slice->nents) % dbc->nelem; 1092 1093 return 0; 1094 } 1095 1096 /* 1097 * Based on the value of resize we may only need to transmit first_n 1098 * entries and the last entry, with last_bytes to send from the last entry. 1099 * Note that first_n could be 0. 1100 */ 1101 static inline int copy_partial_exec_reqs(struct qaic_device *qdev, struct bo_slice *slice, 1102 u64 resize, u32 dbc_id, u32 head, u32 *ptail) 1103 { 1104 struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id]; 1105 struct dbc_req *reqs = slice->reqs; 1106 struct dbc_req *last_req; 1107 u32 tail = *ptail; 1108 u64 total_bytes; 1109 u64 last_bytes; 1110 u32 first_n; 1111 u32 avail; 1112 int ret; 1113 int i; 1114 1115 avail = head - tail; 1116 if (head <= tail) 1117 avail += dbc->nelem; 1118 1119 --avail; 1120 1121 total_bytes = 0; 1122 for (i = 0; i < slice->nents; i++) { 1123 total_bytes += le32_to_cpu(reqs[i].len); 1124 if (total_bytes >= resize) 1125 break; 1126 } 1127 1128 if (total_bytes < resize) { 1129 /* User space should have used the full buffer path. */ 1130 ret = -EINVAL; 1131 return ret; 1132 } 1133 1134 first_n = i; 1135 last_bytes = i ? resize + le32_to_cpu(reqs[i].len) - total_bytes : resize; 1136 1137 if (avail < (first_n + 1)) 1138 return -EAGAIN; 1139 1140 if (first_n) { 1141 if (tail + first_n > dbc->nelem) { 1142 avail = dbc->nelem - tail; 1143 avail = min_t(u32, avail, first_n); 1144 memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs, 1145 sizeof(*reqs) * avail); 1146 last_req = reqs + avail; 1147 avail = first_n - avail; 1148 if (avail) 1149 memcpy(dbc->req_q_base, last_req, sizeof(*reqs) * avail); 1150 } else { 1151 memcpy(dbc->req_q_base + tail * get_dbc_req_elem_size(), reqs, 1152 sizeof(*reqs) * first_n); 1153 } 1154 } 1155 1156 /* Copy over the last entry. Here we need to adjust len to the left over 1157 * size, and set src and dst to the entry it is copied to. 1158 */ 1159 last_req = dbc->req_q_base + (tail + first_n) % dbc->nelem * get_dbc_req_elem_size(); 1160 memcpy(last_req, reqs + slice->nents - 1, sizeof(*reqs)); 1161 1162 /* 1163 * last_bytes holds size of a DMA segment, maximum DMA segment size is 1164 * set to UINT_MAX by qaic and hence last_bytes can never exceed u32 1165 * range. So, by down sizing we are not corrupting the value. 1166 */ 1167 last_req->len = cpu_to_le32((u32)last_bytes); 1168 last_req->src_addr = reqs[first_n].src_addr; 1169 last_req->dest_addr = reqs[first_n].dest_addr; 1170 1171 *ptail = (tail + first_n + 1) % dbc->nelem; 1172 1173 return 0; 1174 } 1175 1176 static int send_bo_list_to_device(struct qaic_device *qdev, struct drm_file *file_priv, 1177 struct qaic_execute_entry *exec, unsigned int count, 1178 bool is_partial, struct dma_bridge_chan *dbc, u32 head, 1179 u32 *tail) 1180 { 1181 struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec; 1182 struct drm_gem_object *obj; 1183 struct bo_slice *slice; 1184 unsigned long flags; 1185 struct qaic_bo *bo; 1186 bool queued; 1187 int i, j; 1188 int ret; 1189 1190 for (i = 0; i < count; i++) { 1191 /* 1192 * ref count will be decremented when the transfer of this 1193 * buffer is complete. It is inside dbc_irq_threaded_fn(). 1194 */ 1195 obj = drm_gem_object_lookup(file_priv, 1196 is_partial ? pexec[i].handle : exec[i].handle); 1197 if (!obj) { 1198 ret = -ENOENT; 1199 goto failed_to_send_bo; 1200 } 1201 1202 bo = to_qaic_bo(obj); 1203 ret = mutex_lock_interruptible(&bo->lock); 1204 if (ret) 1205 goto failed_to_send_bo; 1206 1207 if (!bo->sliced) { 1208 ret = -EINVAL; 1209 goto unlock_bo; 1210 } 1211 1212 if (is_partial && pexec[i].resize > bo->base.size) { 1213 ret = -EINVAL; 1214 goto unlock_bo; 1215 } 1216 1217 spin_lock_irqsave(&dbc->xfer_lock, flags); 1218 queued = bo->queued; 1219 bo->queued = true; 1220 if (queued) { 1221 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1222 ret = -EINVAL; 1223 goto unlock_bo; 1224 } 1225 1226 bo->req_id = dbc->next_req_id++; 1227 1228 list_for_each_entry(slice, &bo->slices, slice) { 1229 /* 1230 * If this slice does not fall under the given 1231 * resize then skip this slice and continue the loop 1232 */ 1233 if (is_partial && pexec[i].resize && pexec[i].resize <= slice->offset) 1234 continue; 1235 1236 for (j = 0; j < slice->nents; j++) 1237 slice->reqs[j].req_id = cpu_to_le16(bo->req_id); 1238 1239 /* 1240 * If it is a partial execute ioctl call then check if 1241 * resize has cut this slice short then do a partial copy 1242 * else do complete copy 1243 */ 1244 if (is_partial && pexec[i].resize && 1245 pexec[i].resize < slice->offset + slice->size) 1246 ret = copy_partial_exec_reqs(qdev, slice, 1247 pexec[i].resize - slice->offset, 1248 dbc->id, head, tail); 1249 else 1250 ret = copy_exec_reqs(qdev, slice, dbc->id, head, tail); 1251 if (ret) { 1252 bo->queued = false; 1253 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1254 goto unlock_bo; 1255 } 1256 } 1257 reinit_completion(&bo->xfer_done); 1258 list_add_tail(&bo->xfer_list, &dbc->xfer_list); 1259 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1260 dma_sync_sgtable_for_device(&qdev->pdev->dev, bo->sgt, bo->dir); 1261 mutex_unlock(&bo->lock); 1262 } 1263 1264 return 0; 1265 1266 unlock_bo: 1267 mutex_unlock(&bo->lock); 1268 failed_to_send_bo: 1269 if (likely(obj)) 1270 drm_gem_object_put(obj); 1271 for (j = 0; j < i; j++) { 1272 spin_lock_irqsave(&dbc->xfer_lock, flags); 1273 bo = list_last_entry(&dbc->xfer_list, struct qaic_bo, xfer_list); 1274 obj = &bo->base; 1275 bo->queued = false; 1276 list_del(&bo->xfer_list); 1277 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1278 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir); 1279 drm_gem_object_put(obj); 1280 } 1281 return ret; 1282 } 1283 1284 static void update_profiling_data(struct drm_file *file_priv, 1285 struct qaic_execute_entry *exec, unsigned int count, 1286 bool is_partial, u64 received_ts, u64 submit_ts, u32 queue_level) 1287 { 1288 struct qaic_partial_execute_entry *pexec = (struct qaic_partial_execute_entry *)exec; 1289 struct drm_gem_object *obj; 1290 struct qaic_bo *bo; 1291 int i; 1292 1293 for (i = 0; i < count; i++) { 1294 /* 1295 * Since we already committed the BO to hardware, the only way 1296 * this should fail is a pending signal. We can't cancel the 1297 * submit to hardware, so we have to just skip the profiling 1298 * data. In case the signal is not fatal to the process, we 1299 * return success so that the user doesn't try to resubmit. 1300 */ 1301 obj = drm_gem_object_lookup(file_priv, 1302 is_partial ? pexec[i].handle : exec[i].handle); 1303 if (!obj) 1304 break; 1305 bo = to_qaic_bo(obj); 1306 bo->perf_stats.req_received_ts = received_ts; 1307 bo->perf_stats.req_submit_ts = submit_ts; 1308 bo->perf_stats.queue_level_before = queue_level; 1309 queue_level += bo->total_slice_nents; 1310 drm_gem_object_put(obj); 1311 } 1312 } 1313 1314 static int __qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv, 1315 bool is_partial) 1316 { 1317 struct qaic_execute *args = data; 1318 struct qaic_execute_entry *exec; 1319 struct dma_bridge_chan *dbc; 1320 int usr_rcu_id, qdev_rcu_id; 1321 struct qaic_device *qdev; 1322 struct qaic_user *usr; 1323 u8 __user *user_data; 1324 unsigned long n; 1325 u64 received_ts; 1326 u32 queue_level; 1327 u64 submit_ts; 1328 int rcu_id; 1329 u32 head; 1330 u32 tail; 1331 u64 size; 1332 int ret; 1333 1334 received_ts = ktime_get_ns(); 1335 1336 size = is_partial ? sizeof(struct qaic_partial_execute_entry) : sizeof(*exec); 1337 n = (unsigned long)size * args->hdr.count; 1338 if (args->hdr.count == 0 || n / args->hdr.count != size) 1339 return -EINVAL; 1340 1341 user_data = u64_to_user_ptr(args->data); 1342 1343 exec = kcalloc(args->hdr.count, size, GFP_KERNEL); 1344 if (!exec) 1345 return -ENOMEM; 1346 1347 if (copy_from_user(exec, user_data, n)) { 1348 ret = -EFAULT; 1349 goto free_exec; 1350 } 1351 1352 usr = file_priv->driver_priv; 1353 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 1354 if (!usr->qddev) { 1355 ret = -ENODEV; 1356 goto unlock_usr_srcu; 1357 } 1358 1359 qdev = usr->qddev->qdev; 1360 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 1361 if (qdev->in_reset) { 1362 ret = -ENODEV; 1363 goto unlock_dev_srcu; 1364 } 1365 1366 if (args->hdr.dbc_id >= qdev->num_dbc) { 1367 ret = -EINVAL; 1368 goto unlock_dev_srcu; 1369 } 1370 1371 dbc = &qdev->dbc[args->hdr.dbc_id]; 1372 1373 rcu_id = srcu_read_lock(&dbc->ch_lock); 1374 if (!dbc->usr || dbc->usr->handle != usr->handle) { 1375 ret = -EPERM; 1376 goto release_ch_rcu; 1377 } 1378 1379 head = readl(dbc->dbc_base + REQHP_OFF); 1380 tail = readl(dbc->dbc_base + REQTP_OFF); 1381 1382 if (head == U32_MAX || tail == U32_MAX) { 1383 /* PCI link error */ 1384 ret = -ENODEV; 1385 goto release_ch_rcu; 1386 } 1387 1388 queue_level = head <= tail ? tail - head : dbc->nelem - (head - tail); 1389 1390 ret = send_bo_list_to_device(qdev, file_priv, exec, args->hdr.count, is_partial, dbc, 1391 head, &tail); 1392 if (ret) 1393 goto release_ch_rcu; 1394 1395 /* Finalize commit to hardware */ 1396 submit_ts = ktime_get_ns(); 1397 writel(tail, dbc->dbc_base + REQTP_OFF); 1398 1399 update_profiling_data(file_priv, exec, args->hdr.count, is_partial, received_ts, 1400 submit_ts, queue_level); 1401 1402 if (datapath_polling) 1403 schedule_work(&dbc->poll_work); 1404 1405 release_ch_rcu: 1406 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1407 unlock_dev_srcu: 1408 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1409 unlock_usr_srcu: 1410 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1411 free_exec: 1412 kfree(exec); 1413 return ret; 1414 } 1415 1416 int qaic_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 1417 { 1418 return __qaic_execute_bo_ioctl(dev, data, file_priv, false); 1419 } 1420 1421 int qaic_partial_execute_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 1422 { 1423 return __qaic_execute_bo_ioctl(dev, data, file_priv, true); 1424 } 1425 1426 /* 1427 * Our interrupt handling is a bit more complicated than a simple ideal, but 1428 * sadly necessary. 1429 * 1430 * Each dbc has a completion queue. Entries in the queue correspond to DMA 1431 * requests which the device has processed. The hardware already has a built 1432 * in irq mitigation. When the device puts an entry into the queue, it will 1433 * only trigger an interrupt if the queue was empty. Therefore, when adding 1434 * the Nth event to a non-empty queue, the hardware doesn't trigger an 1435 * interrupt. This means the host doesn't get additional interrupts signaling 1436 * the same thing - the queue has something to process. 1437 * This behavior can be overridden in the DMA request. 1438 * This means that when the host receives an interrupt, it is required to 1439 * drain the queue. 1440 * 1441 * This behavior is what NAPI attempts to accomplish, although we can't use 1442 * NAPI as we don't have a netdev. We use threaded irqs instead. 1443 * 1444 * However, there is a situation where the host drains the queue fast enough 1445 * that every event causes an interrupt. Typically this is not a problem as 1446 * the rate of events would be low. However, that is not the case with 1447 * lprnet for example. On an Intel Xeon D-2191 where we run 8 instances of 1448 * lprnet, the host receives roughly 80k interrupts per second from the device 1449 * (per /proc/interrupts). While NAPI documentation indicates the host should 1450 * just chug along, sadly that behavior causes instability in some hosts. 1451 * 1452 * Therefore, we implement an interrupt disable scheme similar to NAPI. The 1453 * key difference is that we will delay after draining the queue for a small 1454 * time to allow additional events to come in via polling. Using the above 1455 * lprnet workload, this reduces the number of interrupts processed from 1456 * ~80k/sec to about 64 in 5 minutes and appears to solve the system 1457 * instability. 1458 */ 1459 irqreturn_t dbc_irq_handler(int irq, void *data) 1460 { 1461 struct dma_bridge_chan *dbc = data; 1462 int rcu_id; 1463 u32 head; 1464 u32 tail; 1465 1466 rcu_id = srcu_read_lock(&dbc->ch_lock); 1467 1468 if (!dbc->usr) { 1469 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1470 return IRQ_HANDLED; 1471 } 1472 1473 head = readl(dbc->dbc_base + RSPHP_OFF); 1474 if (head == U32_MAX) { /* PCI link error */ 1475 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1476 return IRQ_NONE; 1477 } 1478 1479 tail = readl(dbc->dbc_base + RSPTP_OFF); 1480 if (tail == U32_MAX) { /* PCI link error */ 1481 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1482 return IRQ_NONE; 1483 } 1484 1485 if (head == tail) { /* queue empty */ 1486 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1487 return IRQ_NONE; 1488 } 1489 1490 disable_irq_nosync(irq); 1491 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1492 return IRQ_WAKE_THREAD; 1493 } 1494 1495 void irq_polling_work(struct work_struct *work) 1496 { 1497 struct dma_bridge_chan *dbc = container_of(work, struct dma_bridge_chan, poll_work); 1498 unsigned long flags; 1499 int rcu_id; 1500 u32 head; 1501 u32 tail; 1502 1503 rcu_id = srcu_read_lock(&dbc->ch_lock); 1504 1505 while (1) { 1506 if (dbc->qdev->in_reset) { 1507 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1508 return; 1509 } 1510 if (!dbc->usr) { 1511 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1512 return; 1513 } 1514 spin_lock_irqsave(&dbc->xfer_lock, flags); 1515 if (list_empty(&dbc->xfer_list)) { 1516 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1517 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1518 return; 1519 } 1520 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1521 1522 head = readl(dbc->dbc_base + RSPHP_OFF); 1523 if (head == U32_MAX) { /* PCI link error */ 1524 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1525 return; 1526 } 1527 1528 tail = readl(dbc->dbc_base + RSPTP_OFF); 1529 if (tail == U32_MAX) { /* PCI link error */ 1530 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1531 return; 1532 } 1533 1534 if (head != tail) { 1535 irq_wake_thread(dbc->irq, dbc); 1536 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1537 return; 1538 } 1539 1540 cond_resched(); 1541 usleep_range(datapath_poll_interval_us, 2 * datapath_poll_interval_us); 1542 } 1543 } 1544 1545 irqreturn_t dbc_irq_threaded_fn(int irq, void *data) 1546 { 1547 struct dma_bridge_chan *dbc = data; 1548 int event_count = NUM_EVENTS; 1549 int delay_count = NUM_DELAYS; 1550 struct qaic_device *qdev; 1551 struct qaic_bo *bo, *i; 1552 struct dbc_rsp *rsp; 1553 unsigned long flags; 1554 int rcu_id; 1555 u16 status; 1556 u16 req_id; 1557 u32 head; 1558 u32 tail; 1559 1560 rcu_id = srcu_read_lock(&dbc->ch_lock); 1561 1562 head = readl(dbc->dbc_base + RSPHP_OFF); 1563 if (head == U32_MAX) /* PCI link error */ 1564 goto error_out; 1565 1566 qdev = dbc->qdev; 1567 read_fifo: 1568 1569 if (!event_count) { 1570 event_count = NUM_EVENTS; 1571 cond_resched(); 1572 } 1573 1574 /* 1575 * if this channel isn't assigned or gets unassigned during processing 1576 * we have nothing further to do 1577 */ 1578 if (!dbc->usr) 1579 goto error_out; 1580 1581 tail = readl(dbc->dbc_base + RSPTP_OFF); 1582 if (tail == U32_MAX) /* PCI link error */ 1583 goto error_out; 1584 1585 if (head == tail) { /* queue empty */ 1586 if (delay_count) { 1587 --delay_count; 1588 usleep_range(100, 200); 1589 goto read_fifo; /* check for a new event */ 1590 } 1591 goto normal_out; 1592 } 1593 1594 delay_count = NUM_DELAYS; 1595 while (head != tail) { 1596 if (!event_count) 1597 break; 1598 --event_count; 1599 rsp = dbc->rsp_q_base + head * sizeof(*rsp); 1600 req_id = le16_to_cpu(rsp->req_id); 1601 status = le16_to_cpu(rsp->status); 1602 if (status) 1603 pci_dbg(qdev->pdev, "req_id %d failed with status %d\n", req_id, status); 1604 spin_lock_irqsave(&dbc->xfer_lock, flags); 1605 /* 1606 * A BO can receive multiple interrupts, since a BO can be 1607 * divided into multiple slices and a buffer receives as many 1608 * interrupts as slices. So until it receives interrupts for 1609 * all the slices we cannot mark that buffer complete. 1610 */ 1611 list_for_each_entry_safe(bo, i, &dbc->xfer_list, xfer_list) { 1612 if (bo->req_id == req_id) 1613 bo->nr_slice_xfer_done++; 1614 else 1615 continue; 1616 1617 if (bo->nr_slice_xfer_done < bo->nr_slice) 1618 break; 1619 1620 /* 1621 * At this point we have received all the interrupts for 1622 * BO, which means BO execution is complete. 1623 */ 1624 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir); 1625 bo->nr_slice_xfer_done = 0; 1626 bo->queued = false; 1627 list_del(&bo->xfer_list); 1628 bo->perf_stats.req_processed_ts = ktime_get_ns(); 1629 complete_all(&bo->xfer_done); 1630 drm_gem_object_put(&bo->base); 1631 break; 1632 } 1633 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1634 head = (head + 1) % dbc->nelem; 1635 } 1636 1637 /* 1638 * Update the head pointer of response queue and let the device know 1639 * that we have consumed elements from the queue. 1640 */ 1641 writel(head, dbc->dbc_base + RSPHP_OFF); 1642 1643 /* elements might have been put in the queue while we were processing */ 1644 goto read_fifo; 1645 1646 normal_out: 1647 if (likely(!datapath_polling)) 1648 enable_irq(irq); 1649 else 1650 schedule_work(&dbc->poll_work); 1651 /* checking the fifo and enabling irqs is a race, missed event check */ 1652 tail = readl(dbc->dbc_base + RSPTP_OFF); 1653 if (tail != U32_MAX && head != tail) { 1654 if (likely(!datapath_polling)) 1655 disable_irq_nosync(irq); 1656 goto read_fifo; 1657 } 1658 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1659 return IRQ_HANDLED; 1660 1661 error_out: 1662 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1663 if (likely(!datapath_polling)) 1664 enable_irq(irq); 1665 else 1666 schedule_work(&dbc->poll_work); 1667 1668 return IRQ_HANDLED; 1669 } 1670 1671 int qaic_wait_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 1672 { 1673 struct qaic_wait *args = data; 1674 int usr_rcu_id, qdev_rcu_id; 1675 struct dma_bridge_chan *dbc; 1676 struct drm_gem_object *obj; 1677 struct qaic_device *qdev; 1678 unsigned long timeout; 1679 struct qaic_user *usr; 1680 struct qaic_bo *bo; 1681 int rcu_id; 1682 int ret; 1683 1684 if (args->pad != 0) 1685 return -EINVAL; 1686 1687 usr = file_priv->driver_priv; 1688 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 1689 if (!usr->qddev) { 1690 ret = -ENODEV; 1691 goto unlock_usr_srcu; 1692 } 1693 1694 qdev = usr->qddev->qdev; 1695 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 1696 if (qdev->in_reset) { 1697 ret = -ENODEV; 1698 goto unlock_dev_srcu; 1699 } 1700 1701 if (args->dbc_id >= qdev->num_dbc) { 1702 ret = -EINVAL; 1703 goto unlock_dev_srcu; 1704 } 1705 1706 dbc = &qdev->dbc[args->dbc_id]; 1707 1708 rcu_id = srcu_read_lock(&dbc->ch_lock); 1709 if (dbc->usr != usr) { 1710 ret = -EPERM; 1711 goto unlock_ch_srcu; 1712 } 1713 1714 obj = drm_gem_object_lookup(file_priv, args->handle); 1715 if (!obj) { 1716 ret = -ENOENT; 1717 goto unlock_ch_srcu; 1718 } 1719 1720 bo = to_qaic_bo(obj); 1721 timeout = args->timeout ? args->timeout : wait_exec_default_timeout_ms; 1722 timeout = msecs_to_jiffies(timeout); 1723 ret = wait_for_completion_interruptible_timeout(&bo->xfer_done, timeout); 1724 if (!ret) { 1725 ret = -ETIMEDOUT; 1726 goto put_obj; 1727 } 1728 if (ret > 0) 1729 ret = 0; 1730 1731 if (!dbc->usr) 1732 ret = -EPERM; 1733 1734 put_obj: 1735 drm_gem_object_put(obj); 1736 unlock_ch_srcu: 1737 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1738 unlock_dev_srcu: 1739 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1740 unlock_usr_srcu: 1741 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1742 return ret; 1743 } 1744 1745 int qaic_perf_stats_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 1746 { 1747 struct qaic_perf_stats_entry *ent = NULL; 1748 struct qaic_perf_stats *args = data; 1749 int usr_rcu_id, qdev_rcu_id; 1750 struct drm_gem_object *obj; 1751 struct qaic_device *qdev; 1752 struct qaic_user *usr; 1753 struct qaic_bo *bo; 1754 int ret, i; 1755 1756 usr = file_priv->driver_priv; 1757 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 1758 if (!usr->qddev) { 1759 ret = -ENODEV; 1760 goto unlock_usr_srcu; 1761 } 1762 1763 qdev = usr->qddev->qdev; 1764 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 1765 if (qdev->in_reset) { 1766 ret = -ENODEV; 1767 goto unlock_dev_srcu; 1768 } 1769 1770 if (args->hdr.dbc_id >= qdev->num_dbc) { 1771 ret = -EINVAL; 1772 goto unlock_dev_srcu; 1773 } 1774 1775 ent = kcalloc(args->hdr.count, sizeof(*ent), GFP_KERNEL); 1776 if (!ent) { 1777 ret = -EINVAL; 1778 goto unlock_dev_srcu; 1779 } 1780 1781 ret = copy_from_user(ent, u64_to_user_ptr(args->data), args->hdr.count * sizeof(*ent)); 1782 if (ret) { 1783 ret = -EFAULT; 1784 goto free_ent; 1785 } 1786 1787 for (i = 0; i < args->hdr.count; i++) { 1788 obj = drm_gem_object_lookup(file_priv, ent[i].handle); 1789 if (!obj) { 1790 ret = -ENOENT; 1791 goto free_ent; 1792 } 1793 bo = to_qaic_bo(obj); 1794 /* 1795 * perf stats ioctl is called before wait ioctl is complete then 1796 * the latency information is invalid. 1797 */ 1798 if (bo->perf_stats.req_processed_ts < bo->perf_stats.req_submit_ts) { 1799 ent[i].device_latency_us = 0; 1800 } else { 1801 ent[i].device_latency_us = div_u64((bo->perf_stats.req_processed_ts - 1802 bo->perf_stats.req_submit_ts), 1000); 1803 } 1804 ent[i].submit_latency_us = div_u64((bo->perf_stats.req_submit_ts - 1805 bo->perf_stats.req_received_ts), 1000); 1806 ent[i].queue_level_before = bo->perf_stats.queue_level_before; 1807 ent[i].num_queue_element = bo->total_slice_nents; 1808 drm_gem_object_put(obj); 1809 } 1810 1811 if (copy_to_user(u64_to_user_ptr(args->data), ent, args->hdr.count * sizeof(*ent))) 1812 ret = -EFAULT; 1813 1814 free_ent: 1815 kfree(ent); 1816 unlock_dev_srcu: 1817 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1818 unlock_usr_srcu: 1819 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1820 return ret; 1821 } 1822 1823 static void detach_slice_bo(struct qaic_device *qdev, struct qaic_bo *bo) 1824 { 1825 qaic_free_slices_bo(bo); 1826 qaic_unprepare_bo(qdev, bo); 1827 qaic_init_bo(bo, true); 1828 list_del(&bo->bo_list); 1829 drm_gem_object_put(&bo->base); 1830 } 1831 1832 int qaic_detach_slice_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) 1833 { 1834 struct qaic_detach_slice *args = data; 1835 int rcu_id, usr_rcu_id, qdev_rcu_id; 1836 struct dma_bridge_chan *dbc; 1837 struct drm_gem_object *obj; 1838 struct qaic_device *qdev; 1839 struct qaic_user *usr; 1840 unsigned long flags; 1841 struct qaic_bo *bo; 1842 int ret; 1843 1844 if (args->pad != 0) 1845 return -EINVAL; 1846 1847 usr = file_priv->driver_priv; 1848 usr_rcu_id = srcu_read_lock(&usr->qddev_lock); 1849 if (!usr->qddev) { 1850 ret = -ENODEV; 1851 goto unlock_usr_srcu; 1852 } 1853 1854 qdev = usr->qddev->qdev; 1855 qdev_rcu_id = srcu_read_lock(&qdev->dev_lock); 1856 if (qdev->in_reset) { 1857 ret = -ENODEV; 1858 goto unlock_dev_srcu; 1859 } 1860 1861 obj = drm_gem_object_lookup(file_priv, args->handle); 1862 if (!obj) { 1863 ret = -ENOENT; 1864 goto unlock_dev_srcu; 1865 } 1866 1867 bo = to_qaic_bo(obj); 1868 ret = mutex_lock_interruptible(&bo->lock); 1869 if (ret) 1870 goto put_bo; 1871 1872 if (!bo->sliced) { 1873 ret = -EINVAL; 1874 goto unlock_bo; 1875 } 1876 1877 dbc = bo->dbc; 1878 rcu_id = srcu_read_lock(&dbc->ch_lock); 1879 if (dbc->usr != usr) { 1880 ret = -EINVAL; 1881 goto unlock_ch_srcu; 1882 } 1883 1884 /* Check if BO is committed to H/W for DMA */ 1885 spin_lock_irqsave(&dbc->xfer_lock, flags); 1886 if (bo->queued) { 1887 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1888 ret = -EBUSY; 1889 goto unlock_ch_srcu; 1890 } 1891 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1892 1893 detach_slice_bo(qdev, bo); 1894 1895 unlock_ch_srcu: 1896 srcu_read_unlock(&dbc->ch_lock, rcu_id); 1897 unlock_bo: 1898 mutex_unlock(&bo->lock); 1899 put_bo: 1900 drm_gem_object_put(obj); 1901 unlock_dev_srcu: 1902 srcu_read_unlock(&qdev->dev_lock, qdev_rcu_id); 1903 unlock_usr_srcu: 1904 srcu_read_unlock(&usr->qddev_lock, usr_rcu_id); 1905 return ret; 1906 } 1907 1908 static void empty_xfer_list(struct qaic_device *qdev, struct dma_bridge_chan *dbc) 1909 { 1910 unsigned long flags; 1911 struct qaic_bo *bo; 1912 1913 spin_lock_irqsave(&dbc->xfer_lock, flags); 1914 while (!list_empty(&dbc->xfer_list)) { 1915 bo = list_first_entry(&dbc->xfer_list, typeof(*bo), xfer_list); 1916 bo->queued = false; 1917 list_del(&bo->xfer_list); 1918 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1919 bo->nr_slice_xfer_done = 0; 1920 bo->req_id = 0; 1921 bo->perf_stats.req_received_ts = 0; 1922 bo->perf_stats.req_submit_ts = 0; 1923 bo->perf_stats.req_processed_ts = 0; 1924 bo->perf_stats.queue_level_before = 0; 1925 dma_sync_sgtable_for_cpu(&qdev->pdev->dev, bo->sgt, bo->dir); 1926 complete_all(&bo->xfer_done); 1927 drm_gem_object_put(&bo->base); 1928 spin_lock_irqsave(&dbc->xfer_lock, flags); 1929 } 1930 spin_unlock_irqrestore(&dbc->xfer_lock, flags); 1931 } 1932 1933 int disable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr) 1934 { 1935 if (!qdev->dbc[dbc_id].usr || qdev->dbc[dbc_id].usr->handle != usr->handle) 1936 return -EPERM; 1937 1938 qdev->dbc[dbc_id].usr = NULL; 1939 synchronize_srcu(&qdev->dbc[dbc_id].ch_lock); 1940 return 0; 1941 } 1942 1943 /** 1944 * enable_dbc - Enable the DBC. DBCs are disabled by removing the context of 1945 * user. Add user context back to DBC to enable it. This function trusts the 1946 * DBC ID passed and expects the DBC to be disabled. 1947 * @qdev: Qranium device handle 1948 * @dbc_id: ID of the DBC 1949 * @usr: User context 1950 */ 1951 void enable_dbc(struct qaic_device *qdev, u32 dbc_id, struct qaic_user *usr) 1952 { 1953 qdev->dbc[dbc_id].usr = usr; 1954 } 1955 1956 void wakeup_dbc(struct qaic_device *qdev, u32 dbc_id) 1957 { 1958 struct dma_bridge_chan *dbc = &qdev->dbc[dbc_id]; 1959 1960 dbc->usr = NULL; 1961 empty_xfer_list(qdev, dbc); 1962 synchronize_srcu(&dbc->ch_lock); 1963 /* 1964 * Threads holding channel lock, may add more elements in the xfer_list. 1965 * Flush out these elements from xfer_list. 1966 */ 1967 empty_xfer_list(qdev, dbc); 1968 } 1969 1970 void release_dbc(struct qaic_device *qdev, u32 dbc_id) 1971 { 1972 struct qaic_bo *bo, *bo_temp; 1973 struct dma_bridge_chan *dbc; 1974 1975 dbc = &qdev->dbc[dbc_id]; 1976 if (!dbc->in_use) 1977 return; 1978 1979 wakeup_dbc(qdev, dbc_id); 1980 1981 dma_free_coherent(&qdev->pdev->dev, dbc->total_size, dbc->req_q_base, dbc->dma_addr); 1982 dbc->total_size = 0; 1983 dbc->req_q_base = NULL; 1984 dbc->dma_addr = 0; 1985 dbc->nelem = 0; 1986 dbc->usr = NULL; 1987 1988 list_for_each_entry_safe(bo, bo_temp, &dbc->bo_lists, bo_list) { 1989 drm_gem_object_get(&bo->base); 1990 mutex_lock(&bo->lock); 1991 detach_slice_bo(qdev, bo); 1992 mutex_unlock(&bo->lock); 1993 drm_gem_object_put(&bo->base); 1994 } 1995 1996 dbc->in_use = false; 1997 wake_up(&dbc->dbc_release); 1998 } 1999