nouveau_notifier.c 4.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright (C) 2007 Ben Skeggs.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "drmP.h"
#include "drm.h"
#include "nouveau_drv.h"
31
#include "nouveau_ramht.h"
32
33
34
35
36
37

int
nouveau_notifier_init_channel(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;
	struct nouveau_bo *ntfy = NULL;
38
	uint32_t flags;
39
40
	int ret;

41
42
43
44
45
46
	if (nouveau_vram_notify)
		flags = TTM_PL_FLAG_VRAM;
	else
		flags = TTM_PL_FLAG_TT;

	ret = nouveau_gem_new(dev, NULL, PAGE_SIZE, 0, flags,
47
48
49
50
			      0, 0x0000, false, true, &ntfy);
	if (ret)
		return ret;

51
	ret = nouveau_bo_pin(ntfy, flags);
52
53
54
55
56
57
58
	if (ret)
		goto out_err;

	ret = nouveau_bo_map(ntfy);
	if (ret)
		goto out_err;

59
	ret = drm_mm_init(&chan->notifier_heap, 0, ntfy->bo.mem.size);
60
61
62
63
64
	if (ret)
		goto out_err;

	chan->notifier_bo = ntfy;
out_err:
65
66
	if (ret)
		drm_gem_object_unreference_unlocked(ntfy->gem);
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

	return ret;
}

void
nouveau_notifier_takedown_channel(struct nouveau_channel *chan)
{
	struct drm_device *dev = chan->dev;

	if (!chan->notifier_bo)
		return;

	nouveau_bo_unmap(chan->notifier_bo);
	mutex_lock(&dev->struct_mutex);
	nouveau_bo_unpin(chan->notifier_bo);
	mutex_unlock(&dev->struct_mutex);
83
	drm_gem_object_unreference_unlocked(chan->notifier_bo->gem);
84
	drm_mm_takedown(&chan->notifier_heap);
85
86
87
88
89
90
91
92
93
}

static void
nouveau_notifier_gpuobj_dtor(struct drm_device *dev,
			     struct nouveau_gpuobj *gpuobj)
{
	NV_DEBUG(dev, "\n");

	if (gpuobj->priv)
94
		drm_mm_put_block(gpuobj->priv);
95
96
97
98
}

int
nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
99
100
		       int size, uint32_t start, uint32_t end,
		       uint32_t *b_offset)
101
102
103
{
	struct drm_device *dev = chan->dev;
	struct nouveau_gpuobj *nobj = NULL;
104
	struct drm_mm_node *mem;
105
106
107
	uint32_t offset;
	int target, ret;

108
109
	mem = drm_mm_search_free_in_range(&chan->notifier_heap, size, 0,
					  start, end, 0);
110
	if (mem)
111
		mem = drm_mm_get_block_range(mem, size, 0, start, end);
112
113
114
115
116
	if (!mem) {
		NV_ERROR(dev, "Channel %d notifier block full\n", chan->id);
		return -ENOMEM;
	}

117
118
119
120
121
	if (chan->notifier_bo->bo.mem.mem_type == TTM_PL_VRAM)
		target = NV_MEM_TARGET_VRAM;
	else
		target = NV_MEM_TARGET_GART;
	offset  = chan->notifier_bo->bo.mem.start << PAGE_SHIFT;
122
123
124
	offset += mem->start;

	ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, offset,
125
				     mem->size, NV_MEM_ACCESS_RW, target,
126
127
				     &nobj);
	if (ret) {
128
		drm_mm_put_block(mem);
129
130
131
		NV_ERROR(dev, "Error creating notifier ctxdma: %d\n", ret);
		return ret;
	}
132
133
	nobj->dtor = nouveau_notifier_gpuobj_dtor;
	nobj->priv = mem;
134

135
136
	ret = nouveau_ramht_insert(chan, handle, nobj);
	nouveau_gpuobj_ref(NULL, &nobj);
137
	if (ret) {
138
		drm_mm_put_block(mem);
139
		NV_ERROR(dev, "Error adding notifier to ramht: %d\n", ret);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
		return ret;
	}

	*b_offset = mem->start;
	return 0;
}

int
nouveau_notifier_offset(struct nouveau_gpuobj *nobj, uint32_t *poffset)
{
	if (!nobj || nobj->dtor != nouveau_notifier_gpuobj_dtor)
		return -EINVAL;

	if (poffset) {
154
		struct drm_mm_node *mem = nobj->priv;
155
156
157
158
159
160
161
162
163
164
165
166
167
168

		if (*poffset >= mem->size)
			return false;

		*poffset += mem->start;
	}

	return 0;
}

int
nouveau_ioctl_notifier_alloc(struct drm_device *dev, void *data,
			     struct drm_file *file_priv)
{
169
	struct drm_nouveau_private *dev_priv = dev->dev_private;
170
171
172
173
	struct drm_nouveau_notifierobj_alloc *na = data;
	struct nouveau_channel *chan;
	int ret;

174
175
176
177
	/* completely unnecessary for these chipsets... */
	if (unlikely(dev_priv->card_type >= NV_C0))
		return -EINVAL;

178
179
180
	chan = nouveau_channel_get(dev, file_priv, na->channel);
	if (IS_ERR(chan))
		return PTR_ERR(chan);
181

182
183
	ret = nouveau_notifier_alloc(chan, na->handle, na->size, 0, 0x1000,
				     &na->offset);
184
185
	nouveau_channel_put(&chan);
	return ret;
186
}