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/printk.h>
25 #include <linux/slab.h>
27 #include "kfd_mqd_manager.h"
29 #include "../../radeon/cikd.h"
30 #include "../../radeon/cik_reg.h"
32 inline void busy_wait(unsigned long ms
)
34 while (time_before(jiffies
, ms
))
38 static inline struct cik_mqd
*get_mqd(void *mqd
)
40 return (struct cik_mqd
*)mqd
;
43 static int init_mqd(struct mqd_manager
*mm
, void **mqd
,
44 struct kfd_mem_obj
**mqd_mem_obj
, uint64_t *gart_addr
,
45 struct queue_properties
*q
)
51 BUG_ON(!mm
|| !q
|| !mqd
);
53 pr_debug("kfd: In func %s\n", __func__
);
55 retval
= kfd_gtt_sa_allocate(mm
->dev
, sizeof(struct cik_mqd
),
61 m
= (struct cik_mqd
*) (*mqd_mem_obj
)->cpu_ptr
;
62 addr
= (*mqd_mem_obj
)->gpu_addr
;
64 memset(m
, 0, ALIGN(sizeof(struct cik_mqd
), 256));
66 m
->header
= 0xC0310800;
67 m
->compute_pipelinestat_enable
= 1;
68 m
->compute_static_thread_mgmt_se0
= 0xFFFFFFFF;
69 m
->compute_static_thread_mgmt_se1
= 0xFFFFFFFF;
70 m
->compute_static_thread_mgmt_se2
= 0xFFFFFFFF;
71 m
->compute_static_thread_mgmt_se3
= 0xFFFFFFFF;
74 * Make sure to use the last queue state saved on mqd when the cp
75 * reassigns the queue, so when queue is switched on/off (e.g over
76 * subscription or quantum timeout) the context will be consistent
78 m
->cp_hqd_persistent_state
=
79 DEFAULT_CP_HQD_PERSISTENT_STATE
| PRELOAD_REQ
;
81 m
->cp_mqd_control
= MQD_CONTROL_PRIV_STATE_EN
;
82 m
->cp_mqd_base_addr_lo
= lower_32_bits(addr
);
83 m
->cp_mqd_base_addr_hi
= upper_32_bits(addr
);
85 m
->cp_hqd_ib_control
= DEFAULT_MIN_IB_AVAIL_SIZE
| IB_ATC_EN
;
86 /* Although WinKFD writes this, I suspect it should not be necessary */
87 m
->cp_hqd_ib_control
= IB_ATC_EN
| DEFAULT_MIN_IB_AVAIL_SIZE
;
89 m
->cp_hqd_quantum
= QUANTUM_EN
| QUANTUM_SCALE_1MS
|
94 * Identifies the pipe relative priority when this queue is connected
95 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
96 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
97 * 0 = CS_LOW (typically below GFX)
98 * 1 = CS_MEDIUM (typically between HP3D and GFX
99 * 2 = CS_HIGH (typically above HP3D)
101 m
->cp_hqd_pipe_priority
= 1;
102 m
->cp_hqd_queue_priority
= 15;
105 if (gart_addr
!= NULL
)
107 retval
= mm
->update_mqd(mm
, m
, q
);
112 static int init_mqd_sdma(struct mqd_manager
*mm
, void **mqd
,
113 struct kfd_mem_obj
**mqd_mem_obj
, uint64_t *gart_addr
,
114 struct queue_properties
*q
)
117 struct cik_sdma_rlc_registers
*m
;
119 BUG_ON(!mm
|| !mqd
|| !mqd_mem_obj
);
121 retval
= kfd_gtt_sa_allocate(mm
->dev
,
122 sizeof(struct cik_sdma_rlc_registers
),
128 m
= (struct cik_sdma_rlc_registers
*) (*mqd_mem_obj
)->cpu_ptr
;
130 memset(m
, 0, sizeof(struct cik_sdma_rlc_registers
));
133 if (gart_addr
!= NULL
)
134 *gart_addr
= (*mqd_mem_obj
)->gpu_addr
;
136 retval
= mm
->update_mqd(mm
, m
, q
);
141 static void uninit_mqd(struct mqd_manager
*mm
, void *mqd
,
142 struct kfd_mem_obj
*mqd_mem_obj
)
145 kfd_gtt_sa_free(mm
->dev
, mqd_mem_obj
);
148 static void uninit_mqd_sdma(struct mqd_manager
*mm
, void *mqd
,
149 struct kfd_mem_obj
*mqd_mem_obj
)
152 kfd_gtt_sa_free(mm
->dev
, mqd_mem_obj
);
155 static int load_mqd(struct mqd_manager
*mm
, void *mqd
, uint32_t pipe_id
,
156 uint32_t queue_id
, uint32_t __user
*wptr
)
158 return kfd2kgd
->hqd_load(mm
->dev
->kgd
, mqd
, pipe_id
, queue_id
, wptr
);
161 static int load_mqd_sdma(struct mqd_manager
*mm
, void *mqd
,
162 uint32_t pipe_id
, uint32_t queue_id
,
163 uint32_t __user
*wptr
)
165 return kfd2kgd
->hqd_sdma_load(mm
->dev
->kgd
, mqd
);
168 static int update_mqd(struct mqd_manager
*mm
, void *mqd
,
169 struct queue_properties
*q
)
173 BUG_ON(!mm
|| !q
|| !mqd
);
175 pr_debug("kfd: In func %s\n", __func__
);
178 m
->cp_hqd_pq_control
= DEFAULT_RPTR_BLOCK_SIZE
|
179 DEFAULT_MIN_AVAIL_SIZE
| PQ_ATC_EN
;
182 * Calculating queue size which is log base 2 of actual queue size -1
183 * dwords and another -1 for ffs
185 m
->cp_hqd_pq_control
|= ffs(q
->queue_size
/ sizeof(unsigned int))
187 m
->cp_hqd_pq_base_lo
= lower_32_bits((uint64_t)q
->queue_address
>> 8);
188 m
->cp_hqd_pq_base_hi
= upper_32_bits((uint64_t)q
->queue_address
>> 8);
189 m
->cp_hqd_pq_rptr_report_addr_lo
= lower_32_bits((uint64_t)q
->read_ptr
);
190 m
->cp_hqd_pq_rptr_report_addr_hi
= upper_32_bits((uint64_t)q
->read_ptr
);
191 m
->cp_hqd_pq_doorbell_control
= DOORBELL_EN
|
192 DOORBELL_OFFSET(q
->doorbell_off
);
194 m
->cp_hqd_vmid
= q
->vmid
;
196 if (q
->format
== KFD_QUEUE_FORMAT_AQL
) {
197 m
->cp_hqd_iq_rptr
= AQL_ENABLE
;
198 m
->cp_hqd_pq_control
|= NO_UPDATE_RPTR
;
201 m
->cp_hqd_active
= 0;
202 q
->is_active
= false;
203 if (q
->queue_size
> 0 &&
204 q
->queue_address
!= 0 &&
205 q
->queue_percent
> 0) {
206 m
->cp_hqd_active
= 1;
213 static int update_mqd_sdma(struct mqd_manager
*mm
, void *mqd
,
214 struct queue_properties
*q
)
216 struct cik_sdma_rlc_registers
*m
;
218 BUG_ON(!mm
|| !mqd
|| !q
);
220 m
= get_sdma_mqd(mqd
);
221 m
->sdma_rlc_rb_cntl
=
222 SDMA_RB_SIZE((ffs(q
->queue_size
/ sizeof(unsigned int)))) |
223 SDMA_RB_VMID(q
->vmid
) |
224 SDMA_RPTR_WRITEBACK_ENABLE
|
225 SDMA_RPTR_WRITEBACK_TIMER(6);
227 m
->sdma_rlc_rb_base
= lower_32_bits(q
->queue_address
>> 8);
228 m
->sdma_rlc_rb_base_hi
= upper_32_bits(q
->queue_address
>> 8);
229 m
->sdma_rlc_rb_rptr_addr_lo
= lower_32_bits((uint64_t)q
->read_ptr
);
230 m
->sdma_rlc_rb_rptr_addr_hi
= upper_32_bits((uint64_t)q
->read_ptr
);
231 m
->sdma_rlc_doorbell
= SDMA_OFFSET(q
->doorbell_off
) | SDMA_DB_ENABLE
;
232 m
->sdma_rlc_virtual_addr
= q
->sdma_vm_addr
;
234 m
->sdma_engine_id
= q
->sdma_engine_id
;
235 m
->sdma_queue_id
= q
->sdma_queue_id
;
237 q
->is_active
= false;
238 if (q
->queue_size
> 0 &&
239 q
->queue_address
!= 0 &&
240 q
->queue_percent
> 0) {
241 m
->sdma_rlc_rb_cntl
|= SDMA_RB_ENABLE
;
248 static int destroy_mqd(struct mqd_manager
*mm
, void *mqd
,
249 enum kfd_preempt_type type
,
250 unsigned int timeout
, uint32_t pipe_id
,
253 return kfd2kgd
->hqd_destroy(mm
->dev
->kgd
, type
, timeout
,
258 * preempt type here is ignored because there is only one way
259 * to preempt sdma queue
261 static int destroy_mqd_sdma(struct mqd_manager
*mm
, void *mqd
,
262 enum kfd_preempt_type type
,
263 unsigned int timeout
, uint32_t pipe_id
,
266 return kfd2kgd
->hqd_sdma_destroy(mm
->dev
->kgd
, mqd
, timeout
);
269 static bool is_occupied(struct mqd_manager
*mm
, void *mqd
,
270 uint64_t queue_address
, uint32_t pipe_id
,
274 return kfd2kgd
->hqd_is_occupies(mm
->dev
->kgd
, queue_address
,
279 static bool is_occupied_sdma(struct mqd_manager
*mm
, void *mqd
,
280 uint64_t queue_address
, uint32_t pipe_id
,
283 return kfd2kgd
->hqd_sdma_is_occupied(mm
->dev
->kgd
, mqd
);
287 * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
288 * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
289 * queues but with different initial values.
292 static int init_mqd_hiq(struct mqd_manager
*mm
, void **mqd
,
293 struct kfd_mem_obj
**mqd_mem_obj
, uint64_t *gart_addr
,
294 struct queue_properties
*q
)
300 BUG_ON(!mm
|| !q
|| !mqd
|| !mqd_mem_obj
);
302 pr_debug("kfd: In func %s\n", __func__
);
304 retval
= kfd_gtt_sa_allocate(mm
->dev
, sizeof(struct cik_mqd
),
310 m
= (struct cik_mqd
*) (*mqd_mem_obj
)->cpu_ptr
;
311 addr
= (*mqd_mem_obj
)->gpu_addr
;
313 memset(m
, 0, ALIGN(sizeof(struct cik_mqd
), 256));
315 m
->header
= 0xC0310800;
316 m
->compute_pipelinestat_enable
= 1;
317 m
->compute_static_thread_mgmt_se0
= 0xFFFFFFFF;
318 m
->compute_static_thread_mgmt_se1
= 0xFFFFFFFF;
319 m
->compute_static_thread_mgmt_se2
= 0xFFFFFFFF;
320 m
->compute_static_thread_mgmt_se3
= 0xFFFFFFFF;
322 m
->cp_hqd_persistent_state
= DEFAULT_CP_HQD_PERSISTENT_STATE
|
324 m
->cp_hqd_quantum
= QUANTUM_EN
| QUANTUM_SCALE_1MS
|
325 QUANTUM_DURATION(10);
327 m
->cp_mqd_control
= MQD_CONTROL_PRIV_STATE_EN
;
328 m
->cp_mqd_base_addr_lo
= lower_32_bits(addr
);
329 m
->cp_mqd_base_addr_hi
= upper_32_bits(addr
);
331 m
->cp_hqd_ib_control
= DEFAULT_MIN_IB_AVAIL_SIZE
;
335 * Identifies the pipe relative priority when this queue is connected
336 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
337 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
338 * 0 = CS_LOW (typically below GFX)
339 * 1 = CS_MEDIUM (typically between HP3D and GFX
340 * 2 = CS_HIGH (typically above HP3D)
342 m
->cp_hqd_pipe_priority
= 1;
343 m
->cp_hqd_queue_priority
= 15;
348 retval
= mm
->update_mqd(mm
, m
, q
);
353 static int update_mqd_hiq(struct mqd_manager
*mm
, void *mqd
,
354 struct queue_properties
*q
)
358 BUG_ON(!mm
|| !q
|| !mqd
);
360 pr_debug("kfd: In func %s\n", __func__
);
363 m
->cp_hqd_pq_control
= DEFAULT_RPTR_BLOCK_SIZE
|
364 DEFAULT_MIN_AVAIL_SIZE
|
369 * Calculating queue size which is log base 2 of actual queue
372 m
->cp_hqd_pq_control
|= ffs(q
->queue_size
/ sizeof(unsigned int))
374 m
->cp_hqd_pq_base_lo
= lower_32_bits((uint64_t)q
->queue_address
>> 8);
375 m
->cp_hqd_pq_base_hi
= upper_32_bits((uint64_t)q
->queue_address
>> 8);
376 m
->cp_hqd_pq_rptr_report_addr_lo
= lower_32_bits((uint64_t)q
->read_ptr
);
377 m
->cp_hqd_pq_rptr_report_addr_hi
= upper_32_bits((uint64_t)q
->read_ptr
);
378 m
->cp_hqd_pq_doorbell_control
= DOORBELL_EN
|
379 DOORBELL_OFFSET(q
->doorbell_off
);
381 m
->cp_hqd_vmid
= q
->vmid
;
383 m
->cp_hqd_active
= 0;
384 q
->is_active
= false;
385 if (q
->queue_size
> 0 &&
386 q
->queue_address
!= 0 &&
387 q
->queue_percent
> 0) {
388 m
->cp_hqd_active
= 1;
396 * SDMA MQD Implementation
399 struct cik_sdma_rlc_registers
*get_sdma_mqd(void *mqd
)
401 struct cik_sdma_rlc_registers
*m
;
405 m
= (struct cik_sdma_rlc_registers
*)mqd
;
410 struct mqd_manager
*mqd_manager_init(enum KFD_MQD_TYPE type
,
413 struct mqd_manager
*mqd
;
416 BUG_ON(type
>= KFD_MQD_TYPE_MAX
);
418 pr_debug("kfd: In func %s\n", __func__
);
420 mqd
= kzalloc(sizeof(struct mqd_manager
), GFP_KERNEL
);
427 case KFD_MQD_TYPE_CIK_CP
:
428 case KFD_MQD_TYPE_CIK_COMPUTE
:
429 mqd
->init_mqd
= init_mqd
;
430 mqd
->uninit_mqd
= uninit_mqd
;
431 mqd
->load_mqd
= load_mqd
;
432 mqd
->update_mqd
= update_mqd
;
433 mqd
->destroy_mqd
= destroy_mqd
;
434 mqd
->is_occupied
= is_occupied
;
436 case KFD_MQD_TYPE_CIK_HIQ
:
437 mqd
->init_mqd
= init_mqd_hiq
;
438 mqd
->uninit_mqd
= uninit_mqd
;
439 mqd
->load_mqd
= load_mqd
;
440 mqd
->update_mqd
= update_mqd_hiq
;
441 mqd
->destroy_mqd
= destroy_mqd
;
442 mqd
->is_occupied
= is_occupied
;
444 case KFD_MQD_TYPE_CIK_SDMA
:
445 mqd
->init_mqd
= init_mqd_sdma
;
446 mqd
->uninit_mqd
= uninit_mqd_sdma
;
447 mqd
->load_mqd
= load_mqd_sdma
;
448 mqd
->update_mqd
= update_mqd_sdma
;
449 mqd
->destroy_mqd
= destroy_mqd_sdma
;
450 mqd
->is_occupied
= is_occupied_sdma
;
460 /* SDMA queues should be implemented here when the cp will supports them */