drm_prime.c 27.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright © 2012 Red Hat
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *      Dave Airlie <airlied@redhat.com>
 *      Rob Clark <rob.clark@linaro.org>
 *
 */

#include <linux/export.h>
#include <linux/dma-buf.h>
31
#include <linux/rbtree.h>
D
Daniel Vetter 已提交
32
#include <drm/drm_prime.h>
D
Daniel Vetter 已提交
33
#include <drm/drm_gem.h>
D
Daniel Vetter 已提交
34
#include <drm/drmP.h>
D
Daniel Vetter 已提交
35

36
#include "drm_internal.h"
37 38 39 40 41 42 43

/*
 * DMA-BUF/GEM Object references and lifetime overview:
 *
 * On the export the dma_buf holds a reference to the exporting GEM
 * object. It takes this reference in handle_to_fd_ioctl, when it
 * first calls .prime_export and stores the exporting GEM object in
44 45 46 47 48
 * the dma_buf priv. This reference needs to be released when the
 * final reference to the &dma_buf itself is dropped and its
 * &dma_buf_ops.release function is called. For GEM-based drivers,
 * the dma_buf should be exported using drm_gem_dmabuf_export() and
 * then released by drm_gem_dmabuf_release().
49 50 51 52 53 54 55 56 57
 *
 * On the import the importing GEM object holds a reference to the
 * dma_buf (which in turn holds a ref to the exporting GEM object).
 * It takes that reference in the fd_to_handle ioctl.
 * It calls dma_buf_get, creates an attachment to it and stores the
 * attachment in the GEM object. When this attachment is destroyed
 * when the imported object is destroyed, we remove the attachment
 * and drop the reference to the dma_buf.
 *
58 59 60 61 62 63 64 65 66 67
 * When all the references to the &dma_buf are dropped, i.e. when
 * userspace has closed both handles to the imported GEM object (through the
 * FD_TO_HANDLE IOCTL) and closed the file descriptor of the exported
 * (through the HANDLE_TO_FD IOCTL) dma_buf, and all kernel-internal references
 * are also gone, then the dma_buf gets destroyed.  This can also happen as a
 * part of the clean up procedure in the drm_release() function if userspace
 * fails to properly clean up.  Note that both the kernel and userspace (by
 * keeeping the PRIME file descriptors open) can hold references onto a
 * &dma_buf.
 *
68 69 70 71 72 73
 * Thus the chain of references always flows in one direction
 * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
 *
 * Self-importing: if userspace is using PRIME as a replacement for flink
 * then it will get a fd->handle request for a GEM object that it created.
 * Drivers should detect this situation and return back the gem object
A
Aaron Plattner 已提交
74 75
 * from the dma-buf private.  Prime will do this automatically for drivers that
 * use the drm_gem_prime_{import,export} helpers.
76 77 78
 *
 * GEM struct &dma_buf_ops symbols are now exported. They can be resued by
 * drivers which implement GEM interface.
79 80 81 82 83
 */

struct drm_prime_member {
	struct dma_buf *dma_buf;
	uint32_t handle;
84 85 86

	struct rb_node dmabuf_rb;
	struct rb_node handle_rb;
87
};
88 89 90 91 92 93

struct drm_prime_attachment {
	struct sg_table *sgt;
	enum dma_data_direction dir;
};

94 95
static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
				    struct dma_buf *dma_buf, uint32_t handle)
96 97
{
	struct drm_prime_member *member;
98
	struct rb_node **p, *rb;
99 100 101 102 103 104 105 106

	member = kmalloc(sizeof(*member), GFP_KERNEL);
	if (!member)
		return -ENOMEM;

	get_dma_buf(dma_buf);
	member->dma_buf = dma_buf;
	member->handle = handle;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	rb = NULL;
	p = &prime_fpriv->dmabufs.rb_node;
	while (*p) {
		struct drm_prime_member *pos;

		rb = *p;
		pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
		if (dma_buf > pos->dma_buf)
			p = &rb->rb_right;
		else
			p = &rb->rb_left;
	}
	rb_link_node(&member->dmabuf_rb, rb, p);
	rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);

	rb = NULL;
	p = &prime_fpriv->handles.rb_node;
	while (*p) {
		struct drm_prime_member *pos;

		rb = *p;
		pos = rb_entry(rb, struct drm_prime_member, handle_rb);
		if (handle > pos->handle)
			p = &rb->rb_right;
		else
			p = &rb->rb_left;
	}
	rb_link_node(&member->handle_rb, rb, p);
	rb_insert_color(&member->handle_rb, &prime_fpriv->handles);

