hidma_mgmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Qualcomm Technologies HIDMA DMA engine Management interface
  3. *
  4. * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/dmaengine.h>
  16. #include <linux/acpi.h>
  17. #include <linux/of.h>
  18. #include <linux/property.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/module.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/slab.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/bitops.h>
  26. #include <linux/dma-mapping.h>
  27. #include "hidma_mgmt.h"
  28. #define HIDMA_QOS_N_OFFSET 0x300
  29. #define HIDMA_CFG_OFFSET 0x400
  30. #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
  31. #define HIDMA_MAX_XACTIONS_OFFSET 0x420
  32. #define HIDMA_HW_VERSION_OFFSET 0x424
  33. #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
  34. #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
  35. #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
  36. #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
  37. #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
  38. #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
  39. #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
  40. #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
  41. #define HIDMA_WRR_BIT_POS 8
  42. #define HIDMA_PRIORITY_BIT_POS 15
  43. #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
  44. #define HIDMA_MAX_CHANNEL_WEIGHT 15
  45. int hidma_mgmt_setup(struct hidma_mgmt_dev *mgmtdev)
  46. {
  47. unsigned int i;
  48. u32 val;
  49. if (!is_power_of_2(mgmtdev->max_write_request) ||
  50. (mgmtdev->max_write_request < 128) ||
  51. (mgmtdev->max_write_request > 1024)) {
  52. dev_err(&mgmtdev->pdev->dev, "invalid write request %d\n",
  53. mgmtdev->max_write_request);
  54. return -EINVAL;
  55. }
  56. if (!is_power_of_2(mgmtdev->max_read_request) ||
  57. (mgmtdev->max_read_request < 128) ||
  58. (mgmtdev->max_read_request > 1024)) {
  59. dev_err(&mgmtdev->pdev->dev, "invalid read request %d\n",
  60. mgmtdev->max_read_request);
  61. return -EINVAL;
  62. }
  63. if (mgmtdev->max_wr_xactions > HIDMA_MAX_WR_XACTIONS_MASK) {
  64. dev_err(&mgmtdev->pdev->dev,
  65. "max_wr_xactions cannot be bigger than %ld\n",
  66. HIDMA_MAX_WR_XACTIONS_MASK);
  67. return -EINVAL;
  68. }
  69. if (mgmtdev->max_rd_xactions > HIDMA_MAX_RD_XACTIONS_MASK) {
  70. dev_err(&mgmtdev->pdev->dev,
  71. "max_rd_xactions cannot be bigger than %ld\n",
  72. HIDMA_MAX_RD_XACTIONS_MASK);
  73. return -EINVAL;
  74. }
  75. for (i = 0; i < mgmtdev->dma_channels; i++) {
  76. if (mgmtdev->priority[i] > 1) {
  77. dev_err(&mgmtdev->pdev->dev,
  78. "priority can be 0 or 1\n");
  79. return -EINVAL;
  80. }
  81. if (mgmtdev->weight[i] > HIDMA_MAX_CHANNEL_WEIGHT) {
  82. dev_err(&mgmtdev->pdev->dev,
  83. "max value of weight can be %d.\n",
  84. HIDMA_MAX_CHANNEL_WEIGHT);
  85. return -EINVAL;
  86. }
  87. /* weight needs to be at least one */
  88. if (mgmtdev->weight[i] == 0)
  89. mgmtdev->weight[i] = 1;
  90. }
  91. pm_runtime_get_sync(&mgmtdev->pdev->dev);
  92. val = readl(mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  93. val &= ~(HIDMA_MAX_BUS_REQ_LEN_MASK << HIDMA_MAX_BUS_WR_REQ_BIT_POS);
  94. val |= mgmtdev->max_write_request << HIDMA_MAX_BUS_WR_REQ_BIT_POS;
  95. val &= ~HIDMA_MAX_BUS_REQ_LEN_MASK;
  96. val |= mgmtdev->max_read_request;
  97. writel(val, mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  98. val = readl(mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  99. val &= ~(HIDMA_MAX_WR_XACTIONS_MASK << HIDMA_MAX_WR_XACTIONS_BIT_POS);
  100. val |= mgmtdev->max_wr_xactions << HIDMA_MAX_WR_XACTIONS_BIT_POS;
  101. val &= ~HIDMA_MAX_RD_XACTIONS_MASK;
  102. val |= mgmtdev->max_rd_xactions;
  103. writel(val, mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  104. mgmtdev->hw_version =
  105. readl(mgmtdev->virtaddr + HIDMA_HW_VERSION_OFFSET);
  106. mgmtdev->hw_version_major = (mgmtdev->hw_version >> 28) & 0xF;
  107. mgmtdev->hw_version_minor = (mgmtdev->hw_version >> 16) & 0xF;
  108. for (i = 0; i < mgmtdev->dma_channels; i++) {
  109. u32 weight = mgmtdev->weight[i];
  110. u32 priority = mgmtdev->priority[i];
  111. val = readl(mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  112. val &= ~(1 << HIDMA_PRIORITY_BIT_POS);
  113. val |= (priority & 0x1) << HIDMA_PRIORITY_BIT_POS;
  114. val &= ~(HIDMA_WEIGHT_MASK << HIDMA_WRR_BIT_POS);
  115. val |= (weight & HIDMA_WEIGHT_MASK) << HIDMA_WRR_BIT_POS;
  116. writel(val, mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  117. }
  118. val = readl(mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  119. val &= ~HIDMA_CHRESET_TIMEOUT_MASK;
  120. val |= mgmtdev->chreset_timeout_cycles & HIDMA_CHRESET_TIMEOUT_MASK;
  121. writel(val, mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  122. pm_runtime_mark_last_busy(&mgmtdev->pdev->dev);
  123. pm_runtime_put_autosuspend(&mgmtdev->pdev->dev);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL_GPL(hidma_mgmt_setup);
  127. static int hidma_mgmt_probe(struct platform_device *pdev)
  128. {
  129. struct hidma_mgmt_dev *mgmtdev;
  130. struct resource *res;
  131. void __iomem *virtaddr;
  132. int irq;
  133. int rc;
  134. u32 val;
  135. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  136. pm_runtime_use_autosuspend(&pdev->dev);
  137. pm_runtime_set_active(&pdev->dev);
  138. pm_runtime_enable(&pdev->dev);
  139. pm_runtime_get_sync(&pdev->dev);
  140. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  141. virtaddr = devm_ioremap_resource(&pdev->dev, res);
  142. if (IS_ERR(virtaddr)) {
  143. rc = -ENOMEM;
  144. goto out;
  145. }
  146. irq = platform_get_irq(pdev, 0);
  147. if (irq < 0) {
  148. dev_err(&pdev->dev, "irq resources not found\n");
  149. rc = irq;
  150. goto out;
  151. }
  152. mgmtdev = devm_kzalloc(&pdev->dev, sizeof(*mgmtdev), GFP_KERNEL);
  153. if (!mgmtdev) {
  154. rc = -ENOMEM;
  155. goto out;
  156. }
  157. mgmtdev->pdev = pdev;
  158. mgmtdev->addrsize = resource_size(res);
  159. mgmtdev->virtaddr = virtaddr;
  160. rc = device_property_read_u32(&pdev->dev, "dma-channels",
  161. &mgmtdev->dma_channels);
  162. if (rc) {
  163. dev_err(&pdev->dev, "number of channels missing\n");
  164. goto out;
  165. }
  166. rc = device_property_read_u32(&pdev->dev,
  167. "channel-reset-timeout-cycles",
  168. &mgmtdev->chreset_timeout_cycles);
  169. if (rc) {
  170. dev_err(&pdev->dev, "channel reset timeout missing\n");
  171. goto out;
  172. }
  173. rc = device_property_read_u32(&pdev->dev, "max-write-burst-bytes",
  174. &mgmtdev->max_write_request);
  175. if (rc) {
  176. dev_err(&pdev->dev, "max-write-burst-bytes missing\n");
  177. goto out;
  178. }
  179. rc = device_property_read_u32(&pdev->dev, "max-read-burst-bytes",
  180. &mgmtdev->max_read_request);
  181. if (rc) {
  182. dev_err(&pdev->dev, "max-read-burst-bytes missing\n");
  183. goto out;
  184. }
  185. rc = device_property_read_u32(&pdev->dev, "max-write-transactions",
  186. &mgmtdev->max_wr_xactions);
  187. if (rc) {
  188. dev_err(&pdev->dev, "max-write-transactions missing\n");
  189. goto out;
  190. }
  191. rc = device_property_read_u32(&pdev->dev, "max-read-transactions",
  192. &mgmtdev->max_rd_xactions);
  193. if (rc) {
  194. dev_err(&pdev->dev, "max-read-transactions missing\n");
  195. goto out;
  196. }
  197. mgmtdev->priority = devm_kcalloc(&pdev->dev,
  198. mgmtdev->dma_channels,
  199. sizeof(*mgmtdev->priority),
  200. GFP_KERNEL);
  201. if (!mgmtdev->priority) {
  202. rc = -ENOMEM;
  203. goto out;
  204. }
  205. mgmtdev->weight = devm_kcalloc(&pdev->dev,
  206. mgmtdev->dma_channels,
  207. sizeof(*mgmtdev->weight), GFP_KERNEL);
  208. if (!mgmtdev->weight) {
  209. rc = -ENOMEM;
  210. goto out;
  211. }
  212. rc = hidma_mgmt_setup(mgmtdev);
  213. if (rc) {
  214. dev_err(&pdev->dev, "setup failed\n");
  215. goto out;
  216. }
  217. /* start the HW */
  218. val = readl(mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  219. val |= 1;
  220. writel(val, mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  221. rc = hidma_mgmt_init_sys(mgmtdev);
  222. if (rc) {
  223. dev_err(&pdev->dev, "sysfs setup failed\n");
  224. goto out;
  225. }
  226. dev_info(&pdev->dev,
  227. "HW rev: %d.%d @ %pa with %d physical channels\n",
  228. mgmtdev->hw_version_major, mgmtdev->hw_version_minor,
  229. &res->start, mgmtdev->dma_channels);
  230. platform_set_drvdata(pdev, mgmtdev);
  231. pm_runtime_mark_last_busy(&pdev->dev);
  232. pm_runtime_put_autosuspend(&pdev->dev);
  233. return 0;
  234. out:
  235. pm_runtime_put_sync_suspend(&pdev->dev);
  236. pm_runtime_disable(&pdev->dev);
  237. return rc;
  238. }
  239. #if IS_ENABLED(CONFIG_ACPI)
  240. static const struct acpi_device_id hidma_mgmt_acpi_ids[] = {
  241. {"QCOM8060"},
  242. {},
  243. };
  244. #endif
  245. static const struct of_device_id hidma_mgmt_match[] = {
  246. {.compatible = "qcom,hidma-mgmt-1.0",},
  247. {},
  248. };
  249. MODULE_DEVICE_TABLE(of, hidma_mgmt_match);
  250. static struct platform_driver hidma_mgmt_driver = {
  251. .probe = hidma_mgmt_probe,
  252. .driver = {
  253. .name = "hidma-mgmt",
  254. .of_match_table = hidma_mgmt_match,
  255. .acpi_match_table = ACPI_PTR(hidma_mgmt_acpi_ids),
  256. },
  257. };
  258. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  259. static int object_counter;
  260. static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
  261. {
  262. struct platform_device *pdev_parent = of_find_device_by_node(np);
  263. struct platform_device_info pdevinfo;
  264. struct of_phandle_args out_irq;
  265. struct device_node *child;
  266. struct resource *res;
  267. const __be32 *cell;
  268. int ret = 0, size, i, num;
  269. u64 addr, addr_size;
  270. for_each_available_child_of_node(np, child) {
  271. struct resource *res_iter;
  272. struct platform_device *new_pdev;
  273. cell = of_get_property(child, "reg", &size);
  274. if (!cell) {
  275. ret = -EINVAL;
  276. goto out;
  277. }
  278. size /= sizeof(*cell);
  279. num = size /
  280. (of_n_addr_cells(child) + of_n_size_cells(child)) + 1;
  281. /* allocate a resource array */
  282. res = kcalloc(num, sizeof(*res), GFP_KERNEL);
  283. if (!res) {
  284. ret = -ENOMEM;
  285. goto out;
  286. }
  287. /* read each reg value */
  288. i = 0;
  289. res_iter = res;
  290. while (i < size) {
  291. addr = of_read_number(&cell[i],
  292. of_n_addr_cells(child));
  293. i += of_n_addr_cells(child);
  294. addr_size = of_read_number(&cell[i],
  295. of_n_size_cells(child));
  296. i += of_n_size_cells(child);
  297. res_iter->start = addr;
  298. res_iter->end = res_iter->start + addr_size - 1;
  299. res_iter->flags = IORESOURCE_MEM;
  300. res_iter++;
  301. }
  302. ret = of_irq_parse_one(child, 0, &out_irq);
  303. if (ret)
  304. goto out;
  305. res_iter->start = irq_create_of_mapping(&out_irq);
  306. res_iter->name = "hidma event irq";
  307. res_iter->flags = IORESOURCE_IRQ;
  308. memset(&pdevinfo, 0, sizeof(pdevinfo));
  309. pdevinfo.fwnode = &child->fwnode;
  310. pdevinfo.parent = pdev_parent ? &pdev_parent->dev : NULL;
  311. pdevinfo.name = child->name;
  312. pdevinfo.id = object_counter++;
  313. pdevinfo.res = res;
  314. pdevinfo.num_res = num;
  315. pdevinfo.data = NULL;
  316. pdevinfo.size_data = 0;
  317. pdevinfo.dma_mask = DMA_BIT_MASK(64);
  318. new_pdev = platform_device_register_full(&pdevinfo);
  319. if (IS_ERR(new_pdev)) {
  320. ret = PTR_ERR(new_pdev);
  321. goto out;
  322. }
  323. of_dma_configure(&new_pdev->dev, child);
  324. kfree(res);
  325. res = NULL;
  326. }
  327. out:
  328. kfree(res);
  329. return ret;
  330. }
  331. #endif
  332. static int __init hidma_mgmt_init(void)
  333. {
  334. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  335. struct device_node *child;
  336. for_each_matching_node(child, hidma_mgmt_match) {
  337. /* device tree based firmware here */
  338. hidma_mgmt_of_populate_channels(child);
  339. of_node_put(child);
  340. }
  341. #endif
  342. platform_driver_register(&hidma_mgmt_driver);
  343. return 0;
  344. }
  345. module_init(hidma_mgmt_init);
  346. MODULE_LICENSE("GPL v2");