intel_region_lmem.c 3.7 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2019 Intel Corporation
 */

#include "i915_drv.h"
7
#include "i915_reg.h"
8
#include "intel_memory_region.h"
9 10
#include "intel_region_lmem.h"
#include "intel_region_ttm.h"
11 12
#include "gem/i915_gem_lmem.h"
#include "gem/i915_gem_region.h"
13
#include "gem/i915_gem_ttm.h"
14
#include "gt/intel_gt.h"
15
#include "gt/intel_gt_regs.h"
16

17
static int
18 19
region_lmem_release(struct intel_memory_region *mem)
{
20 21 22
	int ret;

	ret = intel_region_ttm_fini(mem);
23
	io_mapping_fini(&mem->iomap);
24 25

	return ret;
26 27 28 29 30 31 32 33 34
}

static int
region_lmem_init(struct intel_memory_region *mem)
{
	int ret;

	if (!io_mapping_init_wc(&mem->iomap,
				mem->io_start,
M
Matthew Auld 已提交
35
				mem->io_size))
36
		return -EIO;
37

38
	ret = intel_region_ttm_init(mem);
39
	if (ret)
40 41 42 43 44 45
		goto out_no_buddy;

	return 0;

out_no_buddy:
	io_mapping_fini(&mem->iomap);
46 47 48 49

	return ret;
}

50
static const struct intel_memory_region_ops intel_region_lmem_ops = {
51 52
	.init = region_lmem_init,
	.release = region_lmem_release,
53
	.init_object = __i915_gem_ttm_object_init,
54
};
55

56 57 58
static bool get_legacy_lowmem_region(struct intel_uncore *uncore,
				     u64 *start, u32 *size)
{
59
	if (!IS_DG1_GRAPHICS_STEP(uncore->i915, STEP_A0, STEP_C0))
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		return false;

	*start = 0;
	*size = SZ_1M;

	drm_dbg(&uncore->i915->drm, "LMEM: reserved legacy low-memory [0x%llx-0x%llx]\n",
		*start, *start + *size);

	return true;
}

static int reserve_lowmem_region(struct intel_uncore *uncore,
				 struct intel_memory_region *mem)
{
	u64 reserve_start;
	u32 reserve_size;
	int ret;

	if (!get_legacy_lowmem_region(uncore, &reserve_start, &reserve_size))
		return 0;

	ret = intel_memory_region_reserve(mem, reserve_start, reserve_size);
	if (ret)
		drm_err(&uncore->i915->drm, "LMEM: reserving low memory region failed\n");

	return ret;
}

M
Matthew Auld 已提交
88 89 90
static struct intel_memory_region *setup_lmem(struct intel_gt *gt)
{
	struct drm_i915_private *i915 = gt->i915;
91
	struct intel_uncore *uncore = gt->uncore;
92
	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
M
Matthew Auld 已提交
93
	struct intel_memory_region *mem;
94
	resource_size_t min_page_size;
M
Matthew Auld 已提交
95
	resource_size_t io_start;
96
	resource_size_t lmem_size;
97
	int err;
M
Matthew Auld 已提交
98 99 100 101

	if (!IS_DGFX(i915))
		return ERR_PTR(-ENODEV);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	if (HAS_FLAT_CCS(i915)) {
		u64 tile_stolen, flat_ccs_base;

		lmem_size = pci_resource_len(pdev, 2);
		flat_ccs_base = intel_gt_read_register(gt, XEHPSDV_FLAT_CCS_BASE_ADDR);
		flat_ccs_base = (flat_ccs_base >> XEHPSDV_CCS_BASE_SHIFT) * SZ_64K;

		if (GEM_WARN_ON(lmem_size < flat_ccs_base))
			return ERR_PTR(-ENODEV);

		tile_stolen = lmem_size - flat_ccs_base;

		/* If the FLAT_CCS_BASE_ADDR register is not populated, flag an error */
		if (tile_stolen == lmem_size)
			drm_err(&i915->drm,
				"CCS_BASE_ADDR register did not have expected value\n");

		lmem_size -= tile_stolen;
	} else {
		/* Stolen starts from GSMBASE without CCS */
		lmem_size = intel_uncore_read64(&i915->uncore, GEN12_GSMBASE);
	}

125

M
Matthew Auld 已提交
126
	io_start = pci_resource_start(pdev, 2);
127 128
	if (GEM_WARN_ON(lmem_size > pci_resource_len(pdev, 2)))
		return ERR_PTR(-ENODEV);
M
Matthew Auld 已提交
129

130 131
	min_page_size = HAS_64K_PAGES(i915) ? I915_GTT_PAGE_SIZE_64K :
						I915_GTT_PAGE_SIZE_4K;
M
Matthew Auld 已提交
132 133
	mem = intel_memory_region_create(i915,
					 0,
134
					 lmem_size,
135
					 min_page_size,
M
Matthew Auld 已提交
136
					 io_start,
M
Matthew Auld 已提交
137
					 lmem_size,
138 139
					 INTEL_MEMORY_LOCAL,
					 0,
M
Matthew Auld 已提交
140 141 142 143
					 &intel_region_lmem_ops);
	if (IS_ERR(mem))
		return mem;

144 145 146 147
	err = reserve_lowmem_region(uncore, mem);
	if (err)
		goto err_region_put;

M
Matthew Auld 已提交
148 149 150
	drm_dbg(&i915->drm, "Local memory: %pR\n", &mem->region);
	drm_dbg(&i915->drm, "Local memory IO start: %pa\n",
		&mem->io_start);
M
Matthew Auld 已提交
151 152
	drm_info(&i915->drm, "Local memory IO size: %pa\n",
		 &mem->io_size);
153 154
	drm_info(&i915->drm, "Local memory available: %pa\n",
		 &lmem_size);
M
Matthew Auld 已提交
155 156

	return mem;
157 158

err_region_put:
159
	intel_memory_region_destroy(mem);
160
	return ERR_PTR(err);
M
Matthew Auld 已提交
161 162 163 164 165 166
}

struct intel_memory_region *intel_gt_setup_lmem(struct intel_gt *gt)
{
	return setup_lmem(gt);
}