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
32int
33nouveau_notifier_init_channel(struct nouveau_channel *chan)
34{
35	struct drm_device *dev = chan->dev;
36	int flags, ret;
37
38	flags = (NOUVEAU_MEM_PCI | NOUVEAU_MEM_MAPPED |
39	         NOUVEAU_MEM_FB_ACCEPTABLE);
40
41	chan->notifier_block = nouveau_mem_alloc(dev, 0, PAGE_SIZE, flags,
42						 (struct drm_file *)-2);
43	if (!chan->notifier_block)
44		return -ENOMEM;
45	DRM_DEBUG("Allocated notifier block in 0x%08x\n",
46		  chan->notifier_block->flags);
47
48	ret = nouveau_mem_init_heap(&chan->notifier_heap,
49				    0, chan->notifier_block->size);
50	if (ret)
51		return ret;
52
53	return 0;
54}
55
56void
57nouveau_notifier_takedown_channel(struct nouveau_channel *chan)
58{
59	struct drm_device *dev = chan->dev;
60
61	if (chan->notifier_block) {
62		nouveau_mem_free(dev, chan->notifier_block);
63		chan->notifier_block = NULL;
64	}
65
66	nouveau_mem_takedown(&chan->notifier_heap);
67}
68
69static void
70nouveau_notifier_gpuobj_dtor(struct drm_device *dev,
71			     struct nouveau_gpuobj *gpuobj)
72{
73	DRM_DEBUG("\n");
74
75	if (gpuobj->priv)
76		nouveau_mem_free_block(gpuobj->priv);
77}
78
79int
80nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
81		       int count, uint32_t *b_offset)
82{
83	struct drm_device *dev = chan->dev;
84	struct drm_nouveau_private *dev_priv = dev->dev_private;
85	struct nouveau_gpuobj *nobj = NULL;
86	struct mem_block *mem;
87	uint32_t offset;
88	int target, ret;
89
90	if (!chan->notifier_heap) {
91		DRM_ERROR("Channel %d doesn't have a notifier heap!\n",
92			  chan->id);
93		return -EINVAL;
94	}
95
96	mem = nouveau_mem_alloc_block(chan->notifier_heap, count*32, 0,
97				      (struct drm_file *)-2, 0);
98	if (!mem) {
99		DRM_ERROR("Channel %d notifier block full\n", chan->id);
100		return -ENOMEM;
101	}
102	mem->flags = NOUVEAU_MEM_NOTIFIER;
103
104	offset = chan->notifier_block->start;
105	if (chan->notifier_block->flags & NOUVEAU_MEM_FB) {
106		target = NV_DMA_TARGET_VIDMEM;
107	} else
108	if (chan->notifier_block->flags & NOUVEAU_MEM_AGP) {
109		if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA &&
110		    dev_priv->card_type < NV_50) {
111			ret = nouveau_sgdma_get_page(dev, offset, &offset);
112			if (ret)
113				return ret;
114			target = NV_DMA_TARGET_PCI;
115		} else {
116			target = NV_DMA_TARGET_AGP;
117		}
118	} else
119	if (chan->notifier_block->flags & NOUVEAU_MEM_PCI) {
120		target = NV_DMA_TARGET_PCI_NONLINEAR;
121	} else {
122		DRM_ERROR("Bad DMA target, flags 0x%08x!\n",
123			  chan->notifier_block->flags);
124		return -EINVAL;
125	}
126	offset += mem->start;
127
128	if ((ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
129					  offset, mem->size,
130					  NV_DMA_ACCESS_RW, target, &nobj))) {
131		nouveau_mem_free_block(mem);
132		DRM_ERROR("Error creating notifier ctxdma: %d\n", ret);
133		return ret;
134	}
135	nobj->dtor   = nouveau_notifier_gpuobj_dtor;
136	nobj->priv   = mem;
137
138	if ((ret = nouveau_gpuobj_ref_add(dev, chan, handle, nobj, NULL))) {
139		nouveau_gpuobj_del(dev, &nobj);
140		nouveau_mem_free_block(mem);
141		DRM_ERROR("Error referencing notifier ctxdma: %d\n", ret);
142		return ret;
143	}
144
145	*b_offset = mem->start;
146	return 0;
147}
148
149int
150nouveau_ioctl_notifier_alloc(struct drm_device *dev, void *data,
151			     struct drm_file *file_priv)
152{
153	struct drm_nouveau_notifierobj_alloc *na = data;
154	struct nouveau_channel *chan;
155	int ret;
156
157	NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
158	NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(na->channel, file_priv, chan);
159
160	ret = nouveau_notifier_alloc(chan, na->handle, na->count, &na->offset);
161	if (ret)
162		return ret;
163
164	return 0;
165}
166