drivers/accel/habanalabs/common/mmu/mmu_v2.c

Source file repositories/reference/linux-study-clean/drivers/accel/habanalabs/common/mmu/mmu_v2.c

File Facts

System
Linux kernel
Corpus path
drivers/accel/habanalabs/common/mmu/mmu_v2.c
Extension
.c
Size
9094 bytes
Lines
339
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.

Dependency Surface

Detected Declarations

Annotated Snippet

if ((i <= hop_last) && (curr_pte & mmu_prop->last_mask)) {
			hop_last = i;
			is_huge = true;
			break;
		}
	}

	if (is_dram_addr && !is_huge) {
		dev_err(hdev->dev, "DRAM unmapping should use huge pages only\n");
		return -EFAULT;
	}

	if (!(curr_pte & PAGE_PRESENT_MASK))
		goto not_mapped;

	for (i = hop_last ; i > 0 ; i--) {
		hl_mmu_dr_clear_pte(ctx, hop_pte_addr[i]);
		if (hl_mmu_dr_put_pte(ctx, hop_addr[i]))
			goto mapped;
	}
	hl_mmu_dr_clear_pte(ctx, hop_pte_addr[0]);

mapped:
	return 0;

not_mapped:
	dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
		virt_addr);

	return -EINVAL;
}

static int hl_mmu_v2_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
							u32 page_size, bool is_dram_addr)
{
	u64 hop_addr[MMU_ARCH_6_HOPS] = { 0 }, hop_pte_addr[MMU_ARCH_6_HOPS] = { 0 },
			curr_pte = 0, scrambled_virt_addr, scrambled_phys_addr;
	struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
	bool hop_new[MMU_ARCH_6_HOPS] = { false };
	struct hl_device *hdev = ctx->hdev;
	struct hl_mmu_properties *mmu_prop;
	int rc, i, hop_last;

	/* device resident in V2 are allowed only for HMMU */
	if (!is_dram_addr)
		return -EINVAL;

	mmu_prop = &prop->dmmu;

	hop_last = mmu_prop->num_hops - 1;

	scrambled_virt_addr = hdev->asic_funcs->scramble_addr(hdev, virt_addr);
	scrambled_phys_addr = hdev->asic_funcs->scramble_addr(hdev, phys_addr);

	/* First hop is preallocated therefore it is treated differently  */
	hop_addr[0] = hl_mmu_dr_get_hop0_addr(ctx);
	hop_pte_addr[0] = hl_mmu_get_hop_pte_phys_addr(ctx, mmu_prop, 0,
						hop_addr[0], scrambled_virt_addr);
	curr_pte = *(u64 *) (uintptr_t) hop_pte_addr[0];

	/* Handle hop1 to hop_last */
	for (i = 1 ; i <= hop_last ; i++) {
		hop_addr[i] = hl_mmu_dr_get_alloc_next_hop_addr(ctx, curr_pte, &hop_new[i]);
		if (hop_addr[i] == ULLONG_MAX) {
			rc = -ENOMEM;
			goto err;
		}

		hop_pte_addr[i] = hl_mmu_get_hop_pte_phys_addr(ctx, mmu_prop, i,
					hop_addr[i], scrambled_virt_addr);
		if (hop_pte_addr[i] == U64_MAX) {
			rc = -EINVAL;
			goto err;
		}

		if (!hop_pte_addr[i]) {
			rc = -EINVAL;
			goto err;
		}

		curr_pte = *(u64 *) (uintptr_t) hop_pte_addr[i];
	}

	if (curr_pte & PAGE_PRESENT_MASK) {
		dev_err(hdev->dev,
			"mapping already exists for virt_addr 0x%llx\n",
				virt_addr);

		for (i = 0 ; i <= hop_last ; i++)
			dev_dbg(hdev->dev, "hop%d pte: 0x%llx (0x%llx)\n",

Annotation

Implementation Notes