14a488a7aSOded Gabbay /*
24a488a7aSOded Gabbay  * Copyright 2014 Advanced Micro Devices, Inc.
34a488a7aSOded Gabbay  *
44a488a7aSOded Gabbay  * Permission is hereby granted, free of charge, to any person obtaining a
54a488a7aSOded Gabbay  * copy of this software and associated documentation files (the "Software"),
64a488a7aSOded Gabbay  * to deal in the Software without restriction, including without limitation
74a488a7aSOded Gabbay  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84a488a7aSOded Gabbay  * and/or sell copies of the Software, and to permit persons to whom the
94a488a7aSOded Gabbay  * Software is furnished to do so, subject to the following conditions:
104a488a7aSOded Gabbay  *
114a488a7aSOded Gabbay  * The above copyright notice and this permission notice shall be included in
124a488a7aSOded Gabbay  * all copies or substantial portions of the Software.
134a488a7aSOded Gabbay  *
144a488a7aSOded Gabbay  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154a488a7aSOded Gabbay  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164a488a7aSOded Gabbay  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174a488a7aSOded Gabbay  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184a488a7aSOded Gabbay  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194a488a7aSOded Gabbay  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204a488a7aSOded Gabbay  * OTHER DEALINGS IN THE SOFTWARE.
214a488a7aSOded Gabbay  */
224a488a7aSOded Gabbay 
2364d1c3a4SFelix Kuehling #if defined(CONFIG_AMD_IOMMU_V2_MODULE) || defined(CONFIG_AMD_IOMMU_V2)
244a488a7aSOded Gabbay #include <linux/amd-iommu.h>
2564d1c3a4SFelix Kuehling #endif
264a488a7aSOded Gabbay #include <linux/bsearch.h>
274a488a7aSOded Gabbay #include <linux/pci.h>
284a488a7aSOded Gabbay #include <linux/slab.h>
294a488a7aSOded Gabbay #include "kfd_priv.h"
3064c7f8cfSBen Goz #include "kfd_device_queue_manager.h"
31507968ddSFelix Kuehling #include "kfd_pm4_headers_vi.h"
32373d7080SFelix Kuehling #include "cwsr_trap_handler_gfx8.asm"
3364d1c3a4SFelix Kuehling #include "kfd_iommu.h"
344a488a7aSOded Gabbay 
3519f6d2a6SOded Gabbay #define MQD_SIZE_ALIGNED 768
3626103436SFelix Kuehling static atomic_t kfd_device_suspended = ATOMIC_INIT(0);
3719f6d2a6SOded Gabbay 
3864d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
394a488a7aSOded Gabbay static const struct kfd_device_info kaveri_device_info = {
400da7558cSBen Goz 	.asic_family = CHIP_KAVERI,
410da7558cSBen Goz 	.max_pasid_bits = 16,
42992839adSYair Shachar 	/* max num of queues for KV.TODO should be a dynamic value */
43992839adSYair Shachar 	.max_no_of_hqd	= 24,
44*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
450da7558cSBen Goz 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
46f3a39818SAndrew Lewycky 	.event_interrupt_class = &event_interrupt_class_cik,
47fbeb661bSYair Shachar 	.num_of_watch_points = 4,
48373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
49373d7080SFelix Kuehling 	.supports_cwsr = false,
5064d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
513ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
520da7558cSBen Goz };
530da7558cSBen Goz 
540da7558cSBen Goz static const struct kfd_device_info carrizo_device_info = {
550da7558cSBen Goz 	.asic_family = CHIP_CARRIZO,
564a488a7aSOded Gabbay 	.max_pasid_bits = 16,
57eaccd6e7SOded Gabbay 	/* max num of queues for CZ.TODO should be a dynamic value */
58eaccd6e7SOded Gabbay 	.max_no_of_hqd	= 24,
59*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
60b3f5e6b4SAndrew Lewycky 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
61eaccd6e7SOded Gabbay 	.event_interrupt_class = &event_interrupt_class_cik,
62f7c826adSAlexey Skidanov 	.num_of_watch_points = 4,
63373d7080SFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
64373d7080SFelix Kuehling 	.supports_cwsr = true,
6564d1c3a4SFelix Kuehling 	.needs_iommu_device = true,
663ee2d00cSFelix Kuehling 	.needs_pci_atomics = false,
674a488a7aSOded Gabbay };
6864d1c3a4SFelix Kuehling #endif
694a488a7aSOded Gabbay 
70a3084e6cSFelix Kuehling static const struct kfd_device_info hawaii_device_info = {
71a3084e6cSFelix Kuehling 	.asic_family = CHIP_HAWAII,
72a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
73a3084e6cSFelix Kuehling 	/* max num of queues for KV.TODO should be a dynamic value */
74a3084e6cSFelix Kuehling 	.max_no_of_hqd	= 24,
75*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
76a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
77a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
78a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
79a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
80a3084e6cSFelix Kuehling 	.supports_cwsr = false,
8164d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
82a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
83a3084e6cSFelix Kuehling };
84a3084e6cSFelix Kuehling 
85a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_device_info = {
86a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
87a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
88a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
89*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
90a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
91a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
92a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
93a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
94a3084e6cSFelix Kuehling 	.supports_cwsr = false,
9564d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
96a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
97a3084e6cSFelix Kuehling };
98a3084e6cSFelix Kuehling 
99a3084e6cSFelix Kuehling static const struct kfd_device_info tonga_vf_device_info = {
100a3084e6cSFelix Kuehling 	.asic_family = CHIP_TONGA,
101a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
102a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
103*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
104a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
105a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
106a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
107a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
108a3084e6cSFelix Kuehling 	.supports_cwsr = false,
10964d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
110a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
111a3084e6cSFelix Kuehling };
112a3084e6cSFelix Kuehling 
113a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_device_info = {
114a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
115a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
116a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
117*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
118a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
119a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
120a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
121a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
122a3084e6cSFelix Kuehling 	.supports_cwsr = true,
12364d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
124a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
125a3084e6cSFelix Kuehling };
126a3084e6cSFelix Kuehling 
127a3084e6cSFelix Kuehling static const struct kfd_device_info fiji_vf_device_info = {
128a3084e6cSFelix Kuehling 	.asic_family = CHIP_FIJI,
129a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
130a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
131*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
132a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
133a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
134a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
135a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
136a3084e6cSFelix Kuehling 	.supports_cwsr = true,
13764d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
138a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
139a3084e6cSFelix Kuehling };
140a3084e6cSFelix Kuehling 
141a3084e6cSFelix Kuehling 
142a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_device_info = {
143a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
144a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
145a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
146*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
147a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
148a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
149a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
150a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
151a3084e6cSFelix Kuehling 	.supports_cwsr = true,
15264d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
153a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
154a3084e6cSFelix Kuehling };
155a3084e6cSFelix Kuehling 
156a3084e6cSFelix Kuehling static const struct kfd_device_info polaris10_vf_device_info = {
157a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS10,
158a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
159a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
160*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
161a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
162a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
163a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
164a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
165a3084e6cSFelix Kuehling 	.supports_cwsr = true,
16664d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
167a3084e6cSFelix Kuehling 	.needs_pci_atomics = false,
168a3084e6cSFelix Kuehling };
169a3084e6cSFelix Kuehling 
170a3084e6cSFelix Kuehling static const struct kfd_device_info polaris11_device_info = {
171a3084e6cSFelix Kuehling 	.asic_family = CHIP_POLARIS11,
172a3084e6cSFelix Kuehling 	.max_pasid_bits = 16,
173a3084e6cSFelix Kuehling 	.max_no_of_hqd  = 24,
174*ada2b29cSFelix Kuehling 	.doorbell_size  = 4,
175a3084e6cSFelix Kuehling 	.ih_ring_entry_size = 4 * sizeof(uint32_t),
176a3084e6cSFelix Kuehling 	.event_interrupt_class = &event_interrupt_class_cik,
177a3084e6cSFelix Kuehling 	.num_of_watch_points = 4,
178a3084e6cSFelix Kuehling 	.mqd_size_aligned = MQD_SIZE_ALIGNED,
179a3084e6cSFelix Kuehling 	.supports_cwsr = true,
18064d1c3a4SFelix Kuehling 	.needs_iommu_device = false,
181a3084e6cSFelix Kuehling 	.needs_pci_atomics = true,
182a3084e6cSFelix Kuehling };
183a3084e6cSFelix Kuehling 
184a3084e6cSFelix Kuehling 
1854a488a7aSOded Gabbay struct kfd_deviceid {
1864a488a7aSOded Gabbay 	unsigned short did;
1874a488a7aSOded Gabbay 	const struct kfd_device_info *device_info;
1884a488a7aSOded Gabbay };
1894a488a7aSOded Gabbay 
1904a488a7aSOded Gabbay static const struct kfd_deviceid supported_devices[] = {
19164d1c3a4SFelix Kuehling #ifdef KFD_SUPPORT_IOMMU_V2
1924a488a7aSOded Gabbay 	{ 0x1304, &kaveri_device_info },	/* Kaveri */
1934a488a7aSOded Gabbay 	{ 0x1305, &kaveri_device_info },	/* Kaveri */
1944a488a7aSOded Gabbay 	{ 0x1306, &kaveri_device_info },	/* Kaveri */
1954a488a7aSOded Gabbay 	{ 0x1307, &kaveri_device_info },	/* Kaveri */
1964a488a7aSOded Gabbay 	{ 0x1309, &kaveri_device_info },	/* Kaveri */
1974a488a7aSOded Gabbay 	{ 0x130A, &kaveri_device_info },	/* Kaveri */
1984a488a7aSOded Gabbay 	{ 0x130B, &kaveri_device_info },	/* Kaveri */
1994a488a7aSOded Gabbay 	{ 0x130C, &kaveri_device_info },	/* Kaveri */
2004a488a7aSOded Gabbay 	{ 0x130D, &kaveri_device_info },	/* Kaveri */
2014a488a7aSOded Gabbay 	{ 0x130E, &kaveri_device_info },	/* Kaveri */
2024a488a7aSOded Gabbay 	{ 0x130F, &kaveri_device_info },	/* Kaveri */
2034a488a7aSOded Gabbay 	{ 0x1310, &kaveri_device_info },	/* Kaveri */
2044a488a7aSOded Gabbay 	{ 0x1311, &kaveri_device_info },	/* Kaveri */
2054a488a7aSOded Gabbay 	{ 0x1312, &kaveri_device_info },	/* Kaveri */
2064a488a7aSOded Gabbay 	{ 0x1313, &kaveri_device_info },	/* Kaveri */
2074a488a7aSOded Gabbay 	{ 0x1315, &kaveri_device_info },	/* Kaveri */
2084a488a7aSOded Gabbay 	{ 0x1316, &kaveri_device_info },	/* Kaveri */
2094a488a7aSOded Gabbay 	{ 0x1317, &kaveri_device_info },	/* Kaveri */
2104a488a7aSOded Gabbay 	{ 0x1318, &kaveri_device_info },	/* Kaveri */
2114a488a7aSOded Gabbay 	{ 0x131B, &kaveri_device_info },	/* Kaveri */
2124a488a7aSOded Gabbay 	{ 0x131C, &kaveri_device_info },	/* Kaveri */
213123576d1SBen Goz 	{ 0x131D, &kaveri_device_info },	/* Kaveri */
214123576d1SBen Goz 	{ 0x9870, &carrizo_device_info },	/* Carrizo */
215123576d1SBen Goz 	{ 0x9874, &carrizo_device_info },	/* Carrizo */
216123576d1SBen Goz 	{ 0x9875, &carrizo_device_info },	/* Carrizo */
217123576d1SBen Goz 	{ 0x9876, &carrizo_device_info },	/* Carrizo */
218a3084e6cSFelix Kuehling 	{ 0x9877, &carrizo_device_info },	/* Carrizo */
21964d1c3a4SFelix Kuehling #endif
220a3084e6cSFelix Kuehling 	{ 0x67A0, &hawaii_device_info },	/* Hawaii */
221a3084e6cSFelix Kuehling 	{ 0x67A1, &hawaii_device_info },	/* Hawaii */
222a3084e6cSFelix Kuehling 	{ 0x67A2, &hawaii_device_info },	/* Hawaii */
223a3084e6cSFelix Kuehling 	{ 0x67A8, &hawaii_device_info },	/* Hawaii */
224a3084e6cSFelix Kuehling 	{ 0x67A9, &hawaii_device_info },	/* Hawaii */
225a3084e6cSFelix Kuehling 	{ 0x67AA, &hawaii_device_info },	/* Hawaii */
226a3084e6cSFelix Kuehling 	{ 0x67B0, &hawaii_device_info },	/* Hawaii */
227a3084e6cSFelix Kuehling 	{ 0x67B1, &hawaii_device_info },	/* Hawaii */
228a3084e6cSFelix Kuehling 	{ 0x67B8, &hawaii_device_info },	/* Hawaii */
229a3084e6cSFelix Kuehling 	{ 0x67B9, &hawaii_device_info },	/* Hawaii */
230a3084e6cSFelix Kuehling 	{ 0x67BA, &hawaii_device_info },	/* Hawaii */
231a3084e6cSFelix Kuehling 	{ 0x67BE, &hawaii_device_info },	/* Hawaii */
232a3084e6cSFelix Kuehling 	{ 0x6920, &tonga_device_info },		/* Tonga */
233a3084e6cSFelix Kuehling 	{ 0x6921, &tonga_device_info },		/* Tonga */
234a3084e6cSFelix Kuehling 	{ 0x6928, &tonga_device_info },		/* Tonga */
235a3084e6cSFelix Kuehling 	{ 0x6929, &tonga_device_info },		/* Tonga */
236a3084e6cSFelix Kuehling 	{ 0x692B, &tonga_device_info },		/* Tonga */
237a3084e6cSFelix Kuehling 	{ 0x692F, &tonga_vf_device_info },	/* Tonga vf */
238a3084e6cSFelix Kuehling 	{ 0x6938, &tonga_device_info },		/* Tonga */
239a3084e6cSFelix Kuehling 	{ 0x6939, &tonga_device_info },		/* Tonga */
240a3084e6cSFelix Kuehling 	{ 0x7300, &fiji_device_info },		/* Fiji */
241a3084e6cSFelix Kuehling 	{ 0x730F, &fiji_vf_device_info },	/* Fiji vf*/
242a3084e6cSFelix Kuehling 	{ 0x67C0, &polaris10_device_info },	/* Polaris10 */
243a3084e6cSFelix Kuehling 	{ 0x67C1, &polaris10_device_info },	/* Polaris10 */
244a3084e6cSFelix Kuehling 	{ 0x67C2, &polaris10_device_info },	/* Polaris10 */
245a3084e6cSFelix Kuehling 	{ 0x67C4, &polaris10_device_info },	/* Polaris10 */
246a3084e6cSFelix Kuehling 	{ 0x67C7, &polaris10_device_info },	/* Polaris10 */
247a3084e6cSFelix Kuehling 	{ 0x67C8, &polaris10_device_info },	/* Polaris10 */
248a3084e6cSFelix Kuehling 	{ 0x67C9, &polaris10_device_info },	/* Polaris10 */
249a3084e6cSFelix Kuehling 	{ 0x67CA, &polaris10_device_info },	/* Polaris10 */
250a3084e6cSFelix Kuehling 	{ 0x67CC, &polaris10_device_info },	/* Polaris10 */
251a3084e6cSFelix Kuehling 	{ 0x67CF, &polaris10_device_info },	/* Polaris10 */
252a3084e6cSFelix Kuehling 	{ 0x67D0, &polaris10_vf_device_info },	/* Polaris10 vf*/
253a3084e6cSFelix Kuehling 	{ 0x67DF, &polaris10_device_info },	/* Polaris10 */
254a3084e6cSFelix Kuehling 	{ 0x67E0, &polaris11_device_info },	/* Polaris11 */
255a3084e6cSFelix Kuehling 	{ 0x67E1, &polaris11_device_info },	/* Polaris11 */
256a3084e6cSFelix Kuehling 	{ 0x67E3, &polaris11_device_info },	/* Polaris11 */
257a3084e6cSFelix Kuehling 	{ 0x67E7, &polaris11_device_info },	/* Polaris11 */
258a3084e6cSFelix Kuehling 	{ 0x67E8, &polaris11_device_info },	/* Polaris11 */
259a3084e6cSFelix Kuehling 	{ 0x67E9, &polaris11_device_info },	/* Polaris11 */
260a3084e6cSFelix Kuehling 	{ 0x67EB, &polaris11_device_info },	/* Polaris11 */
261a3084e6cSFelix Kuehling 	{ 0x67EF, &polaris11_device_info },	/* Polaris11 */
262a3084e6cSFelix Kuehling 	{ 0x67FF, &polaris11_device_info },	/* Polaris11 */
2634a488a7aSOded Gabbay };
2644a488a7aSOded Gabbay 
2656e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
2666e81090bSOded Gabbay 				unsigned int chunk_size);
2676e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
2686e81090bSOded Gabbay 
269b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd);
270b8935a7cSYong Zhao 
2714a488a7aSOded Gabbay static const struct kfd_device_info *lookup_device_info(unsigned short did)
2724a488a7aSOded Gabbay {
2734a488a7aSOded Gabbay 	size_t i;
2744a488a7aSOded Gabbay 
2754a488a7aSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
2764a488a7aSOded Gabbay 		if (supported_devices[i].did == did) {
27732fa8219SFelix Kuehling 			WARN_ON(!supported_devices[i].device_info);
2784a488a7aSOded Gabbay 			return supported_devices[i].device_info;
2794a488a7aSOded Gabbay 		}
2804a488a7aSOded Gabbay 	}
2814a488a7aSOded Gabbay 
2824ebc7182SYong Zhao 	dev_warn(kfd_device, "DID %04x is missing in supported_devices\n",
2834ebc7182SYong Zhao 		 did);
2844ebc7182SYong Zhao 
2854a488a7aSOded Gabbay 	return NULL;
2864a488a7aSOded Gabbay }
2874a488a7aSOded Gabbay 
288cea405b1SXihan Zhang struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd,
289cea405b1SXihan Zhang 	struct pci_dev *pdev, const struct kfd2kgd_calls *f2g)
2904a488a7aSOded Gabbay {
2914a488a7aSOded Gabbay 	struct kfd_dev *kfd;
2924a488a7aSOded Gabbay 
2934a488a7aSOded Gabbay 	const struct kfd_device_info *device_info =
2944a488a7aSOded Gabbay 					lookup_device_info(pdev->device);
2954a488a7aSOded Gabbay 
2964ebc7182SYong Zhao 	if (!device_info) {
2974ebc7182SYong Zhao 		dev_err(kfd_device, "kgd2kfd_probe failed\n");
2984a488a7aSOded Gabbay 		return NULL;
2994ebc7182SYong Zhao 	}
3004a488a7aSOded Gabbay 
3013ee2d00cSFelix Kuehling 	if (device_info->needs_pci_atomics) {
3023ee2d00cSFelix Kuehling 		/* Allow BIF to recode atomics to PCIe 3.0
3033ee2d00cSFelix Kuehling 		 * AtomicOps. 32 and 64-bit requests are possible and
3043ee2d00cSFelix Kuehling 		 * must be supported.
3053ee2d00cSFelix Kuehling 		 */
3063ee2d00cSFelix Kuehling 		if (pci_enable_atomic_ops_to_root(pdev,
3073ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3083ee2d00cSFelix Kuehling 				PCI_EXP_DEVCAP2_ATOMIC_COMP64) < 0) {
3093ee2d00cSFelix Kuehling 			dev_info(kfd_device,
3103ee2d00cSFelix Kuehling 				"skipped device %x:%x, PCI rejects atomics",
3113ee2d00cSFelix Kuehling 				 pdev->vendor, pdev->device);
3123ee2d00cSFelix Kuehling 			return NULL;
3133ee2d00cSFelix Kuehling 		}
3143ee2d00cSFelix Kuehling 	}
3153ee2d00cSFelix Kuehling 
3164a488a7aSOded Gabbay 	kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
3174a488a7aSOded Gabbay 	if (!kfd)
3184a488a7aSOded Gabbay 		return NULL;
3194a488a7aSOded Gabbay 
3204a488a7aSOded Gabbay 	kfd->kgd = kgd;
3214a488a7aSOded Gabbay 	kfd->device_info = device_info;
3224a488a7aSOded Gabbay 	kfd->pdev = pdev;
32319f6d2a6SOded Gabbay 	kfd->init_complete = false;
324cea405b1SXihan Zhang 	kfd->kfd2kgd = f2g;
325cea405b1SXihan Zhang 
326cea405b1SXihan Zhang 	mutex_init(&kfd->doorbell_mutex);
327cea405b1SXihan Zhang 	memset(&kfd->doorbell_available_index, 0,
328cea405b1SXihan Zhang 		sizeof(kfd->doorbell_available_index));
3294a488a7aSOded Gabbay 
3304a488a7aSOded Gabbay 	return kfd;
3314a488a7aSOded Gabbay }
3324a488a7aSOded Gabbay 
333373d7080SFelix Kuehling static void kfd_cwsr_init(struct kfd_dev *kfd)
334373d7080SFelix Kuehling {
335373d7080SFelix Kuehling 	if (cwsr_enable && kfd->device_info->supports_cwsr) {
336373d7080SFelix Kuehling 		BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
337373d7080SFelix Kuehling 
338373d7080SFelix Kuehling 		kfd->cwsr_isa = cwsr_trap_gfx8_hex;
339373d7080SFelix Kuehling 		kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
340373d7080SFelix Kuehling 		kfd->cwsr_enabled = true;
341373d7080SFelix Kuehling 	}
342373d7080SFelix Kuehling }
343373d7080SFelix Kuehling 
3444a488a7aSOded Gabbay bool kgd2kfd_device_init(struct kfd_dev *kfd,
3454a488a7aSOded Gabbay 			 const struct kgd2kfd_shared_resources *gpu_resources)
3464a488a7aSOded Gabbay {
34719f6d2a6SOded Gabbay 	unsigned int size;
34819f6d2a6SOded Gabbay 
3494a488a7aSOded Gabbay 	kfd->shared_resources = *gpu_resources;
3504a488a7aSOded Gabbay 
35144008d7aSYong Zhao 	kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
35244008d7aSYong Zhao 	kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
35344008d7aSYong Zhao 	kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
35444008d7aSYong Zhao 			- kfd->vm_info.first_vmid_kfd + 1;
35544008d7aSYong Zhao 
356a99c6d4fSFelix Kuehling 	/* Verify module parameters regarding mapped process number*/
357a99c6d4fSFelix Kuehling 	if ((hws_max_conc_proc < 0)
358a99c6d4fSFelix Kuehling 			|| (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
359a99c6d4fSFelix Kuehling 		dev_err(kfd_device,
360a99c6d4fSFelix Kuehling 			"hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
361a99c6d4fSFelix Kuehling 			hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
362a99c6d4fSFelix Kuehling 			kfd->vm_info.vmid_num_kfd);
363a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
364a99c6d4fSFelix Kuehling 	} else
365a99c6d4fSFelix Kuehling 		kfd->max_proc_per_quantum = hws_max_conc_proc;
366a99c6d4fSFelix Kuehling 
36719f6d2a6SOded Gabbay 	/* calculate max size of mqds needed for queues */
368b8cbab04SOded Gabbay 	size = max_num_of_queues_per_device *
36919f6d2a6SOded Gabbay 			kfd->device_info->mqd_size_aligned;
37019f6d2a6SOded Gabbay 
371e18e794eSOded Gabbay 	/*
372e18e794eSOded Gabbay 	 * calculate max size of runlist packet.
373e18e794eSOded Gabbay 	 * There can be only 2 packets at once
374e18e794eSOded Gabbay 	 */
375507968ddSFelix Kuehling 	size += (KFD_MAX_NUM_OF_PROCESSES * sizeof(struct pm4_mes_map_process) +
376507968ddSFelix Kuehling 		max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
377507968ddSFelix Kuehling 		+ sizeof(struct pm4_mes_runlist)) * 2;
378e18e794eSOded Gabbay 
379e18e794eSOded Gabbay 	/* Add size of HIQ & DIQ */
380e18e794eSOded Gabbay 	size += KFD_KERNEL_QUEUE_SIZE * 2;
381e18e794eSOded Gabbay 
382e18e794eSOded Gabbay 	/* add another 512KB for all other allocations on gart (HPD, fences) */
38319f6d2a6SOded Gabbay 	size += 512 * 1024;
38419f6d2a6SOded Gabbay 
385cea405b1SXihan Zhang 	if (kfd->kfd2kgd->init_gtt_mem_allocation(
386cea405b1SXihan Zhang 			kfd->kgd, size, &kfd->gtt_mem,
38773a1da0bSOded Gabbay 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr)){
38879775b62SKent Russell 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
38919f6d2a6SOded Gabbay 		goto out;
39019f6d2a6SOded Gabbay 	}
39119f6d2a6SOded Gabbay 
39279775b62SKent Russell 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
393e18e794eSOded Gabbay 
39473a1da0bSOded Gabbay 	/* Initialize GTT sa with 512 byte chunk size */
39573a1da0bSOded Gabbay 	if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
39679775b62SKent Russell 		dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
39773a1da0bSOded Gabbay 		goto kfd_gtt_sa_init_error;
39873a1da0bSOded Gabbay 	}
39973a1da0bSOded Gabbay 
400735df2baSFelix Kuehling 	if (kfd_doorbell_init(kfd)) {
401735df2baSFelix Kuehling 		dev_err(kfd_device,
402735df2baSFelix Kuehling 			"Error initializing doorbell aperture\n");
403735df2baSFelix Kuehling 		goto kfd_doorbell_error;
404735df2baSFelix Kuehling 	}
40519f6d2a6SOded Gabbay 
4064eacc26bSKent Russell 	if (kfd_topology_add_device(kfd)) {
40779775b62SKent Russell 		dev_err(kfd_device, "Error adding device to topology\n");
40819f6d2a6SOded Gabbay 		goto kfd_topology_add_device_error;
40919f6d2a6SOded Gabbay 	}
41019f6d2a6SOded Gabbay 
4112249d558SAndrew Lewycky 	if (kfd_interrupt_init(kfd)) {
41279775b62SKent Russell 		dev_err(kfd_device, "Error initializing interrupts\n");
4132249d558SAndrew Lewycky 		goto kfd_interrupt_error;
4142249d558SAndrew Lewycky 	}
4152249d558SAndrew Lewycky 
41664c7f8cfSBen Goz 	kfd->dqm = device_queue_manager_init(kfd);
41764c7f8cfSBen Goz 	if (!kfd->dqm) {
41879775b62SKent Russell 		dev_err(kfd_device, "Error initializing queue manager\n");
41964c7f8cfSBen Goz 		goto device_queue_manager_error;
42064c7f8cfSBen Goz 	}
42164c7f8cfSBen Goz 
42264d1c3a4SFelix Kuehling 	if (kfd_iommu_device_init(kfd)) {
42364d1c3a4SFelix Kuehling 		dev_err(kfd_device, "Error initializing iommuv2\n");
42464d1c3a4SFelix Kuehling 		goto device_iommu_error;
42564c7f8cfSBen Goz 	}
42664c7f8cfSBen Goz 
427373d7080SFelix Kuehling 	kfd_cwsr_init(kfd);
428373d7080SFelix Kuehling 
429b8935a7cSYong Zhao 	if (kfd_resume(kfd))
430b8935a7cSYong Zhao 		goto kfd_resume_error;
431b8935a7cSYong Zhao 
432fbeb661bSYair Shachar 	kfd->dbgmgr = NULL;
433fbeb661bSYair Shachar 
4344a488a7aSOded Gabbay 	kfd->init_complete = true;
43579775b62SKent Russell 	dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
4364a488a7aSOded Gabbay 		 kfd->pdev->device);
4374a488a7aSOded Gabbay 
43879775b62SKent Russell 	pr_debug("Starting kfd with the following scheduling policy %d\n",
439d146c5a7SFelix Kuehling 		kfd->dqm->sched_policy);
44064c7f8cfSBen Goz 
44119f6d2a6SOded Gabbay 	goto out;
44219f6d2a6SOded Gabbay 
443b8935a7cSYong Zhao kfd_resume_error:
44464d1c3a4SFelix Kuehling device_iommu_error:
44564c7f8cfSBen Goz 	device_queue_manager_uninit(kfd->dqm);
44664c7f8cfSBen Goz device_queue_manager_error:
4472249d558SAndrew Lewycky 	kfd_interrupt_exit(kfd);
4482249d558SAndrew Lewycky kfd_interrupt_error:
449b17f068aSOded Gabbay 	kfd_topology_remove_device(kfd);
45019f6d2a6SOded Gabbay kfd_topology_add_device_error:
451735df2baSFelix Kuehling 	kfd_doorbell_fini(kfd);
452735df2baSFelix Kuehling kfd_doorbell_error:
45373a1da0bSOded Gabbay 	kfd_gtt_sa_fini(kfd);
45473a1da0bSOded Gabbay kfd_gtt_sa_init_error:
455cea405b1SXihan Zhang 	kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
45619f6d2a6SOded Gabbay 	dev_err(kfd_device,
45779775b62SKent Russell 		"device %x:%x NOT added due to errors\n",
45819f6d2a6SOded Gabbay 		kfd->pdev->vendor, kfd->pdev->device);
45919f6d2a6SOded Gabbay out:
46019f6d2a6SOded Gabbay 	return kfd->init_complete;
4614a488a7aSOded Gabbay }
4624a488a7aSOded Gabbay 
4634a488a7aSOded Gabbay void kgd2kfd_device_exit(struct kfd_dev *kfd)
4644a488a7aSOded Gabbay {
465b17f068aSOded Gabbay 	if (kfd->init_complete) {
466b8935a7cSYong Zhao 		kgd2kfd_suspend(kfd);
46764c7f8cfSBen Goz 		device_queue_manager_uninit(kfd->dqm);
4682249d558SAndrew Lewycky 		kfd_interrupt_exit(kfd);
46919f6d2a6SOded Gabbay 		kfd_topology_remove_device(kfd);
470735df2baSFelix Kuehling 		kfd_doorbell_fini(kfd);
47173a1da0bSOded Gabbay 		kfd_gtt_sa_fini(kfd);
472cea405b1SXihan Zhang 		kfd->kfd2kgd->free_gtt_mem(kfd->kgd, kfd->gtt_mem);
473b17f068aSOded Gabbay 	}
4745b5c4e40SEvgeny Pinchuk 
4754a488a7aSOded Gabbay 	kfree(kfd);
4764a488a7aSOded Gabbay }
4774a488a7aSOded Gabbay 
4784a488a7aSOded Gabbay void kgd2kfd_suspend(struct kfd_dev *kfd)
4794a488a7aSOded Gabbay {
480733fa1f7SYong Zhao 	if (!kfd->init_complete)
481733fa1f7SYong Zhao 		return;
482733fa1f7SYong Zhao 
48326103436SFelix Kuehling 	/* For first KFD device suspend all the KFD processes */
48426103436SFelix Kuehling 	if (atomic_inc_return(&kfd_device_suspended) == 1)
48526103436SFelix Kuehling 		kfd_suspend_all_processes();
48626103436SFelix Kuehling 
48745c9a5e4SOded Gabbay 	kfd->dqm->ops.stop(kfd->dqm);
488733fa1f7SYong Zhao 
48964d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
4904a488a7aSOded Gabbay }
4914a488a7aSOded Gabbay 
4924a488a7aSOded Gabbay int kgd2kfd_resume(struct kfd_dev *kfd)
4934a488a7aSOded Gabbay {
49426103436SFelix Kuehling 	int ret, count;
49526103436SFelix Kuehling 
496b8935a7cSYong Zhao 	if (!kfd->init_complete)
497b8935a7cSYong Zhao 		return 0;
498b17f068aSOded Gabbay 
49926103436SFelix Kuehling 	ret = kfd_resume(kfd);
50026103436SFelix Kuehling 	if (ret)
50126103436SFelix Kuehling 		return ret;
502b17f068aSOded Gabbay 
50326103436SFelix Kuehling 	count = atomic_dec_return(&kfd_device_suspended);
50426103436SFelix Kuehling 	WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
50526103436SFelix Kuehling 	if (count == 0)
50626103436SFelix Kuehling 		ret = kfd_resume_all_processes();
50726103436SFelix Kuehling 
50826103436SFelix Kuehling 	return ret;
5094ebc7182SYong Zhao }
5104ebc7182SYong Zhao 
511b8935a7cSYong Zhao static int kfd_resume(struct kfd_dev *kfd)
512b8935a7cSYong Zhao {
513b8935a7cSYong Zhao 	int err = 0;
514b8935a7cSYong Zhao 
51564d1c3a4SFelix Kuehling 	err = kfd_iommu_resume(kfd);
51664d1c3a4SFelix Kuehling 	if (err) {
51764d1c3a4SFelix Kuehling 		dev_err(kfd_device,
51864d1c3a4SFelix Kuehling 			"Failed to resume IOMMU for device %x:%x\n",
51964d1c3a4SFelix Kuehling 			kfd->pdev->vendor, kfd->pdev->device);
52064d1c3a4SFelix Kuehling 		return err;
52164d1c3a4SFelix Kuehling 	}
522733fa1f7SYong Zhao 
523b8935a7cSYong Zhao 	err = kfd->dqm->ops.start(kfd->dqm);
524b8935a7cSYong Zhao 	if (err) {
525b8935a7cSYong Zhao 		dev_err(kfd_device,
526b8935a7cSYong Zhao 			"Error starting queue manager for device %x:%x\n",
527b8935a7cSYong Zhao 			kfd->pdev->vendor, kfd->pdev->device);
528b8935a7cSYong Zhao 		goto dqm_start_error;
529b17f068aSOded Gabbay 	}
530b17f068aSOded Gabbay 
531b8935a7cSYong Zhao 	return err;
532b8935a7cSYong Zhao 
533b8935a7cSYong Zhao dqm_start_error:
53464d1c3a4SFelix Kuehling 	kfd_iommu_suspend(kfd);
535b8935a7cSYong Zhao 	return err;
5364a488a7aSOded Gabbay }
5374a488a7aSOded Gabbay 
538b3f5e6b4SAndrew Lewycky /* This is called directly from KGD at ISR. */
539b3f5e6b4SAndrew Lewycky void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
5404a488a7aSOded Gabbay {
5412249d558SAndrew Lewycky 	if (!kfd->init_complete)
5422249d558SAndrew Lewycky 		return;
5432249d558SAndrew Lewycky 
5442249d558SAndrew Lewycky 	spin_lock(&kfd->interrupt_lock);
5452249d558SAndrew Lewycky 
5462249d558SAndrew Lewycky 	if (kfd->interrupts_active
5472249d558SAndrew Lewycky 	    && interrupt_is_wanted(kfd, ih_ring_entry)
5482249d558SAndrew Lewycky 	    && enqueue_ih_ring_entry(kfd, ih_ring_entry))
54948e876a2SAndres Rodriguez 		queue_work(kfd->ih_wq, &kfd->interrupt_work);
5502249d558SAndrew Lewycky 
5512249d558SAndrew Lewycky 	spin_unlock(&kfd->interrupt_lock);
5524a488a7aSOded Gabbay }
5536e81090bSOded Gabbay 
5546b95e797SFelix Kuehling int kgd2kfd_quiesce_mm(struct mm_struct *mm)
5556b95e797SFelix Kuehling {
5566b95e797SFelix Kuehling 	struct kfd_process *p;
5576b95e797SFelix Kuehling 	int r;
5586b95e797SFelix Kuehling 
5596b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
5606b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
5616b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
5626b95e797SFelix Kuehling 	 */
5636b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
5646b95e797SFelix Kuehling 	if (!p)
5656b95e797SFelix Kuehling 		return -ESRCH;
5666b95e797SFelix Kuehling 
5676b95e797SFelix Kuehling 	r = kfd_process_evict_queues(p);
5686b95e797SFelix Kuehling 
5696b95e797SFelix Kuehling 	kfd_unref_process(p);
5706b95e797SFelix Kuehling 	return r;
5716b95e797SFelix Kuehling }
5726b95e797SFelix Kuehling 
5736b95e797SFelix Kuehling int kgd2kfd_resume_mm(struct mm_struct *mm)
5746b95e797SFelix Kuehling {
5756b95e797SFelix Kuehling 	struct kfd_process *p;
5766b95e797SFelix Kuehling 	int r;
5776b95e797SFelix Kuehling 
5786b95e797SFelix Kuehling 	/* Because we are called from arbitrary context (workqueue) as opposed
5796b95e797SFelix Kuehling 	 * to process context, kfd_process could attempt to exit while we are
5806b95e797SFelix Kuehling 	 * running so the lookup function increments the process ref count.
5816b95e797SFelix Kuehling 	 */
5826b95e797SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
5836b95e797SFelix Kuehling 	if (!p)
5846b95e797SFelix Kuehling 		return -ESRCH;
5856b95e797SFelix Kuehling 
5866b95e797SFelix Kuehling 	r = kfd_process_restore_queues(p);
5876b95e797SFelix Kuehling 
5886b95e797SFelix Kuehling 	kfd_unref_process(p);
5896b95e797SFelix Kuehling 	return r;
5906b95e797SFelix Kuehling }
5916b95e797SFelix Kuehling 
59226103436SFelix Kuehling /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
59326103436SFelix Kuehling  *   prepare for safe eviction of KFD BOs that belong to the specified
59426103436SFelix Kuehling  *   process.
59526103436SFelix Kuehling  *
59626103436SFelix Kuehling  * @mm: mm_struct that identifies the specified KFD process
59726103436SFelix Kuehling  * @fence: eviction fence attached to KFD process BOs
59826103436SFelix Kuehling  *
59926103436SFelix Kuehling  */
60026103436SFelix Kuehling int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
60126103436SFelix Kuehling 					       struct dma_fence *fence)
60226103436SFelix Kuehling {
60326103436SFelix Kuehling 	struct kfd_process *p;
60426103436SFelix Kuehling 	unsigned long active_time;
60526103436SFelix Kuehling 	unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
60626103436SFelix Kuehling 
60726103436SFelix Kuehling 	if (!fence)
60826103436SFelix Kuehling 		return -EINVAL;
60926103436SFelix Kuehling 
61026103436SFelix Kuehling 	if (dma_fence_is_signaled(fence))
61126103436SFelix Kuehling 		return 0;
61226103436SFelix Kuehling 
61326103436SFelix Kuehling 	p = kfd_lookup_process_by_mm(mm);
61426103436SFelix Kuehling 	if (!p)
61526103436SFelix Kuehling 		return -ENODEV;
61626103436SFelix Kuehling 
61726103436SFelix Kuehling 	if (fence->seqno == p->last_eviction_seqno)
61826103436SFelix Kuehling 		goto out;
61926103436SFelix Kuehling 
62026103436SFelix Kuehling 	p->last_eviction_seqno = fence->seqno;
62126103436SFelix Kuehling 
62226103436SFelix Kuehling 	/* Avoid KFD process starvation. Wait for at least
62326103436SFelix Kuehling 	 * PROCESS_ACTIVE_TIME_MS before evicting the process again
62426103436SFelix Kuehling 	 */
62526103436SFelix Kuehling 	active_time = get_jiffies_64() - p->last_restore_timestamp;
62626103436SFelix Kuehling 	if (delay_jiffies > active_time)
62726103436SFelix Kuehling 		delay_jiffies -= active_time;
62826103436SFelix Kuehling 	else
62926103436SFelix Kuehling 		delay_jiffies = 0;
63026103436SFelix Kuehling 
63126103436SFelix Kuehling 	/* During process initialization eviction_work.dwork is initialized
63226103436SFelix Kuehling 	 * to kfd_evict_bo_worker
63326103436SFelix Kuehling 	 */
63426103436SFelix Kuehling 	schedule_delayed_work(&p->eviction_work, delay_jiffies);
63526103436SFelix Kuehling out:
63626103436SFelix Kuehling 	kfd_unref_process(p);
63726103436SFelix Kuehling 	return 0;
63826103436SFelix Kuehling }
63926103436SFelix Kuehling 
6406e81090bSOded Gabbay static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
6416e81090bSOded Gabbay 				unsigned int chunk_size)
6426e81090bSOded Gabbay {
6438625ff9cSFelix Kuehling 	unsigned int num_of_longs;
6446e81090bSOded Gabbay 
64532fa8219SFelix Kuehling 	if (WARN_ON(buf_size < chunk_size))
64632fa8219SFelix Kuehling 		return -EINVAL;
64732fa8219SFelix Kuehling 	if (WARN_ON(buf_size == 0))
64832fa8219SFelix Kuehling 		return -EINVAL;
64932fa8219SFelix Kuehling 	if (WARN_ON(chunk_size == 0))
65032fa8219SFelix Kuehling 		return -EINVAL;
6516e81090bSOded Gabbay 
6526e81090bSOded Gabbay 	kfd->gtt_sa_chunk_size = chunk_size;
6536e81090bSOded Gabbay 	kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
6546e81090bSOded Gabbay 
6558625ff9cSFelix Kuehling 	num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
6568625ff9cSFelix Kuehling 		BITS_PER_LONG;
6576e81090bSOded Gabbay 
6588625ff9cSFelix Kuehling 	kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
6596e81090bSOded Gabbay 
6606e81090bSOded Gabbay 	if (!kfd->gtt_sa_bitmap)
6616e81090bSOded Gabbay 		return -ENOMEM;
6626e81090bSOded Gabbay 
66379775b62SKent Russell 	pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
6646e81090bSOded Gabbay 			kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
6656e81090bSOded Gabbay 
6666e81090bSOded Gabbay 	mutex_init(&kfd->gtt_sa_lock);
6676e81090bSOded Gabbay 
6686e81090bSOded Gabbay 	return 0;
6696e81090bSOded Gabbay 
6706e81090bSOded Gabbay }
6716e81090bSOded Gabbay 
6726e81090bSOded Gabbay static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
6736e81090bSOded Gabbay {
6746e81090bSOded Gabbay 	mutex_destroy(&kfd->gtt_sa_lock);
6756e81090bSOded Gabbay 	kfree(kfd->gtt_sa_bitmap);
6766e81090bSOded Gabbay }
6776e81090bSOded Gabbay 
6786e81090bSOded Gabbay static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
6796e81090bSOded Gabbay 						unsigned int bit_num,
6806e81090bSOded Gabbay 						unsigned int chunk_size)
6816e81090bSOded Gabbay {
6826e81090bSOded Gabbay 	return start_addr + bit_num * chunk_size;
6836e81090bSOded Gabbay }
6846e81090bSOded Gabbay 
6856e81090bSOded Gabbay static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
6866e81090bSOded Gabbay 						unsigned int bit_num,
6876e81090bSOded Gabbay 						unsigned int chunk_size)
6886e81090bSOded Gabbay {
6896e81090bSOded Gabbay 	return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
6906e81090bSOded Gabbay }
6916e81090bSOded Gabbay 
6926e81090bSOded Gabbay int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
6936e81090bSOded Gabbay 			struct kfd_mem_obj **mem_obj)
6946e81090bSOded Gabbay {
6956e81090bSOded Gabbay 	unsigned int found, start_search, cur_size;
6966e81090bSOded Gabbay 
6976e81090bSOded Gabbay 	if (size == 0)
6986e81090bSOded Gabbay 		return -EINVAL;
6996e81090bSOded Gabbay 
7006e81090bSOded Gabbay 	if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
7016e81090bSOded Gabbay 		return -ENOMEM;
7026e81090bSOded Gabbay 
703d1853f42SFelix Kuehling 	*mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_NOIO);
7046e81090bSOded Gabbay 	if ((*mem_obj) == NULL)
7056e81090bSOded Gabbay 		return -ENOMEM;
7066e81090bSOded Gabbay 
70779775b62SKent Russell 	pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
7086e81090bSOded Gabbay 
7096e81090bSOded Gabbay 	start_search = 0;
7106e81090bSOded Gabbay 
7116e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
7126e81090bSOded Gabbay 
7136e81090bSOded Gabbay kfd_gtt_restart_search:
7146e81090bSOded Gabbay 	/* Find the first chunk that is free */
7156e81090bSOded Gabbay 	found = find_next_zero_bit(kfd->gtt_sa_bitmap,
7166e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks,
7176e81090bSOded Gabbay 					start_search);
7186e81090bSOded Gabbay 
71979775b62SKent Russell 	pr_debug("Found = %d\n", found);
7206e81090bSOded Gabbay 
7216e81090bSOded Gabbay 	/* If there wasn't any free chunk, bail out */
7226e81090bSOded Gabbay 	if (found == kfd->gtt_sa_num_of_chunks)
7236e81090bSOded Gabbay 		goto kfd_gtt_no_free_chunk;
7246e81090bSOded Gabbay 
7256e81090bSOded Gabbay 	/* Update fields of mem_obj */
7266e81090bSOded Gabbay 	(*mem_obj)->range_start = found;
7276e81090bSOded Gabbay 	(*mem_obj)->range_end = found;
7286e81090bSOded Gabbay 	(*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
7296e81090bSOded Gabbay 					kfd->gtt_start_gpu_addr,
7306e81090bSOded Gabbay 					found,
7316e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7326e81090bSOded Gabbay 	(*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
7336e81090bSOded Gabbay 					kfd->gtt_start_cpu_ptr,
7346e81090bSOded Gabbay 					found,
7356e81090bSOded Gabbay 					kfd->gtt_sa_chunk_size);
7366e81090bSOded Gabbay 
73779775b62SKent Russell 	pr_debug("gpu_addr = %p, cpu_addr = %p\n",
7386e81090bSOded Gabbay 			(uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
7396e81090bSOded Gabbay 
7406e81090bSOded Gabbay 	/* If we need only one chunk, mark it as allocated and get out */
7416e81090bSOded Gabbay 	if (size <= kfd->gtt_sa_chunk_size) {
74279775b62SKent Russell 		pr_debug("Single bit\n");
7436e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7446e81090bSOded Gabbay 		goto kfd_gtt_out;
7456e81090bSOded Gabbay 	}
7466e81090bSOded Gabbay 
7476e81090bSOded Gabbay 	/* Otherwise, try to see if we have enough contiguous chunks */
7486e81090bSOded Gabbay 	cur_size = size - kfd->gtt_sa_chunk_size;
7496e81090bSOded Gabbay 	do {
7506e81090bSOded Gabbay 		(*mem_obj)->range_end =
7516e81090bSOded Gabbay 			find_next_zero_bit(kfd->gtt_sa_bitmap,
7526e81090bSOded Gabbay 					kfd->gtt_sa_num_of_chunks, ++found);
7536e81090bSOded Gabbay 		/*
7546e81090bSOded Gabbay 		 * If next free chunk is not contiguous than we need to
7556e81090bSOded Gabbay 		 * restart our search from the last free chunk we found (which
7566e81090bSOded Gabbay 		 * wasn't contiguous to the previous ones
7576e81090bSOded Gabbay 		 */
7586e81090bSOded Gabbay 		if ((*mem_obj)->range_end != found) {
7596e81090bSOded Gabbay 			start_search = found;
7606e81090bSOded Gabbay 			goto kfd_gtt_restart_search;
7616e81090bSOded Gabbay 		}
7626e81090bSOded Gabbay 
7636e81090bSOded Gabbay 		/*
7646e81090bSOded Gabbay 		 * If we reached end of buffer, bail out with error
7656e81090bSOded Gabbay 		 */
7666e81090bSOded Gabbay 		if (found == kfd->gtt_sa_num_of_chunks)
7676e81090bSOded Gabbay 			goto kfd_gtt_no_free_chunk;
7686e81090bSOded Gabbay 
7696e81090bSOded Gabbay 		/* Check if we don't need another chunk */
7706e81090bSOded Gabbay 		if (cur_size <= kfd->gtt_sa_chunk_size)
7716e81090bSOded Gabbay 			cur_size = 0;
7726e81090bSOded Gabbay 		else
7736e81090bSOded Gabbay 			cur_size -= kfd->gtt_sa_chunk_size;
7746e81090bSOded Gabbay 
7756e81090bSOded Gabbay 	} while (cur_size > 0);
7766e81090bSOded Gabbay 
77779775b62SKent Russell 	pr_debug("range_start = %d, range_end = %d\n",
7786e81090bSOded Gabbay 		(*mem_obj)->range_start, (*mem_obj)->range_end);
7796e81090bSOded Gabbay 
7806e81090bSOded Gabbay 	/* Mark the chunks as allocated */
7816e81090bSOded Gabbay 	for (found = (*mem_obj)->range_start;
7826e81090bSOded Gabbay 		found <= (*mem_obj)->range_end;
7836e81090bSOded Gabbay 		found++)
7846e81090bSOded Gabbay 		set_bit(found, kfd->gtt_sa_bitmap);
7856e81090bSOded Gabbay 
7866e81090bSOded Gabbay kfd_gtt_out:
7876e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
7886e81090bSOded Gabbay 	return 0;
7896e81090bSOded Gabbay 
7906e81090bSOded Gabbay kfd_gtt_no_free_chunk:
79179775b62SKent Russell 	pr_debug("Allocation failed with mem_obj = %p\n", mem_obj);
7926e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
7936e81090bSOded Gabbay 	kfree(mem_obj);
7946e81090bSOded Gabbay 	return -ENOMEM;
7956e81090bSOded Gabbay }
7966e81090bSOded Gabbay 
7976e81090bSOded Gabbay int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
7986e81090bSOded Gabbay {
7996e81090bSOded Gabbay 	unsigned int bit;
8006e81090bSOded Gabbay 
8019216ed29SOded Gabbay 	/* Act like kfree when trying to free a NULL object */
8029216ed29SOded Gabbay 	if (!mem_obj)
8039216ed29SOded Gabbay 		return 0;
8046e81090bSOded Gabbay 
80579775b62SKent Russell 	pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
8066e81090bSOded Gabbay 			mem_obj, mem_obj->range_start, mem_obj->range_end);
8076e81090bSOded Gabbay 
8086e81090bSOded Gabbay 	mutex_lock(&kfd->gtt_sa_lock);
8096e81090bSOded Gabbay 
8106e81090bSOded Gabbay 	/* Mark the chunks as free */
8116e81090bSOded Gabbay 	for (bit = mem_obj->range_start;
8126e81090bSOded Gabbay 		bit <= mem_obj->range_end;
8136e81090bSOded Gabbay 		bit++)
8146e81090bSOded Gabbay 		clear_bit(bit, kfd->gtt_sa_bitmap);
8156e81090bSOded Gabbay 
8166e81090bSOded Gabbay 	mutex_unlock(&kfd->gtt_sa_lock);
8176e81090bSOded Gabbay 
8186e81090bSOded Gabbay 	kfree(mem_obj);
8196e81090bSOded Gabbay 	return 0;
8206e81090bSOded Gabbay }
821