xref: /OK3568_Linux_fs/kernel/include/linux/memory_group_manager.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2019-2022 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 #ifndef _MEMORY_GROUP_MANAGER_H_
23 #define _MEMORY_GROUP_MANAGER_H_
24 
25 #include <linux/mm.h>
26 #include <linux/of.h>
27 #include <linux/version.h>
28 
29 #if (KERNEL_VERSION(4, 17, 0) > LINUX_VERSION_CODE)
30 typedef int vm_fault_t;
31 #endif
32 
33 #define MEMORY_GROUP_MANAGER_NR_GROUPS (16)
34 
35 struct memory_group_manager_device;
36 struct memory_group_manager_import_data;
37 
38 /**
39  * struct memory_group_manager_ops - Callbacks for memory group manager
40  *                                   operations
41  *
42  * @mgm_alloc_page:           Callback to allocate physical memory in a group
43  * @mgm_free_page:            Callback to free physical memory in a group
44  * @mgm_get_import_memory_id: Callback to get the group ID for imported memory
45  * @mgm_update_gpu_pte:       Callback to modify a GPU page table entry
46  * @mgm_pte_to_original_pte:  Callback to get the original PTE entry as given
47  *                            to mgm_update_gpu_pte
48  * @mgm_vmf_insert_pfn_prot:  Callback to map a physical memory page for the CPU
49  */
50 struct memory_group_manager_ops {
51 	/*
52 	 * mgm_alloc_page - Allocate a physical memory page in a group
53 	 *
54 	 * @mgm_dev:  The memory group manager through which the request is
55 	 *            being made.
56 	 * @group_id: A physical memory group ID. The meaning of this is defined
57 	 *            by the systems integrator. Its valid range is
58 	 *            0 .. MEMORY_GROUP_MANAGER_NR_GROUPS-1.
59 	 * @gfp_mask: Bitmask of Get Free Page flags affecting allocator
60 	 *            behavior.
61 	 * @order:    Page order for physical page size (order=0 means 4 KiB,
62 	 *            order=9 means 2 MiB).
63 	 *
64 	 * Return: Pointer to allocated page, or NULL if allocation failed.
65 	 */
66 	struct page *(*mgm_alloc_page)(
67 		struct memory_group_manager_device *mgm_dev, int group_id,
68 		gfp_t gfp_mask, unsigned int order);
69 
70 	/*
71 	 * mgm_free_page - Free a physical memory page in a group
72 	 *
73 	 * @mgm_dev:  The memory group manager through which the request
74 	 *            is being made.
75 	 * @group_id: A physical memory group ID. The meaning of this is
76 	 *            defined by the systems integrator. Its valid range is
77 	 *            0 .. MEMORY_GROUP_MANAGER_NR_GROUPS-1.
78 	 * @page:     Address of the struct associated with a page of physical
79 	 *            memory that was allocated by calling the mgm_alloc_page
80 	 *            method of the same memory pool with the same values of
81 	 *            @group_id and @order.
82 	 * @order:    Page order for physical page size (order=0 means 4 KiB,
83 	 *            order=9 means 2 MiB).
84 	 */
85 	void (*mgm_free_page)(
86 		struct memory_group_manager_device *mgm_dev, int group_id,
87 		struct page *page, unsigned int order);
88 
89 	/*
90 	 * mgm_get_import_memory_id - Get the physical memory group ID for the
91 	 *                            imported memory
92 	 *
93 	 * @mgm_dev:     The memory group manager through which the request
94 	 *               is being made.
95 	 * @import_data: Pointer to the data which describes imported memory.
96 	 *
97 	 * Note that provision of this call back is optional, where it is not
98 	 * provided this call back pointer must be set to NULL to indicate it
99 	 * is not in use.
100 	 *
101 	 * Return: The memory group ID to use when mapping pages from this
102 	 *         imported memory.
103 	 */
104 	int (*mgm_get_import_memory_id)(
105 		struct memory_group_manager_device *mgm_dev,
106 		struct memory_group_manager_import_data *import_data);
107 
108 	/*
109 	 * mgm_update_gpu_pte - Modify a GPU page table entry for a memory group
110 	 *
111 	 * @mgm_dev:   The memory group manager through which the request
112 	 *             is being made.
113 	 * @group_id:  A physical memory group ID. The meaning of this is
114 	 *             defined by the systems integrator. Its valid range is
115 	 *             0 .. MEMORY_GROUP_MANAGER_NR_GROUPS-1.
116 	 * @mmu_level: The level of the page table entry in @ate.
117 	 * @pte:       The page table entry to modify, in LPAE or AArch64 format
118 	 *             (depending on the driver's configuration). This should be
119 	 *             decoded to determine the physical address and any other
120 	 *             properties of the mapping the manager requires.
121 	 *
122 	 * This function allows the memory group manager to modify a GPU page
123 	 * table entry before it is stored by the kbase module (controller
124 	 * driver). It may set certain bits in the page table entry attributes
125 	 * or modify the physical address, based on the physical memory group ID
126 	 * and/or additional data in struct memory_group_manager_device.
127 	 *
128 	 * Return: A modified GPU page table entry to be stored in a page table.
129 	 */
130 	u64 (*mgm_update_gpu_pte)(struct memory_group_manager_device *mgm_dev,
131 			int group_id, int mmu_level, u64 pte);
132 
133 	/*
134 	 * mgm_pte_to_original_pte - Undo any modification done during mgm_update_gpu_pte()
135 	 *
136 	 * @mgm_dev:   The memory group manager through which the request
137 	 *             is being made.
138 	 * @group_id:  A physical memory group ID. The meaning of this is
139 	 *             defined by the systems integrator. Its valid range is
140 	 *             0 .. MEMORY_GROUP_MANAGER_NR_GROUPS-1.
141 	 * @mmu_level: The level of the page table entry in @ate.
142 	 * @pte:       The page table entry to restore the original representation for,
143 	 *             in LPAE or AArch64 format (depending on the driver's configuration).
144 	 *
145 	 * Undo any modifications done during mgm_update_gpu_pte().
146 	 * This function allows getting back the original PTE entry as given
147 	 * to mgm_update_gpu_pte().
148 	 *
149 	 * Return: PTE entry as originally specified to mgm_update_gpu_pte()
150 	 */
151 	u64 (*mgm_pte_to_original_pte)(struct memory_group_manager_device *mgm_dev, int group_id,
152 				       int mmu_level, u64 pte);
153 
154 	/*
155 	 * mgm_vmf_insert_pfn_prot - Map a physical page in a group for the CPU
156 	 *
157 	 * @mgm_dev:   The memory group manager through which the request
158 	 *             is being made.
159 	 * @group_id:  A physical memory group ID. The meaning of this is
160 	 *             defined by the systems integrator. Its valid range is
161 	 *             0 .. MEMORY_GROUP_MANAGER_NR_GROUPS-1.
162 	 * @vma:       The virtual memory area to insert the page into.
163 	 * @addr:      A virtual address (in @vma) to assign to the page.
164 	 * @pfn:       The kernel Page Frame Number to insert at @addr in @vma.
165 	 * @pgprot:    Protection flags for the inserted page.
166 	 *
167 	 * Called from a CPU virtual memory page fault handler. This function
168 	 * creates a page table entry from the given parameter values and stores
169 	 * it at the appropriate location (unlike mgm_update_gpu_pte, which
170 	 * returns a modified entry).
171 	 *
172 	 * Return: Type of fault that occurred or VM_FAULT_NOPAGE if the page
173 	 *         table entry was successfully installed.
174 	 */
175 	vm_fault_t (*mgm_vmf_insert_pfn_prot)(
176 		struct memory_group_manager_device *mgm_dev, int group_id,
177 		struct vm_area_struct *vma, unsigned long addr,
178 		unsigned long pfn, pgprot_t pgprot);
179 };
180 
181 /**
182  * struct memory_group_manager_device - Device structure for a memory group
183  *                                      manager
184  *
185  * @ops:   Callbacks associated with this device
186  * @data:  Pointer to device private data
187  * @owner: pointer to owning module
188  *
189  * In order for a systems integrator to provide custom behaviors for memory
190  * operations performed by the kbase module (controller driver), they must
191  * provide a platform-specific driver module which implements this interface.
192  *
193  * This structure should be registered with the platform device using
194  * platform_set_drvdata().
195  */
196 struct memory_group_manager_device {
197 	struct memory_group_manager_ops ops;
198 	void *data;
199 	struct module *owner;
200 };
201 
202 
203 enum memory_group_manager_import_type {
204 	MEMORY_GROUP_MANAGER_IMPORT_TYPE_DMA_BUF
205 };
206 
207 /**
208  * struct memory_group_manager_import_data - Structure describing the imported
209  *                                           memory
210  *
211  * @type:      type of imported memory
212  * @u:         Union describing the imported memory
213  * @u.dma_buf: imported memory
214  *
215  */
216 struct memory_group_manager_import_data {
217 	enum memory_group_manager_import_type type;
218 	union {
219 		struct dma_buf *dma_buf;
220 	} u;
221 };
222 
223 #endif /* _MEMORY_GROUP_MANAGER_H_ */
224