linux/drivers/gpu/drm/amd/amdkfd/kfd_kernel_queue.c
<<
>>
Prefs
   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/types.h>
  25#include <linux/mutex.h>
  26#include <linux/slab.h>
  27#include <linux/printk.h>
  28#include <linux/sched.h>
  29#include "kfd_kernel_queue.h"
  30#include "kfd_priv.h"
  31#include "kfd_device_queue_manager.h"
  32#include "kfd_pm4_headers.h"
  33#include "kfd_pm4_opcodes.h"
  34
  35#define PM4_COUNT_ZERO (((1 << 15) - 1) << 16)
  36
  37/* Initialize a kernel queue, including allocations of GART memory
  38 * needed for the queue.
  39 */
  40static bool kq_initialize(struct kernel_queue *kq, struct kfd_dev *dev,
  41                enum kfd_queue_type type, unsigned int queue_size)
  42{
  43        struct queue_properties prop;
  44        int retval;
  45        union PM4_MES_TYPE_3_HEADER nop;
  46
  47        if (WARN_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ))
  48                return false;
  49
  50        pr_debug("Initializing queue type %d size %d\n", KFD_QUEUE_TYPE_HIQ,
  51                        queue_size);
  52
  53        memset(&prop, 0, sizeof(prop));
  54        memset(&nop, 0, sizeof(nop));
  55
  56        nop.opcode = IT_NOP;
  57        nop.type = PM4_TYPE_3;
  58        nop.u32all |= PM4_COUNT_ZERO;
  59
  60        kq->dev = dev;
  61        kq->nop_packet = nop.u32all;
  62        switch (type) {
  63        case KFD_QUEUE_TYPE_DIQ:
  64                kq->mqd_mgr = dev->dqm->mqd_mgrs[KFD_MQD_TYPE_DIQ];
  65                break;
  66        case KFD_QUEUE_TYPE_HIQ:
  67                kq->mqd_mgr = dev->dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ];
  68                break;
  69        default:
  70                pr_err("Invalid queue type %d\n", type);
  71                return false;
  72        }
  73
  74        if (!kq->mqd_mgr)
  75                return false;
  76
  77        prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off);
  78
  79        if (!prop.doorbell_ptr) {
  80                pr_err("Failed to initialize doorbell");
  81                goto err_get_kernel_doorbell;
  82        }
  83
  84        retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq);
  85        if (retval != 0) {
  86                pr_err("Failed to init pq queues size %d\n", queue_size);
  87                goto err_pq_allocate_vidmem;
  88        }
  89
  90        kq->pq_kernel_addr = kq->pq->cpu_ptr;
  91        kq->pq_gpu_addr = kq->pq->gpu_addr;
  92
  93        /* For CIK family asics, kq->eop_mem is not needed */
  94        if (dev->device_info->asic_family > CHIP_MULLINS) {
  95                retval = kfd_gtt_sa_allocate(dev, PAGE_SIZE, &kq->eop_mem);
  96                if (retval != 0)
  97                        goto err_eop_allocate_vidmem;
  98
  99                kq->eop_gpu_addr = kq->eop_mem->gpu_addr;
 100                kq->eop_kernel_addr = kq->eop_mem->cpu_ptr;
 101
 102                memset(kq->eop_kernel_addr, 0, PAGE_SIZE);
 103        }
 104
 105        retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel),
 106                                        &kq->rptr_mem);
 107
 108        if (retval != 0)
 109                goto err_rptr_allocate_vidmem;
 110
 111        kq->rptr_kernel = kq->rptr_mem->cpu_ptr;
 112        kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr;
 113
 114        retval = kfd_gtt_sa_allocate(dev, dev->device_info->doorbell_size,
 115                                        &kq->wptr_mem);
 116
 117        if (retval != 0)
 118                goto err_wptr_allocate_vidmem;
 119
 120        kq->wptr_kernel = kq->wptr_mem->cpu_ptr;
 121        kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr;
 122
 123        memset(kq->pq_kernel_addr, 0, queue_size);
 124        memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel));
 125        memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel));
 126
 127        prop.queue_size = queue_size;
 128        prop.is_interop = false;
 129        prop.is_gws = false;
 130        prop.priority = 1;
 131        prop.queue_percent = 100;
 132        prop.type = type;
 133        prop.vmid = 0;
 134        prop.queue_address = kq->pq_gpu_addr;
 135        prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr;
 136        prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr;
 137        prop.eop_ring_buffer_address = kq->eop_gpu_addr;
 138        prop.eop_ring_buffer_size = PAGE_SIZE;
 139        prop.cu_mask = NULL;
 140
 141        if (init_queue(&kq->queue, &prop) != 0)
 142                goto err_init_queue;
 143
 144        kq->queue->device = dev;
 145        kq->queue->process = kfd_get_process(current);
 146
 147        kq->queue->mqd_mem_obj = kq->mqd_mgr->allocate_mqd(kq->mqd_mgr->dev,
 148                                        &kq->queue->properties);
 149        if (!kq->queue->mqd_mem_obj)
 150                goto err_allocate_mqd;
 151        kq->mqd_mgr->init_mqd(kq->mqd_mgr, &kq->queue->mqd,
 152                                        kq->queue->mqd_mem_obj,
 153                                        &kq->queue->gart_mqd_addr,
 154                                        &kq->queue->properties);
 155        /* assign HIQ to HQD */
 156        if (type == KFD_QUEUE_TYPE_HIQ) {
 157                pr_debug("Assigning hiq to hqd\n");
 158                kq->queue->pipe = KFD_CIK_HIQ_PIPE;
 159                kq->queue->queue = KFD_CIK_HIQ_QUEUE;
 160                kq->mqd_mgr->load_mqd(kq->mqd_mgr, kq->queue->mqd,
 161                                kq->queue->pipe, kq->queue->queue,
 162                                &kq->queue->properties, NULL);
 163        } else {
 164                /* allocate fence for DIQ */
 165
 166                retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t),
 167                                                &kq->fence_mem_obj);
 168
 169                if (retval != 0)
 170                        goto err_alloc_fence;
 171
 172                kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr;
 173                kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr;
 174        }
 175
 176        print_queue(kq->queue);
 177
 178        return true;
 179err_alloc_fence:
 180        kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd, kq->queue->mqd_mem_obj);
 181err_allocate_mqd:
 182        uninit_queue(kq->queue);
 183err_init_queue:
 184        kfd_gtt_sa_free(dev, kq->wptr_mem);
 185err_wptr_allocate_vidmem:
 186        kfd_gtt_sa_free(dev, kq->rptr_mem);
 187err_rptr_allocate_vidmem:
 188        kfd_gtt_sa_free(dev, kq->eop_mem);
 189err_eop_allocate_vidmem:
 190        kfd_gtt_sa_free(dev, kq->pq);
 191err_pq_allocate_vidmem:
 192        kfd_release_kernel_doorbell(dev, prop.doorbell_ptr);
 193err_get_kernel_doorbell:
 194        return false;
 195
 196}
 197
 198/* Uninitialize a kernel queue and free all its memory usages. */
 199static void kq_uninitialize(struct kernel_queue *kq, bool hanging)
 200{
 201        if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ && !hanging)
 202                kq->mqd_mgr->destroy_mqd(kq->mqd_mgr,
 203                                        kq->queue->mqd,
 204                                        KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
 205                                        KFD_UNMAP_LATENCY_MS,
 206                                        kq->queue->pipe,
 207                                        kq->queue->queue);
 208        else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ)
 209                kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj);
 210
 211        kq->mqd_mgr->free_mqd(kq->mqd_mgr, kq->queue->mqd,
 212                                kq->queue->mqd_mem_obj);
 213
 214        kfd_gtt_sa_free(kq->dev, kq->rptr_mem);
 215        kfd_gtt_sa_free(kq->dev, kq->wptr_mem);
 216
 217        /* For CIK family asics, kq->eop_mem is Null, kfd_gtt_sa_free()
 218         * is able to handle NULL properly.
 219         */
 220        kfd_gtt_sa_free(kq->dev, kq->eop_mem);
 221
 222        kfd_gtt_sa_free(kq->dev, kq->pq);
 223        kfd_release_kernel_doorbell(kq->dev,
 224                                        kq->queue->properties.doorbell_ptr);
 225        uninit_queue(kq->queue);
 226}
 227
 228int kq_acquire_packet_buffer(struct kernel_queue *kq,
 229                size_t packet_size_in_dwords, unsigned int **buffer_ptr)
 230{
 231        size_t available_size;
 232        size_t queue_size_dwords;
 233        uint32_t wptr, rptr;
 234        uint64_t wptr64;
 235        unsigned int *queue_address;
 236
 237        /* When rptr == wptr, the buffer is empty.
 238         * When rptr == wptr + 1, the buffer is full.
 239         * It is always rptr that advances to the position of wptr, rather than
 240         * the opposite. So we can only use up to queue_size_dwords - 1 dwords.
 241         */
 242        rptr = *kq->rptr_kernel;
 243        wptr = kq->pending_wptr;
 244        wptr64 = kq->pending_wptr64;
 245        queue_address = (unsigned int *)kq->pq_kernel_addr;
 246        queue_size_dwords = kq->queue->properties.queue_size / 4;
 247
 248        pr_debug("rptr: %d\n", rptr);
 249        pr_debug("wptr: %d\n", wptr);
 250        pr_debug("queue_address 0x%p\n", queue_address);
 251
 252        available_size = (rptr + queue_size_dwords - 1 - wptr) %
 253                                                        queue_size_dwords;
 254
 255        if (packet_size_in_dwords > available_size) {
 256                /*
 257                 * make sure calling functions know
 258                 * acquire_packet_buffer() failed
 259                 */
 260                goto err_no_space;
 261        }
 262
 263        if (wptr + packet_size_in_dwords >= queue_size_dwords) {
 264                /* make sure after rolling back to position 0, there is
 265                 * still enough space.
 266                 */
 267                if (packet_size_in_dwords >= rptr)
 268                        goto err_no_space;
 269
 270                /* fill nops, roll back and start at position 0 */
 271                while (wptr > 0) {
 272                        queue_address[wptr] = kq->nop_packet;
 273                        wptr = (wptr + 1) % queue_size_dwords;
 274                        wptr64++;
 275                }
 276        }
 277
 278        *buffer_ptr = &queue_address[wptr];
 279        kq->pending_wptr = wptr + packet_size_in_dwords;
 280        kq->pending_wptr64 = wptr64 + packet_size_in_dwords;
 281
 282        return 0;
 283
 284err_no_space:
 285        *buffer_ptr = NULL;
 286        return -ENOMEM;
 287}
 288
 289void kq_submit_packet(struct kernel_queue *kq)
 290{
 291#ifdef DEBUG
 292        int i;
 293
 294        for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) {
 295                pr_debug("0x%2X ", kq->pq_kernel_addr[i]);
 296                if (i % 15 == 0)
 297                        pr_debug("\n");
 298        }
 299        pr_debug("\n");
 300#endif
 301        if (kq->dev->device_info->doorbell_size == 8) {
 302                *kq->wptr64_kernel = kq->pending_wptr64;
 303                write_kernel_doorbell64(kq->queue->properties.doorbell_ptr,
 304                                        kq->pending_wptr64);
 305        } else {
 306                *kq->wptr_kernel = kq->pending_wptr;
 307                write_kernel_doorbell(kq->queue->properties.doorbell_ptr,
 308                                        kq->pending_wptr);
 309        }
 310}
 311
 312void kq_rollback_packet(struct kernel_queue *kq)
 313{
 314        if (kq->dev->device_info->doorbell_size == 8) {
 315                kq->pending_wptr64 = *kq->wptr64_kernel;
 316                kq->pending_wptr = *kq->wptr_kernel %
 317                        (kq->queue->properties.queue_size / 4);
 318        } else {
 319                kq->pending_wptr = *kq->wptr_kernel;
 320        }
 321}
 322
 323struct kernel_queue *kernel_queue_init(struct kfd_dev *dev,
 324                                        enum kfd_queue_type type)
 325{
 326        struct kernel_queue *kq;
 327
 328        kq = kzalloc(sizeof(*kq), GFP_KERNEL);
 329        if (!kq)
 330                return NULL;
 331
 332        if (kq_initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE))
 333                return kq;
 334
 335        pr_err("Failed to init kernel queue\n");
 336
 337        kfree(kq);
 338        return NULL;
 339}
 340
 341void kernel_queue_uninit(struct kernel_queue *kq, bool hanging)
 342{
 343        kq_uninitialize(kq, hanging);
 344        kfree(kq);
 345}
 346
 347/* FIXME: Can this test be removed? */
 348static __attribute__((unused)) void test_kq(struct kfd_dev *dev)
 349{
 350        struct kernel_queue *kq;
 351        uint32_t *buffer, i;
 352        int retval;
 353
 354        pr_err("Starting kernel queue test\n");
 355
 356        kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ);
 357        if (unlikely(!kq)) {
 358                pr_err("  Failed to initialize HIQ\n");
 359                pr_err("Kernel queue test failed\n");
 360                return;
 361        }
 362
 363        retval = kq_acquire_packet_buffer(kq, 5, &buffer);
 364        if (unlikely(retval != 0)) {
 365                pr_err("  Failed to acquire packet buffer\n");
 366                pr_err("Kernel queue test failed\n");
 367                return;
 368        }
 369        for (i = 0; i < 5; i++)
 370                buffer[i] = kq->nop_packet;
 371        kq_submit_packet(kq);
 372
 373        pr_err("Ending kernel queue test\n");
 374}
 375
 376
 377