1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  */
23 
24 #include <linux/printk.h>
25 #include <linux/slab.h>
26 #include <linux/mm_types.h>
27 
28 #include "kfd_priv.h"
29 #include "kfd_mqd_manager.h"
30 #include "cik_regs.h"
31 #include "cik_structs.h"
32 #include "oss/oss_2_4_sh_mask.h"
33 
34 static inline struct cik_mqd *get_mqd(void *mqd)
35 {
36 	return (struct cik_mqd *)mqd;
37 }
38 
39 static int init_mqd(struct mqd_manager *mm, void **mqd,
40 		struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
41 		struct queue_properties *q)
42 {
43 	uint64_t addr;
44 	struct cik_mqd *m;
45 	int retval;
46 
47 	retval = kfd_gtt_sa_allocate(mm->dev, sizeof(struct cik_mqd),
48 					mqd_mem_obj);
49 
50 	if (retval != 0)
51 		return -ENOMEM;
52 
53 	m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
54 	addr = (*mqd_mem_obj)->gpu_addr;
55 
56 	memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
57 
58 	m->header = 0xC0310800;
59 	m->compute_pipelinestat_enable = 1;
60 	m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
61 	m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
62 	m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
63 	m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
64 
65 	/*
66 	 * Make sure to use the last queue state saved on mqd when the cp
67 	 * reassigns the queue, so when queue is switched on/off (e.g over
68 	 * subscription or quantum timeout) the context will be consistent
69 	 */
70 	m->cp_hqd_persistent_state =
71 				DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
72 
73 	m->cp_mqd_control             = MQD_CONTROL_PRIV_STATE_EN;
74 	m->cp_mqd_base_addr_lo        = lower_32_bits(addr);
75 	m->cp_mqd_base_addr_hi        = upper_32_bits(addr);
76 
77 	m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE | IB_ATC_EN;
78 	/* Although WinKFD writes this, I suspect it should not be necessary */
79 	m->cp_hqd_ib_control = IB_ATC_EN | DEFAULT_MIN_IB_AVAIL_SIZE;
80 
81 	m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
82 				QUANTUM_DURATION(10);
83 
84 	/*
85 	 * Pipe Priority
86 	 * Identifies the pipe relative priority when this queue is connected
87 	 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
88 	 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
89 	 * 0 = CS_LOW (typically below GFX)
90 	 * 1 = CS_MEDIUM (typically between HP3D and GFX
91 	 * 2 = CS_HIGH (typically above HP3D)
92 	 */
93 	m->cp_hqd_pipe_priority = 1;
94 	m->cp_hqd_queue_priority = 15;
95 
96 	if (q->format == KFD_QUEUE_FORMAT_AQL)
97 		m->cp_hqd_iq_rptr = AQL_ENABLE;
98 
99 	*mqd = m;
100 	if (gart_addr)
101 		*gart_addr = addr;
102 	retval = mm->update_mqd(mm, m, q);
103 
104 	return retval;
105 }
106 
107 static int init_mqd_sdma(struct mqd_manager *mm, void **mqd,
108 			struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
109 			struct queue_properties *q)
110 {
111 	int retval;
112 	struct cik_sdma_rlc_registers *m;
113 
114 	retval = kfd_gtt_sa_allocate(mm->dev,
115 					sizeof(struct cik_sdma_rlc_registers),
116 					mqd_mem_obj);
117 
118 	if (retval != 0)
119 		return -ENOMEM;
120 
121 	m = (struct cik_sdma_rlc_registers *) (*mqd_mem_obj)->cpu_ptr;
122 
123 	memset(m, 0, sizeof(struct cik_sdma_rlc_registers));
124 
125 	*mqd = m;
126 	if (gart_addr)
127 		*gart_addr = (*mqd_mem_obj)->gpu_addr;
128 
129 	retval = mm->update_mqd(mm, m, q);
130 
131 	return retval;
132 }
133 
134 static void uninit_mqd(struct mqd_manager *mm, void *mqd,
135 			struct kfd_mem_obj *mqd_mem_obj)
136 {
137 	kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
138 }
139 
140 static void uninit_mqd_sdma(struct mqd_manager *mm, void *mqd,
141 				struct kfd_mem_obj *mqd_mem_obj)
142 {
143 	kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
144 }
145 
146 static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
147 		    uint32_t queue_id, struct queue_properties *p,
148 		    struct mm_struct *mms)
149 {
150 	/* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
151 	uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
152 	uint32_t wptr_mask = (uint32_t)((p->queue_size / sizeof(uint32_t)) - 1);
153 
154 	return mm->dev->kfd2kgd->hqd_load(mm->dev->kgd, mqd, pipe_id, queue_id,
155 					  (uint32_t __user *)p->write_ptr,
156 					  wptr_shift, wptr_mask, mms);
157 }
158 
159 static int load_mqd_sdma(struct mqd_manager *mm, void *mqd,
160 			 uint32_t pipe_id, uint32_t queue_id,
161 			 struct queue_properties *p, struct mm_struct *mms)
162 {
163 	return mm->dev->kfd2kgd->hqd_sdma_load(mm->dev->kgd, mqd);
164 }
165 
166 static int update_mqd(struct mqd_manager *mm, void *mqd,
167 			struct queue_properties *q)
168 {
169 	struct cik_mqd *m;
170 
171 	m = get_mqd(mqd);
172 	m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
173 				DEFAULT_MIN_AVAIL_SIZE | PQ_ATC_EN;
174 
175 	/*
176 	 * Calculating queue size which is log base 2 of actual queue size -1
177 	 * dwords and another -1 for ffs
178 	 */
179 	m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
180 								- 1 - 1;
181 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
182 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
183 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
184 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
185 	m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(q->doorbell_off);
186 
187 	m->cp_hqd_vmid = q->vmid;
188 
189 	if (q->format == KFD_QUEUE_FORMAT_AQL)
190 		m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
191 
192 	q->is_active = false;
193 	if (q->queue_size > 0 &&
194 			q->queue_address != 0 &&
195 			q->queue_percent > 0) {
196 		q->is_active = true;
197 	}
198 
199 	return 0;
200 }
201 
202 static int update_mqd_sdma(struct mqd_manager *mm, void *mqd,
203 				struct queue_properties *q)
204 {
205 	struct cik_sdma_rlc_registers *m;
206 
207 	m = get_sdma_mqd(mqd);
208 	m->sdma_rlc_rb_cntl = ffs(q->queue_size / sizeof(unsigned int)) <<
209 			SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
210 			q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT |
211 			1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
212 			6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT;
213 
214 	m->sdma_rlc_rb_base = lower_32_bits(q->queue_address >> 8);
215 	m->sdma_rlc_rb_base_hi = upper_32_bits(q->queue_address >> 8);
216 	m->sdma_rlc_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
217 	m->sdma_rlc_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
218 	m->sdma_rlc_doorbell = q->doorbell_off <<
219 			SDMA0_RLC0_DOORBELL__OFFSET__SHIFT |
220 			1 << SDMA0_RLC0_DOORBELL__ENABLE__SHIFT;
221 
222 	m->sdma_rlc_virtual_addr = q->sdma_vm_addr;
223 
224 	m->sdma_engine_id = q->sdma_engine_id;
225 	m->sdma_queue_id = q->sdma_queue_id;
226 
227 	q->is_active = false;
228 	if (q->queue_size > 0 &&
229 			q->queue_address != 0 &&
230 			q->queue_percent > 0) {
231 		m->sdma_rlc_rb_cntl |=
232 				1 << SDMA0_RLC0_RB_CNTL__RB_ENABLE__SHIFT;
233 
234 		q->is_active = true;
235 	}
236 
237 	return 0;
238 }
239 
240 static int destroy_mqd(struct mqd_manager *mm, void *mqd,
241 			enum kfd_preempt_type type,
242 			unsigned int timeout, uint32_t pipe_id,
243 			uint32_t queue_id)
244 {
245 	return mm->dev->kfd2kgd->hqd_destroy(mm->dev->kgd, mqd, type, timeout,
246 					pipe_id, queue_id);
247 }
248 
249 /*
250  * preempt type here is ignored because there is only one way
251  * to preempt sdma queue
252  */
253 static int destroy_mqd_sdma(struct mqd_manager *mm, void *mqd,
254 				enum kfd_preempt_type type,
255 				unsigned int timeout, uint32_t pipe_id,
256 				uint32_t queue_id)
257 {
258 	return mm->dev->kfd2kgd->hqd_sdma_destroy(mm->dev->kgd, mqd, timeout);
259 }
260 
261 static bool is_occupied(struct mqd_manager *mm, void *mqd,
262 			uint64_t queue_address,	uint32_t pipe_id,
263 			uint32_t queue_id)
264 {
265 
266 	return mm->dev->kfd2kgd->hqd_is_occupied(mm->dev->kgd, queue_address,
267 					pipe_id, queue_id);
268 
269 }
270 
271 static bool is_occupied_sdma(struct mqd_manager *mm, void *mqd,
272 			uint64_t queue_address,	uint32_t pipe_id,
273 			uint32_t queue_id)
274 {
275 	return mm->dev->kfd2kgd->hqd_sdma_is_occupied(mm->dev->kgd, mqd);
276 }
277 
278 /*
279  * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
280  * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
281  * queues but with different initial values.
282  */
283 
284 static int init_mqd_hiq(struct mqd_manager *mm, void **mqd,
285 		struct kfd_mem_obj **mqd_mem_obj, uint64_t *gart_addr,
286 		struct queue_properties *q)
287 {
288 	uint64_t addr;
289 	struct cik_mqd *m;
290 	int retval;
291 
292 	retval = kfd_gtt_sa_allocate(mm->dev, sizeof(struct cik_mqd),
293 					mqd_mem_obj);
294 
295 	if (retval != 0)
296 		return -ENOMEM;
297 
298 	m = (struct cik_mqd *) (*mqd_mem_obj)->cpu_ptr;
299 	addr = (*mqd_mem_obj)->gpu_addr;
300 
301 	memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
302 
303 	m->header = 0xC0310800;
304 	m->compute_pipelinestat_enable = 1;
305 	m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
306 	m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
307 	m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
308 	m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
309 
310 	m->cp_hqd_persistent_state = DEFAULT_CP_HQD_PERSISTENT_STATE |
311 					PRELOAD_REQ;
312 	m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
313 				QUANTUM_DURATION(10);
314 
315 	m->cp_mqd_control             = MQD_CONTROL_PRIV_STATE_EN;
316 	m->cp_mqd_base_addr_lo        = lower_32_bits(addr);
317 	m->cp_mqd_base_addr_hi        = upper_32_bits(addr);
318 
319 	m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
320 
321 	/*
322 	 * Pipe Priority
323 	 * Identifies the pipe relative priority when this queue is connected
324 	 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
325 	 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
326 	 * 0 = CS_LOW (typically below GFX)
327 	 * 1 = CS_MEDIUM (typically between HP3D and GFX
328 	 * 2 = CS_HIGH (typically above HP3D)
329 	 */
330 	m->cp_hqd_pipe_priority = 1;
331 	m->cp_hqd_queue_priority = 15;
332 
333 	*mqd = m;
334 	if (gart_addr)
335 		*gart_addr = addr;
336 	retval = mm->update_mqd(mm, m, q);
337 
338 	return retval;
339 }
340 
341 static int update_mqd_hiq(struct mqd_manager *mm, void *mqd,
342 				struct queue_properties *q)
343 {
344 	struct cik_mqd *m;
345 
346 	m = get_mqd(mqd);
347 	m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
348 				DEFAULT_MIN_AVAIL_SIZE |
349 				PRIV_STATE |
350 				KMD_QUEUE;
351 
352 	/*
353 	 * Calculating queue size which is log base 2 of actual queue
354 	 * size -1 dwords
355 	 */
356 	m->cp_hqd_pq_control |= ffs(q->queue_size / sizeof(unsigned int))
357 								- 1 - 1;
358 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
359 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
360 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
361 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
362 	m->cp_hqd_pq_doorbell_control = DOORBELL_EN |
363 					DOORBELL_OFFSET(q->doorbell_off);
364 
365 	m->cp_hqd_vmid = q->vmid;
366 
367 	m->cp_hqd_active = 0;
368 	q->is_active = false;
369 	if (q->queue_size > 0 &&
370 			q->queue_address != 0 &&
371 			q->queue_percent > 0) {
372 		m->cp_hqd_active = 1;
373 		q->is_active = true;
374 	}
375 
376 	return 0;
377 }
378 
379 struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
380 {
381 	struct cik_sdma_rlc_registers *m;
382 
383 	m = (struct cik_sdma_rlc_registers *)mqd;
384 
385 	return m;
386 }
387 
388 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
389 		struct kfd_dev *dev)
390 {
391 	struct mqd_manager *mqd;
392 
393 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
394 		return NULL;
395 
396 	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
397 	if (!mqd)
398 		return NULL;
399 
400 	mqd->dev = dev;
401 
402 	switch (type) {
403 	case KFD_MQD_TYPE_CP:
404 	case KFD_MQD_TYPE_COMPUTE:
405 		mqd->init_mqd = init_mqd;
406 		mqd->uninit_mqd = uninit_mqd;
407 		mqd->load_mqd = load_mqd;
408 		mqd->update_mqd = update_mqd;
409 		mqd->destroy_mqd = destroy_mqd;
410 		mqd->is_occupied = is_occupied;
411 		break;
412 	case KFD_MQD_TYPE_HIQ:
413 		mqd->init_mqd = init_mqd_hiq;
414 		mqd->uninit_mqd = uninit_mqd;
415 		mqd->load_mqd = load_mqd;
416 		mqd->update_mqd = update_mqd_hiq;
417 		mqd->destroy_mqd = destroy_mqd;
418 		mqd->is_occupied = is_occupied;
419 		break;
420 	case KFD_MQD_TYPE_SDMA:
421 		mqd->init_mqd = init_mqd_sdma;
422 		mqd->uninit_mqd = uninit_mqd_sdma;
423 		mqd->load_mqd = load_mqd_sdma;
424 		mqd->update_mqd = update_mqd_sdma;
425 		mqd->destroy_mqd = destroy_mqd_sdma;
426 		mqd->is_occupied = is_occupied_sdma;
427 		break;
428 	default:
429 		kfree(mqd);
430 		return NULL;
431 	}
432 
433 	return mqd;
434 }
435 
436