^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright © 2012 Red Hat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * The above copyright notice and this permission notice (including the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * paragraph) shall be included in all copies or substantial portions of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Authors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Dave Airlie <airlied@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Rob Clark <rob.clark@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/dma-buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <drm/drm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <drm/drm_drv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <drm/drm_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <drm/drm_framebuffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <drm/drm_gem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <drm/drm_prime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "drm_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * DOC: overview and lifetime rules
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * Similar to GEM global names, PRIME file descriptors are also used to share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * buffer objects across processes. They offer additional security: as file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * descriptors must be explicitly sent over UNIX domain sockets to be shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * between applications, they can't be guessed like the globally unique GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * names.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Drivers that support the PRIME API implement the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * &drm_driver.prime_handle_to_fd and &drm_driver.prime_fd_to_handle operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * GEM based drivers must use drm_gem_prime_handle_to_fd() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * drm_gem_prime_fd_to_handle() to implement these. For GEM based drivers the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * actual driver interfaces is provided through the &drm_gem_object_funcs.export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * and &drm_driver.gem_prime_import hooks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * &dma_buf_ops implementations for GEM drivers are all individually exported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * for drivers which need to overwrite or reimplement some of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Reference Counting for GEM Drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * On the export the &dma_buf holds a reference to the exported buffer object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * IOCTL, when it first calls &drm_gem_object_funcs.export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * and stores the exporting GEM object in the &dma_buf.priv field. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * reference needs to be released when the final reference to the &dma_buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * itself is dropped and its &dma_buf_ops.release function is called. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * GEM-based drivers, the &dma_buf should be exported using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Thus the chain of references always flows in one direction, avoiding loops:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * importing GEM object -> dma-buf -> exported GEM bo. A further complication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * are the lookup caches for import and export. These are required to guarantee
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * that any given object will always have only one uniqe userspace handle. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * is required to allow userspace to detect duplicated imports, since some GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * drivers do fail command submissions if a given buffer object is listed more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * than once. These import and export caches in &drm_prime_file_private only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * retain a weak reference, which is cleaned up when the corresponding object is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Self-importing: If userspace is using PRIME as a replacement for flink then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * it will get a fd->handle request for a GEM object that it created. Drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * should detect this situation and return back the underlying object from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * dma-buf private. For GEM based drivers this is handled in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * drm_gem_prime_import() already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct drm_prime_member {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct dma_buf *dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) uint32_t handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct rb_node dmabuf_rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct rb_node handle_rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct dma_buf *dma_buf, uint32_t handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct drm_prime_member *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct rb_node **p, *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) member = kmalloc(sizeof(*member), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!member)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) get_dma_buf(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) member->dma_buf = dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) member->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) rb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) p = &prime_fpriv->dmabufs.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct drm_prime_member *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) rb = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (dma_buf > pos->dma_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) p = &rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) p = &rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rb_link_node(&member->dmabuf_rb, rb, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) rb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) p = &prime_fpriv->handles.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct drm_prime_member *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) rb = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) pos = rb_entry(rb, struct drm_prime_member, handle_rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (handle > pos->handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) p = &rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) p = &rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rb_link_node(&member->handle_rb, rb, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) uint32_t handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) rb = prime_fpriv->handles.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) while (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct drm_prime_member *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) member = rb_entry(rb, struct drm_prime_member, handle_rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (member->handle == handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return member->dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) else if (member->handle < handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) rb = rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) rb = rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct dma_buf *dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) uint32_t *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) rb = prime_fpriv->dmabufs.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) while (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct drm_prime_member *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (member->dma_buf == dma_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *handle = member->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) } else if (member->dma_buf < dma_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) rb = rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) rb = rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) uint32_t handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct rb_node *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) mutex_lock(&prime_fpriv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) rb = prime_fpriv->handles.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) while (rb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct drm_prime_member *member;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) member = rb_entry(rb, struct drm_prime_member, handle_rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (member->handle == handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) rb_erase(&member->handle_rb, &prime_fpriv->handles);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dma_buf_put(member->dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) kfree(member);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) } else if (member->handle < handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) rb = rb->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) rb = rb->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) mutex_unlock(&prime_fpriv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) mutex_init(&prime_fpriv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) prime_fpriv->dmabufs = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) prime_fpriv->handles = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* by now drm_gem_release should've made sure the list is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @dev: parent device for the exported dmabuf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * @exp_info: the export information used by dma_buf_export()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * This wraps dma_buf_export() for use by generic GEM drivers that are using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * a reference to the &drm_device and the exported &drm_gem_object (stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * Returns the new dmabuf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct dma_buf_export_info *exp_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct drm_gem_object *obj = exp_info->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct dma_buf *dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) dma_buf = dma_buf_export(exp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (IS_ERR(dma_buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) drm_dev_get(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) drm_gem_object_get(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) EXPORT_SYMBOL(drm_gem_dmabuf_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * @dma_buf: buffer to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * must use this in their &dma_buf_ops structure as the release callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * drm_gem_dmabuf_release() should be used in conjunction with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * drm_gem_dmabuf_export().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct drm_device *dev = obj->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* drop the reference on the export fd holds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) drm_gem_object_put(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) drm_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) EXPORT_SYMBOL(drm_gem_dmabuf_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * @dev: dev to export the buffer from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * @file_priv: drm file-private structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * @prime_fd: fd id of the dma-buf which should be imported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * @handle: pointer to storage for the handle of the imported buffer object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * This is the PRIME import function which must be used mandatorily by GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * drivers to ensure correct lifetime management of the underlying GEM object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * The actual importing of GEM object from the dma-buf is done through the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * &drm_driver.gem_prime_import driver callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Returns 0 on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int drm_gem_prime_fd_to_handle(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct drm_file *file_priv, int prime_fd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) uint32_t *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct dma_buf *dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct drm_gem_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) dma_buf = dma_buf_get(prime_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (IS_ERR(dma_buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return PTR_ERR(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) mutex_lock(&file_priv->prime.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) ret = drm_prime_lookup_buf_handle(&file_priv->prime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) dma_buf, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /* never seen this one, need to import */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) mutex_lock(&dev->object_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (dev->driver->gem_prime_import)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) obj = dev->driver->gem_prime_import(dev, dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) obj = drm_gem_prime_import(dev, dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (IS_ERR(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) ret = PTR_ERR(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (obj->dma_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) WARN_ON(obj->dma_buf != dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) obj->dma_buf = dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) get_dma_buf(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ret = drm_gem_handle_create_tail(file_priv, obj, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) drm_gem_object_put(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ret = drm_prime_add_buf_handle(&file_priv->prime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dma_buf, *handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) mutex_unlock(&file_priv->prime.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) dma_buf_put(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* hmm, if driver attached, we are relying on the free-object path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * to detach.. which seems ok..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) drm_gem_handle_delete(file_priv, *handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dma_buf_put(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) mutex_unlock(&dev->object_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) mutex_unlock(&file_priv->prime.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dma_buf_put(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct drm_prime_handle *args = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!dev->driver->prime_fd_to_handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return dev->driver->prime_fd_to_handle(dev, file_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) args->fd, &args->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static struct dma_buf *export_and_register_object(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct drm_gem_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* prevent races with concurrent gem_close. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (obj->handle_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) dmabuf = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (obj->funcs && obj->funcs->export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) dmabuf = obj->funcs->export(obj, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) else if (dev->driver->gem_prime_export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) dmabuf = dev->driver->gem_prime_export(obj, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) dmabuf = drm_gem_prime_export(obj, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (IS_ERR(dmabuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* normally the created dma-buf takes ownership of the ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * but if that fails then drop the ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * Note that callers do not need to clean up the export cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * since the check for obj->handle_count guarantees that someone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * will clean it up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) obj->dma_buf = dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) get_dma_buf(obj->dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * @dev: dev to export the buffer from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * @file_priv: drm file-private structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * @handle: buffer handle to export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @flags: flags like DRM_CLOEXEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @prime_fd: pointer to storage for the fd id of the create dma-buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * This is the PRIME export function which must be used mandatorily by GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * drivers to ensure correct lifetime management of the underlying GEM object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * The actual exporting from GEM object to a dma-buf is done through the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * &drm_driver.gem_prime_export driver callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int drm_gem_prime_handle_to_fd(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct drm_file *file_priv, uint32_t handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) uint32_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int *prime_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct drm_gem_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) mutex_lock(&file_priv->prime.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) obj = drm_gem_object_lookup(file_priv, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) if (!obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (dmabuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) get_dma_buf(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) goto out_have_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) mutex_lock(&dev->object_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /* re-export the original imported object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (obj->import_attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) dmabuf = obj->import_attach->dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) get_dma_buf(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) goto out_have_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (obj->dma_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) get_dma_buf(obj->dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) dmabuf = obj->dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) goto out_have_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dmabuf = export_and_register_object(dev, obj, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (IS_ERR(dmabuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* normally the created dma-buf takes ownership of the ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * but if that fails then drop the ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = PTR_ERR(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) mutex_unlock(&dev->object_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) out_have_obj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * If we've exported this buffer then cheat and add it to the import list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * so we get the correct handle back. We must do this under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * protection of dev->object_name_lock to ensure that a racing gem close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * ioctl doesn't miss to remove this buffer handle from the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) ret = drm_prime_add_buf_handle(&file_priv->prime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) dmabuf, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) mutex_unlock(&dev->object_name_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) goto fail_put_dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) out_have_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ret = dma_buf_fd(dmabuf, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * We must _not_ remove the buffer from the handle cache since the newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * created dma buf is already linked in the global obj->dma_buf pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) * and that is invariant as long as a userspace gem handle exists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * Closing the handle will clean out the cache anyway, so we don't leak.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) goto fail_put_dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) *prime_fd = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) fail_put_dmabuf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) dma_buf_put(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) drm_gem_object_put(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) mutex_unlock(&file_priv->prime.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct drm_file *file_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct drm_prime_handle *args = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) if (!dev->driver->prime_handle_to_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* check flags are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return dev->driver->prime_handle_to_fd(dev, file_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) args->handle, args->flags, &args->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * DOC: PRIME Helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * Drivers can implement &drm_gem_object_funcs.export and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * implement dma-buf support in terms of some lower-level helpers, which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * again exported for drivers to use individually:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * Exporting buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * Optional pinning of buffers is handled at dma-buf attach and detach time in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * &drm_gem_object_funcs.get_sg_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * For kernel-internal access there's drm_gem_dmabuf_vmap() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * drm_gem_dmabuf_mmap().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * Note that these export helpers can only be used if the underlying backing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * storage is fully coherent and either permanently pinned, or it is safe to pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * it indefinitely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * FIXME: The underlying helper functions are named rather inconsistently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * Exporting buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * ~~~~~~~~~~~~~~~~~
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * Importing dma-bufs using drm_gem_prime_import() relies on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * &drm_driver.gem_prime_import_sg_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Note that similarly to the export helpers this permanently pins the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * underlying backing storage. Which is ok for scanout, but is not the best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * option for sharing lots of buffers for rendering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * drm_gem_map_attach - dma_buf attach implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * @dma_buf: buffer to attach device to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * @attach: buffer attachment data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * used as the &dma_buf_ops.attach callback. Must be used together with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * drm_gem_map_detach().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * Returns 0 on success, negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) int drm_gem_map_attach(struct dma_buf *dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct dma_buf_attachment *attach)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return drm_gem_pin(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) EXPORT_SYMBOL(drm_gem_map_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * drm_gem_map_detach - dma_buf detach implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * @dma_buf: buffer to detach from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) * @attach: attachment to be detached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * &dma_buf_ops.detach callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) void drm_gem_map_detach(struct dma_buf *dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) struct dma_buf_attachment *attach)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) drm_gem_unpin(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) EXPORT_SYMBOL(drm_gem_map_detach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * @attach: attachment whose scatterlist is to be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * @dir: direction of DMA transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * with drm_gem_unmap_dma_buf().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * on error. May return -EINTR if it is interrupted by a signal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) enum dma_data_direction dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct drm_gem_object *obj = attach->dmabuf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct sg_table *sgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (WARN_ON(dir == DMA_NONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (obj->funcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) sgt = obj->funcs->get_sg_table(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ret = dma_map_sgtable(attach->dev, sgt, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) DMA_ATTR_SKIP_CPU_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) sg_free_table(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) kfree(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) sgt = ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) return sgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) EXPORT_SYMBOL(drm_gem_map_dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * @attach: attachment to unmap buffer from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * @sgt: scatterlist info of the buffer to unmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * @dir: direction of DMA transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) struct sg_table *sgt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) enum dma_data_direction dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (!sgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) dma_unmap_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) sg_free_table(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) kfree(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * @dma_buf: buffer to be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * Returns the kernel virtual address or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) void *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) vaddr = drm_gem_vmap(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (IS_ERR(vaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) vaddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * @dma_buf: buffer to be unmapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * @vaddr: the virtual address of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * Releases a kernel virtual mapping. This can be used as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) drm_gem_vunmap(obj, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * @obj: GEM object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * @vma: Virtual address range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * This function sets up a userspace mapping for PRIME exported buffers using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * called to set up the mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * Drivers can use this as their &drm_driver.gem_prime_mmap callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) struct drm_file *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct file *fil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /* Add the fake offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (obj->funcs && obj->funcs->mmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) ret = obj->funcs->mmap(obj, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) vma->vm_private_data = obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) drm_gem_object_get(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) fil = kzalloc(sizeof(*fil), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) if (!priv || !fil) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /* Used by drm_gem_mmap() to lookup the GEM object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) priv->minor = obj->dev->primary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) fil->private_data = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) ret = drm_vma_node_allow(&obj->vma_node, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) ret = obj->dev->driver->fops->mmap(fil, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) drm_vma_node_revoke(&obj->vma_node, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) kfree(fil);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) EXPORT_SYMBOL(drm_gem_prime_mmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * @dma_buf: buffer to be mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * @vma: virtual address range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * Provides memory mapping for the buffer. This can be used as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * &dma_buf_ops.mmap callback. It just forwards to &drm_driver.gem_prime_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * which should be set to drm_gem_prime_mmap().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * FIXME: There's really no point to this wrapper, drivers which need anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * else but drm_gem_prime_mmap can roll their own &dma_buf_ops.mmap callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) * Returns 0 on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) struct drm_device *dev = obj->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (!dev->driver->gem_prime_mmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return dev->driver->gem_prime_mmap(obj, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * drm_gem_dmabuf_get_uuid - dma_buf get_uuid implementation for GEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * @dma_buf: buffer to query
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) * @uuid: uuid outparam
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * Queries the buffer's virtio UUID. This can be used as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * &dma_buf_ops.get_uuid callback. Calls into &drm_driver.gem_prime_get_uuid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) * Returns 0 on success or a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int drm_gem_dmabuf_get_uuid(struct dma_buf *dma_buf, uuid_t *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) struct drm_gem_object *obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) struct drm_device *dev = obj->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (!dev->driver->gem_prime_get_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return dev->driver->gem_prime_get_uuid(obj, uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) EXPORT_SYMBOL(drm_gem_dmabuf_get_uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .cache_sgt_mapping = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .attach = drm_gem_map_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .detach = drm_gem_map_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) .map_dma_buf = drm_gem_map_dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) .unmap_dma_buf = drm_gem_unmap_dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) .release = drm_gem_dmabuf_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) .mmap = drm_gem_dmabuf_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) .vmap = drm_gem_dmabuf_vmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) .vunmap = drm_gem_dmabuf_vunmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) .get_uuid = drm_gem_dmabuf_get_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * drm_prime_pages_to_sg - converts a page array into an sg list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * @pages: pointer to the array of page pointers to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) * @nr_pages: length of the page vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * This helper creates an sg table object from a set of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * the driver is responsible for mapping the pages into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * importers address space for use with dma_buf itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) struct sg_table *drm_prime_pages_to_sg(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) struct page **pages, unsigned int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) struct sg_table *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) struct scatterlist *sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) size_t max_segment = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if (!sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) if (dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) max_segment = dma_max_mapping_size(dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (max_segment == 0 || max_segment > SCATTERLIST_MAX_SEGMENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) max_segment = SCATTERLIST_MAX_SEGMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) sge = __sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) nr_pages << PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) max_segment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) NULL, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (IS_ERR(sge)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) kfree(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) sg = ERR_CAST(sge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) EXPORT_SYMBOL(drm_prime_pages_to_sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * drm_prime_get_contiguous_size - returns the contiguous size of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * @sgt: sg_table describing the buffer to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * This helper calculates the contiguous size in the DMA address space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * of the the buffer described by the provided sg_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) * This is useful for implementing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) * &drm_gem_object_funcs.gem_prime_import_sg_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) dma_addr_t expected = sg_dma_address(sgt->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) unsigned long size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) for_each_sgtable_dma_sg(sgt, sg, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) unsigned int len = sg_dma_len(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (sg_dma_address(sg) != expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) expected += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) size += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) EXPORT_SYMBOL(drm_prime_get_contiguous_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) * drm_gem_prime_export - helper library implementation of the export callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) * @obj: GEM object to export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) * @flags: flags like DRM_CLOEXEC and DRM_RDWR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * using the PRIME helpers. It is used as the default in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) * drm_gem_prime_handle_to_fd().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct drm_device *dev = obj->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct dma_buf_export_info exp_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) .exp_name = KBUILD_MODNAME, /* white lie for debug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) .owner = dev->driver->fops->owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) .ops = &drm_gem_prime_dmabuf_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) .size = obj->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) .flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) .priv = obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) .resv = obj->resv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return drm_gem_dmabuf_export(dev, &exp_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) EXPORT_SYMBOL(drm_gem_prime_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * drm_gem_prime_import_dev - core implementation of the import callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) * @dev: drm_device to import into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * @dma_buf: dma-buf object to import
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * @attach_dev: struct device to dma_buf attach
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) * This is the core of drm_gem_prime_import(). It's designed to be called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) * drivers who want to use a different device structure than &drm_device.dev for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * attaching via dma_buf. This function calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * &drm_driver.gem_prime_import_sg_table internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) * Drivers must arrange to call drm_prime_gem_destroy() from their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) * &drm_gem_object_funcs.free hook when using this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) struct dma_buf *dma_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) struct device *attach_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) struct dma_buf_attachment *attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) struct sg_table *sgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) struct drm_gem_object *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) obj = dma_buf->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) if (obj->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) * Importing dmabuf exported from out own gem increases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * refcount on gem itself instead of f_count of dmabuf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) drm_gem_object_get(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (!dev->driver->gem_prime_import_sg_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) attach = dma_buf_attach(dma_buf, attach_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) if (IS_ERR(attach))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) return ERR_CAST(attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) get_dma_buf(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) if (IS_ERR(sgt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) ret = PTR_ERR(sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) goto fail_detach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) if (IS_ERR(obj)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) ret = PTR_ERR(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) goto fail_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) obj->import_attach = attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) obj->resv = dma_buf->resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) fail_unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) fail_detach:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) dma_buf_detach(dma_buf, attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) dma_buf_put(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) EXPORT_SYMBOL(drm_gem_prime_import_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) * drm_gem_prime_import - helper library implementation of the import callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) * @dev: drm_device to import into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) * @dma_buf: dma-buf object to import
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * This is the implementation of the gem_prime_import functions for GEM drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * using the PRIME helpers. Drivers can use this as their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) * &drm_driver.gem_prime_import implementation. It is used as the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) * implementation in drm_gem_prime_fd_to_handle().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * Drivers must arrange to call drm_prime_gem_destroy() from their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * &drm_gem_object_funcs.free hook when using this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct dma_buf *dma_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) EXPORT_SYMBOL(drm_gem_prime_import);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) * @sgt: scatter-gather table to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) * @pages: optional array of page pointers to store the page array in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * @addrs: optional array to store the dma bus address of each page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) * @max_entries: size of both the passed-in arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * Exports an sg table into an array of pages and addresses. This is currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) * required by the TTM driver in order to do correct fault handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) * Drivers can use this in their &drm_driver.gem_prime_import_sg_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) * implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) dma_addr_t *addrs, int max_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) struct sg_dma_page_iter dma_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) struct sg_page_iter page_iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) struct page **p = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) dma_addr_t *a = addrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) for_each_sgtable_page(sgt, &page_iter, 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (WARN_ON(p - pages >= max_entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) *p++ = sg_page_iter_page(&page_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (addrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) for_each_sgtable_dma_page(sgt, &dma_iter, 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (WARN_ON(a - addrs >= max_entries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) *a++ = sg_page_iter_dma_address(&dma_iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * @obj: GEM object which was created from a dma-buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * @sg: the sg-table which was pinned at import time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * This is the cleanup functions which GEM drivers need to call when they use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) struct dma_buf_attachment *attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) struct dma_buf *dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) attach = obj->import_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (sg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) dma_buf = attach->dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) dma_buf_detach(attach->dmabuf, attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /* remove the reference */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) dma_buf_put(dma_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) EXPORT_SYMBOL(drm_prime_gem_destroy);