drivers/gpu/drm/ttm/tests/ttm_mock_manager.c

Source file repositories/reference/linux-study-clean/drivers/gpu/drm/ttm/tests/ttm_mock_manager.c

File Facts

System
Linux kernel
Corpus path
drivers/gpu/drm/ttm/tests/ttm_mock_manager.c
Extension
.c
Size
5722 bytes
Lines
239
Domain
Driver Families
Bucket
drivers/gpu
Inferred role
Driver Families: exported/initcall integration point
Status
integration 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

// SPDX-License-Identifier: GPL-2.0 AND MIT
/*
 * Copyright © 2023 Intel Corporation
 */

#include <linux/export.h>
#include <linux/module.h>

#include <drm/ttm/ttm_resource.h>
#include <drm/ttm/ttm_device.h>
#include <drm/ttm/ttm_placement.h>

#include "ttm_mock_manager.h"

static inline struct ttm_mock_manager *
to_mock_mgr(struct ttm_resource_manager *man)
{
	return container_of(man, struct ttm_mock_manager, man);
}

static inline struct ttm_mock_resource *
to_mock_mgr_resource(struct ttm_resource *res)
{
	return container_of(res, struct ttm_mock_resource, base);
}

static int ttm_mock_manager_alloc(struct ttm_resource_manager *man,
				  struct ttm_buffer_object *bo,
				  const struct ttm_place *place,
				  struct ttm_resource **res)
{
	struct ttm_mock_manager *manager = to_mock_mgr(man);
	struct ttm_mock_resource *mock_res;
	struct gpu_buddy *mm = &manager->mm;
	u64 lpfn, fpfn, alloc_size;
	int err;

	mock_res = kzalloc_obj(*mock_res);

	if (!mock_res)
		return -ENOMEM;

	fpfn = 0;
	lpfn = man->size;

	ttm_resource_init(bo, place, &mock_res->base);
	INIT_LIST_HEAD(&mock_res->blocks);

	if (place->flags & TTM_PL_FLAG_TOPDOWN)
		mock_res->flags |= GPU_BUDDY_TOPDOWN_ALLOCATION;

	if (place->flags & TTM_PL_FLAG_CONTIGUOUS)
		mock_res->flags |= GPU_BUDDY_CONTIGUOUS_ALLOCATION;

	alloc_size = (uint64_t)mock_res->base.size;
	mutex_lock(&manager->lock);
	err = gpu_buddy_alloc_blocks(mm, fpfn, lpfn, alloc_size,
				     manager->default_page_size,
				     &mock_res->blocks,
				     mock_res->flags);

	if (err)
		goto error_free_blocks;
	mutex_unlock(&manager->lock);

	*res = &mock_res->base;
	return 0;

error_free_blocks:
	gpu_buddy_free_list(mm, &mock_res->blocks, 0);
	ttm_resource_fini(man, &mock_res->base);
	mutex_unlock(&manager->lock);

	return err;
}

static void ttm_mock_manager_free(struct ttm_resource_manager *man,
				  struct ttm_resource *res)
{
	struct ttm_mock_manager *manager = to_mock_mgr(man);
	struct ttm_mock_resource *mock_res = to_mock_mgr_resource(res);
	struct gpu_buddy *mm = &manager->mm;

	mutex_lock(&manager->lock);
	gpu_buddy_free_list(mm, &mock_res->blocks, 0);
	mutex_unlock(&manager->lock);

	ttm_resource_fini(man, res);
	kfree(mock_res);
}

Annotation

Implementation Notes