xref: /linux/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c (revision f057b57270c2a17d3f45c177e9434fa5745caa48)
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 int8_t 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 	char dev_name[20];
54 	int ret;
55 
56 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
57 		return -ENODEV;
58 
59 	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
60 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
61 	if (IS_ERR(pdev))
62 		return PTR_ERR(pdev);
63 
64 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
65 		ret = -ENOMEM;
66 		goto out_unregister;
67 	}
68 
69 	pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
70 	if (IS_ERR(pxcp_dev)) {
71 		ret = PTR_ERR(pxcp_dev);
72 		goto out_devres;
73 	}
74 
75 	xcp_dev[pdev_num] = pxcp_dev;
76 	xcp_dev[pdev_num]->pdev = pdev;
77 	*ddev = &pxcp_dev->drm;
78 	pdev_num++;
79 
80 	return 0;
81 
82 out_devres:
83 	devres_release_group(&pdev->dev, NULL);
84 out_unregister:
85 	platform_device_unregister(pdev);
86 
87 	return ret;
88 }
89 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
90 
91 void amdgpu_xcp_drv_release(void)
92 {
93 	for (--pdev_num; pdev_num >= 0; --pdev_num) {
94 		struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
95 
96 		devres_release_group(&pdev->dev, NULL);
97 		platform_device_unregister(pdev);
98 		xcp_dev[pdev_num] = NULL;
99 	}
100 	pdev_num = 0;
101 }
102 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
103 
104 static void __exit amdgpu_xcp_drv_exit(void)
105 {
106 	amdgpu_xcp_drv_release();
107 }
108 
109 module_exit(amdgpu_xcp_drv_exit);
110 
111 MODULE_AUTHOR("AMD linux driver team");
112 MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
113 MODULE_LICENSE("GPL and additional rights");
114