1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef INTEL_GEM_H 25bf215546Sopenharmony_ci#define INTEL_GEM_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include <assert.h> 30bf215546Sopenharmony_ci#include <errno.h> 31bf215546Sopenharmony_ci#include <stdbool.h> 32bf215546Sopenharmony_ci#include <stdint.h> 33bf215546Sopenharmony_ci#include <stdlib.h> 34bf215546Sopenharmony_ci#include <unistd.h> 35bf215546Sopenharmony_ci#include <sys/ioctl.h> 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_cistatic inline uint64_t 38bf215546Sopenharmony_ciintel_canonical_address(uint64_t v) 39bf215546Sopenharmony_ci{ 40bf215546Sopenharmony_ci /* From the Broadwell PRM Vol. 2a, MI_LOAD_REGISTER_MEM::MemoryAddress: 41bf215546Sopenharmony_ci * 42bf215546Sopenharmony_ci * "This field specifies the address of the memory location where the 43bf215546Sopenharmony_ci * register value specified in the DWord above will read from. The 44bf215546Sopenharmony_ci * address specifies the DWord location of the data. Range = 45bf215546Sopenharmony_ci * GraphicsVirtualAddress[63:2] for a DWord register GraphicsAddress 46bf215546Sopenharmony_ci * [63:48] are ignored by the HW and assumed to be in correct 47bf215546Sopenharmony_ci * canonical form [63:48] == [47]." 48bf215546Sopenharmony_ci */ 49bf215546Sopenharmony_ci const int shift = 63 - 47; 50bf215546Sopenharmony_ci return (int64_t)(v << shift) >> shift; 51bf215546Sopenharmony_ci} 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci/** 54bf215546Sopenharmony_ci * This returns a 48-bit address with the high 16 bits zeroed. 55bf215546Sopenharmony_ci * 56bf215546Sopenharmony_ci * It's the opposite of intel_canonicalize_address. 57bf215546Sopenharmony_ci */ 58bf215546Sopenharmony_cistatic inline uint64_t 59bf215546Sopenharmony_ciintel_48b_address(uint64_t v) 60bf215546Sopenharmony_ci{ 61bf215546Sopenharmony_ci const int shift = 63 - 47; 62bf215546Sopenharmony_ci return (uint64_t)(v << shift) >> shift; 63bf215546Sopenharmony_ci} 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci/** 66bf215546Sopenharmony_ci * Call ioctl, restarting if it is interrupted 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_cistatic inline int 69bf215546Sopenharmony_ciintel_ioctl(int fd, unsigned long request, void *arg) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci int ret; 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci do { 74bf215546Sopenharmony_ci ret = ioctl(fd, request, arg); 75bf215546Sopenharmony_ci } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); 76bf215546Sopenharmony_ci return ret; 77bf215546Sopenharmony_ci} 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistatic inline uint64_t 80bf215546Sopenharmony_ciintel_read_gpu_timestamp(int fd) 81bf215546Sopenharmony_ci{ 82bf215546Sopenharmony_ci struct drm_i915_reg_read reg_read = {}; 83bf215546Sopenharmony_ci const uint64_t render_ring_timestamp = 0x2358; 84bf215546Sopenharmony_ci reg_read.offset = render_ring_timestamp | I915_REG_READ_8B_WA; 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci if (intel_ioctl(fd, DRM_IOCTL_I915_REG_READ, ®_read) < 0) 87bf215546Sopenharmony_ci return 0; 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci return reg_read.val; 90bf215546Sopenharmony_ci} 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci/** 93bf215546Sopenharmony_ci * A wrapper around DRM_IOCTL_I915_QUERY 94bf215546Sopenharmony_ci * 95bf215546Sopenharmony_ci * Unfortunately, the error semantics of this ioctl are rather annoying so 96bf215546Sopenharmony_ci * it's better to have a common helper. 97bf215546Sopenharmony_ci */ 98bf215546Sopenharmony_cistatic inline int 99bf215546Sopenharmony_ciintel_i915_query_flags(int fd, uint64_t query_id, uint32_t flags, 100bf215546Sopenharmony_ci void *buffer, int32_t *buffer_len) 101bf215546Sopenharmony_ci{ 102bf215546Sopenharmony_ci struct drm_i915_query_item item = { 103bf215546Sopenharmony_ci .query_id = query_id, 104bf215546Sopenharmony_ci .length = *buffer_len, 105bf215546Sopenharmony_ci .flags = flags, 106bf215546Sopenharmony_ci .data_ptr = (uintptr_t)buffer, 107bf215546Sopenharmony_ci }; 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci struct drm_i915_query args = { 110bf215546Sopenharmony_ci .num_items = 1, 111bf215546Sopenharmony_ci .flags = 0, 112bf215546Sopenharmony_ci .items_ptr = (uintptr_t)&item, 113bf215546Sopenharmony_ci }; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci int ret = intel_ioctl(fd, DRM_IOCTL_I915_QUERY, &args); 116bf215546Sopenharmony_ci if (ret != 0) 117bf215546Sopenharmony_ci return -errno; 118bf215546Sopenharmony_ci else if (item.length < 0) 119bf215546Sopenharmony_ci return item.length; 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_ci *buffer_len = item.length; 122bf215546Sopenharmony_ci return 0; 123bf215546Sopenharmony_ci} 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_cistatic inline int 126bf215546Sopenharmony_ciintel_i915_query(int fd, uint64_t query_id, void *buffer, 127bf215546Sopenharmony_ci int32_t *buffer_len) 128bf215546Sopenharmony_ci{ 129bf215546Sopenharmony_ci return intel_i915_query_flags(fd, query_id, 0, buffer, buffer_len); 130bf215546Sopenharmony_ci} 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci/** 133bf215546Sopenharmony_ci * Query for the given data, allocating as needed 134bf215546Sopenharmony_ci * 135bf215546Sopenharmony_ci * The caller is responsible for freeing the returned pointer. 136bf215546Sopenharmony_ci */ 137bf215546Sopenharmony_cistatic inline void * 138bf215546Sopenharmony_ciintel_i915_query_alloc(int fd, uint64_t query_id, int32_t *query_length) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci if (query_length) 141bf215546Sopenharmony_ci *query_length = 0; 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci int32_t length = 0; 144bf215546Sopenharmony_ci int ret = intel_i915_query(fd, query_id, NULL, &length); 145bf215546Sopenharmony_ci if (ret < 0) 146bf215546Sopenharmony_ci return NULL; 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci void *data = calloc(1, length); 149bf215546Sopenharmony_ci assert(data != NULL); /* This shouldn't happen in practice */ 150bf215546Sopenharmony_ci if (data == NULL) 151bf215546Sopenharmony_ci return NULL; 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci ret = intel_i915_query(fd, query_id, data, &length); 154bf215546Sopenharmony_ci assert(ret == 0); /* We should have caught the error above */ 155bf215546Sopenharmony_ci if (ret < 0) { 156bf215546Sopenharmony_ci free(data); 157bf215546Sopenharmony_ci return NULL; 158bf215546Sopenharmony_ci } 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci if (query_length) 161bf215546Sopenharmony_ci *query_length = length; 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci return data; 164bf215546Sopenharmony_ci} 165bf215546Sopenharmony_ci 166bf215546Sopenharmony_cibool intel_gem_supports_syncobj_wait(int fd); 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ciint intel_gem_count_engines(const struct drm_i915_query_engine_info *info, 169bf215546Sopenharmony_ci enum drm_i915_gem_engine_class engine_class); 170bf215546Sopenharmony_ciint intel_gem_create_context_engines(int fd, 171bf215546Sopenharmony_ci const struct drm_i915_query_engine_info *info, 172bf215546Sopenharmony_ci int num_engines, uint16_t *engine_classes); 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci#endif /* INTEL_GEM_H */ 175