138 139
	return 0;
}
140

141 142 143
static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
						      uint32_t handle)
{
144 145 146 147 148
	struct rb_node *rb;

	rb = prime_fpriv->handles.rb_node;
	while (rb) {
		struct drm_prime_member *member;
149

150
		member = rb_entry(rb, struct drm_prime_member, handle_rb);
151 152
		if (member->handle == handle)
			return member->dma_buf;
153 154 155 156
		else if (member->handle < handle)
			rb = rb->rb_right;
		else
			rb = rb->rb_left;
157 158 159 160 161
	}

	return NULL;
}

162 163 164 165
static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
				       struct dma_buf *dma_buf,
				       uint32_t *handle)
{
166 167 168 169 170
	struct rb_node *rb;

	rb = prime_fpriv->dmabufs.rb_node;
	while (rb) {
		struct drm_prime_member *member;
171

172
		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
173 174 175
		if (member->dma_buf == dma_buf) {
			*handle = member->handle;
			return 0;
176 177 178 179
		} else if (member->dma_buf < dma_buf) {
			rb = rb->rb_right;
		} else {
			rb = rb->rb_left;
180 181
		}
	}
182

183 184 185
	return -ENOENT;
}

186 187 188 189 190 191 192 193 194 195 196
/**
 * drm_gem_map_attach - dma_buf attach implementation for GEM
 * @dma_buf: buffer to attach device to
 * @attach: buffer attachment data
 *
 * Allocates &drm_prime_attachment and calls &drm_driver.gem_prime_pin for
 * device specific attachment. This can be used as the &dma_buf_ops.attach
 * callback.
 *
 * Returns 0 on success, negative error code on failure.
 */
197
int drm_gem_map_attach(struct dma_buf *dma_buf,
198
		       struct dma_buf_attachment *attach)
199
{
200
	struct drm_prime_attachment *prime_attach;
201 202 203
	struct drm_gem_object *obj = dma_buf->priv;
	struct drm_device *dev = obj->dev;

204 205 206 207 208 209 210
	prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
	if (!prime_attach)
		return -ENOMEM;

	prime_attach->dir = DMA_NONE;
	attach->priv = prime_attach;

211 212 213 214 215
	if (!dev->driver->gem_prime_pin)
		return 0;

	return dev->driver->gem_prime_pin(obj);
}
216
EXPORT_SYMBOL(drm_gem_map_attach);
217

218 219 220 221 222 223 224 225
/**
 * drm_gem_map_detach - dma_buf detach implementation for GEM
 * @dma_buf: buffer to detach from
 * @attach: attachment to be detached
 *
 * Cleans up &dma_buf_attachment. This can be used as the &dma_buf_ops.detach
 * callback.
 */
226 227
void drm_gem_map_detach(struct dma_buf *dma_buf,
			struct dma_buf_attachment *attach)
228
{
229
	struct drm_prime_attachment *prime_attach = attach->priv;
230 231 232
	struct drm_gem_object *obj = dma_buf->priv;
	struct drm_device *dev = obj->dev;

233 234
	if (prime_attach) {
		struct sg_table *sgt = prime_attach->sgt;
235

236 237 238 239 240 241 242 243 244 245 246 247
		if (sgt) {
			if (prime_attach->dir != DMA_NONE)
				dma_unmap_sg_attrs(attach->dev, sgt->sgl,
						   sgt->nents,
						   prime_attach->dir,
						   DMA_ATTR_SKIP_CPU_SYNC);
			sg_free_table(sgt);
		}

		kfree(sgt);
		kfree(prime_attach);
		attach->priv = NULL;
248
	}
249

250 251
	if (dev->driver->gem_prime_unpin)
		dev->driver->gem_prime_unpin(obj);
252
}
253
EXPORT_SYMBOL(drm_gem_map_detach);
254

255 256
void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
					struct dma_buf *dma_buf)
