xref: /linux/drivers/gpu/drm/xe/xe_gt_sriov_pf.c (revision 76d9b92e68f2bb55890f935c5143f4fef97a935d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023-2024 Intel Corporation
4  */
5 
6 #include <drm/drm_managed.h>
7 
8 #include "regs/xe_sriov_regs.h"
9 
10 #include "xe_gt_sriov_pf.h"
11 #include "xe_gt_sriov_pf_config.h"
12 #include "xe_gt_sriov_pf_helpers.h"
13 #include "xe_gt_sriov_pf_service.h"
14 #include "xe_mmio.h"
15 
16 /*
17  * VF's metadata is maintained in the flexible array where:
18  *   - entry [0] contains metadata for the PF (only if applicable),
19  *   - entries [1..n] contain metadata for VF1..VFn::
20  *
21  *       <--------------------------- 1 + total_vfs ----------->
22  *      +-------+-------+-------+-----------------------+-------+
23  *      |   0   |   1   |   2   |                       |   n   |
24  *      +-------+-------+-------+-----------------------+-------+
25  *      |  PF   |  VF1  |  VF2  |      ...     ...      |  VFn  |
26  *      +-------+-------+-------+-----------------------+-------+
27  */
28 static int pf_alloc_metadata(struct xe_gt *gt)
29 {
30 	unsigned int num_vfs = xe_gt_sriov_pf_get_totalvfs(gt);
31 
32 	gt->sriov.pf.vfs = drmm_kcalloc(&gt_to_xe(gt)->drm, 1 + num_vfs,
33 					sizeof(*gt->sriov.pf.vfs), GFP_KERNEL);
34 	if (!gt->sriov.pf.vfs)
35 		return -ENOMEM;
36 
37 	return 0;
38 }
39 
40 /**
41  * xe_gt_sriov_pf_init_early - Prepare SR-IOV PF data structures on PF.
42  * @gt: the &xe_gt to initialize
43  *
44  * Early initialization of the PF data.
45  *
46  * Return: 0 on success or a negative error code on failure.
47  */
48 int xe_gt_sriov_pf_init_early(struct xe_gt *gt)
49 {
50 	int err;
51 
52 	err = pf_alloc_metadata(gt);
53 	if (err)
54 		return err;
55 
56 	err = xe_gt_sriov_pf_service_init(gt);
57 	if (err)
58 		return err;
59 
60 	return 0;
61 }
62 
63 static bool pf_needs_enable_ggtt_guest_update(struct xe_device *xe)
64 {
65 	return GRAPHICS_VERx100(xe) == 1200;
66 }
67 
68 static void pf_enable_ggtt_guest_update(struct xe_gt *gt)
69 {
70 	xe_mmio_write32(gt, VIRTUAL_CTRL_REG, GUEST_GTT_UPDATE_EN);
71 }
72 
73 /**
74  * xe_gt_sriov_pf_init_hw - Initialize SR-IOV hardware support.
75  * @gt: the &xe_gt to initialize
76  *
77  * On some platforms the PF must explicitly enable VF's access to the GGTT.
78  */
79 void xe_gt_sriov_pf_init_hw(struct xe_gt *gt)
80 {
81 	if (pf_needs_enable_ggtt_guest_update(gt_to_xe(gt)))
82 		pf_enable_ggtt_guest_update(gt);
83 
84 	xe_gt_sriov_pf_service_update(gt);
85 }
86 
87 /**
88  * xe_gt_sriov_pf_restart - Restart SR-IOV support after a GT reset.
89  * @gt: the &xe_gt
90  *
91  * This function can only be called on PF.
92  */
93 void xe_gt_sriov_pf_restart(struct xe_gt *gt)
94 {
95 	xe_gt_sriov_pf_config_restart(gt);
96 }
97