Skip to content

Commit 5dbd071

Browse files
Iouri Tarassovchessturo
authored andcommitted
drivers: hv: dxgkrnl: Map(unmap) CPU address to device allocation
Implement ioctls to map/unmap CPU virtual addresses to compute device allocations - LX_DXLOCK2 and LX_DXUNLOCK2. The LX_DXLOCK2 ioctl maps a CPU virtual address to a compute device allocation. The allocation could be located in system memory or local device memory on the host. When the device allocation is created from the guest system memory (existing sysmem allocation), the allocation CPU address is known and is returned to the caller. For other CPU visible allocations the code flow is the following: 1. A VM bus message is sent to the host to map the allocation 2. The host allocates a portion of the guest IO space and maps it to the allocation backing store. The IO space address of the allocation is returned back to the guest. 3. The guest allocates a CPU virtual address and maps it to the IO space (see the dxg_map_iospace function). 4. The CPU VA is returned back to the caller cpu_address_mapped and cpu_address_refcount are used to track how many times an allocation was mapped. The LX_DXUNLOCK2 ioctl unmaps a CPU virtual address from a compute device allocation. Signed-off-by: Iouri Tarassov <iourit@linux.microsoft.com> [kms: forward port to 6.6 from 6.1. No code changes made.] Signed-off-by: Kelsey Steele <kelseysteele@microsoft.com>
1 parent 899af4b commit 5dbd071

6 files changed

Lines changed: 339 additions & 2 deletions

File tree

drivers/hv/dxgkrnl/dxgadapter.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,15 @@ void dxgallocation_stop(struct dxgallocation *alloc)
885885
vfree(alloc->pages);
886886
alloc->pages = NULL;
887887
}
888+
dxgprocess_ht_lock_exclusive_down(alloc->process);
889+
if (alloc->cpu_address_mapped) {
890+
dxg_unmap_iospace(alloc->cpu_address,
891+
alloc->num_pages << PAGE_SHIFT);
892+
alloc->cpu_address_mapped = false;
893+
alloc->cpu_address = NULL;
894+
alloc->cpu_address_refcount = 0;
895+
}
896+
dxgprocess_ht_lock_exclusive_up(alloc->process);
888897
}
889898

890899
void dxgallocation_free_handle(struct dxgallocation *alloc)
@@ -932,6 +941,8 @@ else
932941
#endif
933942
if (alloc->priv_drv_data)
934943
vfree(alloc->priv_drv_data);
944+
if (alloc->cpu_address_mapped)
945+
pr_err("Alloc IO space is mapped: %p", alloc);
935946
kfree(alloc);
936947
}
937948

