1 /* 2 * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2 3 * 4 * Copyright (C) 2010 Samsung Electronics 5 * 6 * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.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/module.h> 14 #include <linux/mm.h> 15 #include <linux/refcount.h> 16 #include <linux/scatterlist.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 21 #include <media/videobuf2-v4l2.h> 22 #include <media/videobuf2-memops.h> 23 #include <media/videobuf2-dma-sg.h> 24 25 static int debug; 26 module_param(debug, int, 0644); 27 28 #define dprintk(level, fmt, arg...) \ 29 do { \ 30 if (debug >= level) \ 31 printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \ 32 } while (0) 33 34 struct vb2_dma_sg_buf { 35 struct device *dev; 36 void *vaddr; 37 struct page **pages; 38 struct frame_vector *vec; 39 int offset; 40 enum dma_data_direction dma_dir; 41 struct sg_table sg_table; 42 /* 43 * This will point to sg_table when used with the MMAP or USERPTR 44 * memory model, and to the dma_buf sglist when used with the 45 * DMABUF memory model. 46 */ 47 struct sg_table *dma_sgt; 48 size_t size; 49 unsigned int num_pages; 50 refcount_t refcount; 51 struct vb2_vmarea_handler handler; 52 53 struct dma_buf_attachment *db_attach; 54 }; 55 56 static void vb2_dma_sg_put(void *buf_priv); 57 58 static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf, 59 gfp_t gfp_flags) 60 { 61 unsigned int last_page = 0; 62 unsigned long size = buf->size; 63 64 while (size > 0) { 65 struct page *pages; 66 int order; 67 int i; 68 69 order = get_order(size); 70 /* Don't over allocate*/ 71 if ((PAGE_SIZE << order) > size) 72 order--; 73 74 pages = NULL; 75 while (!pages) { 76 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO | 77 __GFP_NOWARN | gfp_flags, order); 78 if (pages) 79 break; 80 81 if (order == 0) { 82 while (last_page--) 83 __free_page(buf->pages[last_page]); 84 return -ENOMEM; 85 } 86 order--; 87 } 88 89 split_page(pages, order); 90 for (i = 0; i < (1 << order); i++) 91 buf->pages[last_page++] = &pages[i]; 92 93 size -= PAGE_SIZE << order; 94 } 95 96 return 0; 97 } 98 99 static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs, 100 unsigned long size, enum dma_data_direction dma_dir, 101 gfp_t gfp_flags) 102 { 103 struct vb2_dma_sg_buf *buf; 104 struct sg_table *sgt; 105 int ret; 106 int num_pages; 107 108 if (WARN_ON(!dev)) 109 return ERR_PTR(-EINVAL); 110 111 buf = kzalloc(sizeof *buf, GFP_KERNEL); 112 if (!buf) 113 return ERR_PTR(-ENOMEM); 114 115 buf->vaddr = NULL; 116 buf->dma_dir = dma_dir; 117 buf->offset = 0; 118 buf->size = size; 119 /* size is already page aligned */ 120 buf->num_pages = size >> PAGE_SHIFT; 121 buf->dma_sgt = &buf->sg_table; 122 123 /* 124 * NOTE: dma-sg allocates memory using the page allocator directly, so 125 * there is no memory consistency guarantee, hence dma-sg ignores DMA 126 * attributes passed from the upper layer. That means that 127 * V4L2_FLAG_MEMORY_NON_CONSISTENT has no effect on dma-sg buffers. 128 */ 129 buf->pages = kvmalloc_array(buf->num_pages, sizeof(struct page *), 130 GFP_KERNEL | __GFP_ZERO); 131 if (!buf->pages) 132 goto fail_pages_array_alloc; 133 134 ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags); 135 if (ret) 136 goto fail_pages_alloc; 137 138 ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages, 139 buf->num_pages, 0, size, GFP_KERNEL); 140 if (ret) 141 goto fail_table_alloc; 142 143 /* Prevent the device from being released while the buffer is used */ 144 buf->dev = get_device(dev); 145 146 sgt = &buf->sg_table; 147 /* 148 * No need to sync to the device, this will happen later when the 149 * prepare() memop is called. 150 */ 151 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 152 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 153 if (!sgt->nents) 154 goto fail_map; 155 156 buf->handler.refcount = &buf->refcount; 157 buf->handler.put = vb2_dma_sg_put; 158 buf->handler.arg = buf; 159 160 refcount_set(&buf->refcount, 1); 161 162 dprintk(1, "%s: Allocated buffer of %d pages\n", 163 __func__, buf->num_pages); 164 return buf; 165 166 fail_map: 167 put_device(buf->dev); 168 sg_free_table(buf->dma_sgt); 169 fail_table_alloc: 170 num_pages = buf->num_pages; 171 while (num_pages--) 172 __free_page(buf->pages[num_pages]); 173 fail_pages_alloc: 174 kvfree(buf->pages); 175 fail_pages_array_alloc: 176 kfree(buf); 177 return ERR_PTR(-ENOMEM); 178 } 179 180 static void vb2_dma_sg_put(void *buf_priv) 181 { 182 struct vb2_dma_sg_buf *buf = buf_priv; 183 struct sg_table *sgt = &buf->sg_table; 184 int i = buf->num_pages; 185 186 if (refcount_dec_and_test(&buf->refcount)) { 187 dprintk(1, "%s: Freeing buffer of %d pages\n", __func__, 188 buf->num_pages); 189 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 190 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 191 if (buf->vaddr) 192 vm_unmap_ram(buf->vaddr, buf->num_pages); 193 sg_free_table(buf->dma_sgt); 194 while (--i >= 0) 195 __free_page(buf->pages[i]); 196 kvfree(buf->pages); 197 put_device(buf->dev); 198 kfree(buf); 199 } 200 } 201 202 static void vb2_dma_sg_prepare(void *buf_priv) 203 { 204 struct vb2_dma_sg_buf *buf = buf_priv; 205 struct sg_table *sgt = buf->dma_sgt; 206 207 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents, 208 buf->dma_dir); 209 } 210 211 static void vb2_dma_sg_finish(void *buf_priv) 212 { 213 struct vb2_dma_sg_buf *buf = buf_priv; 214 struct sg_table *sgt = buf->dma_sgt; 215 216 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); 217 } 218 219 static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr, 220 unsigned long size, 221 enum dma_data_direction dma_dir) 222 { 223 struct vb2_dma_sg_buf *buf; 224 struct sg_table *sgt; 225 struct frame_vector *vec; 226 227 if (WARN_ON(!dev)) 228 return ERR_PTR(-EINVAL); 229 230 buf = kzalloc(sizeof *buf, GFP_KERNEL); 231 if (!buf) 232 return ERR_PTR(-ENOMEM); 233 234 buf->vaddr = NULL; 235 buf->dev = dev; 236 buf->dma_dir = dma_dir; 237 buf->offset = vaddr & ~PAGE_MASK; 238 buf->size = size; 239 buf->dma_sgt = &buf->sg_table; 240 vec = vb2_create_framevec(vaddr, size); 241 if (IS_ERR(vec)) 242 goto userptr_fail_pfnvec; 243 buf->vec = vec; 244 245 buf->pages = frame_vector_pages(vec); 246 if (IS_ERR(buf->pages)) 247 goto userptr_fail_sgtable; 248 buf->num_pages = frame_vector_count(vec); 249 250 if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages, 251 buf->num_pages, buf->offset, size, 0)) 252 goto userptr_fail_sgtable; 253 254 sgt = &buf->sg_table; 255 /* 256 * No need to sync to the device, this will happen later when the 257 * prepare() memop is called. 258 */ 259 sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, 260 buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC); 261 if (!sgt->nents) 262 goto userptr_fail_map; 263 264 return buf; 265 266 userptr_fail_map: 267 sg_free_table(&buf->sg_table); 268 userptr_fail_sgtable: 269 vb2_destroy_framevec(vec); 270 userptr_fail_pfnvec: 271 kfree(buf); 272 return ERR_PTR(-ENOMEM); 273 } 274 275 /* 276 * @put_userptr: inform the allocator that a USERPTR buffer will no longer 277 * be used 278 */ 279 static void vb2_dma_sg_put_userptr(void *buf_priv) 280 { 281 struct vb2_dma_sg_buf *buf = buf_priv; 282 struct sg_table *sgt = &buf->sg_table; 283 int i = buf->num_pages; 284 285 dprintk(1, "%s: Releasing userspace buffer of %d pages\n", 286 __func__, buf->num_pages); 287 dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir, 288 DMA_ATTR_SKIP_CPU_SYNC); 289 if (buf->vaddr) 290 vm_unmap_ram(buf->vaddr, buf->num_pages); 291 sg_free_table(buf->dma_sgt); 292 if (buf->dma_dir == DMA_FROM_DEVICE || 293 buf->dma_dir == DMA_BIDIRECTIONAL) 294 while (--i >= 0) 295 set_page_dirty_lock(buf->pages[i]); 296 vb2_destroy_framevec(buf->vec); 297 kfree(buf); 298 } 299 300 static void *vb2_dma_sg_vaddr(void *buf_priv) 301 { 302 struct vb2_dma_sg_buf *buf = buf_priv; 303 304 BUG_ON(!buf); 305 306 if (!buf->vaddr) { 307 if (buf->db_attach) 308 buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf); 309 else 310 buf->vaddr = vm_map_ram(buf->pages, buf->num_pages, -1); 311 } 312 313 /* add offset in case userptr is not page-aligned */ 314 return buf->vaddr ? buf->vaddr + buf->offset : NULL; 315 } 316 317 static unsigned int vb2_dma_sg_num_users(void *buf_priv) 318 { 319 struct vb2_dma_sg_buf *buf = buf_priv; 320 321 return refcount_read(&buf->refcount); 322 } 323 324 static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma) 325 { 326 struct vb2_dma_sg_buf *buf = buf_priv; 327 int err; 328 329 if (!buf) { 330 printk(KERN_ERR "No memory to map\n"); 331 return -EINVAL; 332 } 333 334 err = vm_map_pages(vma, buf->pages, buf->num_pages); 335 if (err) { 336 printk(KERN_ERR "Remapping memory, error: %d\n", err); 337 return err; 338 } 339 340 /* 341 * Use common vm_area operations to track buffer refcount. 342 */ 343 vma->vm_private_data = &buf->handler; 344 vma->vm_ops = &vb2_common_vm_ops; 345 346 vma->vm_ops->open(vma); 347 348 return 0; 349 } 350 351 /*********************************************/ 352 /* DMABUF ops for exporters */ 353 /*********************************************/ 354 355 struct vb2_dma_sg_attachment { 356 struct sg_table sgt; 357 enum dma_data_direction dma_dir; 358 }; 359 360 static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, 361 struct dma_buf_attachment *dbuf_attach) 362 { 363 struct vb2_dma_sg_attachment *attach; 364 unsigned int i; 365 struct scatterlist *rd, *wr; 366 struct sg_table *sgt; 367 struct vb2_dma_sg_buf *buf = dbuf->priv; 368 int ret; 369 370 attach = kzalloc(sizeof(*attach), GFP_KERNEL); 371 if (!attach) 372 return -ENOMEM; 373 374 sgt = &attach->sgt; 375 /* Copy the buf->base_sgt scatter list to the attachment, as we can't 376 * map the same scatter list to multiple attachments at the same time. 377 */ 378 ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL); 379 if (ret) { 380 kfree(attach); 381 return -ENOMEM; 382 } 383 384 rd = buf->dma_sgt->sgl; 385 wr = sgt->sgl; 386 for (i = 0; i < sgt->orig_nents; ++i) { 387 sg_set_page(wr, sg_page(rd), rd->length, rd->offset); 388 rd = sg_next(rd); 389 wr = sg_next(wr); 390 } 391 392 attach->dma_dir = DMA_NONE; 393 dbuf_attach->priv = attach; 394 395 return 0; 396 } 397 398 static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf, 399 struct dma_buf_attachment *db_attach) 400 { 401 struct vb2_dma_sg_attachment *attach = db_attach->priv; 402 struct sg_table *sgt; 403 404 if (!attach) 405 return; 406 407 sgt = &attach->sgt; 408 409 /* release the scatterlist cache */ 410 if (attach->dma_dir != DMA_NONE) 411 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, 412 attach->dma_dir); 413 sg_free_table(sgt); 414 kfree(attach); 415 db_attach->priv = NULL; 416 } 417 418 static struct sg_table *vb2_dma_sg_dmabuf_ops_map( 419 struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir) 420 { 421 struct vb2_dma_sg_attachment *attach = db_attach->priv; 422 /* stealing dmabuf mutex to serialize map/unmap operations */ 423 struct mutex *lock = &db_attach->dmabuf->lock; 424 struct sg_table *sgt; 425 426 mutex_lock(lock); 427 428 sgt = &attach->sgt; 429 /* return previously mapped sg table */ 430 if (attach->dma_dir == dma_dir) { 431 mutex_unlock(lock); 432 return sgt; 433 } 434 435 /* release any previous cache */ 436 if (attach->dma_dir != DMA_NONE) { 437 dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, 438 attach->dma_dir); 439 attach->dma_dir = DMA_NONE; 440 } 441 442 /* mapping to the client with new direction */ 443 sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, 444 dma_dir); 445 if (!sgt->nents) { 446 pr_err("failed to map scatterlist\n"); 447 mutex_unlock(lock); 448 return ERR_PTR(-EIO); 449 } 450 451 attach->dma_dir = dma_dir; 452 453 mutex_unlock(lock); 454 455 return sgt; 456 } 457 458 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach, 459 struct sg_table *sgt, enum dma_data_direction dma_dir) 460 { 461 /* nothing to be done here */ 462 } 463 464 static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf) 465 { 466 /* drop reference obtained in vb2_dma_sg_get_dmabuf */ 467 vb2_dma_sg_put(dbuf->priv); 468 } 469 470 static int 471 vb2_dma_sg_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf, 472 enum dma_data_direction direction) 473 { 474 struct vb2_dma_sg_buf *buf = dbuf->priv; 475 struct sg_table *sgt = buf->dma_sgt; 476 477 dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); 478 return 0; 479 } 480 481 static int 482 vb2_dma_sg_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf, 483 enum dma_data_direction direction) 484 { 485 struct vb2_dma_sg_buf *buf = dbuf->priv; 486 struct sg_table *sgt = buf->dma_sgt; 487 488 dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); 489 return 0; 490 } 491 492 static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf) 493 { 494 struct vb2_dma_sg_buf *buf = dbuf->priv; 495 496 return vb2_dma_sg_vaddr(buf); 497 } 498 499 static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf, 500 struct vm_area_struct *vma) 501 { 502 return vb2_dma_sg_mmap(dbuf->priv, vma); 503 } 504 505 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = { 506 .attach = vb2_dma_sg_dmabuf_ops_attach, 507 .detach = vb2_dma_sg_dmabuf_ops_detach, 508 .map_dma_buf = vb2_dma_sg_dmabuf_ops_map, 509 .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap, 510 .begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access, 511 .end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access, 512 .vmap = vb2_dma_sg_dmabuf_ops_vmap, 513 .mmap = vb2_dma_sg_dmabuf_ops_mmap, 514 .release = vb2_dma_sg_dmabuf_ops_release, 515 }; 516 517 static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags) 518 { 519 struct vb2_dma_sg_buf *buf = buf_priv; 520 struct dma_buf *dbuf; 521 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 522 523 exp_info.ops = &vb2_dma_sg_dmabuf_ops; 524 exp_info.size = buf->size; 525 exp_info.flags = flags; 526 exp_info.priv = buf; 527 528 if (WARN_ON(!buf->dma_sgt)) 529 return NULL; 530 531 dbuf = dma_buf_export(&exp_info); 532 if (IS_ERR(dbuf)) 533 return NULL; 534 535 /* dmabuf keeps reference to vb2 buffer */ 536 refcount_inc(&buf->refcount); 537 538 return dbuf; 539 } 540 541 /*********************************************/ 542 /* callbacks for DMABUF buffers */ 543 /*********************************************/ 544 545 static int vb2_dma_sg_map_dmabuf(void *mem_priv) 546 { 547 struct vb2_dma_sg_buf *buf = mem_priv; 548 struct sg_table *sgt; 549 550 if (WARN_ON(!buf->db_attach)) { 551 pr_err("trying to pin a non attached buffer\n"); 552 return -EINVAL; 553 } 554 555 if (WARN_ON(buf->dma_sgt)) { 556 pr_err("dmabuf buffer is already pinned\n"); 557 return 0; 558 } 559 560 /* get the associated scatterlist for this buffer */ 561 sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir); 562 if (IS_ERR(sgt)) { 563 pr_err("Error getting dmabuf scatterlist\n"); 564 return -EINVAL; 565 } 566 567 buf->dma_sgt = sgt; 568 buf->vaddr = NULL; 569 570 return 0; 571 } 572 573 static void vb2_dma_sg_unmap_dmabuf(void *mem_priv) 574 { 575 struct vb2_dma_sg_buf *buf = mem_priv; 576 struct sg_table *sgt = buf->dma_sgt; 577 578 if (WARN_ON(!buf->db_attach)) { 579 pr_err("trying to unpin a not attached buffer\n"); 580 return; 581 } 582 583 if (WARN_ON(!sgt)) { 584 pr_err("dmabuf buffer is already unpinned\n"); 585 return; 586 } 587 588 if (buf->vaddr) { 589 dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr); 590 buf->vaddr = NULL; 591 } 592 dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir); 593 594 buf->dma_sgt = NULL; 595 } 596 597 static void vb2_dma_sg_detach_dmabuf(void *mem_priv) 598 { 599 struct vb2_dma_sg_buf *buf = mem_priv; 600 601 /* if vb2 works correctly you should never detach mapped buffer */ 602 if (WARN_ON(buf->dma_sgt)) 603 vb2_dma_sg_unmap_dmabuf(buf); 604 605 /* detach this attachment */ 606 dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach); 607 kfree(buf); 608 } 609 610 static void *vb2_dma_sg_attach_dmabuf(struct device *dev, struct dma_buf *dbuf, 611 unsigned long size, enum dma_data_direction dma_dir) 612 { 613 struct vb2_dma_sg_buf *buf; 614 struct dma_buf_attachment *dba; 615 616 if (WARN_ON(!dev)) 617 return ERR_PTR(-EINVAL); 618 619 if (dbuf->size < size) 620 return ERR_PTR(-EFAULT); 621 622 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 623 if (!buf) 624 return ERR_PTR(-ENOMEM); 625 626 buf->dev = dev; 627 /* create attachment for the dmabuf with the user device */ 628 dba = dma_buf_attach(dbuf, buf->dev); 629 if (IS_ERR(dba)) { 630 pr_err("failed to attach dmabuf\n"); 631 kfree(buf); 632 return dba; 633 } 634 635 buf->dma_dir = dma_dir; 636 buf->size = size; 637 buf->db_attach = dba; 638 639 return buf; 640 } 641 642 static void *vb2_dma_sg_cookie(void *buf_priv) 643 { 644 struct vb2_dma_sg_buf *buf = buf_priv; 645 646 return buf->dma_sgt; 647 } 648 649 const struct vb2_mem_ops vb2_dma_sg_memops = { 650 .alloc = vb2_dma_sg_alloc, 651 .put = vb2_dma_sg_put, 652 .get_userptr = vb2_dma_sg_get_userptr, 653 .put_userptr = vb2_dma_sg_put_userptr, 654 .prepare = vb2_dma_sg_prepare, 655 .finish = vb2_dma_sg_finish, 656 .vaddr = vb2_dma_sg_vaddr, 657 .mmap = vb2_dma_sg_mmap, 658 .num_users = vb2_dma_sg_num_users, 659 .get_dmabuf = vb2_dma_sg_get_dmabuf, 660 .map_dmabuf = vb2_dma_sg_map_dmabuf, 661 .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf, 662 .attach_dmabuf = vb2_dma_sg_attach_dmabuf, 663 .detach_dmabuf = vb2_dma_sg_detach_dmabuf, 664 .cookie = vb2_dma_sg_cookie, 665 }; 666 EXPORT_SYMBOL_GPL(vb2_dma_sg_memops); 667 668 MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2"); 669 MODULE_AUTHOR("Andrzej Pietrasiewicz"); 670 MODULE_LICENSE("GPL"); 671