Commit 54303a1a authored by Oded Gabbay's avatar Oded Gabbay

habanalabs: split mmu/no-mmu code paths in memory ioctl

To make the memory ioctl code more readable, this patch moves the
legacy/debug code path of mmu-disabled to a separate function, which is
called (if necessary) from the main memory ioctl function.
Signed-off-by: default avatarOded Gabbay <oded.gabbay@gmail.com>
parent 29593840
...@@ -1090,36 +1090,27 @@ static int unmap_device_va(struct hl_ctx *ctx, u64 vaddr) ...@@ -1090,36 +1090,27 @@ static int unmap_device_va(struct hl_ctx *ctx, u64 vaddr)
return rc; return rc;
} }
int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) static int mem_ioctl_no_mmu(struct hl_fpriv *hpriv, union hl_mem_args *args)
{ {
union hl_mem_args *args = data;
struct hl_device *hdev = hpriv->hdev; struct hl_device *hdev = hpriv->hdev;
struct hl_ctx *ctx = hpriv->ctx; struct hl_ctx *ctx = hpriv->ctx;
u64 device_addr = 0; u64 device_addr = 0;
u32 handle = 0; u32 handle = 0;
int rc; int rc;
if (hl_device_disabled_or_in_reset(hdev)) {
dev_warn_ratelimited(hdev->dev,
"Device is disabled or in reset. Can't execute memory IOCTL\n");
return -EBUSY;
}
if (hdev->mmu_enable) {
switch (args->in.op) { switch (args->in.op) {
case HL_MEM_OP_ALLOC: case HL_MEM_OP_ALLOC:
if (!hdev->dram_supports_virtual_memory) {
dev_err(hdev->dev,
"DRAM alloc is not supported\n");
rc = -EINVAL;
goto out;
}
if (args->in.alloc.mem_size == 0) { if (args->in.alloc.mem_size == 0) {
dev_err(hdev->dev, dev_err(hdev->dev,
"alloc size must be larger than 0\n"); "alloc size must be larger than 0\n");
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
/* Force contiguous as there are no real MMU
* translations to overcome physical memory gaps
*/
args->in.flags |= HL_MEM_CONTIGUOUS;
rc = alloc_device_memory(ctx, &args->in, &handle); rc = alloc_device_memory(ctx, &args->in, &handle);
memset(args, 0, sizeof(*args)); memset(args, 0, sizeof(*args));
...@@ -1127,25 +1118,24 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) ...@@ -1127,25 +1118,24 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
break; break;
case HL_MEM_OP_FREE: case HL_MEM_OP_FREE:
if (!hdev->dram_supports_virtual_memory) {
dev_err(hdev->dev,
"DRAM free is not supported\n");
rc = -EINVAL;
goto out;
}
rc = free_device_memory(ctx, args->in.free.handle); rc = free_device_memory(ctx, args->in.free.handle);
break; break;
case HL_MEM_OP_MAP: case HL_MEM_OP_MAP:
rc = map_device_va(ctx, &args->in, &device_addr); if (args->in.flags & HL_MEM_USERPTR) {
device_addr = args->in.map_host.host_virt_addr;
rc = 0;
} else {
rc = get_paddr_from_handle(ctx, &args->in,
&device_addr);
}
memset(args, 0, sizeof(*args)); memset(args, 0, sizeof(*args));
args->out.device_virt_addr = device_addr; args->out.device_virt_addr = device_addr;
break; break;
case HL_MEM_OP_UNMAP: case HL_MEM_OP_UNMAP:
rc = unmap_device_va(ctx, rc = 0;
args->in.unmap.device_virt_addr);
break; break;
default: default:
...@@ -1153,20 +1143,43 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) ...@@ -1153,20 +1143,43 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
rc = -ENOTTY; rc = -ENOTTY;
break; break;
} }
} else {
out:
return rc;
}
int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
{
union hl_mem_args *args = data;
struct hl_device *hdev = hpriv->hdev;
struct hl_ctx *ctx = hpriv->ctx;
u64 device_addr = 0;
u32 handle = 0;
int rc;
if (hl_device_disabled_or_in_reset(hdev)) {
dev_warn_ratelimited(hdev->dev,
"Device is disabled or in reset. Can't execute memory IOCTL\n");
return -EBUSY;
}
if (!hdev->mmu_enable)
return mem_ioctl_no_mmu(hpriv, args);
switch (args->in.op) { switch (args->in.op) {
case HL_MEM_OP_ALLOC: case HL_MEM_OP_ALLOC:
if (!hdev->dram_supports_virtual_memory) {
dev_err(hdev->dev, "DRAM alloc is not supported\n");
rc = -EINVAL;
goto out;
}
if (args->in.alloc.mem_size == 0) { if (args->in.alloc.mem_size == 0) {
dev_err(hdev->dev, dev_err(hdev->dev,
"alloc size must be larger than 0\n"); "alloc size must be larger than 0\n");
rc = -EINVAL; rc = -EINVAL;
goto out; goto out;
} }
/* Force contiguous as there are no real MMU
* translations to overcome physical memory gaps
*/
args->in.flags |= HL_MEM_CONTIGUOUS;
rc = alloc_device_memory(ctx, &args->in, &handle); rc = alloc_device_memory(ctx, &args->in, &handle);
memset(args, 0, sizeof(*args)); memset(args, 0, sizeof(*args));
...@@ -1178,20 +1191,15 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) ...@@ -1178,20 +1191,15 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
break; break;
case HL_MEM_OP_MAP: case HL_MEM_OP_MAP:
if (args->in.flags & HL_MEM_USERPTR) { rc = map_device_va(ctx, &args->in, &device_addr);
device_addr = args->in.map_host.host_virt_addr;
rc = 0;
} else {
rc = get_paddr_from_handle(ctx, &args->in,
&device_addr);
}
memset(args, 0, sizeof(*args)); memset(args, 0, sizeof(*args));
args->out.device_virt_addr = device_addr; args->out.device_virt_addr = device_addr;
break; break;
case HL_MEM_OP_UNMAP: case HL_MEM_OP_UNMAP:
rc = 0; rc = unmap_device_va(ctx,
args->in.unmap.device_virt_addr);
break; break;
default: default:
...@@ -1199,7 +1207,6 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data) ...@@ -1199,7 +1207,6 @@ int hl_mem_ioctl(struct hl_fpriv *hpriv, void *data)
rc = -ENOTTY; rc = -ENOTTY;
break; break;
} }
}
out: out:
return rc; return rc;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment