light_hwspinlock.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Light hardware spinlock driver
  4. *
  5. * Copyright (C) 2020-2025 Alibaba Group Holding Limited
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/hwspinlock.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include "hwspinlock_internal.h"
  19. struct light_hwspinlock {
  20. void __iomem *io_base;
  21. struct hwspinlock_device bank;
  22. };
  23. #define HW_SPINLOCK_NUMBER 64
  24. #define HW_SPINLOCK_OFFSET(x) (0x4 * (x))
  25. static int light_hwspinlock_trylock(struct hwspinlock *lock)
  26. {
  27. void __iomem *lock_addr = lock->priv;
  28. return !readl(lock_addr);
  29. }
  30. static void light_hwspinlock_unlock(struct hwspinlock *lock)
  31. {
  32. void __iomem *lock_addr = lock->priv;
  33. writel(0, lock_addr);
  34. }
  35. static const struct hwspinlock_ops light_hwspinlock_ops = {
  36. .trylock = light_hwspinlock_trylock,
  37. .unlock = light_hwspinlock_unlock,
  38. };
  39. static int light_hwspinlock_probe(struct platform_device *pdev)
  40. {
  41. struct light_hwspinlock *hwspin;
  42. struct hwspinlock *hwlock;
  43. int idx, ret;
  44. if (!pdev->dev.of_node)
  45. return -ENODEV;
  46. hwspin = devm_kzalloc(&pdev->dev,
  47. struct_size(hwspin, bank.lock,
  48. HW_SPINLOCK_NUMBER),
  49. GFP_KERNEL);
  50. if (!hwspin)
  51. return -ENOMEM;
  52. /* retrieve io base */
  53. hwspin->io_base = of_iomap(pdev->dev.of_node, 0);
  54. if (!hwspin->io_base)
  55. return -ENOMEM;
  56. pr_info("io_base: 0x%lx\n", (long)hwspin->io_base);
  57. for (idx = 0; idx < HW_SPINLOCK_NUMBER; idx++) {
  58. hwlock = &hwspin->bank.lock[idx];
  59. hwlock->priv = hwspin->io_base + HW_SPINLOCK_OFFSET(idx);
  60. }
  61. platform_set_drvdata(pdev, hwspin);
  62. pm_runtime_enable(&pdev->dev);
  63. ret = hwspin_lock_register(&hwspin->bank, &pdev->dev,
  64. &light_hwspinlock_ops, 0,
  65. HW_SPINLOCK_NUMBER);
  66. if (ret)
  67. goto reg_failed;
  68. return 0;
  69. reg_failed:
  70. pm_runtime_disable(&pdev->dev);
  71. iounmap(hwspin->io_base);
  72. return ret;
  73. }
  74. static int light_hwspinlock_remove(struct platform_device *pdev)
  75. {
  76. struct light_hwspinlock *hwspin = platform_get_drvdata(pdev);
  77. int ret;
  78. ret = hwspin_lock_unregister(&hwspin->bank);
  79. if (ret) {
  80. dev_err(&pdev->dev, "%s failed: %d\n", __func__, ret);
  81. return ret;
  82. }
  83. pm_runtime_disable(&pdev->dev);
  84. iounmap(hwspin->io_base);
  85. return 0;
  86. }
  87. static const struct of_device_id light_hwpinlock_ids[] = {
  88. { .compatible = "light,hwspinlock", },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, light_hwpinlock_ids);
  92. static struct platform_driver light_hwspinlock_driver = {
  93. .probe = light_hwspinlock_probe,
  94. .remove = light_hwspinlock_remove,
  95. .driver = {
  96. .name = "light_hwspinlock",
  97. .of_match_table = of_match_ptr(light_hwpinlock_ids),
  98. },
  99. };
  100. module_platform_driver(light_hwspinlock_driver);
  101. MODULE_LICENSE("GPL v2");