xref: /linux/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
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 static DEFINE_MUTEX(xcp_mutex);
50 
51 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
52 {
53 	struct platform_device *pdev;
54 	struct xcp_device *pxcp_dev;
55 	char dev_name[20];
56 	int ret, i;
57 
58 	guard(mutex)(&xcp_mutex);
59 
60 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
61 		return -ENODEV;
62 
63 	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
64 		if (!xcp_dev[i])
65 			break;
66 	}
67 
68 	if (i >= MAX_XCP_PLATFORM_DEVICE)
69 		return -ENODEV;
70 
71 	snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);
72 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
73 	if (IS_ERR(pdev))
74 		return PTR_ERR(pdev);
75 
76 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
77 		ret = -ENOMEM;
78 		goto out_unregister;
79 	}
80 
81 	pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
82 	if (IS_ERR(pxcp_dev)) {
83 		ret = PTR_ERR(pxcp_dev);
84 		goto out_devres;
85 	}
86 
87 	xcp_dev[i] = pxcp_dev;
88 	xcp_dev[i]->pdev = pdev;
89 	*ddev = &pxcp_dev->drm;
90 	pdev_num++;
91 
92 	return 0;
93 
94 out_devres:
95 	devres_release_group(&pdev->dev, NULL);
96 out_unregister:
97 	platform_device_unregister(pdev);
98 
99 	return ret;
100 }
101 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
102 
103 static void free_xcp_dev(int8_t index)
104 {
105 	if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {
106 		struct platform_device *pdev = xcp_dev[index]->pdev;
107 
108 		devres_release_group(&pdev->dev, NULL);
109 		platform_device_unregister(pdev);
110 
111 		xcp_dev[index] = NULL;
112 		pdev_num--;
113 	}
114 }
115 
116 void amdgpu_xcp_drm_dev_free(struct drm_device *ddev)
117 {
118 	int8_t i;
119 
120 	guard(mutex)(&xcp_mutex);
121 
122 	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
123 		if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
124 			free_xcp_dev(i);
125 			break;
126 		}
127 	}
128 }
129 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
130 
131 void amdgpu_xcp_drv_release(void)
132 {
133 	int8_t i;
134 
135 	guard(mutex)(&xcp_mutex);
136 
137 	for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++) {
138 		free_xcp_dev(i);
139 	}
140 }
141 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
142 
143 static void __exit amdgpu_xcp_drv_exit(void)
144 {
145 	amdgpu_xcp_drv_release();
146 }
147 
148 module_exit(amdgpu_xcp_drv_exit);
149 
150 MODULE_AUTHOR("AMD linux driver team");
151 MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
152 MODULE_LICENSE("GPL and additional rights");
153