drm/i915: update dpms property in set_mode
[deliverable/linux.git] / drivers / gpu / drm / i915 / i915_gem_dmabuf.c
CommitLineData
1286ff73
DV
1/*
2 * Copyright 2012 Red Hat Inc
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie <airlied@redhat.com>
25 */
26#include "drmP.h"
27#include "i915_drv.h"
28#include <linux/dma-buf.h>
29
6a101cb2 30static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
9da3da66 31 enum dma_data_direction dir)
1286ff73
DV
32{
33 struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
9da3da66
CW
34 struct sg_table *st;
35 struct scatterlist *src, *dst;
36 int ret, i;
1286ff73 37
9da3da66 38 ret = i915_mutex_lock_interruptible(obj->base.dev);
1286ff73
DV
39 if (ret)
40 return ERR_PTR(ret);
41
37e680a1 42 ret = i915_gem_object_get_pages(obj);
6c085a72 43 if (ret) {
9da3da66
CW
44 st = ERR_PTR(ret);
45 goto out;
46 }
47
48 /* Copy sg so that we make an independent mapping */
49 st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
50 if (st == NULL) {
51 st = ERR_PTR(-ENOMEM);
52 goto out;
53 }
54
55 ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
56 if (ret) {
57 kfree(st);
58 st = ERR_PTR(ret);
59 goto out;
60 }
61
62 src = obj->pages->sgl;
63 dst = st->sgl;
64 for (i = 0; i < obj->pages->nents; i++) {
65 sg_set_page(dst, sg_page(src), PAGE_SIZE, 0);
66 dst = sg_next(dst);
67 src = sg_next(src);
68 }
69
70 if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
71 sg_free_table(st);
72 kfree(st);
73 st = ERR_PTR(-ENOMEM);
6c085a72 74 goto out;
1286ff73
DV
75 }
76
a5570178
CW
77 i915_gem_object_pin_pages(obj);
78
1286ff73 79out:
9da3da66
CW
80 mutex_unlock(&obj->base.dev->struct_mutex);
81 return st;
1286ff73
DV
82}
83
6a101cb2 84static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
1286ff73
DV
85 struct sg_table *sg, enum dma_data_direction dir)
86{
87 dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
88 sg_free_table(sg);
89 kfree(sg);
90}
91
6a101cb2 92static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
1286ff73
DV
93{
94 struct drm_i915_gem_object *obj = dma_buf->priv;
95
96 if (obj->base.export_dma_buf == dma_buf) {
97 /* drop the reference on the export fd holds */
98 obj->base.export_dma_buf = NULL;
99 drm_gem_object_unreference_unlocked(&obj->base);
100 }
101}
102
9a70cc2a
DA
103static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
104{
105 struct drm_i915_gem_object *obj = dma_buf->priv;
106 struct drm_device *dev = obj->base.dev;
9da3da66
CW
107 struct scatterlist *sg;
108 struct page **pages;
109 int ret, i;
9a70cc2a
DA
110
111 ret = i915_mutex_lock_interruptible(dev);
112 if (ret)
113 return ERR_PTR(ret);
114
115 if (obj->dma_buf_vmapping) {
116 obj->vmapping_count++;
117 goto out_unlock;
118 }
119
37e680a1 120 ret = i915_gem_object_get_pages(obj);
9da3da66
CW
121 if (ret)
122 goto error;
9a70cc2a 123
9da3da66
CW
124 ret = -ENOMEM;
125
126 pages = drm_malloc_ab(obj->pages->nents, sizeof(struct page *));
127 if (pages == NULL)
128 goto error;
129
130 for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i)
131 pages[i] = sg_page(sg);
132
133 obj->dma_buf_vmapping = vmap(pages, obj->pages->nents, 0, PAGE_KERNEL);
134 drm_free_large(pages);
135
136 if (!obj->dma_buf_vmapping)
137 goto error;
9a70cc2a
DA
138
139 obj->vmapping_count = 1;
a5570178 140 i915_gem_object_pin_pages(obj);
9a70cc2a
DA
141out_unlock:
142 mutex_unlock(&dev->struct_mutex);
143 return obj->dma_buf_vmapping;
9da3da66
CW
144
145error:
146 mutex_unlock(&dev->struct_mutex);
147 return ERR_PTR(ret);
9a70cc2a
DA
148}
149
150static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
151{
152 struct drm_i915_gem_object *obj = dma_buf->priv;
153 struct drm_device *dev = obj->base.dev;
154 int ret;
155
156 ret = i915_mutex_lock_interruptible(dev);
157 if (ret)
158 return;
159
a5570178 160 if (--obj->vmapping_count == 0) {
9a70cc2a
DA
161 vunmap(obj->dma_buf_vmapping);
162 obj->dma_buf_vmapping = NULL;
a5570178
CW
163
164 i915_gem_object_unpin_pages(obj);
9a70cc2a
DA
165 }
166 mutex_unlock(&dev->struct_mutex);
167}
168
1286ff73
DV
169static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
170{
171 return NULL;
172}
173
174static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
175{
176
177}
178static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
179{
180 return NULL;
181}
182
183static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
184{
185
186}
187
2dad9d4d
DA
188static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
189{
190 return -EINVAL;
191}
192
ec6f1bb9
DA
193static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
194{
195 struct drm_i915_gem_object *obj = dma_buf->priv;
196 struct drm_device *dev = obj->base.dev;
197 int ret;
198 bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
199
200 ret = i915_mutex_lock_interruptible(dev);
201 if (ret)
202 return ret;
203
204 ret = i915_gem_object_set_to_cpu_domain(obj, write);
205 mutex_unlock(&dev->struct_mutex);
206 return ret;
207}
208
6a101cb2 209static const struct dma_buf_ops i915_dmabuf_ops = {
1286ff73
DV
210 .map_dma_buf = i915_gem_map_dma_buf,
211 .unmap_dma_buf = i915_gem_unmap_dma_buf,
212 .release = i915_gem_dmabuf_release,
213 .kmap = i915_gem_dmabuf_kmap,
214 .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
215 .kunmap = i915_gem_dmabuf_kunmap,
216 .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
2dad9d4d 217 .mmap = i915_gem_dmabuf_mmap,
9a70cc2a
DA
218 .vmap = i915_gem_dmabuf_vmap,
219 .vunmap = i915_gem_dmabuf_vunmap,
ec6f1bb9 220 .begin_cpu_access = i915_gem_begin_cpu_access,
1286ff73
DV
221};
222
223struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
9da3da66 224 struct drm_gem_object *gem_obj, int flags)
1286ff73
DV
225{
226 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
227
9da3da66 228 return dma_buf_export(obj, &i915_dmabuf_ops, obj->base.size, 0600);
1286ff73
DV
229}
230
231struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
9da3da66 232 struct dma_buf *dma_buf)
1286ff73
DV
233{
234 struct dma_buf_attachment *attach;
235 struct sg_table *sg;
236 struct drm_i915_gem_object *obj;
1286ff73
DV
237 int ret;
238
239 /* is this one of own objects? */
240 if (dma_buf->ops == &i915_dmabuf_ops) {
241 obj = dma_buf->priv;
242 /* is it from our device? */
243 if (obj->base.dev == dev) {
244 drm_gem_object_reference(&obj->base);
245 return &obj->base;
246 }
247 }
248
249 /* need to attach */
250 attach = dma_buf_attach(dma_buf, dev->dev);
251 if (IS_ERR(attach))
252 return ERR_CAST(attach);
253
254 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
255 if (IS_ERR(sg)) {
256 ret = PTR_ERR(sg);
257 goto fail_detach;
258 }
259
1286ff73
DV
260 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
261 if (obj == NULL) {
262 ret = -ENOMEM;
263 goto fail_unmap;
264 }
265
9da3da66 266 ret = drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
1286ff73
DV
267 if (ret) {
268 kfree(obj);
269 goto fail_unmap;
270 }
271
9da3da66 272 obj->has_dma_mapping = true;
1286ff73
DV
273 obj->sg_table = sg;
274 obj->base.import_attach = attach;
275
276 return &obj->base;
277
278fail_unmap:
279 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
280fail_detach:
281 dma_buf_detach(dma_buf, attach);
282 return ERR_PTR(ret);
283}
9da3da66 284
This page took 0.051393 seconds and 5 git commands to generate.