257
{
258
	struct rb_node *rb;
259

260 261 262 263 264
	rb = prime_fpriv->dmabufs.rb_node;
	while (rb) {
		struct drm_prime_member *member;

		member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
265
		if (member->dma_buf == dma_buf) {
266 267 268
			rb_erase(&member->handle_rb, &prime_fpriv->handles);
			rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);

269 270
			dma_buf_put(dma_buf);
			kfree(member);
271 272 273 274 275
			return;
		} else if (member->dma_buf < dma_buf) {
			rb = rb->rb_right;
		} else {
			rb = rb->rb_left;
276 277 278 279
		}
	}
}

280 281 282 283 284 285 286 287 288 289 290 291
/**
 * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
 * @attach: attachment whose scatterlist is to be returned
 * @dir: direction of DMA transfer
 *
 * Calls &drm_driver.gem_prime_get_sg_table and then maps the scatterlist. This
 * can be used as the &dma_buf_ops.map_dma_buf callback.
 *
 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
 * on error. May return -EINTR if it is interrupted by a signal.
 */

292 293
struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
				     enum dma_data_direction dir)
A
Aaron Plattner 已提交
294
{
295
	struct drm_prime_attachment *prime_attach = attach->priv;
A
Aaron Plattner 已提交
296 297 298
	struct drm_gem_object *obj = attach->dmabuf->priv;
	struct sg_table *sgt;

299 300 301 302 303 304 305 306 307 308 309 310 311 312
	if (WARN_ON(dir == DMA_NONE || !prime_attach))
		return ERR_PTR(-EINVAL);

	/* return the cached mapping when possible */
	if (prime_attach->dir == dir)
		return prime_attach->sgt;

	/*
	 * two mappings with different directions for the same attachment are
	 * not allowed
	 */
	if (WARN_ON(prime_attach->dir != DMA_NONE))
		return ERR_PTR(-EBUSY);

A
Aaron Plattner 已提交
313 314
	sgt = obj->dev->driver->gem_prime_get_sg_table(obj);

315
	if (!IS_ERR(sgt)) {
316 317
		if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
				      DMA_ATTR_SKIP_CPU_SYNC)) {
318 319 320
			sg_free_table(sgt);
			kfree(sgt);
			sgt = ERR_PTR(-ENOMEM);
321 322 323
		} else {
			prime_attach->sgt = sgt;
			prime_attach->dir = dir;
324 325
		}
	}
A
Aaron Plattner 已提交
326 327 328

	return sgt;
}
329
EXPORT_SYMBOL(drm_gem_map_dma_buf);
A
Aaron Plattner 已提交
330

331 332
/**
 * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
333 334 335
 * @attach: attachment to unmap buffer from
 * @sgt: scatterlist info of the buffer to unmap
 * @dir: direction of DMA transfer
336 337 338 339
 *
 * Not implemented. The unmap is done at drm_gem_map_detach().  This can be
 * used as the &dma_buf_ops.unmap_dma_buf callback.
 */
340 341 342
void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
			   struct sg_table *sgt,
			   enum dma_data_direction dir)
A
Aaron Plattner 已提交
343
{
344
	/* nothing to be done here */
A
Aaron Plattner 已提交
345
}
346
EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
A
Aaron Plattner 已提交
347

348 349
/**
 * drm_gem_dmabuf_export - dma_buf export implementation for GEM
350 351
 * @dev: parent device for the exported dmabuf
 * @exp_info: the export information used by dma_buf_export()
352 353 354
 *
 * This wraps dma_buf_export() for use by generic GEM drivers that are using
 * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
355
 * a reference to the &drm_device and the exported &drm_gem_object (stored in
356
 * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
357 358 359 360 361 362 363 364 365
 *
 * Returns the new dmabuf.
 */
struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
				      struct dma_buf_export_info *exp_info)
{
	struct dma_buf *dma_buf;

	dma_buf = dma_buf_export(exp_info);
366 367 368
	if (IS_ERR(dma_buf))
		return dma_buf;

369
	drm_dev_get(dev);
370
	drm_gem_object_get(exp_info->priv);
371 372 373 374 375

	return dma_buf;
}
EXPORT_SYMBOL(drm_gem_dmabuf_export);

376 377 378 379 380 381
/**
 * drm_gem_dmabuf_release - dma_buf release implementation for GEM
 * @dma_buf: buffer to be released
 *
 * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
 * must use this in their dma_buf ops structure as the release callback.
382 383
 * drm_gem_dmabuf_release() should be used in conjunction with
 * drm_gem_dmabuf_export().
384
 */
