• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #ifndef __INTEL_MEMORY_REGION_H__
7 #define __INTEL_MEMORY_REGION_H__
8 
9 #include <linux/kref.h>
10 #include <linux/ioport.h>
11 #include <linux/mutex.h>
12 #include <linux/io-mapping.h>
13 #include <drm/drm_mm.h>
14 #include <drm/i915_drm.h>
15 
16 struct drm_i915_private;
17 struct drm_i915_gem_object;
18 struct intel_memory_region;
19 struct sg_table;
20 struct ttm_resource;
21 
22 enum intel_memory_type {
23 	INTEL_MEMORY_SYSTEM = I915_MEMORY_CLASS_SYSTEM,
24 	INTEL_MEMORY_LOCAL = I915_MEMORY_CLASS_DEVICE,
25 	INTEL_MEMORY_STOLEN_SYSTEM,
26 	INTEL_MEMORY_STOLEN_LOCAL,
27 	INTEL_MEMORY_MOCK,
28 };
29 
30 enum intel_region_id {
31 	INTEL_REGION_SMEM = 0,
32 	INTEL_REGION_LMEM,
33 	INTEL_REGION_STOLEN_SMEM,
34 	INTEL_REGION_STOLEN_LMEM,
35 	INTEL_REGION_UNKNOWN, /* Should be last */
36 };
37 
38 #define REGION_SMEM     BIT(INTEL_REGION_SMEM)
39 #define REGION_LMEM     BIT(INTEL_REGION_LMEM)
40 #define REGION_STOLEN_SMEM   BIT(INTEL_REGION_STOLEN_SMEM)
41 #define REGION_STOLEN_LMEM   BIT(INTEL_REGION_STOLEN_LMEM)
42 
43 #define I915_ALLOC_CONTIGUOUS     BIT(0)
44 
45 #define for_each_memory_region(mr, i915, id) \
46 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
47 		for_each_if((mr) = (i915)->mm.regions[id])
48 
49 struct intel_memory_region_ops {
50 	unsigned int flags;
51 
52 	int (*init)(struct intel_memory_region *mem);
53 	void (*release)(struct intel_memory_region *mem);
54 
55 	int (*init_object)(struct intel_memory_region *mem,
56 			   struct drm_i915_gem_object *obj,
57 			   resource_size_t size,
58 			   resource_size_t page_size,
59 			   unsigned int flags);
60 };
61 
62 struct intel_memory_region {
63 	struct drm_i915_private *i915;
64 
65 	const struct intel_memory_region_ops *ops;
66 
67 	struct io_mapping iomap;
68 	struct resource region;
69 
70 	/* For fake LMEM */
71 	struct drm_mm_node fake_mappable;
72 
73 	struct kref kref;
74 
75 	resource_size_t io_start;
76 	resource_size_t min_page_size;
77 	resource_size_t total;
78 	resource_size_t avail;
79 
80 	u16 type;
81 	u16 instance;
82 	enum intel_region_id id;
83 	char name[16];
84 	bool private; /* not for userspace */
85 
86 	dma_addr_t remap_addr;
87 
88 	struct {
89 		struct mutex lock; /* Protects access to objects */
90 		struct list_head list;
91 	} objects;
92 
93 	bool is_range_manager;
94 
95 	void *region_private;
96 };
97 
98 struct intel_memory_region *
99 intel_memory_region_lookup(struct drm_i915_private *i915,
100 			   u16 class, u16 instance);
101 
102 struct intel_memory_region *
103 intel_memory_region_create(struct drm_i915_private *i915,
104 			   resource_size_t start,
105 			   resource_size_t size,
106 			   resource_size_t min_page_size,
107 			   resource_size_t io_start,
108 			   u16 type,
109 			   u16 instance,
110 			   const struct intel_memory_region_ops *ops);
111 
112 struct intel_memory_region *
113 intel_memory_region_get(struct intel_memory_region *mem);
114 void intel_memory_region_put(struct intel_memory_region *mem);
115 
116 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
117 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
118 struct intel_memory_region *
119 intel_memory_region_by_type(struct drm_i915_private *i915,
120 			    enum intel_memory_type mem_type);
121 
122 __printf(2, 3) void
123 intel_memory_region_set_name(struct intel_memory_region *mem,
124 			     const char *fmt, ...);
125 
126 int intel_memory_region_reserve(struct intel_memory_region *mem,
127 				resource_size_t offset,
128 				resource_size_t size);
129 
130 struct intel_memory_region *
131 i915_gem_ttm_system_setup(struct drm_i915_private *i915,
132 			  u16 type, u16 instance);
133 struct intel_memory_region *
134 i915_gem_shmem_setup(struct drm_i915_private *i915,
135 		     u16 type, u16 instance);
136 
137 #endif
138