drivers/accel/habanalabs/common/command_buffer.c
Source file repositories/reference/linux-study-clean/drivers/accel/habanalabs/common/command_buffer.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/accel/habanalabs/common/command_buffer.c- Extension
.c- Size
- 12728 bytes
- Lines
- 559
- 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.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- 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
uapi/drm/habanalabs_accel.hhabanalabs.hlinux/mm.hlinux/slab.hlinux/uaccess.h
Detected Declarations
struct hl_cb_mmap_mem_alloc_argsfunction cb_map_memfunction cb_unmap_memfunction cb_finifunction cb_do_releasefunction hl_cb_mmap_mem_releasefunction hl_cb_mmap_mem_allocfunction hl_cb_mmapfunction hl_cb_createfunction hl_cb_destroyfunction hl_cb_infofunction hl_cb_ioctlfunction hl_cb_putfunction hl_cb_pool_initfunction hl_cb_pool_finifunction list_for_each_entry_safefunction hl_cb_va_pool_initfunction hl_cb_va_pool_fini
Annotated Snippet
struct hl_cb_mmap_mem_alloc_args {
struct hl_device *hdev;
struct hl_ctx *ctx;
u32 cb_size;
bool internal_cb;
bool map_cb;
};
static void hl_cb_mmap_mem_release(struct hl_mmap_mem_buf *buf)
{
struct hl_cb *cb = buf->private;
hl_debugfs_remove_cb(cb);
if (cb->is_mmu_mapped)
cb_unmap_mem(cb->ctx, cb);
hl_ctx_put(cb->ctx);
cb_do_release(cb->hdev, cb);
}
static int hl_cb_mmap_mem_alloc(struct hl_mmap_mem_buf *buf, gfp_t gfp, void *args)
{
struct hl_cb_mmap_mem_alloc_args *cb_args = args;
struct hl_cb *cb;
int rc, ctx_id = cb_args->ctx->asid;
bool alloc_new_cb = true;
if (!cb_args->internal_cb) {
/* Minimum allocation must be PAGE SIZE */
if (cb_args->cb_size < PAGE_SIZE)
cb_args->cb_size = PAGE_SIZE;
if (ctx_id == HL_KERNEL_ASID_ID &&
cb_args->cb_size <= cb_args->hdev->asic_prop.cb_pool_cb_size) {
spin_lock(&cb_args->hdev->cb_pool_lock);
if (!list_empty(&cb_args->hdev->cb_pool)) {
cb = list_first_entry(&cb_args->hdev->cb_pool,
typeof(*cb), pool_list);
list_del(&cb->pool_list);
spin_unlock(&cb_args->hdev->cb_pool_lock);
alloc_new_cb = false;
} else {
spin_unlock(&cb_args->hdev->cb_pool_lock);
dev_dbg(cb_args->hdev->dev, "CB pool is empty\n");
}
}
}
if (alloc_new_cb) {
cb = hl_cb_alloc(cb_args->hdev, cb_args->cb_size, ctx_id, cb_args->internal_cb);
if (!cb)
return -ENOMEM;
}
cb->hdev = cb_args->hdev;
cb->ctx = cb_args->ctx;
cb->buf = buf;
cb->buf->mappable_size = cb->size;
cb->buf->private = cb;
hl_ctx_get(cb->ctx);
if (cb_args->map_cb) {
if (ctx_id == HL_KERNEL_ASID_ID) {
dev_err(cb_args->hdev->dev,
"CB mapping is not supported for kernel context\n");
rc = -EINVAL;
goto release_cb;
}
rc = cb_map_mem(cb_args->ctx, cb);
if (rc)
goto release_cb;
}
hl_debugfs_add_cb(cb);
return 0;
release_cb:
hl_ctx_put(cb->ctx);
cb_do_release(cb_args->hdev, cb);
return rc;
}
static int hl_cb_mmap(struct hl_mmap_mem_buf *buf,
Annotation
- Immediate include surface: `uapi/drm/habanalabs_accel.h`, `habanalabs.h`, `linux/mm.h`, `linux/slab.h`, `linux/uaccess.h`.
- Detected declarations: `struct hl_cb_mmap_mem_alloc_args`, `function cb_map_mem`, `function cb_unmap_mem`, `function cb_fini`, `function cb_do_release`, `function hl_cb_mmap_mem_release`, `function hl_cb_mmap_mem_alloc`, `function hl_cb_mmap`, `function hl_cb_create`, `function hl_cb_destroy`.
- 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.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
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.