diff --git a/drivers/gpu/nvgpu/gk20a/ctrl_gk20a.c b/drivers/gpu/nvgpu/gk20a/ctrl_gk20a.c index 6b8326708..2fdf719ac 100644 --- a/drivers/gpu/nvgpu/gk20a/ctrl_gk20a.c +++ b/drivers/gpu/nvgpu/gk20a/ctrl_gk20a.c @@ -747,6 +747,24 @@ static int nvgpu_gpu_alloc_vidmem(struct gk20a *g, return 0; } +static int nvgpu_gpu_get_memory_state(struct gk20a *g, + struct nvgpu_gpu_get_memory_state_args *args) +{ + int err; + + gk20a_dbg_fn(""); + + if (args->reserved[0] || args->reserved[1] || + args->reserved[2] || args->reserved[3]) + return -EINVAL; + + err = gk20a_vidmem_get_space(g, &args->total_free_bytes); + + gk20a_dbg_fn("done, err=%d, bytes=%lld", err, args->total_free_bytes); + + return err; +} + long gk20a_ctrl_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct device *dev = filp->private_data; @@ -999,6 +1017,11 @@ long gk20a_ctrl_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg (struct nvgpu_gpu_alloc_vidmem_args *)buf); break; + case NVGPU_GPU_IOCTL_GET_MEMORY_STATE: + err = nvgpu_gpu_get_memory_state(g, + (struct nvgpu_gpu_get_memory_state_args *)buf); + break; + default: dev_dbg(dev_from_gk20a(g), "unrecognized gpu ioctl cmd: 0x%x", cmd); err = -ENOTTY; diff --git a/drivers/gpu/nvgpu/gk20a/mm_gk20a.c b/drivers/gpu/nvgpu/gk20a/mm_gk20a.c index f46f7a818..ff9bb5e2b 100644 --- a/drivers/gpu/nvgpu/gk20a/mm_gk20a.c +++ b/drivers/gpu/nvgpu/gk20a/mm_gk20a.c @@ -2276,6 +2276,26 @@ err_kfree: #endif } +int gk20a_vidmem_get_space(struct gk20a *g, u64 *space) +{ +#if defined(CONFIG_GK20A_VIDMEM) + struct gk20a_allocator *allocator = &g->mm.vidmem.allocator; + + gk20a_dbg_fn(""); + + if (!gk20a_alloc_initialized(allocator)) + return -ENOSYS; + + mutex_lock(&g->mm.vidmem.clear_list_mutex); + *space = gk20a_alloc_space(allocator) + + atomic64_read(&g->mm.vidmem.bytes_pending); + mutex_unlock(&g->mm.vidmem.clear_list_mutex); + return 0; +#else + return -ENOSYS; +#endif +} + static u64 gk20a_mm_get_align(struct gk20a *g, struct scatterlist *sgl, enum gk20a_aperture aperture) { diff --git a/drivers/gpu/nvgpu/gk20a/mm_gk20a.h b/drivers/gpu/nvgpu/gk20a/mm_gk20a.h index b25a7789d..e8f7da984 100644 --- a/drivers/gpu/nvgpu/gk20a/mm_gk20a.h +++ b/drivers/gpu/nvgpu/gk20a/mm_gk20a.h @@ -755,6 +755,7 @@ void gk20a_vm_mapping_batch_finish_locked( int gk20a_vidmem_buf_alloc(struct gk20a *g, size_t bytes); +int gk20a_vidmem_get_space(struct gk20a *g, u64 *space); /* Note: batch may be NULL if map op is not part of a batch */ int gk20a_vm_map_buffer(struct vm_gk20a *vm, diff --git a/include/uapi/linux/nvgpu.h b/include/uapi/linux/nvgpu.h index 84732452d..0bf886280 100644 --- a/include/uapi/linux/nvgpu.h +++ b/include/uapi/linux/nvgpu.h @@ -510,6 +510,18 @@ struct nvgpu_gpu_alloc_vidmem_args { }; }; +struct nvgpu_gpu_get_memory_state_args { + /* + * Current free space for this device; may change even when any + * kernel-managed metadata (e.g., page tables or channels) is allocated + * or freed. For an idle gpu, an allocation of this size would succeed. + */ + __u64 total_free_bytes; + + /* For future use; must be set to 0. */ + __u64 reserved[4]; +}; + #define NVGPU_GPU_IOCTL_ZCULL_GET_CTX_SIZE \ _IOR(NVGPU_GPU_IOCTL_MAGIC, 1, struct nvgpu_gpu_zcull_get_ctx_size_args) #define NVGPU_GPU_IOCTL_ZCULL_GET_INFO \ @@ -568,8 +580,11 @@ struct nvgpu_gpu_alloc_vidmem_args { #define NVGPU_GPU_IOCTL_ALLOC_VIDMEM \ _IOWR(NVGPU_GPU_IOCTL_MAGIC, 27, \ struct nvgpu_gpu_alloc_vidmem_args) +#define NVGPU_GPU_IOCTL_GET_MEMORY_STATE \ + _IOWR(NVGPU_GPU_IOCTL_MAGIC, 33, \ + struct nvgpu_gpu_get_memory_state_args) #define NVGPU_GPU_IOCTL_LAST \ - _IOC_NR(NVGPU_GPU_IOCTL_ALLOC_VIDMEM) + _IOC_NR(NVGPU_GPU_IOCTL_GET_MEMORY_STATE) #define NVGPU_GPU_IOCTL_MAX_ARG_SIZE \ sizeof(struct nvgpu_gpu_get_cpu_time_correlation_info_args)