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