drivers/accel/habanalabs/common/context.c
Source file repositories/reference/linux-study-clean/drivers/accel/habanalabs/common/context.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/accel/habanalabs/common/context.c- Extension
.c- Size
- 10329 bytes
- Lines
- 449
- Domain
- Driver Families
- Bucket
- drivers/accel
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
habanalabs.hlinux/slab.h
Detected Declarations
function encaps_handle_do_releasefunction hl_encaps_release_handle_and_put_ctxfunction hl_encaps_release_handle_and_put_sobfunction hl_encaps_release_handle_and_put_sob_ctxfunction hl_encaps_sig_mgr_initfunction hl_encaps_sig_mgr_finifunction hl_ctx_finifunction hl_ctx_do_releasefunction hl_ctx_createfunction hl_ctx_initfunction hl_ctx_get_unless_zerofunction hl_ctx_getfunction hl_ctx_putfunction list_for_each_entryfunction hl_ctx_get_fencesfunction hl_ctx_mgr_initfunction hl_ctx_mgr_fini
Annotated Snippet
if (rc) {
dev_err(hdev->dev, "Failed to init mem ctx module\n");
rc = -ENOMEM;
goto err_hw_block_mem_fini;
}
rc = hdev->asic_funcs->ctx_init(ctx);
if (rc) {
dev_err(hdev->dev, "ctx_init failed\n");
goto err_vm_ctx_fini;
}
} else {
ctx->asid = hl_asid_alloc(hdev);
if (!ctx->asid) {
dev_err(hdev->dev, "No free ASID, failed to create context\n");
rc = -ENOMEM;
goto err_hw_block_mem_fini;
}
rc = hl_vm_ctx_init(ctx);
if (rc) {
dev_err(hdev->dev, "Failed to init mem ctx module\n");
rc = -ENOMEM;
goto err_asid_free;
}
rc = hl_cb_va_pool_init(ctx);
if (rc) {
dev_err(hdev->dev,
"Failed to init VA pool for mapped CB\n");
goto err_vm_ctx_fini;
}
rc = hdev->asic_funcs->ctx_init(ctx);
if (rc) {
dev_err(hdev->dev, "ctx_init failed\n");
goto err_cb_va_pool_fini;
}
hl_encaps_sig_mgr_init(&ctx->sig_mgr);
mutex_init(&ctx->ts_reg_lock);
dev_dbg(hdev->dev, "create user context, comm=\"%s\", asid=%u\n",
current->comm, ctx->asid);
}
return 0;
err_cb_va_pool_fini:
hl_cb_va_pool_fini(ctx);
err_vm_ctx_fini:
hl_vm_ctx_fini(ctx);
err_asid_free:
if (ctx->asid != HL_KERNEL_ASID_ID)
hl_asid_free(hdev, ctx->asid);
err_hw_block_mem_fini:
hl_hw_block_mem_fini(ctx);
kfree(ctx->cs_pending);
return rc;
}
static int hl_ctx_get_unless_zero(struct hl_ctx *ctx)
{
return kref_get_unless_zero(&ctx->refcount);
}
void hl_ctx_get(struct hl_ctx *ctx)
{
kref_get(&ctx->refcount);
}
int hl_ctx_put(struct hl_ctx *ctx)
{
return kref_put(&ctx->refcount, hl_ctx_do_release);
}
struct hl_ctx *hl_get_compute_ctx(struct hl_device *hdev)
{
struct hl_ctx *ctx = NULL;
struct hl_fpriv *hpriv;
mutex_lock(&hdev->fpriv_list_lock);
list_for_each_entry(hpriv, &hdev->fpriv_list, dev_node) {
mutex_lock(&hpriv->ctx_lock);
ctx = hpriv->ctx;
if (ctx && !hl_ctx_get_unless_zero(ctx))
ctx = NULL;
Annotation
- Immediate include surface: `habanalabs.h`, `linux/slab.h`.
- Detected declarations: `function encaps_handle_do_release`, `function hl_encaps_release_handle_and_put_ctx`, `function hl_encaps_release_handle_and_put_sob`, `function hl_encaps_release_handle_and_put_sob_ctx`, `function hl_encaps_sig_mgr_init`, `function hl_encaps_sig_mgr_fini`, `function hl_ctx_fini`, `function hl_ctx_do_release`, `function hl_ctx_create`, `function hl_ctx_init`.
- Atlas domain: Driver Families / drivers/accel.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.