Some fixes that bring me closer to surviving to the 2nd X server generation.
[nouveau] / src / 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 *userdev, uint32_t fb_ctxdma,
32                       uint32_t tt_ctxdma, struct nouveau_channel **userchan)
33 {
34         struct nouveau_device_priv *nv = nouveau_device(userdev);
35         struct nouveau_channel_priv *chan;
36         int ret;
37
38         if (!nv || !userchan || *userchan)
39             return -EINVAL;
40
41         chan = calloc(1, sizeof(*chan));
42         if (!chan)
43                 return -ENOMEM;
44         chan->base.device = userdev;
45
46         chan->drm.fb_ctxdma_handle = fb_ctxdma;
47         chan->drm.tt_ctxdma_handle = tt_ctxdma;
48         ret = drmCommandWriteRead(nv->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
49                                   &chan->drm, sizeof(chan->drm));
50         if (ret) {
51                 free(chan);
52                 return ret;
53         }
54
55         chan->base.id = chan->drm.channel;
56         if (nouveau_grobj_ref(&chan->base, chan->drm.fb_ctxdma_handle,
57                               &chan->base.vram) ||
58             nouveau_grobj_ref(&chan->base, chan->drm.tt_ctxdma_handle,
59                               &chan->base.gart)) {
60                 nouveau_channel_free((void *)&chan);
61         }
62
63         ret = drmMap(nv->fd, chan->drm.ctrl, chan->drm.ctrl_size,
64                      (void*)&chan->user);
65         if (ret) {
66                 nouveau_channel_free((void *)&chan);
67                 return ret;
68         }
69         chan->put     = &chan->user[0x40/4];
70         chan->get     = &chan->user[0x44/4];
71         chan->ref_cnt = &chan->user[0x48/4];
72
73         ret = drmMap(nv->fd, chan->drm.notifier, chan->drm.notifier_size,
74                      (drmAddressPtr)&chan->notifier_block);
75         if (ret) {
76                 nouveau_channel_free((void *)&chan);
77                 return ret;
78         }
79
80         ret = drmMap(nv->fd, chan->drm.cmdbuf, chan->drm.cmdbuf_size,
81                      (void*)&chan->pushbuf);
82         if (ret) {
83                 nouveau_channel_free((void *)&chan);
84                 return ret;
85         }
86
87         chan->max_relocs = chan->drm.cmdbuf_size / 4;
88         chan->num_relocs = 0;
89         chan->relocs =
90                 malloc(sizeof(struct nouveau_bo_reloc) * chan->max_relocs);
91
92         nouveau_dma_channel_init(&chan->base);
93
94         *userchan = &chan->base;
95         return 0;
96 }
97
98 void
99 nouveau_channel_free(struct nouveau_channel **userchan)
100 {
101         struct nouveau_channel_priv *chan;
102         
103         if (!userchan)
104                 return;
105         chan = nouveau_channel(*userchan);
106
107         if (chan) {
108                 struct nouveau_device_priv *nv;
109                 struct drm_nouveau_channel_free cf;
110
111                 nv = nouveau_device((*userchan)->device);
112
113                 FIRE_RING_CH(*userchan);
114
115                 if (chan->relocs)
116                         free(chan->relocs);
117
118                 nouveau_grobj_free(&chan->base.vram);
119                 nouveau_grobj_free(&chan->base.gart);
120
121                 cf.channel = chan->drm.channel;
122                 drmCommandWrite(nv->fd, DRM_NOUVEAU_CHANNEL_FREE,
123                                 &cf, sizeof(cf));
124                 free(chan);
125                 *userchan = NULL;
126         }
127 }
128
129