18
18
#include <linux/dma-buf.h>
19
19
20
20
struct exynos_drm_dmabuf_attachment {
21
- struct sg_table sgt ;
21
+ struct sg_table * sgt ;
22
22
enum dma_data_direction dir ;
23
23
bool is_mapped ;
24
24
};
@@ -53,13 +53,15 @@ static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
53
53
if (!exynos_attach )
54
54
return ;
55
55
56
- sgt = & exynos_attach -> sgt ;
57
-
58
- if (exynos_attach -> dir != DMA_NONE )
59
- dma_unmap_sg (attach -> dev , sgt -> sgl , sgt -> nents ,
60
- exynos_attach -> dir );
56
+ sgt = exynos_attach -> sgt ;
57
+ if (sgt ) {
58
+ if (exynos_attach -> dir != DMA_NONE )
59
+ dma_unmap_sg (attach -> dev , sgt -> sgl , sgt -> nents ,
60
+ exynos_attach -> dir );
61
+ sg_free_table (sgt );
62
+ }
61
63
62
- sg_free_table (sgt );
64
+ kfree (sgt );
63
65
kfree (exynos_attach );
64
66
attach -> priv = NULL ;
65
67
}
@@ -70,59 +72,43 @@ static struct sg_table *
70
72
{
71
73
struct exynos_drm_dmabuf_attachment * exynos_attach = attach -> priv ;
72
74
struct exynos_drm_gem_obj * gem_obj = dma_buf_to_obj (attach -> dmabuf );
73
- struct drm_device * dev = gem_obj -> base .dev ;
74
75
struct exynos_drm_gem_buf * buf ;
75
- struct scatterlist * rd , * wr ;
76
- struct sg_table * sgt = NULL ;
77
- unsigned int i ;
78
- int nents , ret ;
76
+ struct sg_table * sgt ;
77
+ int npages ;
79
78
80
79
/* just return current sgt if already requested. */
81
80
if (exynos_attach -> dir == dir && exynos_attach -> is_mapped )
82
- return & exynos_attach -> sgt ;
81
+ return exynos_attach -> sgt ;
83
82
84
83
buf = gem_obj -> buffer ;
85
84
if (!buf ) {
86
85
DRM_ERROR ("buffer is null.\n" );
87
86
return ERR_PTR (- ENOMEM );
88
87
}
89
88
90
- sgt = & exynos_attach -> sgt ;
89
+ npages = buf -> size >> PAGE_SHIFT ;
91
90
92
- ret = sg_alloc_table (sgt , buf -> sgt -> orig_nents , GFP_KERNEL );
93
- if (ret ) {
94
- DRM_ERROR ("failed to alloc sgt.\n" );
95
- return ERR_PTR (- ENOMEM );
96
- }
97
-
98
- mutex_lock (& dev -> struct_mutex );
99
-
100
- rd = buf -> sgt -> sgl ;
101
- wr = sgt -> sgl ;
102
- for (i = 0 ; i < sgt -> orig_nents ; ++ i ) {
103
- sg_set_page (wr , sg_page (rd ), rd -> length , rd -> offset );
104
- rd = sg_next (rd );
105
- wr = sg_next (wr );
106
- }
91
+ sgt = drm_prime_pages_to_sg (buf -> pages , npages );
92
+ if (IS_ERR (sgt ))
93
+ goto err ;
107
94
108
95
if (dir != DMA_NONE ) {
109
- nents = dma_map_sg (attach -> dev , sgt -> sgl , sgt -> orig_nents , dir );
110
- if (!nents ) {
96
+ if (!dma_map_sg (attach -> dev , sgt -> sgl , sgt -> orig_nents , dir )) {
111
97
DRM_ERROR ("failed to map sgl with iommu.\n" );
112
98
sg_free_table (sgt );
113
99
sgt = ERR_PTR (- EIO );
114
- goto err_unlock ;
100
+ goto err ;
115
101
}
116
102
}
117
103
118
104
exynos_attach -> is_mapped = true;
105
+ exynos_attach -> sgt = sgt ;
119
106
exynos_attach -> dir = dir ;
120
107
attach -> priv = exynos_attach ;
121
108
122
109
DRM_DEBUG_PRIME ("buffer size = 0x%lx\n" , buf -> size );
123
110
124
- err_unlock :
125
- mutex_unlock (& dev -> struct_mutex );
111
+ err :
126
112
return sgt ;
127
113
}
128
114
@@ -280,7 +266,6 @@ struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
280
266
}
281
267
282
268
exynos_gem_obj -> buffer = buffer ;
283
- buffer -> sgt = sgt ;
284
269
exynos_gem_obj -> base .import_attach = attach ;
285
270
286
271
DRM_DEBUG_PRIME ("dma_addr = %pad, size = 0x%lx\n" , & buffer -> dma_addr ,
0 commit comments