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 kfd2kgd
->program_sh_mem_settings(dqm
->dev
->kgd
, qpd
->vmid
,
87 qpd
->sh_mem_ape1_base
,
88 qpd
->sh_mem_ape1_limit
,
92 static int allocate_vmid(struct device_queue_manager
*dqm
,
93 struct qcm_process_device
*qpd
,
96 int bit
, allocated_vmid
;
98 if (dqm
->vmid_bitmap
== 0)
101 bit
= find_first_bit((unsigned long *)&dqm
->vmid_bitmap
, CIK_VMID_NUM
);
102 clear_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
104 /* Kaveri kfd vmid's starts from vmid 8 */
105 allocated_vmid
= bit
+ KFD_VMID_START_OFFSET
;
106 pr_debug("kfd: vmid allocation %d\n", allocated_vmid
);
107 qpd
->vmid
= allocated_vmid
;
108 q
->properties
.vmid
= allocated_vmid
;
110 set_pasid_vmid_mapping(dqm
, q
->process
->pasid
, q
->properties
.vmid
);
111 program_sh_mem_settings(dqm
, qpd
);
116 static void deallocate_vmid(struct device_queue_manager
*dqm
,
117 struct qcm_process_device
*qpd
,
120 int bit
= qpd
->vmid
- KFD_VMID_START_OFFSET
;
122 /* Release the vmid mapping */
123 set_pasid_vmid_mapping(dqm
, 0, qpd
->vmid
);
125 set_bit(bit
, (unsigned long *)&dqm
->vmid_bitmap
);
127 q
->properties
.vmid
= 0;
130 static int create_queue_nocpsch(struct device_queue_manager
*dqm
,
132 struct qcm_process_device
*qpd
,
137 BUG_ON(!dqm
|| !q
|| !qpd
|| !allocated_vmid
);
139 pr_debug("kfd: In func %s\n", __func__
);
142 mutex_lock(&dqm
->lock
);
144 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
145 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
146 dqm
->total_queue_count
);
147 mutex_unlock(&dqm
->lock
);
151 if (list_empty(&qpd
->queues_list
)) {
152 retval
= allocate_vmid(dqm
, qpd
, q
);
154 mutex_unlock(&dqm
->lock
);
158 *allocated_vmid
= qpd
->vmid
;
159 q
->properties
.vmid
= qpd
->vmid
;
161 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
)
162 retval
= create_compute_queue_nocpsch(dqm
, q
, qpd
);
163 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
164 retval
= create_sdma_queue_nocpsch(dqm
, q
, qpd
);
167 if (list_empty(&qpd
->queues_list
)) {
168 deallocate_vmid(dqm
, qpd
, q
);
171 mutex_unlock(&dqm
->lock
);
175 list_add(&q
->list
, &qpd
->queues_list
);
176 if (q
->properties
.is_active
)
179 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
180 dqm
->sdma_queue_count
++;
183 * Unconditionally increment this counter, regardless of the queue's
184 * type or whether the queue is active.
186 dqm
->total_queue_count
++;
187 pr_debug("Total of %d queues are accountable so far\n",
188 dqm
->total_queue_count
);
190 mutex_unlock(&dqm
->lock
);
194 static int allocate_hqd(struct device_queue_manager
*dqm
, struct queue
*q
)
201 for (pipe
= dqm
->next_pipe_to_allocate
, i
= 0; i
< get_pipes_num(dqm
);
202 pipe
= ((pipe
+ 1) % get_pipes_num(dqm
)), ++i
) {
203 if (dqm
->allocated_queues
[pipe
] != 0) {
204 bit
= find_first_bit(
205 (unsigned long *)&dqm
->allocated_queues
[pipe
],
209 (unsigned long *)&dqm
->allocated_queues
[pipe
]);
220 pr_debug("kfd: DQM %s hqd slot - pipe (%d) queue(%d)\n",
221 __func__
, q
->pipe
, q
->queue
);
222 /* horizontal hqd allocation */
223 dqm
->next_pipe_to_allocate
= (pipe
+ 1) % get_pipes_num(dqm
);
228 static inline void deallocate_hqd(struct device_queue_manager
*dqm
,
231 set_bit(q
->queue
, (unsigned long *)&dqm
->allocated_queues
[q
->pipe
]);
234 static int create_compute_queue_nocpsch(struct device_queue_manager
*dqm
,
236 struct qcm_process_device
*qpd
)
239 struct mqd_manager
*mqd
;
241 BUG_ON(!dqm
|| !q
|| !qpd
);
243 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
247 retval
= allocate_hqd(dqm
, q
);
251 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
252 &q
->gart_mqd_addr
, &q
->properties
);
254 deallocate_hqd(dqm
, q
);
258 pr_debug("kfd: loading mqd to hqd on pipe (%d) queue (%d)\n",
262 retval
= mqd
->load_mqd(mqd
, q
->mqd
, q
->pipe
,
263 q
->queue
, (uint32_t __user
*) q
->properties
.write_ptr
);
265 deallocate_hqd(dqm
, q
);
266 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
273 static int destroy_queue_nocpsch(struct device_queue_manager
*dqm
,
274 struct qcm_process_device
*qpd
,
278 struct mqd_manager
*mqd
;
280 BUG_ON(!dqm
|| !q
|| !q
->mqd
|| !qpd
);
284 pr_debug("kfd: In Func %s\n", __func__
);
286 mutex_lock(&dqm
->lock
);
288 if (q
->properties
.type
== KFD_QUEUE_TYPE_COMPUTE
) {
289 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
294 deallocate_hqd(dqm
, q
);
295 } else if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
) {
296 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
301 dqm
->sdma_queue_count
--;
302 deallocate_sdma_queue(dqm
, q
->sdma_id
);
304 pr_debug("q->properties.type is invalid (%d)\n",
310 retval
= mqd
->destroy_mqd(mqd
, q
->mqd
,
311 KFD_PREEMPT_TYPE_WAVEFRONT_RESET
,
312 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
,
318 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
321 if (list_empty(&qpd
->queues_list
))
322 deallocate_vmid(dqm
, qpd
, q
);
323 if (q
->properties
.is_active
)
327 * Unconditionally decrement this counter, regardless of the queue's
330 dqm
->total_queue_count
--;
331 pr_debug("Total of %d queues are accountable so far\n",
332 dqm
->total_queue_count
);
335 mutex_unlock(&dqm
->lock
);
339 static int update_queue(struct device_queue_manager
*dqm
, struct queue
*q
)
342 struct mqd_manager
*mqd
;
343 bool prev_active
= false;
345 BUG_ON(!dqm
|| !q
|| !q
->mqd
);
347 mutex_lock(&dqm
->lock
);
348 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
349 get_mqd_type_from_queue_type(q
->properties
.type
));
351 mutex_unlock(&dqm
->lock
);
355 if (q
->properties
.is_active
== true)
360 * check active state vs. the previous state
361 * and modify counter accordingly
363 retval
= mqd
->update_mqd(mqd
, q
->mqd
, &q
->properties
);
364 if ((q
->properties
.is_active
== true) && (prev_active
== false))
366 else if ((q
->properties
.is_active
== false) && (prev_active
== true))
369 if (sched_policy
!= KFD_SCHED_POLICY_NO_HWS
)
370 retval
= execute_queues_cpsch(dqm
, false);
372 mutex_unlock(&dqm
->lock
);
376 static struct mqd_manager
*get_mqd_manager_nocpsch(
377 struct device_queue_manager
*dqm
, enum KFD_MQD_TYPE type
)
379 struct mqd_manager
*mqd
;
381 BUG_ON(!dqm
|| type
>= KFD_MQD_TYPE_MAX
);
383 pr_debug("kfd: In func %s mqd type %d\n", __func__
, type
);
385 mqd
= dqm
->mqds
[type
];
387 mqd
= mqd_manager_init(type
, dqm
->dev
);
389 pr_err("kfd: mqd manager is NULL");
390 dqm
->mqds
[type
] = mqd
;
396 static int register_process_nocpsch(struct device_queue_manager
*dqm
,
397 struct qcm_process_device
*qpd
)
399 struct device_process_node
*n
;
402 BUG_ON(!dqm
|| !qpd
);
404 pr_debug("kfd: In func %s\n", __func__
);
406 n
= kzalloc(sizeof(struct device_process_node
), GFP_KERNEL
);
412 mutex_lock(&dqm
->lock
);
413 list_add(&n
->list
, &dqm
->queues
);
415 retval
= dqm
->ops_asic_specific
.register_process(dqm
, qpd
);
417 dqm
->processes_count
++;
419 mutex_unlock(&dqm
->lock
);
424 static int unregister_process_nocpsch(struct device_queue_manager
*dqm
,
425 struct qcm_process_device
*qpd
)
428 struct device_process_node
*cur
, *next
;
430 BUG_ON(!dqm
|| !qpd
);
432 BUG_ON(!list_empty(&qpd
->queues_list
));
434 pr_debug("kfd: In func %s\n", __func__
);
437 mutex_lock(&dqm
->lock
);
439 list_for_each_entry_safe(cur
, next
, &dqm
->queues
, list
) {
440 if (qpd
== cur
->qpd
) {
441 list_del(&cur
->list
);
443 dqm
->processes_count
--;
447 /* qpd not found in dqm list */
450 mutex_unlock(&dqm
->lock
);
455 set_pasid_vmid_mapping(struct device_queue_manager
*dqm
, unsigned int pasid
,
458 uint32_t pasid_mapping
;
460 pasid_mapping
= (pasid
== 0) ? 0 : (uint32_t)pasid
|
461 ATC_VMID_PASID_MAPPING_VALID
;
462 return kfd2kgd
->set_pasid_vmid_mapping(dqm
->dev
->kgd
, pasid_mapping
,
466 int init_pipelines(struct device_queue_manager
*dqm
,
467 unsigned int pipes_num
, unsigned int first_pipe
)
470 struct mqd_manager
*mqd
;
471 unsigned int i
, err
, inx
;
472 uint64_t pipe_hpd_addr
;
474 BUG_ON(!dqm
|| !dqm
->dev
);
476 pr_debug("kfd: In func %s\n", __func__
);
479 * Allocate memory for the HPDs. This is hardware-owned per-pipe data.
480 * The driver never accesses this memory after zeroing it.
481 * It doesn't even have to be saved/restored on suspend/resume
482 * because it contains no data when there are no active queues.
485 err
= kfd_gtt_sa_allocate(dqm
->dev
, CIK_HPD_EOP_BYTES
* pipes_num
,
489 pr_err("kfd: error allocate vidmem num pipes: %d\n",
494 hpdptr
= dqm
->pipeline_mem
->cpu_ptr
;
495 dqm
->pipelines_addr
= dqm
->pipeline_mem
->gpu_addr
;
497 memset(hpdptr
, 0, CIK_HPD_EOP_BYTES
* pipes_num
);
499 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_COMPUTE
);
501 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
505 for (i
= 0; i
< pipes_num
; i
++) {
506 inx
= i
+ first_pipe
;
508 * HPD buffer on GTT is allocated by amdkfd, no need to waste
509 * space in GTT for pipelines we don't initialize
511 pipe_hpd_addr
= dqm
->pipelines_addr
+ i
* CIK_HPD_EOP_BYTES
;
512 pr_debug("kfd: pipeline address %llX\n", pipe_hpd_addr
);
513 /* = log2(bytes/4)-1 */
514 kfd2kgd
->init_pipeline(dqm
->dev
->kgd
, inx
,
515 CIK_HPD_EOP_BYTES_LOG2
- 3, pipe_hpd_addr
);
521 static int init_scheduler(struct device_queue_manager
*dqm
)
527 pr_debug("kfd: In %s\n", __func__
);
529 retval
= init_pipelines(dqm
, get_pipes_num(dqm
), get_first_pipe(dqm
));
533 static int initialize_nocpsch(struct device_queue_manager
*dqm
)
539 pr_debug("kfd: In func %s num of pipes: %d\n",
540 __func__
, get_pipes_num(dqm
));
542 mutex_init(&dqm
->lock
);
543 INIT_LIST_HEAD(&dqm
->queues
);
544 dqm
->queue_count
= dqm
->next_pipe_to_allocate
= 0;
545 dqm
->sdma_queue_count
= 0;
546 dqm
->allocated_queues
= kcalloc(get_pipes_num(dqm
),
547 sizeof(unsigned int), GFP_KERNEL
);
548 if (!dqm
->allocated_queues
) {
549 mutex_destroy(&dqm
->lock
);
553 for (i
= 0; i
< get_pipes_num(dqm
); i
++)
554 dqm
->allocated_queues
[i
] = (1 << QUEUES_PER_PIPE
) - 1;
556 dqm
->vmid_bitmap
= (1 << VMID_PER_DEVICE
) - 1;
557 dqm
->sdma_bitmap
= (1 << CIK_SDMA_QUEUES
) - 1;
563 static void uninitialize_nocpsch(struct device_queue_manager
*dqm
)
569 BUG_ON(dqm
->queue_count
> 0 || dqm
->processes_count
> 0);
571 kfree(dqm
->allocated_queues
);
572 for (i
= 0 ; i
< KFD_MQD_TYPE_MAX
; i
++)
574 mutex_destroy(&dqm
->lock
);
575 kfd_gtt_sa_free(dqm
->dev
, dqm
->pipeline_mem
);
578 static int start_nocpsch(struct device_queue_manager
*dqm
)
583 static int stop_nocpsch(struct device_queue_manager
*dqm
)
588 static int allocate_sdma_queue(struct device_queue_manager
*dqm
,
589 unsigned int *sdma_queue_id
)
593 if (dqm
->sdma_bitmap
== 0)
596 bit
= find_first_bit((unsigned long *)&dqm
->sdma_bitmap
,
599 clear_bit(bit
, (unsigned long *)&dqm
->sdma_bitmap
);
600 *sdma_queue_id
= bit
;
605 static void deallocate_sdma_queue(struct device_queue_manager
*dqm
,
606 unsigned int sdma_queue_id
)
608 if (sdma_queue_id
>= CIK_SDMA_QUEUES
)
610 set_bit(sdma_queue_id
, (unsigned long *)&dqm
->sdma_bitmap
);
613 static void init_sdma_vm(struct device_queue_manager
*dqm
, struct queue
*q
,
614 struct qcm_process_device
*qpd
)
616 uint32_t value
= SDMA_ATC
;
618 if (q
->process
->is_32bit_user_mode
)
619 value
|= SDMA_VA_PTR32
| get_sh_mem_bases_32(qpd_to_pdd(qpd
));
621 value
|= SDMA_VA_SHARED_BASE(get_sh_mem_bases_nybble_64(
623 q
->properties
.sdma_vm_addr
= value
;
626 static int create_sdma_queue_nocpsch(struct device_queue_manager
*dqm
,
628 struct qcm_process_device
*qpd
)
630 struct mqd_manager
*mqd
;
633 mqd
= dqm
->ops
.get_mqd_manager(dqm
, KFD_MQD_TYPE_SDMA
);
637 retval
= allocate_sdma_queue(dqm
, &q
->sdma_id
);
641 q
->properties
.sdma_queue_id
= q
->sdma_id
% CIK_SDMA_QUEUES_PER_ENGINE
;
642 q
->properties
.sdma_engine_id
= q
->sdma_id
/ CIK_SDMA_ENGINE_NUM
;
644 pr_debug("kfd: sdma id is: %d\n", q
->sdma_id
);
645 pr_debug(" sdma queue id: %d\n", q
->properties
.sdma_queue_id
);
646 pr_debug(" sdma engine id: %d\n", q
->properties
.sdma_engine_id
);
648 init_sdma_vm(dqm
, q
, qpd
);
649 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
650 &q
->gart_mqd_addr
, &q
->properties
);
652 deallocate_sdma_queue(dqm
, q
->sdma_id
);
656 retval
= mqd
->load_mqd(mqd
, q
->mqd
, 0,
659 deallocate_sdma_queue(dqm
, q
->sdma_id
);
660 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
668 * Device Queue Manager implementation for cp scheduler
671 static int set_sched_resources(struct device_queue_manager
*dqm
)
673 struct scheduling_resources res
;
674 unsigned int queue_num
, queue_mask
;
678 pr_debug("kfd: In func %s\n", __func__
);
680 queue_num
= get_pipes_num_cpsch() * QUEUES_PER_PIPE
;
681 queue_mask
= (1 << queue_num
) - 1;
682 res
.vmid_mask
= (1 << VMID_PER_DEVICE
) - 1;
683 res
.vmid_mask
<<= KFD_VMID_START_OFFSET
;
684 res
.queue_mask
= queue_mask
<< (get_first_pipe(dqm
) * QUEUES_PER_PIPE
);
685 res
.gws_mask
= res
.oac_mask
= res
.gds_heap_base
=
686 res
.gds_heap_size
= 0;
688 pr_debug("kfd: scheduling resources:\n"
689 " vmid mask: 0x%8X\n"
690 " queue mask: 0x%8llX\n",
691 res
.vmid_mask
, res
.queue_mask
);
693 return pm_send_set_resources(&dqm
->packets
, &res
);
696 static int initialize_cpsch(struct device_queue_manager
*dqm
)
702 pr_debug("kfd: In func %s num of pipes: %d\n",
703 __func__
, get_pipes_num_cpsch());
705 mutex_init(&dqm
->lock
);
706 INIT_LIST_HEAD(&dqm
->queues
);
707 dqm
->queue_count
= dqm
->processes_count
= 0;
708 dqm
->sdma_queue_count
= 0;
709 dqm
->active_runlist
= false;
710 retval
= dqm
->ops_asic_specific
.initialize(dqm
);
712 goto fail_init_pipelines
;
717 mutex_destroy(&dqm
->lock
);
721 static int start_cpsch(struct device_queue_manager
*dqm
)
723 struct device_process_node
*node
;
730 retval
= pm_init(&dqm
->packets
, dqm
);
732 goto fail_packet_manager_init
;
734 retval
= set_sched_resources(dqm
);
736 goto fail_set_sched_resources
;
738 pr_debug("kfd: allocating fence memory\n");
740 /* allocate fence memory on the gart */
741 retval
= kfd_gtt_sa_allocate(dqm
->dev
, sizeof(*dqm
->fence_addr
),
745 goto fail_allocate_vidmem
;
747 dqm
->fence_addr
= dqm
->fence_mem
->cpu_ptr
;
748 dqm
->fence_gpu_addr
= dqm
->fence_mem
->gpu_addr
;
749 list_for_each_entry(node
, &dqm
->queues
, list
)
750 if (node
->qpd
->pqm
->process
&& dqm
->dev
)
751 kfd_bind_process_to_device(dqm
->dev
,
752 node
->qpd
->pqm
->process
);
754 execute_queues_cpsch(dqm
, true);
757 fail_allocate_vidmem
:
758 fail_set_sched_resources
:
759 pm_uninit(&dqm
->packets
);
760 fail_packet_manager_init
:
764 static int stop_cpsch(struct device_queue_manager
*dqm
)
766 struct device_process_node
*node
;
767 struct kfd_process_device
*pdd
;
771 destroy_queues_cpsch(dqm
, true);
773 list_for_each_entry(node
, &dqm
->queues
, list
) {
774 pdd
= qpd_to_pdd(node
->qpd
);
777 kfd_gtt_sa_free(dqm
->dev
, dqm
->fence_mem
);
778 pm_uninit(&dqm
->packets
);
783 static int create_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
784 struct kernel_queue
*kq
,
785 struct qcm_process_device
*qpd
)
787 BUG_ON(!dqm
|| !kq
|| !qpd
);
789 pr_debug("kfd: In func %s\n", __func__
);
791 mutex_lock(&dqm
->lock
);
792 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
793 pr_warn("amdkfd: Can't create new kernel queue because %d queues were already created\n",
794 dqm
->total_queue_count
);
795 mutex_unlock(&dqm
->lock
);
800 * Unconditionally increment this counter, regardless of the queue's
801 * type or whether the queue is active.
803 dqm
->total_queue_count
++;
804 pr_debug("Total of %d queues are accountable so far\n",
805 dqm
->total_queue_count
);
807 list_add(&kq
->list
, &qpd
->priv_queue_list
);
809 qpd
->is_debug
= true;
810 execute_queues_cpsch(dqm
, false);
811 mutex_unlock(&dqm
->lock
);
816 static void destroy_kernel_queue_cpsch(struct device_queue_manager
*dqm
,
817 struct kernel_queue
*kq
,
818 struct qcm_process_device
*qpd
)
822 pr_debug("kfd: In %s\n", __func__
);
824 mutex_lock(&dqm
->lock
);
825 destroy_queues_cpsch(dqm
, false);
828 qpd
->is_debug
= false;
829 execute_queues_cpsch(dqm
, false);
831 * Unconditionally decrement this counter, regardless of the queue's
834 dqm
->total_queue_count
--;
835 pr_debug("Total of %d queues are accountable so far\n",
836 dqm
->total_queue_count
);
837 mutex_unlock(&dqm
->lock
);
840 static void select_sdma_engine_id(struct queue
*q
)
844 q
->sdma_id
= sdma_id
;
845 sdma_id
= (sdma_id
+ 1) % 2;
848 static int create_queue_cpsch(struct device_queue_manager
*dqm
, struct queue
*q
,
849 struct qcm_process_device
*qpd
, int *allocate_vmid
)
852 struct mqd_manager
*mqd
;
854 BUG_ON(!dqm
|| !q
|| !qpd
);
861 mutex_lock(&dqm
->lock
);
863 if (dqm
->total_queue_count
>= max_num_of_queues_per_device
) {
864 pr_warn("amdkfd: Can't create new usermode queue because %d queues were already created\n",
865 dqm
->total_queue_count
);
870 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
871 select_sdma_engine_id(q
);
873 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
874 get_mqd_type_from_queue_type(q
->properties
.type
));
877 mutex_unlock(&dqm
->lock
);
881 retval
= mqd
->init_mqd(mqd
, &q
->mqd
, &q
->mqd_mem_obj
,
882 &q
->gart_mqd_addr
, &q
->properties
);
886 list_add(&q
->list
, &qpd
->queues_list
);
887 if (q
->properties
.is_active
) {
889 retval
= execute_queues_cpsch(dqm
, false);
892 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
893 dqm
->sdma_queue_count
++;
895 * Unconditionally increment this counter, regardless of the queue's
896 * type or whether the queue is active.
898 dqm
->total_queue_count
++;
900 pr_debug("Total of %d queues are accountable so far\n",
901 dqm
->total_queue_count
);
904 mutex_unlock(&dqm
->lock
);
908 static int fence_wait_timeout(unsigned int *fence_addr
,
909 unsigned int fence_value
,
910 unsigned long timeout
)
915 while (*fence_addr
!= fence_value
) {
916 if (time_after(jiffies
, timeout
)) {
917 pr_err("kfd: qcm fence wait loop timeout expired\n");
926 static int destroy_sdma_queues(struct device_queue_manager
*dqm
,
927 unsigned int sdma_engine
)
929 return pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_SDMA
,
930 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false,
934 static int destroy_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
943 mutex_lock(&dqm
->lock
);
944 if (dqm
->active_runlist
== false)
947 pr_debug("kfd: Before destroying queues, sdma queue count is : %u\n",
948 dqm
->sdma_queue_count
);
950 if (dqm
->sdma_queue_count
> 0) {
951 destroy_sdma_queues(dqm
, 0);
952 destroy_sdma_queues(dqm
, 1);
955 retval
= pm_send_unmap_queue(&dqm
->packets
, KFD_QUEUE_TYPE_COMPUTE
,
956 KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES
, 0, false, 0);
960 *dqm
->fence_addr
= KFD_FENCE_INIT
;
961 pm_send_query_status(&dqm
->packets
, dqm
->fence_gpu_addr
,
962 KFD_FENCE_COMPLETED
);
963 /* should be timed out */
964 fence_wait_timeout(dqm
->fence_addr
, KFD_FENCE_COMPLETED
,
965 QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS
);
966 pm_release_ib(&dqm
->packets
);
967 dqm
->active_runlist
= false;
971 mutex_unlock(&dqm
->lock
);
975 static int execute_queues_cpsch(struct device_queue_manager
*dqm
, bool lock
)
982 mutex_lock(&dqm
->lock
);
984 retval
= destroy_queues_cpsch(dqm
, false);
986 pr_err("kfd: the cp might be in an unrecoverable state due to an unsuccessful queues preemption");
990 if (dqm
->queue_count
<= 0 || dqm
->processes_count
<= 0) {
995 if (dqm
->active_runlist
) {
1000 retval
= pm_send_runlist(&dqm
->packets
, &dqm
->queues
);
1002 pr_err("kfd: failed to execute runlist");
1005 dqm
->active_runlist
= true;
1009 mutex_unlock(&dqm
->lock
);
1013 static int destroy_queue_cpsch(struct device_queue_manager
*dqm
,
1014 struct qcm_process_device
*qpd
,
1018 struct mqd_manager
*mqd
;
1020 BUG_ON(!dqm
|| !qpd
|| !q
);
1024 /* remove queue from list to prevent rescheduling after preemption */
1025 mutex_lock(&dqm
->lock
);
1026 mqd
= dqm
->ops
.get_mqd_manager(dqm
,
1027 get_mqd_type_from_queue_type(q
->properties
.type
));
1033 if (q
->properties
.type
== KFD_QUEUE_TYPE_SDMA
)
1034 dqm
->sdma_queue_count
--;
1037 if (q
->properties
.is_active
)
1040 execute_queues_cpsch(dqm
, false);
1042 mqd
->uninit_mqd(mqd
, q
->mqd
, q
->mqd_mem_obj
);
1045 * Unconditionally decrement this counter, regardless of the queue's
1048 dqm
->total_queue_count
--;
1049 pr_debug("Total of %d queues are accountable so far\n",
1050 dqm
->total_queue_count
);
1052 mutex_unlock(&dqm
->lock
);
1057 mutex_unlock(&dqm
->lock
);
1062 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1063 * stay in user mode.
1065 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1066 /* APE1 limit is inclusive and 64K aligned. */
1067 #define APE1_LIMIT_ALIGNMENT 0xFFFF
1069 static bool set_cache_memory_policy(struct device_queue_manager
*dqm
,
1070 struct qcm_process_device
*qpd
,
1071 enum cache_policy default_policy
,
1072 enum cache_policy alternate_policy
,
1073 void __user
*alternate_aperture_base
,
1074 uint64_t alternate_aperture_size
)
1078 pr_debug("kfd: In func %s\n", __func__
);
1080 mutex_lock(&dqm
->lock
);
1082 if (alternate_aperture_size
== 0) {
1083 /* base > limit disables APE1 */
1084 qpd
->sh_mem_ape1_base
= 1;
1085 qpd
->sh_mem_ape1_limit
= 0;
1088 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1089 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1090 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1091 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1092 * Verify that the base and size parameters can be
1093 * represented in this format and convert them.
1094 * Additionally restrict APE1 to user-mode addresses.
1097 uint64_t base
= (uintptr_t)alternate_aperture_base
;
1098 uint64_t limit
= base
+ alternate_aperture_size
- 1;
1103 if ((base
& APE1_FIXED_BITS_MASK
) != 0)
1106 if ((limit
& APE1_FIXED_BITS_MASK
) != APE1_LIMIT_ALIGNMENT
)
1109 qpd
->sh_mem_ape1_base
= base
>> 16;
1110 qpd
->sh_mem_ape1_limit
= limit
>> 16;
1113 retval
= dqm
->ops_asic_specific
.set_cache_memory_policy(
1118 alternate_aperture_base
,
1119 alternate_aperture_size
);
1121 if ((sched_policy
== KFD_SCHED_POLICY_NO_HWS
) && (qpd
->vmid
!= 0))
1122 program_sh_mem_settings(dqm
, qpd
);
1124 pr_debug("kfd: sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
1125 qpd
->sh_mem_config
, qpd
->sh_mem_ape1_base
,
1126 qpd
->sh_mem_ape1_limit
);
1128 mutex_unlock(&dqm
->lock
);
1132 mutex_unlock(&dqm
->lock
);
1136 struct device_queue_manager
*device_queue_manager_init(struct kfd_dev
*dev
)
1138 struct device_queue_manager
*dqm
;
1142 pr_debug("kfd: loading device queue manager\n");
1144 dqm
= kzalloc(sizeof(struct device_queue_manager
), GFP_KERNEL
);
1149 switch (sched_policy
) {
1150 case KFD_SCHED_POLICY_HWS
:
1151 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION
:
1152 /* initialize dqm for cp scheduling */
1153 dqm
->ops
.create_queue
= create_queue_cpsch
;
1154 dqm
->ops
.initialize
= initialize_cpsch
;
1155 dqm
->ops
.start
= start_cpsch
;
1156 dqm
->ops
.stop
= stop_cpsch
;
1157 dqm
->ops
.destroy_queue
= destroy_queue_cpsch
;
1158 dqm
->ops
.update_queue
= update_queue
;
1159 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1160 dqm
->ops
.register_process
= register_process_nocpsch
;
1161 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1162 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1163 dqm
->ops
.create_kernel_queue
= create_kernel_queue_cpsch
;
1164 dqm
->ops
.destroy_kernel_queue
= destroy_kernel_queue_cpsch
;
1165 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1167 case KFD_SCHED_POLICY_NO_HWS
:
1168 /* initialize dqm for no cp scheduling */
1169 dqm
->ops
.start
= start_nocpsch
;
1170 dqm
->ops
.stop
= stop_nocpsch
;
1171 dqm
->ops
.create_queue
= create_queue_nocpsch
;
1172 dqm
->ops
.destroy_queue
= destroy_queue_nocpsch
;
1173 dqm
->ops
.update_queue
= update_queue
;
1174 dqm
->ops
.get_mqd_manager
= get_mqd_manager_nocpsch
;
1175 dqm
->ops
.register_process
= register_process_nocpsch
;
1176 dqm
->ops
.unregister_process
= unregister_process_nocpsch
;
1177 dqm
->ops
.initialize
= initialize_nocpsch
;
1178 dqm
->ops
.uninitialize
= uninitialize_nocpsch
;
1179 dqm
->ops
.set_cache_memory_policy
= set_cache_memory_policy
;
1186 switch (dev
->device_info
->asic_family
) {
1188 device_queue_manager_init_vi(&dqm
->ops_asic_specific
);
1192 device_queue_manager_init_cik(&dqm
->ops_asic_specific
);
1196 if (dqm
->ops
.initialize(dqm
) != 0) {
1204 void device_queue_manager_uninit(struct device_queue_manager
*dqm
)
1208 dqm
->ops
.uninitialize(dqm
);