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 /**
51  * Memory regions encoded as type | instance
52  */
53 extern const u32 intel_region_map[];
54 
55 struct intel_memory_region_ops {
56 	unsigned int flags;
57 
58 	int (*init)(struct intel_memory_region *mem);
59 	void (*release)(struct intel_memory_region *mem);
60 
61 	struct drm_i915_gem_object *
62 	(*create_object)(struct intel_memory_region *mem,
63 			 resource_size_t size,
64 			 unsigned int flags);
65 };
66 
67 struct intel_memory_region {
68 	struct drm_i915_private *i915;
69 
70 	const struct intel_memory_region_ops *ops;
71 
72 	struct io_mapping iomap;
73 	struct resource region;
74 
75 	/* For fake LMEM */
76 	struct drm_mm_node fake_mappable;
77 
78 	struct i915_buddy_mm mm;
79 	struct mutex mm_lock;
80 
81 	struct kref kref;
82 
83 	resource_size_t io_start;
84 	resource_size_t min_page_size;
85 
86 	unsigned int type;
87 	unsigned int instance;
88 	unsigned int id;
89 
90 	dma_addr_t remap_addr;
91 
92 	struct {
93 		struct mutex lock; /* Protects access to objects */
94 		struct list_head list;
95 		struct list_head purgeable;
96 	} objects;
97 };
98 
99 int intel_memory_region_init_buddy(struct intel_memory_region *mem);
100 void intel_memory_region_release_buddy(struct intel_memory_region *mem);
101 
102 int __intel_memory_region_get_pages_buddy(struct intel_memory_region *mem,
103 					  resource_size_t size,
104 					  unsigned int flags,
105 					  struct list_head *blocks);
106 struct i915_buddy_block *
107 __intel_memory_region_get_block_buddy(struct intel_memory_region *mem,
108 				      resource_size_t size,
109 				      unsigned int flags);
110 void __intel_memory_region_put_pages_buddy(struct intel_memory_region *mem,
111 					   struct list_head *blocks);
112 void __intel_memory_region_put_block_buddy(struct i915_buddy_block *block);
113 
114 struct intel_memory_region *
115 intel_memory_region_create(struct drm_i915_private *i915,
116 			   resource_size_t start,
117 			   resource_size_t size,
118 			   resource_size_t min_page_size,
119 			   resource_size_t io_start,
120 			   const struct intel_memory_region_ops *ops);
121 
122 struct intel_memory_region *
123 intel_memory_region_get(struct intel_memory_region *mem);
124 void intel_memory_region_put(struct intel_memory_region *mem);
125 
126 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
127 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
128 
129 #endif
130