dma-buf-phys.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DMA-BUF contiguous buffer physical address user-space exporter
  4. *
  5. * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
  6. * Andrew F. Davis <afd@ti.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/dma-buf.h>
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <uapi/linux/dma_buf_phys.h>
  19. #define DEVICE_NAME "dma-buf-phys"
  20. struct dma_buf_phys_priv {
  21. struct miscdevice miscdev;
  22. };
  23. struct dma_buf_phys_file {
  24. struct device *dev;
  25. struct dma_buf *dma_buf;
  26. struct dma_buf_attachment *attachment;
  27. struct sg_table *sgt;
  28. };
  29. static int dma_buf_phys_open(struct inode *inode, struct file *file)
  30. {
  31. struct miscdevice *miscdev = file->private_data;
  32. struct device *dev = miscdev->this_device;
  33. struct dma_buf_phys_file *priv;
  34. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  35. if (!priv)
  36. return -ENOMEM;
  37. priv->dev = dev;
  38. file->private_data = (void *)priv;
  39. return 0;
  40. }
  41. static int dma_buf_phys_release(struct inode *inode, struct file *file)
  42. {
  43. struct dma_buf_phys_file *priv = file->private_data;
  44. if (priv->attachment && priv->sgt)
  45. dma_buf_unmap_attachment(priv->attachment, priv->sgt, DMA_BIDIRECTIONAL);
  46. if (priv->dma_buf && priv->attachment)
  47. dma_buf_detach(priv->dma_buf, priv->attachment);
  48. if (priv->dma_buf)
  49. dma_buf_put(priv->dma_buf);
  50. kfree(priv);
  51. return 0;
  52. }
  53. static int dma_buf_phys_convert(struct dma_buf_phys_file *priv, int fd, u64 *phys)
  54. {
  55. struct device *dev = priv->dev;
  56. struct dma_buf *dma_buf;
  57. struct dma_buf_attachment *attachment;
  58. struct sg_table *sgt;
  59. dma_addr_t dma_addr;
  60. int ret;
  61. dma_buf = dma_buf_get(fd);
  62. if (IS_ERR(dma_buf))
  63. return PTR_ERR(dma_buf);
  64. /* Attach as the parent device as it will have the correct DMA ops set */
  65. attachment = dma_buf_attach(dma_buf, dev->parent);
  66. if (IS_ERR(attachment)) {
  67. ret = PTR_ERR(attachment);
  68. goto fail_put;
  69. }
  70. sgt = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
  71. if (IS_ERR(sgt)) {
  72. ret = PTR_ERR(sgt);
  73. goto fail_detach;
  74. }
  75. /* Without PAT only physically contiguous buffers can be supported */
  76. if (sgt->orig_nents != 1) {
  77. dev_err(dev, "DMA-BUF not contiguous\n");
  78. ret = -EINVAL;
  79. goto fail_unmap;
  80. }
  81. dma_addr = sg_dma_address(sgt->sgl);
  82. *phys = dma_addr;
  83. priv->dma_buf = dma_buf;
  84. priv->attachment = attachment;
  85. priv->sgt = sgt;
  86. return 0;
  87. fail_unmap:
  88. dma_buf_unmap_attachment(attachment, sgt, DMA_BIDIRECTIONAL);
  89. fail_detach:
  90. dma_buf_detach(dma_buf, attachment);
  91. fail_put:
  92. dma_buf_put(dma_buf);
  93. return ret;
  94. }
  95. static long dma_buf_phys_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  96. {
  97. struct dma_buf_phys_file *priv = file->private_data;
  98. switch (cmd) {
  99. case DMA_BUF_PHYS_IOC_CONVERT:
  100. {
  101. struct dma_buf_phys_data data;
  102. int ret;
  103. /*
  104. * TODO: this should likely be properly serialized, but I
  105. * see no reason this file would ever need to be shared.
  106. */
  107. /* one attachment per file */
  108. if (priv->dma_buf)
  109. return -EFAULT;
  110. if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
  111. return -EFAULT;
  112. ret = dma_buf_phys_convert(priv, data.fd, &data.phys);
  113. if (ret)
  114. return ret;
  115. if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))
  116. return -EFAULT;
  117. break;
  118. }
  119. default:
  120. return -ENOTTY;
  121. }
  122. return 0;
  123. }
  124. static const struct file_operations dma_buf_phys_fops = {
  125. .owner = THIS_MODULE,
  126. .open = dma_buf_phys_open,
  127. .release = dma_buf_phys_release,
  128. .unlocked_ioctl = dma_buf_phys_ioctl,
  129. #ifdef CONFIG_COMPAT
  130. .compat_ioctl = dma_buf_phys_ioctl,
  131. #endif
  132. };
  133. static int dma_buf_phys_probe(struct platform_device *pdev)
  134. {
  135. struct dma_buf_phys_priv *priv;
  136. struct device *dev = &pdev->dev;
  137. int err;
  138. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  139. if (!priv)
  140. return -ENOMEM;
  141. dev_set_drvdata(dev, priv);
  142. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  143. priv->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s", DEVICE_NAME);
  144. priv->miscdev.fops = &dma_buf_phys_fops;
  145. priv->miscdev.parent = dev;
  146. err = misc_register(&priv->miscdev);
  147. if (err) {
  148. dev_err(dev, "unable to register DMA-BUF to Phys misc device\n");
  149. return err;
  150. }
  151. return 0;
  152. }
  153. static int dma_buf_phys_remove(struct platform_device *pdev)
  154. {
  155. struct dma_buf_phys_priv *priv = dev_get_drvdata(&pdev->dev);
  156. misc_deregister(&priv->miscdev);
  157. return 0;
  158. }
  159. static const struct of_device_id dma_buf_phys_of_match[] = {
  160. { .compatible = "ti,dma_buf_phys", },
  161. {},
  162. };
  163. MODULE_DEVICE_TABLE(of, dma_buf_phys_of_match);
  164. static struct platform_driver dma_buf_phys_driver = {
  165. .probe = dma_buf_phys_probe,
  166. .remove = dma_buf_phys_remove,
  167. .driver = {
  168. .name = "dma_buf_phys",
  169. .of_match_table = dma_buf_phys_of_match,
  170. }
  171. };
  172. module_platform_driver(dma_buf_phys_driver);
  173. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  174. MODULE_DESCRIPTION("DMA-BUF contiguous buffer physical address user-space exporter");
  175. MODULE_LICENSE("GPL v2");