xref: /linux/drivers/gpu/drm/i915/intel_memory_region.h (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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/ioport.h>
10 #include <linux/mutex.h>
11 #include <linux/io-mapping.h>
12 #include <drm/drm_mm.h>
13 #include <uapi/drm/i915_drm.h>
14 
15 struct drm_i915_private;
16 struct drm_i915_gem_object;
17 struct drm_printer;
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_0,
33 	INTEL_REGION_LMEM_1,
34 	INTEL_REGION_LMEM_2,
35 	INTEL_REGION_LMEM_3,
36 	INTEL_REGION_STOLEN_SMEM,
37 	INTEL_REGION_STOLEN_LMEM,
38 	INTEL_REGION_UNKNOWN, /* Should be last */
39 };
40 
41 #define REGION_SMEM     BIT(INTEL_REGION_SMEM)
42 #define REGION_LMEM     BIT(INTEL_REGION_LMEM_0)
43 #define REGION_STOLEN_SMEM   BIT(INTEL_REGION_STOLEN_SMEM)
44 #define REGION_STOLEN_LMEM   BIT(INTEL_REGION_STOLEN_LMEM)
45 
46 #define I915_ALLOC_CONTIGUOUS     BIT(0)
47 
48 #define for_each_memory_region(mr, i915, id) \
49 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
50 		for_each_if((mr) = (i915)->mm.regions[id])
51 
52 struct intel_memory_region_ops {
53 	int (*init)(struct intel_memory_region *mem);
54 	int (*release)(struct intel_memory_region *mem);
55 
56 	int (*init_object)(struct intel_memory_region *mem,
57 			   struct drm_i915_gem_object *obj,
58 			   resource_size_t offset,
59 			   resource_size_t size,
60 			   resource_size_t page_size,
61 			   unsigned int flags);
62 };
63 
64 struct intel_memory_region {
65 	struct drm_i915_private *i915;
66 
67 	const struct intel_memory_region_ops *ops;
68 
69 	struct io_mapping iomap;
70 	struct resource region;
71 
72 	struct resource io;
73 	resource_size_t min_page_size;
74 	resource_size_t total;
75 
76 	u16 type;
77 	u16 instance;
78 	enum intel_region_id id;
79 	char name[16];
80 	char uabi_name[16];
81 	bool private; /* not for userspace */
82 
83 	struct {
84 		struct mutex lock; /* Protects access to objects */
85 		struct list_head list;
86 	} objects;
87 
88 	bool is_range_manager;
89 
90 	void *region_private;
91 };
92 
93 struct intel_memory_region *
94 intel_memory_region_lookup(struct drm_i915_private *i915,
95 			   u16 class, u16 instance);
96 
97 struct intel_memory_region *
98 intel_memory_region_create(struct drm_i915_private *i915,
99 			   resource_size_t start,
100 			   resource_size_t size,
101 			   resource_size_t min_page_size,
102 			   resource_size_t io_start,
103 			   resource_size_t io_size,
104 			   u16 type,
105 			   u16 instance,
106 			   const struct intel_memory_region_ops *ops);
107 
108 void intel_memory_region_destroy(struct intel_memory_region *mem);
109 
110 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
111 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
112 struct intel_memory_region *
113 intel_memory_region_by_type(struct drm_i915_private *i915,
114 			    enum intel_memory_type mem_type);
115 
116 __printf(2, 3) void
117 intel_memory_region_set_name(struct intel_memory_region *mem,
118 			     const char *fmt, ...);
119 
120 int intel_memory_region_reserve(struct intel_memory_region *mem,
121 				resource_size_t offset,
122 				resource_size_t size);
123 
124 void intel_memory_region_debug(struct intel_memory_region *mr,
125 			       struct drm_printer *printer);
126 
127 void intel_memory_region_avail(struct intel_memory_region *mr,
128 			       u64 *avail, u64 *visible_avail);
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