385
void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
A
Aaron Plattner 已提交
386 387
{
	struct drm_gem_object *obj = dma_buf->priv;
388
	struct drm_device *dev = obj->dev;
A
Aaron Plattner 已提交
389

390
	/* drop the reference on the export fd holds */
391
	drm_gem_object_put_unlocked(obj);
392

393
	drm_dev_put(dev);
A
Aaron Plattner 已提交
394
}
395
EXPORT_SYMBOL(drm_gem_dmabuf_release);
A
Aaron Plattner 已提交
396

397 398 399 400 401 402 403 404 405
/**
 * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
 * @dma_buf: buffer to be mapped
 *
 * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
 * callback.
 *
 * Returns the kernel virtual address.
 */
406
void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
A
Aaron Plattner 已提交
407 408 409 410
{
	struct drm_gem_object *obj = dma_buf->priv;
	struct drm_device *dev = obj->dev;

411 412 413 414
	if (dev->driver->gem_prime_vmap)
		return dev->driver->gem_prime_vmap(obj);
	else
		return NULL;
A
Aaron Plattner 已提交
415
}
416
EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
A
Aaron Plattner 已提交
417

418 419 420 421 422 423 424 425
/**
 * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
 * @dma_buf: buffer to be unmapped
 * @vaddr: the virtual address of the buffer
 *
 * Releases a kernel virtual mapping. This can be used as the
 * &dma_buf_ops.vunmap callback.
 */
426
void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
A
Aaron Plattner 已提交
427 428 429 430
{
	struct drm_gem_object *obj = dma_buf->priv;
	struct drm_device *dev = obj->dev;

431 432
	if (dev->driver->gem_prime_vunmap)
		dev->driver->gem_prime_vunmap(obj, vaddr);
A
Aaron Plattner 已提交
433
}
434
EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
A
Aaron Plattner 已提交
435

436 437
/**
 * drm_gem_dmabuf_kmap_atomic - map_atomic implementation for GEM
438 439
 * @dma_buf: buffer to be mapped
 * @page_num: page number within the buffer
440 441 442
 *
 * Not implemented. This can be used as the &dma_buf_ops.map_atomic callback.
 */
443 444
void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
				 unsigned long page_num)
A
Aaron Plattner 已提交
445 446 447
{
	return NULL;
}
448
EXPORT_SYMBOL(drm_gem_dmabuf_kmap_atomic);
A
Aaron Plattner 已提交
449

450 451
/**
 * drm_gem_dmabuf_kunmap_atomic - unmap_atomic implementation for GEM
452 453 454
 * @dma_buf: buffer to be unmapped
 * @page_num: page number within the buffer
 * @addr: virtual address of the buffer
455 456 457
 *
 * Not implemented. This can be used as the &dma_buf_ops.unmap_atomic callback.
 */
458 459
void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
				  unsigned long page_num, void *addr)
A
Aaron Plattner 已提交
460 461 462
{

}
463 464
EXPORT_SYMBOL(drm_gem_dmabuf_kunmap_atomic);

465 466
/**
 * drm_gem_dmabuf_kmap - map implementation for GEM
467 468
 * @dma_buf: buffer to be mapped
 * @page_num: page number within the buffer
469 470 471
 *
 * Not implemented. This can be used as the &dma_buf_ops.map callback.
 */
472
void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
A
Aaron Plattner 已提交
473 474 475
{
	return NULL;
}
476
EXPORT_SYMBOL(drm_gem_dmabuf_kmap);
A
Aaron Plattner 已提交
477

478 479
/**
 * drm_gem_dmabuf_kunmap - unmap implementation for GEM
480 481 482
 * @dma_buf: buffer to be unmapped
 * @page_num: page number within the buffer
 * @addr: virtual address of the buffer
483 484 485
 *
 * Not implemented. This can be used as the &dma_buf_ops.unmap callback.
 */
486 487
void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num,
			   void *addr)
A
Aaron Plattner 已提交
488 489 490
{

}
491
EXPORT_SYMBOL(drm_gem_dmabuf_kunmap);
A
Aaron Plattner 已提交
492

493 494 495 496 497 498 499 500 501 502
/**
 * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
 * @dma_buf: buffer to be mapped
 * @vma: virtual address range
 *
 * Provides memory mapping for the buffer. This can be used as the
 * &dma_buf_ops.mmap callback.
 *
 * Returns 0 on success or a negative error code on failure.
 */
