1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #include <linux/dma-buf.h>
8 
9 #include <drm/drm_prime.h>
10 
11 #include "msm_drv.h"
12 #include "msm_gem.h"
13 
14 int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
15 {
16 	int ret;
17 
18 	/* Ensure the mmap offset is initialized.  We lazily initialize it,
19 	 * so if it has not been first mmap'd directly as a GEM object, the
20 	 * mmap offset will not be already initialized.
21 	 */
22 	ret = drm_gem_create_mmap_offset(obj);
23 	if (ret)
24 		return ret;
25 
26 	return drm_gem_prime_mmap(obj, vma);
27 }
28 
29 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
30 {
31 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
32 	int npages = obj->size >> PAGE_SHIFT;
33 
34 	if (WARN_ON(!msm_obj->pages))  /* should have already pinned! */
35 		return ERR_PTR(-ENOMEM);
36 
37 	return drm_prime_pages_to_sg(obj->dev, msm_obj->pages, npages);
38 }
39 
40 int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
41 {
42 	void *vaddr;
43 
44 	vaddr = msm_gem_get_vaddr(obj);
45 	if (IS_ERR(vaddr))
46 		return PTR_ERR(vaddr);
47 	iosys_map_set_vaddr(map, vaddr);
48 
49 	return 0;
50 }
51 
52 void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
53 {
54 	msm_gem_put_vaddr(obj);
55 }
56 
57 struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
58 		struct dma_buf_attachment *attach, struct sg_table *sg)
59 {
60 	return msm_gem_import(dev, attach->dmabuf, sg);
61 }
62 
63 int msm_gem_prime_pin(struct drm_gem_object *obj)
64 {
65 	if (!obj->import_attach)
66 		msm_gem_get_pages(obj);
67 	return 0;
68 }
69 
70 void msm_gem_prime_unpin(struct drm_gem_object *obj)
71 {
72 	if (!obj->import_attach)
73 		msm_gem_put_pages(obj);
74 }
75