1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2020 NVIDIA Corporation */ 3 4 #include <linux/host1x.h> 5 #include <linux/iommu.h> 6 #include <linux/list.h> 7 8 #include <drm/drm_drv.h> 9 #include <drm/drm_file.h> 10 #include <drm/drm_timeout.h> 11 12 #include "drm.h" 13 #include "uapi.h" 14 15 static void tegra_drm_mapping_release(struct kref *ref) 16 { 17 struct tegra_drm_mapping *mapping = 18 container_of(ref, struct tegra_drm_mapping, ref); 19 20 if (mapping->ctx_map) 21 host1x_memory_context_unmap(mapping->ctx_map); 22 else 23 host1x_bo_unpin(mapping->bo_map); 24 25 host1x_bo_put(mapping->bo); 26 27 kfree(mapping); 28 } 29 30 void tegra_drm_mapping_put(struct tegra_drm_mapping *mapping) 31 { 32 kref_put(&mapping->ref, tegra_drm_mapping_release); 33 } 34 35 static void tegra_drm_channel_context_close(struct tegra_drm_context *context) 36 { 37 struct tegra_drm_mapping *mapping; 38 unsigned long id; 39 40 xa_for_each(&context->mappings, id, mapping) 41 tegra_drm_mapping_put(mapping); 42 43 if (context->memory_context) 44 host1x_memory_context_put(context->memory_context); 45 46 xa_destroy(&context->mappings); 47 48 host1x_channel_put(context->channel); 49 50 kfree(context); 51 } 52 53 void tegra_drm_uapi_close_file(struct tegra_drm_file *file) 54 { 55 struct tegra_drm_context *context; 56 struct host1x_syncpt *sp; 57 unsigned long id; 58 59 xa_for_each(&file->contexts, id, context) 60 tegra_drm_channel_context_close(context); 61 62 xa_for_each(&file->syncpoints, id, sp) 63 host1x_syncpt_put(sp); 64 65 xa_destroy(&file->contexts); 66 xa_destroy(&file->syncpoints); 67 } 68 69 static struct tegra_drm_client *tegra_drm_find_client(struct tegra_drm *tegra, u32 class) 70 { 71 struct tegra_drm_client *client; 72 73 list_for_each_entry(client, &tegra->clients, list) 74 if (client->base.class == class) 75 return client; 76 77 return NULL; 78 } 79 80 int tegra_drm_ioctl_channel_open(struct drm_device *drm, void *data, struct drm_file *file) 81 { 82 struct host1x *host = tegra_drm_to_host1x(drm->dev_private); 83 struct tegra_drm_file *fpriv = file->driver_priv; 84 struct tegra_drm *tegra = drm->dev_private; 85 struct drm_tegra_channel_open *args = data; 86 struct tegra_drm_client *client = NULL; 87 struct tegra_drm_context *context; 88 int err; 89 90 if (args->flags) 91 return -EINVAL; 92 93 context = kzalloc_obj(*context); 94 if (!context) 95 return -ENOMEM; 96 97 client = tegra_drm_find_client(tegra, args->host1x_class); 98 if (!client) { 99 err = -ENODEV; 100 goto free; 101 } 102 103 if (client->shared_channel) { 104 context->channel = host1x_channel_get(client->shared_channel); 105 } else { 106 context->channel = host1x_channel_request(&client->base); 107 if (!context->channel) { 108 err = -EBUSY; 109 goto free; 110 } 111 } 112 113 /* Only allocate context if the engine supports context isolation. */ 114 if (device_iommu_mapped(client->base.dev) && client->ops->can_use_memory_ctx) { 115 bool supported; 116 117 err = client->ops->can_use_memory_ctx(client, &supported); 118 if (err) 119 goto put_channel; 120 121 if (supported) { 122 struct pid *pid = get_task_pid(current, PIDTYPE_TGID); 123 context->memory_context = host1x_memory_context_alloc( 124 host, client->base.dev, pid); 125 put_pid(pid); 126 } 127 128 if (IS_ERR(context->memory_context)) { 129 if (PTR_ERR(context->memory_context) != -EOPNOTSUPP) { 130 err = PTR_ERR(context->memory_context); 131 goto put_channel; 132 } else { 133 /* 134 * OK, HW does not support contexts or contexts 135 * are disabled. 136 */ 137 context->memory_context = NULL; 138 } 139 } 140 } 141 142 err = xa_alloc(&fpriv->contexts, &args->context, context, XA_LIMIT(1, U32_MAX), 143 GFP_KERNEL); 144 if (err < 0) 145 goto put_memctx; 146 147 context->client = client; 148 xa_init_flags(&context->mappings, XA_FLAGS_ALLOC1); 149 150 args->version = client->version; 151 args->capabilities = 0; 152 153 if (device_get_dma_attr(client->base.dev) == DEV_DMA_COHERENT) 154 args->capabilities |= DRM_TEGRA_CHANNEL_CAP_CACHE_COHERENT; 155 156 return 0; 157 158 put_memctx: 159 if (context->memory_context) 160 host1x_memory_context_put(context->memory_context); 161 put_channel: 162 host1x_channel_put(context->channel); 163 free: 164 kfree(context); 165 166 return err; 167 } 168 169 int tegra_drm_ioctl_channel_close(struct drm_device *drm, void *data, struct drm_file *file) 170 { 171 struct tegra_drm_file *fpriv = file->driver_priv; 172 struct drm_tegra_channel_close *args = data; 173 struct tegra_drm_context *context; 174 175 mutex_lock(&fpriv->lock); 176 177 context = xa_load(&fpriv->contexts, args->context); 178 if (!context) { 179 mutex_unlock(&fpriv->lock); 180 return -EINVAL; 181 } 182 183 xa_erase(&fpriv->contexts, args->context); 184 185 mutex_unlock(&fpriv->lock); 186 187 tegra_drm_channel_context_close(context); 188 189 return 0; 190 } 191 192 int tegra_drm_ioctl_channel_map(struct drm_device *drm, void *data, struct drm_file *file) 193 { 194 struct tegra_drm_file *fpriv = file->driver_priv; 195 struct drm_tegra_channel_map *args = data; 196 struct tegra_drm_mapping *mapping; 197 struct tegra_drm_context *context; 198 enum dma_data_direction direction; 199 int err = 0; 200 201 if (args->flags & ~DRM_TEGRA_CHANNEL_MAP_READ_WRITE) 202 return -EINVAL; 203 204 mutex_lock(&fpriv->lock); 205 206 context = xa_load(&fpriv->contexts, args->context); 207 if (!context) { 208 mutex_unlock(&fpriv->lock); 209 return -EINVAL; 210 } 211 212 mapping = kzalloc_obj(*mapping); 213 if (!mapping) { 214 err = -ENOMEM; 215 goto unlock; 216 } 217 218 kref_init(&mapping->ref); 219 220 mapping->bo = tegra_gem_lookup(file, args->handle); 221 if (!mapping->bo) { 222 err = -EINVAL; 223 goto free; 224 } 225 226 switch (args->flags & DRM_TEGRA_CHANNEL_MAP_READ_WRITE) { 227 case DRM_TEGRA_CHANNEL_MAP_READ_WRITE: 228 direction = DMA_BIDIRECTIONAL; 229 break; 230 231 case DRM_TEGRA_CHANNEL_MAP_WRITE: 232 direction = DMA_FROM_DEVICE; 233 break; 234 235 case DRM_TEGRA_CHANNEL_MAP_READ: 236 direction = DMA_TO_DEVICE; 237 break; 238 239 default: 240 err = -EINVAL; 241 goto put_gem; 242 } 243 244 if (context->memory_context) { 245 mapping->ctx_map = host1x_memory_context_map(context->memory_context, 246 mapping->bo, direction); 247 248 if (IS_ERR(mapping->ctx_map)) { 249 err = PTR_ERR(mapping->ctx_map); 250 goto put_gem; 251 } 252 } else { 253 mapping->bo_map = host1x_bo_pin(context->client->base.dev, 254 mapping->bo, direction, NULL); 255 256 if (IS_ERR(mapping->bo_map)) { 257 err = PTR_ERR(mapping->bo_map); 258 goto put_gem; 259 } 260 261 mapping->iova = mapping->bo_map->phys; 262 mapping->iova_end = mapping->iova + host1x_to_tegra_bo(mapping->bo)->gem.size; 263 } 264 265 err = xa_alloc(&context->mappings, &args->mapping, mapping, XA_LIMIT(1, U32_MAX), 266 GFP_KERNEL); 267 if (err < 0) 268 goto unpin; 269 270 mutex_unlock(&fpriv->lock); 271 272 return 0; 273 274 unpin: 275 if (mapping->ctx_map) 276 host1x_memory_context_unmap(mapping->ctx_map); 277 else 278 host1x_bo_unpin(mapping->bo_map); 279 put_gem: 280 host1x_bo_put(mapping->bo); 281 free: 282 kfree(mapping); 283 unlock: 284 mutex_unlock(&fpriv->lock); 285 return err; 286 } 287 288 int tegra_drm_ioctl_channel_unmap(struct drm_device *drm, void *data, struct drm_file *file) 289 { 290 struct tegra_drm_file *fpriv = file->driver_priv; 291 struct drm_tegra_channel_unmap *args = data; 292 struct tegra_drm_mapping *mapping; 293 struct tegra_drm_context *context; 294 295 mutex_lock(&fpriv->lock); 296 297 context = xa_load(&fpriv->contexts, args->context); 298 if (!context) { 299 mutex_unlock(&fpriv->lock); 300 return -EINVAL; 301 } 302 303 mapping = xa_erase(&context->mappings, args->mapping); 304 305 mutex_unlock(&fpriv->lock); 306 307 if (!mapping) 308 return -EINVAL; 309 310 tegra_drm_mapping_put(mapping); 311 return 0; 312 } 313 314 int tegra_drm_ioctl_syncpoint_allocate(struct drm_device *drm, void *data, struct drm_file *file) 315 { 316 struct host1x *host1x = tegra_drm_to_host1x(drm->dev_private); 317 struct tegra_drm_file *fpriv = file->driver_priv; 318 struct drm_tegra_syncpoint_allocate *args = data; 319 struct host1x_syncpt *sp; 320 int err; 321 322 if (args->id) 323 return -EINVAL; 324 325 sp = host1x_syncpt_alloc(host1x, HOST1X_SYNCPT_CLIENT_MANAGED, current->comm); 326 if (!sp) 327 return -EBUSY; 328 329 args->id = host1x_syncpt_id(sp); 330 331 err = xa_insert(&fpriv->syncpoints, args->id, sp, GFP_KERNEL); 332 if (err) { 333 host1x_syncpt_put(sp); 334 return err; 335 } 336 337 return 0; 338 } 339 340 int tegra_drm_ioctl_syncpoint_free(struct drm_device *drm, void *data, struct drm_file *file) 341 { 342 struct tegra_drm_file *fpriv = file->driver_priv; 343 struct drm_tegra_syncpoint_allocate *args = data; 344 struct host1x_syncpt *sp; 345 346 mutex_lock(&fpriv->lock); 347 sp = xa_erase(&fpriv->syncpoints, args->id); 348 mutex_unlock(&fpriv->lock); 349 350 if (!sp) 351 return -EINVAL; 352 353 host1x_syncpt_put(sp); 354 355 return 0; 356 } 357 358 int tegra_drm_ioctl_syncpoint_wait(struct drm_device *drm, void *data, struct drm_file *file) 359 { 360 struct host1x *host1x = tegra_drm_to_host1x(drm->dev_private); 361 struct drm_tegra_syncpoint_wait *args = data; 362 signed long timeout_jiffies; 363 struct host1x_syncpt *sp; 364 365 if (args->padding != 0) 366 return -EINVAL; 367 368 sp = host1x_syncpt_get_by_id_noref(host1x, args->id); 369 if (!sp) 370 return -EINVAL; 371 372 timeout_jiffies = drm_timeout_abs_to_jiffies(args->timeout_ns); 373 374 return host1x_syncpt_wait(sp, args->threshold, timeout_jiffies, &args->value); 375 } 376