503
int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
A
Aaron Plattner 已提交
504
{
505 506 507 508 509 510 511
	struct drm_gem_object *obj = dma_buf->priv;
	struct drm_device *dev = obj->dev;

	if (!dev->driver->gem_prime_mmap)
		return -ENOSYS;

	return dev->driver->gem_prime_mmap(obj, vma);
A
Aaron Plattner 已提交
512
}
513
EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
A
Aaron Plattner 已提交
514 515

static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  {
516 517
	.attach = drm_gem_map_attach,
	.detach = drm_gem_map_detach,
A
Aaron Plattner 已提交
518 519 520
	.map_dma_buf = drm_gem_map_dma_buf,
	.unmap_dma_buf = drm_gem_unmap_dma_buf,
	.release = drm_gem_dmabuf_release,
521 522 523 524
	.map = drm_gem_dmabuf_kmap,
	.map_atomic = drm_gem_dmabuf_kmap_atomic,
	.unmap = drm_gem_dmabuf_kunmap,
	.unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
A
Aaron Plattner 已提交
525 526 527 528 529 530 531 532 533 534 535
	.mmap = drm_gem_dmabuf_mmap,
	.vmap = drm_gem_dmabuf_vmap,
	.vunmap = drm_gem_dmabuf_vunmap,
};

/**
 * DOC: PRIME Helpers
 *
 * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
 * simpler APIs by using the helper functions @drm_gem_prime_export and
 * @drm_gem_prime_import.  These functions implement dma-buf support in terms of
536
 * six lower-level driver callbacks:
A
Aaron Plattner 已提交
537 538 539
 *
 * Export callbacks:
 *
540 541 542 543 544
 *  * @gem_prime_pin (optional): prepare a GEM object for exporting
 *  * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
 *  * @gem_prime_vmap: vmap a buffer exported by your driver
 *  * @gem_prime_vunmap: vunmap a buffer exported by your driver
 *  * @gem_prime_mmap (optional): mmap a buffer exported by your driver
545
 *
A
Aaron Plattner 已提交
546 547
 * Import callback:
 *
548
 *  * @gem_prime_import_sg_table (import): produce a GEM object from another
A
Aaron Plattner 已提交
549 550 551
 *    driver's scatter/gather table
 */

552
/**
553
 * drm_gem_prime_export - helper library implementation of the export callback
554 555
 * @dev: drm_device to export from
 * @obj: GEM object to export
556
 * @flags: flags like DRM_CLOEXEC and DRM_RDWR
557 558 559 560
 *
 * This is the implementation of the gem_prime_export functions for GEM drivers
 * using the PRIME helpers.
 */
A
Aaron Plattner 已提交
561
struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
562 563
				     struct drm_gem_object *obj,
				     int flags)
A
Aaron Plattner 已提交
564
{
565 566 567 568 569 570 571 572
	struct dma_buf_export_info exp_info = {
		.exp_name = KBUILD_MODNAME, /* white lie for debug */
		.owner = dev->driver->fops->owner,
		.ops = &drm_gem_prime_dmabuf_ops,
		.size = obj->size,
		.flags = flags,
		.priv = obj,
	};
573 574

	if (dev->driver->gem_prime_res_obj)
575
		exp_info.resv = dev->driver->gem_prime_res_obj(obj);
576

577
	return drm_gem_dmabuf_export(dev, &exp_info);
A
Aaron Plattner 已提交
578 579 580
}
EXPORT_SYMBOL(drm_gem_prime_export);

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
static struct dma_buf *export_and_register_object(struct drm_device *dev,
						  struct drm_gem_object *obj,
						  uint32_t flags)
{
	struct dma_buf *dmabuf;

	/* prevent races with concurrent gem_close. */
	if (obj->handle_count == 0) {
		dmabuf = ERR_PTR(-ENOENT);
		return dmabuf;
	}

	dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
	if (IS_ERR(dmabuf)) {
		/* normally the created dma-buf takes ownership of the ref,
		 * but if that fails then drop the ref
		 */
		return dmabuf;
	}

	/*
	 * Note that callers do not need to clean up the export cache
	 * since the check for obj->handle_count guarantees that someone
	 * will clean it up.
	 */
	obj->dma_buf = dmabuf;
	get_dma_buf(obj->dma_buf);

	return dmabuf;
}

