Path: blob/master/drivers/gpu/drm/amd/amdxcp/amdgpu_xcp_drv.c
29286 views
/*1* Copyright 2023 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21*/2223#include <linux/export.h>24#include <linux/init.h>25#include <linux/module.h>26#include <linux/platform_device.h>2728#include <drm/drm_drv.h>2930#include "amdgpu_xcp_drv.h"3132#define MAX_XCP_PLATFORM_DEVICE 643334struct xcp_device {35struct drm_device drm;36struct platform_device *pdev;37};3839static 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};4546static int8_t pdev_num;47static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];48static DEFINE_MUTEX(xcp_mutex);4950int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)51{52struct platform_device *pdev;53struct xcp_device *pxcp_dev;54char dev_name[20];55int ret, i;5657guard(mutex)(&xcp_mutex);5859if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)60return -ENODEV;6162for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {63if (!xcp_dev[i])64break;65}6667if (i >= MAX_XCP_PLATFORM_DEVICE)68return -ENODEV;6970snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", i);71pdev = platform_device_register_simple(dev_name, -1, NULL, 0);72if (IS_ERR(pdev))73return PTR_ERR(pdev);7475if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {76ret = -ENOMEM;77goto out_unregister;78}7980pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);81if (IS_ERR(pxcp_dev)) {82ret = PTR_ERR(pxcp_dev);83goto out_devres;84}8586xcp_dev[i] = pxcp_dev;87xcp_dev[i]->pdev = pdev;88*ddev = &pxcp_dev->drm;89pdev_num++;9091return 0;9293out_devres:94devres_release_group(&pdev->dev, NULL);95out_unregister:96platform_device_unregister(pdev);9798return ret;99}100EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);101102static void free_xcp_dev(int8_t index)103{104if ((index < MAX_XCP_PLATFORM_DEVICE) && (xcp_dev[index])) {105struct platform_device *pdev = xcp_dev[index]->pdev;106107devres_release_group(&pdev->dev, NULL);108platform_device_unregister(pdev);109110xcp_dev[index] = NULL;111pdev_num--;112}113}114115void amdgpu_xcp_drm_dev_free(struct drm_device *ddev)116{117int8_t i;118119guard(mutex)(&xcp_mutex);120121for (i = 0; i < MAX_XCP_PLATFORM_DEVICE; i++) {122if ((xcp_dev[i]) && (&xcp_dev[i]->drm == ddev)) {123free_xcp_dev(i);124break;125}126}127}128EXPORT_SYMBOL(amdgpu_xcp_drm_dev_free);129130void amdgpu_xcp_drv_release(void)131{132int8_t i;133134guard(mutex)(&xcp_mutex);135136for (i = 0; pdev_num && i < MAX_XCP_PLATFORM_DEVICE; i++) {137free_xcp_dev(i);138}139}140EXPORT_SYMBOL(amdgpu_xcp_drv_release);141142static void __exit amdgpu_xcp_drv_exit(void)143{144amdgpu_xcp_drv_release();145}146147module_exit(amdgpu_xcp_drv_exit);148149MODULE_AUTHOR("AMD linux driver team");150MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");151MODULE_LICENSE("GPL and additional rights");152153154