drm/amdkfd: Fixed calculation of gart buffer size
[deliverable/linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_device.c
CommitLineData
4a488a7a
OG
1/*
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"
64c7f8cf 28#include "kfd_device_queue_manager.h"
e18e794e 29#include "kfd_pm4_headers.h"
4a488a7a 30
19f6d2a6
OG
31#define MQD_SIZE_ALIGNED 768
32
4a488a7a
OG
33static const struct kfd_device_info kaveri_device_info = {
34 .max_pasid_bits = 16,
b3f5e6b4 35 .ih_ring_entry_size = 4 * sizeof(uint32_t),
f7c826ad 36 .num_of_watch_points = 4,
19f6d2a6 37 .mqd_size_aligned = MQD_SIZE_ALIGNED
4a488a7a
OG
38};
39
40struct kfd_deviceid {
41 unsigned short did;
42 const struct kfd_device_info *device_info;
43};
44
45/* Please keep this sorted by increasing device id. */
46static const struct kfd_deviceid supported_devices[] = {
47 { 0x1304, &kaveri_device_info }, /* Kaveri */
48 { 0x1305, &kaveri_device_info }, /* Kaveri */
49 { 0x1306, &kaveri_device_info }, /* Kaveri */
50 { 0x1307, &kaveri_device_info }, /* Kaveri */
51 { 0x1309, &kaveri_device_info }, /* Kaveri */
52 { 0x130A, &kaveri_device_info }, /* Kaveri */
53 { 0x130B, &kaveri_device_info }, /* Kaveri */
54 { 0x130C, &kaveri_device_info }, /* Kaveri */
55 { 0x130D, &kaveri_device_info }, /* Kaveri */
56 { 0x130E, &kaveri_device_info }, /* Kaveri */
57 { 0x130F, &kaveri_device_info }, /* Kaveri */
58 { 0x1310, &kaveri_device_info }, /* Kaveri */
59 { 0x1311, &kaveri_device_info }, /* Kaveri */
60 { 0x1312, &kaveri_device_info }, /* Kaveri */
61 { 0x1313, &kaveri_device_info }, /* Kaveri */
62 { 0x1315, &kaveri_device_info }, /* Kaveri */
63 { 0x1316, &kaveri_device_info }, /* Kaveri */
64 { 0x1317, &kaveri_device_info }, /* Kaveri */
65 { 0x1318, &kaveri_device_info }, /* Kaveri */
66 { 0x131B, &kaveri_device_info }, /* Kaveri */
67 { 0x131C, &kaveri_device_info }, /* Kaveri */
68 { 0x131D, &kaveri_device_info }, /* Kaveri */
69};
70
6e81090b
OG
71static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
72 unsigned int chunk_size);
73static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
74
4a488a7a
OG
75static const struct kfd_device_info *lookup_device_info(unsigned short did)
76{
77 size_t i;
78
79 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
80 if (supported_devices[i].did == did) {
81 BUG_ON(supported_devices[i].device_info == NULL);
82 return supported_devices[i].device_info;
83 }
84 }
85
86 return NULL;
87}
88
89struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev)
90{
91 struct kfd_dev *kfd;
92
93 const struct kfd_device_info *device_info =
94 lookup_device_info(pdev->device);
95
96 if (!device_info)
97 return NULL;
98
99 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
100 if (!kfd)
101 return NULL;
102
103 kfd->kgd = kgd;
104 kfd->device_info = device_info;
105 kfd->pdev = pdev;
19f6d2a6 106 kfd->init_complete = false;
4a488a7a
OG
107
108 return kfd;
109}
110
b17f068a
OG
111static bool device_iommu_pasid_init(struct kfd_dev *kfd)
112{
113 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
114 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
115 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
116
117 struct amd_iommu_device_info iommu_info;
118 unsigned int pasid_limit;
119 int err;
120
121 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
122 if (err < 0) {
123 dev_err(kfd_device,
124 "error getting iommu info. is the iommu enabled?\n");
125 return false;
126 }
127
128 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
129 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
130 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
131 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
132 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
133 return false;
134 }
135
136 pasid_limit = min_t(unsigned int,
137 (unsigned int)1 << kfd->device_info->max_pasid_bits,
138 iommu_info.max_pasids);
139 /*
140 * last pasid is used for kernel queues doorbells
141 * in the future the last pasid might be used for a kernel thread.
142 */
143 pasid_limit = min_t(unsigned int,
144 pasid_limit,
145 kfd->doorbell_process_limit - 1);
146
147 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
148 if (err < 0) {
149 dev_err(kfd_device, "error initializing iommu device\n");
150 return false;
151 }
152
153 if (!kfd_set_pasid_limit(pasid_limit)) {
154 dev_err(kfd_device, "error setting pasid limit\n");
155 amd_iommu_free_device(kfd->pdev);
156 return false;
157 }
158
159 return true;
160}
161
162static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
163{
164 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
165
166 if (dev)
167 kfd_unbind_process_from_device(dev, pasid);
168}
169
4a488a7a
OG
170bool kgd2kfd_device_init(struct kfd_dev *kfd,
171 const struct kgd2kfd_shared_resources *gpu_resources)
172{
19f6d2a6
OG
173 unsigned int size;
174
4a488a7a
OG
175 kfd->shared_resources = *gpu_resources;
176
19f6d2a6
OG
177 /* calculate max size of mqds needed for queues */
178 size = max_num_of_processes *
179 max_num_of_queues_per_process *
180 kfd->device_info->mqd_size_aligned;
181
e18e794e
OG
182 /*
183 * calculate max size of runlist packet.
184 * There can be only 2 packets at once
185 */
186 size += (max_num_of_processes * sizeof(struct pm4_map_process) +
187 max_num_of_processes * max_num_of_queues_per_process *
188 sizeof(struct pm4_map_queues) + sizeof(struct pm4_runlist)) * 2;
189
190 /* Add size of HIQ & DIQ */
191 size += KFD_KERNEL_QUEUE_SIZE * 2;
192
193 /* add another 512KB for all other allocations on gart (HPD, fences) */
19f6d2a6
OG
194 size += 512 * 1024;
195
196 if (kfd2kgd->init_sa_manager(kfd->kgd, size)) {
197 dev_err(kfd_device,
e18e794e
OG
198 "Could not allocate %d bytes for device (%x:%x)\n",
199 size, kfd->pdev->vendor, kfd->pdev->device);
19f6d2a6
OG
200 goto out;
201 }
202
e18e794e
OG
203 dev_info(kfd_device,
204 "Allocated %d bytes on gart for device(%x:%x)\n",
205 size, kfd->pdev->vendor, kfd->pdev->device);
206
19f6d2a6
OG
207 kfd_doorbell_init(kfd);
208
209 if (kfd_topology_add_device(kfd) != 0) {
210 dev_err(kfd_device,
211 "Error adding device (%x:%x) to topology\n",
212 kfd->pdev->vendor, kfd->pdev->device);
213 goto kfd_topology_add_device_error;
214 }
215
b3f5e6b4
AL
216 if (kfd_interrupt_init(kfd)) {
217 dev_err(kfd_device,
218 "Error initializing interrupts for device (%x:%x)\n",
219 kfd->pdev->vendor, kfd->pdev->device);
220 goto kfd_interrupt_error;
221 }
222
b17f068a
OG
223 if (!device_iommu_pasid_init(kfd)) {
224 dev_err(kfd_device,
225 "Error initializing iommuv2 for device (%x:%x)\n",
226 kfd->pdev->vendor, kfd->pdev->device);
227 goto device_iommu_pasid_error;
228 }
229 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
230 iommu_pasid_shutdown_callback);
5b5c4e40 231
64c7f8cf
BG
232 kfd->dqm = device_queue_manager_init(kfd);
233 if (!kfd->dqm) {
234 dev_err(kfd_device,
235 "Error initializing queue manager for device (%x:%x)\n",
236 kfd->pdev->vendor, kfd->pdev->device);
237 goto device_queue_manager_error;
238 }
239
240 if (kfd->dqm->start(kfd->dqm) != 0) {
241 dev_err(kfd_device,
242 "Error starting queuen manager for device (%x:%x)\n",
243 kfd->pdev->vendor, kfd->pdev->device);
244 goto dqm_start_error;
245 }
246
4a488a7a
OG
247 kfd->init_complete = true;
248 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
249 kfd->pdev->device);
250
64c7f8cf
BG
251 pr_debug("kfd: Starting kfd with the following scheduling policy %d\n",
252 sched_policy);
253
19f6d2a6
OG
254 goto out;
255
64c7f8cf
BG
256dqm_start_error:
257 device_queue_manager_uninit(kfd->dqm);
258device_queue_manager_error:
259 amd_iommu_free_device(kfd->pdev);
b17f068a 260device_iommu_pasid_error:
b3f5e6b4
AL
261 kfd_interrupt_exit(kfd);
262kfd_interrupt_error:
b17f068a 263 kfd_topology_remove_device(kfd);
19f6d2a6
OG
264kfd_topology_add_device_error:
265 kfd2kgd->fini_sa_manager(kfd->kgd);
266 dev_err(kfd_device,
267 "device (%x:%x) NOT added due to errors\n",
268 kfd->pdev->vendor, kfd->pdev->device);
269out:
270 return kfd->init_complete;
4a488a7a
OG
271}
272
273void kgd2kfd_device_exit(struct kfd_dev *kfd)
274{
b17f068a 275 if (kfd->init_complete) {
64c7f8cf 276 device_queue_manager_uninit(kfd->dqm);
b17f068a 277 amd_iommu_free_device(kfd->pdev);
b3f5e6b4 278 kfd_interrupt_exit(kfd);
b17f068a
OG
279 kfd_topology_remove_device(kfd);
280 }
5b5c4e40 281
4a488a7a
OG
282 kfree(kfd);
283}
284
285void kgd2kfd_suspend(struct kfd_dev *kfd)
286{
287 BUG_ON(kfd == NULL);
b17f068a 288
64c7f8cf
BG
289 if (kfd->init_complete) {
290 kfd->dqm->stop(kfd->dqm);
abc9d3e3 291 amd_iommu_set_invalidate_ctx_cb(kfd->pdev, NULL);
b17f068a 292 amd_iommu_free_device(kfd->pdev);
64c7f8cf 293 }
4a488a7a
OG
294}
295
296int kgd2kfd_resume(struct kfd_dev *kfd)
297{
b17f068a
OG
298 unsigned int pasid_limit;
299 int err;
300
4a488a7a
OG
301 BUG_ON(kfd == NULL);
302
b17f068a
OG
303 pasid_limit = kfd_get_pasid_limit();
304
305 if (kfd->init_complete) {
306 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
307 if (err < 0)
308 return -ENXIO;
309 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
310 iommu_pasid_shutdown_callback);
64c7f8cf 311 kfd->dqm->start(kfd->dqm);
b17f068a
OG
312 }
313
4a488a7a
OG
314 return 0;
315}
316
b3f5e6b4
AL
317/* This is called directly from KGD at ISR. */
318void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
4a488a7a 319{
b3f5e6b4
AL
320 if (kfd->init_complete) {
321 spin_lock(&kfd->interrupt_lock);
322
323 if (kfd->interrupts_active
324 && enqueue_ih_ring_entry(kfd, ih_ring_entry))
325 schedule_work(&kfd->interrupt_work);
326
327 spin_unlock(&kfd->interrupt_lock);
328 }
4a488a7a 329}
6e81090b
OG
330
331static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
332 unsigned int chunk_size)
333{
334 unsigned int num_of_bits;
335
336 BUG_ON(!kfd);
337 BUG_ON(!kfd->gtt_mem);
338 BUG_ON(buf_size < chunk_size);
339 BUG_ON(buf_size == 0);
340 BUG_ON(chunk_size == 0);
341
342 kfd->gtt_sa_chunk_size = chunk_size;
343 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
344
345 num_of_bits = kfd->gtt_sa_num_of_chunks / BITS_PER_BYTE;
346 BUG_ON(num_of_bits == 0);
347
348 kfd->gtt_sa_bitmap = kzalloc(num_of_bits, GFP_KERNEL);
349
350 if (!kfd->gtt_sa_bitmap)
351 return -ENOMEM;
352
353 pr_debug("kfd: gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
354 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
355
356 mutex_init(&kfd->gtt_sa_lock);
357
358 return 0;
359
360}
361
362static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
363{
364 mutex_destroy(&kfd->gtt_sa_lock);
365 kfree(kfd->gtt_sa_bitmap);
366}
367
368static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
369 unsigned int bit_num,
370 unsigned int chunk_size)
371{
372 return start_addr + bit_num * chunk_size;
373}
374
375static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
376 unsigned int bit_num,
377 unsigned int chunk_size)
378{
379 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
380}
381
382int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
383 struct kfd_mem_obj **mem_obj)
384{
385 unsigned int found, start_search, cur_size;
386
387 BUG_ON(!kfd);
388
389 if (size == 0)
390 return -EINVAL;
391
392 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
393 return -ENOMEM;
394
395 *mem_obj = kmalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
396 if ((*mem_obj) == NULL)
397 return -ENOMEM;
398
399 pr_debug("kfd: allocated mem_obj = %p for size = %d\n", *mem_obj, size);
400
401 start_search = 0;
402
403 mutex_lock(&kfd->gtt_sa_lock);
404
405kfd_gtt_restart_search:
406 /* Find the first chunk that is free */
407 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
408 kfd->gtt_sa_num_of_chunks,
409 start_search);
410
411 pr_debug("kfd: found = %d\n", found);
412
413 /* If there wasn't any free chunk, bail out */
414 if (found == kfd->gtt_sa_num_of_chunks)
415 goto kfd_gtt_no_free_chunk;
416
417 /* Update fields of mem_obj */
418 (*mem_obj)->range_start = found;
419 (*mem_obj)->range_end = found;
420 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
421 kfd->gtt_start_gpu_addr,
422 found,
423 kfd->gtt_sa_chunk_size);
424 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
425 kfd->gtt_start_cpu_ptr,
426 found,
427 kfd->gtt_sa_chunk_size);
428
429 pr_debug("kfd: gpu_addr = %p, cpu_addr = %p\n",
430 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
431
432 /* If we need only one chunk, mark it as allocated and get out */
433 if (size <= kfd->gtt_sa_chunk_size) {
434 pr_debug("kfd: single bit\n");
435 set_bit(found, kfd->gtt_sa_bitmap);
436 goto kfd_gtt_out;
437 }
438
439 /* Otherwise, try to see if we have enough contiguous chunks */
440 cur_size = size - kfd->gtt_sa_chunk_size;
441 do {
442 (*mem_obj)->range_end =
443 find_next_zero_bit(kfd->gtt_sa_bitmap,
444 kfd->gtt_sa_num_of_chunks, ++found);
445 /*
446 * If next free chunk is not contiguous than we need to
447 * restart our search from the last free chunk we found (which
448 * wasn't contiguous to the previous ones
449 */
450 if ((*mem_obj)->range_end != found) {
451 start_search = found;
452 goto kfd_gtt_restart_search;
453 }
454
455 /*
456 * If we reached end of buffer, bail out with error
457 */
458 if (found == kfd->gtt_sa_num_of_chunks)
459 goto kfd_gtt_no_free_chunk;
460
461 /* Check if we don't need another chunk */
462 if (cur_size <= kfd->gtt_sa_chunk_size)
463 cur_size = 0;
464 else
465 cur_size -= kfd->gtt_sa_chunk_size;
466
467 } while (cur_size > 0);
468
469 pr_debug("kfd: range_start = %d, range_end = %d\n",
470 (*mem_obj)->range_start, (*mem_obj)->range_end);
471
472 /* Mark the chunks as allocated */
473 for (found = (*mem_obj)->range_start;
474 found <= (*mem_obj)->range_end;
475 found++)
476 set_bit(found, kfd->gtt_sa_bitmap);
477
478kfd_gtt_out:
479 mutex_unlock(&kfd->gtt_sa_lock);
480 return 0;
481
482kfd_gtt_no_free_chunk:
483 pr_debug("kfd: allocation failed with mem_obj = %p\n", mem_obj);
484 mutex_unlock(&kfd->gtt_sa_lock);
485 kfree(mem_obj);
486 return -ENOMEM;
487}
488
489int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
490{
491 unsigned int bit;
492
493 BUG_ON(!kfd);
494 BUG_ON(!mem_obj);
495
496 pr_debug("kfd: free mem_obj = %p, range_start = %d, range_end = %d\n",
497 mem_obj, mem_obj->range_start, mem_obj->range_end);
498
499 mutex_lock(&kfd->gtt_sa_lock);
500
501 /* Mark the chunks as free */
502 for (bit = mem_obj->range_start;
503 bit <= mem_obj->range_end;
504 bit++)
505 clear_bit(bit, kfd->gtt_sa_bitmap);
506
507 mutex_unlock(&kfd->gtt_sa_lock);
508
509 kfree(mem_obj);
510 return 0;
511}
This page took 0.081148 seconds and 5 git commands to generate.