drivers/hv/dxgkrnl/dxgkrnl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ struct dxgallocation {
708708
struct d3dkmthandle alloc_handle;
709709
/* Set to 1 when allocation belongs to resource. */
710710
u32 resource_owner:1;
711+
/* Set to 1 when 'cpu_address' is mapped to the IO space. */
712+
u32 cpu_address_mapped:1;
711713
/* Set to 1 when the allocatio is mapped as cached */
712714
u32 cached:1;
713715
u32 handle_valid:1;
@@ -719,6 +721,11 @@ struct dxgallocation {
719721
#endif
720722
/* Number of pages in the 'pages' array */
721723
u32 num_pages;
724+
/*
725+
* How many times dxgk_lock2 is called to allocation, which is mapped
726+
* to IO space.
727+
*/
728+
u32 cpu_address_refcount;
722729
/*
723730
* CPU address from the existing sysmem allocation, or
724731
* mapped to the CPU visible backing store in the IO space
@@ -837,6 +844,13 @@ int dxgvmb_send_wait_sync_object_cpu(struct dxgprocess *process,
837844
d3dkmt_waitforsynchronizationobjectfromcpu
838845
*args,
839846
u64 cpu_event);
847+
int dxgvmb_send_lock2(struct dxgprocess *process,
848+
struct dxgadapter *adapter,
849+
struct d3dkmt_lock2 *args,
850+
struct d3dkmt_lock2 *__user outargs);
851+
int dxgvmb_send_unlock2(struct dxgprocess *process,
852+
struct dxgadapter *adapter,
853+
struct d3dkmt_unlock2 *args);
840854
int dxgvmb_send_create_hwqueue(struct dxgprocess *process,
841855
struct dxgadapter *adapter,
842856
struct d3dkmt_createhwqueue *args,

drivers/hv/dxgkrnl/dxgvmbus.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,113 @@ int dxgvmb_send_wait_sync_object_gpu(struct dxgprocess *process,
23542354
return ret;
23552355
}
23562356

2357+
int dxgvmb_send_lock2(struct dxgprocess *process,
2358+
struct dxgadapter *adapter,
2359+
struct d3dkmt_lock2 *args,
2360+
struct d3dkmt_lock2 *__user outargs)
2361+
{
2362+
int ret;
2363+
struct dxgkvmb_command_lock2 *command;
2364+
struct dxgkvmb_command_lock2_return result = { };
2365+
struct dxgallocation *alloc = NULL;
2366+
struct dxgvmbusmsg msg = {.hdr = NULL};
2367+
2368+
ret = init_message(&msg, adapter, process, sizeof(*command));
2369+
if (ret)
2370+
goto cleanup;
2371+
command = (void *)msg.msg;
2372+
2373+
command_vgpu_to_host_init2(&command->hdr,
2374+
DXGK_VMBCOMMAND_LOCK2, process->host_handle);
2375+
command->args = *args;
2376+
2377+
ret = dxgvmb_send_sync_msg(msg.channel, msg.hdr, msg.size,
2378+
&result, sizeof(result));
2379+
if (ret < 0)
2380+
goto cleanup;
2381+
2382+
ret = ntstatus2int(result.status);
2383+
if (ret < 0)
2384+
goto cleanup;
2385+
2386+
hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
2387+
alloc = hmgrtable_get_object_by_type(&process->handle_table,
2388+
HMGRENTRY_TYPE_DXGALLOCATION,
2389+
args->allocation);
2390+
if (alloc == NULL) {
2391+
DXG_ERR("invalid alloc");
2392+
ret = -EINVAL;
2393+
} else {
2394+
if (alloc->cpu_address) {
2395+
args->data = alloc->cpu_address;
2396+
if (alloc->cpu_address_mapped)
2397+
alloc->cpu_address_refcount++;
2398+
} else {
2399+
u64 offset = (u64)result.cpu_visible_buffer_offset;
2400+
2401+
args->data = dxg_map_iospace(offset,
2402+
alloc->num_pages << PAGE_SHIFT,
2403+
PROT_READ | PROT_WRITE, alloc->cached);
2404+
if (args->data) {
2405+
alloc->cpu_address_refcount = 1;
2406+
alloc->cpu_address_mapped = true;
2407+
alloc->cpu_address = args->data;
2408+
}
2409+
}
2410+
if (args->data == NULL) {
2411+
ret = -ENOMEM;
2412+
} else {
2413+
ret = copy_to_user(&outargs->data, &args->data,
2414+
sizeof(args->data));
2415+
if (ret) {
2416+
DXG_ERR("failed to copy data");
2417+
ret = -EINVAL;
2418+
alloc->cpu_address_refcount--;
2419+
if (alloc->cpu_address_refcount == 0) {
2420+
dxg_unmap_iospace(alloc->cpu_address,
2421+
alloc->num_pages << PAGE_SHIFT);
2422+
alloc->cpu_address_mapped = false;
2423+
alloc->cpu_address = NULL;
2424+
}
2425+
}
2426+
}
2427+
}
2428+
hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
2429+
2430+
cleanup:
2431+
free_message(&msg, process);
2432+
if (ret)
2433+
DXG_TRACE("err: %d", ret);
2434+
return ret;
2435+
}
2436+
2437+
int dxgvmb_send_unlock2(struct dxgprocess *process,
2438+
struct dxgadapter *adapter,
2439+
struct d3dkmt_unlock2 *args)
2440+
{
2441+
int ret;
2442+
struct dxgkvmb_command_unlock2 *command;
2443+
struct dxgvmbusmsg msg = {.hdr = NULL};
2444+
2445+
ret = init_message(&msg, adapter, process, sizeof(*command));
2446+
if (ret)
2447+
goto cleanup;
2448+
command = (void *)msg.msg;
2449+
2450+
command_vgpu_to_host_init2(&command->hdr,
2451+
DXGK_VMBCOMMAND_UNLOCK2,
2452+
process->host_handle);
2453+
command->args = *args;
2454+
2455+
ret = dxgvmb_send_sync_msg_ntstatus(msg.channel, msg.hdr, msg.size);
2456+
2457+
cleanup:
2458+
free_message(&msg, process);
2459+
if (ret)
2460+
DXG_TRACE("err: %d", ret);
2461+
return ret;
2462+
}
2463+
23572464
int dxgvmb_send_create_hwqueue(struct dxgprocess *process,
23582465
struct dxgadapter *adapter,
23592466
struct d3dkmt_createhwqueue *args,

drivers/hv/dxgkrnl/dxgvmbus.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,25 @@ struct dxgkvmb_command_waitforsyncobjectfromgpu {
570570
/* struct d3dkmthandle ObjectHandles[object_count] */
571571
};
572572

573+
struct dxgkvmb_command_lock2 {
574+
struct dxgkvmb_command_vgpu_to_host hdr;
575+
struct d3dkmt_lock2 args;
576+
bool use_legacy_lock;
577+
u32 flags;
578+
u32 priv_drv_data;
579+
};
580+
581+
struct dxgkvmb_command_lock2_return {
582+
struct ntstatus status;
583+
void *cpu_visible_buffer_offset;
584+
};
585+
586+
struct dxgkvmb_command_unlock2 {
587+
struct dxgkvmb_command_vgpu_to_host hdr;
588+
struct d3dkmt_unlock2 args;
589+
bool use_legacy_unlock;
590+
};
591+
573592
/* Returns the same structure */
574593
struct dxgkvmb_command_createhwqueue {
575594
struct dxgkvmb_command_vgpu_to_host hdr;

drivers/hv/dxgkrnl/ioctl.c

Lines changed: 158 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3142,6 +3142,162 @@ dxgkio_wait_sync_object_gpu(struct dxgprocess *process, void *__user inargs)
31423142
return ret;
31433143
}
31443144

3145+
static int
3146+
dxgkio_lock2(struct dxgprocess *process, void *__user inargs)
3147+
{
3148+
struct d3dkmt_lock2 args;
3149+
struct d3dkmt_lock2 *__user result = inargs;
3150+
int ret;
3151+
struct dxgadapter *adapter = NULL;
3152+
struct dxgdevice *device = NULL;
3153+
struct dxgallocation *alloc = NULL;
3154+
3155+
ret = copy_from_user(&args, inargs, sizeof(args));
3156+
if (ret) {
3157+
DXG_ERR("failed to copy input args");
3158+
ret = -EINVAL;
3159+
goto cleanup;
3160+
}
3161+
3162+
args.data = NULL;
3163+
hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
3164+
alloc = hmgrtable_get_object_by_type(&process->handle_table,
3165+
HMGRENTRY_TYPE_DXGALLOCATION,
3166+
args.allocation);
3167+
if (alloc == NULL) {
3168+
ret = -EINVAL;
3169+
} else {
3170+
if (alloc->cpu_address) {
3171+
ret = copy_to_user(&result->data,
3172+
&alloc->cpu_address,
3173+
sizeof(args.data));
3174+
if (ret == 0) {
3175+
args.data = alloc->cpu_address;
3176+
if (alloc->cpu_address_mapped)
3177+
alloc->cpu_address_refcount++;
3178+
} else {
3179+
DXG_ERR("Failed to copy cpu address");
3180+
ret = -EINVAL;
3181+
}
3182+
}
3183+
}
3184+
hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
3185+
if (ret < 0)
3186+
goto cleanup;
3187+
if (args.data)
3188+
goto success;
3189+
3190+
/*
3191+
* The call acquires reference on the device. It is safe to access the
3192+
* adapter, because the device holds reference on it.
3193+
*/
3194+
device = dxgprocess_device_by_handle(process, args.device);
3195+
if (device == NULL) {
3196+
ret = -EINVAL;
3197+
goto cleanup;
3198+
}
3199+
adapter = device->adapter;
3200+
ret = dxgadapter_acquire_lock_shared(adapter);
3201+
if (ret < 0) {
3202+
adapter = NULL;
3203+
goto cleanup;
3204+
}
3205+
3206+
ret = dxgvmb_send_lock2(process, adapter, &args, result);
3207+
3208+
cleanup:
3209+
3210+
if (adapter)
3211+
dxgadapter_release_lock_shared(adapter);
3212+
3213+
if (device)
3214+
kref_put(&device->device_kref, dxgdevice_release);
3215+
3216+
success:
3217+
DXG_TRACE("ioctl:%s %s %d", errorstr(ret), __func__, ret);
3218+
return ret;
3219+
}
3220+
3221+
static int
3222+
dxgkio_unlock2(struct dxgprocess *process, void *__user inargs)
3223+
{
3224+
struct d3dkmt_unlock2 args;
3225+
int ret;
3226+
struct dxgadapter *adapter = NULL;
3227+
struct dxgdevice *device = NULL;
3228+
struct dxgallocation *alloc = NULL;
3229+
bool done = false;
3230+
3231+
ret = copy_from_user(&args, inargs, sizeof(args));
3232+
if (ret) {
3233+
DXG_ERR("failed to copy input args");
3234+
ret = -EINVAL;
3235+
goto cleanup;
3236+
}
3237+
3238+
hmgrtable_lock(&process->handle_table, DXGLOCK_EXCL);
3239+
alloc = hmgrtable_get_object_by_type(&process->handle_table,
3240+
HMGRENTRY_TYPE_DXGALLOCATION,
3241+
args.allocation);
3242+
if (alloc == NULL) {
3243+
ret = -EINVAL;
3244+
} else {
3245+
if (alloc->cpu_address == NULL) {
3246+
DXG_ERR("Allocation is not locked: %p", alloc);
3247+
ret = -EINVAL;
3248+
} else if (alloc->cpu_address_mapped) {
3249+
if (alloc->cpu_address_refcount > 0) {
3250+
alloc->cpu_address_refcount--;
3251+
if (alloc->cpu_address_refcount != 0) {
3252+
done = true;
3253+
} else {
3254+
dxg_unmap_iospace(alloc->cpu_address,
3255+
alloc->num_pages << PAGE_SHIFT);
3256+
alloc->cpu_address_mapped = false;
3257+
alloc->cpu_address = NULL;
3258+
}
3259+
} else {
3260+
DXG_ERR("Invalid cpu access refcount");
3261+
done = true;
3262+
}
3263+
}
3264+
}
3265+
hmgrtable_unlock(&process->handle_table, DXGLOCK_EXCL);
3266+
if (done)
3267+
goto success;
3268+
if (ret < 0)
3269+
goto cleanup;
3270+
3271+
/*
3272+
* The call acquires reference on the device. It is safe to access the
3273+
* adapter, because the device holds reference on it.
3274+
*/
3275+
device = dxgprocess_device_by_handle(process, args.device);
3276+
if (device == NULL) {
3277+
ret = -EINVAL;
3278+
goto cleanup;
3279+
}
3280+
adapter = device->adapter;
3281+
ret = dxgadapter_acquire_lock_shared(adapter);
3282+
if (ret < 0) {
3283+
adapter = NULL;
3284+
goto cleanup;
3285+
}
3286+
3287+
ret = dxgvmb_send_unlock2(process, adapter, &args);
3288+
3289+
cleanup:
3290+
if (adapter)
3291+
dxgadapter_release_lock_shared(adapter);
3292+
3293+
if (device)
3294+
kref_put(&device->device_kref, dxgdevice_release);
3295+
3296+
success:
3297+
DXG_TRACE("ioctl:%s %s %d", errorstr(ret), __func__, ret);
3298+
return ret;
3299+
}
3300+
31453301
static int
31463302
dxgkio_get_device_state(struct dxgprocess *process, void *__user inargs)
31473303
{
@@ -3909,7 +4065,7 @@ static struct ioctl_desc ioctls[] = {
39094065
/* 0x22 */ {},
39104066
/* 0x23 */ {},
39114067
/* 0x24 */ {},
3912-
/* 0x25 */ {},
4068+
/* 0x25 */ {dxgkio_lock2, LX_DXLOCK2},
39134069
/* 0x26 */ {},
39144070
/* 0x27 */ {},
39154071
/* 0x28 */ {},
@@ -3932,7 +4088,7 @@ static struct ioctl_desc ioctls[] = {
39324088
LX_DXSUBMITSIGNALSYNCOBJECTSTOHWQUEUE},
39334089
/* 0x36 */ {dxgkio_submit_wait_to_hwqueue,
39344090
LX_DXSUBMITWAITFORSYNCOBJECTSTOHWQUEUE},
3935-
/* 0x37 */ {},
4091+
/* 0x37 */ {dxgkio_unlock2, LX_DXUNLOCK2},
39364092
/* 0x38 */ {},
39374093
/* 0x39 */ {},
39384094
/* 0x3a */ {dxgkio_wait_sync_object_cpu,

0 commit comments

Comments
 (0)