612 613 614 615 616 617 618 619 620 621 622 623 624
/**
 * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
 * @dev: dev to export the buffer from
 * @file_priv: drm file-private structure
 * @handle: buffer handle to export
 * @flags: flags like DRM_CLOEXEC
 * @prime_fd: pointer to storage for the fd id of the create dma-buf
 *
 * This is the PRIME export function which must be used mandatorily by GEM
 * drivers to ensure correct lifetime management of the underlying GEM object.
 * The actual exporting from GEM object to a dma-buf is done through the
 * gem_prime_export driver callback.
 */
625
int drm_gem_prime_handle_to_fd(struct drm_device *dev,
626 627 628
			       struct drm_file *file_priv, uint32_t handle,
			       uint32_t flags,
			       int *prime_fd)
629 630
{
	struct drm_gem_object *obj;
631 632
	int ret = 0;
	struct dma_buf *dmabuf;
633

634
	mutex_lock(&file_priv->prime.lock);
635
	obj = drm_gem_object_lookup(file_priv, handle);
636 637 638 639 640 641 642 643 644 645
	if (!obj)  {
		ret = -ENOENT;
		goto out_unlock;
	}

	dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
	if (dmabuf) {
		get_dma_buf(dmabuf);
		goto out_have_handle;
	}
646

647
	mutex_lock(&dev->object_name_lock);
648 649
	/* re-export the original imported object */
	if (obj->import_attach) {
650
		dmabuf = obj->import_attach->dmabuf;
651
		get_dma_buf(dmabuf);
652
		goto out_have_obj;
653 654
	}

655 656 657
	if (obj->dma_buf) {
		get_dma_buf(obj->dma_buf);
		dmabuf = obj->dma_buf;
658
		goto out_have_obj;
659
	}
660

661
	dmabuf = export_and_register_object(dev, obj, flags);
662
	if (IS_ERR(dmabuf)) {
663 664 665
		/* normally the created dma-buf takes ownership of the ref,
		 * but if that fails then drop the ref
		 */
666
		ret = PTR_ERR(dmabuf);
667
		mutex_unlock(&dev->object_name_lock);
668 669 670
		goto out;
	}

671 672 673 674 675 676
out_have_obj:
	/*
	 * If we've exported this buffer then cheat and add it to the import list
	 * so we get the correct handle back. We must do this under the
	 * protection of dev->object_name_lock to ensure that a racing gem close
	 * ioctl doesn't miss to remove this buffer handle from the cache.
677
	 */
678
	ret = drm_prime_add_buf_handle(&file_priv->prime,
679
				       dmabuf, handle);
680
	mutex_unlock(&dev->object_name_lock);
681
	if (ret)
682
		goto fail_put_dmabuf;
683

684
out_have_handle:
685
	ret = dma_buf_fd(dmabuf, flags);
686 687 688 689 690 691
	/*
	 * We must _not_ remove the buffer from the handle cache since the newly
	 * created dma buf is already linked in the global obj->dma_buf pointer,
	 * and that is invariant as long as a userspace gem handle exists.
	 * Closing the handle will clean out the cache anyway, so we don't leak.
	 */
692
	if (ret < 0) {
693
		goto fail_put_dmabuf;
694
	} else {
695
		*prime_fd = ret;
696 697 698
		ret = 0;
	}

699 700 701
	goto out;

fail_put_dmabuf:
702
	dma_buf_put(dmabuf);
703
out:
704
	drm_gem_object_put_unlocked(obj);
705 706 707
out_unlock:
	mutex_unlock(&file_priv->prime.lock);

708
	return ret;
709 710 711
}
EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);

712
/**
713
 * drm_gem_prime_import_dev - core implementation of the import callback
714 715
 * @dev: drm_device to import into
 * @dma_buf: dma-buf object to import
716
 * @attach_dev: struct device to dma_buf attach
717
 *
718 719 720
 * This is the core of drm_gem_prime_import. It's designed to be called by
 * drivers who want to use a different device structure than dev->dev for
 * attaching via dma_buf.
721
 */
722 723 724
struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
					    struct dma_buf *dma_buf,
					    struct device *attach_dev)
A
Aaron Plattner 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737
{
	struct dma_buf_attachment *attach;
	struct sg_table *sgt;
	struct drm_gem_object *obj;
	int ret;

	if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
		obj = dma_buf->priv;
		if (obj->dev == dev) {
			/*
			 * Importing dmabuf exported from out own gem increases
			 * refcount on gem itself instead of f_count of dmabuf.
			 */
738
			drm_gem_object_get(obj);
A
Aaron Plattner 已提交
739 740 741 742
			return obj;
		}
	}

