Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: core: Add new DMABUF interface infrastructure

Add the necessary infrastructure to the IIO core to support a new
optional DMABUF based interface.

With this new interface, DMABUF objects (externally created) can be
attached to a IIO buffer, and subsequently used for data transfer.

A userspace application can then use this interface to share DMABUF
objects between several interfaces, allowing it to transfer data in a
zero-copy fashion, for instance between IIO and the USB stack.

The userspace application can also memory-map the DMABUF objects, and
access the sample data directly. The advantage of doing this vs. the
read() interface is that it avoids an extra copy of the data between the
kernel and userspace. This is particularly userful for high-speed
devices which produce several megabytes or even gigabytes of data per
second.

As part of the interface, 3 new IOCTLs have been added:

IIO_BUFFER_DMABUF_ATTACH_IOCTL(int fd):
Attach the DMABUF object identified by the given file descriptor to the
buffer.

IIO_BUFFER_DMABUF_DETACH_IOCTL(int fd):
Detach the DMABUF object identified by the given file descriptor from
the buffer. Note that closing the IIO buffer's file descriptor will
automatically detach all previously attached DMABUF objects.

IIO_BUFFER_DMABUF_ENQUEUE_IOCTL(struct iio_dmabuf *):
Request a data transfer to/from the given DMABUF object. Its file
descriptor, as well as the transfer size and flags are provided in the
"iio_dmabuf" structure.

These three IOCTLs have to be performed on the IIO buffer's file
descriptor, obtained using the IIO_BUFFER_GET_FD_IOCTL() ioctl.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
Co-developed-by: Nuno Sa <nuno.sa@analog.com>
Signed-off-by: Nuno Sa <nuno.sa@analog.com>
Link: https://patch.msgid.link/20240620122726.41232-4-paul@crapouillou.net
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Paul Cercueil and committed by
Jonathan Cameron
3e26d9f0 da5a6fa0

