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 drm_printer;
19 struct intel_memory_region;
20 struct sg_table;
21 struct ttm_resource;
22 
23 enum intel_memory_type {
24 	INTEL_MEMORY_SYSTEM = I915_MEMORY_CLASS_SYSTEM,
25 	INTEL_MEMORY_LOCAL = I915_MEMORY_CLASS_DEVICE,
26 	INTEL_MEMORY_STOLEN_SYSTEM,
27 	INTEL_MEMORY_STOLEN_LOCAL,
28 	INTEL_MEMORY_MOCK,
29 };
30 
31 enum intel_region_id {
32 	INTEL_REGION_SMEM = 0,
33 	INTEL_REGION_LMEM,
34 	INTEL_REGION_STOLEN_SMEM,
35 	INTEL_REGION_STOLEN_LMEM,
36 	INTEL_REGION_UNKNOWN, /* Should be last */
37 };
38 
39 #define REGION_SMEM     BIT(INTEL_REGION_SMEM)
40 #define REGION_LMEM     BIT(INTEL_REGION_LMEM)
41 #define REGION_STOLEN_SMEM   BIT(INTEL_REGION_STOLEN_SMEM)
42 #define REGION_STOLEN_LMEM   BIT(INTEL_REGION_STOLEN_LMEM)
43 
44 #define I915_ALLOC_CONTIGUOUS     BIT(0)
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 			   resource_size_t page_size,
60 			   unsigned int flags);
61 };
62 
63 struct intel_memory_region {
64 	struct drm_i915_private *i915;
65 
66 	const struct intel_memory_region_ops *ops;
67 
68 	struct io_mapping iomap;
69 	struct resource region;
70 
71 	/* For fake LMEM */
72 	struct drm_mm_node fake_mappable;
73 
74 	struct kref kref;
75 
76 	resource_size_t io_start;
77 	resource_size_t min_page_size;
78 	resource_size_t total;
79 	resource_size_t avail;
80 
81 	u16 type;
82 	u16 instance;
83 	enum intel_region_id id;
84 	char name[16];
85 	bool private; /* not for userspace */
86 
87 	dma_addr_t remap_addr;
88 
89 	struct {
90 		struct mutex lock; /* Protects access to objects */
91 		struct list_head list;
92 	} objects;
93 
94 	bool is_range_manager;
95 
96 	void *region_private;
97 };
98 
99 struct intel_memory_region *
100 intel_memory_region_lookup(struct drm_i915_private *i915,
101 			   u16 class, u16 instance);
102 
103 struct intel_memory_region *
104 intel_memory_region_create(struct drm_i915_private *i915,
105 			   resource_size_t start,
106 			   resource_size_t size,
107 			   resource_size_t min_page_size,
108 			   resource_size_t io_start,
109 			   u16 type,
110 			   u16 instance,
111 			   const struct intel_memory_region_ops *ops);
112 
113 struct intel_memory_region *
114 intel_memory_region_get(struct intel_memory_region *mem);
115 void intel_memory_region_put(struct intel_memory_region *mem);
116 
117 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
118 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
119 struct intel_memory_region *
120 intel_memory_region_by_type(struct drm_i915_private *i915,
121 			    enum intel_memory_type mem_type);
122 
123 __printf(2, 3) void
124 intel_memory_region_set_name(struct intel_memory_region *mem,
125 			     const char *fmt, ...);
126 
127 int intel_memory_region_reserve(struct intel_memory_region *mem,
128 				resource_size_t offset,
129 				resource_size_t size);
130 
131 void intel_memory_region_debug(struct intel_memory_region *mr,
132 			       struct drm_printer *printer);
133 
134 struct intel_memory_region *
135 i915_gem_ttm_system_setup(struct drm_i915_private *i915,
136 			  u16 type, u16 instance);
137 struct intel_memory_region *
138 i915_gem_shmem_setup(struct drm_i915_private *i915,
139 		     u16 type, u16 instance);
140 
141 #endif
142