|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */ |
| 2 | +/* |
| 3 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <cn_api.h> |
| 10 | +#include "mlu_memory.h" |
| 11 | +#include "perftest_parameters.h" |
| 12 | + |
| 13 | +static inline const char *getErrorName(CNresult error) |
| 14 | +{ |
| 15 | + const char *str; |
| 16 | + cnGetErrorName(error, &str); |
| 17 | + return str; |
| 18 | +} |
| 19 | + |
| 20 | +static inline const char *getErrorString(CNresult error) |
| 21 | +{ |
| 22 | + const char *str; |
| 23 | + cnGetErrorString(error, &str); |
| 24 | + return str; |
| 25 | +} |
| 26 | + |
| 27 | +#define ERROR_CHECK(ret) \ |
| 28 | + do { \ |
| 29 | + CNresult r__ = (ret); \ |
| 30 | + if (r__ != CN_SUCCESS) { \ |
| 31 | + printf( \ |
| 32 | + "error occur, func: %s, line: %d, ret:%d, cn_error_code:%s, cn_error_string:%s\n", \ |
| 33 | + __func__, __LINE__, r__, getErrorName(r__), getErrorString(r__)); \ |
| 34 | + exit(0); \ |
| 35 | + } \ |
| 36 | + } while (0) |
| 37 | + |
| 38 | +#define ACCEL_PAGE_SIZE (64 * 1024) |
| 39 | + |
| 40 | + |
| 41 | +struct mlu_memory_ctx { |
| 42 | + struct memory_ctx base; |
| 43 | + int device_id; |
| 44 | + CNdev cnDevice; |
| 45 | + CNcontext cnContext; |
| 46 | + |
| 47 | +}; |
| 48 | + |
| 49 | + |
| 50 | +static int init_mlu(struct mlu_memory_ctx *ctx) |
| 51 | +{ |
| 52 | + int mlu_device_id = ctx->device_id; |
| 53 | + int mlu_pci_bus_id; |
| 54 | + int mlu_pci_device_id; |
| 55 | + int index; |
| 56 | + CNdev cn_device; |
| 57 | + |
| 58 | + printf("initializing MLU\n"); |
| 59 | + CNresult error = cnInit(0); |
| 60 | + if (error != CN_SUCCESS) { |
| 61 | + printf("cnInit(0) returned %d\n", error); |
| 62 | + return FAILURE; |
| 63 | + } |
| 64 | + |
| 65 | + int deviceCount = 0; |
| 66 | + error = cnDeviceGetCount(&deviceCount); |
| 67 | + if (error != CN_SUCCESS) { |
| 68 | + printf("cnDeviceGetCount() returned %d\n", error); |
| 69 | + return FAILURE; |
| 70 | + } |
| 71 | + |
| 72 | + if (deviceCount == 0) { |
| 73 | + printf("There are no available device(s) that support MLU\n"); |
| 74 | + return FAILURE; |
| 75 | + } |
| 76 | + if (mlu_device_id >= deviceCount) { |
| 77 | + fprintf(stderr, "No such device ID (%d) exists in system\n", mlu_device_id); |
| 78 | + return FAILURE; |
| 79 | + } |
| 80 | + |
| 81 | + printf("Listing all MLU devices in system:\n"); |
| 82 | + for (index = 0; index < deviceCount; index++) { |
| 83 | + ERROR_CHECK(cnDeviceGet(&cn_device, index)); |
| 84 | + cnDeviceGetAttribute(&mlu_pci_bus_id, CN_DEVICE_ATTRIBUTE_PCI_BUS_ID , cn_device); |
| 85 | + cnDeviceGetAttribute(&mlu_pci_device_id, CN_DEVICE_ATTRIBUTE_PCI_DEVICE_ID , cn_device); |
| 86 | + printf("MLU device %d: PCIe address is %02X:%02X\n", index, (unsigned int)mlu_pci_bus_id, (unsigned int)mlu_pci_device_id); |
| 87 | + } |
| 88 | + |
| 89 | + printf("\nPicking device No. %d\n", mlu_device_id); |
| 90 | + |
| 91 | + ERROR_CHECK(cnDeviceGet(&ctx->cnDevice, mlu_device_id)); |
| 92 | + |
| 93 | + char name[128]; |
| 94 | + ERROR_CHECK(cnDeviceGetName(name, sizeof(name), mlu_device_id)); |
| 95 | + printf("[pid = %d, dev = %ld] device name = [%s]\n", getpid(), ctx->cnDevice, name); |
| 96 | + printf("creating MLU Ctx\n"); |
| 97 | + |
| 98 | + /* Create context */ |
| 99 | + error = cnCtxCreate(&ctx->cnContext, 0, ctx->cnDevice); |
| 100 | + if (error != CN_SUCCESS) { |
| 101 | + printf("cnCtxCreate() error=%d\n", error); |
| 102 | + return FAILURE; |
| 103 | + } |
| 104 | + |
| 105 | + printf("making it the current MLU Ctx\n"); |
| 106 | + error = cnCtxSetCurrent(ctx->cnContext); |
| 107 | + if (error != CN_SUCCESS) { |
| 108 | + printf("cnCtxSetCurrent() error=%d\n", error); |
| 109 | + return FAILURE; |
| 110 | + } |
| 111 | + |
| 112 | + return SUCCESS; |
| 113 | +} |
| 114 | + |
| 115 | +static void free_mlu(struct mlu_memory_ctx *ctx) |
| 116 | +{ |
| 117 | + printf("destroying current MLU Ctx\n"); |
| 118 | + ERROR_CHECK(cnCtxDestroy(ctx->cnContext)); |
| 119 | +} |
| 120 | + |
| 121 | +int mlu_memory_init(struct memory_ctx *ctx) { |
| 122 | + struct mlu_memory_ctx *mlu_ctx = container_of(ctx, struct mlu_memory_ctx, base); |
| 123 | + int return_value = 0; |
| 124 | + |
| 125 | + return_value = init_mlu(mlu_ctx); |
| 126 | + |
| 127 | + if (return_value) { |
| 128 | + fprintf(stderr, "Couldn't initialize mlu device : %d\n", return_value); |
| 129 | + return FAILURE; |
| 130 | + } |
| 131 | + |
| 132 | + return SUCCESS; |
| 133 | +} |
| 134 | + |
| 135 | +int mlu_memory_destroy(struct memory_ctx *ctx) { |
| 136 | + struct mlu_memory_ctx *mlu_ctx = container_of(ctx, struct mlu_memory_ctx, base); |
| 137 | + |
| 138 | + free_mlu(mlu_ctx); |
| 139 | + free(mlu_ctx); |
| 140 | + return SUCCESS; |
| 141 | +} |
| 142 | + |
| 143 | +int mlu_memory_allocate_buffer(struct memory_ctx *ctx, int alignment, uint64_t size, int *dmabuf_fd, |
| 144 | + uint64_t *dmabuf_offset, void **addr, bool *can_init) { |
| 145 | + CNresult error; |
| 146 | + size_t buf_size = (size + ACCEL_PAGE_SIZE - 1) & ~(ACCEL_PAGE_SIZE - 1); |
| 147 | + |
| 148 | + CNaddr mlu_addr; |
| 149 | + printf("cnMalloc() of a %lu bytes MLU buffer\n", size); |
| 150 | + |
| 151 | + error = cnMalloc(&mlu_addr, buf_size); |
| 152 | + if (error != CN_SUCCESS) { |
| 153 | + printf("cnMalloc error=%d\n", error); |
| 154 | + return FAILURE; |
| 155 | + } |
| 156 | + |
| 157 | + printf("allocated %lu bytes of MLU buffer at %ld\n", (unsigned long)buf_size, mlu_addr); |
| 158 | + *addr = (void *)mlu_addr; |
| 159 | + *can_init = false; |
| 160 | + return SUCCESS; |
| 161 | +} |
| 162 | + |
| 163 | +int mlu_memory_free_buffer(struct memory_ctx *ctx, int dmabuf_fd, void *addr, uint64_t size) |
| 164 | +{ |
| 165 | + CNaddr mlu_addr = (CNaddr)addr; |
| 166 | + printf("deallocating MLU buffer %016lx\n", mlu_addr); |
| 167 | + cnFree(mlu_addr); |
| 168 | + return SUCCESS; |
| 169 | +} |
| 170 | + |
| 171 | +void *mlu_memory_copy_host_buffer(void *dest, const void *src, size_t size) { |
| 172 | + cnMemcpy((CNaddr) dest, (CNaddr) src, size); |
| 173 | + return dest; |
| 174 | +} |
| 175 | + |
| 176 | +void *mlu_memory_copy_buffer_to_buffer(void *dest, const void *src, size_t size) { |
| 177 | + cnMemcpyDtoD((CNaddr) dest, (CNaddr) src, size); |
| 178 | + return dest; |
| 179 | +} |
| 180 | + |
| 181 | +bool mlu_memory_supported() { |
| 182 | + return true; |
| 183 | +} |
| 184 | + |
| 185 | +struct memory_ctx *mlu_memory_create(struct perftest_parameters *params) { |
| 186 | + struct mlu_memory_ctx *ctx; |
| 187 | + |
| 188 | + ALLOCATE(ctx, struct mlu_memory_ctx, 1); |
| 189 | + ctx->base.init = mlu_memory_init; |
| 190 | + ctx->base.destroy = mlu_memory_destroy; |
| 191 | + ctx->base.allocate_buffer = mlu_memory_allocate_buffer; |
| 192 | + ctx->base.free_buffer = mlu_memory_free_buffer; |
| 193 | + ctx->base.copy_host_to_buffer = mlu_memory_copy_host_buffer; |
| 194 | + ctx->base.copy_buffer_to_host = mlu_memory_copy_host_buffer; |
| 195 | + ctx->base.copy_buffer_to_buffer = mlu_memory_copy_buffer_to_buffer; |
| 196 | + ctx->device_id = params->mlu_device_id; |
| 197 | + |
| 198 | + return &ctx->base; |
| 199 | +} |
0 commit comments