2015-04-20 10:04:22 -06:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Advanced Micro Devices, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* 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 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 NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
|
|
|
|
*
|
2015-08-07 10:20:51 -06:00
|
|
|
*/
|
2015-04-20 10:04:22 -06:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-07-08 23:50:36 -06:00
|
|
|
#include <errno.h>
|
2015-04-20 10:04:22 -06:00
|
|
|
#include "amdgpu.h"
|
|
|
|
#include "amdgpu_drm.h"
|
|
|
|
#include "amdgpu_internal.h"
|
|
|
|
#include "util_math.h"
|
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_va_range_query(amdgpu_device_handle dev,
|
|
|
|
enum amdgpu_gpu_va_range type,
|
|
|
|
uint64_t *start, uint64_t *end)
|
2015-07-08 23:53:24 -06:00
|
|
|
{
|
2017-11-02 11:47:34 -06:00
|
|
|
if (type != amdgpu_gpu_va_range_general)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*start = dev->dev_info.virtual_address_offset;
|
|
|
|
*end = dev->dev_info.virtual_address_max;
|
|
|
|
return 0;
|
2015-07-08 23:53:24 -06:00
|
|
|
}
|
|
|
|
|
2015-08-16 21:09:08 -06:00
|
|
|
drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
|
2017-11-02 11:47:34 -06:00
|
|
|
uint64_t max, uint64_t alignment)
|
2015-05-21 03:21:21 -06:00
|
|
|
{
|
2018-02-07 23:52:11 -07:00
|
|
|
struct amdgpu_bo_va_hole *n;
|
|
|
|
|
2015-08-16 21:09:07 -06:00
|
|
|
mgr->va_max = max;
|
|
|
|
mgr->va_alignment = alignment;
|
2015-05-21 03:21:21 -06:00
|
|
|
|
|
|
|
list_inithead(&mgr->va_holes);
|
|
|
|
pthread_mutex_init(&mgr->bo_va_mutex, NULL);
|
2018-02-07 23:52:11 -07:00
|
|
|
pthread_mutex_lock(&mgr->bo_va_mutex);
|
|
|
|
n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
|
2018-02-19 00:18:36 -07:00
|
|
|
n->size = mgr->va_max - start;
|
2018-02-07 23:52:11 -07:00
|
|
|
n->offset = start;
|
|
|
|
list_add(&n->list, &mgr->va_holes);
|
|
|
|
pthread_mutex_unlock(&mgr->bo_va_mutex);
|
2015-05-21 03:21:21 -06:00
|
|
|
}
|
|
|
|
|
2015-08-16 21:09:08 -06:00
|
|
|
drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
|
2015-05-21 03:21:21 -06:00
|
|
|
{
|
2015-10-09 10:07:26 -06:00
|
|
|
struct amdgpu_bo_va_hole *hole, *tmp;
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(hole, tmp, &mgr->va_holes, list) {
|
2015-05-21 03:21:21 -06:00
|
|
|
list_del(&hole->list);
|
|
|
|
free(hole);
|
|
|
|
}
|
|
|
|
pthread_mutex_destroy(&mgr->bo_va_mutex);
|
|
|
|
}
|
|
|
|
|
2020-11-22 18:18:05 -07:00
|
|
|
static drm_private int
|
|
|
|
amdgpu_vamgr_subtract_hole(struct amdgpu_bo_va_hole *hole, uint64_t start_va,
|
|
|
|
uint64_t end_va)
|
|
|
|
{
|
|
|
|
if (start_va > hole->offset && end_va - hole->offset < hole->size) {
|
|
|
|
struct amdgpu_bo_va_hole *n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
|
|
|
|
if (!n)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
n->size = start_va - hole->offset;
|
|
|
|
n->offset = hole->offset;
|
|
|
|
list_add(&n->list, &hole->list);
|
|
|
|
|
|
|
|
hole->size -= (end_va - hole->offset);
|
|
|
|
hole->offset = end_va;
|
|
|
|
} else if (start_va > hole->offset) {
|
|
|
|
hole->size = start_va - hole->offset;
|
|
|
|
} else if (end_va - hole->offset < hole->size) {
|
|
|
|
hole->size -= (end_va - hole->offset);
|
|
|
|
hole->offset = end_va;
|
|
|
|
} else {
|
|
|
|
list_del(&hole->list);
|
|
|
|
free(hole);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static drm_private int
|
2015-08-07 09:54:29 -06:00
|
|
|
amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
|
2020-11-22 18:18:05 -07:00
|
|
|
uint64_t alignment, uint64_t base_required,
|
|
|
|
bool search_from_top, uint64_t *va_out)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
2015-05-12 23:58:43 -06:00
|
|
|
struct amdgpu_bo_va_hole *hole, *n;
|
2020-11-22 18:18:05 -07:00
|
|
|
uint64_t offset = 0;
|
|
|
|
int ret;
|
2015-05-12 23:58:43 -06:00
|
|
|
|
2018-02-19 00:18:36 -07:00
|
|
|
|
2015-05-12 23:58:43 -06:00
|
|
|
alignment = MAX2(alignment, mgr->va_alignment);
|
|
|
|
size = ALIGN(size, mgr->va_alignment);
|
|
|
|
|
2015-07-08 23:48:25 -06:00
|
|
|
if (base_required % alignment)
|
2020-11-22 18:18:05 -07:00
|
|
|
return -EINVAL;
|
2015-07-08 23:48:25 -06:00
|
|
|
|
2015-05-12 23:58:43 -06:00
|
|
|
pthread_mutex_lock(&mgr->bo_va_mutex);
|
2020-11-22 18:18:05 -07:00
|
|
|
if (!search_from_top) {
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE_REV(hole, n, &mgr->va_holes, list) {
|
|
|
|
if (base_required) {
|
|
|
|
if (hole->offset > base_required ||
|
|
|
|
(hole->offset + hole->size) < (base_required + size))
|
|
|
|
continue;
|
|
|
|
offset = base_required;
|
|
|
|
} else {
|
|
|
|
uint64_t waste = hole->offset % alignment;
|
|
|
|
waste = waste ? alignment - waste : 0;
|
|
|
|
offset = hole->offset + waste;
|
|
|
|
if (offset >= (hole->offset + hole->size) ||
|
|
|
|
size > (hole->offset + hole->size) - offset) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-08 23:48:25 -06:00
|
|
|
}
|
2020-11-22 18:18:05 -07:00
|
|
|
ret = amdgpu_vamgr_subtract_hole(hole, offset, offset + size);
|
2015-05-12 23:58:43 -06:00
|
|
|
pthread_mutex_unlock(&mgr->bo_va_mutex);
|
2020-11-22 18:18:05 -07:00
|
|
|
*va_out = offset;
|
|
|
|
return ret;
|
2015-05-12 23:58:43 -06:00
|
|
|
}
|
2020-11-22 18:18:05 -07:00
|
|
|
} else {
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
|
|
|
|
if (base_required) {
|
|
|
|
if (hole->offset > base_required ||
|
|
|
|
(hole->offset + hole->size) < (base_required + size))
|
|
|
|
continue;
|
|
|
|
offset = base_required;
|
|
|
|
} else {
|
|
|
|
if (size > hole->size)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
offset = hole->offset + hole->size - size;
|
|
|
|
offset -= offset % alignment;
|
|
|
|
if (offset < hole->offset) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-12 23:58:43 -06:00
|
|
|
}
|
2020-11-22 18:18:05 -07:00
|
|
|
|
|
|
|
ret = amdgpu_vamgr_subtract_hole(hole, offset, offset + size);
|
2015-05-12 23:58:43 -06:00
|
|
|
pthread_mutex_unlock(&mgr->bo_va_mutex);
|
2020-11-22 18:18:05 -07:00
|
|
|
*va_out = offset;
|
|
|
|
return ret;
|
2015-05-12 23:58:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&mgr->bo_va_mutex);
|
2020-11-22 18:18:05 -07:00
|
|
|
return -ENOMEM;
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
|
|
|
|
2017-11-02 11:54:59 -06:00
|
|
|
static drm_private void
|
2015-08-07 09:54:29 -06:00
|
|
|
amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size)
|
2015-04-20 10:04:22 -06:00
|
|
|
{
|
2018-02-07 23:52:11 -07:00
|
|
|
struct amdgpu_bo_va_hole *hole, *next;
|
2015-05-12 23:58:43 -06:00
|
|
|
|
2015-05-13 00:01:53 -06:00
|
|
|
if (va == AMDGPU_INVALID_VA_ADDRESS)
|
|
|
|
return;
|
|
|
|
|
2015-05-12 23:58:43 -06:00
|
|
|
size = ALIGN(size, mgr->va_alignment);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mgr->bo_va_mutex);
|
2018-02-07 23:52:11 -07:00
|
|
|
hole = container_of(&mgr->va_holes, hole, list);
|
|
|
|
LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
|
|
|
|
if (next->offset < va)
|
|
|
|
break;
|
|
|
|
hole = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (&hole->list != &mgr->va_holes) {
|
|
|
|
/* Grow upper hole if it's adjacent */
|
|
|
|
if (hole->offset == (va + size)) {
|
|
|
|
hole->offset = va;
|
|
|
|
hole->size += size;
|
|
|
|
/* Merge lower hole if it's adjacent */
|
|
|
|
if (next != hole &&
|
|
|
|
&next->list != &mgr->va_holes &&
|
|
|
|
(next->offset + next->size) == va) {
|
|
|
|
next->size += hole->size;
|
2015-05-12 23:58:43 -06:00
|
|
|
list_del(&hole->list);
|
|
|
|
free(hole);
|
|
|
|
}
|
2018-02-08 01:50:53 -07:00
|
|
|
goto out;
|
|
|
|
}
|
2018-02-07 23:52:11 -07:00
|
|
|
}
|
2018-02-07 23:52:11 -07:00
|
|
|
|
2018-02-07 23:52:11 -07:00
|
|
|
/* Grow lower hole if it's adjacent */
|
|
|
|
if (next != hole && &next->list != &mgr->va_holes &&
|
|
|
|
(next->offset + next->size) == va) {
|
|
|
|
next->size += size;
|
|
|
|
goto out;
|
2018-02-08 01:50:53 -07:00
|
|
|
}
|
2018-02-07 23:52:11 -07:00
|
|
|
|
|
|
|
/* FIXME on allocation failure we just lose virtual address space
|
|
|
|
* maybe print a warning
|
|
|
|
*/
|
|
|
|
next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
|
|
|
|
if (next) {
|
|
|
|
next->size = size;
|
|
|
|
next->offset = va;
|
|
|
|
list_add(&next->list, &hole->list);
|
|
|
|
}
|
|
|
|
|
2015-04-20 10:04:22 -06:00
|
|
|
out:
|
2015-05-12 23:58:43 -06:00
|
|
|
pthread_mutex_unlock(&mgr->bo_va_mutex);
|
2015-04-20 10:04:22 -06:00
|
|
|
}
|
2015-07-08 23:50:36 -06:00
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_va_range_alloc(amdgpu_device_handle dev,
|
|
|
|
enum amdgpu_gpu_va_range va_range_type,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t va_base_alignment,
|
|
|
|
uint64_t va_base_required,
|
|
|
|
uint64_t *va_base_allocated,
|
|
|
|
amdgpu_va_handle *va_range_handle,
|
|
|
|
uint64_t flags)
|
2023-12-18 07:27:00 -07:00
|
|
|
{
|
|
|
|
return amdgpu_va_range_alloc2(&dev->va_mgr, va_range_type, size,
|
|
|
|
va_base_alignment, va_base_required,
|
|
|
|
va_base_allocated, va_range_handle,
|
|
|
|
flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_public int amdgpu_va_range_alloc2(amdgpu_va_manager_handle va_mgr,
|
|
|
|
enum amdgpu_gpu_va_range va_range_type,
|
|
|
|
uint64_t size,
|
|
|
|
uint64_t va_base_alignment,
|
|
|
|
uint64_t va_base_required,
|
|
|
|
uint64_t *va_base_allocated,
|
|
|
|
amdgpu_va_handle *va_range_handle,
|
|
|
|
uint64_t flags)
|
2015-07-08 23:50:36 -06:00
|
|
|
{
|
2015-08-16 21:09:08 -06:00
|
|
|
struct amdgpu_bo_va_mgr *vamgr;
|
2020-11-22 18:18:05 -07:00
|
|
|
bool search_from_top = !!(flags & AMDGPU_VA_RANGE_REPLAYABLE);
|
|
|
|
int ret;
|
2015-07-08 23:50:36 -06:00
|
|
|
|
2018-02-26 06:11:52 -07:00
|
|
|
/* Clear the flag when the high VA manager is not initialized */
|
2023-12-18 07:27:00 -07:00
|
|
|
if (flags & AMDGPU_VA_RANGE_HIGH && !va_mgr->vamgr_high_32.va_max)
|
2018-02-26 06:11:52 -07:00
|
|
|
flags &= ~AMDGPU_VA_RANGE_HIGH;
|
|
|
|
|
|
|
|
if (flags & AMDGPU_VA_RANGE_HIGH) {
|
|
|
|
if (flags & AMDGPU_VA_RANGE_32_BIT)
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_high_32;
|
2018-02-26 06:11:52 -07:00
|
|
|
else
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_high;
|
2018-02-26 06:11:52 -07:00
|
|
|
} else {
|
|
|
|
if (flags & AMDGPU_VA_RANGE_32_BIT)
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_32;
|
2018-02-26 06:11:52 -07:00
|
|
|
else
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_low;
|
2018-02-26 06:11:52 -07:00
|
|
|
}
|
2015-08-16 21:09:08 -06:00
|
|
|
|
|
|
|
va_base_alignment = MAX2(va_base_alignment, vamgr->va_alignment);
|
|
|
|
size = ALIGN(size, vamgr->va_alignment);
|
|
|
|
|
2020-11-22 18:18:05 -07:00
|
|
|
ret = amdgpu_vamgr_find_va(vamgr, size,
|
|
|
|
va_base_alignment, va_base_required,
|
|
|
|
search_from_top, va_base_allocated);
|
2015-08-16 21:09:08 -06:00
|
|
|
|
2020-11-22 18:18:05 -07:00
|
|
|
if (!(flags & AMDGPU_VA_RANGE_32_BIT) && ret) {
|
2015-08-16 21:09:08 -06:00
|
|
|
/* fallback to 32bit address */
|
2018-02-26 06:11:52 -07:00
|
|
|
if (flags & AMDGPU_VA_RANGE_HIGH)
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_high_32;
|
2018-02-26 06:11:52 -07:00
|
|
|
else
|
2023-12-18 07:27:00 -07:00
|
|
|
vamgr = &va_mgr->vamgr_32;
|
2020-11-22 18:18:05 -07:00
|
|
|
ret = amdgpu_vamgr_find_va(vamgr, size,
|
|
|
|
va_base_alignment, va_base_required,
|
|
|
|
search_from_top, va_base_allocated);
|
2015-08-16 21:09:08 -06:00
|
|
|
}
|
2015-07-08 23:50:36 -06:00
|
|
|
|
2020-11-22 18:18:05 -07:00
|
|
|
if (!ret) {
|
2015-07-08 23:50:36 -06:00
|
|
|
struct amdgpu_va* va;
|
|
|
|
va = calloc(1, sizeof(struct amdgpu_va));
|
|
|
|
if(!va){
|
2015-08-16 21:09:08 -06:00
|
|
|
amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size);
|
2015-07-08 23:50:36 -06:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
va->address = *va_base_allocated;
|
|
|
|
va->size = size;
|
|
|
|
va->range = va_range_type;
|
2015-08-16 21:09:08 -06:00
|
|
|
va->vamgr = vamgr;
|
2015-07-08 23:50:36 -06:00
|
|
|
*va_range_handle = va;
|
|
|
|
}
|
|
|
|
|
2020-11-22 18:18:05 -07:00
|
|
|
return ret;
|
2015-07-08 23:50:36 -06:00
|
|
|
}
|
|
|
|
|
2018-09-13 15:57:13 -06:00
|
|
|
drm_public int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)
|
2015-07-08 23:50:36 -06:00
|
|
|
{
|
|
|
|
if(!va_range_handle || !va_range_handle->address)
|
|
|
|
return 0;
|
2015-08-16 21:09:08 -06:00
|
|
|
|
|
|
|
amdgpu_vamgr_free_va(va_range_handle->vamgr,
|
|
|
|
va_range_handle->address,
|
2015-07-08 23:50:36 -06:00
|
|
|
va_range_handle->size);
|
|
|
|
free(va_range_handle);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-20 22:30:41 -07:00
|
|
|
|
|
|
|
drm_public uint64_t amdgpu_va_get_start_addr(amdgpu_va_handle va_handle)
|
|
|
|
{
|
|
|
|
return va_handle->address;
|
|
|
|
}
|
2023-12-18 07:02:30 -07:00
|
|
|
|
|
|
|
drm_public amdgpu_va_manager_handle amdgpu_va_manager_alloc(void)
|
|
|
|
{
|
|
|
|
amdgpu_va_manager_handle r = calloc(1, sizeof(struct amdgpu_va_manager));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_public void amdgpu_va_manager_init(struct amdgpu_va_manager *va_mgr,
|
|
|
|
uint64_t low_va_offset, uint64_t low_va_max,
|
|
|
|
uint64_t high_va_offset, uint64_t high_va_max,
|
|
|
|
uint32_t virtual_address_alignment)
|
|
|
|
{
|
|
|
|
uint64_t start, max;
|
|
|
|
|
|
|
|
start = low_va_offset;
|
|
|
|
max = MIN2(low_va_max, 0x100000000ULL);
|
|
|
|
amdgpu_vamgr_init(&va_mgr->vamgr_32, start, max,
|
|
|
|
virtual_address_alignment);
|
|
|
|
|
|
|
|
start = max;
|
|
|
|
max = MAX2(low_va_max, 0x100000000ULL);
|
|
|
|
amdgpu_vamgr_init(&va_mgr->vamgr_low, start, max,
|
|
|
|
virtual_address_alignment);
|
|
|
|
|
|
|
|
start = high_va_offset;
|
|
|
|
max = MIN2(high_va_max, (start & ~0xffffffffULL) + 0x100000000ULL);
|
|
|
|
amdgpu_vamgr_init(&va_mgr->vamgr_high_32, start, max,
|
|
|
|
virtual_address_alignment);
|
|
|
|
|
|
|
|
start = max;
|
|
|
|
max = MAX2(high_va_max, (start & ~0xffffffffULL) + 0x100000000ULL);
|
|
|
|
amdgpu_vamgr_init(&va_mgr->vamgr_high, start, max,
|
|
|
|
virtual_address_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_public void amdgpu_va_manager_deinit(struct amdgpu_va_manager *va_mgr)
|
|
|
|
{
|
|
|
|
amdgpu_vamgr_deinit(&va_mgr->vamgr_32);
|
|
|
|
amdgpu_vamgr_deinit(&va_mgr->vamgr_low);
|
|
|
|
amdgpu_vamgr_deinit(&va_mgr->vamgr_high_32);
|
|
|
|
amdgpu_vamgr_deinit(&va_mgr->vamgr_high);
|
|
|
|
}
|