xref: /linux/drivers/gpu/drm/xe/xe_gt_sriov_pf.c (revision 569d7db70e5dcf13fbf072f10e9096577ac1e565)
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_helpers.h"
12 #include "xe_gt_sriov_pf_service.h"
13 #include "xe_mmio.h"
14 
15 /*
16  * VF's metadata is maintained in the flexible array where:
17  *   - entry [0] contains metadata for the PF (only if applicable),
18  *   - entries [1..n] contain metadata for VF1..VFn::
19  *
20  *       <--------------------------- 1 + total_vfs ----------->
21  *      +-------+-------+-------+-----------------------+-------+
22  *      |   0   |   1   |   2   |                       |   n   |
23  *      +-------+-------+-------+-----------------------+-------+
24  *      |  PF   |  VF1  |  VF2  |      ...     ...      |  VFn  |
25  *      +-------+-------+-------+-----------------------+-------+
26  */
27 static int pf_alloc_metadata(struct xe_gt *gt)
28 {
29 	unsigned int num_vfs = xe_gt_sriov_pf_get_totalvfs(gt);
30 
31 	gt->sriov.pf.vfs = drmm_kcalloc(&gt_to_xe(gt)->drm, 1 + num_vfs,
32 					sizeof(*gt->sriov.pf.vfs), GFP_KERNEL);
33 	if (!gt->sriov.pf.vfs)
34 		return -ENOMEM;
35 
36 	return 0;
37 }
38 
39 /**
40  * xe_gt_sriov_pf_init_early - Prepare SR-IOV PF data structures on PF.
41  * @gt: the &xe_gt to initialize
42  *
43  * Early initialization of the PF data.
44  *
45  * Return: 0 on success or a negative error code on failure.
46  */
47 int xe_gt_sriov_pf_init_early(struct xe_gt *gt)
48 {
49 	int err;
50 
51 	err = pf_alloc_metadata(gt);
52 	if (err)
53 		return err;
54 
55 	err = xe_gt_sriov_pf_service_init(gt);
56 	if (err)
57 		return err;
58 
59 	return 0;
60 }
61 
62 static bool pf_needs_enable_ggtt_guest_update(struct xe_device *xe)
63 {
64 	return GRAPHICS_VERx100(xe) == 1200;
65 }
66 
67 static void pf_enable_ggtt_guest_update(struct xe_gt *gt)
68 {
69 	xe_mmio_write32(gt, VIRTUAL_CTRL_REG, GUEST_GTT_UPDATE_EN);
70 }
71 
72 /**
73  * xe_gt_sriov_pf_init_hw - Initialize SR-IOV hardware support.
74  * @gt: the &xe_gt to initialize
75  *
76  * On some platforms the PF must explicitly enable VF's access to the GGTT.
77  */
78 void xe_gt_sriov_pf_init_hw(struct xe_gt *gt)
79 {
80 	if (pf_needs_enable_ggtt_guest_update(gt_to_xe(gt)))
81 		pf_enable_ggtt_guest_update(gt);
82 
83 	xe_gt_sriov_pf_service_update(gt);
84 }
85