blob: 33c30dc21d677bea953ee333ffec90e56c6dd4e4 [file] [log] [blame]
Oded Gabbay4a488a72014-07-16 21:08:55 +03001/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#include <linux/amd-iommu.h>
24#include <linux/bsearch.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include "kfd_priv.h"
Ben Goz64c7f8c2014-07-17 01:27:00 +030028#include "kfd_device_queue_manager.h"
Oded Gabbay4a488a72014-07-16 21:08:55 +030029
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030030#define MQD_SIZE_ALIGNED 768
31
Oded Gabbay4a488a72014-07-16 21:08:55 +030032static const struct kfd_device_info kaveri_device_info = {
33 .max_pasid_bits = 16,
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +030034 .ih_ring_entry_size = 4 * sizeof(uint32_t),
Alexey Skidanovf7c826a2014-10-13 16:35:12 +030035 .num_of_watch_points = 4,
Oded Gabbay19f6d2a2014-07-16 23:25:31 +030036 .mqd_size_aligned = MQD_SIZE_ALIGNED
Oded Gabbay4a488a72014-07-16 21:08:55 +030037};
38
39struct kfd_deviceid {
40 unsigned short did;
41 const struct kfd_device_info *device_info;
42};
43
44/* Please keep this sorted by increasing device id. */
45static const struct kfd_deviceid supported_devices[] = {
46 { 0x1304, &kaveri_device_info }, /* Kaveri */
47 { 0x1305, &kaveri_device_info }, /* Kaveri */
48 { 0x1306, &kaveri_device_info }, /* Kaveri */
49 { 0x1307, &kaveri_device_info }, /* Kaveri */
50 { 0x1309, &kaveri_device_info }, /* Kaveri */
51 { 0x130A, &kaveri_device_info }, /* Kaveri */
52 { 0x130B, &kaveri_device_info }, /* Kaveri */
53 { 0x130C, &kaveri_device_info }, /* Kaveri */
54 { 0x130D, &kaveri_device_info }, /* Kaveri */
55 { 0x130E, &kaveri_device_info }, /* Kaveri */
56 { 0x130F, &kaveri_device_info }, /* Kaveri */
57 { 0x1310, &kaveri_device_info }, /* Kaveri */
58 { 0x1311, &kaveri_device_info }, /* Kaveri */
59 { 0x1312, &kaveri_device_info }, /* Kaveri */
60 { 0x1313, &kaveri_device_info }, /* Kaveri */
61 { 0x1315, &kaveri_device_info }, /* Kaveri */
62 { 0x1316, &kaveri_device_info }, /* Kaveri */
63 { 0x1317, &kaveri_device_info }, /* Kaveri */
64 { 0x1318, &kaveri_device_info }, /* Kaveri */
65 { 0x131B, &kaveri_device_info }, /* Kaveri */
66 { 0x131C, &kaveri_device_info }, /* Kaveri */
67 { 0x131D, &kaveri_device_info }, /* Kaveri */
68};
69
Oded Gabbay6e810902014-10-27 14:36:07 +020070static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
71 unsigned int chunk_size);
72static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
73
Oded Gabbay4a488a72014-07-16 21:08:55 +030074static const struct kfd_device_info *lookup_device_info(unsigned short did)
75{
76 size_t i;
77
78 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
79 if (supported_devices[i].did == did) {
80 BUG_ON(supported_devices[i].device_info == NULL);
81 return supported_devices[i].device_info;
82 }
83 }
84
85 return NULL;
86}
87
88struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev)
89{
90 struct kfd_dev *kfd;
91
92 const struct kfd_device_info *device_info =
93 lookup_device_info(pdev->device);
94
95 if (!device_info)
96 return NULL;
97
98 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
99 if (!kfd)
100 return NULL;
101
102 kfd->kgd = kgd;
103 kfd->device_info = device_info;
104 kfd->pdev = pdev;
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300105 kfd->init_complete = false;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300106
107 return kfd;
108}
109
Oded Gabbayb17f0682014-07-17 00:06:27 +0300110static bool device_iommu_pasid_init(struct kfd_dev *kfd)
111{
112 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
113 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
114 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
115
116 struct amd_iommu_device_info iommu_info;
117 unsigned int pasid_limit;
118 int err;
119
120 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
121 if (err < 0) {
122 dev_err(kfd_device,
123 "error getting iommu info. is the iommu enabled?\n");
124 return false;
125 }
126
127 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
128 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
129 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
130 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
131 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
132 return false;
133 }
134
135 pasid_limit = min_t(unsigned int,
136 (unsigned int)1 << kfd->device_info->max_pasid_bits,
137 iommu_info.max_pasids);
138 /*
139 * last pasid is used for kernel queues doorbells
140 * in the future the last pasid might be used for a kernel thread.
141 */
142 pasid_limit = min_t(unsigned int,
143 pasid_limit,
144 kfd->doorbell_process_limit - 1);
145
146 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
147 if (err < 0) {
148 dev_err(kfd_device, "error initializing iommu device\n");
149 return false;
150 }
151
152 if (!kfd_set_pasid_limit(pasid_limit)) {
153 dev_err(kfd_device, "error setting pasid limit\n");
154 amd_iommu_free_device(kfd->pdev);
155 return false;
156 }
157
158 return true;
159}
160
161static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
162{
163 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
164
165 if (dev)
166 kfd_unbind_process_from_device(dev, pasid);
167}
168
Oded Gabbay4a488a72014-07-16 21:08:55 +0300169bool kgd2kfd_device_init(struct kfd_dev *kfd,
170 const struct kgd2kfd_shared_resources *gpu_resources)
171{
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300172 unsigned int size;
173
Oded Gabbay4a488a72014-07-16 21:08:55 +0300174 kfd->shared_resources = *gpu_resources;
175
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300176 /* calculate max size of mqds needed for queues */
177 size = max_num_of_processes *
178 max_num_of_queues_per_process *
179 kfd->device_info->mqd_size_aligned;
180
181 /* add another 512KB for all other allocations on gart */
182 size += 512 * 1024;
183
184 if (kfd2kgd->init_sa_manager(kfd->kgd, size)) {
185 dev_err(kfd_device,
186 "Error initializing sa manager for device (%x:%x)\n",
187 kfd->pdev->vendor, kfd->pdev->device);
188 goto out;
189 }
190
191 kfd_doorbell_init(kfd);
192
193 if (kfd_topology_add_device(kfd) != 0) {
194 dev_err(kfd_device,
195 "Error adding device (%x:%x) to topology\n",
196 kfd->pdev->vendor, kfd->pdev->device);
197 goto kfd_topology_add_device_error;
198 }
199
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300200 if (kfd_interrupt_init(kfd)) {
201 dev_err(kfd_device,
202 "Error initializing interrupts for device (%x:%x)\n",
203 kfd->pdev->vendor, kfd->pdev->device);
204 goto kfd_interrupt_error;
205 }
206
Oded Gabbayb17f0682014-07-17 00:06:27 +0300207 if (!device_iommu_pasid_init(kfd)) {
208 dev_err(kfd_device,
209 "Error initializing iommuv2 for device (%x:%x)\n",
210 kfd->pdev->vendor, kfd->pdev->device);
211 goto device_iommu_pasid_error;
212 }
213 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
214 iommu_pasid_shutdown_callback);
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300215
Ben Goz64c7f8c2014-07-17 01:27:00 +0300216 kfd->dqm = device_queue_manager_init(kfd);
217 if (!kfd->dqm) {
218 dev_err(kfd_device,
219 "Error initializing queue manager for device (%x:%x)\n",
220 kfd->pdev->vendor, kfd->pdev->device);
221 goto device_queue_manager_error;
222 }
223
224 if (kfd->dqm->start(kfd->dqm) != 0) {
225 dev_err(kfd_device,
226 "Error starting queuen manager for device (%x:%x)\n",
227 kfd->pdev->vendor, kfd->pdev->device);
228 goto dqm_start_error;
229 }
230
Oded Gabbay4a488a72014-07-16 21:08:55 +0300231 kfd->init_complete = true;
232 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
233 kfd->pdev->device);
234
Ben Goz64c7f8c2014-07-17 01:27:00 +0300235 pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
236 sched_policy);
237
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300238 goto out;
239
Ben Goz64c7f8c2014-07-17 01:27:00 +0300240dqm_start_error:
241 device_queue_manager_uninit(kfd->dqm);
242device_queue_manager_error:
243 amd_iommu_free_device(kfd->pdev);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300244device_iommu_pasid_error:
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300245 kfd_interrupt_exit(kfd);
246kfd_interrupt_error:
Oded Gabbayb17f0682014-07-17 00:06:27 +0300247 kfd_topology_remove_device(kfd);
Oded Gabbay19f6d2a2014-07-16 23:25:31 +0300248kfd_topology_add_device_error:
249 kfd2kgd->fini_sa_manager(kfd->kgd);
250 dev_err(kfd_device,
251 "device (%x:%x) NOT added due to errors\n",
252 kfd->pdev->vendor, kfd->pdev->device);
253out:
254 return kfd->init_complete;
Oded Gabbay4a488a72014-07-16 21:08:55 +0300255}
256
257void kgd2kfd_device_exit(struct kfd_dev *kfd)
258{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300259 if (kfd->init_complete) {
Ben Goz64c7f8c2014-07-17 01:27:00 +0300260 device_queue_manager_uninit(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300261 amd_iommu_free_device(kfd->pdev);
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300262 kfd_interrupt_exit(kfd);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300263 kfd_topology_remove_device(kfd);
264 }
Evgeny Pinchuk5b5c4e42014-07-16 21:22:32 +0300265
Oded Gabbay4a488a72014-07-16 21:08:55 +0300266 kfree(kfd);
267}
268
269void kgd2kfd_suspend(struct kfd_dev *kfd)
270{
271 BUG_ON(kfd == NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300272
Ben Goz64c7f8c2014-07-17 01:27:00 +0300273 if (kfd->init_complete) {
274 kfd->dqm->stop(kfd->dqm);
Oded Gabbayabc9d3e2014-11-09 22:36:22 +0200275 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300276 amd_iommu_free_device(kfd->pdev);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300277 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300278}
279
280int kgd2kfd_resume(struct kfd_dev *kfd)
281{
Oded Gabbayb17f0682014-07-17 00:06:27 +0300282 unsigned int pasid_limit;
283 int err;
284
Oded Gabbay4a488a72014-07-16 21:08:55 +0300285 BUG_ON(kfd == NULL);
286
Oded Gabbayb17f0682014-07-17 00:06:27 +0300287 pasid_limit = kfd_get_pasid_limit();
288
289 if (kfd->init_complete) {
290 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
291 if (err < 0)
292 return -ENXIO;
293 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
294 iommu_pasid_shutdown_callback);
Ben Goz64c7f8c2014-07-17 01:27:00 +0300295 kfd->dqm->start(kfd->dqm);
Oded Gabbayb17f0682014-07-17 00:06:27 +0300296 }
297
Oded Gabbay4a488a72014-07-16 21:08:55 +0300298 return 0;
299}
300
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300301/* This is called directly from KGD at ISR. */
302void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
Oded Gabbay4a488a72014-07-16 21:08:55 +0300303{
Andrew Lewyckyb3f5e6b2014-07-17 01:37:30 +0300304 if (kfd->init_complete) {
305 spin_lock(&kfd->interrupt_lock);
306
307 if (kfd->interrupts_active
308 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
309 schedule_work(&kfd->interrupt_work);
310
311 spin_unlock(&kfd->interrupt_lock);
312 }
Oded Gabbay4a488a72014-07-16 21:08:55 +0300313}
Oded Gabbay6e810902014-10-27 14:36:07 +0200314
315static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
316 unsigned int chunk_size)
317{
318 unsigned int num_of_bits;
319
320 BUG_ON(!kfd);
321 BUG_ON(!kfd->gtt_mem);
322 BUG_ON(buf_size < chunk_size);
323 BUG_ON(buf_size == 0);
324 BUG_ON(chunk_size == 0);
325
326 kfd->gtt_sa_chunk_size = chunk_size;
327 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
328
329 num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
330 BUG_ON(num_of_bits == 0);
331
332 kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
333
334 if (!kfd->gtt_sa_bitmap)
335 return -ENOMEM;
336
337 pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
338 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
339
340 mutex_init(&kfd->gtt_sa_lock);
341
342 return 0;
343
344}
345
346static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
347{
348 mutex_destroy(&kfd->gtt_sa_lock);
349 kfree(kfd->gtt_sa_bitmap);
350}
351
352static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
353 unsigned int bit_num,
354 unsigned int chunk_size)
355{
356 return start_addr + bit_num * chunk_size;
357}
358
359static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
360 unsigned int bit_num,
361 unsigned int chunk_size)
362{
363 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
364}
365
366int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
367 struct kfd_mem_obj **mem_obj)
368{
369 unsigned int found, start_search, cur_size;
370
371 BUG_ON(!kfd);
372
373 if (size == 0)
374 return -EINVAL;
375
376 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
377 return -ENOMEM;
378
379 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
380 if ((*mem_obj) == NULL)
381 return -ENOMEM;
382
383 pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
384
385 start_search = 0;
386
387 mutex_lock(&kfd->gtt_sa_lock);
388
389kfd_gtt_restart_search:
390 /* Find the first chunk that is free */
391 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
392 kfd->gtt_sa_num_of_chunks,
393 start_search);
394
395 pr_debug("kfd: found = %d\n", found);
396
397 /* If there wasn't any free chunk, bail out */
398 if (found == kfd->gtt_sa_num_of_chunks)
399 goto kfd_gtt_no_free_chunk;
400
401 /* Update fields of mem_obj */
402 (*mem_obj)->range_start = found;
403 (*mem_obj)->range_end = found;
404 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
405 kfd->gtt_start_gpu_addr,
406 found,
407 kfd->gtt_sa_chunk_size);
408 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
409 kfd->gtt_start_cpu_ptr,
410 found,
411 kfd->gtt_sa_chunk_size);
412
413 pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
414 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
415
416 /* If we need only one chunk, mark it as allocated and get out */
417 if (size <= kfd->gtt_sa_chunk_size) {
418 pr_debug("kfd: single bit\n");
419 set_bit(found, kfd->gtt_sa_bitmap);
420 goto kfd_gtt_out;
421 }
422
423 /* Otherwise, try to see if we have enough contiguous chunks */
424 cur_size = size - kfd->gtt_sa_chunk_size;
425 do {
426 (*mem_obj)->range_end =
427 find_next_zero_bit(kfd->gtt_sa_bitmap,
428 kfd->gtt_sa_num_of_chunks, ++found);
429 /*
430 * If next free chunk is not contiguous than we need to
431 * restart our search from the last free chunk we found (which
432 * wasn't contiguous to the previous ones
433 */
434 if ((*mem_obj)->range_end != found) {
435 start_search = found;
436 goto kfd_gtt_restart_search;
437 }
438
439 /*
440 * If we reached end of buffer, bail out with error
441 */
442 if (found == kfd->gtt_sa_num_of_chunks)
443 goto kfd_gtt_no_free_chunk;
444
445 /* Check if we don't need another chunk */
446 if (cur_size <= kfd->gtt_sa_chunk_size)
447 cur_size = 0;
448 else
449 cur_size -= kfd->gtt_sa_chunk_size;
450
451 } while (cur_size > 0);
452
453 pr_debug("kfd: range_start = %d, range_end = %d\n",
454 (*mem_obj)->range_start, (*mem_obj)->range_end);
455
456 /* Mark the chunks as allocated */
457 for (found = (*mem_obj)->range_start;
458 found <= (*mem_obj)->range_end;
459 found++)
460 set_bit(found, kfd->gtt_sa_bitmap);
461
462kfd_gtt_out:
463 mutex_unlock(&kfd->gtt_sa_lock);
464 return 0;
465
466kfd_gtt_no_free_chunk:
467 pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
468 mutex_unlock(&kfd->gtt_sa_lock);
469 kfree(mem_obj);
470 return -ENOMEM;
471}
472
473int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
474{
475 unsigned int bit;
476
477 BUG_ON(!kfd);
478 BUG_ON(!mem_obj);
479
480 pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
481 mem_obj, mem_obj->range_start, mem_obj->range_end);
482
483 mutex_lock(&kfd->gtt_sa_lock);
484
485 /* Mark the chunks as free */
486 for (bit = mem_obj->range_start;
487 bit <= mem_obj->range_end;
488 bit++)
489 clear_bit(bit, kfd->gtt_sa_bitmap);
490
491 mutex_unlock(&kfd->gtt_sa_lock);
492
493 kfree(mem_obj);
494 return 0;
495}