amdgpu_xcp_drv.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <drm/drm_drv.h>
  27. #include "amdgpu_xcp_drv.h"
  28. #define MAX_XCP_PLATFORM_DEVICE 64
  29. struct xcp_device {
  30. struct drm_device drm;
  31. struct platform_device *pdev;
  32. };
  33. static const struct drm_driver amdgpu_xcp_driver = {
  34. .driver_features = DRIVER_GEM | DRIVER_RENDER,
  35. .name = "amdgpu_xcp_drv",
  36. .major = 1,
  37. .minor = 0,
  38. };
  39. static int8_t pdev_num;
  40. static struct xcp_device *xcp_dev[MAX_XCP_PLATFORM_DEVICE];
  41. int amdgpu_xcp_drm_dev_alloc(struct drm_device **ddev)
  42. {
  43. struct platform_device *pdev;
  44. struct xcp_device *pxcp_dev;
  45. char dev_name[20];
  46. int ret;
  47. if (pdev_num >= MAX_XCP_PLATFORM_DEVICE)
  48. return -ENODEV;
  49. snprintf(dev_name, sizeof(dev_name), "amdgpu_xcp_%d", pdev_num);
  50. pdev = platform_device_register_simple(dev_name, -1, NULL, 0);
  51. if (IS_ERR(pdev))
  52. return PTR_ERR(pdev);
  53. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
  54. ret = -ENOMEM;
  55. goto out_unregister;
  56. }
  57. pxcp_dev = devm_drm_dev_alloc(&pdev->dev, &amdgpu_xcp_driver, struct xcp_device, drm);
  58. if (IS_ERR(pxcp_dev)) {
  59. ret = PTR_ERR(pxcp_dev);
  60. goto out_devres;
  61. }
  62. xcp_dev[pdev_num] = pxcp_dev;
  63. xcp_dev[pdev_num]->pdev = pdev;
  64. *ddev = &pxcp_dev->drm;
  65. pdev_num++;
  66. return 0;
  67. out_devres:
  68. devres_release_group(&pdev->dev, NULL);
  69. out_unregister:
  70. platform_device_unregister(pdev);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL(amdgpu_xcp_drm_dev_alloc);
  74. void amdgpu_xcp_drv_release(void)
  75. {
  76. for (--pdev_num; pdev_num >= 0; --pdev_num) {
  77. struct platform_device *pdev = xcp_dev[pdev_num]->pdev;
  78. devres_release_group(&pdev->dev, NULL);
  79. platform_device_unregister(pdev);
  80. xcp_dev[pdev_num] = NULL;
  81. }
  82. pdev_num = 0;
  83. }
  84. EXPORT_SYMBOL(amdgpu_xcp_drv_release);
  85. static void __exit amdgpu_xcp_drv_exit(void)
  86. {
  87. amdgpu_xcp_drv_release();
  88. }
  89. module_exit(amdgpu_xcp_drv_exit);
  90. MODULE_AUTHOR("AMD linux driver team");
  91. MODULE_DESCRIPTION("AMD XCP PLATFORM DEVICES");
  92. MODULE_LICENSE("GPL and additional rights");