gpu_sysfs_main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "gpu_sysfs_header.h"
  2. /* Platform device global pointer. */
  3. static struct platform_device *gpu_sysfs_pdev;
  4. /* *
  5. * *********************************************************************
  6. * Device attribute macros, linking functions with sysfs.
  7. * *********************************************************************
  8. * */
  9. DEVICE_ATTR(gpu_min_clock, 0444, gpu_min_clock_show, NULL);
  10. DEVICE_ATTR(gpu_max_clock, 0444, gpu_max_clock_show, NULL);
  11. DEVICE_ATTR(gpu_busy, 0444, gpu_busy_show, NULL);
  12. DEVICE_ATTR(gpu_voltage, 0444, gpu_vol_show, NULL);
  13. DEVICE_ATTR(gpu_clock, 0666, gpu_freq_show, gpu_freq_write);
  14. DEVICE_ATTR(gpu_freq_table, 0444, gpu_freq_table_show, NULL);
  15. DEVICE_ATTR(gpu_governor, 0666, gpu_governor_show, gpu_governor_write);
  16. DEVICE_ATTR(gpu_available_governor, 0444, gpu_available_governor_show, NULL);
  17. DEVICE_ATTR(gpu_cores_config, 0444, gpu_cores_config_show, NULL);
  18. DEVICE_ATTR(gpu_tmu, 0444, gpu_tmu_show, NULL);
  19. DEVICE_ATTR(gpu_model, 0444, gpu_model_show, NULL);
  20. DEVICE_ATTR(gpu_version, 0444, gpu_version_show, NULL);
  21. DEVICE_ATTR(gpu_mem, 0444, gpu_mem_show, NULL);
  22. DEVICE_ATTR(fps, 0666, fps_show, fps_write);
  23. /* The below function will generate sysfs during initialization stage. */
  24. int gpu_sysfs_create_sysfs_files(struct device *dev)
  25. {
  26. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  27. if (device_create_file(dev, &dev_attr_gpu_min_clock))
  28. {
  29. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  30. goto out;
  31. }
  32. if (device_create_file(dev, &dev_attr_gpu_max_clock))
  33. {
  34. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  35. goto out;
  36. }
  37. if (device_create_file(dev, &dev_attr_gpu_busy))
  38. {
  39. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  40. goto out;
  41. }
  42. if (device_create_file(dev, &dev_attr_gpu_voltage))
  43. {
  44. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  45. goto out;
  46. }
  47. if (device_create_file(dev, &dev_attr_gpu_clock))
  48. {
  49. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  50. goto out;
  51. }
  52. if (device_create_file(dev, &dev_attr_gpu_freq_table))
  53. {
  54. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  55. goto out;
  56. }
  57. if (device_create_file(dev, &dev_attr_gpu_governor))
  58. {
  59. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  60. goto out;
  61. }
  62. if (device_create_file(dev, &dev_attr_gpu_available_governor))
  63. {
  64. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  65. goto out;
  66. }
  67. if (device_create_file(dev, &dev_attr_gpu_cores_config))
  68. {
  69. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  70. goto out;
  71. }
  72. if (device_create_file(dev, &dev_attr_gpu_tmu))
  73. {
  74. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  75. goto out;
  76. }
  77. if (device_create_file(dev, &dev_attr_gpu_model))
  78. {
  79. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  80. goto out;
  81. }
  82. if (device_create_file(dev, &dev_attr_gpu_version))
  83. {
  84. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  85. goto out;
  86. }
  87. if (device_create_file(dev, &dev_attr_gpu_mem))
  88. {
  89. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  90. goto out;
  91. }
  92. if (device_create_file(dev, &dev_attr_fps))
  93. {
  94. pr_info("GPU_SYSFS: Couldn't create sysfs file %d\n", __LINE__);
  95. goto out;
  96. }
  97. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  98. return 0;
  99. out:
  100. return -ENOENT;
  101. }
  102. void gpu_sysfs_remove_sysfs_files(struct device *dev)
  103. {
  104. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  105. device_remove_file(dev, &dev_attr_gpu_min_clock);
  106. device_remove_file(dev, &dev_attr_gpu_max_clock);
  107. device_remove_file(dev, &dev_attr_gpu_busy);
  108. device_remove_file(dev, &dev_attr_gpu_voltage);
  109. device_remove_file(dev, &dev_attr_gpu_clock);
  110. device_remove_file(dev, &dev_attr_gpu_freq_table);
  111. device_remove_file(dev, &dev_attr_gpu_governor);
  112. device_remove_file(dev, &dev_attr_gpu_available_governor);
  113. device_remove_file(dev, &dev_attr_gpu_cores_config);
  114. device_remove_file(dev, &dev_attr_gpu_tmu);
  115. device_remove_file(dev, &dev_attr_gpu_model);
  116. device_remove_file(dev, &dev_attr_gpu_version);
  117. device_remove_file(dev, &dev_attr_gpu_mem);
  118. device_remove_file(dev, &dev_attr_fps);
  119. }
  120. sruk_bool gpu_sysfs_device_init(sruk_device * const kbdev)
  121. {
  122. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  123. if (gpu_sysfs_create_sysfs_files(kbdev->osdev.dev))
  124. return SRUK_FALSE;
  125. return SRUK_TRUE;
  126. }
  127. static int gpu_sysfs_probe(struct platform_device *pdev)
  128. {
  129. struct sruk_device *kbdev;
  130. struct sruk_os_device *osdev;
  131. int err;
  132. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  133. kbdev = kzalloc(sizeof(sruk_device), GFP_KERNEL);
  134. if (!kbdev)
  135. {
  136. dev_err(&pdev->dev, "GPU_SYSFS: Can't allocate device\n");
  137. err = -ENOMEM;
  138. goto out;
  139. }
  140. osdev = &kbdev->osdev;
  141. osdev->dev = &pdev->dev;
  142. if (SRUK_TRUE != gpu_sysfs_device_init(kbdev)) {
  143. pr_info( "GPU_SYSFS: gpu_sysfs_device_init failed");
  144. dev_err(&pdev->dev, "GPU_SYSFS: Can't initialize device\n");
  145. err = -ENOMEM;
  146. goto out_reg_unmap;
  147. }
  148. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  149. osdev = NULL;
  150. kzfree(kbdev);
  151. return 0;
  152. out_reg_unmap:
  153. osdev = NULL;
  154. kzfree(kbdev);
  155. out:
  156. return err;
  157. }
  158. static int gpu_sysfs_remove(struct platform_device *pdev)
  159. {
  160. struct sruk_device *kbdev = (struct sruk_device *)(&pdev->dev);
  161. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  162. if (!kbdev)
  163. return -ENODEV;
  164. gpu_sysfs_remove_sysfs_files(kbdev->osdev.dev);
  165. kzfree(kbdev);
  166. return 0;
  167. }
  168. /* Platform driver operations and assigning function pointers. */
  169. static struct platform_driver gpu_sysfs_platform_driver =
  170. {
  171. .probe = gpu_sysfs_probe,
  172. .remove = gpu_sysfs_remove,
  173. .driver =
  174. {
  175. .name = GPU_SYSFS_MODULE_NAME,
  176. .owner = THIS_MODULE,
  177. },
  178. };
  179. static int __init gpu_sysfs_init(void)
  180. {
  181. int ret;
  182. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  183. gpu_sysfs_pdev = platform_device_alloc(GPU_SYSFS_MODULE_NAME, -1);
  184. if (!gpu_sysfs_pdev)
  185. {
  186. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  187. pr_err("Failed to allocate dummy regulator device\n");
  188. return 0;
  189. }
  190. /* Add platform device. */
  191. ret = platform_device_add(gpu_sysfs_pdev);
  192. if (ret != 0)
  193. {
  194. pr_err("GPU_SYSFS: Failed to register dummy regulator device: %d\n", ret);
  195. platform_device_put(gpu_sysfs_pdev);
  196. return 0;
  197. }
  198. ret = platform_driver_register(&gpu_sysfs_platform_driver);
  199. if (ret != 0)
  200. {
  201. pr_err("GPU_SYSFS: Failed to register dummy regulator driver: %d\n", ret);
  202. platform_device_unregister(gpu_sysfs_pdev);
  203. }
  204. pr_info("GPU_SYSFS ----------- %s -- %d", __FUNCTION__, __LINE__);
  205. return 0;
  206. }
  207. static void __exit gpu_sysfs_exit(void)
  208. {
  209. pr_info( "GPU_SYSFS: Unified Sysfs for GPU: Exiting" );
  210. platform_driver_unregister(&gpu_sysfs_platform_driver);
  211. }
  212. module_init(gpu_sysfs_init);
  213. module_exit(gpu_sysfs_exit);
  214. MODULE_AUTHOR("SRUK_GFX");
  215. MODULE_DESCRIPTION("GPU_SYSFS: Module for Unified Sysfs for GPU");
  216. MODULE_LICENSE("GPL");
  217. MODULE_VERSION("1.0");