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_SYSTEM,
29 };
30 
31 enum intel_region_id {
32 	INTEL_REGION_SMEM = 0,
33 	INTEL_REGION_LMEM,
34 	INTEL_REGION_STOLEN_SMEM,
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 
42 #define I915_ALLOC_MIN_PAGE_SIZE  BIT(0)
43 #define I915_ALLOC_CONTIGUOUS     BIT(1)
44 
45 #define for_each_memory_region(mr, i915, id) \
46 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
47 		for_each_if((mr) = (i915)->mm.regions[id])
48 
49 struct intel_memory_region_ops {
50 	unsigned int flags;
51 
52 	int (*init)(struct intel_memory_region *mem);
53 	void (*release)(struct intel_memory_region *mem);
54 
55 	int (*init_object)(struct intel_memory_region *mem,
56 			   struct drm_i915_gem_object *obj,
57 			   resource_size_t size,
58 			   unsigned int flags);
59 };
60 
61 struct intel_memory_region {
62 	struct drm_i915_private *i915;
63 
64 	const struct intel_memory_region_ops *ops;
65 
66 	struct io_mapping iomap;
67 	struct resource region;
68 
69 	/* For fake LMEM */
70 	struct drm_mm_node fake_mappable;
71 
72 	struct i915_buddy_mm mm;
73 	struct mutex mm_lock;
74 
75 	struct kref kref;
76 
77 	resource_size_t io_start;
78 	resource_size_t min_page_size;
79 	resource_size_t total;
80 	resource_size_t avail;
81 
82 	u16 type;
83 	u16 instance;
84 	enum intel_region_id id;
85 	char name[8];
86 
87 	struct list_head reserved;
88 
89 	dma_addr_t remap_addr;
90 
91 	struct {
92 		struct mutex lock; /* Protects access to objects */
93 		struct list_head list;
94 		struct list_head purgeable;
95 	} objects;
96 };
97 
98 int intel_memory_region_init_buddy(struct intel_memory_region *mem);
99 void intel_memory_region_release_buddy(struct intel_memory_region *mem);
100 
101 int __intel_memory_region_get_pages_buddy(struct intel_memory_region *mem,
102 					  resource_size_t size,
103 					  unsigned int flags,
104 					  struct list_head *blocks);
105 struct i915_buddy_block *
106 __intel_memory_region_get_block_buddy(struct intel_memory_region *mem,
107 				      resource_size_t size,
108 				      unsigned int flags);
109 void __intel_memory_region_put_pages_buddy(struct intel_memory_region *mem,
110 					   struct list_head *blocks);
111 void __intel_memory_region_put_block_buddy(struct i915_buddy_block *block);
112 
113 int intel_memory_region_reserve(struct intel_memory_region *mem,
114 				u64 offset, u64 size);
115 
116 struct intel_memory_region *
117 intel_memory_region_create(struct drm_i915_private *i915,
118 			   resource_size_t start,
119 			   resource_size_t size,
120 			   resource_size_t min_page_size,
121 			   resource_size_t io_start,
122 			   const struct intel_memory_region_ops *ops);
123 
124 struct intel_memory_region *
125 intel_memory_region_get(struct intel_memory_region *mem);
126 void intel_memory_region_put(struct intel_memory_region *mem);
127 
128 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
129 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
130 struct intel_memory_region *
131 intel_memory_region_by_type(struct drm_i915_private *i915,
132 			    enum intel_memory_type mem_type);
133 
134 __printf(2, 3) void
135 intel_memory_region_set_name(struct intel_memory_region *mem,
136 			     const char *fmt, ...);
137 
138 #endif
139