Path: blob/master/drivers/gpu/drm/amd/display/modules/vmid/vmid.c
26552 views
/*1* Copyright 2019 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21* Authors: AMD22*23*/2425#include "mod_vmid.h"2627struct core_vmid {28struct mod_vmid public;29struct dc *dc;3031unsigned int num_vmid;32unsigned int num_vmids_available;33uint64_t ptb_assigned_to_vmid[MAX_VMID];34struct dc_virtual_addr_space_config base_config;35};3637#define MOD_VMID_TO_CORE(mod_vmid)\38container_of(mod_vmid, struct core_vmid, public)3940static void add_ptb_to_table(struct core_vmid *core_vmid, unsigned int vmid, uint64_t ptb)41{42if (vmid < MAX_VMID) {43core_vmid->ptb_assigned_to_vmid[vmid] = ptb;44core_vmid->num_vmids_available--;45}46}4748static void clear_entry_from_vmid_table(struct core_vmid *core_vmid, unsigned int vmid)49{50if (vmid < MAX_VMID) {51core_vmid->ptb_assigned_to_vmid[vmid] = 0;52core_vmid->num_vmids_available++;53}54}5556static void evict_vmids(struct core_vmid *core_vmid)57{58int i;59uint16_t ord = dc_get_vmid_use_vector(core_vmid->dc);6061// At this point any positions with value 0 are unused vmids, evict them62for (i = 1; i < core_vmid->num_vmid; i++) {63if (!(ord & (1u << i)))64clear_entry_from_vmid_table(core_vmid, i);65}66}6768// Return value of -1 indicates vmid table uninitialized or ptb dne in the table69static int get_existing_vmid_for_ptb(struct core_vmid *core_vmid, uint64_t ptb)70{71int i;7273for (i = 0; i < core_vmid->num_vmid; i++) {74if (core_vmid->ptb_assigned_to_vmid[i] == ptb)75return i;76}7778return -1;79}8081// Expected to be called only when there's an available vmid82static int get_next_available_vmid(struct core_vmid *core_vmid)83{84int i;8586for (i = 1; i < core_vmid->num_vmid; i++) {87if (core_vmid->ptb_assigned_to_vmid[i] == 0)88return i;89}9091return -1;92}9394uint8_t mod_vmid_get_for_ptb(struct mod_vmid *mod_vmid, uint64_t ptb)95{96struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);97int vmid = 0;9899// Physical address gets vmid 0100if (ptb == 0)101return 0;102103vmid = get_existing_vmid_for_ptb(core_vmid, ptb);104105if (vmid == -1) {106struct dc_virtual_addr_space_config va_config = core_vmid->base_config;107108va_config.page_table_base_addr = ptb;109110if (core_vmid->num_vmids_available == 0)111evict_vmids(core_vmid);112113vmid = get_next_available_vmid(core_vmid);114if (vmid != -1) {115add_ptb_to_table(core_vmid, vmid, ptb);116117dc_setup_vm_context(core_vmid->dc, &va_config, vmid);118} else119ASSERT(0);120}121122return vmid;123}124125void mod_vmid_reset(struct mod_vmid *mod_vmid)126{127struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);128129core_vmid->num_vmids_available = core_vmid->num_vmid - 1;130memset(core_vmid->ptb_assigned_to_vmid, 0, sizeof(core_vmid->ptb_assigned_to_vmid[0]) * MAX_VMID);131}132133struct mod_vmid *mod_vmid_create(134struct dc *dc,135unsigned int num_vmid,136struct dc_virtual_addr_space_config *va_config)137{138struct core_vmid *core_vmid;139140if (num_vmid <= 1)141goto fail_no_vm_ctx;142143if (dc == NULL)144goto fail_dc_null;145146core_vmid = kzalloc(sizeof(struct core_vmid), GFP_KERNEL);147148if (core_vmid == NULL)149goto fail_alloc_context;150151core_vmid->dc = dc;152core_vmid->num_vmid = num_vmid;153core_vmid->num_vmids_available = num_vmid - 1;154core_vmid->base_config = *va_config;155156memset(core_vmid->ptb_assigned_to_vmid, 0, sizeof(core_vmid->ptb_assigned_to_vmid[0]) * MAX_VMID);157158return &core_vmid->public;159160fail_no_vm_ctx:161fail_alloc_context:162fail_dc_null:163return NULL;164}165166void mod_vmid_destroy(struct mod_vmid *mod_vmid)167{168if (mod_vmid != NULL) {169struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);170171kfree(core_vmid);172}173}174175176