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