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_MIN_PAGE_SIZE  BIT(0)
44 #define I915_ALLOC_CONTIGUOUS     BIT(1)
45 
46 #define for_each_memory_region(mr, i915, id) \
47 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
48 		for_each_if((mr) = (i915)->mm.regions[id])
49 
50 struct intel_memory_region_ops {
51 	unsigned int flags;
52 
53 	int (*init)(struct intel_memory_region *mem);
54 	void (*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 size,
59 			   unsigned int flags);
60 };
61 
62 struct intel_memory_region_private_ops {
63 	struct ttm_resource *(*reserve)(struct intel_memory_region *mem,
64 					resource_size_t offset,
65 					resource_size_t size);
66 	void (*free)(struct intel_memory_region *mem,
67 		     struct ttm_resource *res);
68 };
69 
70 struct intel_memory_region {
71 	struct drm_i915_private *i915;
72 
73 	const struct intel_memory_region_ops *ops;
74 	const struct intel_memory_region_private_ops *priv_ops;
75 
76 	struct io_mapping iomap;
77 	struct resource region;
78 
79 	/* For fake LMEM */
80 	struct drm_mm_node fake_mappable;
81 
82 	struct mutex mm_lock;
83 
84 	struct kref kref;
85 
86 	resource_size_t io_start;
87 	resource_size_t min_page_size;
88 	resource_size_t total;
89 	resource_size_t avail;
90 
91 	u16 type;
92 	u16 instance;
93 	enum intel_region_id id;
94 	char name[16];
95 	bool private; /* not for userspace */
96 
97 	struct list_head reserved;
98 
99 	dma_addr_t remap_addr;
100 
101 	struct {
102 		struct mutex lock; /* Protects access to objects */
103 		struct list_head list;
104 		struct list_head purgeable;
105 	} objects;
106 
107 	size_t chunk_size;
108 	unsigned int max_order;
109 	bool is_range_manager;
110 
111 	void *region_private;
112 };
113 
114 struct intel_memory_region *
115 intel_memory_region_lookup(struct drm_i915_private *i915,
116 			   u16 class, u16 instance);
117 
118 struct intel_memory_region *
119 intel_memory_region_create(struct drm_i915_private *i915,
120 			   resource_size_t start,
121 			   resource_size_t size,
122 			   resource_size_t min_page_size,
123 			   resource_size_t io_start,
124 			   u16 type,
125 			   u16 instance,
126 			   const struct intel_memory_region_ops *ops);
127 
128 struct intel_memory_region *
129 intel_memory_region_get(struct intel_memory_region *mem);
130 void intel_memory_region_put(struct intel_memory_region *mem);
131 
132 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
133 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
134 struct intel_memory_region *
135 intel_memory_region_by_type(struct drm_i915_private *i915,
136 			    enum intel_memory_type mem_type);
137 
138 __printf(2, 3) void
139 intel_memory_region_set_name(struct intel_memory_region *mem,
140 			     const char *fmt, ...);
141 
142 void intel_memory_region_unreserve(struct intel_memory_region *mem);
143 
144 int intel_memory_region_reserve(struct intel_memory_region *mem,
145 				resource_size_t offset,
146 				resource_size_t size);
147 #endif
148