2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/slab.h>
25 #include <linux/list.h>
26 #include <linux/types.h>
27 #include <linux/printk.h>
28 #include <linux/bitops.h>
29 #include <linux/sched.h>
31 #include "kfd_device_queue_manager.h"
32 #include "kfd_mqd_manager.h"
34 #include "kfd_kernel_queue.h"
36 /* Size of the per-pipe EOP queue */
37 #define CIK_HPD_EOP_BYTES_LOG2 11
38 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
40 static int set_pasid_vmid_mapping(struct device_queue_manager
*dqm
,
41 unsigned int pasid
, unsigned int vmid
);
43 static int create_compute_queue_nocpsch(struct device_queue_manager
*dqm
,
45 struct qcm_process_device
*qpd
);
47 static int execute_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
);
48 static int destroy_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
);
50 static int create_sdma_queue_nocpsch(struct device_queue_manager
*dqm
,
52 struct qcm_process_device
*qpd
);
54 static void deallocate_sdma_queue(struct device_queue_manager
*dqm
,
55 unsigned int sdma_queue_id
);
58 enum KFD_MQD_TYPE
get_mqd_type_from_queue_type(enum kfd_queue_type type
)
60 if (type
== KFD_QUEUE_TYPE_SDMA
)
61 return KFD_MQD_TYPE_SDMA
;
62 return KFD_MQD_TYPE_CP
;
65 unsigned int get_first_pipe(struct device_queue_manager
*dqm
)
67 BUG_ON(!dqm
|| !dqm
->dev
);
68 return dqm
->dev
->shared_resources
.first_compute_pipe
;
71 unsigned int get_pipes_num(struct device_queue_manager
*dqm
)
73 BUG_ON(!dqm
|| !dqm
->dev
);
74 return dqm
->dev
->shared_resources
.compute_pipe_count
;
77 static inline unsigned int get_pipes_num_cpsch(void)
79 return PIPE_PER_ME_CP_SCHEDULING
;
82 void program_sh_mem_settings(struct device_queue_manager
*dqm
,
83 struct qcm_process_device
*qpd
)
85 return dqm
->dev
->kfd2kgd
->program_sh_mem_settings(
86 dqm
->dev
->kgd
, qpd
->vmid
,
88 qpd
->sh_mem_ape1_base
,
89 qpd
->sh_mem_ape1_limit
,
93 static int allocate_vmid(struct device_queue_manager
*dqm
,
94 struct qcm_process_device
*qpd
,
97 int bit
, allocated_vmid
;
99 if (dqm
->vmid_bitmap
== 0)
102 bit
= find_first_bit((unsigned long *)&dqm
->vmid_bitmap
, CIK_VMID_NUM
);
103 clear_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
105 /* Kaveri kfd vmid's starts from vmid 8 */
106 allocated_vmid
= bit
+ KFD_VMID_START_OFFSET
;
107 pr_debug("kfd: vmid allocation %d\n", allocated_vmid
);
108 qpd
->vmid
= allocated_vmid
;
109 q
->properties
.vmid
= allocated_vmid
;
111 set_pasid_vmid_mapping(dqm
, q
->process
->pasid
, q
->properties
.vmid
);
112 program_sh_mem_settings(dqm
, qpd
);
117 static void deallocate_vmid(struct device_queue_manager
*dqm
,
118 struct qcm_process_device
*qpd
,
121 int bit
= qpd
->vmid
- KFD_VMID_START_OFFSET
;
123 /* Release the vmid mapping */
124 set_pasid_vmid_mapping(dqm
, 0, qpd
->vmid
);
126 set_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
128 q
->properties
.vmid
= 0;
131 static int create_queue_nocpsch(struct device_queue_manager
*dqm
,
133 struct qcm_process_device
*qpd
,
138 BUG_ON(!dqm
|| !q
|| !qpd
|| !allocated_vmid
);
140 pr_debug("kfd: In func %s\n", __func__
);
143 mutex_lock(&dqm
->lock
);
145 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
146 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
147 dqm
->total_queue_count
);
148 mutex_unlock(&dqm
->lock
);
152 if (list_empty(&qpd
->queues_list
)) {
153 retval
= allocate_vmid(dqm
, qpd
, q
);
155 mutex_unlock(&dqm
->lock
);
159 *allocated_vmid
= qpd
->vmid
;
160 q
->properties
.vmid
= qpd
->vmid
;
162 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
)
163 retval
= create_compute_queue_nocpsch(dqm
, q
, qpd
);
164 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
165 retval
= create_sdma_queue_nocpsch(dqm
, q
, qpd
);
168 if (list_empty(&qpd
->queues_list
)) {
169 deallocate_vmid(dqm
, qpd
, q
);
172 mutex_unlock(&dqm
->lock
);
176 list_add(&q
->list
, &qpd
->queues_list
);
177 if (q
->properties
.is_active
)
180 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
181 dqm
->sdma_queue_count
++;
184 * Unconditionally increment this counter, regardless of the queue's
185 * type or whether the queue is active.
187 dqm
->total_queue_count
++;
188 pr_debug("Total of %d queues are accountable so far\n",
189 dqm
->total_queue_count
);
191 mutex_unlock(&dqm
->lock
);
195 static int allocate_hqd(struct device_queue_manager
*dqm
, struct queue
*q
)
202 for (pipe
= dqm
->next_pipe_to_allocate
, i
= 0; i
< get_pipes_num(dqm
);
203 pipe
= ((pipe
+ 1) % get_pipes_num(dqm
)), ++i
) {
204 if (dqm
->allocated_queues
[pipe
] != 0) {
205 bit
= find_first_bit(
206 (unsigned long *)&dqm
->allocated_queues
[pipe
],
210 (unsigned long *)&dqm
->allocated_queues
[pipe
]);
221 pr_debug("kfd: DQM %s hqd slot - pipe (%d) queue(%d)\n",
222 __func__
, q
->pipe
, q
->queue
);
223 /* horizontal hqd allocation */
224 dqm
->next_pipe_to_allocate
= (pipe
+ 1) % get_pipes_num(dqm
);
229 static inline void deallocate_hqd(struct device_queue_manager
*dqm
,
232 set_bit(q
->queue
, (unsigned long *)&dqm
->allocated_queues
[q
->pipe
]);
235 static int create_compute_queue_nocpsch(struct device_queue_manager
*dqm
,
237 struct qcm_process_device
*qpd
)
240 struct mqd_manager
*mqd
;
242 BUG_ON(!dqm
|| !q
|| !qpd
);
244 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
248 retval
= allocate_hqd(dqm
, q
);
252 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
253 &q
->gart_mqd_addr
, &q
->properties
);
255 deallocate_hqd(dqm
, q
);
259 pr_debug("kfd: loading mqd to hqd on pipe (%d) queue (%d)\n",
263 retval
= mqd
->load_mqd(mqd
, q
->mqd
, q
->pipe
,
264 q
->queue
, (uint32_t __user
*) q
->properties
.write_ptr
);
266 deallocate_hqd(dqm
, q
);
267 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
274 static int destroy_queue_nocpsch(struct device_queue_manager
*dqm
,
275 struct qcm_process_device
*qpd
,
279 struct mqd_manager
*mqd
;
281 BUG_ON(!dqm
|| !q
|| !q
->mqd
|| !qpd
);
285 pr_debug("kfd: In Func %s\n", __func__
);
287 mutex_lock(&dqm
->lock
);
289 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
) {
290 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
295 deallocate_hqd(dqm
, q
);
296 } else if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
) {
297 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
302 dqm
->sdma_queue_count
--;
303 deallocate_sdma_queue(dqm
, q
->sdma_id
);
305 pr_debug("q->properties.type is invalid (%d)\n",
311 retval
= mqd
->destroy_mqd(mqd
, q
->mqd
,
312 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
,
313 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
,
319 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
322 if (list_empty(&qpd
->queues_list
))
323 deallocate_vmid(dqm
, qpd
, q
);
324 if (q
->properties
.is_active
)
328 * Unconditionally decrement this counter, regardless of the queue's
331 dqm
->total_queue_count
--;
332 pr_debug("Total of %d queues are accountable so far\n",
333 dqm
->total_queue_count
);
336 mutex_unlock(&dqm
->lock
);
340 static int update_queue(struct device_queue_manager
*dqm
, struct queue
*q
)
343 struct mqd_manager
*mqd
;
344 bool prev_active
= false;
346 BUG_ON(!dqm
|| !q
|| !q
->mqd
);
348 mutex_lock(&dqm
->lock
);
349 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
350 get_mqd_type_from_queue_type(q
->properties
.type
));
352 mutex_unlock(&dqm
->lock
);
356 if (q
->properties
.is_active
== true)
361 * check active state vs. the previous state
362 * and modify counter accordingly
364 retval
= mqd
->update_mqd(mqd
, q
->mqd
, &q
->properties
);
365 if ((q
->properties
.is_active
== true) && (prev_active
== false))
367 else if ((q
->properties
.is_active
== false) && (prev_active
== true))
370 if (sched_policy
!= KFD_SCHED_POLICY_NO_HWS
)
371 retval
= execute_queues_cpsch(dqm
, false);
373 mutex_unlock(&dqm
->lock
);
377 static struct mqd_manager
*get_mqd_manager_nocpsch(
378 struct device_queue_manager
*dqm
, enum KFD_MQD_TYPE type
)
380 struct mqd_manager
*mqd
;
382 BUG_ON(!dqm
|| type
>= KFD_MQD_TYPE_MAX
);
384 pr_debug("kfd: In func %s mqd type %d\n", __func__
, type
);
386 mqd
= dqm
->mqds
[type
];
388 mqd
= mqd_manager_init(type
, dqm
->dev
);
390 pr_err("kfd: mqd manager is NULL");
391 dqm
->mqds
[type
] = mqd
;
397 static int register_process_nocpsch(struct device_queue_manager
*dqm
,
398 struct qcm_process_device
*qpd
)
400 struct device_process_node
*n
;
403 BUG_ON(!dqm
|| !qpd
);
405 pr_debug("kfd: In func %s\n", __func__
);
407 n
= kzalloc(sizeof(struct device_process_node
), GFP_KERNEL
);
413 mutex_lock(&dqm
->lock
);
414 list_add(&n
->list
, &dqm
->queues
);
416 retval
= dqm
->ops_asic_specific
.register_process(dqm
, qpd
);
418 dqm
->processes_count
++;
420 mutex_unlock(&dqm
->lock
);
425 static int unregister_process_nocpsch(struct device_queue_manager
*dqm
,
426 struct qcm_process_device
*qpd
)
429 struct device_process_node
*cur
, *next
;
431 BUG_ON(!dqm
|| !qpd
);
433 pr_debug("In func %s\n", __func__
);
435 pr_debug("qpd->queues_list is %s\n",
436 list_empty(&qpd
->queues_list
) ? "empty" : "not empty");
439 mutex_lock(&dqm
->lock
);
441 list_for_each_entry_safe(cur
, next
, &dqm
->queues
, list
) {
442 if (qpd
== cur
->qpd
) {
443 list_del(&cur
->list
);
445 dqm
->processes_count
--;
449 /* qpd not found in dqm list */
452 mutex_unlock(&dqm
->lock
);
457 set_pasid_vmid_mapping(struct device_queue_manager
*dqm
, unsigned int pasid
,
460 uint32_t pasid_mapping
;
462 pasid_mapping
= (pasid
== 0) ? 0 :
464 ATC_VMID_PASID_MAPPING_VALID
;
466 return dqm
->dev
->kfd2kgd
->set_pasid_vmid_mapping(
467 dqm
->dev
->kgd
, pasid_mapping
,
471 int init_pipelines(struct device_queue_manager
*dqm
,
472 unsigned int pipes_num
, unsigned int first_pipe
)
475 struct mqd_manager
*mqd
;
476 unsigned int i
, err
, inx
;
477 uint64_t pipe_hpd_addr
;
479 BUG_ON(!dqm
|| !dqm
->dev
);
481 pr_debug("kfd: In func %s\n", __func__
);
484 * Allocate memory for the HPDs. This is hardware-owned per-pipe data.
485 * The driver never accesses this memory after zeroing it.
486 * It doesn't even have to be saved/restored on suspend/resume
487 * because it contains no data when there are no active queues.
490 err
= kfd_gtt_sa_allocate(dqm
->dev
, CIK_HPD_EOP_BYTES
* pipes_num
,
494 pr_err("kfd: error allocate vidmem num pipes: %d\n",
499 hpdptr
= dqm
->pipeline_mem
->cpu_ptr
;
500 dqm
->pipelines_addr
= dqm
->pipeline_mem
->gpu_addr
;
502 memset(hpdptr
, 0, CIK_HPD_EOP_BYTES
* pipes_num
);
504 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
506 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
510 for (i
= 0; i
< pipes_num
; i
++) {
511 inx
= i
+ first_pipe
;
513 * HPD buffer on GTT is allocated by amdkfd, no need to waste
514 * space in GTT for pipelines we don't initialize
516 pipe_hpd_addr
= dqm
->pipelines_addr
+ i
* CIK_HPD_EOP_BYTES
;
517 pr_debug("kfd: pipeline address %llX\n", pipe_hpd_addr
);
518 /* = log2(bytes/4)-1 */
519 dqm
->dev
->kfd2kgd
->init_pipeline(dqm
->dev
->kgd
, inx
,
520 CIK_HPD_EOP_BYTES_LOG2
- 3, pipe_hpd_addr
);
526 static int init_scheduler(struct device_queue_manager
*dqm
)
532 pr_debug("kfd: In %s\n", __func__
);
534 retval
= init_pipelines(dqm
, get_pipes_num(dqm
), get_first_pipe(dqm
));
538 static int initialize_nocpsch(struct device_queue_manager
*dqm
)
544 pr_debug("kfd: In func %s num of pipes: %d\n",
545 __func__
, get_pipes_num(dqm
));
547 mutex_init(&dqm
->lock
);
548 INIT_LIST_HEAD(&dqm
->queues
);
549 dqm
->queue_count
= dqm
->next_pipe_to_allocate
= 0;
550 dqm
->sdma_queue_count
= 0;
551 dqm
->allocated_queues
= kcalloc(get_pipes_num(dqm
),
552 sizeof(unsigned int), GFP_KERNEL
);
553 if (!dqm
->allocated_queues
) {
554 mutex_destroy(&dqm
->lock
);
558 for (i
= 0; i
< get_pipes_num(dqm
); i
++)
559 dqm
->allocated_queues
[i
] = (1 << QUEUES_PER_PIPE
) - 1;
561 dqm
->vmid_bitmap
= (1 << VMID_PER_DEVICE
) - 1;
562 dqm
->sdma_bitmap
= (1 << CIK_SDMA_QUEUES
) - 1;
568 static void uninitialize_nocpsch(struct device_queue_manager
*dqm
)
574 BUG_ON(dqm
->queue_count
> 0 || dqm
->processes_count
> 0);
576 kfree(dqm
->allocated_queues
);
577 for (i
= 0 ; i
< KFD_MQD_TYPE_MAX
; i
++)
579 mutex_destroy(&dqm
->lock
);
580 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
583 static int start_nocpsch(struct device_queue_manager
*dqm
)
588 static int stop_nocpsch(struct device_queue_manager
*dqm
)
593 static int allocate_sdma_queue(struct device_queue_manager
*dqm
,
594 unsigned int *sdma_queue_id
)
598 if (dqm
->sdma_bitmap
== 0)
601 bit
= find_first_bit((unsigned long *)&dqm
->sdma_bitmap
,
604 clear_bit(bit
, (unsigned long *)&dqm
->sdma_bitmap
);
605 *sdma_queue_id
= bit
;
610 static void deallocate_sdma_queue(struct device_queue_manager
*dqm
,
611 unsigned int sdma_queue_id
)
613 if (sdma_queue_id
>= CIK_SDMA_QUEUES
)
615 set_bit(sdma_queue_id
, (unsigned long *)&dqm
->sdma_bitmap
);
618 static void init_sdma_vm(struct device_queue_manager
*dqm
, struct queue
*q
,
619 struct qcm_process_device
*qpd
)
621 uint32_t value
= SDMA_ATC
;
623 if (q
->process
->is_32bit_user_mode
)
624 value
|= SDMA_VA_PTR32
| get_sh_mem_bases_32(qpd_to_pdd(qpd
));
626 value
|= SDMA_VA_SHARED_BASE(get_sh_mem_bases_nybble_64(
628 q
->properties
.sdma_vm_addr
= value
;
631 static int create_sdma_queue_nocpsch(struct device_queue_manager
*dqm
,
633 struct qcm_process_device
*qpd
)
635 struct mqd_manager
*mqd
;
638 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
642 retval
= allocate_sdma_queue(dqm
, &q
->sdma_id
);
646 q
->properties
.sdma_queue_id
= q
->sdma_id
% CIK_SDMA_QUEUES_PER_ENGINE
;
647 q
->properties
.sdma_engine_id
= q
->sdma_id
/ CIK_SDMA_ENGINE_NUM
;
649 pr_debug("kfd: sdma id is: %d\n", q
->sdma_id
);
650 pr_debug(" sdma queue id: %d\n", q
->properties
.sdma_queue_id
);
651 pr_debug(" sdma engine id: %d\n", q
->properties
.sdma_engine_id
);
653 init_sdma_vm(dqm
, q
, qpd
);
654 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
655 &q
->gart_mqd_addr
, &q
->properties
);
657 deallocate_sdma_queue(dqm
, q
->sdma_id
);
661 retval
= mqd
->load_mqd(mqd
, q
->mqd
, 0,
664 deallocate_sdma_queue(dqm
, q
->sdma_id
);
665 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
673 * Device Queue Manager implementation for cp scheduler
676 static int set_sched_resources(struct device_queue_manager
*dqm
)
678 struct scheduling_resources res
;
679 unsigned int queue_num
, queue_mask
;
683 pr_debug("kfd: In func %s\n", __func__
);
685 queue_num
= get_pipes_num_cpsch() * QUEUES_PER_PIPE
;
686 queue_mask
= (1 << queue_num
) - 1;
687 res
.vmid_mask
= (1 << VMID_PER_DEVICE
) - 1;
688 res
.vmid_mask
<<= KFD_VMID_START_OFFSET
;
689 res
.queue_mask
= queue_mask
<< (get_first_pipe(dqm
) * QUEUES_PER_PIPE
);
690 res
.gws_mask
= res
.oac_mask
= res
.gds_heap_base
=
691 res
.gds_heap_size
= 0;
693 pr_debug("kfd: scheduling resources:\n"
694 " vmid mask: 0x%8X\n"
695 " queue mask: 0x%8llX\n",
696 res
.vmid_mask
, res
.queue_mask
);
698 return pm_send_set_resources(&dqm
->packets
, &res
);
701 static int initialize_cpsch(struct device_queue_manager
*dqm
)
707 pr_debug("kfd: In func %s num of pipes: %d\n",
708 __func__
, get_pipes_num_cpsch());
710 mutex_init(&dqm
->lock
);
711 INIT_LIST_HEAD(&dqm
->queues
);
712 dqm
->queue_count
= dqm
->processes_count
= 0;
713 dqm
->sdma_queue_count
= 0;
714 dqm
->active_runlist
= false;
715 retval
= dqm
->ops_asic_specific
.initialize(dqm
);
717 goto fail_init_pipelines
;
722 mutex_destroy(&dqm
->lock
);
726 static int start_cpsch(struct device_queue_manager
*dqm
)
728 struct device_process_node
*node
;
735 retval
= pm_init(&dqm
->packets
, dqm
);
737 goto fail_packet_manager_init
;
739 retval
= set_sched_resources(dqm
);
741 goto fail_set_sched_resources
;
743 pr_debug("kfd: allocating fence memory\n");
745 /* allocate fence memory on the gart */
746 retval
= kfd_gtt_sa_allocate(dqm
->dev
, sizeof(*dqm
->fence_addr
),
750 goto fail_allocate_vidmem
;
752 dqm
->fence_addr
= dqm
->fence_mem
->cpu_ptr
;
753 dqm
->fence_gpu_addr
= dqm
->fence_mem
->gpu_addr
;
754 list_for_each_entry(node
, &dqm
->queues
, list
)
755 if (node
->qpd
->pqm
->process
&& dqm
->dev
)
756 kfd_bind_process_to_device(dqm
->dev
,
757 node
->qpd
->pqm
->process
);
759 execute_queues_cpsch(dqm
, true);
762 fail_allocate_vidmem
:
763 fail_set_sched_resources
:
764 pm_uninit(&dqm
->packets
);
765 fail_packet_manager_init
:
769 static int stop_cpsch(struct device_queue_manager
*dqm
)
771 struct device_process_node
*node
;
772 struct kfd_process_device
*pdd
;
776 destroy_queues_cpsch(dqm
, true);
778 list_for_each_entry(node
, &dqm
->queues
, list
) {
779 pdd
= qpd_to_pdd(node
->qpd
);
782 kfd_gtt_sa_free(dqm
->dev
, dqm
->fence_mem
);
783 pm_uninit(&dqm
->packets
);
788 static int create_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
789 struct kernel_queue
*kq
,
790 struct qcm_process_device
*qpd
)
792 BUG_ON(!dqm
|| !kq
|| !qpd
);
794 pr_debug("kfd: In func %s\n", __func__
);
796 mutex_lock(&dqm
->lock
);
797 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
798 pr_warn("amdkfd: Can't create new kernel queue because %d queues were already created\n",
799 dqm
->total_queue_count
);
800 mutex_unlock(&dqm
->lock
);
805 * Unconditionally increment this counter, regardless of the queue's
806 * type or whether the queue is active.
808 dqm
->total_queue_count
++;
809 pr_debug("Total of %d queues are accountable so far\n",
810 dqm
->total_queue_count
);
812 list_add(&kq
->list
, &qpd
->priv_queue_list
);
814 qpd
->is_debug
= true;
815 execute_queues_cpsch(dqm
, false);
816 mutex_unlock(&dqm
->lock
);
821 static void destroy_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
822 struct kernel_queue
*kq
,
823 struct qcm_process_device
*qpd
)
827 pr_debug("kfd: In %s\n", __func__
);
829 mutex_lock(&dqm
->lock
);
830 destroy_queues_cpsch(dqm
, false);
833 qpd
->is_debug
= false;
834 execute_queues_cpsch(dqm
, false);
836 * Unconditionally decrement this counter, regardless of the queue's
839 dqm
->total_queue_count
--;
840 pr_debug("Total of %d queues are accountable so far\n",
841 dqm
->total_queue_count
);
842 mutex_unlock(&dqm
->lock
);
845 static void select_sdma_engine_id(struct queue
*q
)
849 q
->sdma_id
= sdma_id
;
850 sdma_id
= (sdma_id
+ 1) % 2;
853 static int create_queue_cpsch(struct device_queue_manager
*dqm
, struct queue
*q
,
854 struct qcm_process_device
*qpd
, int *allocate_vmid
)
857 struct mqd_manager
*mqd
;
859 BUG_ON(!dqm
|| !q
|| !qpd
);
866 mutex_lock(&dqm
->lock
);
868 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
869 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
870 dqm
->total_queue_count
);
875 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
876 select_sdma_engine_id(q
);
878 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
879 get_mqd_type_from_queue_type(q
->properties
.type
));
882 mutex_unlock(&dqm
->lock
);
886 init_sdma_vm(dqm
, q
, qpd
);
888 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
889 &q
->gart_mqd_addr
, &q
->properties
);
893 list_add(&q
->list
, &qpd
->queues_list
);
894 if (q
->properties
.is_active
) {
896 retval
= execute_queues_cpsch(dqm
, false);
899 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
900 dqm
->sdma_queue_count
++;
902 * Unconditionally increment this counter, regardless of the queue's
903 * type or whether the queue is active.
905 dqm
->total_queue_count
++;
907 pr_debug("Total of %d queues are accountable so far\n",
908 dqm
->total_queue_count
);
911 mutex_unlock(&dqm
->lock
);
915 static int amdkfd_fence_wait_timeout(unsigned int *fence_addr
,
916 unsigned int fence_value
,
917 unsigned long timeout
)
922 while (*fence_addr
!= fence_value
) {
923 if (time_after(jiffies
, timeout
)) {
924 pr_err("kfd: qcm fence wait loop timeout expired\n");
933 static int destroy_sdma_queues(struct device_queue_manager
*dqm
,
934 unsigned int sdma_engine
)
936 return pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_SDMA
,
937 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false,
941 static int destroy_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
950 mutex_lock(&dqm
->lock
);
951 if (dqm
->active_runlist
== false)
954 pr_debug("kfd: Before destroying queues, sdma queue count is : %u\n",
955 dqm
->sdma_queue_count
);
957 if (dqm
->sdma_queue_count
> 0) {
958 destroy_sdma_queues(dqm
, 0);
959 destroy_sdma_queues(dqm
, 1);
962 retval
= pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_COMPUTE
,
963 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false, 0);
967 *dqm
->fence_addr
= KFD_FENCE_INIT
;
968 pm_send_query_status(&dqm
->packets
, dqm
->fence_gpu_addr
,
969 KFD_FENCE_COMPLETED
);
970 /* should be timed out */
971 amdkfd_fence_wait_timeout(dqm
->fence_addr
, KFD_FENCE_COMPLETED
,
972 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
);
973 pm_release_ib(&dqm
->packets
);
974 dqm
->active_runlist
= false;
978 mutex_unlock(&dqm
->lock
);
982 static int execute_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
989 mutex_lock(&dqm
->lock
);
991 retval
= destroy_queues_cpsch(dqm
, false);
993 pr_err("kfd: the cp might be in an unrecoverable state due to an unsuccessful queues preemption");
997 if (dqm
->queue_count
<= 0 || dqm
->processes_count
<= 0) {
1002 if (dqm
->active_runlist
) {
1007 retval
= pm_send_runlist(&dqm
->packets
, &dqm
->queues
);
1009 pr_err("kfd: failed to execute runlist");
1012 dqm
->active_runlist
= true;
1016 mutex_unlock(&dqm
->lock
);
1020 static int destroy_queue_cpsch(struct device_queue_manager
*dqm
,
1021 struct qcm_process_device
*qpd
,
1025 struct mqd_manager
*mqd
;
1027 BUG_ON(!dqm
|| !qpd
|| !q
);
1031 /* remove queue from list to prevent rescheduling after preemption */
1032 mutex_lock(&dqm
->lock
);
1033 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
1034 get_mqd_type_from_queue_type(q
->properties
.type
));
1040 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
1041 dqm
->sdma_queue_count
--;
1044 if (q
->properties
.is_active
)
1047 execute_queues_cpsch(dqm
, false);
1049 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
1052 * Unconditionally decrement this counter, regardless of the queue's
1055 dqm
->total_queue_count
--;
1056 pr_debug("Total of %d queues are accountable so far\n",
1057 dqm
->total_queue_count
);
1059 mutex_unlock(&dqm
->lock
);
1064 mutex_unlock(&dqm
->lock
);
1069 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1070 * stay in user mode.
1072 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1073 /* APE1 limit is inclusive and 64K aligned. */
1074 #define APE1_LIMIT_ALIGNMENT 0xFFFF
1076 static bool set_cache_memory_policy(struct device_queue_manager
*dqm
,
1077 struct qcm_process_device
*qpd
,
1078 enum cache_policy default_policy
,
1079 enum cache_policy alternate_policy
,
1080 void __user
*alternate_aperture_base
,
1081 uint64_t alternate_aperture_size
)
1085 pr_debug("kfd: In func %s\n", __func__
);
1087 mutex_lock(&dqm
->lock
);
1089 if (alternate_aperture_size
== 0) {
1090 /* base > limit disables APE1 */
1091 qpd
->sh_mem_ape1_base
= 1;
1092 qpd
->sh_mem_ape1_limit
= 0;
1095 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1096 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1097 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1098 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1099 * Verify that the base and size parameters can be
1100 * represented in this format and convert them.
1101 * Additionally restrict APE1 to user-mode addresses.
1104 uint64_t base
= (uintptr_t)alternate_aperture_base
;
1105 uint64_t limit
= base
+ alternate_aperture_size
- 1;
1110 if ((base
& APE1_FIXED_BITS_MASK
) != 0)
1113 if ((limit
& APE1_FIXED_BITS_MASK
) != APE1_LIMIT_ALIGNMENT
)
1116 qpd
->sh_mem_ape1_base
= base
>> 16;
1117 qpd
->sh_mem_ape1_limit
= limit
>> 16;
1120 retval
= dqm
->ops_asic_specific
.set_cache_memory_policy(
1125 alternate_aperture_base
,
1126 alternate_aperture_size
);
1128 if ((sched_policy
== KFD_SCHED_POLICY_NO_HWS
) && (qpd
->vmid
!= 0))
1129 program_sh_mem_settings(dqm
, qpd
);
1131 pr_debug("kfd: sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
1132 qpd
->sh_mem_config
, qpd
->sh_mem_ape1_base
,
1133 qpd
->sh_mem_ape1_limit
);
1135 mutex_unlock(&dqm
->lock
);
1139 mutex_unlock(&dqm
->lock
);
1143 struct device_queue_manager
*device_queue_manager_init(struct kfd_dev
*dev
)
1145 struct device_queue_manager
*dqm
;
1149 pr_debug("kfd: loading device queue manager\n");
1151 dqm
= kzalloc(sizeof(struct device_queue_manager
), GFP_KERNEL
);
1156 switch (sched_policy
) {
1157 case KFD_SCHED_POLICY_HWS
:
1158 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION
:
1159 /* initialize dqm for cp scheduling */
1160 dqm
->ops
.create_queue
= create_queue_cpsch
;
1161 dqm
->ops
.initialize
= initialize_cpsch
;
1162 dqm
->ops
.start
= start_cpsch
;
1163 dqm
->ops
.stop
= stop_cpsch
;
1164 dqm
->ops
.destroy_queue
= destroy_queue_cpsch
;
1165 dqm
->ops
.update_queue
= update_queue
;
1166 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1167 dqm
->ops
.register_process
= register_process_nocpsch
;
1168 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1169 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1170 dqm
->ops
.create_kernel_queue
= create_kernel_queue_cpsch
;
1171 dqm
->ops
.destroy_kernel_queue
= destroy_kernel_queue_cpsch
;
1172 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1174 case KFD_SCHED_POLICY_NO_HWS
:
1175 /* initialize dqm for no cp scheduling */
1176 dqm
->ops
.start
= start_nocpsch
;
1177 dqm
->ops
.stop
= stop_nocpsch
;
1178 dqm
->ops
.create_queue
= create_queue_nocpsch
;
1179 dqm
->ops
.destroy_queue
= destroy_queue_nocpsch
;
1180 dqm
->ops
.update_queue
= update_queue
;
1181 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1182 dqm
->ops
.register_process
= register_process_nocpsch
;
1183 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1184 dqm
->ops
.initialize
= initialize_nocpsch
;
1185 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1186 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1193 switch (dev
->device_info
->asic_family
) {
1195 device_queue_manager_init_vi(&dqm
->ops_asic_specific
);
1199 device_queue_manager_init_cik(&dqm
->ops_asic_specific
);
1203 if (dqm
->ops
.initialize(dqm
) != 0) {
1211 void device_queue_manager_uninit(struct device_queue_manager
*dqm
)
1215 dqm
->ops
.uninitialize(dqm
);