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 
15 #include "i915_buddy.h"
16 
17 struct drm_i915_private;
18 struct drm_i915_gem_object;
19 struct intel_memory_region;
20 struct sg_table;
21 
22 /**
23  *  Base memory type
24  */
25 enum intel_memory_type {
26 	INTEL_MEMORY_SYSTEM = 0,
27 	INTEL_MEMORY_LOCAL,
28 	INTEL_MEMORY_STOLEN,
29 };
30 
31 enum intel_region_id {
32 	INTEL_REGION_SMEM = 0,
33 	INTEL_REGION_LMEM,
34 	INTEL_REGION_STOLEN,
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   BIT(INTEL_REGION_STOLEN)
41 
42 #define INTEL_MEMORY_TYPE_SHIFT 16
43 
44 #define MEMORY_TYPE_FROM_REGION(r) (ilog2((r) >> INTEL_MEMORY_TYPE_SHIFT))
45 #define MEMORY_INSTANCE_FROM_REGION(r) (ilog2((r) & 0xffff))
46 
47 #define I915_ALLOC_MIN_PAGE_SIZE  BIT(0)
48 #define I915_ALLOC_CONTIGUOUS     BIT(1)
49 
50 #define for_each_memory_region(mr, i915, id) \
51 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
52 		for_each_if((mr) = (i915)->mm.regions[id])
53 
54 /**
55  * Memory regions encoded as type | instance
56  */
57 extern const u32 intel_region_map[];
58 
59 struct intel_memory_region_ops {
60 	unsigned int flags;
61 
62 	int (*init)(struct intel_memory_region *mem);
63 	void (*release)(struct intel_memory_region *mem);
64 
65 	struct drm_i915_gem_object *
66 	(*create_object)(struct intel_memory_region *mem,
67 			 resource_size_t size,
68 			 unsigned int flags);
69 };
70 
71 struct intel_memory_region {
72 	struct drm_i915_private *i915;
73 
74 	const struct intel_memory_region_ops *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 i915_buddy_mm mm;
83 	struct mutex mm_lock;
84 
85 	struct kref kref;
86 
87 	resource_size_t io_start;
88 	resource_size_t min_page_size;
89 	resource_size_t total;
90 	resource_size_t avail;
91 
92 	unsigned int type;
93 	unsigned int instance;
94 	unsigned int id;
95 	char name[8];
96 
97 	dma_addr_t remap_addr;
98 
99 	struct {
100 		struct mutex lock; /* Protects access to objects */
101 		struct list_head list;
102 		struct list_head purgeable;
103 	} objects;
104 };
105 
106 int intel_memory_region_init_buddy(struct intel_memory_region *mem);
107 void intel_memory_region_release_buddy(struct intel_memory_region *mem);
108 
109 int __intel_memory_region_get_pages_buddy(struct intel_memory_region *mem,
110 					  resource_size_t size,
111 					  unsigned int flags,
112 					  struct list_head *blocks);
113 struct i915_buddy_block *
114 __intel_memory_region_get_block_buddy(struct intel_memory_region *mem,
115 				      resource_size_t size,
116 				      unsigned int flags);
117 void __intel_memory_region_put_pages_buddy(struct intel_memory_region *mem,
118 					   struct list_head *blocks);
119 void __intel_memory_region_put_block_buddy(struct i915_buddy_block *block);
120 
121 struct intel_memory_region *
122 intel_memory_region_create(struct drm_i915_private *i915,
123 			   resource_size_t start,
124 			   resource_size_t size,
125 			   resource_size_t min_page_size,
126 			   resource_size_t io_start,
127 			   const struct intel_memory_region_ops *ops);
128 
129 struct intel_memory_region *
130 intel_memory_region_get(struct intel_memory_region *mem);
131 void intel_memory_region_put(struct intel_memory_region *mem);
132 
133 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
134 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
135 struct intel_memory_region *
136 intel_memory_region_by_type(struct drm_i915_private *i915,
137 			    enum intel_memory_type mem_type);
138 
139 __printf(2, 3) void
140 intel_memory_region_set_name(struct intel_memory_region *mem,
141 			     const char *fmt, ...);
142 
143 #endif
144