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