Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 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 | |
Rob Clark | c09dcbc | 2014-09-28 14:19:14 -0400 | [diff] [blame] | 25 | #ifdef HAVE_CONFIG_H |
| 26 | # include <config.h> |
| 27 | #endif |
| 28 | |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
Ben Skeggs | c41b494 | 2012-11-23 12:40:30 +1000 | [diff] [blame] | 31 | #include <stddef.h> |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 32 | |
| 33 | #include "private.h" |
| 34 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 35 | #include "nvif/class.h" |
Ben Skeggs | c41b494 | 2012-11-23 12:40:30 +1000 | [diff] [blame] | 36 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 37 | |
| 38 | static int |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 39 | abi16_chan_nv04(struct nouveau_object *obj) |
| 40 | { |
| 41 | struct nouveau_device *dev = (struct nouveau_device *)obj->parent; |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 42 | struct nv04_fifo *nv04 = obj->data; |
Emil Velikov | a9e5880 | 2015-08-15 18:01:53 +0100 | [diff] [blame] | 43 | struct drm_nouveau_channel_alloc req = { |
| 44 | .fb_ctxdma_handle = nv04->vram, |
| 45 | .tt_ctxdma_handle = nv04->gart |
| 46 | }; |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 47 | int ret; |
| 48 | |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 49 | ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_CHANNEL_ALLOC, |
| 50 | &req, sizeof(req)); |
| 51 | if (ret) |
| 52 | return ret; |
| 53 | |
| 54 | nv04->base.channel = req.channel; |
| 55 | nv04->base.pushbuf = req.pushbuf_domains; |
| 56 | nv04->notify = req.notifier_handle; |
| 57 | nv04->base.object->handle = req.channel; |
| 58 | nv04->base.object->length = sizeof(*nv04); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 62 | static int |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 63 | abi16_chan_nvc0(struct nouveau_object *obj) |
| 64 | { |
| 65 | struct nouveau_device *dev = (struct nouveau_device *)obj->parent; |
Ben Skeggs | 73b9a28 | 2012-04-17 08:35:43 +1000 | [diff] [blame] | 66 | struct drm_nouveau_channel_alloc req = {}; |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 67 | struct nvc0_fifo *nvc0 = obj->data; |
| 68 | int ret; |
| 69 | |
| 70 | ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_CHANNEL_ALLOC, |
| 71 | &req, sizeof(req)); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | |
| 75 | nvc0->base.channel = req.channel; |
| 76 | nvc0->base.pushbuf = req.pushbuf_domains; |
Christoph Bumiller | 754655c | 2012-04-19 20:03:39 +0200 | [diff] [blame] | 77 | nvc0->notify = req.notifier_handle; |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 78 | nvc0->base.object->handle = req.channel; |
| 79 | nvc0->base.object->length = sizeof(*nvc0); |
| 80 | return 0; |
| 81 | } |
| 82 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 83 | static int |
Ben Skeggs | c41b494 | 2012-11-23 12:40:30 +1000 | [diff] [blame] | 84 | abi16_chan_nve0(struct nouveau_object *obj) |
| 85 | { |
| 86 | struct nouveau_device *dev = (struct nouveau_device *)obj->parent; |
| 87 | struct drm_nouveau_channel_alloc req = {}; |
| 88 | struct nve0_fifo *nve0 = obj->data; |
| 89 | int ret; |
| 90 | |
| 91 | if (obj->length > offsetof(struct nve0_fifo, engine)) { |
| 92 | req.fb_ctxdma_handle = 0xffffffff; |
| 93 | req.tt_ctxdma_handle = nve0->engine; |
| 94 | } |
| 95 | |
| 96 | ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_CHANNEL_ALLOC, |
| 97 | &req, sizeof(req)); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | nve0->base.channel = req.channel; |
| 102 | nve0->base.pushbuf = req.pushbuf_domains; |
| 103 | nve0->notify = req.notifier_handle; |
| 104 | nve0->base.object->handle = req.channel; |
| 105 | nve0->base.object->length = sizeof(*nve0); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 109 | static int |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 110 | abi16_engobj(struct nouveau_object *obj) |
| 111 | { |
| 112 | struct drm_nouveau_grobj_alloc req = { |
Emil Velikov | a9e5880 | 2015-08-15 18:01:53 +0100 | [diff] [blame] | 113 | .channel = obj->parent->handle, |
| 114 | .handle = obj->handle, |
| 115 | .class = obj->oclass, |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 116 | }; |
| 117 | struct nouveau_device *dev; |
| 118 | int ret; |
| 119 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 120 | /* Older kernel versions did not have the concept of nouveau- |
| 121 | * specific classes and abused some NVIDIA-assigned ones for |
| 122 | * a SW class. The ABI16 layer has compatibility in place to |
| 123 | * translate these older identifiers to the newer ones. |
| 124 | * |
| 125 | * Clients that have been updated to use NVIF are required to |
| 126 | * use the newer class identifiers, which means that they'll |
| 127 | * break if running on an older kernel. |
| 128 | * |
| 129 | * To handle this case, when using ABI16, we translate to the |
| 130 | * older values which work on any kernel. |
| 131 | */ |
| 132 | switch (req.class) { |
| 133 | case NVIF_CLASS_SW_NV04 : req.class = 0x006e; break; |
| 134 | case NVIF_CLASS_SW_NV10 : req.class = 0x016e; break; |
| 135 | case NVIF_CLASS_SW_NV50 : req.class = 0x506e; break; |
| 136 | case NVIF_CLASS_SW_GF100: req.class = 0x906e; break; |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 141 | dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS); |
| 142 | ret = drmCommandWrite(dev->fd, DRM_NOUVEAU_GROBJ_ALLOC, |
| 143 | &req, sizeof(req)); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | obj->length = sizeof(struct nouveau_object *); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 151 | static int |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 152 | abi16_ntfy(struct nouveau_object *obj) |
| 153 | { |
| 154 | struct nv04_notify *ntfy = obj->data; |
| 155 | struct drm_nouveau_notifierobj_alloc req = { |
Emil Velikov | a9e5880 | 2015-08-15 18:01:53 +0100 | [diff] [blame] | 156 | .channel = obj->parent->handle, |
| 157 | .handle = ntfy->object->handle, |
| 158 | .size = ntfy->length, |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 159 | }; |
| 160 | struct nouveau_device *dev; |
| 161 | int ret; |
| 162 | |
| 163 | dev = nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS); |
| 164 | ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_NOTIFIEROBJ_ALLOC, |
| 165 | &req, sizeof(req)); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | ntfy->offset = req.offset; |
| 170 | ntfy->object->length = sizeof(*ntfy); |
| 171 | return 0; |
| 172 | } |
| 173 | |
Emil Velikov | 76e9799 | 2015-03-23 21:52:00 +0000 | [diff] [blame] | 174 | drm_private void |
Ben Skeggs | 4a3cbf5 | 2015-11-24 09:17:52 +1000 | [diff] [blame^] | 175 | abi16_delete(struct nouveau_object *obj) |
| 176 | { |
| 177 | struct nouveau_device *dev = |
| 178 | nouveau_object_find(obj, NOUVEAU_DEVICE_CLASS); |
| 179 | if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) { |
| 180 | struct drm_nouveau_channel_free req; |
| 181 | req.channel = obj->handle; |
| 182 | drmCommandWrite(dev->fd, DRM_NOUVEAU_CHANNEL_FREE, |
| 183 | &req, sizeof(req)); |
| 184 | } else { |
| 185 | struct drm_nouveau_gpuobj_free req; |
| 186 | req.channel = obj->parent->handle; |
| 187 | req.handle = obj->handle; |
| 188 | drmCommandWrite(dev->fd, DRM_NOUVEAU_GPUOBJ_FREE, |
| 189 | &req, sizeof(req)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | drm_private bool |
| 194 | abi16_object(struct nouveau_object *obj, int (**func)(struct nouveau_object *)) |
| 195 | { |
| 196 | struct nouveau_object *parent = obj->parent; |
| 197 | |
| 198 | /* nouveau_object::length is (ab)used to determine whether the |
| 199 | * object is a legacy object (!=0), or a real NVIF object. |
| 200 | */ |
| 201 | if ((parent->length != 0 && parent->oclass == NOUVEAU_DEVICE_CLASS)) { |
| 202 | if (obj->oclass == NOUVEAU_FIFO_CHANNEL_CLASS) { |
| 203 | struct nouveau_device *dev = (void *)parent; |
| 204 | if (dev->chipset < 0xc0) |
| 205 | *func = abi16_chan_nv04; |
| 206 | else |
| 207 | if (dev->chipset < 0xe0) |
| 208 | *func = abi16_chan_nvc0; |
| 209 | else |
| 210 | *func = abi16_chan_nve0; |
| 211 | return true; |
| 212 | } |
| 213 | } else |
| 214 | if ((parent->length != 0 && |
| 215 | parent->oclass == NOUVEAU_FIFO_CHANNEL_CLASS)) { |
| 216 | if (obj->oclass == NOUVEAU_NOTIFIER_CLASS) { |
| 217 | *func = abi16_ntfy; |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | *func = abi16_engobj; |
| 222 | return false; /* try NVIF, if supported, before calling func */ |
| 223 | } |
| 224 | |
| 225 | *func = NULL; |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | drm_private void |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 230 | abi16_bo_info(struct nouveau_bo *bo, struct drm_nouveau_gem_info *info) |
| 231 | { |
| 232 | struct nouveau_bo_priv *nvbo = nouveau_bo(bo); |
| 233 | |
| 234 | nvbo->map_handle = info->map_handle; |
| 235 | bo->handle = info->handle; |
| 236 | bo->size = info->size; |
| 237 | bo->offset = info->offset; |
| 238 | |
| 239 | bo->flags = 0; |
| 240 | if (info->domain & NOUVEAU_GEM_DOMAIN_VRAM) |
| 241 | bo->flags |= NOUVEAU_BO_VRAM; |
| 242 | if (info->domain & NOUVEAU_GEM_DOMAIN_GART) |
| 243 | bo->flags |= NOUVEAU_BO_GART; |
| 244 | if (!(info->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG)) |
| 245 | bo->flags |= NOUVEAU_BO_CONTIG; |
| 246 | if (nvbo->map_handle) |
| 247 | bo->flags |= NOUVEAU_BO_MAP; |
| 248 | |
| 249 | if (bo->device->chipset >= 0xc0) { |
| 250 | bo->config.nvc0.memtype = (info->tile_flags & 0xff00) >> 8; |
| 251 | bo->config.nvc0.tile_mode = info->tile_mode; |
| 252 | } else |
| 253 | if (bo->device->chipset >= 0x80 || bo->device->chipset == 0x50) { |
| 254 | bo->config.nv50.memtype = (info->tile_flags & 0x07f00) >> 8 | |
| 255 | (info->tile_flags & 0x30000) >> 9; |
| 256 | bo->config.nv50.tile_mode = info->tile_mode << 4; |
| 257 | } else { |
| 258 | bo->config.nv04.surf_flags = info->tile_flags & 7; |
| 259 | bo->config.nv04.surf_pitch = info->tile_mode; |
| 260 | } |
| 261 | } |
| 262 | |
Emil Velikov | 76e9799 | 2015-03-23 21:52:00 +0000 | [diff] [blame] | 263 | drm_private int |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 264 | abi16_bo_init(struct nouveau_bo *bo, uint32_t alignment, |
| 265 | union nouveau_bo_config *config) |
| 266 | { |
| 267 | struct nouveau_device *dev = bo->device; |
| 268 | struct drm_nouveau_gem_new req = {}; |
| 269 | struct drm_nouveau_gem_info *info = &req.info; |
| 270 | int ret; |
| 271 | |
| 272 | if (bo->flags & NOUVEAU_BO_VRAM) |
| 273 | info->domain |= NOUVEAU_GEM_DOMAIN_VRAM; |
| 274 | if (bo->flags & NOUVEAU_BO_GART) |
| 275 | info->domain |= NOUVEAU_GEM_DOMAIN_GART; |
| 276 | if (!info->domain) |
| 277 | info->domain |= NOUVEAU_GEM_DOMAIN_VRAM | |
| 278 | NOUVEAU_GEM_DOMAIN_GART; |
| 279 | |
| 280 | if (bo->flags & NOUVEAU_BO_MAP) |
| 281 | info->domain |= NOUVEAU_GEM_DOMAIN_MAPPABLE; |
| 282 | |
Alexandre Courbot | a1acffd | 2015-05-21 15:08:28 +0900 | [diff] [blame] | 283 | if (bo->flags & NOUVEAU_BO_COHERENT) |
| 284 | info->domain |= NOUVEAU_GEM_DOMAIN_COHERENT; |
| 285 | |
Ben Skeggs | 292da61 | 2011-12-09 16:11:06 +1000 | [diff] [blame] | 286 | if (!(bo->flags & NOUVEAU_BO_CONTIG)) |
| 287 | info->tile_flags = NOUVEAU_GEM_TILE_NONCONTIG; |
| 288 | |
| 289 | info->size = bo->size; |
| 290 | req.align = alignment; |
| 291 | |
| 292 | if (config) { |
| 293 | if (dev->chipset >= 0xc0) { |
| 294 | info->tile_flags = (config->nvc0.memtype & 0xff) << 8; |
| 295 | info->tile_mode = config->nvc0.tile_mode; |
| 296 | } else |
| 297 | if (dev->chipset >= 0x80 || dev->chipset == 0x50) { |
| 298 | info->tile_flags = (config->nv50.memtype & 0x07f) << 8 | |
| 299 | (config->nv50.memtype & 0x180) << 9; |
| 300 | info->tile_mode = config->nv50.tile_mode >> 4; |
| 301 | } else { |
| 302 | info->tile_flags = config->nv04.surf_flags & 7; |
| 303 | info->tile_mode = config->nv04.surf_pitch; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (!nouveau_device(dev)->have_bo_usage) |
| 308 | info->tile_flags &= 0x0000ff00; |
| 309 | |
| 310 | ret = drmCommandWriteRead(dev->fd, DRM_NOUVEAU_GEM_NEW, |
| 311 | &req, sizeof(req)); |
| 312 | if (ret == 0) |
| 313 | abi16_bo_info(bo, &req.info); |
| 314 | return ret; |
| 315 | } |