1 /* 2 * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2 3 * 4 * Copyright (C) 2010 Samsung Electronics 5 * 6 * Author: Pawel Osciak <pawel@osciak.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation. 11 */ 12 13 #include <linux/dma-buf.h> 14 #include <linux/module.h> 15 #include <linux/refcount.h> 16 #include <linux/scatterlist.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/dma-mapping.h> 20 21 #include <media/videobuf2-v4l2.h> 22 #include <media/videobuf2-dma-contig.h> 23 #include <media/videobuf2-memops.h> 24 25 struct vb2_dc_buf { 26 struct device *dev; 27 void *vaddr; 28 unsigned long size; 29 void *cookie; 30 dma_addr_t dma_addr; 31 unsigned long attrs; 32 enum dma_data_direction dma_dir; 33 struct sg_table *dma_sgt; 34 struct frame_vector *vec; 35 36 /* MMAP related */ 37 struct vb2_vmarea_handler handler; 38 refcount_t refcount; 39 struct sg_table *sgt_base; 40 41 /* DMABUF related */ 42 struct dma_buf_attachment *db_attach; 43 }; 44 45 static inline bool vb2_dc_buffer_consistent(unsigned long attr) 46 { 47 return !(attr & DMA_ATTR_NON_CONSISTENT); 48 } 49 50 /*********************************************/ 51 /* scatterlist table functions */ 52 /*********************************************/ 53 54 static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt) 55 { 56 struct scatterlist *s; 57 dma_addr_t expected = sg_dma_address(sgt->sgl); 58 unsigned int i; 59 unsigned long size = 0; 60 61 for_each_sg(sgt->sgl, s, sgt->nents, i) { 62 if (sg_dma_address(s) != expected) 63 break; 64 expected = sg_dma_address(s) + sg_dma_len(s); 65 size += sg_dma_len(s); 66 } 67 return size; 68 } 69 70 /*********************************************/ 71 /* callbacks for all buffers */ 72 /*********************************************/ 73 74 static void *vb2_dc_cookie(void *buf_priv) 75 { 76 struct vb2_dc_buf *buf = buf_priv; 77 78 return &buf->dma_addr; 79 } 80 81 static void *vb2_dc_vaddr(void *buf_priv) 82 { 83 struct vb2_dc_buf *buf = buf_priv; 84 85 if (!buf->vaddr && buf->db_attach) 86 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf); 87 88 return buf->vaddr; 89 } 90 91 static unsigned int vb2_dc_num_users(void *buf_priv) 92 { 93 struct vb2_dc_buf *buf = buf_priv; 94 95 return refcount_read(&buf->refcount); 96 } 97 98 static void vb2_dc_prepare(void *buf_priv) 99 { 100 struct vb2_dc_buf *buf = buf_priv; 101 struct sg_table *sgt = buf->dma_sgt; 102 103 if (!sgt) 104 return; 105 106 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents, 107 buf->dma_dir); 108 } 109 110 static void vb2_dc_finish(void *buf_priv) 111 { 112 struct vb2_dc_buf *buf = buf_priv; 113 struct sg_table *sgt = buf->dma_sgt; 114 115 if (!sgt) 116 return; 117 118 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); 119 } 120 121 /*********************************************/ 122 /* callbacks for MMAP buffers */ 123 /*********************************************/ 124 125 static void vb2_dc_put(void *buf_priv) 126 { 127 struct vb2_dc_buf *buf = buf_priv; 128 129 if (!refcount_dec_and_test(&buf->refcount)) 130 return; 131 132 if (buf->sgt_base) { 133 sg_free_table(buf->sgt_base); 134 kfree(buf->sgt_base); 135 } 136 dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr, 137 buf->attrs); 138 put_device(buf->dev); 139 kfree(buf); 140 } 141 142 static void *vb2_dc_alloc(struct device *dev, unsigned long attrs, 143 unsigned long size, enum dma_data_direction dma_dir, 144 gfp_t gfp_flags) 145 { 146 struct vb2_dc_buf *buf; 147 148 if (WARN_ON(!dev)) 149 return ERR_PTR(-EINVAL); 150 151 buf = kzalloc(sizeof *buf, GFP_KERNEL); 152 if (!buf) 153 return ERR_PTR(-ENOMEM); 154 155 buf->attrs = attrs; 156 buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr, 157 GFP_KERNEL | gfp_flags, buf->attrs); 158 if (!buf->cookie) { 159 dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size); 160 kfree(buf); 161 return ERR_PTR(-ENOMEM); 162 } 163 164 if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0) 165 buf->vaddr = buf->cookie; 166 167 /* Prevent the device from being released while the buffer is used */ 168 buf->dev = get_device(dev); 169 buf->size = size; 170 buf->dma_dir = dma_dir; 171 172 buf->handler.refcount = &buf->refcount; 173 buf->handler.put = vb2_dc_put; 174 buf->handler.arg = buf; 175 176 refcount_set(&buf->refcount, 1); 177 178 return buf; 179 } 180 181 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma) 182 { 183 struct vb2_dc_buf *buf = buf_priv; 184 int ret; 185 186 if (!buf) { 187 printk(KERN_ERR "No buffer to map\n"); 188 return -EINVAL; 189 } 190 191 ret = dma_mmap_attrs(buf->dev, vma, buf->cookie, 192 buf->dma_addr, buf->size, buf->attrs); 193 194 if (ret) { 195 pr_err("Remapping memory failed, error: %d\n", ret); 196 return ret; 197 } 198 199 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 200 vma->vm_private_data = &buf->handler; 201 vma->vm_ops = &vb2_common_vm_ops; 202 203 vma->vm_ops->open(vma); 204 205 pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n", 206 __func__, (unsigned long)buf->dma_addr, vma->vm_start, 207 buf->size); 208 209 return 0; 210 } 211 212 /*********************************************/ 213 /* DMABUF ops for exporters */ 214 /*********************************************/ 215 216 struct vb2_dc_attachment { 217 struct sg_table sgt; 218 enum dma_data_direction dma_dir; 219 }; 220 221 static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, 222 struct dma_buf_attachment *dbuf_attach) 223 { 224 struct vb2_dc_attachment *attach; 225 unsigned int i; 226 struct scatterlist *rd, *wr; 227 struct sg_table *sgt; 228 struct vb2_dc_buf *buf = dbuf->priv; 229 int ret; 230 231 attach = kzalloc(sizeof(*attach), GFP_KERNEL); 232 if (!attach) 233 return -ENOMEM; 234 235 sgt = &attach->sgt; 236 /* Copy the buf->base_sgt scatter list to the attachment, as we can't 237 * map the same scatter list to multiple attachments at the same time. 238 */ 239 ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL); 240 if (ret) { 241 kfree(attach); 242 return -ENOMEM; 243 } 244 245 rd = buf->sgt_base->sgl; 246 wr = sgt->sgl; 247 for (i = 0; i < sgt->orig_nents; ++i) { 248 sg_set_page(wr, sg_page(rd), rd->length, rd->offset); 249 rd = sg_next(rd); 250 wr = sg_next(wr); 251 } 252 253 attach->dma_dir = DMA_NONE; 254 dbuf_attach->priv = attach; 255 256 return 0; 257 } 258 259 static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf, 260 struct dma_buf_attachment *db_attach) 261 { 262 struct vb2_dc_attachment *attach = db_attach->priv; 263 struct sg_table *sgt; 264 265 if (!attach) 266 return; 267 268 sgt = &attach->sgt; 269 270 /* release the scatterlist cache */ 271 if (attach->dma_dir != DMA_NONE) 272 /* 273 * Cache sync can be skipped here, as the vb2_dc memory is 274 * allocated from device coherent memory, which means the 275 * memory locations do not require any explicit cache 276 * maintenance prior or after being used by the device. 277 */ 278 dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents, 279 attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 280 sg_free_table(sgt); 281 kfree(attach); 282 db_attach->priv = NULL; 283 } 284 285 static struct sg_table *vb2_dc_dmabuf_ops_map( 286 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir) 287 { 288 struct vb2_dc_attachment *attach = db_attach->priv; 289 /* stealing dmabuf mutex to serialize map/unmap operations */ 290 struct mutex *lock = &db_attach->dmabuf->lock; 291 struct sg_table *sgt; 292 293 mutex_lock(lock); 294 295 sgt = &attach->sgt; 296 /* return previously mapped sg table */ 297 if (attach->dma_dir == dma_dir) { 298 mutex_unlock(lock); 299 return sgt; 300 } 301 302 /* release any previous cache */ 303 if (attach->dma_dir != DMA_NONE) { 304 dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents, 305 attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 306 attach->dma_dir = DMA_NONE; 307 } 308 309 /* 310 * mapping to the client with new direction, no cache sync 311 * required see comment in vb2_dc_dmabuf_ops_detach() 312 */ 313 sgt->nents = dma_map_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents, 314 dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 315 if (!sgt->nents) { 316 pr_err("failed to map scatterlist\n"); 317 mutex_unlock(lock); 318 return ERR_PTR(-EIO); 319 } 320 321 attach->dma_dir = dma_dir; 322 323 mutex_unlock(lock); 324 325 return sgt; 326 } 327 328 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach, 329 struct sg_table *sgt, enum dma_data_direction dma_dir) 330 { 331 /* nothing to be done here */ 332 } 333 334 static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf) 335 { 336 /* drop reference obtained in vb2_dc_get_dmabuf */ 337 vb2_dc_put(dbuf->priv); 338 } 339 340 static int 341 vb2_dc_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf, 342 enum dma_data_direction direction) 343 { 344 struct vb2_dc_buf *buf = dbuf->priv; 345 struct sg_table *sgt = buf->dma_sgt; 346 347 if (vb2_dc_buffer_consistent(buf->attrs)) 348 return 0; 349 350 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); 351 return 0; 352 } 353 354 static int 355 vb2_dc_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf, 356 enum dma_data_direction direction) 357 { 358 struct vb2_dc_buf *buf = dbuf->priv; 359 struct sg_table *sgt = buf->dma_sgt; 360 361 if (vb2_dc_buffer_consistent(buf->attrs)) 362 return 0; 363 364 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); 365 return 0; 366 } 367 368 static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf) 369 { 370 struct vb2_dc_buf *buf = dbuf->priv; 371 372 return buf->vaddr; 373 } 374 375 static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf, 376 struct vm_area_struct *vma) 377 { 378 return vb2_dc_mmap(dbuf->priv, vma); 379 } 380 381 static const struct dma_buf_ops vb2_dc_dmabuf_ops = { 382 .attach = vb2_dc_dmabuf_ops_attach, 383 .detach = vb2_dc_dmabuf_ops_detach, 384 .map_dma_buf = vb2_dc_dmabuf_ops_map, 385 .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap, 386 .begin_cpu_access = vb2_dc_dmabuf_ops_begin_cpu_access, 387 .end_cpu_access = vb2_dc_dmabuf_ops_end_cpu_access, 388 .vmap = vb2_dc_dmabuf_ops_vmap, 389 .mmap = vb2_dc_dmabuf_ops_mmap, 390 .release = vb2_dc_dmabuf_ops_release, 391 }; 392 393 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf) 394 { 395 int ret; 396 struct sg_table *sgt; 397 398 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); 399 if (!sgt) { 400 dev_err(buf->dev, "failed to alloc sg table\n"); 401 return NULL; 402 } 403 404 ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr, 405 buf->size, buf->attrs); 406 if (ret < 0) { 407 dev_err(buf->dev, "failed to get scatterlist from DMA API\n"); 408 kfree(sgt); 409 return NULL; 410 } 411 412 return sgt; 413 } 414 415 static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags) 416 { 417 struct vb2_dc_buf *buf = buf_priv; 418 struct dma_buf *dbuf; 419 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 420 421 exp_info.ops = &vb2_dc_dmabuf_ops; 422 exp_info.size = buf->size; 423 exp_info.flags = flags; 424 exp_info.priv = buf; 425 426 if (!buf->sgt_base) 427 buf->sgt_base = vb2_dc_get_base_sgt(buf); 428 429 if (WARN_ON(!buf->sgt_base)) 430 return NULL; 431 432 dbuf = dma_buf_export(&exp_info); 433 if (IS_ERR(dbuf)) 434 return NULL; 435 436 /* dmabuf keeps reference to vb2 buffer */ 437 refcount_inc(&buf->refcount); 438 439 return dbuf; 440 } 441 442 /*********************************************/ 443 /* callbacks for USERPTR buffers */ 444 /*********************************************/ 445 446 static void vb2_dc_put_userptr(void *buf_priv) 447 { 448 struct vb2_dc_buf *buf = buf_priv; 449 struct sg_table *sgt = buf->dma_sgt; 450 int i; 451 struct page **pages; 452 453 if (sgt) { 454 /* 455 * No need to sync to CPU, it's already synced to the CPU 456 * since the finish() memop will have been called before this. 457 */ 458 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 459 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 460 pages = frame_vector_pages(buf->vec); 461 /* sgt should exist only if vector contains pages... */ 462 BUG_ON(IS_ERR(pages)); 463 if (buf->dma_dir == DMA_FROM_DEVICE || 464 buf->dma_dir == DMA_BIDIRECTIONAL) 465 for (i = 0; i < frame_vector_count(buf->vec); i++) 466 set_page_dirty_lock(pages[i]); 467 sg_free_table(sgt); 468 kfree(sgt); 469 } else { 470 dma_unmap_resource(buf->dev, buf->dma_addr, buf->size, 471 buf->dma_dir, 0); 472 } 473 vb2_destroy_framevec(buf->vec); 474 kfree(buf); 475 } 476 477 static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, 478 unsigned long size, enum dma_data_direction dma_dir) 479 { 480 struct vb2_dc_buf *buf; 481 struct frame_vector *vec; 482 unsigned int offset; 483 int n_pages, i; 484 int ret = 0; 485 struct sg_table *sgt; 486 unsigned long contig_size; 487 unsigned long dma_align = dma_get_cache_alignment(); 488 489 /* Only cache aligned DMA transfers are reliable */ 490 if (!IS_ALIGNED(vaddr | size, dma_align)) { 491 pr_debug("user data must be aligned to %lu bytes\n", dma_align); 492 return ERR_PTR(-EINVAL); 493 } 494 495 if (!size) { 496 pr_debug("size is zero\n"); 497 return ERR_PTR(-EINVAL); 498 } 499 500 if (WARN_ON(!dev)) 501 return ERR_PTR(-EINVAL); 502 503 buf = kzalloc(sizeof *buf, GFP_KERNEL); 504 if (!buf) 505 return ERR_PTR(-ENOMEM); 506 507 buf->dev = dev; 508 buf->dma_dir = dma_dir; 509 510 offset = lower_32_bits(offset_in_page(vaddr)); 511 vec = vb2_create_framevec(vaddr, size); 512 if (IS_ERR(vec)) { 513 ret = PTR_ERR(vec); 514 goto fail_buf; 515 } 516 buf->vec = vec; 517 n_pages = frame_vector_count(vec); 518 ret = frame_vector_to_pages(vec); 519 if (ret < 0) { 520 unsigned long *nums = frame_vector_pfns(vec); 521 522 /* 523 * Failed to convert to pages... Check the memory is physically 524 * contiguous and use direct mapping 525 */ 526 for (i = 1; i < n_pages; i++) 527 if (nums[i-1] + 1 != nums[i]) 528 goto fail_pfnvec; 529 buf->dma_addr = dma_map_resource(buf->dev, 530 __pfn_to_phys(nums[0]), size, buf->dma_dir, 0); 531 if (dma_mapping_error(buf->dev, buf->dma_addr)) { 532 ret = -ENOMEM; 533 goto fail_pfnvec; 534 } 535 goto out; 536 } 537 538 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); 539 if (!sgt) { 540 pr_err("failed to allocate sg table\n"); 541 ret = -ENOMEM; 542 goto fail_pfnvec; 543 } 544 545 ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages, 546 offset, size, GFP_KERNEL); 547 if (ret) { 548 pr_err("failed to initialize sg table\n"); 549 goto fail_sgt; 550 } 551 552 /* 553 * No need to sync to the device, this will happen later when the 554 * prepare() memop is called. 555 */ 556 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 557 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 558 if (sgt->nents <= 0) { 559 pr_err("failed to map scatterlist\n"); 560 ret = -EIO; 561 goto fail_sgt_init; 562 } 563 564 contig_size = vb2_dc_get_contiguous_size(sgt); 565 if (contig_size < size) { 566 pr_err("contiguous mapping is too small %lu/%lu\n", 567 contig_size, size); 568 ret = -EFAULT; 569 goto fail_map_sg; 570 } 571 572 buf->dma_addr = sg_dma_address(sgt->sgl); 573 buf->dma_sgt = sgt; 574 out: 575 buf->size = size; 576 577 return buf; 578 579 fail_map_sg: 580 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 581 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 582 583 fail_sgt_init: 584 sg_free_table(sgt); 585 586 fail_sgt: 587 kfree(sgt); 588 589 fail_pfnvec: 590 vb2_destroy_framevec(vec); 591 592 fail_buf: 593 kfree(buf); 594 595 return ERR_PTR(ret); 596 } 597 598 /*********************************************/ 599 /* callbacks for DMABUF buffers */ 600 /*********************************************/ 601 602 static int vb2_dc_map_dmabuf(void *mem_priv) 603 { 604 struct vb2_dc_buf *buf = mem_priv; 605 struct sg_table *sgt; 606 unsigned long contig_size; 607 608 if (WARN_ON(!buf->db_attach)) { 609 pr_err("trying to pin a non attached buffer\n"); 610 return -EINVAL; 611 } 612 613 if (WARN_ON(buf->dma_sgt)) { 614 pr_err("dmabuf buffer is already pinned\n"); 615 return 0; 616 } 617 618 /* get the associated scatterlist for this buffer */ 619 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir); 620 if (IS_ERR(sgt)) { 621 pr_err("Error getting dmabuf scatterlist\n"); 622 return -EINVAL; 623 } 624 625 /* checking if dmabuf is big enough to store contiguous chunk */ 626 contig_size = vb2_dc_get_contiguous_size(sgt); 627 if (contig_size < buf->size) { 628 pr_err("contiguous chunk is too small %lu/%lu\n", 629 contig_size, buf->size); 630 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); 631 return -EFAULT; 632 } 633 634 buf->dma_addr = sg_dma_address(sgt->sgl); 635 buf->dma_sgt = sgt; 636 buf->vaddr = NULL; 637 638 return 0; 639 } 640 641 static void vb2_dc_unmap_dmabuf(void *mem_priv) 642 { 643 struct vb2_dc_buf *buf = mem_priv; 644 struct sg_table *sgt = buf->dma_sgt; 645 646 if (WARN_ON(!buf->db_attach)) { 647 pr_err("trying to unpin a not attached buffer\n"); 648 return; 649 } 650 651 if (WARN_ON(!sgt)) { 652 pr_err("dmabuf buffer is already unpinned\n"); 653 return; 654 } 655 656 if (buf->vaddr) { 657 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr); 658 buf->vaddr = NULL; 659 } 660 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); 661 662 buf->dma_addr = 0; 663 buf->dma_sgt = NULL; 664 } 665 666 static void vb2_dc_detach_dmabuf(void *mem_priv) 667 { 668 struct vb2_dc_buf *buf = mem_priv; 669 670 /* if vb2 works correctly you should never detach mapped buffer */ 671 if (WARN_ON(buf->dma_addr)) 672 vb2_dc_unmap_dmabuf(buf); 673 674 /* detach this attachment */ 675 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach); 676 kfree(buf); 677 } 678 679 static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf, 680 unsigned long size, enum dma_data_direction dma_dir) 681 { 682 struct vb2_dc_buf *buf; 683 struct dma_buf_attachment *dba; 684 685 if (dbuf->size < size) 686 return ERR_PTR(-EFAULT); 687 688 if (WARN_ON(!dev)) 689 return ERR_PTR(-EINVAL); 690 691 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 692 if (!buf) 693 return ERR_PTR(-ENOMEM); 694 695 buf->dev = dev; 696 /* create attachment for the dmabuf with the user device */ 697 dba = dma_buf_attach(dbuf, buf->dev); 698 if (IS_ERR(dba)) { 699 pr_err("failed to attach dmabuf\n"); 700 kfree(buf); 701 return dba; 702 } 703 704 buf->dma_dir = dma_dir; 705 buf->size = size; 706 buf->db_attach = dba; 707 708 return buf; 709 } 710 711 /*********************************************/ 712 /* DMA CONTIG exported functions */ 713 /*********************************************/ 714 715 const struct vb2_mem_ops vb2_dma_contig_memops = { 716 .alloc = vb2_dc_alloc, 717 .put = vb2_dc_put, 718 .get_dmabuf = vb2_dc_get_dmabuf, 719 .cookie = vb2_dc_cookie, 720 .vaddr = vb2_dc_vaddr, 721 .mmap = vb2_dc_mmap, 722 .get_userptr = vb2_dc_get_userptr, 723 .put_userptr = vb2_dc_put_userptr, 724 .prepare = vb2_dc_prepare, 725 .finish = vb2_dc_finish, 726 .map_dmabuf = vb2_dc_map_dmabuf, 727 .unmap_dmabuf = vb2_dc_unmap_dmabuf, 728 .attach_dmabuf = vb2_dc_attach_dmabuf, 729 .detach_dmabuf = vb2_dc_detach_dmabuf, 730 .num_users = vb2_dc_num_users, 731 }; 732 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops); 733 734 /** 735 * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size 736 * @dev: device for configuring DMA parameters 737 * @size: size of DMA max segment size to set 738 * 739 * To allow mapping the scatter-list into a single chunk in the DMA 740 * address space, the device is required to have the DMA max segment 741 * size parameter set to a value larger than the buffer size. Otherwise, 742 * the DMA-mapping subsystem will split the mapping into max segment 743 * size chunks. This function sets the DMA max segment size 744 * parameter to let DMA-mapping map a buffer as a single chunk in DMA 745 * address space. 746 * This code assumes that the DMA-mapping subsystem will merge all 747 * scatterlist segments if this is really possible (for example when 748 * an IOMMU is available and enabled). 749 * Ideally, this parameter should be set by the generic bus code, but it 750 * is left with the default 64KiB value due to historical litmiations in 751 * other subsystems (like limited USB host drivers) and there no good 752 * place to set it to the proper value. 753 * This function should be called from the drivers, which are known to 754 * operate on platforms with IOMMU and provide access to shared buffers 755 * (either USERPTR or DMABUF). This should be done before initializing 756 * videobuf2 queue. 757 */ 758 int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size) 759 { 760 if (!dev->dma_parms) { 761 dev_err(dev, "Failed to set max_seg_size: dma_parms is NULL\n"); 762 return -ENODEV; 763 } 764 if (dma_get_max_seg_size(dev) < size) 765 return dma_set_max_seg_size(dev, size); 766 767 return 0; 768 } 769 EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size); 770 771 MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2"); 772 MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>"); 773 MODULE_LICENSE("GPL"); 774