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 I915_ALLOC_CONTIGUOUS     BIT(0)
42 
43 #define for_each_memory_region(mr, i915, id) \
44 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
45 		for_each_if((mr) = (i915)->mm.regions[id])
46 
47 struct intel_memory_region_ops {
48 	int (*init)(struct intel_memory_region *mem);
49 	int (*release)(struct intel_memory_region *mem);
50 
51 	int (*init_object)(struct intel_memory_region *mem,
52 			   struct drm_i915_gem_object *obj,
53 			   resource_size_t offset,
54 			   resource_size_t size,
55 			   resource_size_t page_size,
56 			   unsigned int flags);
57 };
58 
59 struct intel_memory_region {
60 	struct drm_i915_private *i915;
61 
62 	const struct intel_memory_region_ops *ops;
63 
64 	struct io_mapping iomap;
65 	struct resource region;
66 
67 	struct resource io;
68 	resource_size_t min_page_size;
69 	resource_size_t total;
70 
71 	u16 type;
72 	u16 instance;
73 	enum intel_region_id id;
74 	char name[16];
75 	char uabi_name[16];
76 	bool private; /* not for userspace */
77 
78 	struct {
79 		struct mutex lock; /* Protects access to objects */
80 		struct list_head list;
81 	} objects;
82 
83 	bool is_range_manager;
84 
85 	void *region_private;
86 };
87 
88 struct intel_memory_region *
89 intel_memory_region_lookup(struct drm_i915_private *i915,
90 			   u16 class, u16 instance);
91 
92 struct intel_memory_region *
93 intel_memory_region_create(struct drm_i915_private *i915,
94 			   resource_size_t start,
95 			   resource_size_t size,
96 			   resource_size_t min_page_size,
97 			   resource_size_t io_start,
98 			   resource_size_t io_size,
99 			   u16 type,
100 			   u16 instance,
101 			   const struct intel_memory_region_ops *ops);
102 
103 void intel_memory_region_destroy(struct intel_memory_region *mem);
104 
105 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
106 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
107 struct intel_memory_region *
108 intel_memory_region_by_type(struct drm_i915_private *i915,
109 			    enum intel_memory_type mem_type);
110 
111 __printf(2, 3) void
112 intel_memory_region_set_name(struct intel_memory_region *mem,
113 			     const char *fmt, ...);
114 
115 int intel_memory_region_reserve(struct intel_memory_region *mem,
116 				resource_size_t offset,
117 				resource_size_t size);
118 
119 void intel_memory_region_debug(struct intel_memory_region *mr,
120 			       struct drm_printer *printer);
121 
122 void intel_memory_region_avail(struct intel_memory_region *mr,
123 			       u64 *avail, u64 *visible_avail);
124 
125 struct intel_memory_region *
126 i915_gem_ttm_system_setup(struct drm_i915_private *i915,
127 			  u16 type, u16 instance);
128 struct intel_memory_region *
129 i915_gem_shmem_setup(struct drm_i915_private *i915,
130 		     u16 type, u16 instance);
131 
132 #endif
133