743 744 745
	if (!dev->driver->gem_prime_import_sg_table)
		return ERR_PTR(-EINVAL);

746
	attach = dma_buf_attach(dma_buf, attach_dev);
A
Aaron Plattner 已提交
747
	if (IS_ERR(attach))
748
		return ERR_CAST(attach);
A
Aaron Plattner 已提交
749

750 751
	get_dma_buf(dma_buf);

A
Aaron Plattner 已提交
752
	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
753
	if (IS_ERR(sgt)) {
A
Aaron Plattner 已提交
754 755 756 757
		ret = PTR_ERR(sgt);
		goto fail_detach;
	}

758
	obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
A
Aaron Plattner 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771
	if (IS_ERR(obj)) {
		ret = PTR_ERR(obj);
		goto fail_unmap;
	}

	obj->import_attach = attach;

	return obj;

fail_unmap:
	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
fail_detach:
	dma_buf_detach(dma_buf, attach);
772 773
	dma_buf_put(dma_buf);

A
Aaron Plattner 已提交
774 775
	return ERR_PTR(ret);
}
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
EXPORT_SYMBOL(drm_gem_prime_import_dev);

/**
 * drm_gem_prime_import - helper library implementation of the import callback
 * @dev: drm_device to import into
 * @dma_buf: dma-buf object to import
 *
 * This is the implementation of the gem_prime_import functions for GEM drivers
 * using the PRIME helpers.
 */
struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
					    struct dma_buf *dma_buf)
{
	return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
}
A
Aaron Plattner 已提交
791 792
EXPORT_SYMBOL(drm_gem_prime_import);

793 794 795 796 797 798 799 800 801 802 803 804
/**
 * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
 * @dev: dev to export the buffer from
 * @file_priv: drm file-private structure
 * @prime_fd: fd id of the dma-buf which should be imported
 * @handle: pointer to storage for the handle of the imported buffer object
 *
 * This is the PRIME import function which must be used mandatorily by GEM
 * drivers to ensure correct lifetime management of the underlying GEM object.
 * The actual importing of GEM object from the dma-buf is done through the
 * gem_import_export driver callback.
 */
805
int drm_gem_prime_fd_to_handle(struct drm_device *dev,
806 807
			       struct drm_file *file_priv, int prime_fd,
			       uint32_t *handle)
808 809 810 811 812 813 814 815 816 817 818
{
	struct dma_buf *dma_buf;
	struct drm_gem_object *obj;
	int ret;

	dma_buf = dma_buf_get(prime_fd);
	if (IS_ERR(dma_buf))
		return PTR_ERR(dma_buf);

	mutex_lock(&file_priv->prime.lock);

819
	ret = drm_prime_lookup_buf_handle(&file_priv->prime,
820
			dma_buf, handle);
821
	if (ret == 0)
822 823 824
		goto out_put;

	/* never seen this one, need to import */
825
	mutex_lock(&dev->object_name_lock);
826 827 828
	obj = dev->driver->gem_prime_import(dev, dma_buf);
	if (IS_ERR(obj)) {
		ret = PTR_ERR(obj);
829 830 831 832 833 834 835 836
		goto out_unlock;
	}

	if (obj->dma_buf) {
		WARN_ON(obj->dma_buf != dma_buf);
	} else {
		obj->dma_buf = dma_buf;
		get_dma_buf(dma_buf);
837 838
	}

839
	/* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
840
	ret = drm_gem_handle_create_tail(file_priv, obj, handle);
841
	drm_gem_object_put_unlocked(obj);
842 843 844
	if (ret)
		goto out_put;

845
	ret = drm_prime_add_buf_handle(&file_priv->prime,
846
			dma_buf, *handle);
847
	mutex_unlock(&file_priv->prime.lock);
848 849 850
	if (ret)
		goto fail;

851 852
	dma_buf_put(dma_buf);

853 854 855 856 857 858
	return 0;

fail:
	/* hmm, if driver attached, we are relying on the free-object path
	 * to detach.. which seems ok..
	 */
859
	drm_gem_handle_delete(file_priv, *handle);
860 861 862
	dma_buf_put(dma_buf);
	return ret;

863
out_unlock:
D
Dan Carpenter 已提交
864
	mutex_unlock(&dev->object_name_lock);
865 866
out_put:
	mutex_unlock(&file_priv->prime.lock);
867
	dma_buf_put(dma_buf);
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	return ret;
}
EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);

