nouveau: expose notifier handle on nvc0 as well
[platform/upstream/libdrm.git] / nouveau / abi16.c
1 /*
2  * Copyright 2012 Red Hat Inc.
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include <stdlib.h>
26 #include <stdint.h>
27
28 #include "private.h"
29
30 int
31 abi16_chan_nv04(struct nouveau_object *obj)
32 {
33         struct nouveau_device *dev = (struct nouveau_device *)obj->parent;
34         struct drm_nouveau_channel_alloc req;
35         struct nv04_fifo *nv04 = obj->data;
36         int ret;
37
38         req.fb_ctxdma_handle = nv04->vram;
39         req.tt_ctxdma_handle = nv04->gart;
40
41         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
42                                   &req, sizeof(req));
43         if (ret)
44                 return ret;
45
46         nv04->base.channel = req.channel;
47         nv04->base.pushbuf = req.pushbuf_domains;
48         nv04->notify = req.notifier_handle;
49         nv04->base.object->handle = req.channel;
50         nv04->base.object->length = sizeof(*nv04);
51         return 0;
52 }
53
54 int
55 abi16_chan_nvc0(struct nouveau_object *obj)
56 {
57         struct nouveau_device *dev = (struct nouveau_device *)obj->parent;
58         struct drm_nouveau_channel_alloc req = {};
59         struct nvc0_fifo *nvc0 = obj->data;
60         int ret;
61
62         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
63                                   &req, sizeof(req));
64         if (ret)
65                 return ret;
66
67         nvc0->base.channel = req.channel;
68         nvc0->base.pushbuf = req.pushbuf_domains;
69         nvc0->notify = req.notifier_handle;
70         nvc0->base.object->handle = req.channel;
71         nvc0->base.object->length = sizeof(*nvc0);
72         return 0;
73 }
74
75 int
76 abi16_engobj(struct nouveau_object *obj)
77 {
78         struct drm_nouveau_grobj_alloc req = {
79                 obj->parent->handle, obj->handle, obj->oclass
80         };
81         struct nouveau_device *dev;
82         int ret;
83
84         dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
85         ret = drmCommandWrite(dev->fd, DRM_NOUVEAU_GROBJ_ALLOC,
86                               &req, sizeof(req));
87         if (ret)
88                 return ret;
89
90         obj->length = sizeof(struct nouveau_object *);
91         return 0;
92 }
93
94 int
95 abi16_ntfy(struct nouveau_object *obj)
96 {
97         struct nv04_notify *ntfy = obj->data;
98         struct drm_nouveau_notifierobj_alloc req = {
99                 obj->parent->handle, ntfy->object->handle, ntfy->length
100         };
101         struct nouveau_device *dev;
102         int ret;
103
104         dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS);
105         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_NOTIFIEROBJ_ALLOC,
106                                   &req, sizeof(req));
107         if (ret)
108                 return ret;
109
110         ntfy->offset = req.offset;
111         ntfy->object->length = sizeof(*ntfy);
112         return 0;
113 }
114
115 void
116 abi16_bo_info(struct nouveau_bo *bo, struct drm_nouveau_gem_info *info)
117 {
118         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
119
120         nvbo->map_handle = info->map_handle;
121         bo->handle = info->handle;
122         bo->size = info->size;
123         bo->offset = info->offset;
124
125         bo->flags = 0;
126         if (info->domain & NOUVEAU_GEM_DOMAIN_VRAM)
127                 bo->flags |= NOUVEAU_BO_VRAM;
128         if (info->domain & NOUVEAU_GEM_DOMAIN_GART)
129                 bo->flags |= NOUVEAU_BO_GART;
130         if (!(info->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG))
131                 bo->flags |= NOUVEAU_BO_CONTIG;
132         if (nvbo->map_handle)
133                 bo->flags |= NOUVEAU_BO_MAP;
134
135         if (bo->device->chipset >= 0xc0) {
136                 bo->config.nvc0.memtype   = (info->tile_flags & 0xff00) >> 8;
137                 bo->config.nvc0.tile_mode = info->tile_mode;
138         } else
139         if (bo->device->chipset >= 0x80 || bo->device->chipset == 0x50) {
140                 bo->config.nv50.memtype   = (info->tile_flags & 0x07f00) >> 8 |
141                                             (info->tile_flags & 0x30000) >> 9;
142                 bo->config.nv50.tile_mode = info->tile_mode << 4;
143         } else {
144                 bo->config.nv04.surf_flags = info->tile_flags & 7;
145                 bo->config.nv04.surf_pitch = info->tile_mode;
146         }
147 }
148
149 int
150 abi16_bo_init(struct nouveau_bo *bo, uint32_t alignment,
151               union nouveau_bo_config *config)
152 {
153         struct nouveau_device *dev = bo->device;
154         struct drm_nouveau_gem_new req = {};
155         struct drm_nouveau_gem_info *info = &req.info;
156         int ret;
157
158         if (bo->flags & NOUVEAU_BO_VRAM)
159                 info->domain |= NOUVEAU_GEM_DOMAIN_VRAM;
160         if (bo->flags & NOUVEAU_BO_GART)
161                 info->domain |= NOUVEAU_GEM_DOMAIN_GART;
162         if (!info->domain)
163                 info->domain |= NOUVEAU_GEM_DOMAIN_VRAM |
164                                 NOUVEAU_GEM_DOMAIN_GART;
165
166         if (bo->flags & NOUVEAU_BO_MAP)
167                 info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE;
168
169         if (!(bo->flags & NOUVEAU_BO_CONTIG))
170                 info->tile_flags = NOUVEAU_GEM_TILE_NONCONTIG;
171
172         info->size = bo->size;
173         req.align = alignment;
174
175         if (config) {
176                 if (dev->chipset >= 0xc0) {
177                         info->tile_flags = (config->nvc0.memtype & 0xff) << 8;
178                         info->tile_mode  = config->nvc0.tile_mode;
179                 } else
180                 if (dev->chipset >= 0x80 || dev->chipset == 0x50) {
181                         info->tile_flags = (config->nv50.memtype & 0x07f) << 8 |
182                                            (config->nv50.memtype & 0x180) << 9;
183                         info->tile_mode  = config->nv50.tile_mode >> 4;
184                 } else {
185                         info->tile_flags = config->nv04.surf_flags & 7;
186                         info->tile_mode  = config->nv04.surf_pitch;
187                 }
188         }
189
190         if (!nouveau_device(dev)->have_bo_usage)
191                 info->tile_flags &= 0x0000ff00;
192
193         ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_NEW,
194                                   &req, sizeof(req));
195         if (ret == 0)
196                 abi16_bo_info(bo, &req.info);
197         return ret;
198 }