1*146e4384SMichal Wajdeczko // SPDX-License-Identifier: MIT 2*146e4384SMichal Wajdeczko /* 3*146e4384SMichal Wajdeczko * Copyright © 2023-2024 Intel Corporation 4*146e4384SMichal Wajdeczko */ 5*146e4384SMichal Wajdeczko 6*146e4384SMichal Wajdeczko #include "xe_assert.h" 7*146e4384SMichal Wajdeczko #include "xe_device.h" 8*146e4384SMichal Wajdeczko #include "xe_module.h" 9*146e4384SMichal Wajdeczko #include "xe_sriov.h" 10*146e4384SMichal Wajdeczko #include "xe_sriov_pf.h" 11*146e4384SMichal Wajdeczko #include "xe_sriov_printk.h" 12*146e4384SMichal Wajdeczko 13*146e4384SMichal Wajdeczko static unsigned int wanted_max_vfs(struct xe_device *xe) 14*146e4384SMichal Wajdeczko { 15*146e4384SMichal Wajdeczko return xe_modparam.max_vfs; 16*146e4384SMichal Wajdeczko } 17*146e4384SMichal Wajdeczko 18*146e4384SMichal Wajdeczko static int pf_reduce_totalvfs(struct xe_device *xe, int limit) 19*146e4384SMichal Wajdeczko { 20*146e4384SMichal Wajdeczko struct device *dev = xe->drm.dev; 21*146e4384SMichal Wajdeczko struct pci_dev *pdev = to_pci_dev(dev); 22*146e4384SMichal Wajdeczko int err; 23*146e4384SMichal Wajdeczko 24*146e4384SMichal Wajdeczko err = pci_sriov_set_totalvfs(pdev, limit); 25*146e4384SMichal Wajdeczko if (err) 26*146e4384SMichal Wajdeczko xe_sriov_notice(xe, "Failed to set number of VFs to %d (%pe)\n", 27*146e4384SMichal Wajdeczko limit, ERR_PTR(err)); 28*146e4384SMichal Wajdeczko return err; 29*146e4384SMichal Wajdeczko } 30*146e4384SMichal Wajdeczko 31*146e4384SMichal Wajdeczko static bool pf_continue_as_native(struct xe_device *xe, const char *why) 32*146e4384SMichal Wajdeczko { 33*146e4384SMichal Wajdeczko xe_sriov_dbg(xe, "%s, continuing as native\n", why); 34*146e4384SMichal Wajdeczko pf_reduce_totalvfs(xe, 0); 35*146e4384SMichal Wajdeczko return false; 36*146e4384SMichal Wajdeczko } 37*146e4384SMichal Wajdeczko 38*146e4384SMichal Wajdeczko /** 39*146e4384SMichal Wajdeczko * xe_sriov_pf_readiness - Check if PF functionality can be enabled. 40*146e4384SMichal Wajdeczko * @xe: the &xe_device to check 41*146e4384SMichal Wajdeczko * 42*146e4384SMichal Wajdeczko * This function is called as part of the SR-IOV probe to validate if all 43*146e4384SMichal Wajdeczko * PF prerequisites are satisfied and we can continue with enabling PF mode. 44*146e4384SMichal Wajdeczko * 45*146e4384SMichal Wajdeczko * Return: true if the PF mode can be turned on. 46*146e4384SMichal Wajdeczko */ 47*146e4384SMichal Wajdeczko bool xe_sriov_pf_readiness(struct xe_device *xe) 48*146e4384SMichal Wajdeczko { 49*146e4384SMichal Wajdeczko struct device *dev = xe->drm.dev; 50*146e4384SMichal Wajdeczko struct pci_dev *pdev = to_pci_dev(dev); 51*146e4384SMichal Wajdeczko int totalvfs = pci_sriov_get_totalvfs(pdev); 52*146e4384SMichal Wajdeczko int newlimit = min_t(u16, wanted_max_vfs(xe), totalvfs); 53*146e4384SMichal Wajdeczko 54*146e4384SMichal Wajdeczko xe_assert(xe, totalvfs <= U16_MAX); 55*146e4384SMichal Wajdeczko 56*146e4384SMichal Wajdeczko if (!dev_is_pf(dev)) 57*146e4384SMichal Wajdeczko return false; 58*146e4384SMichal Wajdeczko 59*146e4384SMichal Wajdeczko if (!xe_device_uc_enabled(xe)) 60*146e4384SMichal Wajdeczko return pf_continue_as_native(xe, "Guc submission disabled"); 61*146e4384SMichal Wajdeczko 62*146e4384SMichal Wajdeczko if (!newlimit) 63*146e4384SMichal Wajdeczko return pf_continue_as_native(xe, "all VFs disabled"); 64*146e4384SMichal Wajdeczko 65*146e4384SMichal Wajdeczko pf_reduce_totalvfs(xe, newlimit); 66*146e4384SMichal Wajdeczko 67*146e4384SMichal Wajdeczko xe->sriov.pf.device_total_vfs = totalvfs; 68*146e4384SMichal Wajdeczko xe->sriov.pf.driver_max_vfs = newlimit; 69*146e4384SMichal Wajdeczko 70*146e4384SMichal Wajdeczko return true; 71*146e4384SMichal Wajdeczko } 72*146e4384SMichal Wajdeczko 73*146e4384SMichal Wajdeczko /** 74*146e4384SMichal Wajdeczko * xe_sriov_pf_print_vfs_summary - Print SR-IOV PF information. 75*146e4384SMichal Wajdeczko * @xe: the &xe_device to print info from 76*146e4384SMichal Wajdeczko * @p: the &drm_printer 77*146e4384SMichal Wajdeczko * 78*146e4384SMichal Wajdeczko * Print SR-IOV PF related information into provided DRM printer. 79*146e4384SMichal Wajdeczko */ 80*146e4384SMichal Wajdeczko void xe_sriov_pf_print_vfs_summary(struct xe_device *xe, struct drm_printer *p) 81*146e4384SMichal Wajdeczko { 82*146e4384SMichal Wajdeczko struct pci_dev *pdev = to_pci_dev(xe->drm.dev); 83*146e4384SMichal Wajdeczko 84*146e4384SMichal Wajdeczko xe_assert(xe, IS_SRIOV_PF(xe)); 85*146e4384SMichal Wajdeczko 86*146e4384SMichal Wajdeczko drm_printf(p, "total: %u\n", xe->sriov.pf.device_total_vfs); 87*146e4384SMichal Wajdeczko drm_printf(p, "supported: %u\n", xe->sriov.pf.driver_max_vfs); 88*146e4384SMichal Wajdeczko drm_printf(p, "enabled: %u\n", pci_num_vf(pdev)); 89*146e4384SMichal Wajdeczko } 90