xref: /linux/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
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/export.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 
29 #include <drm/drm_drv.h>
30 
31 #include "amdgpu_xcp_drv.h"
32 
33 #define MAX_XCP_PLATFORM_DEVICE 64
34 
35 struct xcp_device {
36 	struct drm_device drm;
37 	struct platform_device *pdev;
38 };
39 
40 static const struct drm_driver amdgpu_xcp_driver = {
41 	.driver_features = DRIVER_GEM | DRIVER_RENDER,
42 	.name = "amdgpu_xcp_drv",
43 	.major = 1,
44 	.minor = 0,
45 };
46 
47 static int8_t pdev_num;
48 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
49 
amdgpu_xcp_drm_dev_alloc(struct drm_device ** ddev)50 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
51 {
52 	struct platform_device *pdev;
53 	struct xcp_device *pxcp_dev;
54 	char dev_name[20];
55 	int ret;
56 
57 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
58 		return -ENODEV;
59 
60 	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
61 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
62 	if (IS_ERR(pdev))
63 		return PTR_ERR(pdev);
64 
65 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
66 		ret = -ENOMEM;
67 		goto out_unregister;
68 	}
69 
70 	pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
71 	if (IS_ERR(pxcp_dev)) {
72 		ret = PTR_ERR(pxcp_dev);
73 		goto out_devres;
74 	}
75 
76 	xcp_dev[pdev_num] = pxcp_dev;
77 	xcp_dev[pdev_num]->pdev = pdev;
78 	*ddev = &pxcp_dev->drm;
79 	pdev_num++;
80 
81 	return 0;
82 
83 out_devres:
84 	devres_release_group(&pdev->dev, NULL);
85 out_unregister:
86 	platform_device_unregister(pdev);
87 
88 	return ret;
89 }
90 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
91 
amdgpu_xcp_drv_release(void)92 void amdgpu_xcp_drv_release(void)
93 {
94 	for (--pdev_num; pdev_num >= 0; --pdev_num) {
95 		struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
96 
97 		devres_release_group(&pdev->dev, NULL);
98 		platform_device_unregister(pdev);
99 		xcp_dev[pdev_num] = NULL;
100 	}
101 	pdev_num = 0;
102 }
103 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
104 
amdgpu_xcp_drv_exit(void)105 static void __exit amdgpu_xcp_drv_exit(void)
106 {
107 	amdgpu_xcp_drv_release();
108 }
109 
110 module_exit(amdgpu_xcp_drv_exit);
111 
112 MODULE_AUTHOR("AMD linux driver team");
113 MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
114 MODULE_LICENSE("GPL and additional rights");
115