hdpu_cpustate.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Sky CPU State Driver
  3. *
  4. * Copyright (C) 2002 Brian Waite
  5. *
  6. * This driver allows use of the CPU state bits
  7. * It exports the /dev/sky_cpustate and also
  8. * /proc/sky_cpustate pseudo-file for status information.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. *
  15. */
  16. #include <linux/version.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/pci.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/device.h>
  24. #include <asm/uaccess.h>
  25. #include <linux/hdpu_features.h>
  26. #define SKY_CPUSTATE_VERSION "1.1"
  27. static int hdpu_cpustate_probe(struct device *ddev);
  28. static int hdpu_cpustate_remove(struct device *ddev);
  29. struct cpustate_t cpustate;
  30. static int cpustate_get_ref(int excl)
  31. {
  32. int retval = -EBUSY;
  33. spin_lock(&cpustate.lock);
  34. if (cpustate.excl)
  35. goto out_busy;
  36. if (excl) {
  37. if (cpustate.open_count)
  38. goto out_busy;
  39. cpustate.excl = 1;
  40. }
  41. cpustate.open_count++;
  42. retval = 0;
  43. out_busy:
  44. spin_unlock(&cpustate.lock);
  45. return retval;
  46. }
  47. static int cpustate_free_ref(void)
  48. {
  49. spin_lock(&cpustate.lock);
  50. cpustate.excl = 0;
  51. cpustate.open_count--;
  52. spin_unlock(&cpustate.lock);
  53. return 0;
  54. }
  55. unsigned char cpustate_get_state(void)
  56. {
  57. return cpustate.cached_val;
  58. }
  59. void cpustate_set_state(unsigned char new_state)
  60. {
  61. unsigned int state = (new_state << 21);
  62. #ifdef DEBUG_CPUSTATE
  63. printk("CPUSTATE -> 0x%x\n", new_state);
  64. #endif
  65. spin_lock(&cpustate.lock);
  66. cpustate.cached_val = new_state;
  67. writel((0xff << 21), cpustate.clr_addr);
  68. writel(state, cpustate.set_addr);
  69. spin_unlock(&cpustate.lock);
  70. }
  71. /*
  72. * Now all the various file operations that we export.
  73. */
  74. static ssize_t cpustate_read(struct file *file, char *buf,
  75. size_t count, loff_t * ppos)
  76. {
  77. unsigned char data;
  78. if (count < 0)
  79. return -EFAULT;
  80. if (count == 0)
  81. return 0;
  82. data = cpustate_get_state();
  83. if (copy_to_user(buf, &data, sizeof(unsigned char)))
  84. return -EFAULT;
  85. return sizeof(unsigned char);
  86. }
  87. static ssize_t cpustate_write(struct file *file, const char *buf,
  88. size_t count, loff_t * ppos)
  89. {
  90. unsigned char data;
  91. if (count < 0)
  92. return -EFAULT;
  93. if (count == 0)
  94. return 0;
  95. if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
  96. return -EFAULT;
  97. cpustate_set_state(data);
  98. return sizeof(unsigned char);
  99. }
  100. static int cpustate_open(struct inode *inode, struct file *file)
  101. {
  102. return cpustate_get_ref((file->f_flags & O_EXCL));
  103. }
  104. static int cpustate_release(struct inode *inode, struct file *file)
  105. {
  106. return cpustate_free_ref();
  107. }
  108. /*
  109. * Info exported via "/proc/sky_cpustate".
  110. */
  111. static int cpustate_read_proc(char *page, char **start, off_t off,
  112. int count, int *eof, void *data)
  113. {
  114. char *p = page;
  115. int len = 0;
  116. p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
  117. len = p - page;
  118. if (len <= off + count)
  119. *eof = 1;
  120. *start = page + off;
  121. len -= off;
  122. if (len > count)
  123. len = count;
  124. if (len < 0)
  125. len = 0;
  126. return len;
  127. }
  128. static struct device_driver hdpu_cpustate_driver = {
  129. .name = HDPU_CPUSTATE_NAME,
  130. .bus = &platform_bus_type,
  131. .probe = hdpu_cpustate_probe,
  132. .remove = hdpu_cpustate_remove,
  133. };
  134. /*
  135. * The various file operations we support.
  136. */
  137. static struct file_operations cpustate_fops = {
  138. owner:THIS_MODULE,
  139. open:cpustate_open,
  140. release:cpustate_release,
  141. read:cpustate_read,
  142. write:cpustate_write,
  143. fasync:NULL,
  144. poll:NULL,
  145. ioctl:NULL,
  146. llseek:no_llseek,
  147. };
  148. static struct miscdevice cpustate_dev = {
  149. MISC_DYNAMIC_MINOR,
  150. "sky_cpustate",
  151. &cpustate_fops
  152. };
  153. static int hdpu_cpustate_probe(struct device *ddev)
  154. {
  155. struct platform_device *pdev = to_platform_device(ddev);
  156. struct resource *res;
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. cpustate.set_addr = (unsigned long *)res->start;
  159. cpustate.clr_addr = (unsigned long *)res->end - 1;
  160. misc_register(&cpustate_dev);
  161. create_proc_read_entry("sky_cpustate", 0, 0, cpustate_read_proc, NULL);
  162. printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
  163. return 0;
  164. }
  165. static int hdpu_cpustate_remove(struct device *ddev)
  166. {
  167. cpustate.set_addr = 0;
  168. cpustate.clr_addr = 0;
  169. remove_proc_entry("sky_cpustate", NULL);
  170. misc_deregister(&cpustate_dev);
  171. return 0;
  172. }
  173. static int __init cpustate_init(void)
  174. {
  175. int rc;
  176. rc = driver_register(&hdpu_cpustate_driver);
  177. return rc;
  178. }
  179. static void __exit cpustate_exit(void)
  180. {
  181. driver_unregister(&hdpu_cpustate_driver);
  182. }
  183. module_init(cpustate_init);
  184. module_exit(cpustate_exit);
  185. MODULE_AUTHOR("Brian Waite");
  186. MODULE_LICENSE("GPL");