/* * Copyright 2010 Jerome Glisse * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * on the rights to use, copy, modify, merge, publish, distribute, sub * license, and/or sell copies of the Software, and to permit persons to whom * the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice (including the next * paragraph) shall be included in all copies or substantial portions of the * Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. * * Authors: * Jerome Glisse */ #include "r600.h" #include "r600_winsys.h" #pragma pack(1) struct ib_reloc_gem { uint32_t handle; uint32_t read_domain; uint32_t write_domain; uint32_t flags; }; #pragma pack() #define RELOC_SIZE (sizeof(struct ib_reloc_gem) / sizeof(uint32_t)) u32 radeon_ib_reloc(struct radeon_ib *ib, struct radeon_bo *bo, u32 d) { struct ib_reloc_gem *reloc; int i; for (i = 0; i < ib->crelocs; i++) { reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE]; if (reloc->handle == bo->handle) { reloc->read_domain |= d; return (i * RELOC_SIZE); } } if (ib->crelocs >= ib->nrelocs) return 0xFFFFFFFFUL; i = ib->crelocs++; reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE]; reloc->handle = bo->handle; reloc->read_domain = d; reloc->write_domain = 0; reloc->flags = 0; return (i * RELOC_SIZE); } int radeon_ib_get(struct r600_winsys *rdev, struct radeon_ib **ib) { struct radeon_ib *lib; lib = malloc(sizeof(struct radeon_ib)); if (lib == NULL) return -ENOMEM; memset(lib, sizeof(*lib), 0); lib->ptr = malloc(64 * 1024); if (lib->ptr == NULL) { free(lib); return -ENOMEM; } lib->cpkts = 0; lib->length_dw = 64 * 1024 / 4; lib->relocs = malloc(64 * 1024); if (lib->relocs == NULL) { free(lib->ptr); free(lib); return -ENOMEM; } lib->nrelocs = 64 * 1024 / RELOC_SIZE; lib->crelocs = 0; *ib = lib; return 0; } void radeon_ib_free(struct radeon_ib *ib) { if (ib == NULL) return; free(ib->ptr); free(ib->relocs); free(ib); } int radeon_ib_schedule(struct r600_winsys *rdev, struct radeon_ib *ib) { struct drm_radeon_cs drmib; struct drm_radeon_cs_chunk chunks[2]; uint64_t chunk_array[2]; int r = 0; #if 0 for (r = 0; r < ib->cpkts; r++) { printf("0x%08X\n", ib->ptr[r]); } #endif drmib.num_chunks = 2; drmib.chunks = (uint64_t)(uintptr_t)chunk_array; chunks[0].chunk_id = RADEON_CHUNK_ID_IB; chunks[0].length_dw = ib->cpkts; chunks[0].chunk_data = (uint64_t)(uintptr_t)ib->ptr; chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS; chunks[1].length_dw = ib->crelocs * 4; chunks[1].chunk_data = (uint64_t)(uintptr_t)ib->relocs; chunk_array[0] = (uint64_t)(uintptr_t)&chunks[0]; chunk_array[1] = (uint64_t)(uintptr_t)&chunks[1]; #if 1 r = drmCommandWriteRead(rdev->fd, DRM_RADEON_CS, &drmib, sizeof(struct drm_radeon_cs)); #endif return r; } int r600_winsys_init(struct r600_winsys **rdev, struct radeon_bo_manager *bom, int fd) { struct r600_winsys *dev; int r; *rdev = NULL; dev = malloc(sizeof(struct r600_winsys)); if (dev == NULL) return -ENOMEM; memset(dev, 0, sizeof(struct r600_winsys)); dev->fd = fd; dev->bom = bom; r = r600_atoms_init(dev); if (r) return r; *rdev = dev; return r; } void r600_winsys_release(struct r600_winsys *rdev) { memset(rdev, 0, sizeof(struct r600_winsys)); free(rdev); } u64 crc_64(void *d, size_t len) { u8 *data = (uint8_t*)d; u64 div = 0x42F0E1EBA9EA3693; u64 crc = 0xFFFFFFFFFFFFFFFF; u8 t; int i, j; for (j = 0; j < len; j++) { t = data[j]; for (i = 0; i < 8; i++) { if ((t >> 7) ^ (crc >> 63)) { crc = (crc << 1) ^ div; } else { crc = (crc << 1); } t = (t << 1) & 0xFF; } } return crc; } void r600_memcpy_bo(struct radeon_bo *bo, u32 *src, u32 size) { int r; r = radeon_bo_map(bo, 1); if (r) { return; } memcpy(bo->ptr, src, size); radeon_bo_unmap(bo); } void r600_winsys_set_bo_list(struct r600_winsys *rdev, u32 nbo, struct radeon_bo **bo) { memcpy(rdev->bo, bo, sizeof(void*) * nbo); rdev->nbo = nbo; } struct radeon_bo *radeon_bo_lookup(struct r600_winsys *rdev, u32 handle) { int i; for (i = 0; i < rdev->nbo; i++) { if (rdev->bo[i] && rdev->bo[i]->handle == handle) { radeon_bo_ref(rdev->bo[i]); return rdev->bo[i]; } } return NULL; }