1d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
24b8f589bSBen Goz /*
3d87f36a0SRajneesh Bhardwaj * Copyright 2014-2022 Advanced Micro Devices, Inc.
44b8f589bSBen Goz *
54b8f589bSBen Goz * Permission is hereby granted, free of charge, to any person obtaining a
64b8f589bSBen Goz * copy of this software and associated documentation files (the "Software"),
74b8f589bSBen Goz * to deal in the Software without restriction, including without limitation
84b8f589bSBen Goz * the rights to use, copy, modify, merge, publish, distribute, sublicense,
94b8f589bSBen Goz * and/or sell copies of the Software, and to permit persons to whom the
104b8f589bSBen Goz * Software is furnished to do so, subject to the following conditions:
114b8f589bSBen Goz *
124b8f589bSBen Goz * The above copyright notice and this permission notice shall be included in
134b8f589bSBen Goz * all copies or substantial portions of the Software.
144b8f589bSBen Goz *
154b8f589bSBen Goz * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164b8f589bSBen Goz * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174b8f589bSBen Goz * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
184b8f589bSBen Goz * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
194b8f589bSBen Goz * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
204b8f589bSBen Goz * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
214b8f589bSBen Goz * OTHER DEALINGS IN THE SOFTWARE.
224b8f589bSBen Goz *
234b8f589bSBen Goz */
244b8f589bSBen Goz
254b8f589bSBen Goz #include <linux/printk.h>
264b8f589bSBen Goz #include <linux/slab.h>
27589ee628SIngo Molnar #include <linux/mm_types.h>
28589ee628SIngo Molnar
294b8f589bSBen Goz #include "kfd_priv.h"
304b8f589bSBen Goz #include "kfd_mqd_manager.h"
314b8f589bSBen Goz #include "cik_regs.h"
324b8f589bSBen Goz #include "cik_structs.h"
333d30b28bSOded Gabbay #include "oss/oss_2_4_sh_mask.h"
344b8f589bSBen Goz
get_mqd(void * mqd)354b8f589bSBen Goz static inline struct cik_mqd *get_mqd(void *mqd)
364b8f589bSBen Goz {
374b8f589bSBen Goz return (struct cik_mqd *)mqd;
384b8f589bSBen Goz }
394b8f589bSBen Goz
get_sdma_mqd(void * mqd)4097b9ad12SFelix Kuehling static inline struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
4197b9ad12SFelix Kuehling {
4297b9ad12SFelix Kuehling return (struct cik_sdma_rlc_registers *)mqd;
4397b9ad12SFelix Kuehling }
4497b9ad12SFelix Kuehling
update_cu_mask(struct mqd_manager * mm,void * mqd,struct mqd_update_info * minfo)4539e7f331SFelix Kuehling static void update_cu_mask(struct mqd_manager *mm, void *mqd,
467c695a2cSLang Yu struct mqd_update_info *minfo)
4739e7f331SFelix Kuehling {
4839e7f331SFelix Kuehling struct cik_mqd *m;
4939e7f331SFelix Kuehling uint32_t se_mask[4] = {0}; /* 4 is the max # of SEs */
5039e7f331SFelix Kuehling
5169a8c3aeSJonathan Kim if (!minfo || !minfo->cu_mask.ptr)
5239e7f331SFelix Kuehling return;
5339e7f331SFelix Kuehling
5439e7f331SFelix Kuehling mqd_symmetrically_map_cu_mask(mm,
55fc6efed2SMukul Joshi minfo->cu_mask.ptr, minfo->cu_mask.count, se_mask, 0);
5639e7f331SFelix Kuehling
5739e7f331SFelix Kuehling m = get_mqd(mqd);
5839e7f331SFelix Kuehling m->compute_static_thread_mgmt_se0 = se_mask[0];
5939e7f331SFelix Kuehling m->compute_static_thread_mgmt_se1 = se_mask[1];
6039e7f331SFelix Kuehling m->compute_static_thread_mgmt_se2 = se_mask[2];
6139e7f331SFelix Kuehling m->compute_static_thread_mgmt_se3 = se_mask[3];
6239e7f331SFelix Kuehling
6339e7f331SFelix Kuehling pr_debug("Update cu mask to %#x %#x %#x %#x\n",
6439e7f331SFelix Kuehling m->compute_static_thread_mgmt_se0,
6539e7f331SFelix Kuehling m->compute_static_thread_mgmt_se1,
6639e7f331SFelix Kuehling m->compute_static_thread_mgmt_se2,
6739e7f331SFelix Kuehling m->compute_static_thread_mgmt_se3);
6839e7f331SFelix Kuehling }
6939e7f331SFelix Kuehling
set_priority(struct cik_mqd * m,struct queue_properties * q)700ccbc7cdSOak Zeng static void set_priority(struct cik_mqd *m, struct queue_properties *q)
710ccbc7cdSOak Zeng {
720ccbc7cdSOak Zeng m->cp_hqd_pipe_priority = pipe_priority_map[q->priority];
730ccbc7cdSOak Zeng m->cp_hqd_queue_priority = q->priority;
740ccbc7cdSOak Zeng }
750ccbc7cdSOak Zeng
allocate_mqd(struct kfd_node * kfd,struct queue_properties * q)768dc1db31SMukul Joshi static struct kfd_mem_obj *allocate_mqd(struct kfd_node *kfd,
77d1f8f0d1SOak Zeng struct queue_properties *q)
78d1f8f0d1SOak Zeng {
79d1f8f0d1SOak Zeng struct kfd_mem_obj *mqd_mem_obj;
80d1f8f0d1SOak Zeng
81d1f8f0d1SOak Zeng if (kfd_gtt_sa_allocate(kfd, sizeof(struct cik_mqd),
82d1f8f0d1SOak Zeng &mqd_mem_obj))
83d1f8f0d1SOak Zeng return NULL;
84d1f8f0d1SOak Zeng
85d1f8f0d1SOak Zeng return mqd_mem_obj;
86d1f8f0d1SOak Zeng }
87d1f8f0d1SOak Zeng
init_mqd(struct mqd_manager * mm,void ** mqd,struct kfd_mem_obj * mqd_mem_obj,uint64_t * gart_addr,struct queue_properties * q)888636e53cSOak Zeng static void init_mqd(struct mqd_manager *mm, void **mqd,
898636e53cSOak Zeng struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
904b8f589bSBen Goz struct queue_properties *q)
914b8f589bSBen Goz {
924b8f589bSBen Goz uint64_t addr;
934b8f589bSBen Goz struct cik_mqd *m;
944b8f589bSBen Goz
958636e53cSOak Zeng m = (struct cik_mqd *) mqd_mem_obj->cpu_ptr;
968636e53cSOak Zeng addr = mqd_mem_obj->gpu_addr;
974b8f589bSBen Goz
984b8f589bSBen Goz memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
994b8f589bSBen Goz
1004b8f589bSBen Goz m->header = 0xC0310800;
1014b8f589bSBen Goz m->compute_pipelinestat_enable = 1;
1024b8f589bSBen Goz m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
1034b8f589bSBen Goz m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
1044b8f589bSBen Goz m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
1054b8f589bSBen Goz m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
1064b8f589bSBen Goz
1074b8f589bSBen Goz /*
1084b8f589bSBen Goz * Make sure to use the last queue state saved on mqd when the cp
1094b8f589bSBen Goz * reassigns the queue, so when queue is switched on/off (e.g over
1104b8f589bSBen Goz * subscription or quantum timeout) the context will be consistent
1114b8f589bSBen Goz */
1124b8f589bSBen Goz m->cp_hqd_persistent_state =
1134b8f589bSBen Goz DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
1144b8f589bSBen Goz
1154b8f589bSBen Goz m->cp_mqd_control = MQD_CONTROL_PRIV_STATE_EN;
1164b8f589bSBen Goz m->cp_mqd_base_addr_lo = lower_32_bits(addr);
1174b8f589bSBen Goz m->cp_mqd_base_addr_hi = upper_32_bits(addr);
1184b8f589bSBen Goz
1194b8f589bSBen Goz m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
1204b8f589bSBen Goz QUANTUM_DURATION(10);
1214b8f589bSBen Goz
1224b8f589bSBen Goz /*
1234b8f589bSBen Goz * Pipe Priority
1244b8f589bSBen Goz * Identifies the pipe relative priority when this queue is connected
1254b8f589bSBen Goz * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
1264b8f589bSBen Goz * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
1274b8f589bSBen Goz * 0 = CS_LOW (typically below GFX)
1284b8f589bSBen Goz * 1 = CS_MEDIUM (typically between HP3D and GFX
1294b8f589bSBen Goz * 2 = CS_HIGH (typically above HP3D)
1304b8f589bSBen Goz */
1310ccbc7cdSOak Zeng set_priority(m, q);
1324b8f589bSBen Goz
133d752f95eSJay Cornwall if (q->format == KFD_QUEUE_FORMAT_AQL)
134d752f95eSJay Cornwall m->cp_hqd_iq_rptr = AQL_ENABLE;
135d752f95eSJay Cornwall
1364b8f589bSBen Goz *mqd = m;
1374eacc26bSKent Russell if (gart_addr)
1384b8f589bSBen Goz *gart_addr = addr;
139c6e559ebSLang Yu mm->update_mqd(mm, m, q, NULL);
1404b8f589bSBen Goz }
1414b8f589bSBen Goz
init_mqd_sdma(struct mqd_manager * mm,void ** mqd,struct kfd_mem_obj * mqd_mem_obj,uint64_t * gart_addr,struct queue_properties * q)1428636e53cSOak Zeng static void init_mqd_sdma(struct mqd_manager *mm, void **mqd,
1438636e53cSOak Zeng struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
1444b8f589bSBen Goz struct queue_properties *q)
1454b8f589bSBen Goz {
1464b8f589bSBen Goz struct cik_sdma_rlc_registers *m;
1474b8f589bSBen Goz
1488636e53cSOak Zeng m = (struct cik_sdma_rlc_registers *) mqd_mem_obj->cpu_ptr;
1494b8f589bSBen Goz
1504b8f589bSBen Goz memset(m, 0, sizeof(struct cik_sdma_rlc_registers));
1514b8f589bSBen Goz
1524b8f589bSBen Goz *mqd = m;
1534eacc26bSKent Russell if (gart_addr)
1548636e53cSOak Zeng *gart_addr = mqd_mem_obj->gpu_addr;
1554b8f589bSBen Goz
156c6e559ebSLang Yu mm->update_mqd(mm, m, q, NULL);
1574b8f589bSBen Goz }
1584b8f589bSBen Goz
load_mqd(struct mqd_manager * mm,void * mqd,uint32_t pipe_id,uint32_t queue_id,struct queue_properties * p,struct mm_struct * mms)1594b8f589bSBen Goz static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
16070539bd7SFelix Kuehling uint32_t queue_id, struct queue_properties *p,
16170539bd7SFelix Kuehling struct mm_struct *mms)
1624b8f589bSBen Goz {
16370539bd7SFelix Kuehling /* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
16470539bd7SFelix Kuehling uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
1656d566930SFelix Kuehling uint32_t wptr_mask = (uint32_t)((p->queue_size / 4) - 1);
16670539bd7SFelix Kuehling
167420185fdSGraham Sider return mm->dev->kfd2kgd->hqd_load(mm->dev->adev, mqd, pipe_id, queue_id,
16870539bd7SFelix Kuehling (uint32_t __user *)p->write_ptr,
169e2069a7bSMukul Joshi wptr_shift, wptr_mask, mms, 0);
1704b8f589bSBen Goz }
1714b8f589bSBen Goz
__update_mqd(struct mqd_manager * mm,void * mqd,struct queue_properties * q,struct mqd_update_info * minfo,unsigned int atc_bit)1728636e53cSOak Zeng static void __update_mqd(struct mqd_manager *mm, void *mqd,
173c6e559ebSLang Yu struct queue_properties *q, struct mqd_update_info *minfo,
174c6e559ebSLang Yu unsigned int atc_bit)
1754b8f589bSBen Goz {
1764b8f589bSBen Goz struct cik_mqd *m;
1774b8f589bSBen Goz
1784b8f589bSBen Goz m = get_mqd(mqd);
1794b8f589bSBen Goz m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
180ee04955aSFelix Kuehling DEFAULT_MIN_AVAIL_SIZE;
181ee04955aSFelix Kuehling m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
182ee04955aSFelix Kuehling if (atc_bit) {
183ee04955aSFelix Kuehling m->cp_hqd_pq_control |= PQ_ATC_EN;
184ee04955aSFelix Kuehling m->cp_hqd_ib_control |= IB_ATC_EN;
185ee04955aSFelix Kuehling }
1864b8f589bSBen Goz
1874b8f589bSBen Goz /*
1884b8f589bSBen Goz * Calculating queue size which is log base 2 of actual queue size -1
1894b8f589bSBen Goz * dwords and another -1 for ffs
1904b8f589bSBen Goz */
191115c8c41SFelix Kuehling m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1;
1924b8f589bSBen Goz m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
1934b8f589bSBen Goz m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
1944b8f589bSBen Goz m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
1954b8f589bSBen Goz m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
19670539bd7SFelix Kuehling m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(q->doorbell_off);
1974b8f589bSBen Goz
1984b8f589bSBen Goz m->cp_hqd_vmid = q->vmid;
1994b8f589bSBen Goz
2008eabaf54SKent Russell if (q->format == KFD_QUEUE_FORMAT_AQL)
2014b8f589bSBen Goz m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
2024b8f589bSBen Goz
2037c695a2cSLang Yu update_cu_mask(mm, mqd, minfo);
2040ccbc7cdSOak Zeng set_priority(m, q);
20539e7f331SFelix Kuehling
206bb2d2128SFelix Kuehling q->is_active = QUEUE_IS_ACTIVE(*q);
2074b8f589bSBen Goz }
2084b8f589bSBen Goz
check_preemption_failed(struct mqd_manager * mm,void * mqd)209*0991a4c1SMukul Joshi static bool check_preemption_failed(struct mqd_manager *mm, void *mqd)
21051a0f459SOak Zeng {
21151a0f459SOak Zeng struct cik_mqd *m = (struct cik_mqd *)mqd;
21251a0f459SOak Zeng
213*0991a4c1SMukul Joshi return kfd_check_hiq_mqd_doorbell_id(mm->dev, m->queue_doorbell_id0, 0);
21451a0f459SOak Zeng }
21551a0f459SOak Zeng
update_mqd(struct mqd_manager * mm,void * mqd,struct queue_properties * q,struct mqd_update_info * minfo)216c99a2e7aSAlex Deucher static void update_mqd(struct mqd_manager *mm, void *mqd,
217c6e559ebSLang Yu struct queue_properties *q,
218c6e559ebSLang Yu struct mqd_update_info *minfo)
219ee04955aSFelix Kuehling {
220c6e559ebSLang Yu __update_mqd(mm, mqd, q, minfo, 0);
221ee04955aSFelix Kuehling }
222ee04955aSFelix Kuehling
update_mqd_sdma(struct mqd_manager * mm,void * mqd,struct queue_properties * q,struct mqd_update_info * minfo)2238636e53cSOak Zeng static void update_mqd_sdma(struct mqd_manager *mm, void *mqd,
224c6e559ebSLang Yu struct queue_properties *q,
225c6e559ebSLang Yu struct mqd_update_info *minfo)
2264b8f589bSBen Goz {
2274b8f589bSBen Goz struct cik_sdma_rlc_registers *m;
2284b8f589bSBen Goz
2294b8f589bSBen Goz m = get_sdma_mqd(mqd);
230115c8c41SFelix Kuehling m->sdma_rlc_rb_cntl = order_base_2(q->queue_size / 4)
231d12fb13fSshaoyunl << SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
2323d30b28bSOded Gabbay q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT |
2333d30b28bSOded Gabbay 1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
2343d30b28bSOded Gabbay 6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT;
2354b8f589bSBen Goz
2364b8f589bSBen Goz m->sdma_rlc_rb_base = lower_32_bits(q->queue_address >> 8);
2374b8f589bSBen Goz m->sdma_rlc_rb_base_hi = upper_32_bits(q->queue_address >> 8);
2384b8f589bSBen Goz m->sdma_rlc_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
2394b8f589bSBen Goz m->sdma_rlc_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
240bba9662dSJay Cornwall m->sdma_rlc_doorbell =
241bba9662dSJay Cornwall q->doorbell_off << SDMA0_RLC0_DOORBELL__OFFSET__SHIFT;
2423d30b28bSOded Gabbay
2434b8f589bSBen Goz m->sdma_rlc_virtual_addr = q->sdma_vm_addr;
2444b8f589bSBen Goz
2454b8f589bSBen Goz m->sdma_engine_id = q->sdma_engine_id;
2464b8f589bSBen Goz m->sdma_queue_id = q->sdma_queue_id;
2474b8f589bSBen Goz
248bb2d2128SFelix Kuehling q->is_active = QUEUE_IS_ACTIVE(*q);
2494b8f589bSBen Goz }
2504b8f589bSBen Goz
checkpoint_mqd(struct mqd_manager * mm,void * mqd,void * mqd_dst,void * ctl_stack_dst)2513a9822d7SDavid Yat Sin static void checkpoint_mqd(struct mqd_manager *mm, void *mqd, void *mqd_dst, void *ctl_stack_dst)
25242c6c482SDavid Yat Sin {
25342c6c482SDavid Yat Sin struct cik_mqd *m;
25442c6c482SDavid Yat Sin
25542c6c482SDavid Yat Sin m = get_mqd(mqd);
25642c6c482SDavid Yat Sin
25742c6c482SDavid Yat Sin memcpy(mqd_dst, m, sizeof(struct cik_mqd));
25842c6c482SDavid Yat Sin }
25942c6c482SDavid Yat Sin
restore_mqd(struct mqd_manager * mm,void ** mqd,struct kfd_mem_obj * mqd_mem_obj,uint64_t * gart_addr,struct queue_properties * qp,const void * mqd_src,const void * ctl_stack_src,const u32 ctl_stack_size)26042c6c482SDavid Yat Sin static void restore_mqd(struct mqd_manager *mm, void **mqd,
26142c6c482SDavid Yat Sin struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
26242c6c482SDavid Yat Sin struct queue_properties *qp,
2633a9822d7SDavid Yat Sin const void *mqd_src,
2643a9822d7SDavid Yat Sin const void *ctl_stack_src, const u32 ctl_stack_size)
26542c6c482SDavid Yat Sin {
26642c6c482SDavid Yat Sin uint64_t addr;
26742c6c482SDavid Yat Sin struct cik_mqd *m;
26842c6c482SDavid Yat Sin
26942c6c482SDavid Yat Sin m = (struct cik_mqd *) mqd_mem_obj->cpu_ptr;
27042c6c482SDavid Yat Sin addr = mqd_mem_obj->gpu_addr;
27142c6c482SDavid Yat Sin
27242c6c482SDavid Yat Sin memcpy(m, mqd_src, sizeof(*m));
27342c6c482SDavid Yat Sin
27442c6c482SDavid Yat Sin *mqd = m;
27542c6c482SDavid Yat Sin if (gart_addr)
27642c6c482SDavid Yat Sin *gart_addr = addr;
27742c6c482SDavid Yat Sin
27842c6c482SDavid Yat Sin m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(qp->doorbell_off);
27942c6c482SDavid Yat Sin
28042c6c482SDavid Yat Sin pr_debug("cp_hqd_pq_doorbell_control 0x%x\n",
28142c6c482SDavid Yat Sin m->cp_hqd_pq_doorbell_control);
28242c6c482SDavid Yat Sin
28342c6c482SDavid Yat Sin qp->is_active = 0;
28442c6c482SDavid Yat Sin }
28542c6c482SDavid Yat Sin
checkpoint_mqd_sdma(struct mqd_manager * mm,void * mqd,void * mqd_dst,void * ctl_stack_dst)2863a9822d7SDavid Yat Sin static void checkpoint_mqd_sdma(struct mqd_manager *mm,
2873a9822d7SDavid Yat Sin void *mqd,
2883a9822d7SDavid Yat Sin void *mqd_dst,
2893a9822d7SDavid Yat Sin void *ctl_stack_dst)
29042c6c482SDavid Yat Sin {
29142c6c482SDavid Yat Sin struct cik_sdma_rlc_registers *m;
29242c6c482SDavid Yat Sin
29342c6c482SDavid Yat Sin m = get_sdma_mqd(mqd);
29442c6c482SDavid Yat Sin
29542c6c482SDavid Yat Sin memcpy(mqd_dst, m, sizeof(struct cik_sdma_rlc_registers));
29642c6c482SDavid Yat Sin }
29742c6c482SDavid Yat Sin
restore_mqd_sdma(struct mqd_manager * mm,void ** mqd,struct kfd_mem_obj * mqd_mem_obj,uint64_t * gart_addr,struct queue_properties * qp,const void * mqd_src,const void * ctl_stack_src,const u32 ctl_stack_size)29842c6c482SDavid Yat Sin static void restore_mqd_sdma(struct mqd_manager *mm, void **mqd,
29942c6c482SDavid Yat Sin struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
30042c6c482SDavid Yat Sin struct queue_properties *qp,
3013a9822d7SDavid Yat Sin const void *mqd_src,
3023a9822d7SDavid Yat Sin const void *ctl_stack_src, const u32 ctl_stack_size)
30342c6c482SDavid Yat Sin {
30442c6c482SDavid Yat Sin uint64_t addr;
30542c6c482SDavid Yat Sin struct cik_sdma_rlc_registers *m;
30642c6c482SDavid Yat Sin
30742c6c482SDavid Yat Sin m = (struct cik_sdma_rlc_registers *) mqd_mem_obj->cpu_ptr;
30842c6c482SDavid Yat Sin addr = mqd_mem_obj->gpu_addr;
30942c6c482SDavid Yat Sin
31042c6c482SDavid Yat Sin memcpy(m, mqd_src, sizeof(*m));
31142c6c482SDavid Yat Sin
31242c6c482SDavid Yat Sin m->sdma_rlc_doorbell =
31342c6c482SDavid Yat Sin qp->doorbell_off << SDMA0_RLC0_DOORBELL__OFFSET__SHIFT;
31442c6c482SDavid Yat Sin
31542c6c482SDavid Yat Sin *mqd = m;
31642c6c482SDavid Yat Sin if (gart_addr)
31742c6c482SDavid Yat Sin *gart_addr = addr;
31842c6c482SDavid Yat Sin
31942c6c482SDavid Yat Sin qp->is_active = 0;
32042c6c482SDavid Yat Sin }
32142c6c482SDavid Yat Sin
3224b8f589bSBen Goz /*
3234b8f589bSBen Goz * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
3244b8f589bSBen Goz * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
3254b8f589bSBen Goz * queues but with different initial values.
3264b8f589bSBen Goz */
3274b8f589bSBen Goz
init_mqd_hiq(struct mqd_manager * mm,void ** mqd,struct kfd_mem_obj * mqd_mem_obj,uint64_t * gart_addr,struct queue_properties * q)3288636e53cSOak Zeng static void init_mqd_hiq(struct mqd_manager *mm, void **mqd,
3298636e53cSOak Zeng struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
3304b8f589bSBen Goz struct queue_properties *q)
3314b8f589bSBen Goz {
3328636e53cSOak Zeng init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q);
3334b8f589bSBen Goz }
3344b8f589bSBen Goz
update_mqd_hiq(struct mqd_manager * mm,void * mqd,struct queue_properties * q,struct mqd_update_info * minfo)3358636e53cSOak Zeng static void update_mqd_hiq(struct mqd_manager *mm, void *mqd,
336c6e559ebSLang Yu struct queue_properties *q,
337c6e559ebSLang Yu struct mqd_update_info *minfo)
3384b8f589bSBen Goz {
3394b8f589bSBen Goz struct cik_mqd *m;
3404b8f589bSBen Goz
3414b8f589bSBen Goz m = get_mqd(mqd);
3424b8f589bSBen Goz m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
3434b8f589bSBen Goz DEFAULT_MIN_AVAIL_SIZE |
3444b8f589bSBen Goz PRIV_STATE |
3454b8f589bSBen Goz KMD_QUEUE;
3464b8f589bSBen Goz
3474b8f589bSBen Goz /*
3484b8f589bSBen Goz * Calculating queue size which is log base 2 of actual queue
3494b8f589bSBen Goz * size -1 dwords
3504b8f589bSBen Goz */
351115c8c41SFelix Kuehling m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1;
3524b8f589bSBen Goz m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
3534b8f589bSBen Goz m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
3544b8f589bSBen Goz m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
3554b8f589bSBen Goz m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
356bba9662dSJay Cornwall m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(q->doorbell_off);
3574b8f589bSBen Goz
3584b8f589bSBen Goz m->cp_hqd_vmid = q->vmid;
3594b8f589bSBen Goz
360bb2d2128SFelix Kuehling q->is_active = QUEUE_IS_ACTIVE(*q);
3614b8f589bSBen Goz
3620ccbc7cdSOak Zeng set_priority(m, q);
3634b8f589bSBen Goz }
3644b8f589bSBen Goz
365851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
366851a645eSFelix Kuehling
debugfs_show_mqd(struct seq_file * m,void * data)367851a645eSFelix Kuehling static int debugfs_show_mqd(struct seq_file *m, void *data)
368851a645eSFelix Kuehling {
369851a645eSFelix Kuehling seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4,
370851a645eSFelix Kuehling data, sizeof(struct cik_mqd), false);
371851a645eSFelix Kuehling return 0;
372851a645eSFelix Kuehling }
373851a645eSFelix Kuehling
debugfs_show_mqd_sdma(struct seq_file * m,void * data)374851a645eSFelix Kuehling static int debugfs_show_mqd_sdma(struct seq_file *m, void *data)
375851a645eSFelix Kuehling {
376851a645eSFelix Kuehling seq_hex_dump(m, " ", DUMP_PREFIX_OFFSET, 32, 4,
377851a645eSFelix Kuehling data, sizeof(struct cik_sdma_rlc_registers), false);
378851a645eSFelix Kuehling return 0;
379851a645eSFelix Kuehling }
380851a645eSFelix Kuehling
381851a645eSFelix Kuehling #endif
382851a645eSFelix Kuehling
mqd_manager_init_cik(enum KFD_MQD_TYPE type,struct kfd_node * dev)3834b8f589bSBen Goz struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
3848dc1db31SMukul Joshi struct kfd_node *dev)
3854b8f589bSBen Goz {
3864b8f589bSBen Goz struct mqd_manager *mqd;
3874b8f589bSBen Goz
38832fa8219SFelix Kuehling if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
38932fa8219SFelix Kuehling return NULL;
3904b8f589bSBen Goz
3911cd106ecSFelix Kuehling mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
3924b8f589bSBen Goz if (!mqd)
3934b8f589bSBen Goz return NULL;
3944b8f589bSBen Goz
3954b8f589bSBen Goz mqd->dev = dev;
3964b8f589bSBen Goz
3974b8f589bSBen Goz switch (type) {
3984b8f589bSBen Goz case KFD_MQD_TYPE_CP:
3998636e53cSOak Zeng mqd->allocate_mqd = allocate_mqd;
4004b8f589bSBen Goz mqd->init_mqd = init_mqd;
401a439b890SMukul Joshi mqd->free_mqd = kfd_free_mqd_cp;
4024b8f589bSBen Goz mqd->load_mqd = load_mqd;
4034b8f589bSBen Goz mqd->update_mqd = update_mqd;
404a439b890SMukul Joshi mqd->destroy_mqd = kfd_destroy_mqd_cp;
405a439b890SMukul Joshi mqd->is_occupied = kfd_is_occupied_cp;
40642c6c482SDavid Yat Sin mqd->checkpoint_mqd = checkpoint_mqd;
40742c6c482SDavid Yat Sin mqd->restore_mqd = restore_mqd;
4086c6cde55SOak Zeng mqd->mqd_size = sizeof(struct cik_mqd);
409851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
410851a645eSFelix Kuehling mqd->debugfs_show_mqd = debugfs_show_mqd;
411851a645eSFelix Kuehling #endif
4124b8f589bSBen Goz break;
4134b8f589bSBen Goz case KFD_MQD_TYPE_HIQ:
4148636e53cSOak Zeng mqd->allocate_mqd = allocate_hiq_mqd;
4154b8f589bSBen Goz mqd->init_mqd = init_mqd_hiq;
4168636e53cSOak Zeng mqd->free_mqd = free_mqd_hiq_sdma;
4174b8f589bSBen Goz mqd->load_mqd = load_mqd;
4184b8f589bSBen Goz mqd->update_mqd = update_mqd_hiq;
419a439b890SMukul Joshi mqd->destroy_mqd = kfd_destroy_mqd_cp;
420a439b890SMukul Joshi mqd->is_occupied = kfd_is_occupied_cp;
4216c6cde55SOak Zeng mqd->mqd_size = sizeof(struct cik_mqd);
4222f77b9a2SMukul Joshi mqd->mqd_stride = kfd_mqd_stride;
423851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
424851a645eSFelix Kuehling mqd->debugfs_show_mqd = debugfs_show_mqd;
425851a645eSFelix Kuehling #endif
42626d97182SMukul Joshi mqd->check_preemption_failed = check_preemption_failed;
4274b8f589bSBen Goz break;
42859f650a0SOak Zeng case KFD_MQD_TYPE_DIQ:
4297633c5e0SYong Zhao mqd->allocate_mqd = allocate_mqd;
43059f650a0SOak Zeng mqd->init_mqd = init_mqd_hiq;
431a439b890SMukul Joshi mqd->free_mqd = kfd_free_mqd_cp;
43259f650a0SOak Zeng mqd->load_mqd = load_mqd;
43359f650a0SOak Zeng mqd->update_mqd = update_mqd_hiq;
434a439b890SMukul Joshi mqd->destroy_mqd = kfd_destroy_mqd_cp;
435a439b890SMukul Joshi mqd->is_occupied = kfd_is_occupied_cp;
4366c6cde55SOak Zeng mqd->mqd_size = sizeof(struct cik_mqd);
4372f77b9a2SMukul Joshi mqd->mqd_stride = kfd_mqd_stride;
43859f650a0SOak Zeng #if defined(CONFIG_DEBUG_FS)
43959f650a0SOak Zeng mqd->debugfs_show_mqd = debugfs_show_mqd;
44059f650a0SOak Zeng #endif
44159f650a0SOak Zeng break;
4424b8f589bSBen Goz case KFD_MQD_TYPE_SDMA:
4438636e53cSOak Zeng mqd->allocate_mqd = allocate_sdma_mqd;
4444b8f589bSBen Goz mqd->init_mqd = init_mqd_sdma;
4458636e53cSOak Zeng mqd->free_mqd = free_mqd_hiq_sdma;
446a439b890SMukul Joshi mqd->load_mqd = kfd_load_mqd_sdma;
4474b8f589bSBen Goz mqd->update_mqd = update_mqd_sdma;
448a439b890SMukul Joshi mqd->destroy_mqd = kfd_destroy_mqd_sdma;
449a439b890SMukul Joshi mqd->is_occupied = kfd_is_occupied_sdma;
45042c6c482SDavid Yat Sin mqd->checkpoint_mqd = checkpoint_mqd_sdma;
45142c6c482SDavid Yat Sin mqd->restore_mqd = restore_mqd_sdma;
4526c6cde55SOak Zeng mqd->mqd_size = sizeof(struct cik_sdma_rlc_registers);
4532f77b9a2SMukul Joshi mqd->mqd_stride = kfd_mqd_stride;
454851a645eSFelix Kuehling #if defined(CONFIG_DEBUG_FS)
455851a645eSFelix Kuehling mqd->debugfs_show_mqd = debugfs_show_mqd_sdma;
456851a645eSFelix Kuehling #endif
4574b8f589bSBen Goz break;
4584b8f589bSBen Goz default:
4594b8f589bSBen Goz kfree(mqd);
4604b8f589bSBen Goz return NULL;
4614b8f589bSBen Goz }
4624b8f589bSBen Goz
4634b8f589bSBen Goz return mqd;
4644b8f589bSBen Goz }
465