drm/nv50: fix iommu errors caused by device reading from address 0
[safe/jmp/linux-2.6] / drivers / gpu / drm / nouveau / nouveau_notifier.c
1 /*
2  * Copyright (C) 2007 Ben Skeggs.
3  *
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27
28 #include "drmP.h"
29 #include "drm.h"
30 #include "nouveau_drv.h"
31
32 int
33 nouveau_notifier_init_channel(struct nouveau_channel *chan)
34 {
35         struct drm_device *dev = chan->dev;
36         struct nouveau_bo *ntfy = NULL;
37         uint32_t flags;
38         int ret;
39
40         if (nouveau_vram_notify)
41                 flags = TTM_PL_FLAG_VRAM;
42         else
43                 flags = TTM_PL_FLAG_TT;
44
45         ret = nouveau_gem_new(dev, NULL, PAGE_SIZE, 0, flags,
46                               0, 0x0000, false, true, &ntfy);
47         if (ret)
48                 return ret;
49
50         ret = nouveau_bo_pin(ntfy, flags);
51         if (ret)
52                 goto out_err;
53
54         ret = nouveau_bo_map(ntfy);
55         if (ret)
56                 goto out_err;
57
58         ret = nouveau_mem_init_heap(&chan->notifier_heap, 0, ntfy->bo.mem.size);
59         if (ret)
60                 goto out_err;
61
62         chan->notifier_bo = ntfy;
63 out_err:
64         if (ret)
65                 drm_gem_object_unreference_unlocked(ntfy->gem);
66
67         return ret;
68 }
69
70 void
71 nouveau_notifier_takedown_channel(struct nouveau_channel *chan)
72 {
73         struct drm_device *dev = chan->dev;
74
75         if (!chan->notifier_bo)
76                 return;
77
78         nouveau_bo_unmap(chan->notifier_bo);
79         mutex_lock(&dev->struct_mutex);
80         nouveau_bo_unpin(chan->notifier_bo);
81         mutex_unlock(&dev->struct_mutex);
82         drm_gem_object_unreference_unlocked(chan->notifier_bo->gem);
83         nouveau_mem_takedown(&chan->notifier_heap);
84 }
85
86 static void
87 nouveau_notifier_gpuobj_dtor(struct drm_device *dev,
88                              struct nouveau_gpuobj *gpuobj)
89 {
90         NV_DEBUG(dev, "\n");
91
92         if (gpuobj->priv)
93                 nouveau_mem_free_block(gpuobj->priv);
94 }
95
96 int
97 nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
98                        int size, uint32_t *b_offset)
99 {
100         struct drm_device *dev = chan->dev;
101         struct drm_nouveau_private *dev_priv = dev->dev_private;
102         struct nouveau_gpuobj *nobj = NULL;
103         struct mem_block *mem;
104         uint32_t offset;
105         int target, ret;
106
107         if (!chan->notifier_heap) {
108                 NV_ERROR(dev, "Channel %d doesn't have a notifier heap!\n",
109                          chan->id);
110                 return -EINVAL;
111         }
112
113         mem = nouveau_mem_alloc_block(chan->notifier_heap, size, 0,
114                                       (struct drm_file *)-2, 0);
115         if (!mem) {
116                 NV_ERROR(dev, "Channel %d notifier block full\n", chan->id);
117                 return -ENOMEM;
118         }
119
120         offset = chan->notifier_bo->bo.mem.mm_node->start << PAGE_SHIFT;
121         if (chan->notifier_bo->bo.mem.mem_type == TTM_PL_VRAM) {
122                 target = NV_DMA_TARGET_VIDMEM;
123         } else
124         if (chan->notifier_bo->bo.mem.mem_type == TTM_PL_TT) {
125                 if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA &&
126                     dev_priv->card_type < NV_50) {
127                         ret = nouveau_sgdma_get_page(dev, offset, &offset);
128                         if (ret)
129                                 return ret;
130                         target = NV_DMA_TARGET_PCI;
131                 } else {
132                         target = NV_DMA_TARGET_AGP;
133                         if (dev_priv->card_type >= NV_50)
134                                 offset += dev_priv->vm_gart_base;
135                 }
136         } else {
137                 NV_ERROR(dev, "Bad DMA target, mem_type %d!\n",
138                          chan->notifier_bo->bo.mem.mem_type);
139                 return -EINVAL;
140         }
141         offset += mem->start;
142
143         ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, offset,
144                                      mem->size, NV_DMA_ACCESS_RW, target,
145                                      &nobj);
146         if (ret) {
147                 nouveau_mem_free_block(mem);
148                 NV_ERROR(dev, "Error creating notifier ctxdma: %d\n", ret);
149                 return ret;
150         }
151         nobj->dtor   = nouveau_notifier_gpuobj_dtor;
152         nobj->priv   = mem;
153
154         ret = nouveau_gpuobj_ref_add(dev, chan, handle, nobj, NULL);
155         if (ret) {
156                 nouveau_gpuobj_del(dev, &nobj);
157                 nouveau_mem_free_block(mem);
158                 NV_ERROR(dev, "Error referencing notifier ctxdma: %d\n", ret);
159                 return ret;
160         }
161
162         *b_offset = mem->start;
163         return 0;
164 }
165
166 int
167 nouveau_notifier_offset(struct nouveau_gpuobj *nobj, uint32_t *poffset)
168 {
169         if (!nobj || nobj->dtor != nouveau_notifier_gpuobj_dtor)
170                 return -EINVAL;
171
172         if (poffset) {
173                 struct mem_block *mem = nobj->priv;
174
175                 if (*poffset >= mem->size)
176                         return false;
177
178                 *poffset += mem->start;
179         }
180
181         return 0;
182 }
183
184 int
185 nouveau_ioctl_notifier_alloc(struct drm_device *dev, void *data,
186                              struct drm_file *file_priv)
187 {
188         struct drm_nouveau_notifierobj_alloc *na = data;
189         struct nouveau_channel *chan;
190         int ret;
191
192         NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
193         NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(na->channel, file_priv, chan);
194
195         ret = nouveau_notifier_alloc(chan, na->handle, na->size, &na->offset);
196         if (ret)
197                 return ret;
198
199         return 0;
200 }