msm-buspm-dev.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* #define DEBUG */
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/mm.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/errno.h>
  19. #include <linux/device.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/memory_alloc.h>
  23. #include <mach/rpm-smd.h>
  24. #include "msm-buspm-dev.h"
  25. #include <linux/clk.h>
  26. #define MSM_BUSPM_DRV_NAME "msm-buspm-dev"
  27. enum msm_buspm_spdm_res {
  28. SPDM_RES_ID = 0,
  29. SPDM_RES_TYPE = 0x63707362,
  30. SPDM_KEY = 0x00006e65,
  31. SPDM_SIZE = 4,
  32. };
  33. /*
  34. * Allocate kernel buffer.
  35. * Currently limited to one buffer per file descriptor. If alloc() is
  36. * called twice for the same descriptor, the original buffer is freed.
  37. * There is also no locking protection so the same descriptor can not be shared.
  38. */
  39. static inline void *msm_buspm_dev_get_vaddr(struct file *filp)
  40. {
  41. struct msm_buspm_map_dev *dev = filp->private_data;
  42. return (dev) ? dev->vaddr : NULL;
  43. }
  44. static inline unsigned int msm_buspm_dev_get_buflen(struct file *filp)
  45. {
  46. struct msm_buspm_map_dev *dev = filp->private_data;
  47. return dev ? dev->buflen : 0;
  48. }
  49. static inline unsigned long msm_buspm_dev_get_paddr(struct file *filp)
  50. {
  51. struct msm_buspm_map_dev *dev = filp->private_data;
  52. return (dev) ? dev->paddr : 0L;
  53. }
  54. static void msm_buspm_dev_free(struct file *filp)
  55. {
  56. struct msm_buspm_map_dev *dev = filp->private_data;
  57. if (dev) {
  58. pr_debug("freeing memory at 0x%p\n", dev->vaddr);
  59. free_contiguous_memory(dev->vaddr);
  60. dev->paddr = 0L;
  61. dev->vaddr = NULL;
  62. }
  63. }
  64. static int msm_buspm_dev_open(struct inode *inode, struct file *filp)
  65. {
  66. struct msm_buspm_map_dev *dev;
  67. if (capable(CAP_SYS_ADMIN)) {
  68. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  69. if (dev)
  70. filp->private_data = dev;
  71. else
  72. return -ENOMEM;
  73. } else {
  74. return -EPERM;
  75. }
  76. return 0;
  77. }
  78. static int
  79. msm_buspm_dev_alloc(struct file *filp, struct buspm_alloc_params data)
  80. {
  81. unsigned long paddr;
  82. void *vaddr;
  83. struct msm_buspm_map_dev *dev = filp->private_data;
  84. /* If buffer already allocated, then free it */
  85. if (dev->vaddr)
  86. msm_buspm_dev_free(filp);
  87. /* Allocate uncached memory */
  88. vaddr = allocate_contiguous_ebi(data.size, PAGE_SIZE, 0);
  89. paddr = (vaddr) ? memory_pool_node_paddr(vaddr) : 0L;
  90. if (vaddr == NULL) {
  91. pr_err("allocation of 0x%x bytes failed", data.size);
  92. return -ENOMEM;
  93. }
  94. dev->vaddr = vaddr;
  95. dev->paddr = paddr;
  96. dev->buflen = data.size;
  97. filp->f_pos = 0;
  98. pr_debug("virt addr = 0x%p\n", dev->vaddr);
  99. pr_debug("phys addr = 0x%lx\n", dev->paddr);
  100. return 0;
  101. }
  102. static int msm_bus_rpm_req(u32 rsc_type, u32 key, u32 hwid,
  103. int ctx, u32 val)
  104. {
  105. struct msm_rpm_request *rpm_req;
  106. int ret, msg_id;
  107. rpm_req = msm_rpm_create_request(ctx, rsc_type, hwid, 1);
  108. if (rpm_req == NULL) {
  109. pr_err("RPM: Couldn't create RPM Request\n");
  110. return -ENXIO;
  111. }
  112. ret = msm_rpm_add_kvp_data(rpm_req, key, (const uint8_t *)&val,
  113. (int)(sizeof(uint32_t)));
  114. if (ret) {
  115. pr_err("RPM: Add KVP failed for RPM Req:%u\n",
  116. rsc_type);
  117. goto err;
  118. }
  119. pr_debug("Added Key: %d, Val: %u, size: %d\n", key,
  120. (uint32_t)val, sizeof(uint32_t));
  121. msg_id = msm_rpm_send_request(rpm_req);
  122. if (!msg_id) {
  123. pr_err("RPM: No message ID for req\n");
  124. ret = -ENXIO;
  125. goto err;
  126. }
  127. ret = msm_rpm_wait_for_ack(msg_id);
  128. if (ret) {
  129. pr_err("RPM: Ack failed\n");
  130. goto err;
  131. }
  132. err:
  133. msm_rpm_free_request(rpm_req);
  134. return ret;
  135. }
  136. static int msm_buspm_bus_set(uint32_t arg)
  137. {
  138. unsigned int type = 0x0;
  139. unsigned int id = 0x0;
  140. unsigned int key = 0x0;
  141. int ret = 0;
  142. struct msm_buspm_bus_set bs;
  143. char *clockmap[] = {
  144. [0x0] = "pnoc_a_clk",
  145. [0x1] = "snoc_a_clk",
  146. [0x2] = "cnoc_a_clk",
  147. [0x10] = "bimc_a_clk",
  148. [0x20] = "pnoc_clk",
  149. [0x21] = "snoc_clk",
  150. [0x22] = "cnoc_clk",
  151. [0x30] = "bimc_clk",
  152. };
  153. if (copy_from_user(&bs, (void __user *)arg, sizeof(bs)))
  154. return -EFAULT;
  155. switch (bs.nocid) {
  156. case 0x0: //PNOC
  157. case 0x1: //SNOC
  158. case 0x2: //CNOC
  159. case 0x3: //MMSSNOC_AHB
  160. type = 0x316b6c63;
  161. id = bs.nocid;
  162. key = 0x0078616D;
  163. break;
  164. case 0x10: //BIMC
  165. case 0x11: //OXILI
  166. case 0x12: //OCMEM
  167. type = 0x326b6c63;
  168. id = bs.nocid;
  169. id &= ~0xF0;
  170. key = 0x0078616D;
  171. break;
  172. default:
  173. break;
  174. }
  175. switch (bs.op) {
  176. case MSM_BUSPM_BUS_MAX_SET:
  177. {
  178. ret = msm_bus_rpm_req(type, key, id, bs.set, bs.max);
  179. pr_err("MAXSET: %d NOC %u Khz MAX ret=%d\n", bs.nocid, bs.max, ret);
  180. }
  181. break;
  182. case MSM_BUSPM_BUS_MAX_CLR:
  183. {
  184. ret = msm_bus_rpm_req(type, key, id, bs.set, 0xFFFFFFFF);
  185. pr_err("MAXCLR: %d NOC %u Khz CLEAR MAX ret=%d\n", bs.nocid, 0xFFFFFFFF, ret);
  186. }
  187. break;
  188. case MSM_BUSPM_BUS_MIN_SET:
  189. case MSM_BUSPM_BUS_MIN_CLR:
  190. {
  191. struct clk * clk;
  192. clk = clk_get_sys("msm-buspm", clockmap[bs.set*0x10+bs.nocid]);
  193. if (IS_ERR(clk)) {
  194. pr_err("MINCLR: no such clock\n");
  195. return -EINVAL;
  196. }
  197. if (bs.op == MSM_BUSPM_BUS_MIN_SET) {
  198. clk_set_rate(clk, bs.min*1000);
  199. clk_prepare_enable(clk);
  200. pr_err("MINSET: %d NOC %u Khz MIN\n", bs.nocid, bs.min);
  201. }
  202. else {
  203. clk_disable_unprepare(clk);
  204. pr_err("MINCLR: %d NOC MIN CLR\n", bs.nocid);
  205. }
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. return ret;
  212. }
  213. static int msm_buspm_ioc_cmds(uint32_t arg)
  214. {
  215. int ret = 0;
  216. switch (arg) {
  217. case MSM_BUSPM_SPDM_CLK_DIS:
  218. case MSM_BUSPM_SPDM_CLK_EN:
  219. return msm_bus_rpm_req(SPDM_RES_TYPE, SPDM_KEY, SPDM_RES_ID,
  220. MSM_RPM_CTX_ACTIVE_SET, arg);
  221. default:
  222. ret = msm_buspm_bus_set(arg);
  223. if (ret) {
  224. pr_warn("Unsupported ioctl command: %d\n", arg);
  225. return -EINVAL;
  226. }
  227. return ret;
  228. }
  229. }
  230. static long
  231. msm_buspm_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  232. {
  233. struct buspm_xfer_req xfer;
  234. struct buspm_alloc_params alloc_data;
  235. unsigned long paddr;
  236. int retval = 0;
  237. void *buf = msm_buspm_dev_get_vaddr(filp);
  238. unsigned int buflen = msm_buspm_dev_get_buflen(filp);
  239. unsigned char *dbgbuf = buf;
  240. if (_IOC_TYPE(cmd) != MSM_BUSPM_IOC_MAGIC) {
  241. pr_err("Wrong IOC_MAGIC.Exiting\n");
  242. return -ENOTTY;
  243. }
  244. switch (cmd) {
  245. case MSM_BUSPM_IOC_FREE:
  246. pr_debug("cmd = 0x%x (FREE)\n", cmd);
  247. msm_buspm_dev_free(filp);
  248. break;
  249. case MSM_BUSPM_IOC_ALLOC:
  250. pr_debug("cmd = 0x%x (ALLOC)\n", cmd);
  251. retval = __get_user(alloc_data.size, (size_t __user *)arg);
  252. if (retval == 0)
  253. retval = msm_buspm_dev_alloc(filp, alloc_data);
  254. break;
  255. case MSM_BUSPM_IOC_RD_PHYS_ADDR:
  256. pr_debug("Read Physical Address\n");
  257. paddr = msm_buspm_dev_get_paddr(filp);
  258. if (paddr == 0L) {
  259. retval = -EINVAL;
  260. } else {
  261. pr_debug("phys addr = 0x%lx\n", paddr);
  262. retval = __put_user(paddr,
  263. (unsigned long __user *)arg);
  264. }
  265. break;
  266. case MSM_BUSPM_IOC_RDBUF:
  267. pr_debug("Read Buffer: 0x%x%x%x%x\n",
  268. dbgbuf[0], dbgbuf[1], dbgbuf[2], dbgbuf[3]);
  269. if (!buf) {
  270. retval = -EINVAL;
  271. break;
  272. }
  273. if (copy_from_user(&xfer, (void __user *)arg, sizeof(xfer))) {
  274. retval = -EFAULT;
  275. break;
  276. }
  277. if ((xfer.size <= buflen) &&
  278. (copy_to_user((void __user *)xfer.data, buf,
  279. xfer.size))) {
  280. retval = -EFAULT;
  281. break;
  282. }
  283. break;
  284. case MSM_BUSPM_IOC_WRBUF:
  285. pr_debug("Write Buffer\n");
  286. if (!buf) {
  287. retval = -EINVAL;
  288. break;
  289. }
  290. if (copy_from_user(&xfer, (void __user *)arg, sizeof(xfer))) {
  291. retval = -EFAULT;
  292. break;
  293. }
  294. if ((buflen <= xfer.size) &&
  295. (copy_from_user(buf, (void __user *)xfer.data,
  296. xfer.size))) {
  297. retval = -EFAULT;
  298. break;
  299. }
  300. break;
  301. case MSM_BUSPM_IOC_CMD:
  302. pr_debug("IOCTL command: cmd: %d arg: %lu\n", cmd, arg);
  303. retval = msm_buspm_ioc_cmds(arg);
  304. break;
  305. default:
  306. pr_debug("Unknown command 0x%x\n", cmd);
  307. retval = -EINVAL;
  308. break;
  309. }
  310. return retval;
  311. }
  312. static int msm_buspm_dev_release(struct inode *inode, struct file *filp)
  313. {
  314. struct msm_buspm_map_dev *dev = filp->private_data;
  315. msm_buspm_dev_free(filp);
  316. kfree(dev);
  317. filp->private_data = NULL;
  318. return 0;
  319. }
  320. static int msm_buspm_dev_mmap(struct file *filp, struct vm_area_struct *vma)
  321. {
  322. pr_debug("vma = 0x%p\n", vma);
  323. /* Mappings are uncached */
  324. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  325. if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  326. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  327. return -EFAULT;
  328. return 0;
  329. }
  330. static const struct file_operations msm_buspm_dev_fops = {
  331. .owner = THIS_MODULE,
  332. .mmap = msm_buspm_dev_mmap,
  333. .open = msm_buspm_dev_open,
  334. .unlocked_ioctl = msm_buspm_dev_ioctl,
  335. .llseek = noop_llseek,
  336. .release = msm_buspm_dev_release,
  337. };
  338. struct miscdevice msm_buspm_misc = {
  339. .minor = MISC_DYNAMIC_MINOR,
  340. .name = MSM_BUSPM_DRV_NAME,
  341. .fops = &msm_buspm_dev_fops,
  342. };
  343. static int __init msm_buspm_dev_init(void)
  344. {
  345. int ret = 0;
  346. ret = misc_register(&msm_buspm_misc);
  347. if (ret < 0)
  348. pr_err("%s: Cannot register misc device\n", __func__);
  349. return ret;
  350. }
  351. static void __exit msm_buspm_dev_exit(void)
  352. {
  353. misc_deregister(&msm_buspm_misc);
  354. }
  355. module_init(msm_buspm_dev_init);
  356. module_exit(msm_buspm_dev_exit);
  357. MODULE_LICENSE("GPL v2");
  358. MODULE_VERSION("1.0");
  359. MODULE_ALIAS("platform:"MSM_BUSPM_DRV_NAME);