|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2022, Microsoft Corporation. |
| 5 | + * |
| 6 | + * Author: |
| 7 | + * Iouri Tarassov <iourit@linux.microsoft.com> |
| 8 | + * |
| 9 | + * Dxgkrnl Graphics Driver |
| 10 | + * Ioctl implementation |
| 11 | + * |
| 12 | + */ |
| 13 | + |
| 14 | +#include <linux/eventfd.h> |
| 15 | +#include <linux/file.h> |
| 16 | +#include <linux/fs.h> |
| 17 | +#include <linux/anon_inodes.h> |
| 18 | +#include <linux/mman.h> |
| 19 | + |
| 20 | +#include "dxgkrnl.h" |
| 21 | +#include "dxgvmbus.h" |
| 22 | +#include "dxgsyncfile.h" |
| 23 | + |
| 24 | +#undef dev_fmt |
| 25 | +#define dev_fmt(fmt) "dxgk: " fmt |
| 26 | + |
| 27 | +#ifdef DEBUG |
| 28 | +static char *errorstr(int ret) |
| 29 | +{ |
| 30 | + return ret < 0 ? "err" : ""; |
| 31 | +} |
| 32 | +#endif |
| 33 | + |
| 34 | +static const struct dma_fence_ops dxgdmafence_ops; |
| 35 | + |
| 36 | +static struct dxgsyncpoint *to_syncpoint(struct dma_fence *fence) |
| 37 | +{ |
| 38 | + if (fence->ops != &dxgdmafence_ops) |
| 39 | + return NULL; |
| 40 | + return container_of(fence, struct dxgsyncpoint, base); |
| 41 | +} |
| 42 | + |
| 43 | +int dxgkio_create_sync_file(struct dxgprocess *process, void *__user inargs) |
| 44 | +{ |
| 45 | + struct d3dkmt_createsyncfile args; |
| 46 | + struct dxgsyncpoint *pt = NULL; |
| 47 | + int ret = 0; |
| 48 | + int fd = get_unused_fd_flags(O_CLOEXEC); |
| 49 | + struct sync_file *sync_file = NULL; |
| 50 | + struct dxgdevice *device = NULL; |
| 51 | + struct dxgadapter *adapter = NULL; |
| 52 | + struct d3dkmt_waitforsynchronizationobjectfromcpu waitargs = {}; |
| 53 | + |
| 54 | + if (fd < 0) { |
| 55 | + DXG_ERR("get_unused_fd_flags failed: %d", fd); |
| 56 | + ret = fd; |
| 57 | + goto cleanup; |
| 58 | + } |
| 59 | + |
| 60 | + ret = copy_from_user(&args, inargs, sizeof(args)); |
| 61 | + if (ret) { |
| 62 | + DXG_ERR("failed to copy input args"); |
| 63 | + ret = -EFAULT; |
| 64 | + goto cleanup; |
| 65 | + } |
| 66 | + |
| 67 | + device = dxgprocess_device_by_handle(process, args.device); |
| 68 | + if (device == NULL) { |
| 69 | + DXG_ERR("dxgprocess_device_by_handle failed"); |
| 70 | + ret = -EINVAL; |
| 71 | + goto cleanup; |
| 72 | + } |
| 73 | + |
| 74 | + ret = dxgdevice_acquire_lock_shared(device); |
| 75 | + if (ret < 0) { |
| 76 | + DXG_ERR("dxgdevice_acquire_lock_shared failed"); |
| 77 | + device = NULL; |
| 78 | + goto cleanup; |
| 79 | + } |
| 80 | + |
| 81 | + adapter = device->adapter; |
| 82 | + ret = dxgadapter_acquire_lock_shared(adapter); |
| 83 | + if (ret < 0) { |
| 84 | + DXG_ERR("dxgadapter_acquire_lock_shared failed"); |
| 85 | + adapter = NULL; |
| 86 | + goto cleanup; |
| 87 | + } |
| 88 | + |
| 89 | + pt = kzalloc(sizeof(*pt), GFP_KERNEL); |
| 90 | + if (!pt) { |
| 91 | + ret = -ENOMEM; |
| 92 | + goto cleanup; |
| 93 | + } |
| 94 | + spin_lock_init(&pt->lock); |
| 95 | + pt->fence_value = args.fence_value; |
| 96 | + pt->context = dma_fence_context_alloc(1); |
| 97 | + pt->hdr.event_id = dxgglobal_new_host_event_id(); |
| 98 | + pt->hdr.event_type = dxghostevent_dma_fence; |
| 99 | + dxgglobal_add_host_event(&pt->hdr); |
| 100 | + |
| 101 | + dma_fence_init(&pt->base, &dxgdmafence_ops, &pt->lock, |
| 102 | + pt->context, args.fence_value); |
| 103 | + |
| 104 | + sync_file = sync_file_create(&pt->base); |
| 105 | + if (sync_file == NULL) { |
| 106 | + DXG_ERR("sync_file_create failed"); |
| 107 | + ret = -ENOMEM; |
| 108 | + goto cleanup; |
| 109 | + } |
| 110 | + dma_fence_put(&pt->base); |
| 111 | + |
| 112 | + waitargs.device = args.device; |
| 113 | + waitargs.object_count = 1; |
| 114 | + waitargs.objects = &args.monitored_fence; |
| 115 | + waitargs.fence_values = &args.fence_value; |
| 116 | + ret = dxgvmb_send_wait_sync_object_cpu(process, adapter, |
| 117 | + &waitargs, false, |
| 118 | + pt->hdr.event_id); |
| 119 | + if (ret < 0) { |
| 120 | + DXG_ERR("dxgvmb_send_wait_sync_object_cpu failed"); |
| 121 | + goto cleanup; |
| 122 | + } |
| 123 | + |
| 124 | + args.sync_file_handle = (u64)fd; |
| 125 | + ret = copy_to_user(inargs, &args, sizeof(args)); |
| 126 | + if (ret) { |
| 127 | + DXG_ERR("failed to copy output args"); |
| 128 | + ret = -EFAULT; |
| 129 | + goto cleanup; |
| 130 | + } |
| 131 | + |
| 132 | + fd_install(fd, sync_file->file); |
| 133 | + |
| 134 | +cleanup: |
| 135 | + if (adapter) |
| 136 | + dxgadapter_release_lock_shared(adapter); |
| 137 | + if (device) |
| 138 | + dxgdevice_release_lock_shared(device); |
| 139 | + if (ret) { |
| 140 | + if (sync_file) { |
| 141 | + fput(sync_file->file); |
| 142 | + /* sync_file_release will destroy dma_fence */ |
| 143 | + pt = NULL; |
| 144 | + } |
| 145 | + if (pt) |
| 146 | + dma_fence_put(&pt->base); |
| 147 | + if (fd >= 0) |
| 148 | + put_unused_fd(fd); |
| 149 | + } |
| 150 | + DXG_TRACE("ioctl:%s %d", errorstr(ret), ret); |
| 151 | + return ret; |
| 152 | +} |
| 153 | + |
| 154 | +static const char *dxgdmafence_get_driver_name(struct dma_fence *fence) |
| 155 | +{ |
| 156 | + return "dxgkrnl"; |
| 157 | +} |
| 158 | + |
| 159 | +static const char *dxgdmafence_get_timeline_name(struct dma_fence *fence) |
| 160 | +{ |
| 161 | + return "no_timeline"; |
| 162 | +} |
| 163 | + |
| 164 | +static void dxgdmafence_release(struct dma_fence *fence) |
| 165 | +{ |
| 166 | + struct dxgsyncpoint *syncpoint; |
| 167 | + |
| 168 | + syncpoint = to_syncpoint(fence); |
| 169 | + if (syncpoint) { |
| 170 | + if (syncpoint->hdr.event_id) |
| 171 | + dxgglobal_get_host_event(syncpoint->hdr.event_id); |
| 172 | + kfree(syncpoint); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +static bool dxgdmafence_signaled(struct dma_fence *fence) |
| 177 | +{ |
| 178 | + struct dxgsyncpoint *syncpoint; |
| 179 | + |
| 180 | + syncpoint = to_syncpoint(fence); |
| 181 | + if (syncpoint == 0) |
| 182 | + return true; |
| 183 | + return __dma_fence_is_later(syncpoint->fence_value, fence->seqno, |
| 184 | + fence->ops); |
| 185 | +} |
| 186 | + |
| 187 | +static bool dxgdmafence_enable_signaling(struct dma_fence *fence) |
| 188 | +{ |
| 189 | + return true; |
| 190 | +} |
| 191 | + |
| 192 | +static void dxgdmafence_value_str(struct dma_fence *fence, |
| 193 | + char *str, int size) |
| 194 | +{ |
| 195 | + snprintf(str, size, "%lld", fence->seqno); |
| 196 | +} |
| 197 | + |
| 198 | +static void dxgdmafence_timeline_value_str(struct dma_fence *fence, |
| 199 | + char *str, int size) |
| 200 | +{ |
| 201 | + struct dxgsyncpoint *syncpoint; |
| 202 | + |
| 203 | + syncpoint = to_syncpoint(fence); |
| 204 | + snprintf(str, size, "%lld", syncpoint->fence_value); |
| 205 | +} |
| 206 | + |
| 207 | +static const struct dma_fence_ops dxgdmafence_ops = { |
| 208 | + .get_driver_name = dxgdmafence_get_driver_name, |
| 209 | + .get_timeline_name = dxgdmafence_get_timeline_name, |
| 210 | + .enable_signaling = dxgdmafence_enable_signaling, |
| 211 | + .signaled = dxgdmafence_signaled, |
| 212 | + .release = dxgdmafence_release, |
| 213 | + .fence_value_str = dxgdmafence_value_str, |
| 214 | + .timeline_value_str = dxgdmafence_timeline_value_str, |
| 215 | +}; |
0 commit comments