1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * helper functions for SG DMA video4linux capture buffers 4 * 5 * The functions expect the hardware being able to scatter gather 6 * (i.e. the buffers are not linear in physical memory, but fragmented 7 * into PAGE_SIZE chunks). They also assume the driver does not need 8 * to touch the video data. 9 * 10 * (c) 2007 Mauro Carvalho Chehab, <mchehab@kernel.org> 11 * 12 * Highly based on video-buf written originally by: 13 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> 14 * (c) 2006 Mauro Carvalho Chehab, <mchehab@kernel.org> 15 * (c) 2006 Ted Walther and John Sokol 16 */ 17 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/moduleparam.h> 21 #include <linux/sched/mm.h> 22 #include <linux/slab.h> 23 #include <linux/interrupt.h> 24 #include <linux/pgtable.h> 25 26 #include <linux/dma-mapping.h> 27 #include <linux/vmalloc.h> 28 #include <linux/pagemap.h> 29 #include <linux/scatterlist.h> 30 #include <asm/page.h> 31 32 #include <media/videobuf-dma-sg.h> 33 34 #define MAGIC_DMABUF 0x19721112 35 #define MAGIC_SG_MEM 0x17890714 36 37 #define MAGIC_CHECK(is, should) \ 38 if (unlikely((is) != (should))) { \ 39 printk(KERN_ERR "magic mismatch: %x (expected %x)\n", \ 40 is, should); \ 41 BUG(); \ 42 } 43 44 static int debug; 45 module_param(debug, int, 0644); 46 47 MODULE_DESCRIPTION("helper module to manage video4linux dma sg buffers"); 48 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>"); 49 MODULE_LICENSE("GPL"); 50 51 #define dprintk(level, fmt, arg...) \ 52 if (debug >= level) \ 53 printk(KERN_DEBUG "vbuf-sg: " fmt , ## arg) 54 55 /* --------------------------------------------------------------------- */ 56 57 /* 58 * Return a scatterlist for some page-aligned vmalloc()'ed memory 59 * block (NULL on errors). Memory for the scatterlist is allocated 60 * using kmalloc. The caller must free the memory. 61 */ 62 static struct scatterlist *videobuf_vmalloc_to_sg(unsigned char *virt, 63 int nr_pages) 64 { 65 struct scatterlist *sglist; 66 struct page *pg; 67 int i; 68 69 sglist = vzalloc(array_size(nr_pages, sizeof(*sglist))); 70 if (NULL == sglist) 71 return NULL; 72 sg_init_table(sglist, nr_pages); 73 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) { 74 pg = vmalloc_to_page(virt); 75 if (NULL == pg) 76 goto err; 77 BUG_ON(PageHighMem(pg)); 78 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0); 79 } 80 return sglist; 81 82 err: 83 vfree(sglist); 84 return NULL; 85 } 86 87 /* 88 * Return a scatterlist for a an array of userpages (NULL on errors). 89 * Memory for the scatterlist is allocated using kmalloc. The caller 90 * must free the memory. 91 */ 92 static struct scatterlist *videobuf_pages_to_sg(struct page **pages, 93 int nr_pages, int offset, size_t size) 94 { 95 struct scatterlist *sglist; 96 int i; 97 98 if (NULL == pages[0]) 99 return NULL; 100 sglist = vmalloc(array_size(nr_pages, sizeof(*sglist))); 101 if (NULL == sglist) 102 return NULL; 103 sg_init_table(sglist, nr_pages); 104 105 if (PageHighMem(pages[0])) 106 /* DMA to highmem pages might not work */ 107 goto highmem; 108 sg_set_page(&sglist[0], pages[0], 109 min_t(size_t, PAGE_SIZE - offset, size), offset); 110 size -= min_t(size_t, PAGE_SIZE - offset, size); 111 for (i = 1; i < nr_pages; i++) { 112 if (NULL == pages[i]) 113 goto nopage; 114 if (PageHighMem(pages[i])) 115 goto highmem; 116 sg_set_page(&sglist[i], pages[i], min_t(size_t, PAGE_SIZE, size), 0); 117 size -= min_t(size_t, PAGE_SIZE, size); 118 } 119 return sglist; 120 121 nopage: 122 dprintk(2, "sgl: oops - no page\n"); 123 vfree(sglist); 124 return NULL; 125 126 highmem: 127 dprintk(2, "sgl: oops - highmem page\n"); 128 vfree(sglist); 129 return NULL; 130 } 131 132 /* --------------------------------------------------------------------- */ 133 134 struct videobuf_dmabuf *videobuf_to_dma(struct videobuf_buffer *buf) 135 { 136 struct videobuf_dma_sg_memory *mem = buf->priv; 137 BUG_ON(!mem); 138 139 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 140 141 return &mem->dma; 142 } 143 EXPORT_SYMBOL_GPL(videobuf_to_dma); 144 145 static void videobuf_dma_init(struct videobuf_dmabuf *dma) 146 { 147 memset(dma, 0, sizeof(*dma)); 148 dma->magic = MAGIC_DMABUF; 149 } 150 151 static int videobuf_dma_init_user_locked(struct videobuf_dmabuf *dma, 152 int direction, unsigned long data, unsigned long size) 153 { 154 unsigned long first, last; 155 int err, rw = 0; 156 unsigned int flags = FOLL_FORCE; 157 158 dma->direction = direction; 159 switch (dma->direction) { 160 case DMA_FROM_DEVICE: 161 rw = READ; 162 break; 163 case DMA_TO_DEVICE: 164 rw = WRITE; 165 break; 166 default: 167 BUG(); 168 } 169 170 first = (data & PAGE_MASK) >> PAGE_SHIFT; 171 last = ((data+size-1) & PAGE_MASK) >> PAGE_SHIFT; 172 dma->offset = data & ~PAGE_MASK; 173 dma->size = size; 174 dma->nr_pages = last-first+1; 175 dma->pages = kmalloc_array(dma->nr_pages, sizeof(struct page *), 176 GFP_KERNEL); 177 if (NULL == dma->pages) 178 return -ENOMEM; 179 180 if (rw == READ) 181 flags |= FOLL_WRITE; 182 183 dprintk(1, "init user [0x%lx+0x%lx => %lu pages]\n", 184 data, size, dma->nr_pages); 185 186 err = pin_user_pages(data & PAGE_MASK, dma->nr_pages, 187 flags | FOLL_LONGTERM, dma->pages, NULL); 188 189 if (err != dma->nr_pages) { 190 dma->nr_pages = (err >= 0) ? err : 0; 191 dprintk(1, "pin_user_pages: err=%d [%lu]\n", err, 192 dma->nr_pages); 193 return err < 0 ? err : -EINVAL; 194 } 195 return 0; 196 } 197 198 static int videobuf_dma_init_user(struct videobuf_dmabuf *dma, int direction, 199 unsigned long data, unsigned long size) 200 { 201 int ret; 202 203 mmap_read_lock(current->mm); 204 ret = videobuf_dma_init_user_locked(dma, direction, data, size); 205 mmap_read_unlock(current->mm); 206 207 return ret; 208 } 209 210 static int videobuf_dma_init_kernel(struct videobuf_dmabuf *dma, int direction, 211 unsigned long nr_pages) 212 { 213 int i; 214 215 dprintk(1, "init kernel [%lu pages]\n", nr_pages); 216 217 dma->direction = direction; 218 dma->vaddr_pages = kcalloc(nr_pages, sizeof(*dma->vaddr_pages), 219 GFP_KERNEL); 220 if (!dma->vaddr_pages) 221 return -ENOMEM; 222 223 dma->dma_addr = kcalloc(nr_pages, sizeof(*dma->dma_addr), GFP_KERNEL); 224 if (!dma->dma_addr) { 225 kfree(dma->vaddr_pages); 226 return -ENOMEM; 227 } 228 for (i = 0; i < nr_pages; i++) { 229 void *addr; 230 231 addr = dma_alloc_coherent(dma->dev, PAGE_SIZE, 232 &(dma->dma_addr[i]), GFP_KERNEL); 233 if (addr == NULL) 234 goto out_free_pages; 235 236 dma->vaddr_pages[i] = virt_to_page(addr); 237 } 238 dma->vaddr = vmap(dma->vaddr_pages, nr_pages, VM_MAP | VM_IOREMAP, 239 PAGE_KERNEL); 240 if (NULL == dma->vaddr) { 241 dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages); 242 goto out_free_pages; 243 } 244 245 dprintk(1, "vmalloc is at addr %p, size=%lu\n", 246 dma->vaddr, nr_pages << PAGE_SHIFT); 247 248 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT); 249 dma->nr_pages = nr_pages; 250 251 return 0; 252 out_free_pages: 253 while (i > 0) { 254 void *addr; 255 256 i--; 257 addr = page_address(dma->vaddr_pages[i]); 258 dma_free_coherent(dma->dev, PAGE_SIZE, addr, dma->dma_addr[i]); 259 } 260 kfree(dma->dma_addr); 261 dma->dma_addr = NULL; 262 kfree(dma->vaddr_pages); 263 dma->vaddr_pages = NULL; 264 265 return -ENOMEM; 266 267 } 268 269 static int videobuf_dma_init_overlay(struct videobuf_dmabuf *dma, int direction, 270 dma_addr_t addr, unsigned long nr_pages) 271 { 272 dprintk(1, "init overlay [%lu pages @ bus 0x%lx]\n", 273 nr_pages, (unsigned long)addr); 274 dma->direction = direction; 275 276 if (0 == addr) 277 return -EINVAL; 278 279 dma->bus_addr = addr; 280 dma->nr_pages = nr_pages; 281 282 return 0; 283 } 284 285 static int videobuf_dma_map(struct device *dev, struct videobuf_dmabuf *dma) 286 { 287 MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 288 BUG_ON(0 == dma->nr_pages); 289 290 if (dma->pages) { 291 dma->sglist = videobuf_pages_to_sg(dma->pages, dma->nr_pages, 292 dma->offset, dma->size); 293 } 294 if (dma->vaddr) { 295 dma->sglist = videobuf_vmalloc_to_sg(dma->vaddr, 296 dma->nr_pages); 297 } 298 if (dma->bus_addr) { 299 dma->sglist = vmalloc(sizeof(*dma->sglist)); 300 if (NULL != dma->sglist) { 301 dma->sglen = 1; 302 sg_dma_address(&dma->sglist[0]) = dma->bus_addr 303 & PAGE_MASK; 304 dma->sglist[0].offset = dma->bus_addr & ~PAGE_MASK; 305 sg_dma_len(&dma->sglist[0]) = dma->nr_pages * PAGE_SIZE; 306 } 307 } 308 if (NULL == dma->sglist) { 309 dprintk(1, "scatterlist is NULL\n"); 310 return -ENOMEM; 311 } 312 if (!dma->bus_addr) { 313 dma->sglen = dma_map_sg(dev, dma->sglist, 314 dma->nr_pages, dma->direction); 315 if (0 == dma->sglen) { 316 printk(KERN_WARNING 317 "%s: videobuf_map_sg failed\n", __func__); 318 vfree(dma->sglist); 319 dma->sglist = NULL; 320 dma->sglen = 0; 321 return -ENOMEM; 322 } 323 } 324 325 return 0; 326 } 327 328 int videobuf_dma_unmap(struct device *dev, struct videobuf_dmabuf *dma) 329 { 330 MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 331 332 if (!dma->sglen) 333 return 0; 334 335 dma_unmap_sg(dev, dma->sglist, dma->nr_pages, dma->direction); 336 337 vfree(dma->sglist); 338 dma->sglist = NULL; 339 dma->sglen = 0; 340 341 return 0; 342 } 343 EXPORT_SYMBOL_GPL(videobuf_dma_unmap); 344 345 int videobuf_dma_free(struct videobuf_dmabuf *dma) 346 { 347 int i; 348 MAGIC_CHECK(dma->magic, MAGIC_DMABUF); 349 BUG_ON(dma->sglen); 350 351 if (dma->pages) { 352 unpin_user_pages_dirty_lock(dma->pages, dma->nr_pages, 353 dma->direction == DMA_FROM_DEVICE); 354 kfree(dma->pages); 355 dma->pages = NULL; 356 } 357 358 if (dma->dma_addr) { 359 for (i = 0; i < dma->nr_pages; i++) { 360 void *addr; 361 362 addr = page_address(dma->vaddr_pages[i]); 363 dma_free_coherent(dma->dev, PAGE_SIZE, addr, 364 dma->dma_addr[i]); 365 } 366 kfree(dma->dma_addr); 367 dma->dma_addr = NULL; 368 kfree(dma->vaddr_pages); 369 dma->vaddr_pages = NULL; 370 vunmap(dma->vaddr); 371 dma->vaddr = NULL; 372 } 373 374 if (dma->bus_addr) 375 dma->bus_addr = 0; 376 dma->direction = DMA_NONE; 377 378 return 0; 379 } 380 EXPORT_SYMBOL_GPL(videobuf_dma_free); 381 382 /* --------------------------------------------------------------------- */ 383 384 static void videobuf_vm_open(struct vm_area_struct *vma) 385 { 386 struct videobuf_mapping *map = vma->vm_private_data; 387 388 dprintk(2, "vm_open %p [count=%d,vma=%08lx-%08lx]\n", map, 389 map->count, vma->vm_start, vma->vm_end); 390 391 map->count++; 392 } 393 394 static void videobuf_vm_close(struct vm_area_struct *vma) 395 { 396 struct videobuf_mapping *map = vma->vm_private_data; 397 struct videobuf_queue *q = map->q; 398 struct videobuf_dma_sg_memory *mem; 399 int i; 400 401 dprintk(2, "vm_close %p [count=%d,vma=%08lx-%08lx]\n", map, 402 map->count, vma->vm_start, vma->vm_end); 403 404 map->count--; 405 if (0 == map->count) { 406 dprintk(1, "munmap %p q=%p\n", map, q); 407 videobuf_queue_lock(q); 408 for (i = 0; i < VIDEO_MAX_FRAME; i++) { 409 if (NULL == q->bufs[i]) 410 continue; 411 mem = q->bufs[i]->priv; 412 if (!mem) 413 continue; 414 415 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 416 417 if (q->bufs[i]->map != map) 418 continue; 419 q->bufs[i]->map = NULL; 420 q->bufs[i]->baddr = 0; 421 q->ops->buf_release(q, q->bufs[i]); 422 } 423 videobuf_queue_unlock(q); 424 kfree(map); 425 } 426 return; 427 } 428 429 /* 430 * Get a anonymous page for the mapping. Make sure we can DMA to that 431 * memory location with 32bit PCI devices (i.e. don't use highmem for 432 * now ...). Bounce buffers don't work very well for the data rates 433 * video capture has. 434 */ 435 static vm_fault_t videobuf_vm_fault(struct vm_fault *vmf) 436 { 437 struct vm_area_struct *vma = vmf->vma; 438 struct page *page; 439 440 dprintk(3, "fault: fault @ %08lx [vma %08lx-%08lx]\n", 441 vmf->address, vma->vm_start, vma->vm_end); 442 443 page = alloc_page(GFP_USER | __GFP_DMA32); 444 if (!page) 445 return VM_FAULT_OOM; 446 clear_user_highpage(page, vmf->address); 447 vmf->page = page; 448 449 return 0; 450 } 451 452 static const struct vm_operations_struct videobuf_vm_ops = { 453 .open = videobuf_vm_open, 454 .close = videobuf_vm_close, 455 .fault = videobuf_vm_fault, 456 }; 457 458 /* --------------------------------------------------------------------- 459 * SG handlers for the generic methods 460 */ 461 462 /* Allocated area consists on 3 parts: 463 struct video_buffer 464 struct <driver>_buffer (cx88_buffer, saa7134_buf, ...) 465 struct videobuf_dma_sg_memory 466 */ 467 468 static struct videobuf_buffer *__videobuf_alloc_vb(size_t size) 469 { 470 struct videobuf_dma_sg_memory *mem; 471 struct videobuf_buffer *vb; 472 473 vb = kzalloc(size + sizeof(*mem), GFP_KERNEL); 474 if (!vb) 475 return vb; 476 477 mem = vb->priv = ((char *)vb) + size; 478 mem->magic = MAGIC_SG_MEM; 479 480 videobuf_dma_init(&mem->dma); 481 482 dprintk(1, "%s: allocated at %p(%ld+%ld) & %p(%ld)\n", 483 __func__, vb, (long)sizeof(*vb), (long)size - sizeof(*vb), 484 mem, (long)sizeof(*mem)); 485 486 return vb; 487 } 488 489 static void *__videobuf_to_vaddr(struct videobuf_buffer *buf) 490 { 491 struct videobuf_dma_sg_memory *mem = buf->priv; 492 BUG_ON(!mem); 493 494 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 495 496 return mem->dma.vaddr; 497 } 498 499 static int __videobuf_iolock(struct videobuf_queue *q, 500 struct videobuf_buffer *vb, 501 struct v4l2_framebuffer *fbuf) 502 { 503 struct videobuf_dma_sg_memory *mem = vb->priv; 504 unsigned long pages; 505 dma_addr_t bus; 506 int err; 507 508 BUG_ON(!mem); 509 510 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 511 512 if (!mem->dma.dev) 513 mem->dma.dev = q->dev; 514 else 515 WARN_ON(mem->dma.dev != q->dev); 516 517 switch (vb->memory) { 518 case V4L2_MEMORY_MMAP: 519 case V4L2_MEMORY_USERPTR: 520 if (0 == vb->baddr) { 521 /* no userspace addr -- kernel bounce buffer */ 522 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; 523 err = videobuf_dma_init_kernel(&mem->dma, 524 DMA_FROM_DEVICE, 525 pages); 526 if (0 != err) 527 return err; 528 } else if (vb->memory == V4L2_MEMORY_USERPTR) { 529 /* dma directly to userspace */ 530 err = videobuf_dma_init_user(&mem->dma, 531 DMA_FROM_DEVICE, 532 vb->baddr, vb->bsize); 533 if (0 != err) 534 return err; 535 } else { 536 /* NOTE: HACK: videobuf_iolock on V4L2_MEMORY_MMAP 537 buffers can only be called from videobuf_qbuf 538 we take current->mm->mmap_lock there, to prevent 539 locking inversion, so don't take it here */ 540 541 err = videobuf_dma_init_user_locked(&mem->dma, 542 DMA_FROM_DEVICE, 543 vb->baddr, vb->bsize); 544 if (0 != err) 545 return err; 546 } 547 break; 548 case V4L2_MEMORY_OVERLAY: 549 if (NULL == fbuf) 550 return -EINVAL; 551 /* FIXME: need sanity checks for vb->boff */ 552 /* 553 * Using a double cast to avoid compiler warnings when 554 * building for PAE. Compiler doesn't like direct casting 555 * of a 32 bit ptr to 64 bit integer. 556 */ 557 bus = (dma_addr_t)(unsigned long)fbuf->base + vb->boff; 558 pages = PAGE_ALIGN(vb->size) >> PAGE_SHIFT; 559 err = videobuf_dma_init_overlay(&mem->dma, DMA_FROM_DEVICE, 560 bus, pages); 561 if (0 != err) 562 return err; 563 break; 564 default: 565 BUG(); 566 } 567 err = videobuf_dma_map(q->dev, &mem->dma); 568 if (0 != err) 569 return err; 570 571 return 0; 572 } 573 574 static int __videobuf_sync(struct videobuf_queue *q, 575 struct videobuf_buffer *buf) 576 { 577 struct videobuf_dma_sg_memory *mem = buf->priv; 578 BUG_ON(!mem || !mem->dma.sglen); 579 580 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 581 MAGIC_CHECK(mem->dma.magic, MAGIC_DMABUF); 582 583 dma_sync_sg_for_cpu(q->dev, mem->dma.sglist, 584 mem->dma.nr_pages, mem->dma.direction); 585 586 return 0; 587 } 588 589 static int __videobuf_mmap_mapper(struct videobuf_queue *q, 590 struct videobuf_buffer *buf, 591 struct vm_area_struct *vma) 592 { 593 struct videobuf_dma_sg_memory *mem = buf->priv; 594 struct videobuf_mapping *map; 595 unsigned int first, last, size = 0, i; 596 int retval; 597 598 retval = -EINVAL; 599 600 BUG_ON(!mem); 601 MAGIC_CHECK(mem->magic, MAGIC_SG_MEM); 602 603 /* look for first buffer to map */ 604 for (first = 0; first < VIDEO_MAX_FRAME; first++) { 605 if (buf == q->bufs[first]) { 606 size = PAGE_ALIGN(q->bufs[first]->bsize); 607 break; 608 } 609 } 610 611 /* paranoia, should never happen since buf is always valid. */ 612 if (!size) { 613 dprintk(1, "mmap app bug: offset invalid [offset=0x%lx]\n", 614 (vma->vm_pgoff << PAGE_SHIFT)); 615 goto done; 616 } 617 618 last = first; 619 620 /* create mapping + update buffer list */ 621 retval = -ENOMEM; 622 map = kmalloc(sizeof(struct videobuf_mapping), GFP_KERNEL); 623 if (NULL == map) 624 goto done; 625 626 size = 0; 627 for (i = first; i <= last; i++) { 628 if (NULL == q->bufs[i]) 629 continue; 630 q->bufs[i]->map = map; 631 q->bufs[i]->baddr = vma->vm_start + size; 632 size += PAGE_ALIGN(q->bufs[i]->bsize); 633 } 634 635 map->count = 1; 636 map->q = q; 637 vma->vm_ops = &videobuf_vm_ops; 638 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 639 vma->vm_flags &= ~VM_IO; /* using shared anonymous pages */ 640 vma->vm_private_data = map; 641 dprintk(1, "mmap %p: q=%p %08lx-%08lx pgoff %08lx bufs %d-%d\n", 642 map, q, vma->vm_start, vma->vm_end, vma->vm_pgoff, first, last); 643 retval = 0; 644 645 done: 646 return retval; 647 } 648 649 static struct videobuf_qtype_ops sg_ops = { 650 .magic = MAGIC_QTYPE_OPS, 651 652 .alloc_vb = __videobuf_alloc_vb, 653 .iolock = __videobuf_iolock, 654 .sync = __videobuf_sync, 655 .mmap_mapper = __videobuf_mmap_mapper, 656 .vaddr = __videobuf_to_vaddr, 657 }; 658 659 void *videobuf_sg_alloc(size_t size) 660 { 661 struct videobuf_queue q; 662 663 /* Required to make generic handler to call __videobuf_alloc */ 664 q.int_ops = &sg_ops; 665 666 q.msize = size; 667 668 return videobuf_alloc_vb(&q); 669 } 670 EXPORT_SYMBOL_GPL(videobuf_sg_alloc); 671 672 void videobuf_queue_sg_init(struct videobuf_queue *q, 673 const struct videobuf_queue_ops *ops, 674 struct device *dev, 675 spinlock_t *irqlock, 676 enum v4l2_buf_type type, 677 enum v4l2_field field, 678 unsigned int msize, 679 void *priv, 680 struct mutex *ext_lock) 681 { 682 videobuf_queue_core_init(q, ops, dev, irqlock, type, field, msize, 683 priv, &sg_ops, ext_lock); 684 } 685 EXPORT_SYMBOL_GPL(videobuf_queue_sg_init); 686 687