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 "msm_drv.h"
8 #include "msm_gem.h"
9 
10 #include <linux/dma-buf.h>
11 
12 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
13 {
14 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
15 	int npages = obj->size >> PAGE_SHIFT;
16 
17 	if (WARN_ON(!msm_obj->pages))  /* should have already pinned! */
18 		return NULL;
19 
20 	return drm_prime_pages_to_sg(msm_obj->pages, npages);
21 }
22 
23 void *msm_gem_prime_vmap(struct drm_gem_object *obj)
24 {
25 	return msm_gem_get_vaddr(obj);
26 }
27 
28 void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
29 {
30 	msm_gem_put_vaddr(obj);
31 }
32 
33 int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
34 {
35 	int ret;
36 
37 	ret = drm_gem_mmap_obj(obj, obj->size, vma);
38 	if (ret < 0)
39 		return ret;
40 
41 	return msm_gem_mmap_obj(vma->vm_private_data, vma);
42 }
43 
44 struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
45 		struct dma_buf_attachment *attach, struct sg_table *sg)
46 {
47 	return msm_gem_import(dev, attach->dmabuf, sg);
48 }
49 
50 int msm_gem_prime_pin(struct drm_gem_object *obj)
51 {
52 	if (!obj->import_attach)
53 		msm_gem_get_pages(obj);
54 	return 0;
55 }
56 
57 void msm_gem_prime_unpin(struct drm_gem_object *obj)
58 {
59 	if (!obj->import_attach)
60 		msm_gem_put_pages(obj);
61 }
62