1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014-2017 Broadcom
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/**
25bf215546Sopenharmony_ci * @file v3d_simulator.c
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci * Implements V3D simulation on top of a non-V3D GEM fd.
28bf215546Sopenharmony_ci *
29bf215546Sopenharmony_ci * This file's goal is to emulate the V3D ioctls' behavior in the kernel on
30bf215546Sopenharmony_ci * top of the simpenrose software simulator.  Generally, V3D driver BOs have a
31bf215546Sopenharmony_ci * GEM-side copy of their contents and a simulator-side memory area that the
32bf215546Sopenharmony_ci * GEM contents get copied into during simulation.  Once simulation is done,
33bf215546Sopenharmony_ci * the simulator's data is copied back out to the GEM BOs, so that rendering
34bf215546Sopenharmony_ci * appears on the screen as if actual hardware rendering had been done.
35bf215546Sopenharmony_ci *
36bf215546Sopenharmony_ci * One of the limitations of this code is that we shouldn't really need a
37bf215546Sopenharmony_ci * GEM-side BO for non-window-system BOs.  However, do we need unique BO
38bf215546Sopenharmony_ci * handles for each of our GEM bos so that this file can look up its state
39bf215546Sopenharmony_ci * from the handle passed in at submit ioctl time (also, a couple of places
40bf215546Sopenharmony_ci * outside of this file still call ioctls directly on the fd).
41bf215546Sopenharmony_ci *
42bf215546Sopenharmony_ci * Another limitation is that BO import doesn't work unless the underlying
43bf215546Sopenharmony_ci * window system's BO size matches what V3D is going to use, which of course
44bf215546Sopenharmony_ci * doesn't work out in practice.  This means that for now, only DRI3 (V3D
45bf215546Sopenharmony_ci * makes the winsys BOs) is supported, not DRI2 (window system makes the winys
46bf215546Sopenharmony_ci * BOs).
47bf215546Sopenharmony_ci */
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#ifdef USE_V3D_SIMULATOR
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#include <stdio.h>
52bf215546Sopenharmony_ci#include <sys/mman.h>
53bf215546Sopenharmony_ci#include "c11/threads.h"
54bf215546Sopenharmony_ci#include "util/hash_table.h"
55bf215546Sopenharmony_ci#include "util/ralloc.h"
56bf215546Sopenharmony_ci#include "util/set.h"
57bf215546Sopenharmony_ci#include "util/u_dynarray.h"
58bf215546Sopenharmony_ci#include "util/u_memory.h"
59bf215546Sopenharmony_ci#include "util/u_mm.h"
60bf215546Sopenharmony_ci#include "util/u_math.h"
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci#include <xf86drm.h>
63bf215546Sopenharmony_ci#include "drm-uapi/amdgpu_drm.h"
64bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h"
65bf215546Sopenharmony_ci#include "drm-uapi/v3d_drm.h"
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci#include "v3d_simulator.h"
68bf215546Sopenharmony_ci#include "v3d_simulator_wrapper.h"
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci/** Global (across GEM fds) state for the simulator */
71bf215546Sopenharmony_cistatic struct v3d_simulator_state {
72bf215546Sopenharmony_ci        mtx_t mutex;
73bf215546Sopenharmony_ci        mtx_t submit_lock;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci        struct v3d_hw *v3d;
76bf215546Sopenharmony_ci        int ver;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci        /* Base virtual address of the heap. */
79bf215546Sopenharmony_ci        void *mem;
80bf215546Sopenharmony_ci        /* Base hardware address of the heap. */
81bf215546Sopenharmony_ci        uint32_t mem_base;
82bf215546Sopenharmony_ci        /* Size of the heap. */
83bf215546Sopenharmony_ci        uint32_t mem_size;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci        struct mem_block *heap;
86bf215546Sopenharmony_ci        struct mem_block *overflow;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci        /** Mapping from GEM fd to struct v3d_simulator_file * */
89bf215546Sopenharmony_ci        struct hash_table *fd_map;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci        /** Last performance monitor ID. */
92bf215546Sopenharmony_ci        uint32_t last_perfid;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci        struct util_dynarray bin_oom;
95bf215546Sopenharmony_ci        int refcount;
96bf215546Sopenharmony_ci} sim_state = {
97bf215546Sopenharmony_ci        .mutex = _MTX_INITIALIZER_NP,
98bf215546Sopenharmony_ci};
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cienum gem_type {
101bf215546Sopenharmony_ci        GEM_I915,
102bf215546Sopenharmony_ci        GEM_AMDGPU,
103bf215546Sopenharmony_ci        GEM_DUMB
104bf215546Sopenharmony_ci};
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci/** Per-GEM-fd state for the simulator. */
107bf215546Sopenharmony_cistruct v3d_simulator_file {
108bf215546Sopenharmony_ci        int fd;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci        /** Mapping from GEM handle to struct v3d_simulator_bo * */
111bf215546Sopenharmony_ci        struct hash_table *bo_map;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci        /** Dynamic array with performance monitors */
114bf215546Sopenharmony_ci        struct v3d_simulator_perfmon **perfmons;
115bf215546Sopenharmony_ci        uint32_t perfmons_size;
116bf215546Sopenharmony_ci        uint32_t active_perfid;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci        struct mem_block *gmp;
119bf215546Sopenharmony_ci        void *gmp_vaddr;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci        /** For specific gpus, use their create ioctl. Otherwise use dumb bo. */
122bf215546Sopenharmony_ci        enum gem_type gem_type;
123bf215546Sopenharmony_ci};
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci/** Wrapper for drm_v3d_bo tracking the simulator-specific state. */
126bf215546Sopenharmony_cistruct v3d_simulator_bo {
127bf215546Sopenharmony_ci        struct v3d_simulator_file *file;
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci        /** Area for this BO within sim_state->mem */
130bf215546Sopenharmony_ci        struct mem_block *block;
131bf215546Sopenharmony_ci        uint32_t size;
132bf215546Sopenharmony_ci        uint64_t mmap_offset;
133bf215546Sopenharmony_ci        void *sim_vaddr;
134bf215546Sopenharmony_ci        void *gem_vaddr;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci        int handle;
137bf215546Sopenharmony_ci};
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cistruct v3d_simulator_perfmon {
140bf215546Sopenharmony_ci        uint32_t ncounters;
141bf215546Sopenharmony_ci        uint8_t counters[DRM_V3D_MAX_PERF_COUNTERS];
142bf215546Sopenharmony_ci        uint64_t values[DRM_V3D_MAX_PERF_COUNTERS];
143bf215546Sopenharmony_ci};
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cistatic void *
146bf215546Sopenharmony_ciint_to_key(int key)
147bf215546Sopenharmony_ci{
148bf215546Sopenharmony_ci        return (void *)(uintptr_t)key;
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci#define PERFMONS_ALLOC_SIZE 100
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_cistatic uint32_t
154bf215546Sopenharmony_ciperfmons_next_id(struct v3d_simulator_file *sim_file) {
155bf215546Sopenharmony_ci        sim_state.last_perfid++;
156bf215546Sopenharmony_ci        if (sim_state.last_perfid > sim_file->perfmons_size) {
157bf215546Sopenharmony_ci                sim_file->perfmons_size += PERFMONS_ALLOC_SIZE;
158bf215546Sopenharmony_ci                sim_file->perfmons = reralloc(sim_file,
159bf215546Sopenharmony_ci                                              sim_file->perfmons,
160bf215546Sopenharmony_ci                                              struct v3d_simulator_perfmon *,
161bf215546Sopenharmony_ci                                              sim_file->perfmons_size);
162bf215546Sopenharmony_ci        }
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci        return sim_state.last_perfid;
165bf215546Sopenharmony_ci}
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_cistatic struct v3d_simulator_file *
168bf215546Sopenharmony_civ3d_get_simulator_file_for_fd(int fd)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci        struct hash_entry *entry = _mesa_hash_table_search(sim_state.fd_map,
171bf215546Sopenharmony_ci                                                           int_to_key(fd + 1));
172bf215546Sopenharmony_ci        return entry ? entry->data : NULL;
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci/* A marker placed just after each BO, then checked after rendering to make
176bf215546Sopenharmony_ci * sure it's still there.
177bf215546Sopenharmony_ci */
178bf215546Sopenharmony_ci#define BO_SENTINEL		0xfedcba98
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci/* 128kb */
181bf215546Sopenharmony_ci#define GMP_ALIGN2		17
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci/**
184bf215546Sopenharmony_ci * Sets the range of GPU virtual address space to have the given GMP
185bf215546Sopenharmony_ci * permissions (bit 0 = read, bit 1 = write, write-only forbidden).
186bf215546Sopenharmony_ci */
187bf215546Sopenharmony_cistatic void
188bf215546Sopenharmony_ciset_gmp_flags(struct v3d_simulator_file *file,
189bf215546Sopenharmony_ci              uint32_t offset, uint32_t size, uint32_t flag)
190bf215546Sopenharmony_ci{
191bf215546Sopenharmony_ci        assert((offset & ((1 << GMP_ALIGN2) - 1)) == 0);
192bf215546Sopenharmony_ci        int gmp_offset = offset >> GMP_ALIGN2;
193bf215546Sopenharmony_ci        int gmp_count = align(size, 1 << GMP_ALIGN2) >> GMP_ALIGN2;
194bf215546Sopenharmony_ci        uint32_t *gmp = file->gmp_vaddr;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci        assert(flag <= 0x3);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci        for (int i = gmp_offset; i < gmp_offset + gmp_count; i++) {
199bf215546Sopenharmony_ci                int32_t bitshift = (i % 16) * 2;
200bf215546Sopenharmony_ci                gmp[i / 16] &= ~(0x3 << bitshift);
201bf215546Sopenharmony_ci                gmp[i / 16] |= flag << bitshift;
202bf215546Sopenharmony_ci        }
203bf215546Sopenharmony_ci}
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci/**
206bf215546Sopenharmony_ci * Allocates space in simulator memory and returns a tracking struct for it
207bf215546Sopenharmony_ci * that also contains the drm_gem_cma_object struct.
208bf215546Sopenharmony_ci */
209bf215546Sopenharmony_cistatic struct v3d_simulator_bo *
210bf215546Sopenharmony_civ3d_create_simulator_bo(int fd, unsigned size)
211bf215546Sopenharmony_ci{
212bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
213bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = rzalloc(file,
214bf215546Sopenharmony_ci                                                  struct v3d_simulator_bo);
215bf215546Sopenharmony_ci        size = align(size, 4096);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci        sim_bo->file = file;
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
220bf215546Sopenharmony_ci        sim_bo->block = u_mmAllocMem(sim_state.heap, size + 4, GMP_ALIGN2, 0);
221bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
222bf215546Sopenharmony_ci        assert(sim_bo->block);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci        set_gmp_flags(file, sim_bo->block->ofs, size, 0x3);
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci        sim_bo->size = size;
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci        /* Allocate space for the buffer in simulator memory. */
229bf215546Sopenharmony_ci        sim_bo->sim_vaddr = sim_state.mem + sim_bo->block->ofs - sim_state.mem_base;
230bf215546Sopenharmony_ci        memset(sim_bo->sim_vaddr, 0xd0, size);
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci        *(uint32_t *)(sim_bo->sim_vaddr + sim_bo->size) = BO_SENTINEL;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci        return sim_bo;
235bf215546Sopenharmony_ci}
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_cistatic struct v3d_simulator_bo *
238bf215546Sopenharmony_civ3d_create_simulator_bo_for_gem(int fd, int handle, unsigned size)
239bf215546Sopenharmony_ci{
240bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
241bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo =
242bf215546Sopenharmony_ci                v3d_create_simulator_bo(fd, size);
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci        sim_bo->handle = handle;
245bf215546Sopenharmony_ci
246bf215546Sopenharmony_ci        /* Map the GEM buffer for copy in/out to the simulator.  i915 blocks
247bf215546Sopenharmony_ci         * dumb mmap on render nodes, so use their ioctl directly if we're on
248bf215546Sopenharmony_ci         * one.
249bf215546Sopenharmony_ci         */
250bf215546Sopenharmony_ci        int ret;
251bf215546Sopenharmony_ci        switch (file->gem_type) {
252bf215546Sopenharmony_ci        case GEM_I915:
253bf215546Sopenharmony_ci        {
254bf215546Sopenharmony_ci                struct drm_i915_gem_mmap_gtt map = {
255bf215546Sopenharmony_ci                        .handle = handle,
256bf215546Sopenharmony_ci                };
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci                /* We could potentially use non-gtt (cached) for LLC systems,
259bf215546Sopenharmony_ci                 * but the copy-in/out won't be the limiting factor on
260bf215546Sopenharmony_ci                 * simulation anyway.
261bf215546Sopenharmony_ci                 */
262bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &map);
263bf215546Sopenharmony_ci                sim_bo->mmap_offset = map.offset;
264bf215546Sopenharmony_ci                break;
265bf215546Sopenharmony_ci        }
266bf215546Sopenharmony_ci        case GEM_AMDGPU:
267bf215546Sopenharmony_ci        {
268bf215546Sopenharmony_ci                union drm_amdgpu_gem_mmap map = { 0 };
269bf215546Sopenharmony_ci                map.in.handle = handle;
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &map);
272bf215546Sopenharmony_ci                sim_bo->mmap_offset = map.out.addr_ptr;
273bf215546Sopenharmony_ci                break;
274bf215546Sopenharmony_ci        }
275bf215546Sopenharmony_ci        default:
276bf215546Sopenharmony_ci        {
277bf215546Sopenharmony_ci                struct drm_mode_map_dumb map = {
278bf215546Sopenharmony_ci                        .handle = handle,
279bf215546Sopenharmony_ci                };
280bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
281bf215546Sopenharmony_ci                sim_bo->mmap_offset = map.offset;
282bf215546Sopenharmony_ci        }
283bf215546Sopenharmony_ci        }
284bf215546Sopenharmony_ci        if (ret) {
285bf215546Sopenharmony_ci                fprintf(stderr, "Failed to get MMAP offset: %d\n", ret);
286bf215546Sopenharmony_ci                abort();
287bf215546Sopenharmony_ci        }
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_ci        sim_bo->gem_vaddr = mmap(NULL, sim_bo->size,
290bf215546Sopenharmony_ci                                 PROT_READ | PROT_WRITE, MAP_SHARED,
291bf215546Sopenharmony_ci                                 fd, sim_bo->mmap_offset);
292bf215546Sopenharmony_ci        if (sim_bo->gem_vaddr == MAP_FAILED) {
293bf215546Sopenharmony_ci                fprintf(stderr, "mmap of bo %d (offset 0x%016llx, size %d) failed\n",
294bf215546Sopenharmony_ci                        handle, (long long)sim_bo->mmap_offset, sim_bo->size);
295bf215546Sopenharmony_ci                abort();
296bf215546Sopenharmony_ci        }
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci        /* A handle of 0 is used for v3d_gem.c internal allocations that
299bf215546Sopenharmony_ci         * don't need to go in the lookup table.
300bf215546Sopenharmony_ci         */
301bf215546Sopenharmony_ci        if (handle != 0) {
302bf215546Sopenharmony_ci                mtx_lock(&sim_state.mutex);
303bf215546Sopenharmony_ci                _mesa_hash_table_insert(file->bo_map, int_to_key(handle),
304bf215546Sopenharmony_ci                                        sim_bo);
305bf215546Sopenharmony_ci                mtx_unlock(&sim_state.mutex);
306bf215546Sopenharmony_ci        }
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci        return sim_bo;
309bf215546Sopenharmony_ci}
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_cistatic int bin_fd;
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ciuint32_t
314bf215546Sopenharmony_civ3d_simulator_get_spill(uint32_t spill_size)
315bf215546Sopenharmony_ci{
316bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo =
317bf215546Sopenharmony_ci                v3d_create_simulator_bo(bin_fd, spill_size);
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci        util_dynarray_append(&sim_state.bin_oom, struct v3d_simulator_bo *,
320bf215546Sopenharmony_ci                             sim_bo);
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci        return sim_bo->block->ofs;
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cistatic void
326bf215546Sopenharmony_civ3d_free_simulator_bo(struct v3d_simulator_bo *sim_bo)
327bf215546Sopenharmony_ci{
328bf215546Sopenharmony_ci        struct v3d_simulator_file *sim_file = sim_bo->file;
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci        set_gmp_flags(sim_file, sim_bo->block->ofs, sim_bo->size, 0x0);
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci        if (sim_bo->gem_vaddr)
333bf215546Sopenharmony_ci                munmap(sim_bo->gem_vaddr, sim_bo->size);
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
336bf215546Sopenharmony_ci        u_mmFreeMem(sim_bo->block);
337bf215546Sopenharmony_ci        if (sim_bo->handle) {
338bf215546Sopenharmony_ci                _mesa_hash_table_remove_key(sim_file->bo_map,
339bf215546Sopenharmony_ci                                            int_to_key(sim_bo->handle));
340bf215546Sopenharmony_ci        }
341bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
342bf215546Sopenharmony_ci        ralloc_free(sim_bo);
343bf215546Sopenharmony_ci}
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_cistatic struct v3d_simulator_bo *
346bf215546Sopenharmony_civ3d_get_simulator_bo(struct v3d_simulator_file *file, int gem_handle)
347bf215546Sopenharmony_ci{
348bf215546Sopenharmony_ci        if (gem_handle == 0)
349bf215546Sopenharmony_ci                return NULL;
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
352bf215546Sopenharmony_ci        struct hash_entry *entry =
353bf215546Sopenharmony_ci                _mesa_hash_table_search(file->bo_map, int_to_key(gem_handle));
354bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci        return entry ? entry->data : NULL;
357bf215546Sopenharmony_ci}
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_cistatic void
360bf215546Sopenharmony_civ3d_simulator_copy_in_handle(struct v3d_simulator_file *file, int handle)
361bf215546Sopenharmony_ci{
362bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci        if (!sim_bo)
365bf215546Sopenharmony_ci                return;
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci        memcpy(sim_bo->sim_vaddr, sim_bo->gem_vaddr, sim_bo->size);
368bf215546Sopenharmony_ci}
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_cistatic void
371bf215546Sopenharmony_civ3d_simulator_copy_out_handle(struct v3d_simulator_file *file, int handle)
372bf215546Sopenharmony_ci{
373bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file, handle);
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_ci        if (!sim_bo)
376bf215546Sopenharmony_ci                return;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci        memcpy(sim_bo->gem_vaddr, sim_bo->sim_vaddr, sim_bo->size);
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci        if (*(uint32_t *)(sim_bo->sim_vaddr +
381bf215546Sopenharmony_ci                          sim_bo->size) != BO_SENTINEL) {
382bf215546Sopenharmony_ci                fprintf(stderr, "Buffer overflow in handle %d\n",
383bf215546Sopenharmony_ci                        handle);
384bf215546Sopenharmony_ci        }
385bf215546Sopenharmony_ci}
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_cistatic int
388bf215546Sopenharmony_civ3d_simulator_pin_bos(struct v3d_simulator_file *file,
389bf215546Sopenharmony_ci                      struct drm_v3d_submit_cl *submit)
390bf215546Sopenharmony_ci{
391bf215546Sopenharmony_ci        uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci        for (int i = 0; i < submit->bo_handle_count; i++)
394bf215546Sopenharmony_ci                v3d_simulator_copy_in_handle(file, bo_handles[i]);
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci        return 0;
397bf215546Sopenharmony_ci}
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_cistatic int
400bf215546Sopenharmony_civ3d_simulator_unpin_bos(struct v3d_simulator_file *file,
401bf215546Sopenharmony_ci                        struct drm_v3d_submit_cl *submit)
402bf215546Sopenharmony_ci{
403bf215546Sopenharmony_ci        uint32_t *bo_handles = (uint32_t *)(uintptr_t)submit->bo_handles;
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci        for (int i = 0; i < submit->bo_handle_count; i++)
406bf215546Sopenharmony_ci                v3d_simulator_copy_out_handle(file, bo_handles[i]);
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_ci        return 0;
409bf215546Sopenharmony_ci}
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_cistatic struct v3d_simulator_perfmon *
412bf215546Sopenharmony_civ3d_get_simulator_perfmon(int fd, uint32_t perfid)
413bf215546Sopenharmony_ci{
414bf215546Sopenharmony_ci        if (!perfid || perfid > sim_state.last_perfid)
415bf215546Sopenharmony_ci                return NULL;
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
420bf215546Sopenharmony_ci        assert(perfid <= file->perfmons_size);
421bf215546Sopenharmony_ci        struct v3d_simulator_perfmon *perfmon = file->perfmons[perfid - 1];
422bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci        return perfmon;
425bf215546Sopenharmony_ci}
426bf215546Sopenharmony_ci
427bf215546Sopenharmony_cistatic void
428bf215546Sopenharmony_civ3d_simulator_perfmon_switch(int fd, uint32_t perfid)
429bf215546Sopenharmony_ci{
430bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
431bf215546Sopenharmony_ci        struct v3d_simulator_perfmon *perfmon;
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci        if (perfid == file->active_perfid)
434bf215546Sopenharmony_ci                return;
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_ci        perfmon = v3d_get_simulator_perfmon(fd, file->active_perfid);
437bf215546Sopenharmony_ci        if (perfmon)
438bf215546Sopenharmony_ci                v3d41_simulator_perfmon_stop(sim_state.v3d,
439bf215546Sopenharmony_ci                                             perfmon->ncounters,
440bf215546Sopenharmony_ci                                             perfmon->values);
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_ci        perfmon = v3d_get_simulator_perfmon(fd, perfid);
443bf215546Sopenharmony_ci        if (perfmon)
444bf215546Sopenharmony_ci                v3d41_simulator_perfmon_start(sim_state.v3d,
445bf215546Sopenharmony_ci                                              perfmon->ncounters,
446bf215546Sopenharmony_ci                                              perfmon->counters);
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci        file->active_perfid = perfid;
449bf215546Sopenharmony_ci}
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_cistatic int
452bf215546Sopenharmony_civ3d_simulator_signal_syncobjs(int fd, struct drm_v3d_multi_sync *ms)
453bf215546Sopenharmony_ci{
454bf215546Sopenharmony_ci        struct drm_v3d_sem *out_syncs = (void *)(uintptr_t)ms->out_syncs;
455bf215546Sopenharmony_ci        int n_syncobjs = ms->out_sync_count;
456bf215546Sopenharmony_ci        uint32_t syncobjs[n_syncobjs];
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci        for (int i = 0; i < n_syncobjs; i++)
459bf215546Sopenharmony_ci                syncobjs[i] = out_syncs[i].handle;
460bf215546Sopenharmony_ci        return drmSyncobjSignal(fd, (uint32_t *) &syncobjs, n_syncobjs);
461bf215546Sopenharmony_ci}
462bf215546Sopenharmony_ci
463bf215546Sopenharmony_cistatic int
464bf215546Sopenharmony_civ3d_simulator_process_post_deps(int fd, struct drm_v3d_extension *ext)
465bf215546Sopenharmony_ci{
466bf215546Sopenharmony_ci        int ret = 0;
467bf215546Sopenharmony_ci        while (ext && ext->id != DRM_V3D_EXT_ID_MULTI_SYNC)
468bf215546Sopenharmony_ci                ext = (void *)(uintptr_t) ext->next;
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci        if (ext) {
471bf215546Sopenharmony_ci                struct drm_v3d_multi_sync *ms = (struct drm_v3d_multi_sync *) ext;
472bf215546Sopenharmony_ci                ret = v3d_simulator_signal_syncobjs(fd, ms);
473bf215546Sopenharmony_ci        }
474bf215546Sopenharmony_ci        return ret;
475bf215546Sopenharmony_ci}
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_cistatic int
478bf215546Sopenharmony_civ3d_simulator_submit_cl_ioctl(int fd, struct drm_v3d_submit_cl *submit)
479bf215546Sopenharmony_ci{
480bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
481bf215546Sopenharmony_ci        int ret;
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci        ret = v3d_simulator_pin_bos(file, submit);
484bf215546Sopenharmony_ci        if (ret)
485bf215546Sopenharmony_ci                return ret;
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci        mtx_lock(&sim_state.submit_lock);
488bf215546Sopenharmony_ci        bin_fd = fd;
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci        v3d_simulator_perfmon_switch(fd, submit->perfmon_id);
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci        if (sim_state.ver >= 41)
493bf215546Sopenharmony_ci                v3d41_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
494bf215546Sopenharmony_ci        else
495bf215546Sopenharmony_ci                v3d33_simulator_submit_cl_ioctl(sim_state.v3d, submit, file->gmp->ofs);
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci        util_dynarray_foreach(&sim_state.bin_oom, struct v3d_simulator_bo *,
498bf215546Sopenharmony_ci                              sim_bo) {
499bf215546Sopenharmony_ci                v3d_free_simulator_bo(*sim_bo);
500bf215546Sopenharmony_ci        }
501bf215546Sopenharmony_ci        util_dynarray_clear(&sim_state.bin_oom);
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci        mtx_unlock(&sim_state.submit_lock);
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_ci        ret = v3d_simulator_unpin_bos(file, submit);
506bf215546Sopenharmony_ci        if (ret)
507bf215546Sopenharmony_ci                return ret;
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci        if (submit->flags & DRM_V3D_SUBMIT_EXTENSION) {
510bf215546Sopenharmony_ci                struct drm_v3d_extension *ext = (void *)(uintptr_t)submit->extensions;
511bf215546Sopenharmony_ci                ret = v3d_simulator_process_post_deps(fd, ext);
512bf215546Sopenharmony_ci        }
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci        return ret;
515bf215546Sopenharmony_ci}
516bf215546Sopenharmony_ci
517bf215546Sopenharmony_ci/**
518bf215546Sopenharmony_ci * Do fixups after a BO has been opened from a handle.
519bf215546Sopenharmony_ci *
520bf215546Sopenharmony_ci * This could be done at DRM_IOCTL_GEM_OPEN/DRM_IOCTL_GEM_PRIME_FD_TO_HANDLE
521bf215546Sopenharmony_ci * time, but we're still using drmPrimeFDToHandle() so we have this helper to
522bf215546Sopenharmony_ci * be called afterward instead.
523bf215546Sopenharmony_ci */
524bf215546Sopenharmony_civoid v3d_simulator_open_from_handle(int fd, int handle, uint32_t size)
525bf215546Sopenharmony_ci{
526bf215546Sopenharmony_ci        v3d_create_simulator_bo_for_gem(fd, handle, size);
527bf215546Sopenharmony_ci}
528bf215546Sopenharmony_ci
529bf215546Sopenharmony_ci/**
530bf215546Sopenharmony_ci * Simulated ioctl(fd, DRM_V3D_CREATE_BO) implementation.
531bf215546Sopenharmony_ci *
532bf215546Sopenharmony_ci * Making a V3D BO is just a matter of making a corresponding BO on the host.
533bf215546Sopenharmony_ci */
534bf215546Sopenharmony_cistatic int
535bf215546Sopenharmony_civ3d_simulator_create_bo_ioctl(int fd, struct drm_v3d_create_bo *args)
536bf215546Sopenharmony_ci{
537bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
538bf215546Sopenharmony_ci
539bf215546Sopenharmony_ci        /* i915 bans dumb create on render nodes, so we have to use their
540bf215546Sopenharmony_ci         * native ioctl in case we're on a render node.
541bf215546Sopenharmony_ci         */
542bf215546Sopenharmony_ci        int ret;
543bf215546Sopenharmony_ci        switch (file->gem_type) {
544bf215546Sopenharmony_ci        case GEM_I915:
545bf215546Sopenharmony_ci        {
546bf215546Sopenharmony_ci                struct drm_i915_gem_create create = {
547bf215546Sopenharmony_ci                        .size = args->size,
548bf215546Sopenharmony_ci                };
549bf215546Sopenharmony_ci
550bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci                args->handle = create.handle;
553bf215546Sopenharmony_ci                break;
554bf215546Sopenharmony_ci        }
555bf215546Sopenharmony_ci        case GEM_AMDGPU:
556bf215546Sopenharmony_ci        {
557bf215546Sopenharmony_ci                union drm_amdgpu_gem_create create = { 0 };
558bf215546Sopenharmony_ci                create.in.bo_size = args->size;
559bf215546Sopenharmony_ci
560bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_CREATE, &create);
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci                args->handle = create.out.handle;
563bf215546Sopenharmony_ci                break;
564bf215546Sopenharmony_ci        }
565bf215546Sopenharmony_ci        default:
566bf215546Sopenharmony_ci        {
567bf215546Sopenharmony_ci                struct drm_mode_create_dumb create = {
568bf215546Sopenharmony_ci                        .width = 128,
569bf215546Sopenharmony_ci                        .bpp = 8,
570bf215546Sopenharmony_ci                        .height = (args->size + 127) / 128,
571bf215546Sopenharmony_ci                };
572bf215546Sopenharmony_ci
573bf215546Sopenharmony_ci                ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
574bf215546Sopenharmony_ci                assert(ret != 0 || create.size >= args->size);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci                args->handle = create.handle;
577bf215546Sopenharmony_ci        }
578bf215546Sopenharmony_ci        }
579bf215546Sopenharmony_ci        if (ret == 0) {
580bf215546Sopenharmony_ci                struct v3d_simulator_bo *sim_bo =
581bf215546Sopenharmony_ci                        v3d_create_simulator_bo_for_gem(fd, args->handle,
582bf215546Sopenharmony_ci                                                        args->size);
583bf215546Sopenharmony_ci
584bf215546Sopenharmony_ci                args->offset = sim_bo->block->ofs;
585bf215546Sopenharmony_ci        }
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci        return ret;
588bf215546Sopenharmony_ci}
589bf215546Sopenharmony_ci
590bf215546Sopenharmony_ci/**
591bf215546Sopenharmony_ci * Simulated ioctl(fd, DRM_V3D_MMAP_BO) implementation.
592bf215546Sopenharmony_ci *
593bf215546Sopenharmony_ci * We've already grabbed the mmap offset when we created the sim bo, so just
594bf215546Sopenharmony_ci * return it.
595bf215546Sopenharmony_ci */
596bf215546Sopenharmony_cistatic int
597bf215546Sopenharmony_civ3d_simulator_mmap_bo_ioctl(int fd, struct drm_v3d_mmap_bo *args)
598bf215546Sopenharmony_ci{
599bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
600bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
601bf215546Sopenharmony_ci                                                               args->handle);
602bf215546Sopenharmony_ci
603bf215546Sopenharmony_ci        args->offset = sim_bo->mmap_offset;
604bf215546Sopenharmony_ci
605bf215546Sopenharmony_ci        return 0;
606bf215546Sopenharmony_ci}
607bf215546Sopenharmony_ci
608bf215546Sopenharmony_cistatic int
609bf215546Sopenharmony_civ3d_simulator_get_bo_offset_ioctl(int fd, struct drm_v3d_get_bo_offset *args)
610bf215546Sopenharmony_ci{
611bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
612bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
613bf215546Sopenharmony_ci                                                               args->handle);
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci        args->offset = sim_bo->block->ofs;
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci        return 0;
618bf215546Sopenharmony_ci}
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_cistatic int
621bf215546Sopenharmony_civ3d_simulator_gem_close_ioctl(int fd, struct drm_gem_close *args)
622bf215546Sopenharmony_ci{
623bf215546Sopenharmony_ci        /* Free the simulator's internal tracking. */
624bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
625bf215546Sopenharmony_ci        struct v3d_simulator_bo *sim_bo = v3d_get_simulator_bo(file,
626bf215546Sopenharmony_ci                                                               args->handle);
627bf215546Sopenharmony_ci
628bf215546Sopenharmony_ci        v3d_free_simulator_bo(sim_bo);
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci        /* Pass the call on down. */
631bf215546Sopenharmony_ci        return drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, args);
632bf215546Sopenharmony_ci}
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_cistatic int
635bf215546Sopenharmony_civ3d_simulator_get_param_ioctl(int fd, struct drm_v3d_get_param *args)
636bf215546Sopenharmony_ci{
637bf215546Sopenharmony_ci        if (sim_state.ver >= 41)
638bf215546Sopenharmony_ci                return v3d41_simulator_get_param_ioctl(sim_state.v3d, args);
639bf215546Sopenharmony_ci        else
640bf215546Sopenharmony_ci                return v3d33_simulator_get_param_ioctl(sim_state.v3d, args);
641bf215546Sopenharmony_ci}
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_cistatic int
644bf215546Sopenharmony_civ3d_simulator_submit_tfu_ioctl(int fd, struct drm_v3d_submit_tfu *args)
645bf215546Sopenharmony_ci{
646bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
647bf215546Sopenharmony_ci        int ret;
648bf215546Sopenharmony_ci
649bf215546Sopenharmony_ci        v3d_simulator_copy_in_handle(file, args->bo_handles[0]);
650bf215546Sopenharmony_ci        v3d_simulator_copy_in_handle(file, args->bo_handles[1]);
651bf215546Sopenharmony_ci        v3d_simulator_copy_in_handle(file, args->bo_handles[2]);
652bf215546Sopenharmony_ci        v3d_simulator_copy_in_handle(file, args->bo_handles[3]);
653bf215546Sopenharmony_ci
654bf215546Sopenharmony_ci        if (sim_state.ver >= 41)
655bf215546Sopenharmony_ci                ret = v3d41_simulator_submit_tfu_ioctl(sim_state.v3d, args);
656bf215546Sopenharmony_ci        else
657bf215546Sopenharmony_ci                ret = v3d33_simulator_submit_tfu_ioctl(sim_state.v3d, args);
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_ci        v3d_simulator_copy_out_handle(file, args->bo_handles[0]);
660bf215546Sopenharmony_ci
661bf215546Sopenharmony_ci        if (ret)
662bf215546Sopenharmony_ci                return ret;
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci        if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
665bf215546Sopenharmony_ci                struct drm_v3d_extension *ext = (void *)(uintptr_t)args->extensions;
666bf215546Sopenharmony_ci                ret = v3d_simulator_process_post_deps(fd, ext);
667bf215546Sopenharmony_ci        }
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci        return ret;
670bf215546Sopenharmony_ci}
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_cistatic int
673bf215546Sopenharmony_civ3d_simulator_submit_csd_ioctl(int fd, struct drm_v3d_submit_csd *args)
674bf215546Sopenharmony_ci{
675bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
676bf215546Sopenharmony_ci        uint32_t *bo_handles = (uint32_t *)(uintptr_t)args->bo_handles;
677bf215546Sopenharmony_ci        int ret;
678bf215546Sopenharmony_ci
679bf215546Sopenharmony_ci        for (int i = 0; i < args->bo_handle_count; i++)
680bf215546Sopenharmony_ci                v3d_simulator_copy_in_handle(file, bo_handles[i]);
681bf215546Sopenharmony_ci
682bf215546Sopenharmony_ci        v3d_simulator_perfmon_switch(fd, args->perfmon_id);
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci        if (sim_state.ver >= 41)
685bf215546Sopenharmony_ci                ret = v3d41_simulator_submit_csd_ioctl(sim_state.v3d, args,
686bf215546Sopenharmony_ci                                                       file->gmp->ofs);
687bf215546Sopenharmony_ci        else
688bf215546Sopenharmony_ci                ret = -1;
689bf215546Sopenharmony_ci
690bf215546Sopenharmony_ci        for (int i = 0; i < args->bo_handle_count; i++)
691bf215546Sopenharmony_ci                v3d_simulator_copy_out_handle(file, bo_handles[i]);
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci        if (ret < 0)
694bf215546Sopenharmony_ci                return ret;
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci        if (args->flags & DRM_V3D_SUBMIT_EXTENSION) {
697bf215546Sopenharmony_ci                struct drm_v3d_extension *ext = (void *)(uintptr_t)args->extensions;
698bf215546Sopenharmony_ci                ret = v3d_simulator_process_post_deps(fd, ext);
699bf215546Sopenharmony_ci        }
700bf215546Sopenharmony_ci
701bf215546Sopenharmony_ci        return ret;
702bf215546Sopenharmony_ci}
703bf215546Sopenharmony_ci
704bf215546Sopenharmony_cistatic int
705bf215546Sopenharmony_civ3d_simulator_perfmon_create_ioctl(int fd, struct drm_v3d_perfmon_create *args)
706bf215546Sopenharmony_ci{
707bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_ci        if (args->ncounters == 0 ||
710bf215546Sopenharmony_ci            args->ncounters > DRM_V3D_MAX_PERF_COUNTERS)
711bf215546Sopenharmony_ci                return -EINVAL;
712bf215546Sopenharmony_ci
713bf215546Sopenharmony_ci        struct v3d_simulator_perfmon *perfmon = rzalloc(file,
714bf215546Sopenharmony_ci                                                        struct v3d_simulator_perfmon);
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_ci        perfmon->ncounters = args->ncounters;
717bf215546Sopenharmony_ci        for (int i = 0; i < args->ncounters; i++) {
718bf215546Sopenharmony_ci                if (args->counters[i] >= V3D_PERFCNT_NUM) {
719bf215546Sopenharmony_ci                        ralloc_free(perfmon);
720bf215546Sopenharmony_ci                        return -EINVAL;
721bf215546Sopenharmony_ci                } else {
722bf215546Sopenharmony_ci                        perfmon->counters[i] = args->counters[i];
723bf215546Sopenharmony_ci                }
724bf215546Sopenharmony_ci        }
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
727bf215546Sopenharmony_ci        args->id = perfmons_next_id(file);
728bf215546Sopenharmony_ci        file->perfmons[args->id - 1] = perfmon;
729bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
730bf215546Sopenharmony_ci
731bf215546Sopenharmony_ci        return 0;
732bf215546Sopenharmony_ci}
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_cistatic int
735bf215546Sopenharmony_civ3d_simulator_perfmon_destroy_ioctl(int fd, struct drm_v3d_perfmon_destroy *args)
736bf215546Sopenharmony_ci{
737bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
738bf215546Sopenharmony_ci        struct v3d_simulator_perfmon *perfmon =
739bf215546Sopenharmony_ci                v3d_get_simulator_perfmon(fd, args->id);
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci        if (!perfmon)
742bf215546Sopenharmony_ci                return -EINVAL;
743bf215546Sopenharmony_ci
744bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
745bf215546Sopenharmony_ci        file->perfmons[args->id - 1] = NULL;
746bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_ci        ralloc_free(perfmon);
749bf215546Sopenharmony_ci
750bf215546Sopenharmony_ci        return 0;
751bf215546Sopenharmony_ci}
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_cistatic int
754bf215546Sopenharmony_civ3d_simulator_perfmon_get_values_ioctl(int fd, struct drm_v3d_perfmon_get_values *args)
755bf215546Sopenharmony_ci{
756bf215546Sopenharmony_ci        struct v3d_simulator_file *file = v3d_get_simulator_file_for_fd(fd);
757bf215546Sopenharmony_ci
758bf215546Sopenharmony_ci        mtx_lock(&sim_state.submit_lock);
759bf215546Sopenharmony_ci
760bf215546Sopenharmony_ci        /* Stop the perfmon if it is still active */
761bf215546Sopenharmony_ci        if (args->id == file->active_perfid)
762bf215546Sopenharmony_ci                v3d_simulator_perfmon_switch(fd, 0);
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci        mtx_unlock(&sim_state.submit_lock);
765bf215546Sopenharmony_ci
766bf215546Sopenharmony_ci        struct v3d_simulator_perfmon *perfmon =
767bf215546Sopenharmony_ci                v3d_get_simulator_perfmon(fd, args->id);
768bf215546Sopenharmony_ci
769bf215546Sopenharmony_ci        if (!perfmon)
770bf215546Sopenharmony_ci                return -EINVAL;
771bf215546Sopenharmony_ci
772bf215546Sopenharmony_ci        memcpy((void *)args->values_ptr, perfmon->values, perfmon->ncounters * sizeof(uint64_t));
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci        return 0;
775bf215546Sopenharmony_ci}
776bf215546Sopenharmony_ci
777bf215546Sopenharmony_ciint
778bf215546Sopenharmony_civ3d_simulator_ioctl(int fd, unsigned long request, void *args)
779bf215546Sopenharmony_ci{
780bf215546Sopenharmony_ci        switch (request) {
781bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_SUBMIT_CL:
782bf215546Sopenharmony_ci                return v3d_simulator_submit_cl_ioctl(fd, args);
783bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_CREATE_BO:
784bf215546Sopenharmony_ci                return v3d_simulator_create_bo_ioctl(fd, args);
785bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_MMAP_BO:
786bf215546Sopenharmony_ci                return v3d_simulator_mmap_bo_ioctl(fd, args);
787bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_GET_BO_OFFSET:
788bf215546Sopenharmony_ci                return v3d_simulator_get_bo_offset_ioctl(fd, args);
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_WAIT_BO:
791bf215546Sopenharmony_ci                /* We do all of the v3d rendering synchronously, so we just
792bf215546Sopenharmony_ci                 * return immediately on the wait ioctls.  This ignores any
793bf215546Sopenharmony_ci                 * native rendering to the host BO, so it does mean we race on
794bf215546Sopenharmony_ci                 * front buffer rendering.
795bf215546Sopenharmony_ci                 */
796bf215546Sopenharmony_ci                return 0;
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_GET_PARAM:
799bf215546Sopenharmony_ci                return v3d_simulator_get_param_ioctl(fd, args);
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_ci        case DRM_IOCTL_GEM_CLOSE:
802bf215546Sopenharmony_ci                return v3d_simulator_gem_close_ioctl(fd, args);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_SUBMIT_TFU:
805bf215546Sopenharmony_ci                return v3d_simulator_submit_tfu_ioctl(fd, args);
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_SUBMIT_CSD:
808bf215546Sopenharmony_ci                return v3d_simulator_submit_csd_ioctl(fd, args);
809bf215546Sopenharmony_ci
810bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_PERFMON_CREATE:
811bf215546Sopenharmony_ci                return v3d_simulator_perfmon_create_ioctl(fd, args);
812bf215546Sopenharmony_ci
813bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_PERFMON_DESTROY:
814bf215546Sopenharmony_ci                return v3d_simulator_perfmon_destroy_ioctl(fd, args);
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci        case DRM_IOCTL_V3D_PERFMON_GET_VALUES:
817bf215546Sopenharmony_ci                return v3d_simulator_perfmon_get_values_ioctl(fd, args);
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci        case DRM_IOCTL_GEM_OPEN:
820bf215546Sopenharmony_ci        case DRM_IOCTL_GEM_FLINK:
821bf215546Sopenharmony_ci                return drmIoctl(fd, request, args);
822bf215546Sopenharmony_ci        default:
823bf215546Sopenharmony_ci                fprintf(stderr, "Unknown ioctl 0x%08x\n", (int)request);
824bf215546Sopenharmony_ci                abort();
825bf215546Sopenharmony_ci        }
826bf215546Sopenharmony_ci}
827bf215546Sopenharmony_ci
828bf215546Sopenharmony_ciuint32_t
829bf215546Sopenharmony_civ3d_simulator_get_mem_size(void)
830bf215546Sopenharmony_ci{
831bf215546Sopenharmony_ci   return sim_state.mem_size;
832bf215546Sopenharmony_ci}
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_cistatic void
835bf215546Sopenharmony_civ3d_simulator_init_global()
836bf215546Sopenharmony_ci{
837bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
838bf215546Sopenharmony_ci        if (sim_state.refcount++) {
839bf215546Sopenharmony_ci                mtx_unlock(&sim_state.mutex);
840bf215546Sopenharmony_ci                return;
841bf215546Sopenharmony_ci        }
842bf215546Sopenharmony_ci
843bf215546Sopenharmony_ci        sim_state.v3d = v3d_hw_auto_new(NULL);
844bf215546Sopenharmony_ci        v3d_hw_alloc_mem(sim_state.v3d, 1024 * 1024 * 1024);
845bf215546Sopenharmony_ci        sim_state.mem_base =
846bf215546Sopenharmony_ci                v3d_hw_get_mem(sim_state.v3d, &sim_state.mem_size,
847bf215546Sopenharmony_ci                               &sim_state.mem);
848bf215546Sopenharmony_ci
849bf215546Sopenharmony_ci        /* Allocate from anywhere from 4096 up.  We don't allocate at 0,
850bf215546Sopenharmony_ci         * because for OQs and some other addresses in the HW, 0 means
851bf215546Sopenharmony_ci         * disabled.
852bf215546Sopenharmony_ci         */
853bf215546Sopenharmony_ci        sim_state.heap = u_mmInit(4096, sim_state.mem_size - 4096);
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_ci        /* Make a block of 0xd0 at address 0 to make sure we don't screw up
856bf215546Sopenharmony_ci         * and land there.
857bf215546Sopenharmony_ci         */
858bf215546Sopenharmony_ci        struct mem_block *b = u_mmAllocMem(sim_state.heap, 4096, GMP_ALIGN2, 0);
859bf215546Sopenharmony_ci        memset(sim_state.mem + b->ofs - sim_state.mem_base, 0xd0, 4096);
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_ci        sim_state.ver = v3d_hw_get_version(sim_state.v3d);
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
864bf215546Sopenharmony_ci
865bf215546Sopenharmony_ci        sim_state.fd_map =
866bf215546Sopenharmony_ci                _mesa_hash_table_create(NULL,
867bf215546Sopenharmony_ci                                        _mesa_hash_pointer,
868bf215546Sopenharmony_ci                                        _mesa_key_pointer_equal);
869bf215546Sopenharmony_ci
870bf215546Sopenharmony_ci        util_dynarray_init(&sim_state.bin_oom, NULL);
871bf215546Sopenharmony_ci
872bf215546Sopenharmony_ci        if (sim_state.ver >= 41)
873bf215546Sopenharmony_ci                v3d41_simulator_init_regs(sim_state.v3d);
874bf215546Sopenharmony_ci        else
875bf215546Sopenharmony_ci                v3d33_simulator_init_regs(sim_state.v3d);
876bf215546Sopenharmony_ci}
877bf215546Sopenharmony_ci
878bf215546Sopenharmony_cistruct v3d_simulator_file *
879bf215546Sopenharmony_civ3d_simulator_init(int fd)
880bf215546Sopenharmony_ci{
881bf215546Sopenharmony_ci        v3d_simulator_init_global();
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci        struct v3d_simulator_file *sim_file = rzalloc(NULL, struct v3d_simulator_file);
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_ci        drmVersionPtr version = drmGetVersion(fd);
886bf215546Sopenharmony_ci        if (version && strncmp(version->name, "i915", version->name_len) == 0)
887bf215546Sopenharmony_ci                sim_file->gem_type = GEM_I915;
888bf215546Sopenharmony_ci        else if (version && strncmp(version->name, "amdgpu", version->name_len) == 0)
889bf215546Sopenharmony_ci                sim_file->gem_type = GEM_AMDGPU;
890bf215546Sopenharmony_ci        else
891bf215546Sopenharmony_ci                sim_file->gem_type = GEM_DUMB;
892bf215546Sopenharmony_ci        drmFreeVersion(version);
893bf215546Sopenharmony_ci
894bf215546Sopenharmony_ci        sim_file->bo_map =
895bf215546Sopenharmony_ci                _mesa_hash_table_create(sim_file,
896bf215546Sopenharmony_ci                                        _mesa_hash_pointer,
897bf215546Sopenharmony_ci                                        _mesa_key_pointer_equal);
898bf215546Sopenharmony_ci
899bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
900bf215546Sopenharmony_ci        _mesa_hash_table_insert(sim_state.fd_map, int_to_key(fd + 1),
901bf215546Sopenharmony_ci                                sim_file);
902bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci        sim_file->gmp = u_mmAllocMem(sim_state.heap, 8096, GMP_ALIGN2, 0);
905bf215546Sopenharmony_ci        sim_file->gmp_vaddr = (sim_state.mem + sim_file->gmp->ofs -
906bf215546Sopenharmony_ci                               sim_state.mem_base);
907bf215546Sopenharmony_ci        memset(sim_file->gmp_vaddr, 0, 8096);
908bf215546Sopenharmony_ci
909bf215546Sopenharmony_ci        return sim_file;
910bf215546Sopenharmony_ci}
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_civoid
913bf215546Sopenharmony_civ3d_simulator_destroy(struct v3d_simulator_file *sim_file)
914bf215546Sopenharmony_ci{
915bf215546Sopenharmony_ci        mtx_lock(&sim_state.mutex);
916bf215546Sopenharmony_ci        if (!--sim_state.refcount) {
917bf215546Sopenharmony_ci                _mesa_hash_table_destroy(sim_state.fd_map, NULL);
918bf215546Sopenharmony_ci                util_dynarray_fini(&sim_state.bin_oom);
919bf215546Sopenharmony_ci                u_mmDestroy(sim_state.heap);
920bf215546Sopenharmony_ci                /* No memsetting the struct, because it contains the mutex. */
921bf215546Sopenharmony_ci                sim_state.mem = NULL;
922bf215546Sopenharmony_ci        }
923bf215546Sopenharmony_ci        mtx_unlock(&sim_state.mutex);
924bf215546Sopenharmony_ci        ralloc_free(sim_file);
925bf215546Sopenharmony_ci}
926bf215546Sopenharmony_ci
927bf215546Sopenharmony_ci#endif /* USE_V3D_SIMULATOR */
928