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 static inline unsigned int get_first_pipe(struct device_queue_manager
*dqm
)
68 return dqm
->dev
->shared_resources
.first_compute_pipe
;
71 static inline unsigned int get_pipes_num_cpsch(void)
73 return PIPE_PER_ME_CP_SCHEDULING
;
76 void program_sh_mem_settings(struct device_queue_manager
*dqm
,
77 struct qcm_process_device
*qpd
)
79 return kfd2kgd
->program_sh_mem_settings(dqm
->dev
->kgd
, qpd
->vmid
,
81 qpd
->sh_mem_ape1_base
,
82 qpd
->sh_mem_ape1_limit
,
86 static int allocate_vmid(struct device_queue_manager
*dqm
,
87 struct qcm_process_device
*qpd
,
90 int bit
, allocated_vmid
;
92 if (dqm
->vmid_bitmap
== 0)
95 bit
= find_first_bit((unsigned long *)&dqm
->vmid_bitmap
, CIK_VMID_NUM
);
96 clear_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
98 /* Kaveri kfd vmid's starts from vmid 8 */
99 allocated_vmid
= bit
+ KFD_VMID_START_OFFSET
;
100 pr_debug("kfd: vmid allocation %d\n", allocated_vmid
);
101 qpd
->vmid
= allocated_vmid
;
102 q
->properties
.vmid
= allocated_vmid
;
104 set_pasid_vmid_mapping(dqm
, q
->process
->pasid
, q
->properties
.vmid
);
105 program_sh_mem_settings(dqm
, qpd
);
110 static void deallocate_vmid(struct device_queue_manager
*dqm
,
111 struct qcm_process_device
*qpd
,
114 int bit
= qpd
->vmid
- KFD_VMID_START_OFFSET
;
116 /* Release the vmid mapping */
117 set_pasid_vmid_mapping(dqm
, 0, qpd
->vmid
);
119 set_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
121 q
->properties
.vmid
= 0;
124 static int create_queue_nocpsch(struct device_queue_manager
*dqm
,
126 struct qcm_process_device
*qpd
,
131 BUG_ON(!dqm
|| !q
|| !qpd
|| !allocated_vmid
);
133 pr_debug("kfd: In func %s\n", __func__
);
136 mutex_lock(&dqm
->lock
);
138 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
139 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
140 dqm
->total_queue_count
);
141 mutex_unlock(&dqm
->lock
);
145 if (list_empty(&qpd
->queues_list
)) {
146 retval
= allocate_vmid(dqm
, qpd
, q
);
148 mutex_unlock(&dqm
->lock
);
152 *allocated_vmid
= qpd
->vmid
;
153 q
->properties
.vmid
= qpd
->vmid
;
155 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
)
156 retval
= create_compute_queue_nocpsch(dqm
, q
, qpd
);
157 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
158 retval
= create_sdma_queue_nocpsch(dqm
, q
, qpd
);
161 if (list_empty(&qpd
->queues_list
)) {
162 deallocate_vmid(dqm
, qpd
, q
);
165 mutex_unlock(&dqm
->lock
);
169 list_add(&q
->list
, &qpd
->queues_list
);
170 if (q
->properties
.is_active
)
173 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
174 dqm
->sdma_queue_count
++;
177 * Unconditionally increment this counter, regardless of the queue's
178 * type or whether the queue is active.
180 dqm
->total_queue_count
++;
181 pr_debug("Total of %d queues are accountable so far\n",
182 dqm
->total_queue_count
);
184 mutex_unlock(&dqm
->lock
);
188 static int allocate_hqd(struct device_queue_manager
*dqm
, struct queue
*q
)
195 for (pipe
= dqm
->next_pipe_to_allocate
, i
= 0; i
< get_pipes_num(dqm
);
196 pipe
= ((pipe
+ 1) % get_pipes_num(dqm
)), ++i
) {
197 if (dqm
->allocated_queues
[pipe
] != 0) {
198 bit
= find_first_bit(
199 (unsigned long *)&dqm
->allocated_queues
[pipe
],
203 (unsigned long *)&dqm
->allocated_queues
[pipe
]);
214 pr_debug("kfd: DQM %s hqd slot - pipe (%d) queue(%d)\n",
215 __func__
, q
->pipe
, q
->queue
);
216 /* horizontal hqd allocation */
217 dqm
->next_pipe_to_allocate
= (pipe
+ 1) % get_pipes_num(dqm
);
222 static inline void deallocate_hqd(struct device_queue_manager
*dqm
,
225 set_bit(q
->queue
, (unsigned long *)&dqm
->allocated_queues
[q
->pipe
]);
228 static int create_compute_queue_nocpsch(struct device_queue_manager
*dqm
,
230 struct qcm_process_device
*qpd
)
233 struct mqd_manager
*mqd
;
235 BUG_ON(!dqm
|| !q
|| !qpd
);
237 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
241 retval
= allocate_hqd(dqm
, q
);
245 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
246 &q
->gart_mqd_addr
, &q
->properties
);
248 deallocate_hqd(dqm
, q
);
252 pr_debug("kfd: loading mqd to hqd on pipe (%d) queue (%d)\n",
256 retval
= mqd
->load_mqd(mqd
, q
->mqd
, q
->pipe
,
257 q
->queue
, (uint32_t __user
*) q
->properties
.write_ptr
);
259 deallocate_hqd(dqm
, q
);
260 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
267 static int destroy_queue_nocpsch(struct device_queue_manager
*dqm
,
268 struct qcm_process_device
*qpd
,
272 struct mqd_manager
*mqd
;
274 BUG_ON(!dqm
|| !q
|| !q
->mqd
|| !qpd
);
278 pr_debug("kfd: In Func %s\n", __func__
);
280 mutex_lock(&dqm
->lock
);
282 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
) {
283 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
288 deallocate_hqd(dqm
, q
);
289 } else if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
) {
290 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
295 dqm
->sdma_queue_count
--;
296 deallocate_sdma_queue(dqm
, q
->sdma_id
);
298 pr_debug("q->properties.type is invalid (%d)\n",
304 retval
= mqd
->destroy_mqd(mqd
, q
->mqd
,
305 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
,
306 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
,
312 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
315 if (list_empty(&qpd
->queues_list
))
316 deallocate_vmid(dqm
, qpd
, q
);
317 if (q
->properties
.is_active
)
321 * Unconditionally decrement this counter, regardless of the queue's
324 dqm
->total_queue_count
--;
325 pr_debug("Total of %d queues are accountable so far\n",
326 dqm
->total_queue_count
);
329 mutex_unlock(&dqm
->lock
);
333 static int update_queue(struct device_queue_manager
*dqm
, struct queue
*q
)
336 struct mqd_manager
*mqd
;
337 bool prev_active
= false;
339 BUG_ON(!dqm
|| !q
|| !q
->mqd
);
341 mutex_lock(&dqm
->lock
);
342 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
343 get_mqd_type_from_queue_type(q
->properties
.type
));
345 mutex_unlock(&dqm
->lock
);
349 if (q
->properties
.is_active
== true)
354 * check active state vs. the previous state
355 * and modify counter accordingly
357 retval
= mqd
->update_mqd(mqd
, q
->mqd
, &q
->properties
);
358 if ((q
->properties
.is_active
== true) && (prev_active
== false))
360 else if ((q
->properties
.is_active
== false) && (prev_active
== true))
363 if (sched_policy
!= KFD_SCHED_POLICY_NO_HWS
)
364 retval
= execute_queues_cpsch(dqm
, false);
366 mutex_unlock(&dqm
->lock
);
370 static struct mqd_manager
*get_mqd_manager_nocpsch(
371 struct device_queue_manager
*dqm
, enum KFD_MQD_TYPE type
)
373 struct mqd_manager
*mqd
;
375 BUG_ON(!dqm
|| type
>= KFD_MQD_TYPE_MAX
);
377 pr_debug("kfd: In func %s mqd type %d\n", __func__
, type
);
379 mqd
= dqm
->mqds
[type
];
381 mqd
= mqd_manager_init(type
, dqm
->dev
);
383 pr_err("kfd: mqd manager is NULL");
384 dqm
->mqds
[type
] = mqd
;
390 static int register_process_nocpsch(struct device_queue_manager
*dqm
,
391 struct qcm_process_device
*qpd
)
393 struct device_process_node
*n
;
396 BUG_ON(!dqm
|| !qpd
);
398 pr_debug("kfd: In func %s\n", __func__
);
400 n
= kzalloc(sizeof(struct device_process_node
), GFP_KERNEL
);
406 mutex_lock(&dqm
->lock
);
407 list_add(&n
->list
, &dqm
->queues
);
409 retval
= dqm
->ops_asic_specific
.register_process(dqm
, qpd
);
411 dqm
->processes_count
++;
413 mutex_unlock(&dqm
->lock
);
418 static int unregister_process_nocpsch(struct device_queue_manager
*dqm
,
419 struct qcm_process_device
*qpd
)
422 struct device_process_node
*cur
, *next
;
424 BUG_ON(!dqm
|| !qpd
);
426 BUG_ON(!list_empty(&qpd
->queues_list
));
428 pr_debug("kfd: In func %s\n", __func__
);
431 mutex_lock(&dqm
->lock
);
433 list_for_each_entry_safe(cur
, next
, &dqm
->queues
, list
) {
434 if (qpd
== cur
->qpd
) {
435 list_del(&cur
->list
);
437 dqm
->processes_count
--;
441 /* qpd not found in dqm list */
444 mutex_unlock(&dqm
->lock
);
449 set_pasid_vmid_mapping(struct device_queue_manager
*dqm
, unsigned int pasid
,
452 uint32_t pasid_mapping
;
454 pasid_mapping
= (pasid
== 0) ? 0 : (uint32_t)pasid
|
455 ATC_VMID_PASID_MAPPING_VALID
;
456 return kfd2kgd
->set_pasid_vmid_mapping(dqm
->dev
->kgd
, pasid_mapping
,
460 int init_pipelines(struct device_queue_manager
*dqm
,
461 unsigned int pipes_num
, unsigned int first_pipe
)
464 struct mqd_manager
*mqd
;
465 unsigned int i
, err
, inx
;
466 uint64_t pipe_hpd_addr
;
468 BUG_ON(!dqm
|| !dqm
->dev
);
470 pr_debug("kfd: In func %s\n", __func__
);
473 * Allocate memory for the HPDs. This is hardware-owned per-pipe data.
474 * The driver never accesses this memory after zeroing it.
475 * It doesn't even have to be saved/restored on suspend/resume
476 * because it contains no data when there are no active queues.
479 err
= kfd_gtt_sa_allocate(dqm
->dev
, CIK_HPD_EOP_BYTES
* pipes_num
,
483 pr_err("kfd: error allocate vidmem num pipes: %d\n",
488 hpdptr
= dqm
->pipeline_mem
->cpu_ptr
;
489 dqm
->pipelines_addr
= dqm
->pipeline_mem
->gpu_addr
;
491 memset(hpdptr
, 0, CIK_HPD_EOP_BYTES
* pipes_num
);
493 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
495 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
499 for (i
= 0; i
< pipes_num
; i
++) {
500 inx
= i
+ first_pipe
;
502 * HPD buffer on GTT is allocated by amdkfd, no need to waste
503 * space in GTT for pipelines we don't initialize
505 pipe_hpd_addr
= dqm
->pipelines_addr
+ i
* CIK_HPD_EOP_BYTES
;
506 pr_debug("kfd: pipeline address %llX\n", pipe_hpd_addr
);
507 /* = log2(bytes/4)-1 */
508 kfd2kgd
->init_pipeline(dqm
->dev
->kgd
, inx
,
509 CIK_HPD_EOP_BYTES_LOG2
- 3, pipe_hpd_addr
);
515 static int init_scheduler(struct device_queue_manager
*dqm
)
521 pr_debug("kfd: In %s\n", __func__
);
523 retval
= init_pipelines(dqm
, get_pipes_num(dqm
), get_first_pipe(dqm
));
527 static int initialize_nocpsch(struct device_queue_manager
*dqm
)
533 pr_debug("kfd: In func %s num of pipes: %d\n",
534 __func__
, get_pipes_num(dqm
));
536 mutex_init(&dqm
->lock
);
537 INIT_LIST_HEAD(&dqm
->queues
);
538 dqm
->queue_count
= dqm
->next_pipe_to_allocate
= 0;
539 dqm
->sdma_queue_count
= 0;
540 dqm
->allocated_queues
= kcalloc(get_pipes_num(dqm
),
541 sizeof(unsigned int), GFP_KERNEL
);
542 if (!dqm
->allocated_queues
) {
543 mutex_destroy(&dqm
->lock
);
547 for (i
= 0; i
< get_pipes_num(dqm
); i
++)
548 dqm
->allocated_queues
[i
] = (1 << QUEUES_PER_PIPE
) - 1;
550 dqm
->vmid_bitmap
= (1 << VMID_PER_DEVICE
) - 1;
551 dqm
->sdma_bitmap
= (1 << CIK_SDMA_QUEUES
) - 1;
557 static void uninitialize_nocpsch(struct device_queue_manager
*dqm
)
563 BUG_ON(dqm
->queue_count
> 0 || dqm
->processes_count
> 0);
565 kfree(dqm
->allocated_queues
);
566 for (i
= 0 ; i
< KFD_MQD_TYPE_MAX
; i
++)
568 mutex_destroy(&dqm
->lock
);
569 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
572 static int start_nocpsch(struct device_queue_manager
*dqm
)
577 static int stop_nocpsch(struct device_queue_manager
*dqm
)
582 static int allocate_sdma_queue(struct device_queue_manager
*dqm
,
583 unsigned int *sdma_queue_id
)
587 if (dqm
->sdma_bitmap
== 0)
590 bit
= find_first_bit((unsigned long *)&dqm
->sdma_bitmap
,
593 clear_bit(bit
, (unsigned long *)&dqm
->sdma_bitmap
);
594 *sdma_queue_id
= bit
;
599 static void deallocate_sdma_queue(struct device_queue_manager
*dqm
,
600 unsigned int sdma_queue_id
)
602 if (sdma_queue_id
>= CIK_SDMA_QUEUES
)
604 set_bit(sdma_queue_id
, (unsigned long *)&dqm
->sdma_bitmap
);
607 static void init_sdma_vm(struct device_queue_manager
*dqm
, struct queue
*q
,
608 struct qcm_process_device
*qpd
)
610 uint32_t value
= SDMA_ATC
;
612 if (q
->process
->is_32bit_user_mode
)
613 value
|= SDMA_VA_PTR32
| get_sh_mem_bases_32(qpd_to_pdd(qpd
));
615 value
|= SDMA_VA_SHARED_BASE(get_sh_mem_bases_nybble_64(
617 q
->properties
.sdma_vm_addr
= value
;
620 static int create_sdma_queue_nocpsch(struct device_queue_manager
*dqm
,
622 struct qcm_process_device
*qpd
)
624 struct mqd_manager
*mqd
;
627 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
631 retval
= allocate_sdma_queue(dqm
, &q
->sdma_id
);
635 q
->properties
.sdma_queue_id
= q
->sdma_id
% CIK_SDMA_QUEUES_PER_ENGINE
;
636 q
->properties
.sdma_engine_id
= q
->sdma_id
/ CIK_SDMA_ENGINE_NUM
;
638 pr_debug("kfd: sdma id is: %d\n", q
->sdma_id
);
639 pr_debug(" sdma queue id: %d\n", q
->properties
.sdma_queue_id
);
640 pr_debug(" sdma engine id: %d\n", q
->properties
.sdma_engine_id
);
642 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
643 &q
->gart_mqd_addr
, &q
->properties
);
645 deallocate_sdma_queue(dqm
, q
->sdma_id
);
649 init_sdma_vm(dqm
, q
, qpd
);
654 * Device Queue Manager implementation for cp scheduler
657 static int set_sched_resources(struct device_queue_manager
*dqm
)
659 struct scheduling_resources res
;
660 unsigned int queue_num
, queue_mask
;
664 pr_debug("kfd: In func %s\n", __func__
);
666 queue_num
= get_pipes_num_cpsch() * QUEUES_PER_PIPE
;
667 queue_mask
= (1 << queue_num
) - 1;
668 res
.vmid_mask
= (1 << VMID_PER_DEVICE
) - 1;
669 res
.vmid_mask
<<= KFD_VMID_START_OFFSET
;
670 res
.queue_mask
= queue_mask
<< (get_first_pipe(dqm
) * QUEUES_PER_PIPE
);
671 res
.gws_mask
= res
.oac_mask
= res
.gds_heap_base
=
672 res
.gds_heap_size
= 0;
674 pr_debug("kfd: scheduling resources:\n"
675 " vmid mask: 0x%8X\n"
676 " queue mask: 0x%8llX\n",
677 res
.vmid_mask
, res
.queue_mask
);
679 return pm_send_set_resources(&dqm
->packets
, &res
);
682 static int initialize_cpsch(struct device_queue_manager
*dqm
)
688 pr_debug("kfd: In func %s num of pipes: %d\n",
689 __func__
, get_pipes_num_cpsch());
691 mutex_init(&dqm
->lock
);
692 INIT_LIST_HEAD(&dqm
->queues
);
693 dqm
->queue_count
= dqm
->processes_count
= 0;
694 dqm
->sdma_queue_count
= 0;
695 dqm
->active_runlist
= false;
696 retval
= dqm
->ops_asic_specific
.initialize(dqm
);
698 goto fail_init_pipelines
;
703 mutex_destroy(&dqm
->lock
);
707 static int start_cpsch(struct device_queue_manager
*dqm
)
709 struct device_process_node
*node
;
716 retval
= pm_init(&dqm
->packets
, dqm
);
718 goto fail_packet_manager_init
;
720 retval
= set_sched_resources(dqm
);
722 goto fail_set_sched_resources
;
724 pr_debug("kfd: allocating fence memory\n");
726 /* allocate fence memory on the gart */
727 retval
= kfd_gtt_sa_allocate(dqm
->dev
, sizeof(*dqm
->fence_addr
),
731 goto fail_allocate_vidmem
;
733 dqm
->fence_addr
= dqm
->fence_mem
->cpu_ptr
;
734 dqm
->fence_gpu_addr
= dqm
->fence_mem
->gpu_addr
;
735 list_for_each_entry(node
, &dqm
->queues
, list
)
736 if (node
->qpd
->pqm
->process
&& dqm
->dev
)
737 kfd_bind_process_to_device(dqm
->dev
,
738 node
->qpd
->pqm
->process
);
740 execute_queues_cpsch(dqm
, true);
743 fail_allocate_vidmem
:
744 fail_set_sched_resources
:
745 pm_uninit(&dqm
->packets
);
746 fail_packet_manager_init
:
750 static int stop_cpsch(struct device_queue_manager
*dqm
)
752 struct device_process_node
*node
;
753 struct kfd_process_device
*pdd
;
757 destroy_queues_cpsch(dqm
, true);
759 list_for_each_entry(node
, &dqm
->queues
, list
) {
760 pdd
= qpd_to_pdd(node
->qpd
);
763 kfd_gtt_sa_free(dqm
->dev
, dqm
->fence_mem
);
764 pm_uninit(&dqm
->packets
);
769 static int create_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
770 struct kernel_queue
*kq
,
771 struct qcm_process_device
*qpd
)
773 BUG_ON(!dqm
|| !kq
|| !qpd
);
775 pr_debug("kfd: In func %s\n", __func__
);
777 mutex_lock(&dqm
->lock
);
778 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
779 pr_warn("amdkfd: Can't create new kernel queue because %d queues were already created\n",
780 dqm
->total_queue_count
);
781 mutex_unlock(&dqm
->lock
);
786 * Unconditionally increment this counter, regardless of the queue's
787 * type or whether the queue is active.
789 dqm
->total_queue_count
++;
790 pr_debug("Total of %d queues are accountable so far\n",
791 dqm
->total_queue_count
);
793 list_add(&kq
->list
, &qpd
->priv_queue_list
);
795 qpd
->is_debug
= true;
796 execute_queues_cpsch(dqm
, false);
797 mutex_unlock(&dqm
->lock
);
802 static void destroy_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
803 struct kernel_queue
*kq
,
804 struct qcm_process_device
*qpd
)
808 pr_debug("kfd: In %s\n", __func__
);
810 mutex_lock(&dqm
->lock
);
811 destroy_queues_cpsch(dqm
, false);
814 qpd
->is_debug
= false;
815 execute_queues_cpsch(dqm
, false);
817 * Unconditionally decrement this counter, regardless of the queue's
820 dqm
->total_queue_count
++;
821 pr_debug("Total of %d queues are accountable so far\n",
822 dqm
->total_queue_count
);
823 mutex_unlock(&dqm
->lock
);
826 static void select_sdma_engine_id(struct queue
*q
)
830 q
->sdma_id
= sdma_id
;
831 sdma_id
= (sdma_id
+ 1) % 2;
834 static int create_queue_cpsch(struct device_queue_manager
*dqm
, struct queue
*q
,
835 struct qcm_process_device
*qpd
, int *allocate_vmid
)
838 struct mqd_manager
*mqd
;
840 BUG_ON(!dqm
|| !q
|| !qpd
);
847 mutex_lock(&dqm
->lock
);
849 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
850 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
851 dqm
->total_queue_count
);
856 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
857 select_sdma_engine_id(q
);
859 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
860 get_mqd_type_from_queue_type(q
->properties
.type
));
863 mutex_unlock(&dqm
->lock
);
867 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
868 &q
->gart_mqd_addr
, &q
->properties
);
872 list_add(&q
->list
, &qpd
->queues_list
);
873 if (q
->properties
.is_active
) {
875 retval
= execute_queues_cpsch(dqm
, false);
878 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
879 dqm
->sdma_queue_count
++;
881 * Unconditionally increment this counter, regardless of the queue's
882 * type or whether the queue is active.
884 dqm
->total_queue_count
++;
886 pr_debug("Total of %d queues are accountable so far\n",
887 dqm
->total_queue_count
);
890 mutex_unlock(&dqm
->lock
);
894 static int fence_wait_timeout(unsigned int *fence_addr
,
895 unsigned int fence_value
,
896 unsigned long timeout
)
901 while (*fence_addr
!= fence_value
) {
902 if (time_after(jiffies
, timeout
)) {
903 pr_err("kfd: qcm fence wait loop timeout expired\n");
912 static int destroy_sdma_queues(struct device_queue_manager
*dqm
,
913 unsigned int sdma_engine
)
915 return pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_SDMA
,
916 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false,
920 static int destroy_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
929 mutex_lock(&dqm
->lock
);
930 if (dqm
->active_runlist
== false)
933 pr_debug("kfd: Before destroying queues, sdma queue count is : %u\n",
934 dqm
->sdma_queue_count
);
936 if (dqm
->sdma_queue_count
> 0) {
937 destroy_sdma_queues(dqm
, 0);
938 destroy_sdma_queues(dqm
, 1);
941 retval
= pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_COMPUTE
,
942 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false, 0);
946 *dqm
->fence_addr
= KFD_FENCE_INIT
;
947 pm_send_query_status(&dqm
->packets
, dqm
->fence_gpu_addr
,
948 KFD_FENCE_COMPLETED
);
949 /* should be timed out */
950 fence_wait_timeout(dqm
->fence_addr
, KFD_FENCE_COMPLETED
,
951 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
);
952 pm_release_ib(&dqm
->packets
);
953 dqm
->active_runlist
= false;
957 mutex_unlock(&dqm
->lock
);
961 static int execute_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
968 mutex_lock(&dqm
->lock
);
970 retval
= destroy_queues_cpsch(dqm
, false);
972 pr_err("kfd: the cp might be in an unrecoverable state due to an unsuccessful queues preemption");
976 if (dqm
->queue_count
<= 0 || dqm
->processes_count
<= 0) {
981 if (dqm
->active_runlist
) {
986 retval
= pm_send_runlist(&dqm
->packets
, &dqm
->queues
);
988 pr_err("kfd: failed to execute runlist");
991 dqm
->active_runlist
= true;
995 mutex_unlock(&dqm
->lock
);
999 static int destroy_queue_cpsch(struct device_queue_manager
*dqm
,
1000 struct qcm_process_device
*qpd
,
1004 struct mqd_manager
*mqd
;
1006 BUG_ON(!dqm
|| !qpd
|| !q
);
1010 /* remove queue from list to prevent rescheduling after preemption */
1011 mutex_lock(&dqm
->lock
);
1012 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
1013 get_mqd_type_from_queue_type(q
->properties
.type
));
1019 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
1020 dqm
->sdma_queue_count
--;
1023 if (q
->properties
.is_active
)
1026 execute_queues_cpsch(dqm
, false);
1028 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
1031 * Unconditionally decrement this counter, regardless of the queue's
1034 dqm
->total_queue_count
--;
1035 pr_debug("Total of %d queues are accountable so far\n",
1036 dqm
->total_queue_count
);
1038 mutex_unlock(&dqm
->lock
);
1043 mutex_unlock(&dqm
->lock
);
1048 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1049 * stay in user mode.
1051 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1052 /* APE1 limit is inclusive and 64K aligned. */
1053 #define APE1_LIMIT_ALIGNMENT 0xFFFF
1055 static bool set_cache_memory_policy(struct device_queue_manager
*dqm
,
1056 struct qcm_process_device
*qpd
,
1057 enum cache_policy default_policy
,
1058 enum cache_policy alternate_policy
,
1059 void __user
*alternate_aperture_base
,
1060 uint64_t alternate_aperture_size
)
1064 pr_debug("kfd: In func %s\n", __func__
);
1066 mutex_lock(&dqm
->lock
);
1068 if (alternate_aperture_size
== 0) {
1069 /* base > limit disables APE1 */
1070 qpd
->sh_mem_ape1_base
= 1;
1071 qpd
->sh_mem_ape1_limit
= 0;
1074 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1075 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1076 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1077 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1078 * Verify that the base and size parameters can be
1079 * represented in this format and convert them.
1080 * Additionally restrict APE1 to user-mode addresses.
1083 uint64_t base
= (uintptr_t)alternate_aperture_base
;
1084 uint64_t limit
= base
+ alternate_aperture_size
- 1;
1089 if ((base
& APE1_FIXED_BITS_MASK
) != 0)
1092 if ((limit
& APE1_FIXED_BITS_MASK
) != APE1_LIMIT_ALIGNMENT
)
1095 qpd
->sh_mem_ape1_base
= base
>> 16;
1096 qpd
->sh_mem_ape1_limit
= limit
>> 16;
1099 retval
= dqm
->ops_asic_specific
.set_cache_memory_policy(
1104 alternate_aperture_base
,
1105 alternate_aperture_size
);
1107 if ((sched_policy
== KFD_SCHED_POLICY_NO_HWS
) && (qpd
->vmid
!= 0))
1108 program_sh_mem_settings(dqm
, qpd
);
1110 pr_debug("kfd: sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
1111 qpd
->sh_mem_config
, qpd
->sh_mem_ape1_base
,
1112 qpd
->sh_mem_ape1_limit
);
1114 mutex_unlock(&dqm
->lock
);
1118 mutex_unlock(&dqm
->lock
);
1122 struct device_queue_manager
*device_queue_manager_init(struct kfd_dev
*dev
)
1124 struct device_queue_manager
*dqm
;
1128 pr_debug("kfd: loading device queue manager\n");
1130 dqm
= kzalloc(sizeof(struct device_queue_manager
), GFP_KERNEL
);
1135 switch (sched_policy
) {
1136 case KFD_SCHED_POLICY_HWS
:
1137 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION
:
1138 /* initialize dqm for cp scheduling */
1139 dqm
->ops
.create_queue
= create_queue_cpsch
;
1140 dqm
->ops
.initialize
= initialize_cpsch
;
1141 dqm
->ops
.start
= start_cpsch
;
1142 dqm
->ops
.stop
= stop_cpsch
;
1143 dqm
->ops
.destroy_queue
= destroy_queue_cpsch
;
1144 dqm
->ops
.update_queue
= update_queue
;
1145 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1146 dqm
->ops
.register_process
= register_process_nocpsch
;
1147 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1148 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1149 dqm
->ops
.create_kernel_queue
= create_kernel_queue_cpsch
;
1150 dqm
->ops
.destroy_kernel_queue
= destroy_kernel_queue_cpsch
;
1151 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1153 case KFD_SCHED_POLICY_NO_HWS
:
1154 /* initialize dqm for no cp scheduling */
1155 dqm
->ops
.start
= start_nocpsch
;
1156 dqm
->ops
.stop
= stop_nocpsch
;
1157 dqm
->ops
.create_queue
= create_queue_nocpsch
;
1158 dqm
->ops
.destroy_queue
= destroy_queue_nocpsch
;
1159 dqm
->ops
.update_queue
= update_queue
;
1160 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1161 dqm
->ops
.register_process
= register_process_nocpsch
;
1162 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1163 dqm
->ops
.initialize
= initialize_nocpsch
;
1164 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1165 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1172 switch (dev
->device_info
->asic_family
) {
1174 device_queue_manager_init_vi(&dqm
->ops_asic_specific
);
1178 device_queue_manager_init_cik(&dqm
->ops_asic_specific
);
1182 if (dqm
->ops
.initialize(dqm
) != 0) {
1190 void device_queue_manager_uninit(struct device_queue_manager
*dqm
)
1194 dqm
->ops
.uninitialize(dqm
);