xref: /linux/drivers/gpu/drm/i915/intel_memory_region.h (revision b77e0ce62d63a761ffb7f7245a215a49f5921c2f)
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 struct intel_memory_region_ops {
55 	unsigned int flags;
56 
57 	int (*init)(struct intel_memory_region *mem);
58 	void (*release)(struct intel_memory_region *mem);
59 
60 	int (*init_object)(struct intel_memory_region *mem,
61 			   struct drm_i915_gem_object *obj,
62 			   resource_size_t size,
63 			   unsigned int flags);
64 };
65 
66 struct intel_memory_region {
67 	struct drm_i915_private *i915;
68 
69 	const struct intel_memory_region_ops *ops;
70 
71 	struct io_mapping iomap;
72 	struct resource region;
73 
74 	/* For fake LMEM */
75 	struct drm_mm_node fake_mappable;
76 
77 	struct i915_buddy_mm mm;
78 	struct mutex mm_lock;
79 
80 	struct kref kref;
81 
82 	resource_size_t io_start;
83 	resource_size_t min_page_size;
84 	resource_size_t total;
85 	resource_size_t avail;
86 
87 	unsigned int type;
88 	unsigned int instance;
89 	unsigned int id;
90 	char name[8];
91 
92 	dma_addr_t remap_addr;
93 
94 	struct {
95 		struct mutex lock; /* Protects access to objects */
96 		struct list_head list;
97 		struct list_head purgeable;
98 	} objects;
99 };
100 
101 int intel_memory_region_init_buddy(struct intel_memory_region *mem);
102 void intel_memory_region_release_buddy(struct intel_memory_region *mem);
103 
104 int __intel_memory_region_get_pages_buddy(struct intel_memory_region *mem,
105 					  resource_size_t size,
106 					  unsigned int flags,
107 					  struct list_head *blocks);
108 struct i915_buddy_block *
109 __intel_memory_region_get_block_buddy(struct intel_memory_region *mem,
110 				      resource_size_t size,
111 				      unsigned int flags);
112 void __intel_memory_region_put_pages_buddy(struct intel_memory_region *mem,
113 					   struct list_head *blocks);
114 void __intel_memory_region_put_block_buddy(struct i915_buddy_block *block);
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