// SPDX-License-Identifier: GPL-2.0 OR MIT /************************************************************************** * * Copyright (c) 2009-2024 Broadcom. All Rights Reserved. The term * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. * * 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, 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 COPYRIGHT HOLDERS, AUTHORS AND/OR ITS 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. * **************************************************************************/ #include <drm/ttm/ttm_placement.h> #include "vmwgfx_binding.h" #include "vmwgfx_bo.h" #include "vmwgfx_drv.h" #include "vmwgfx_resource_priv.h" #define VMW_RES_EVICT_ERR_COUNT … /** * vmw_resource_mob_attach - Mark a resource as attached to its backing mob * @res: The resource */ void vmw_resource_mob_attach(struct vmw_resource *res) { … } /** * vmw_resource_mob_detach - Mark a resource as detached from its backing mob * @res: The resource */ void vmw_resource_mob_detach(struct vmw_resource *res) { … } struct vmw_resource *vmw_resource_reference(struct vmw_resource *res) { … } struct vmw_resource * vmw_resource_reference_unless_doomed(struct vmw_resource *res) { … } /** * vmw_resource_release_id - release a resource id to the id manager. * * @res: Pointer to the resource. * * Release the resource id to the resource id manager and set it to -1 */ void vmw_resource_release_id(struct vmw_resource *res) { … } static void vmw_resource_release(struct kref *kref) { … } void vmw_resource_unreference(struct vmw_resource **p_res) { … } /** * vmw_resource_alloc_id - release a resource id to the id manager. * * @res: Pointer to the resource. * * Allocate the lowest free resource from the resource manager, and set * @res->id to that id. Returns 0 on success and -ENOMEM on failure. */ int vmw_resource_alloc_id(struct vmw_resource *res) { … } /** * vmw_resource_init - initialize a struct vmw_resource * * @dev_priv: Pointer to a device private struct. * @res: The struct vmw_resource to initialize. * @delay_id: Boolean whether to defer device id allocation until * the first validation. * @res_free: Resource destructor. * @func: Resource function table. */ int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res, bool delay_id, void (*res_free) (struct vmw_resource *res), const struct vmw_res_func *func) { … } /** * vmw_user_resource_lookup_handle - lookup a struct resource from a * TTM user-space handle and perform basic type checks * * @dev_priv: Pointer to a device private struct * @tfile: Pointer to a struct ttm_object_file identifying the caller * @handle: The TTM user-space handle * @converter: Pointer to an object describing the resource type * @p_res: On successful return the location pointed to will contain * a pointer to a refcounted struct vmw_resource. * * If the handle can't be found or is associated with an incorrect resource * type, -EINVAL will be returned. */ int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv, struct ttm_object_file *tfile, uint32_t handle, const struct vmw_user_resource_conv *converter, struct vmw_resource **p_res) { … } /* * Helper function that looks either a surface or bo. * * The pointer this pointed at by out_surf and out_buf needs to be null. */ int vmw_user_object_lookup(struct vmw_private *dev_priv, struct drm_file *filp, u32 handle, struct vmw_user_object *uo) { … } /** * vmw_resource_buf_alloc - Allocate a guest memory buffer for a resource. * * @res: The resource for which to allocate a gbo buffer. * @interruptible: Whether any sleeps during allocation should be * performed while interruptible. */ static int vmw_resource_buf_alloc(struct vmw_resource *res, bool interruptible) { … } /** * vmw_resource_do_validate - Make a resource up-to-date and visible * to the device. * * @res: The resource to make visible to the device. * @val_buf: Information about a buffer possibly * containing backup data if a bind operation is needed. * @dirtying: Transfer dirty regions. * * On hardware resource shortage, this function returns -EBUSY and * should be retried once resources have been freed up. */ static int vmw_resource_do_validate(struct vmw_resource *res, struct ttm_validate_buffer *val_buf, bool dirtying) { … } /** * vmw_resource_unreserve - Unreserve a resource previously reserved for * command submission. * * @res: Pointer to the struct vmw_resource to unreserve. * @dirty_set: Change dirty status of the resource. * @dirty: When changing dirty status indicates the new status. * @switch_guest_memory: Guest memory buffer has been switched. * @new_guest_memory_bo: Pointer to new guest memory buffer if command submission * switched. May be NULL. * @new_guest_memory_offset: New gbo offset if @switch_guest_memory is true. * * Currently unreserving a resource means putting it back on the device's * resource lru list, so that it can be evicted if necessary. */ void vmw_resource_unreserve(struct vmw_resource *res, bool dirty_set, bool dirty, bool switch_guest_memory, struct vmw_bo *new_guest_memory_bo, unsigned long new_guest_memory_offset) { … } /** * vmw_resource_check_buffer - Check whether a backup buffer is needed * for a resource and in that case, allocate * one, reserve and validate it. * * @ticket: The ww acquire context to use, or NULL if trylocking. * @res: The resource for which to allocate a backup buffer. * @interruptible: Whether any sleeps during allocation should be * performed while interruptible. * @val_buf: On successful return contains data about the * reserved and validated backup buffer. */ static int vmw_resource_check_buffer(struct ww_acquire_ctx *ticket, struct vmw_resource *res, bool interruptible, struct ttm_validate_buffer *val_buf) { … } /* * vmw_resource_reserve - Reserve a resource for command submission * * @res: The resource to reserve. * * This function takes the resource off the LRU list and make sure * a guest memory buffer is present for guest-backed resources. * However, the buffer may not be bound to the resource at this * point. * */ int vmw_resource_reserve(struct vmw_resource *res, bool interruptible, bool no_guest_memory) { … } /** * vmw_resource_backoff_reservation - Unreserve and unreference a * guest memory buffer *. * @ticket: The ww acquire ctx used for reservation. * @val_buf: Guest memory buffer information. */ static void vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket, struct ttm_validate_buffer *val_buf) { … } /** * vmw_resource_do_evict - Evict a resource, and transfer its data * to a backup buffer. * * @ticket: The ww acquire ticket to use, or NULL if trylocking. * @res: The resource to evict. * @interruptible: Whether to wait interruptible. */ static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket, struct vmw_resource *res, bool interruptible) { … } /** * vmw_resource_validate - Make a resource up-to-date and visible * to the device. * @res: The resource to make visible to the device. * @intr: Perform waits interruptible if possible. * @dirtying: Pending GPU operation will dirty the resource * * On successful return, any backup DMA buffer pointed to by @res->backup will * be reserved and validated. * On hardware resource shortage, this function will repeatedly evict * resources of the same type until the validation succeeds. * * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code * on failure. */ int vmw_resource_validate(struct vmw_resource *res, bool intr, bool dirtying) { … } /** * vmw_resource_unbind_list * * @vbo: Pointer to the current backing MOB. * * Evicts the Guest Backed hardware resource if the backup * buffer is being moved out of MOB memory. * Note that this function will not race with the resource * validation code, since resource validation and eviction * both require the backup buffer to be reserved. */ void vmw_resource_unbind_list(struct vmw_bo *vbo) { … } /** * vmw_query_readback_all - Read back cached query states * * @dx_query_mob: Buffer containing the DX query MOB * * Read back cached states from the device if they exist. This function * assumes binding_mutex is held. */ int vmw_query_readback_all(struct vmw_bo *dx_query_mob) { … } /** * vmw_query_move_notify - Read back cached query states * * @bo: The TTM buffer object about to move. * @old_mem: The memory region @bo is moving from. * @new_mem: The memory region @bo is moving to. * * Called before the query MOB is swapped out to read back cached query * states from the device. */ void vmw_query_move_notify(struct ttm_buffer_object *bo, struct ttm_resource *old_mem, struct ttm_resource *new_mem) { … } /** * vmw_resource_needs_backup - Return whether a resource needs a backup buffer. * * @res: The resource being queried. */ bool vmw_resource_needs_backup(const struct vmw_resource *res) { … } /** * vmw_resource_evict_type - Evict all resources of a specific type * * @dev_priv: Pointer to a device private struct * @type: The resource type to evict * * To avoid thrashing starvation or as part of the hibernation sequence, * try to evict all evictable resources of a specific type. */ static void vmw_resource_evict_type(struct vmw_private *dev_priv, enum vmw_res_type type) { … } /** * vmw_resource_evict_all - Evict all evictable resources * * @dev_priv: Pointer to a device private struct * * To avoid thrashing starvation or as part of the hibernation sequence, * evict all evictable resources. In particular this means that all * guest-backed resources that are registered with the device are * evicted and the OTable becomes clean. */ void vmw_resource_evict_all(struct vmw_private *dev_priv) { … } /* * vmw_resource_pin - Add a pin reference on a resource * * @res: The resource to add a pin reference on * * This function adds a pin reference, and if needed validates the resource. * Having a pin reference means that the resource can never be evicted, and * its id will never change as long as there is a pin reference. * This function returns 0 on success and a negative error code on failure. */ int vmw_resource_pin(struct vmw_resource *res, bool interruptible) { … } /** * vmw_resource_unpin - Remove a pin reference from a resource * * @res: The resource to remove a pin reference from * * Having a pin reference means that the resource can never be evicted, and * its id will never change as long as there is a pin reference. */ void vmw_resource_unpin(struct vmw_resource *res) { … } /** * vmw_res_type - Return the resource type * * @res: Pointer to the resource */ enum vmw_res_type vmw_res_type(const struct vmw_resource *res) { … } /** * vmw_resource_dirty_update - Update a resource's dirty tracker with a * sequential range of touched backing store memory. * @res: The resource. * @start: The first page touched. * @end: The last page touched + 1. */ void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start, pgoff_t end) { … } int vmw_resource_clean(struct vmw_resource *res) { … } /** * vmw_resources_clean - Clean resources intersecting a mob range * @vbo: The mob buffer object * @start: The mob page offset starting the range * @end: The mob page offset ending the range * @num_prefault: Returns how many pages including the first have been * cleaned and are ok to prefault */ int vmw_resources_clean(struct vmw_bo *vbo, pgoff_t start, pgoff_t end, pgoff_t *num_prefault) { … }