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/cik_reg.h"
31 inline void busy_wait(unsigned long ms)
33 while (time_before(jiffies, ms))
37 static inline struct cik_mqd *get_mqd(void *mqd)
39 return (struct cik_mqd *)mqd;
42 static int init_mqd(struct mqd_manager *mm, void **mqd,
43 struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
44 struct queue_properties *q)
50 BUG_ON(!mm || !q || !mqd);
52 pr_debug("kfd: In func %s\n", __func__);
54 retval = kfd2kgd->allocate_mem(mm->dev->kgd,
55 sizeof(struct cik_mqd),
57 KFD_MEMPOOL_SYSTEM_WRITECOMBINE,
58 (struct kgd_mem **) mqd_mem_obj);
63 m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
64 addr = (*mqd_mem_obj)->gpu_addr;
66 memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
68 m->header = 0xC0310800;
69 m->compute_pipelinestat_enable = 1;
70 m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
71 m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
72 m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
73 m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
76 * Make sure to use the last queue state saved on mqd when the cp
77 * reassigns the queue, so when queue is switched on/off (e.g over
78 * subscription or quantum timeout) the context will be consistent
80 m->cp_hqd_persistent_state =
81 DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
83 m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
84 m->cp_mqd_base_addr_lo = lower_32_bits(addr);
85 m->cp_mqd_base_addr_hi = upper_32_bits(addr);
87 m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE | IB_ATC_EN;
88 /* Although WinKFD writes this, I suspect it should not be necessary */
89 m->cp_hqd_ib_control = IB_ATC_EN | DEFAULT_MIN_IB_AVAIL_SIZE;
91 m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
96 * Identifies the pipe relative priority when this queue is connected
97 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
98 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
99 * 0 = CS_LOW (typically below GFX)
100 * 1 = CS_MEDIUM (typically between HP3D and GFX
101 * 2 = CS_HIGH (typically above HP3D)
103 m->cp_hqd_pipe_priority = 1;
104 m->cp_hqd_queue_priority = 15;
107 if (gart_addr != NULL)
109 retval = mm->update_mqd(mm, m, q);
114 static void uninit_mqd(struct mqd_manager *mm, void *mqd,
115 struct kfd_mem_obj *mqd_mem_obj)
118 kfd2kgd->free_mem(mm->dev->kgd, (struct kgd_mem *) mqd_mem_obj);
121 static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
122 uint32_t queue_id, uint32_t __user *wptr)
124 return kfd2kgd->hqd_load(mm->dev->kgd, mqd, pipe_id, queue_id, wptr);
128 static int update_mqd(struct mqd_manager *mm, void *mqd,
129 struct queue_properties *q)
133 BUG_ON(!mm || !q || !mqd);
135 pr_debug("kfd: In func %s\n", __func__);
138 m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
139 DEFAULT_MIN_AVAIL_SIZE | PQ_ATC_EN;
142 * Calculating queue size which is log base 2 of actual queue size -1
143 * dwords and another -1 for ffs
145 m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
147 m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
148 m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
149 m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
150 m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
151 m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
152 DOORBELL_OFFSET(q->doorbell_off);
154 m->cp_hqd_vmid = q->vmid;
156 if (q->format == KFD_QUEUE_FORMAT_AQL) {
157 m->cp_hqd_iq_rptr = AQL_ENABLE;
158 m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
161 m->cp_hqd_active = 0;
162 q->is_active = false;
163 if (q->queue_size > 0 &&
164 q->queue_address != 0 &&
165 q->queue_percent > 0) {
166 m->cp_hqd_active = 1;
173 static int destroy_mqd(struct mqd_manager *mm, void *mqd,
174 enum kfd_preempt_type type,
175 unsigned int timeout, uint32_t pipe_id,
178 return kfd2kgd->hqd_destroy(mm->dev->kgd, type, timeout,
182 static bool is_occupied(struct mqd_manager *mm, void *mqd,
183 uint64_t queue_address, uint32_t pipe_id,
187 return kfd2kgd->hqd_is_occupies(mm->dev->kgd, queue_address,
193 * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
194 * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
195 * queues but with different initial values.
198 static int init_mqd_hiq(struct mqd_manager *mm, void **mqd,
199 struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
200 struct queue_properties *q)
206 BUG_ON(!mm || !q || !mqd || !mqd_mem_obj);
208 pr_debug("kfd: In func %s\n", __func__);
210 retval = kfd2kgd->allocate_mem(mm->dev->kgd,
211 sizeof(struct cik_mqd),
213 KFD_MEMPOOL_SYSTEM_WRITECOMBINE,
214 (struct kgd_mem **) mqd_mem_obj);
219 m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
220 addr = (*mqd_mem_obj)->gpu_addr;
222 memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
224 m->header = 0xC0310800;
225 m->compute_pipelinestat_enable = 1;
226 m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
227 m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
228 m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
229 m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
231 m->cp_hqd_persistent_state = DEFAULT_CP_HQD_PERSISTENT_STATE |
233 m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
234 QUANTUM_DURATION(10);
236 m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
237 m->cp_mqd_base_addr_lo = lower_32_bits(addr);
238 m->cp_mqd_base_addr_hi = upper_32_bits(addr);
240 m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
244 * Identifies the pipe relative priority when this queue is connected
245 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
246 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
247 * 0 = CS_LOW (typically below GFX)
248 * 1 = CS_MEDIUM (typically between HP3D and GFX
249 * 2 = CS_HIGH (typically above HP3D)
251 m->cp_hqd_pipe_priority = 1;
252 m->cp_hqd_queue_priority = 15;
257 retval = mm->update_mqd(mm, m, q);
262 static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
263 struct queue_properties *q)
267 BUG_ON(!mm || !q || !mqd);
269 pr_debug("kfd: In func %s\n", __func__);
272 m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
273 DEFAULT_MIN_AVAIL_SIZE |
278 * Calculating queue size which is log base 2 of actual queue
281 m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
283 m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
284 m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
285 m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
286 m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
287 m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
288 DOORBELL_OFFSET(q->doorbell_off);
290 m->cp_hqd_vmid = q->vmid;
292 m->cp_hqd_active = 0;
293 q->is_active = false;
294 if (q->queue_size > 0 &&
295 q->queue_address != 0 &&
296 q->queue_percent > 0) {
297 m->cp_hqd_active = 1;
304 struct mqd_manager *mqd_manager_init(enum KFD_MQD_TYPE type,
307 struct mqd_manager *mqd;
310 BUG_ON(type >= KFD_MQD_TYPE_MAX);
312 pr_debug("kfd: In func %s\n", __func__);
314 mqd = kzalloc(sizeof(struct mqd_manager), GFP_KERNEL);
321 case KFD_MQD_TYPE_CIK_CP:
322 case KFD_MQD_TYPE_CIK_COMPUTE:
323 mqd->init_mqd = init_mqd;
324 mqd->uninit_mqd = uninit_mqd;
325 mqd->load_mqd = load_mqd;
326 mqd->update_mqd = update_mqd;
327 mqd->destroy_mqd = destroy_mqd;
328 mqd->is_occupied = is_occupied;
330 case KFD_MQD_TYPE_CIK_HIQ:
331 mqd->init_mqd = init_mqd_hiq;
332 mqd->uninit_mqd = uninit_mqd;
333 mqd->load_mqd = load_mqd;
334 mqd->update_mqd = update_mqd_hiq;
335 mqd->destroy_mqd = destroy_mqd;
336 mqd->is_occupied = is_occupied;
346 /* SDMA queues should be implemented here when the cp will supports them */