+515
+1
drivers/iio/Kconfig
··· 14 14 15 15 config IIO_BUFFER 16 16 bool "Enable buffer support within IIO" 17 + select DMA_SHARED_BUFFER 17 18 help 18 19 Provide core support for various buffer based data 19 20 acquisition methods.
+459
drivers/iio/industrialio-buffer.c
··· 9 9 * - Better memory allocation techniques? 10 10 * - Alternative access techniques? 11 11 */ 12 + #include <linux/atomic.h> 12 13 #include <linux/anon_inodes.h> 13 14 #include <linux/cleanup.h> 14 15 #include <linux/kernel.h> 15 16 #include <linux/export.h> 16 17 #include <linux/device.h> 18 + #include <linux/dma-buf.h> 19 + #include <linux/dma-fence.h> 20 + #include <linux/dma-resv.h> 17 21 #include <linux/file.h> 18 22 #include <linux/fs.h> 19 23 #include <linux/cdev.h> 20 24 #include <linux/slab.h> 25 + #include <linux/mm.h> 21 26 #include <linux/poll.h> 22 27 #include <linux/sched/signal.h> 23 28 ··· 33 28 #include <linux/iio/sysfs.h> 34 29 #include <linux/iio/buffer.h> 35 30 #include <linux/iio/buffer_impl.h> 31 + 32 + #define DMABUF_ENQUEUE_TIMEOUT_MS 5000 33 + 34 + MODULE_IMPORT_NS(DMA_BUF); 35 + 36 + struct iio_dmabuf_priv { 37 + struct list_head entry; 38 + struct kref ref; 39 + 40 + struct iio_buffer *buffer; 41 + struct iio_dma_buffer_block *block; 42 + 43 + u64 context; 44 + 45 + /* Spinlock used for locking the dma_fence */ 46 + spinlock_t lock; 47 + 48 + struct dma_buf_attachment *attach; 49 + struct sg_table *sgt; 50 + enum dma_data_direction dir; 51 + atomic_t seqno; 52 + }; 53 + 54 + struct iio_dma_fence { 55 + struct dma_fence base; 56 + struct iio_dmabuf_priv *priv; 57 + struct work_struct work; 58 + }; 36 59 37 60 static const char * const iio_endian_prefix[] = { 38 61 [IIO_BE] = "be", ··· 366 333 { 367 334 INIT_LIST_HEAD(&buffer->demux_list); 368 335 INIT_LIST_HEAD(&buffer->buffer_list); 336 + INIT_LIST_HEAD(&buffer->dmabufs); 337 + mutex_init(&buffer->dmabufs_mutex); 369 338 init_waitqueue_head(&buffer->pollq); 370 339 kref_init(&buffer->ref); 371 340 if (!buffer->watermark) ··· 1561 1526 kfree(iio_dev_opaque->legacy_scan_el_group.attrs); 1562 1527 } 1563 1528 1529 + static void iio_buffer_dmabuf_release(struct kref *ref) 1530 + { 1531 + struct iio_dmabuf_priv *priv = container_of(ref, struct iio_dmabuf_priv, ref); 1532 + struct dma_buf_attachment *attach = priv->attach; 1533 + struct iio_buffer *buffer = priv->buffer; 1534 + struct dma_buf *dmabuf = attach->dmabuf; 1535 + 1536 + dma_resv_lock(dmabuf->resv, NULL); 1537 + dma_buf_unmap_attachment(attach, priv->sgt, priv->dir); 1538 + dma_resv_unlock(dmabuf->resv); 1539 + 1540 + buffer->access->detach_dmabuf(buffer, priv->block); 1541 + 1542 + dma_buf_detach(attach->dmabuf, attach); 1543 + dma_buf_put(dmabuf); 1544 + kfree(priv); 1545 + } 1546 + 1547 + static void iio_buffer_dmabuf_get(struct dma_buf_attachment *attach) 1548 + { 1549 + struct iio_dmabuf_priv *priv = attach->importer_priv; 1550 + 1551 + kref_get(&priv->ref); 1552 + } 1553 + 1554 + static void iio_buffer_dmabuf_put(struct dma_buf_attachment *attach) 1555 + { 1556 + struct iio_dmabuf_priv *priv = attach->importer_priv; 1557 + 1558 + kref_put(&priv->ref, iio_buffer_dmabuf_release); 1559 + } 1560 + 1564 1561 static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep) 1565 1562 { 1566 1563 struct iio_dev_buffer_pair *ib = filep->private_data; 1567 1564 struct iio_dev *indio_dev = ib->indio_dev; 1568 1565 struct iio_buffer *buffer = ib->buffer; 1566 + struct iio_dmabuf_priv *priv, *tmp; 1569 1567 1570 1568 wake_up(&buffer->pollq); 1569 + 1570 + guard(mutex)(&buffer->dmabufs_mutex); 1571 + 1572 + /* Close all attached DMABUFs */ 1573 + list_for_each_entry_safe(priv, tmp, &buffer->dmabufs, entry) { 1574 + list_del_init(&priv->entry); 1575 + iio_buffer_dmabuf_put(priv->attach); 1576 + } 1571 1577 1572 1578 kfree(ib); 1573 1579 clear_bit(IIO_BUSY_BIT_POS, &buffer->flags); ··· 1617 1541 return 0; 1618 1542 } 1619 1543 1544 + static int iio_dma_resv_lock(struct dma_buf *dmabuf, bool nonblock) 1545 + { 1546 + if (!nonblock) 1547 + return dma_resv_lock_interruptible(dmabuf->resv, NULL); 1548 + 1549 + if (!dma_resv_trylock(dmabuf->resv)) 1550 + return -EBUSY; 1551 + 1552 + return 0; 1553 + } 1554 + 1555 + static struct dma_buf_attachment * 1556 + iio_buffer_find_attachment(struct iio_dev_buffer_pair *ib, 1557 + struct dma_buf *dmabuf, bool nonblock) 1558 + { 1559 + struct device *dev = ib->indio_dev->dev.parent; 1560 + struct iio_buffer *buffer = ib->buffer; 1561 + struct dma_buf_attachment *attach = NULL; 1562 + struct iio_dmabuf_priv *priv; 1563 + 1564 + guard(mutex)(&buffer->dmabufs_mutex); 1565 + 1566 + list_for_each_entry(priv, &buffer->dmabufs, entry) { 1567 + if (priv->attach->dev == dev 1568 + && priv->attach->dmabuf == dmabuf) { 1569 + attach = priv->attach; 1570 + break; 1571 + } 1572 + } 1573 + 1574 + if (attach) 1575 + iio_buffer_dmabuf_get(attach); 1576 + 1577 + return attach ?: ERR_PTR(-EPERM); 1578 + } 1579 + 1580 + static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib, 1581 + int __user *user_fd, bool nonblock) 1582 + { 1583 + struct iio_dev *indio_dev = ib->indio_dev; 1584 + struct iio_buffer *buffer = ib->buffer; 1585 + struct dma_buf_attachment *attach; 1586 + struct iio_dmabuf_priv *priv, *each; 1587 + struct dma_buf *dmabuf; 1588 + int err, fd; 1589 + 1590 + if (!buffer->access->attach_dmabuf 1591 + || !buffer->access->detach_dmabuf 1592 + || !buffer->access->enqueue_dmabuf) 1593 + return -EPERM; 1594 + 1595 + if (copy_from_user(&fd, user_fd, sizeof(fd))) 1596 + return -EFAULT; 1597 + 1598 + priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1599 + if (!priv) 1600 + return -ENOMEM; 1601 + 1602 + spin_lock_init(&priv->lock); 1603 + priv->context = dma_fence_context_alloc(1); 1604 + 1605 + dmabuf = dma_buf_get(fd); 1606 + if (IS_ERR(dmabuf)) { 1607 + err = PTR_ERR(dmabuf); 1608 + goto err_free_priv; 1609 + } 1610 + 1611 + attach = dma_buf_attach(dmabuf, indio_dev->dev.parent); 1612 + if (IS_ERR(attach)) { 1613 + err = PTR_ERR(attach); 1614 + goto err_dmabuf_put; 1615 + } 1616 + 1617 + err = iio_dma_resv_lock(dmabuf, nonblock); 1618 + if (err) 1619 + goto err_dmabuf_detach; 1620 + 1621 + priv->dir = buffer->direction == IIO_BUFFER_DIRECTION_IN 1622 + ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 1623 + 1624 + priv->sgt = dma_buf_map_attachment(attach, priv->dir); 1625 + if (IS_ERR(priv->sgt)) { 1626 + err = PTR_ERR(priv->sgt); 1627 + dev_err(&indio_dev->dev, "Unable to map attachment: %d\n", err); 1628 + goto err_resv_unlock; 1629 + } 1630 + 1631 + kref_init(&priv->ref); 1632 + priv->buffer = buffer; 1633 + priv->attach = attach; 1634 + attach->importer_priv = priv; 1635 + 1636 + priv->block = buffer->access->attach_dmabuf(buffer, attach); 1637 + if (IS_ERR(priv->block)) { 1638 + err = PTR_ERR(priv->block); 1639 + goto err_dmabuf_unmap_attachment; 1640 + } 1641 + 1642 + dma_resv_unlock(dmabuf->resv); 1643 + 1644 + mutex_lock(&buffer->dmabufs_mutex); 1645 + 1646 + /* 1647 + * Check whether we already have an attachment for this driver/DMABUF 1648 + * combo. If we do, refuse to attach. 1649 + */ 1650 + list_for_each_entry(each, &buffer->dmabufs, entry) { 1651 + if (each->attach->dev == indio_dev->dev.parent 1652 + && each->attach->dmabuf == dmabuf) { 1653 + /* 1654 + * We unlocked the reservation object, so going through 1655 + * the cleanup code would mean re-locking it first. 1656 + * At this stage it is simpler to free the attachment 1657 + * using iio_buffer_dma_put(). 1658 + */ 1659 + mutex_unlock(&buffer->dmabufs_mutex); 1660 + iio_buffer_dmabuf_put(attach); 1661 + return -EBUSY; 1662 + } 1663 + } 1664 + 1665 + /* Otherwise, add the new attachment to our dmabufs list. */ 1666 + list_add(&priv->entry, &buffer->dmabufs); 1667 + mutex_unlock(&buffer->dmabufs_mutex); 1668 + 1669 + return 0; 1670 + 1671 + err_dmabuf_unmap_attachment: 1672 + dma_buf_unmap_attachment(attach, priv->sgt, priv->dir); 1673 + err_resv_unlock: 1674 + dma_resv_unlock(dmabuf->resv); 1675 + err_dmabuf_detach: 1676 + dma_buf_detach(dmabuf, attach); 1677 + err_dmabuf_put: 1678 + dma_buf_put(dmabuf); 1679 + err_free_priv: 1680 + kfree(priv); 1681 + 1682 + return err; 1683 + } 1684 + 1685 + static int iio_buffer_detach_dmabuf(struct iio_dev_buffer_pair *ib, 1686 + int __user *user_req, bool nonblock) 1687 + { 1688 + struct iio_buffer *buffer = ib->buffer; 1689 + struct iio_dev *indio_dev = ib->indio_dev; 1690 + struct iio_dmabuf_priv *priv; 1691 + struct dma_buf *dmabuf; 1692 + int dmabuf_fd, ret = -EPERM; 1693 + 1694 + if (copy_from_user(&dmabuf_fd, user_req, sizeof(dmabuf_fd))) 1695 + return -EFAULT; 1696 + 1697 + dmabuf = dma_buf_get(dmabuf_fd); 1698 + if (IS_ERR(dmabuf)) 1699 + return PTR_ERR(dmabuf); 1700 + 1701 + guard(mutex)(&buffer->dmabufs_mutex); 1702 + 1703 + list_for_each_entry(priv, &buffer->dmabufs, entry) { 1704 + if (priv->attach->dev == indio_dev->dev.parent 1705 + && priv->attach->dmabuf == dmabuf) { 1706 + list_del(&priv->entry); 1707 + 1708 + /* Unref the reference from iio_buffer_attach_dmabuf() */ 1709 + iio_buffer_dmabuf_put(priv->attach); 1710 + ret = 0; 1711 + break; 1712 + } 1713 + } 1714 + 1715 + dma_buf_put(dmabuf); 1716 + 1717 + return ret; 1718 + } 1719 + 1720 + static const char * 1721 + iio_buffer_dma_fence_get_driver_name(struct dma_fence *fence) 1722 + { 1723 + return "iio"; 1724 + } 1725 + 1726 + static void iio_buffer_dma_fence_release(struct dma_fence *fence) 1727 + { 1728 + struct iio_dma_fence *iio_fence = 1729 + container_of(fence, struct iio_dma_fence, base); 1730 + 1731 + kfree(iio_fence); 1732 + } 1733 + 1734 + static const struct dma_fence_ops iio_buffer_dma_fence_ops = { 1735 + .get_driver_name = iio_buffer_dma_fence_get_driver_name, 1736 + .get_timeline_name = iio_buffer_dma_fence_get_driver_name, 1737 + .release = iio_buffer_dma_fence_release, 1738 + }; 1739 + 1740 + static int iio_buffer_enqueue_dmabuf(struct iio_dev_buffer_pair *ib, 1741 + struct iio_dmabuf __user *iio_dmabuf_req, 1742 + bool nonblock) 1743 + { 1744 + struct iio_buffer *buffer = ib->buffer; 1745 + struct iio_dmabuf iio_dmabuf; 1746 + struct dma_buf_attachment *attach; 1747 + struct iio_dmabuf_priv *priv; 1748 + struct iio_dma_fence *fence; 1749 + struct dma_buf *dmabuf; 1750 + unsigned long timeout; 1751 + bool cookie, cyclic, dma_to_ram; 1752 + long retl; 1753 + u32 seqno; 1754 + int ret; 1755 + 1756 + if (copy_from_user(&iio_dmabuf, iio_dmabuf_req, sizeof(iio_dmabuf))) 1757 + return -EFAULT; 1758 + 1759 + if (iio_dmabuf.flags & ~IIO_BUFFER_DMABUF_SUPPORTED_FLAGS) 1760 + return -EINVAL; 1761 + 1762 + cyclic = iio_dmabuf.flags & IIO_BUFFER_DMABUF_CYCLIC; 1763 + 1764 + /* Cyclic flag is only supported on output buffers */ 1765 + if (cyclic && buffer->direction != IIO_BUFFER_DIRECTION_OUT) 1766 + return -EINVAL; 1767 + 1768 + dmabuf = dma_buf_get(iio_dmabuf.fd); 1769 + if (IS_ERR(dmabuf)) 1770 + return PTR_ERR(dmabuf); 1771 + 1772 + if (!iio_dmabuf.bytes_used || iio_dmabuf.bytes_used > dmabuf->size) { 1773 + ret = -EINVAL; 1774 + goto err_dmabuf_put; 1775 + } 1776 + 1777 + attach = iio_buffer_find_attachment(ib, dmabuf, nonblock); 1778 + if (IS_ERR(attach)) { 1779 + ret = PTR_ERR(attach); 1780 + goto err_dmabuf_put; 1781 + } 1782 + 1783 + priv = attach->importer_priv; 1784 + 1785 + fence = kmalloc(sizeof(*fence), GFP_KERNEL); 1786 + if (!fence) { 1787 + ret = -ENOMEM; 1788 + goto err_attachment_put; 1789 + } 1790 + 1791 + fence->priv = priv; 1792 + 1793 + seqno = atomic_add_return(1, &priv->seqno); 1794 + 1795 + /* 1796 + * The transfers are guaranteed to be processed in the order they are 1797 + * enqueued, so we can use a simple incrementing sequence number for 1798 + * the dma_fence. 1799 + */ 1800 + dma_fence_init(&fence->base, &iio_buffer_dma_fence_ops, 1801 + &priv->lock, priv->context, seqno); 1802 + 1803 + ret = iio_dma_resv_lock(dmabuf, nonblock); 1804 + if (ret) 1805 + goto err_fence_put; 1806 + 1807 + timeout = nonblock ? 0 : msecs_to_jiffies(DMABUF_ENQUEUE_TIMEOUT_MS); 1808 + dma_to_ram = buffer->direction == IIO_BUFFER_DIRECTION_IN; 1809 + 1810 + /* Make sure we don't have writers */ 1811 + retl = dma_resv_wait_timeout(dmabuf->resv, 1812 + dma_resv_usage_rw(dma_to_ram), 1813 + true, timeout); 1814 + if (retl == 0) 1815 + retl = -EBUSY; 1816 + if (retl < 0) { 1817 + ret = (int)retl; 1818 + goto err_resv_unlock; 1819 + } 1820 + 1821 + if (buffer->access->lock_queue) 1822 + buffer->access->lock_queue(buffer); 1823 + 1824 + ret = dma_resv_reserve_fences(dmabuf->resv, 1); 1825 + if (ret) 1826 + goto err_queue_unlock; 1827 + 1828 + dma_resv_add_fence(dmabuf->resv, &fence->base, 1829 + dma_to_ram ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ); 1830 + dma_resv_unlock(dmabuf->resv); 1831 + 1832 + cookie = dma_fence_begin_signalling(); 1833 + 1834 + ret = buffer->access->enqueue_dmabuf(buffer, priv->block, &fence->base, 1835 + priv->sgt, iio_dmabuf.bytes_used, 1836 + cyclic); 1837 + if (ret) { 1838 + /* 1839 + * DMABUF enqueue failed, but we already added the fence. 1840 + * Signal the error through the fence completion mechanism. 1841 + */ 1842 + iio_buffer_signal_dmabuf_done(&fence->base, ret); 1843 + } 1844 + 1845 + if (buffer->access->unlock_queue) 1846 + buffer->access->unlock_queue(buffer); 1847 + 1848 + dma_fence_end_signalling(cookie); 1849 + dma_buf_put(dmabuf); 1850 + 1851 + return ret; 1852 + 1853 + err_queue_unlock: 1854 + if (buffer->access->unlock_queue) 1855 + buffer->access->unlock_queue(buffer); 1856 + err_resv_unlock: 1857 + dma_resv_unlock(dmabuf->resv); 1858 + err_fence_put: 1859 + dma_fence_put(&fence->base); 1860 + err_attachment_put: 1861 + iio_buffer_dmabuf_put(attach); 1862 + err_dmabuf_put: 1863 + dma_buf_put(dmabuf); 1864 + 1865 + return ret; 1866 + } 1867 + 1868 + static void iio_buffer_cleanup(struct work_struct *work) 1869 + { 1870 + struct iio_dma_fence *fence = 1871 + container_of(work, struct iio_dma_fence, work); 1872 + struct iio_dmabuf_priv *priv = fence->priv; 1873 + struct dma_buf_attachment *attach = priv->attach; 1874 + 1875 + dma_fence_put(&fence->base); 1876 + iio_buffer_dmabuf_put(attach); 1877 + } 1878 + 1879 + void iio_buffer_signal_dmabuf_done(struct dma_fence *fence, int ret) 1880 + { 1881 + struct iio_dma_fence *iio_fence = 1882 + container_of(fence, struct iio_dma_fence, base); 1883 + bool cookie = dma_fence_begin_signalling(); 1884 + 1885 + /* 1886 + * Get a reference to the fence, so that it's not freed as soon as 1887 + * it's signaled. 1888 + */ 1889 + dma_fence_get(fence); 1890 + 1891 + fence->error = ret; 1892 + dma_fence_signal(fence); 1893 + dma_fence_end_signalling(cookie); 1894 + 1895 + /* 1896 + * The fence will be unref'd in iio_buffer_cleanup. 1897 + * It can't be done here, as the unref functions might try to lock the 1898 + * resv object, which can deadlock. 1899 + */ 1900 + INIT_WORK(&iio_fence->work, iio_buffer_cleanup); 1901 + schedule_work(&iio_fence->work); 1902 + } 1903 + EXPORT_SYMBOL_GPL(iio_buffer_signal_dmabuf_done); 1904 + 1905 + static long iio_buffer_chrdev_ioctl(struct file *filp, 1906 + unsigned int cmd, unsigned long arg) 1907 + { 1908 + struct iio_dev_buffer_pair *ib = filp->private_data; 1909 + void __user *_arg = (void __user *)arg; 1910 + bool nonblock = filp->f_flags & O_NONBLOCK; 1911 + 1912 + switch (cmd) { 1913 + case IIO_BUFFER_DMABUF_ATTACH_IOCTL: 1914 + return iio_buffer_attach_dmabuf(ib, _arg, nonblock); 1915 + case IIO_BUFFER_DMABUF_DETACH_IOCTL: 1916 + return iio_buffer_detach_dmabuf(ib, _arg, nonblock); 1917 + case IIO_BUFFER_DMABUF_ENQUEUE_IOCTL: 1918 + return iio_buffer_enqueue_dmabuf(ib, _arg, nonblock); 1919 + default: 1920 + return -EINVAL; 1921 + } 1922 + } 1923 + 1620 1924 static const struct file_operations iio_buffer_chrdev_fileops = { 1621 1925 .owner = THIS_MODULE, 1622 1926 .llseek = noop_llseek, 1623 1927 .read = iio_buffer_read, 1624 1928 .write = iio_buffer_write, 1929 + .unlocked_ioctl = iio_buffer_chrdev_ioctl, 1930 + .compat_ioctl = compat_ptr_ioctl, 1625 1931 .poll = iio_buffer_poll, 1626 1932 .release = iio_buffer_chrdev_release, 1627 1933 }; ··· 2452 1994 { 2453 1995 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 2454 1996 1997 + mutex_destroy(&buffer->dmabufs_mutex); 2455 1998 buffer->access->release(buffer); 2456 1999 } 2457 2000
+33
include/linux/iio/buffer_impl.h
··· 9 9 #include <uapi/linux/iio/buffer.h> 10 10 #include <linux/iio/buffer.h> 11 11 12 + struct dma_buf_attachment; 13 + struct dma_fence; 12 14 struct iio_dev; 15 + struct iio_dma_buffer_block; 13 16 struct iio_buffer; 17 + struct sg_table; 14 18 15 19 /** 16 20 * INDIO_BUFFER_FLAG_FIXED_WATERMARK - Watermark level of the buffer can not be ··· 43 39 * device stops sampling. Calles are balanced with @enable. 44 40 * @release: called when the last reference to the buffer is dropped, 45 41 * should free all resources allocated by the buffer. 42 + * @attach_dmabuf: called from userspace via ioctl to attach one external 43 + * DMABUF. 44 + * @detach_dmabuf: called from userspace via ioctl to detach one previously 45 + * attached DMABUF. 46 + * @enqueue_dmabuf: called from userspace via ioctl to queue this DMABUF 47 + * object to this buffer. Requires a valid DMABUF fd, that 48 + * was previouly attached to this buffer. 49 + * @lock_queue: called when the core needs to lock the buffer queue; 50 + * it is used when enqueueing DMABUF objects. 51 + * @unlock_queue: used to unlock a previously locked buffer queue 46 52 * @modes: Supported operating modes by this buffer type 47 53 * @flags: A bitmask combination of INDIO_BUFFER_FLAG_* 48 54 * ··· 81 67 int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev); 82 68 83 69 void (*release)(struct iio_buffer *buffer); 70 + 71 + struct iio_dma_buffer_block * (*attach_dmabuf)(struct iio_buffer *buffer, 72 + struct dma_buf_attachment *attach); 73 + void (*detach_dmabuf)(struct iio_buffer *buffer, 74 + struct iio_dma_buffer_block *block); 75 + int (*enqueue_dmabuf)(struct iio_buffer *buffer, 76 + struct iio_dma_buffer_block *block, 77 + struct dma_fence *fence, struct sg_table *sgt, 78 + size_t size, bool cyclic); 79 + void (*lock_queue)(struct iio_buffer *buffer); 80 + void (*unlock_queue)(struct iio_buffer *buffer); 84 81 85 82 unsigned int modes; 86 83 unsigned int flags; ··· 161 136 162 137 /* @ref: Reference count of the buffer. */ 163 138 struct kref ref; 139 + 140 + /* @dmabufs: List of DMABUF attachments */ 141 + struct list_head dmabufs; /* P: dmabufs_mutex */ 142 + 143 + /* @dmabufs_mutex: Protects dmabufs */ 144 + struct mutex dmabufs_mutex; 164 145 }; 165 146 166 147 /** ··· 189 158 190 159 struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer); 191 160 void iio_buffer_put(struct iio_buffer *buffer); 161 + 162 + void iio_buffer_signal_dmabuf_done(struct dma_fence *fence, int ret); 192 163 193 164 #else /* CONFIG_IIO_BUFFER */ 194 165
+22
include/uapi/linux/iio/buffer.h
··· 5 5 #ifndef _UAPI_IIO_BUFFER_H_ 6 6 #define _UAPI_IIO_BUFFER_H_ 7 7 8 + #include <linux/types.h> 9 + 10 + /* Flags for iio_dmabuf.flags */ 11 + #define IIO_BUFFER_DMABUF_CYCLIC (1 << 0) 12 + #define IIO_BUFFER_DMABUF_SUPPORTED_FLAGS 0x00000001 13 + 14 + /** 15 + * struct iio_dmabuf - Descriptor for a single IIO DMABUF object 16 + * @fd: file descriptor of the DMABUF object 17 + * @flags: one or more IIO_BUFFER_DMABUF_* flags 18 + * @bytes_used: number of bytes used in this DMABUF for the data transfer. 19 + * Should generally be set to the DMABUF's size. 20 + */ 21 + struct iio_dmabuf { 22 + __u32 fd; 23 + __u32 flags; 24 + __u64 bytes_used; 25 + }; 26 + 8 27 #define IIO_BUFFER_GET_FD_IOCTL _IOWR('i', 0x91, int) 28 + #define IIO_BUFFER_DMABUF_ATTACH_IOCTL _IOW('i', 0x92, int) 29 + #define IIO_BUFFER_DMABUF_DETACH_IOCTL _IOW('i', 0x93, int) 30 + #define IIO_BUFFER_DMABUF_ENQUEUE_IOCTL _IOW('i', 0x94, struct iio_dmabuf) 9 31 10 32 #endif /* _UAPI_IIO_BUFFER_H_ */