blob: 813a416db5386121dc55c006eb7a215f7e1f8c7f [file] [log] [blame]
Oded Gabbaye28740e2014-07-15 13:53:32 +03001/*
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#include <linux/module.h>
24#include <linux/fdtable.h>
25#include <linux/uaccess.h>
26#include <drm/drmP.h>
27#include "radeon.h"
28#include "cikd.h"
29#include "cik_reg.h"
30#include "radeon_kfd.h"
Oded Gabbayf7694322014-11-09 12:45:11 +020031#include "radeon_ucode.h"
32#include <linux/firmware.h>
Oded Gabbay836aabc2015-01-02 23:33:20 +020033#include "cik_structs.h"
Oded Gabbaye28740e2014-07-15 13:53:32 +030034
35#define CIK_PIPE_PER_MEC (4)
36
37struct kgd_mem {
Oded Gabbayceae8812014-10-26 20:52:55 +020038 struct radeon_bo *bo;
Oded Gabbay632aa2c2014-10-26 09:43:43 +020039 uint64_t gpu_addr;
Oded Gabbayceae8812014-10-26 20:52:55 +020040 void *cpu_ptr;
Oded Gabbaye28740e2014-07-15 13:53:32 +030041};
42
Oded Gabbaye28740e2014-07-15 13:53:32 +030043
Oded Gabbayceae8812014-10-26 20:52:55 +020044static int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
45 void **mem_obj, uint64_t *gpu_addr,
46 void **cpu_ptr);
47
48static void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj);
49
Oded Gabbaye28740e2014-07-15 13:53:32 +030050static uint64_t get_vmem_size(struct kgd_dev *kgd);
51static uint64_t get_gpu_clock_counter(struct kgd_dev *kgd);
52
53static uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd);
Oded Gabbayf7694322014-11-09 12:45:11 +020054static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type);
Oded Gabbaye28740e2014-07-15 13:53:32 +030055
56/*
57 * Register access functions
58 */
59
60static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
61 uint32_t sh_mem_config, uint32_t sh_mem_ape1_base,
62 uint32_t sh_mem_ape1_limit, uint32_t sh_mem_bases);
63
64static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
65 unsigned int vmid);
66
Oded Gabbaye28740e2014-07-15 13:53:32 +030067static int kgd_init_pipeline(struct kgd_dev *kgd, uint32_t pipe_id,
68 uint32_t hpd_size, uint64_t hpd_gpu_addr);
Oded Gabbayd36b94f2015-03-05 15:13:18 +020069static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id);
Oded Gabbaye28740e2014-07-15 13:53:32 +030070static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
71 uint32_t queue_id, uint32_t __user *wptr);
Ben Goza84a99032015-01-03 22:12:30 +020072static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd);
Ben Gozb64b8af2014-12-09 12:00:09 +020073static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
Oded Gabbaye28740e2014-07-15 13:53:32 +030074 uint32_t pipe_id, uint32_t queue_id);
75
76static int kgd_hqd_destroy(struct kgd_dev *kgd, uint32_t reset_type,
77 unsigned int timeout, uint32_t pipe_id,
78 uint32_t queue_id);
Ben Goza84a99032015-01-03 22:12:30 +020079static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd);
80static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
81 unsigned int timeout);
Oded Gabbaye28740e2014-07-15 13:53:32 +030082
83static const struct kfd2kgd_calls kfd2kgd = {
Oded Gabbayceae8812014-10-26 20:52:55 +020084 .init_gtt_mem_allocation = alloc_gtt_mem,
85 .free_gtt_mem = free_gtt_mem,
Oded Gabbaye28740e2014-07-15 13:53:32 +030086 .get_vmem_size = get_vmem_size,
87 .get_gpu_clock_counter = get_gpu_clock_counter,
88 .get_max_engine_clock_in_mhz = get_max_engine_clock_in_mhz,
89 .program_sh_mem_settings = kgd_program_sh_mem_settings,
90 .set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
Oded Gabbaye28740e2014-07-15 13:53:32 +030091 .init_pipeline = kgd_init_pipeline,
Oded Gabbayd36b94f2015-03-05 15:13:18 +020092 .init_interrupts = kgd_init_interrupts,
Oded Gabbaye28740e2014-07-15 13:53:32 +030093 .hqd_load = kgd_hqd_load,
Ben Goza84a99032015-01-03 22:12:30 +020094 .hqd_sdma_load = kgd_hqd_sdma_load,
Ben Gozb64b8af2014-12-09 12:00:09 +020095 .hqd_is_occupied = kgd_hqd_is_occupied,
Ben Goza84a99032015-01-03 22:12:30 +020096 .hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
Oded Gabbaye28740e2014-07-15 13:53:32 +030097 .hqd_destroy = kgd_hqd_destroy,
Ben Goza84a99032015-01-03 22:12:30 +020098 .hqd_sdma_destroy = kgd_hqd_sdma_destroy,
Oded Gabbayf7694322014-11-09 12:45:11 +020099 .get_fw_version = get_fw_version
Oded Gabbaye28740e2014-07-15 13:53:32 +0300100};
101
102static const struct kgd2kfd_calls *kgd2kfd;
103
104bool radeon_kfd_init(void)
105{
Oded Gabbay38c2adf2014-12-22 11:19:23 +0200106#if defined(CONFIG_HSA_AMD_MODULE)
Xihan Zhangcea405b2015-03-17 19:32:53 +0800107 bool (*kgd2kfd_init_p)(unsigned, const struct kgd2kfd_calls**);
Oded Gabbaye28740e2014-07-15 13:53:32 +0300108
109 kgd2kfd_init_p = symbol_request(kgd2kfd_init);
110
111 if (kgd2kfd_init_p == NULL)
112 return false;
113
Xihan Zhangcea405b2015-03-17 19:32:53 +0800114 if (!kgd2kfd_init_p(KFD_INTERFACE_VERSION, &kgd2kfd)) {
Oded Gabbaye28740e2014-07-15 13:53:32 +0300115 symbol_put(kgd2kfd_init);
116 kgd2kfd = NULL;
117
118 return false;
119 }
120
121 return true;
Oded Gabbay38c2adf2014-12-22 11:19:23 +0200122#elif defined(CONFIG_HSA_AMD)
Xihan Zhangcea405b2015-03-17 19:32:53 +0800123 if (!kgd2kfd_init(KFD_INTERFACE_VERSION, &kgd2kfd)) {
Oded Gabbay38c2adf2014-12-22 11:19:23 +0200124 kgd2kfd = NULL;
125
126 return false;
127 }
128
129 return true;
130#else
131 return false;
132#endif
Oded Gabbaye28740e2014-07-15 13:53:32 +0300133}
134
135void radeon_kfd_fini(void)
136{
137 if (kgd2kfd) {
138 kgd2kfd->exit();
139 symbol_put(kgd2kfd_init);
140 }
141}
142
143void radeon_kfd_device_probe(struct radeon_device *rdev)
144{
145 if (kgd2kfd)
Xihan Zhangcea405b2015-03-17 19:32:53 +0800146 rdev->kfd = kgd2kfd->probe((struct kgd_dev *)rdev,
147 rdev->pdev, &kfd2kgd);
Oded Gabbaye28740e2014-07-15 13:53:32 +0300148}
149
150void radeon_kfd_device_init(struct radeon_device *rdev)
151{
152 if (rdev->kfd) {
153 struct kgd2kfd_shared_resources gpu_resources = {
154 .compute_vmid_bitmap = 0xFF00,
155
156 .first_compute_pipe = 1,
Ben Goze405ca32015-03-08 14:15:16 +0200157 .compute_pipe_count = 4 - 1,
Oded Gabbaye28740e2014-07-15 13:53:32 +0300158 };
159
160 radeon_doorbell_get_kfd_info(rdev,
161 &gpu_resources.doorbell_physical_address,
162 &gpu_resources.doorbell_aperture_size,
163 &gpu_resources.doorbell_start_offset);
164
165 kgd2kfd->device_init(rdev->kfd, &gpu_resources);
166 }
167}
168
169void radeon_kfd_device_fini(struct radeon_device *rdev)
170{
171 if (rdev->kfd) {
172 kgd2kfd->device_exit(rdev->kfd);
173 rdev->kfd = NULL;
174 }
175}
176
177void radeon_kfd_interrupt(struct radeon_device *rdev, const void *ih_ring_entry)
178{
179 if (rdev->kfd)
180 kgd2kfd->interrupt(rdev->kfd, ih_ring_entry);
181}
182
183void radeon_kfd_suspend(struct radeon_device *rdev)
184{
185 if (rdev->kfd)
186 kgd2kfd->suspend(rdev->kfd);
187}
188
189int radeon_kfd_resume(struct radeon_device *rdev)
190{
191 int r = 0;
192
193 if (rdev->kfd)
194 r = kgd2kfd->resume(rdev->kfd);
195
196 return r;
197}
198
Oded Gabbayceae8812014-10-26 20:52:55 +0200199static int alloc_gtt_mem(struct kgd_dev *kgd, size_t size,
200 void **mem_obj, uint64_t *gpu_addr,
201 void **cpu_ptr)
202{
203 struct radeon_device *rdev = (struct radeon_device *)kgd;
204 struct kgd_mem **mem = (struct kgd_mem **) mem_obj;
205 int r;
206
207 BUG_ON(kgd == NULL);
208 BUG_ON(gpu_addr == NULL);
209 BUG_ON(cpu_ptr == NULL);
210
211 *mem = kmalloc(sizeof(struct kgd_mem), GFP_KERNEL);
212 if ((*mem) == NULL)
213 return -ENOMEM;
214
215 r = radeon_bo_create(rdev, size, PAGE_SIZE, true, RADEON_GEM_DOMAIN_GTT,
216 RADEON_GEM_GTT_WC, NULL, NULL, &(*mem)->bo);
217 if (r) {
218 dev_err(rdev->dev,
219 "failed to allocate BO for amdkfd (%d)\n", r);
220 return r;
221 }
222
223 /* map the buffer */
224 r = radeon_bo_reserve((*mem)->bo, true);
225 if (r) {
226 dev_err(rdev->dev, "(%d) failed to reserve bo for amdkfd\n", r);
227 goto allocate_mem_reserve_bo_failed;
228 }
229
230 r = radeon_bo_pin((*mem)->bo, RADEON_GEM_DOMAIN_GTT,
231 &(*mem)->gpu_addr);
232 if (r) {
233 dev_err(rdev->dev, "(%d) failed to pin bo for amdkfd\n", r);
234 goto allocate_mem_pin_bo_failed;
235 }
236 *gpu_addr = (*mem)->gpu_addr;
237
238 r = radeon_bo_kmap((*mem)->bo, &(*mem)->cpu_ptr);
239 if (r) {
240 dev_err(rdev->dev,
241 "(%d) failed to map bo to kernel for amdkfd\n", r);
242 goto allocate_mem_kmap_bo_failed;
243 }
244 *cpu_ptr = (*mem)->cpu_ptr;
245
246 radeon_bo_unreserve((*mem)->bo);
247
248 return 0;
249
250allocate_mem_kmap_bo_failed:
251 radeon_bo_unpin((*mem)->bo);
252allocate_mem_pin_bo_failed:
253 radeon_bo_unreserve((*mem)->bo);
254allocate_mem_reserve_bo_failed:
255 radeon_bo_unref(&(*mem)->bo);
256
257 return r;
258}
259
260static void free_gtt_mem(struct kgd_dev *kgd, void *mem_obj)
261{
262 struct kgd_mem *mem = (struct kgd_mem *) mem_obj;
263
264 BUG_ON(mem == NULL);
265
266 radeon_bo_reserve(mem->bo, true);
267 radeon_bo_kunmap(mem->bo);
268 radeon_bo_unpin(mem->bo);
269 radeon_bo_unreserve(mem->bo);
270 radeon_bo_unref(&(mem->bo));
271 kfree(mem);
272}
273
Oded Gabbaye28740e2014-07-15 13:53:32 +0300274static uint64_t get_vmem_size(struct kgd_dev *kgd)
275{
276 struct radeon_device *rdev = (struct radeon_device *)kgd;
277
278 BUG_ON(kgd == NULL);
279
280 return rdev->mc.real_vram_size;
281}
282
283static uint64_t get_gpu_clock_counter(struct kgd_dev *kgd)
284{
285 struct radeon_device *rdev = (struct radeon_device *)kgd;
286
287 return rdev->asic->get_gpu_clock_counter(rdev);
288}
289
290static uint32_t get_max_engine_clock_in_mhz(struct kgd_dev *kgd)
291{
292 struct radeon_device *rdev = (struct radeon_device *)kgd;
293
294 /* The sclk is in quantas of 10kHz */
295 return rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk / 100;
296}
297
298static inline struct radeon_device *get_radeon_device(struct kgd_dev *kgd)
299{
300 return (struct radeon_device *)kgd;
301}
302
303static void write_register(struct kgd_dev *kgd, uint32_t offset, uint32_t value)
304{
305 struct radeon_device *rdev = get_radeon_device(kgd);
306
307 writel(value, (void __iomem *)(rdev->rmmio + offset));
308}
309
310static uint32_t read_register(struct kgd_dev *kgd, uint32_t offset)
311{
312 struct radeon_device *rdev = get_radeon_device(kgd);
313
314 return readl((void __iomem *)(rdev->rmmio + offset));
315}
316
317static void lock_srbm(struct kgd_dev *kgd, uint32_t mec, uint32_t pipe,
318 uint32_t queue, uint32_t vmid)
319{
320 struct radeon_device *rdev = get_radeon_device(kgd);
321 uint32_t value = PIPEID(pipe) | MEID(mec) | VMID(vmid) | QUEUEID(queue);
322
323 mutex_lock(&rdev->srbm_mutex);
324 write_register(kgd, SRBM_GFX_CNTL, value);
325}
326
327static void unlock_srbm(struct kgd_dev *kgd)
328{
329 struct radeon_device *rdev = get_radeon_device(kgd);
330
331 write_register(kgd, SRBM_GFX_CNTL, 0);
332 mutex_unlock(&rdev->srbm_mutex);
333}
334
335static void acquire_queue(struct kgd_dev *kgd, uint32_t pipe_id,
336 uint32_t queue_id)
337{
338 uint32_t mec = (++pipe_id / CIK_PIPE_PER_MEC) + 1;
339 uint32_t pipe = (pipe_id % CIK_PIPE_PER_MEC);
340
341 lock_srbm(kgd, mec, pipe, queue_id, 0);
342}
343
344static void release_queue(struct kgd_dev *kgd)
345{
346 unlock_srbm(kgd);
347}
348
349static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
350 uint32_t sh_mem_config,
351 uint32_t sh_mem_ape1_base,
352 uint32_t sh_mem_ape1_limit,
353 uint32_t sh_mem_bases)
354{
355 lock_srbm(kgd, 0, 0, 0, vmid);
356
357 write_register(kgd, SH_MEM_CONFIG, sh_mem_config);
358 write_register(kgd, SH_MEM_APE1_BASE, sh_mem_ape1_base);
359 write_register(kgd, SH_MEM_APE1_LIMIT, sh_mem_ape1_limit);
360 write_register(kgd, SH_MEM_BASES, sh_mem_bases);
361
362 unlock_srbm(kgd);
363}
364
365static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
366 unsigned int vmid)
367{
368 /*
369 * We have to assume that there is no outstanding mapping.
370 * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0
371 * because a mapping is in progress or because a mapping finished and
372 * the SW cleared it.
373 * So the protocol is to always wait & clear.
374 */
375 uint32_t pasid_mapping = (pasid == 0) ? 0 :
376 (uint32_t)pasid | ATC_VMID_PASID_MAPPING_VALID;
377
378 write_register(kgd, ATC_VMID0_PASID_MAPPING + vmid*sizeof(uint32_t),
379 pasid_mapping);
380
381 while (!(read_register(kgd, ATC_VMID_PASID_MAPPING_UPDATE_STATUS) &
382 (1U << vmid)))
383 cpu_relax();
384 write_register(kgd, ATC_VMID_PASID_MAPPING_UPDATE_STATUS, 1U << vmid);
385
Ben Gozfec77bb2014-12-17 14:09:10 +0200386 /* Mapping vmid to pasid also for IH block */
387 write_register(kgd, IH_VMID_0_LUT + vmid * sizeof(uint32_t),
388 pasid_mapping);
389
Oded Gabbaye28740e2014-07-15 13:53:32 +0300390 return 0;
391}
392
Oded Gabbaye28740e2014-07-15 13:53:32 +0300393static int kgd_init_pipeline(struct kgd_dev *kgd, uint32_t pipe_id,
394 uint32_t hpd_size, uint64_t hpd_gpu_addr)
395{
Oded Gabbay5a8888a2015-01-22 12:48:33 +0200396 uint32_t mec = (pipe_id / CIK_PIPE_PER_MEC) + 1;
Oded Gabbaye28740e2014-07-15 13:53:32 +0300397 uint32_t pipe = (pipe_id % CIK_PIPE_PER_MEC);
398
399 lock_srbm(kgd, mec, pipe, 0, 0);
400 write_register(kgd, CP_HPD_EOP_BASE_ADDR,
401 lower_32_bits(hpd_gpu_addr >> 8));
402 write_register(kgd, CP_HPD_EOP_BASE_ADDR_HI,
403 upper_32_bits(hpd_gpu_addr >> 8));
404 write_register(kgd, CP_HPD_EOP_VMID, 0);
405 write_register(kgd, CP_HPD_EOP_CONTROL, hpd_size);
406 unlock_srbm(kgd);
407
408 return 0;
409}
410
Oded Gabbayd36b94f2015-03-05 15:13:18 +0200411static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id)
412{
413 uint32_t mec;
414 uint32_t pipe;
415
416 mec = (pipe_id / CIK_PIPE_PER_MEC) + 1;
417 pipe = (pipe_id % CIK_PIPE_PER_MEC);
418
419 lock_srbm(kgd, mec, pipe, 0, 0);
420
421 write_register(kgd, CPC_INT_CNTL,
422 TIME_STAMP_INT_ENABLE | OPCODE_ERROR_INT_ENABLE);
423
424 unlock_srbm(kgd);
425
426 return 0;
427}
428
Ben Goza84a99032015-01-03 22:12:30 +0200429static inline uint32_t get_sdma_base_addr(struct cik_sdma_rlc_registers *m)
430{
431 uint32_t retval;
432
433 retval = m->sdma_engine_id * SDMA1_REGISTER_OFFSET +
434 m->sdma_queue_id * KFD_CIK_SDMA_QUEUE_OFFSET;
435
436 pr_debug("kfd: sdma base address: 0x%x\n", retval);
437
438 return retval;
439}
440
Oded Gabbaye28740e2014-07-15 13:53:32 +0300441static inline struct cik_mqd *get_mqd(void *mqd)
442{
443 return (struct cik_mqd *)mqd;
444}
445
Ben Goza84a99032015-01-03 22:12:30 +0200446static inline struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
447{
448 return (struct cik_sdma_rlc_registers *)mqd;
449}
450
Oded Gabbaye28740e2014-07-15 13:53:32 +0300451static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
452 uint32_t queue_id, uint32_t __user *wptr)
453{
454 uint32_t wptr_shadow, is_wptr_shadow_valid;
455 struct cik_mqd *m;
456
457 m = get_mqd(mqd);
458
459 is_wptr_shadow_valid = !get_user(wptr_shadow, wptr);
460
461 acquire_queue(kgd, pipe_id, queue_id);
462 write_register(kgd, CP_MQD_BASE_ADDR, m->cp_mqd_base_addr_lo);
463 write_register(kgd, CP_MQD_BASE_ADDR_HI, m->cp_mqd_base_addr_hi);
464 write_register(kgd, CP_MQD_CONTROL, m->cp_mqd_control);
465
466 write_register(kgd, CP_HQD_PQ_BASE, m->cp_hqd_pq_base_lo);
467 write_register(kgd, CP_HQD_PQ_BASE_HI, m->cp_hqd_pq_base_hi);
468 write_register(kgd, CP_HQD_PQ_CONTROL, m->cp_hqd_pq_control);
469
470 write_register(kgd, CP_HQD_IB_CONTROL, m->cp_hqd_ib_control);
471 write_register(kgd, CP_HQD_IB_BASE_ADDR, m->cp_hqd_ib_base_addr_lo);
472 write_register(kgd, CP_HQD_IB_BASE_ADDR_HI, m->cp_hqd_ib_base_addr_hi);
473
474 write_register(kgd, CP_HQD_IB_RPTR, m->cp_hqd_ib_rptr);
475
476 write_register(kgd, CP_HQD_PERSISTENT_STATE,
477 m->cp_hqd_persistent_state);
478 write_register(kgd, CP_HQD_SEMA_CMD, m->cp_hqd_sema_cmd);
479 write_register(kgd, CP_HQD_MSG_TYPE, m->cp_hqd_msg_type);
480
481 write_register(kgd, CP_HQD_ATOMIC0_PREOP_LO,
482 m->cp_hqd_atomic0_preop_lo);
483
484 write_register(kgd, CP_HQD_ATOMIC0_PREOP_HI,
485 m->cp_hqd_atomic0_preop_hi);
486
487 write_register(kgd, CP_HQD_ATOMIC1_PREOP_LO,
488 m->cp_hqd_atomic1_preop_lo);
489
490 write_register(kgd, CP_HQD_ATOMIC1_PREOP_HI,
491 m->cp_hqd_atomic1_preop_hi);
492
493 write_register(kgd, CP_HQD_PQ_RPTR_REPORT_ADDR,
494 m->cp_hqd_pq_rptr_report_addr_lo);
495
496 write_register(kgd, CP_HQD_PQ_RPTR_REPORT_ADDR_HI,
497 m->cp_hqd_pq_rptr_report_addr_hi);
498
499 write_register(kgd, CP_HQD_PQ_RPTR, m->cp_hqd_pq_rptr);
500
501 write_register(kgd, CP_HQD_PQ_WPTR_POLL_ADDR,
502 m->cp_hqd_pq_wptr_poll_addr_lo);
503
504 write_register(kgd, CP_HQD_PQ_WPTR_POLL_ADDR_HI,
505 m->cp_hqd_pq_wptr_poll_addr_hi);
506
507 write_register(kgd, CP_HQD_PQ_DOORBELL_CONTROL,
508 m->cp_hqd_pq_doorbell_control);
509
510 write_register(kgd, CP_HQD_VMID, m->cp_hqd_vmid);
511
512 write_register(kgd, CP_HQD_QUANTUM, m->cp_hqd_quantum);
513
514 write_register(kgd, CP_HQD_PIPE_PRIORITY, m->cp_hqd_pipe_priority);
515 write_register(kgd, CP_HQD_QUEUE_PRIORITY, m->cp_hqd_queue_priority);
516
517 write_register(kgd, CP_HQD_IQ_RPTR, m->cp_hqd_iq_rptr);
518
519 if (is_wptr_shadow_valid)
520 write_register(kgd, CP_HQD_PQ_WPTR, wptr_shadow);
521
522 write_register(kgd, CP_HQD_ACTIVE, m->cp_hqd_active);
523 release_queue(kgd);
524
525 return 0;
526}
527
Ben Goza84a99032015-01-03 22:12:30 +0200528static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd)
529{
530 struct cik_sdma_rlc_registers *m;
531 uint32_t sdma_base_addr;
532
533 m = get_sdma_mqd(mqd);
534 sdma_base_addr = get_sdma_base_addr(m);
535
536 write_register(kgd,
537 sdma_base_addr + SDMA0_RLC0_VIRTUAL_ADDR,
538 m->sdma_rlc_virtual_addr);
539
540 write_register(kgd,
541 sdma_base_addr + SDMA0_RLC0_RB_BASE,
542 m->sdma_rlc_rb_base);
543
544 write_register(kgd,
545 sdma_base_addr + SDMA0_RLC0_RB_BASE_HI,
546 m->sdma_rlc_rb_base_hi);
547
548 write_register(kgd,
549 sdma_base_addr + SDMA0_RLC0_RB_RPTR_ADDR_LO,
550 m->sdma_rlc_rb_rptr_addr_lo);
551
552 write_register(kgd,
553 sdma_base_addr + SDMA0_RLC0_RB_RPTR_ADDR_HI,
554 m->sdma_rlc_rb_rptr_addr_hi);
555
556 write_register(kgd,
557 sdma_base_addr + SDMA0_RLC0_DOORBELL,
558 m->sdma_rlc_doorbell);
559
560 write_register(kgd,
561 sdma_base_addr + SDMA0_RLC0_RB_CNTL,
562 m->sdma_rlc_rb_cntl);
563
564 return 0;
565}
566
Ben Gozb64b8af2014-12-09 12:00:09 +0200567static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
Oded Gabbaye28740e2014-07-15 13:53:32 +0300568 uint32_t pipe_id, uint32_t queue_id)
569{
570 uint32_t act;
571 bool retval = false;
572 uint32_t low, high;
573
574 acquire_queue(kgd, pipe_id, queue_id);
575 act = read_register(kgd, CP_HQD_ACTIVE);
576 if (act) {
577 low = lower_32_bits(queue_address >> 8);
578 high = upper_32_bits(queue_address >> 8);
579
580 if (low == read_register(kgd, CP_HQD_PQ_BASE) &&
581 high == read_register(kgd, CP_HQD_PQ_BASE_HI))
582 retval = true;
583 }
584 release_queue(kgd);
585 return retval;
586}
587
Ben Goza84a99032015-01-03 22:12:30 +0200588static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd)
589{
590 struct cik_sdma_rlc_registers *m;
591 uint32_t sdma_base_addr;
592 uint32_t sdma_rlc_rb_cntl;
593
594 m = get_sdma_mqd(mqd);
595 sdma_base_addr = get_sdma_base_addr(m);
596
597 sdma_rlc_rb_cntl = read_register(kgd,
598 sdma_base_addr + SDMA0_RLC0_RB_CNTL);
599
600 if (sdma_rlc_rb_cntl & SDMA_RB_ENABLE)
601 return true;
602
603 return false;
604}
605
Oded Gabbaye28740e2014-07-15 13:53:32 +0300606static int kgd_hqd_destroy(struct kgd_dev *kgd, uint32_t reset_type,
607 unsigned int timeout, uint32_t pipe_id,
608 uint32_t queue_id)
609{
610 uint32_t temp;
611
612 acquire_queue(kgd, pipe_id, queue_id);
613 write_register(kgd, CP_HQD_PQ_DOORBELL_CONTROL, 0);
614
615 write_register(kgd, CP_HQD_DEQUEUE_REQUEST, reset_type);
616
617 while (true) {
618 temp = read_register(kgd, CP_HQD_ACTIVE);
619 if (temp & 0x1)
620 break;
621 if (timeout == 0) {
622 pr_err("kfd: cp queue preemption time out (%dms)\n",
623 temp);
Alexey Khoroshilov4c184422015-01-04 02:31:20 +0300624 release_queue(kgd);
Oded Gabbaye28740e2014-07-15 13:53:32 +0300625 return -ETIME;
626 }
627 msleep(20);
628 timeout -= 20;
629 }
630
631 release_queue(kgd);
632 return 0;
633}
Oded Gabbayf7694322014-11-09 12:45:11 +0200634
Ben Goza84a99032015-01-03 22:12:30 +0200635static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
636 unsigned int timeout)
637{
638 struct cik_sdma_rlc_registers *m;
639 uint32_t sdma_base_addr;
640 uint32_t temp;
641
642 m = get_sdma_mqd(mqd);
643 sdma_base_addr = get_sdma_base_addr(m);
644
645 temp = read_register(kgd, sdma_base_addr + SDMA0_RLC0_RB_CNTL);
646 temp = temp & ~SDMA_RB_ENABLE;
647 write_register(kgd, sdma_base_addr + SDMA0_RLC0_RB_CNTL, temp);
648
649 while (true) {
650 temp = read_register(kgd, sdma_base_addr +
651 SDMA0_RLC0_CONTEXT_STATUS);
652 if (temp & SDMA_RLC_IDLE)
653 break;
654 if (timeout == 0)
655 return -ETIME;
656 msleep(20);
657 timeout -= 20;
658 }
659
660 write_register(kgd, sdma_base_addr + SDMA0_RLC0_DOORBELL, 0);
661 write_register(kgd, sdma_base_addr + SDMA0_RLC0_RB_RPTR, 0);
662 write_register(kgd, sdma_base_addr + SDMA0_RLC0_RB_WPTR, 0);
663 write_register(kgd, sdma_base_addr + SDMA0_RLC0_RB_BASE, 0);
664
665 return 0;
666}
667
Oded Gabbayf7694322014-11-09 12:45:11 +0200668static uint16_t get_fw_version(struct kgd_dev *kgd, enum kgd_engine_type type)
669{
670 struct radeon_device *rdev = (struct radeon_device *) kgd;
671 const union radeon_firmware_header *hdr;
672
673 BUG_ON(kgd == NULL || rdev->mec_fw == NULL);
674
675 switch (type) {
676 case KGD_ENGINE_PFP:
677 hdr = (const union radeon_firmware_header *) rdev->pfp_fw->data;
678 break;
679
680 case KGD_ENGINE_ME:
681 hdr = (const union radeon_firmware_header *) rdev->me_fw->data;
682 break;
683
684 case KGD_ENGINE_CE:
685 hdr = (const union radeon_firmware_header *) rdev->ce_fw->data;
686 break;
687
688 case KGD_ENGINE_MEC1:
689 hdr = (const union radeon_firmware_header *) rdev->mec_fw->data;
690 break;
691
692 case KGD_ENGINE_MEC2:
693 hdr = (const union radeon_firmware_header *)
694 rdev->mec2_fw->data;
695 break;
696
697 case KGD_ENGINE_RLC:
698 hdr = (const union radeon_firmware_header *) rdev->rlc_fw->data;
699 break;
700
701 case KGD_ENGINE_SDMA:
702 hdr = (const union radeon_firmware_header *)
703 rdev->sdma_fw->data;
704 break;
705
706 default:
707 return 0;
708 }
709
710 if (hdr == NULL)
711 return 0;
712
713 /* Only 12 bit in use*/
714 return hdr->common.ucode_version;
715}