Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / winsys / dri / nouveau / nouveau_channel.c
1 /*
2 * Copyright 2007 Nouveau Project
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "nouveau_drmif.h"
28 #include "nouveau_dma.h"
29
30 int
31 nouveau_channel_alloc(struct nouveau_device *dev, uint32_t fb_ctxdma,
32 uint32_t tt_ctxdma, struct nouveau_channel **chan)
33 {
34 struct nouveau_device_priv *nvdev = nouveau_device(dev);
35 struct nouveau_channel_priv *nvchan;
36 int ret;
37
38 if (!nvdev || !chan || *chan)
39 return -EINVAL;
40
41 nvchan = calloc(1, sizeof(struct nouveau_channel_priv));
42 if (!nvchan)
43 return -ENOMEM;
44 nvchan->base.device = dev;
45
46 nvchan->drm.fb_ctxdma_handle = fb_ctxdma;
47 nvchan->drm.tt_ctxdma_handle = tt_ctxdma;
48 ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
49 &nvchan->drm, sizeof(nvchan->drm));
50 if (ret) {
51 free(nvchan);
52 return ret;
53 }
54
55 nvchan->base.id = nvchan->drm.channel;
56 if (nouveau_grobj_ref(&nvchan->base, nvchan->drm.fb_ctxdma_handle,
57 &nvchan->base.vram) ||
58 nouveau_grobj_ref(&nvchan->base, nvchan->drm.tt_ctxdma_handle,
59 &nvchan->base.gart)) {
60 nouveau_channel_free((void *)&nvchan);
61 return -EINVAL;
62 }
63
64 ret = drmMap(nvdev->fd, nvchan->drm.ctrl, nvchan->drm.ctrl_size,
65 (void*)&nvchan->user);
66 if (ret) {
67 nouveau_channel_free((void *)&nvchan);
68 return ret;
69 }
70 nvchan->put = &nvchan->user[0x40/4];
71 nvchan->get = &nvchan->user[0x44/4];
72 nvchan->ref_cnt = &nvchan->user[0x48/4];
73
74 ret = drmMap(nvdev->fd, nvchan->drm.notifier, nvchan->drm.notifier_size,
75 (drmAddressPtr)&nvchan->notifier_block);
76 if (ret) {
77 nouveau_channel_free((void *)&nvchan);
78 return ret;
79 }
80
81 ret = drmMap(nvdev->fd, nvchan->drm.cmdbuf, nvchan->drm.cmdbuf_size,
82 (void*)&nvchan->pushbuf);
83 if (ret) {
84 nouveau_channel_free((void *)&nvchan);
85 return ret;
86 }
87
88 ret = nouveau_grobj_alloc(&nvchan->base, 0x00000000, 0x0030,
89 &nvchan->base.nullobj);
90 if (ret) {
91 nouveau_channel_free((void *)&nvchan);
92 return ret;
93 }
94
95 nouveau_dma_channel_init(&nvchan->base);
96 nouveau_pushbuf_init(&nvchan->base);
97
98 *chan = &nvchan->base;
99 return 0;
100 }
101
102 void
103 nouveau_channel_free(struct nouveau_channel **chan)
104 {
105 struct nouveau_channel_priv *nvchan;
106 struct nouveau_device_priv *nvdev;
107 struct drm_nouveau_channel_free cf;
108
109 if (!chan || !*chan)
110 return;
111 nvchan = nouveau_channel(*chan);
112 *chan = NULL;
113 nvdev = nouveau_device(nvchan->base.device);
114
115 FIRE_RING_CH(&nvchan->base);
116
117 nouveau_grobj_free(&nvchan->base.vram);
118 nouveau_grobj_free(&nvchan->base.gart);
119 nouveau_grobj_free(&nvchan->base.nullobj);
120
121 cf.channel = nvchan->drm.channel;
122 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_CHANNEL_FREE, &cf, sizeof(cf));
123 free(nvchan);
124 }
125
126