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