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