OSDN Git Service

android: set LOCAL_MODULE_TAGS to optional
[android-x86/external-libdrm.git] / 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_private.h"
28
29 int
30 nouveau_channel_alloc(struct nouveau_device *dev, uint32_t fb_ctxdma,
31                       uint32_t tt_ctxdma, int pushbuf_size,
32                       struct nouveau_channel **chan)
33 {
34         struct nouveau_device_priv *nvdev = nouveau_device(dev);
35         struct nouveau_channel_priv *nvchan;
36         unsigned i;
37         int ret;
38
39         if (!nvdev || !chan || *chan)
40             return -EINVAL;
41
42         nvchan = calloc(1, sizeof(struct nouveau_channel_priv));
43         if (!nvchan)
44                 return -ENOMEM;
45         nvchan->base.device = dev;
46
47         nvchan->drm.fb_ctxdma_handle = fb_ctxdma;
48         nvchan->drm.tt_ctxdma_handle = tt_ctxdma;
49         ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
50                                   &nvchan->drm, sizeof(nvchan->drm));
51         if (ret) {
52                 free(nvchan);
53                 return ret;
54         }
55
56         nvchan->base.id = nvchan->drm.channel;
57         if (nouveau_grobj_ref(&nvchan->base, nvchan->drm.fb_ctxdma_handle,
58                               &nvchan->base.vram) ||
59             nouveau_grobj_ref(&nvchan->base, nvchan->drm.tt_ctxdma_handle,
60                               &nvchan->base.gart)) {
61                 nouveau_channel_free((void *)&nvchan);
62                 return -EINVAL;
63         }
64
65         /* Mark all DRM-assigned subchannels as in-use */
66         for (i = 0; i < nvchan->drm.nr_subchan; i++) {
67                 struct nouveau_grobj_priv *gr = calloc(1, sizeof(*gr));
68
69                 gr->base.bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
70                 gr->base.subc = i;
71                 gr->base.handle = nvchan->drm.subchan[i].handle;
72                 gr->base.grclass = nvchan->drm.subchan[i].grclass;
73                 gr->base.channel = &nvchan->base;
74
75                 nvchan->base.subc[i].gr = &gr->base;
76         }
77
78         if (dev->chipset < 0xc0) {
79                 ret = nouveau_bo_wrap(dev, nvchan->drm.notifier_handle,
80                                       &nvchan->notifier_bo);
81                 if (!ret)
82                         ret = nouveau_bo_map(nvchan->notifier_bo,
83                                              NOUVEAU_BO_RDWR);
84                 if (ret) {
85                         nouveau_channel_free((void *)&nvchan);
86                         return ret;
87                 }
88
89                 ret = nouveau_grobj_alloc(&nvchan->base, 0x00000000, 0x0030,
90                                           &nvchan->base.nullobj);
91                 if (ret) {
92                         nouveau_channel_free((void *)&nvchan);
93                         return ret;
94                 }
95         }
96
97         ret = nouveau_pushbuf_init(&nvchan->base, pushbuf_size);
98         if (ret) {
99                 nouveau_channel_free((void *)&nvchan);
100                 return ret;
101         }
102
103         *chan = &nvchan->base;
104         return 0;
105 }
106
107 void
108 nouveau_channel_free(struct nouveau_channel **chan)
109 {
110         struct nouveau_channel_priv *nvchan;
111         struct nouveau_device_priv *nvdev;
112         struct drm_nouveau_channel_free cf;
113         unsigned i;
114
115         if (!chan || !*chan)
116                 return;
117         nvchan = nouveau_channel(*chan);
118         (*chan)->flush_notify = NULL;
119         *chan = NULL;
120         nvdev = nouveau_device(nvchan->base.device);
121
122         FIRE_RING(&nvchan->base);
123
124         nouveau_pushbuf_fini(&nvchan->base);
125         if (nvchan->notifier_bo) {
126                 nouveau_bo_unmap(nvchan->notifier_bo);
127                 nouveau_bo_ref(NULL, &nvchan->notifier_bo);
128         }
129
130         for (i = 0; i < nvchan->drm.nr_subchan; i++)
131                 free(nvchan->base.subc[i].gr);
132
133         nouveau_grobj_free(&nvchan->base.vram);
134         nouveau_grobj_free(&nvchan->base.gart);
135         nouveau_grobj_free(&nvchan->base.nullobj);
136
137         cf.channel = nvchan->drm.channel;
138         drmCommandWrite(nvdev->fd, DRM_NOUVEAU_CHANNEL_FREE, &cf, sizeof(cf));
139         free(nvchan);
140 }
141
142