xref: /linux/drivers/gpu/drm/xe/xe_gt_sriov_pf.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023-2024 Intel Corporation
4  */
5 
6 #include <drm/drm_managed.h>
7 
8 #include "xe_gt_sriov_pf.h"
9 #include "xe_gt_sriov_pf_helpers.h"
10 
11 /*
12  * VF's metadata is maintained in the flexible array where:
13  *   - entry [0] contains metadata for the PF (only if applicable),
14  *   - entries [1..n] contain metadata for VF1..VFn::
15  *
16  *       <--------------------------- 1 + total_vfs ----------->
17  *      +-------+-------+-------+-----------------------+-------+
18  *      |   0   |   1   |   2   |                       |   n   |
19  *      +-------+-------+-------+-----------------------+-------+
20  *      |  PF   |  VF1  |  VF2  |      ...     ...      |  VFn  |
21  *      +-------+-------+-------+-----------------------+-------+
22  */
23 static int pf_alloc_metadata(struct xe_gt *gt)
24 {
25 	unsigned int num_vfs = xe_gt_sriov_pf_get_totalvfs(gt);
26 
27 	gt->sriov.pf.vfs = drmm_kcalloc(&gt_to_xe(gt)->drm, 1 + num_vfs,
28 					sizeof(*gt->sriov.pf.vfs), GFP_KERNEL);
29 	if (!gt->sriov.pf.vfs)
30 		return -ENOMEM;
31 
32 	return 0;
33 }
34 
35 /**
36  * xe_gt_sriov_pf_init_early - Prepare SR-IOV PF data structures on PF.
37  * @gt: the &xe_gt to initialize
38  *
39  * Early initialization of the PF data.
40  *
41  * Return: 0 on success or a negative error code on failure.
42  */
43 int xe_gt_sriov_pf_init_early(struct xe_gt *gt)
44 {
45 	int err;
46 
47 	err = pf_alloc_metadata(gt);
48 	if (err)
49 		return err;
50 
51 	return 0;
52 }
53