int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
				 struct drm_file *file_priv)
{
	struct drm_prime_handle *args = data;

	if (!drm_core_check_feature(dev, DRIVER_PRIME))
		return -EINVAL;

	if (!dev->driver->prime_handle_to_fd)
		return -ENOSYS;

	/* check flags are valid */
884
	if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
885 886 887
		return -EINVAL;

	return dev->driver->prime_handle_to_fd(dev, file_priv,
888
			args->handle, args->flags, &args->fd);
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
}

int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
				 struct drm_file *file_priv)
{
	struct drm_prime_handle *args = data;

	if (!drm_core_check_feature(dev, DRIVER_PRIME))
		return -EINVAL;

	if (!dev->driver->prime_fd_to_handle)
		return -ENOSYS;

	return dev->driver->prime_fd_to_handle(dev, file_priv,
			args->fd, &args->handle);
}

906 907 908 909
/**
 * drm_prime_pages_to_sg - converts a page array into an sg list
 * @pages: pointer to the array of page pointers to convert
 * @nr_pages: length of the page vector
910
 *
911
 * This helper creates an sg table object from a set of pages
912
 * the driver is responsible for mapping the pages into the
913
 * importers address space for use with dma_buf itself.
914
 */
915
struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
916 917 918 919 920
{
	struct sg_table *sg = NULL;
	int ret;

	sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
921 922
	if (!sg) {
		ret = -ENOMEM;
923
		goto out;
924
	}
925

926 927
	ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
				nr_pages << PAGE_SHIFT, GFP_KERNEL);
928 929 930 931 932 933
	if (ret)
		goto out;

	return sg;
out:
	kfree(sg);
934
	return ERR_PTR(ret);
935 936 937
}
EXPORT_SYMBOL(drm_prime_pages_to_sg);

938 939 940
/**
 * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
 * @sgt: scatter-gather table to convert
941
 * @pages: optional array of page pointers to store the page array in
942
 * @addrs: optional array to store the dma bus address of each page
943
 * @max_entries: size of both the passed-in arrays
944 945 946 947
 *
 * Exports an sg table into an array of pages and addresses. This is currently
 * required by the TTM driver in order to do correct fault handling.
 */
948
int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
949
				     dma_addr_t *addrs, int max_entries)
950 951 952 953
{
	unsigned count;
	struct scatterlist *sg;
	struct page *page;
954
	u32 len, index;
955 956
	dma_addr_t addr;

957
	index = 0;
958 959 960 961 962 963
	for_each_sg(sgt->sgl, sg, sgt->nents, count) {
		len = sg->length;
		page = sg_page(sg);
		addr = sg_dma_address(sg);

		while (len > 0) {
964
			if (WARN_ON(index >= max_entries))
965
				return -1;
966 967
			if (pages)
				pages[index] = page;
968
			if (addrs)
969
				addrs[index] = addr;
970 971 972 973

			page++;
			addr += PAGE_SIZE;
			len -= PAGE_SIZE;
974
			index++;
975 976 977 978 979
		}
	}
	return 0;
}
EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
980 981 982 983 984 985 986 987 988

/**
 * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
 * @obj: GEM object which was created from a dma-buf
 * @sg: the sg-table which was pinned at import time
 *
 * This is the cleanup functions which GEM drivers need to call when they use
 * @drm_gem_prime_import to import dma-bufs.
 */
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
{
	struct dma_buf_attachment *attach;
	struct dma_buf *dma_buf;
	attach = obj->import_attach;
	if (sg)
		dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
	dma_buf = attach->dmabuf;
	dma_buf_detach(attach->dmabuf, attach);
	/* remove the reference */
	dma_buf_put(dma_buf);
}
EXPORT_SYMBOL(drm_prime_gem_destroy);

void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
{
	mutex_init(&prime_fpriv->lock);
1006 1007
	prime_fpriv->dmabufs = RB_ROOT;
	prime_fpriv->handles = RB_ROOT;
1008 1009 1010 1011
}

void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
{
1012
	/* by now drm_gem_release should've made sure the list is empty */
1013
	WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
1014
}