xref: /linux/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 /*
2  * Copyright 2023 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 
28 #include <drm/drm_drv.h>
29 
30 #include "amdgpu_xcp_drv.h"
31 
32 #define MAX_XCP_PLATFORM_DEVICE 64
33 
34 struct xcp_device {
35 	struct drm_device drm;
36 	struct platform_device *pdev;
37 };
38 
39 static const struct drm_driver amdgpu_xcp_driver = {
40 	.driver_features = DRIVER_GEM | DRIVER_RENDER,
41 	.name = "amdgpu_xcp_drv",
42 	.major = 1,
43 	.minor = 0,
44 };
45 
46 static int pdev_num;
47 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
48 
49 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
50 {
51 	struct platform_device *pdev;
52 	struct xcp_device *pxcp_dev;
53 	int ret;
54 
55 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
56 		return -ENODEV;
57 
58 	pdev = platform_device_register_simple("amdgpu_xcp", pdev_num, NULL, 0);
59 	if (IS_ERR(pdev))
60 		return PTR_ERR(pdev);
61 
62 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
63 		ret = -ENOMEM;
64 		goto out_unregister;
65 	}
66 
67 	pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
68 	if (IS_ERR(pxcp_dev)) {
69 		ret = PTR_ERR(pxcp_dev);
70 		goto out_devres;
71 	}
72 
73 	xcp_dev[pdev_num] = pxcp_dev;
74 	xcp_dev[pdev_num]->pdev = pdev;
75 	*ddev = &pxcp_dev->drm;
76 	pdev_num++;
77 
78 	return 0;
79 
80 out_devres:
81 	devres_release_group(&pdev->dev, NULL);
82 out_unregister:
83 	platform_device_unregister(pdev);
84 
85 	return ret;
86 }
87 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
88 
89 void amdgpu_xcp_drv_release(void)
90 {
91 	for (--pdev_num; pdev_num >= 0; --pdev_num) {
92 		devres_release_group(&xcp_dev[pdev_num]->pdev->dev, NULL);
93 		platform_device_unregister(xcp_dev[pdev_num]->pdev);
94 		xcp_dev[pdev_num]->pdev = NULL;
95 		xcp_dev[pdev_num] = NULL;
96 	}
97 	pdev_num = 0;
98 }
99 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
100 
101 static void __exit amdgpu_xcp_drv_exit(void)
102 {
103 	amdgpu_xcp_drv_release();
104 }
105 
106 module_exit(amdgpu_xcp_drv_exit);
107 
108 MODULE_AUTHOR("AMD linux driver team");
109 MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
110 MODULE_LICENSE("GPL and additional rights");
111