xref: /linux/drivers/gpu/drm/msm/msm_gem_prime.c (revision db5d28c0bfe566908719bec8e25443aabecbb802)
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 
msm_gem_prime_get_sg_table(struct drm_gem_object * obj)14 struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
15 {
16 	struct msm_gem_object *msm_obj = to_msm_bo(obj);
17 	int npages = obj->size >> PAGE_SHIFT;
18 
19 	if (WARN_ON(!msm_obj->pages))  /* should have already pinned! */
20 		return ERR_PTR(-ENOMEM);
21 
22 	return drm_prime_pages_to_sg(obj->dev, msm_obj->pages, npages);
23 }
24 
msm_gem_prime_vmap(struct drm_gem_object * obj,struct iosys_map * map)25 int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
26 {
27 	void *vaddr;
28 
29 	vaddr = msm_gem_get_vaddr_locked(obj);
30 	if (IS_ERR(vaddr))
31 		return PTR_ERR(vaddr);
32 	iosys_map_set_vaddr(map, vaddr);
33 
34 	return 0;
35 }
36 
msm_gem_prime_vunmap(struct drm_gem_object * obj,struct iosys_map * map)37 void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
38 {
39 	msm_gem_put_vaddr_locked(obj);
40 }
41 
msm_gem_prime_import_sg_table(struct drm_device * dev,struct dma_buf_attachment * attach,struct sg_table * sg)42 struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
43 		struct dma_buf_attachment *attach, struct sg_table *sg)
44 {
45 	return msm_gem_import(dev, attach->dmabuf, sg);
46 }
47 
msm_gem_prime_pin(struct drm_gem_object * obj)48 int msm_gem_prime_pin(struct drm_gem_object *obj)
49 {
50 	struct page **pages;
51 	int ret = 0;
52 
53 	if (obj->import_attach)
54 		return 0;
55 
56 	pages = msm_gem_pin_pages_locked(obj);
57 	if (IS_ERR(pages))
58 		ret = PTR_ERR(pages);
59 
60 	return ret;
61 }
62 
msm_gem_prime_unpin(struct drm_gem_object * obj)63 void msm_gem_prime_unpin(struct drm_gem_object *obj)
64 {
65 	if (obj->import_attach)
66 		return;
67 
68 	msm_gem_unpin_pages_locked(obj);
69 }
70