xref: /linux/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c (revision 18fbf5151d2c0bfe433c7428eef03cabf5fdb2fa)
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 #include <linux/slab.h>
29 
30 #include <drm/drm_drv.h>
31 
32 #include "amdgpu_xcp_drv.h"
33 
34 #define MAX_XCP_PLATFORM_DEVICE 64
35 
36 struct xcp_device {
37 	struct drm_device drm;
38 	struct platform_device *pdev;
39 };
40 
41 static const struct drm_driver amdgpu_xcp_driver = {
42 	.driver_features = DRIVER_GEM | DRIVER_RENDER,
43 	.name = "amdgpu_xcp_drv",
44 	.major = 1,
45 	.minor = 0,
46 };
47 
48 static u8 pdev_num;
49 static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
50 static DEFINE_MUTEX(xcp_mutex);
51 
52 int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
53 {
54 	struct platform_device *pdev;
55 	struct xcp_device *pxcp_dev;
56 	char *dev_name;
57 	int ret, i;
58 
59 	if (!ddev)
60 		return -EINVAL;
61 
62 	BUILD_BUG_ON(MAX_XCP_PLATFORM_DEVICE >= U8_MAX);
63 
64 	guard(mutex)(&xcp_mutex);
65 
66 	if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
67 		return -ENODEV;
68 
69 	for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {
70 		if (!xcp_dev[i])
71 			break;
72 	}
73 
74 	if (i >= MAX_XCP_PLATFORM_DEVICE)
75 		return -ENODEV;
76 
77 	dev_name = kasprintf(GFP_KERNEL, "amdgpu_xcp_%d", i);
78 	if (!dev_name)
79 		return -ENOMEM;
80 
81 	pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
82 	kfree(dev_name);
83 	if (IS_ERR(pdev))
84 		return PTR_ERR(pdev);
85 
86 	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
87 		ret = -ENOMEM;
88 		goto out_unregister;
89 	}
90 
91 	pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
92 	if (IS_ERR(pxcp_dev)) {
93 		ret = PTR_ERR(pxcp_dev);
94 		goto out_devres;
95 	}
96 
97 	xcp_dev[i] = pxcp_dev;
98 	xcp_dev[i]->pdev = pdev;
99 	*ddev = &pxcp_dev->drm;
100 	pdev_num++;
101 
102 	return 0;
103 
104 out_devres:
105 	devres_release_group(&pdev->dev, NULL);
106 out_unregister:
107 	platform_device_unregister(pdev);
108 
109 	return ret;
110 }
111 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
112 
113 static void free_xcp_dev(uint8_t index)
114 {
115 	if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {
116 		struct platform_device *pdev = xcp_dev[index]->pdev;
117 
118 		devres_release_group(&pdev->dev, NULL);
119 		platform_device_unregister(pdev);
120 
121 		xcp_dev[index] = NULL;
122 		if (pdev_num > 0)
123 			pdev_num--;
124 	}
125 }
126 
127 void amdgpu_xcp_drm_dev_free(struct drm_device *ddev)
128 {
129 	uint8_t i;
130 
131 	guard(mutex)(&xcp_mutex);
132 
133 	for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++) {
134 		if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {
135 			free_xcp_dev(i);
136 			break;
137 		}
138 	}
139 }
140 EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);
141 
142 void amdgpu_xcp_drv_release(void)
143 {
144 	uint8_t i;
145 
146 	guard(mutex)(&xcp_mutex);
147 
148 	for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++) {
149 		free_xcp_dev(i);
150 	}
151 }
152 EXPORT_SYMBOL(amdgpu_xcp_drv_release);
153 
154 static void __exit amdgpu_xcp_drv_exit(void)
155 {
156 	amdgpu_xcp_drv_release();
157 }
158 
159 module_exit(amdgpu_xcp_drv_exit);
160 
161 MODULE_AUTHOR("AMD linux driver team");
162 MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
163 MODULE_LICENSE("GPL and additional rights");
164