hidma_mgmt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Qualcomm Technologies HIDMA DMA engine Management interface
  3. *
  4. * Copyright (c) 2015-2017, 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 0x700
  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. static unsigned int max_write_request;
  46. module_param(max_write_request, uint, 0644);
  47. MODULE_PARM_DESC(max_write_request,
  48. "maximum write burst (default: ACPI/DT value)");
  49. static unsigned int max_read_request;
  50. module_param(max_read_request, uint, 0644);
  51. MODULE_PARM_DESC(max_read_request,
  52. "maximum read burst (default: ACPI/DT value)");
  53. static unsigned int max_wr_xactions;
  54. module_param(max_wr_xactions, uint, 0644);
  55. MODULE_PARM_DESC(max_wr_xactions,
  56. "maximum number of write transactions (default: ACPI/DT value)");
  57. static unsigned int max_rd_xactions;
  58. module_param(max_rd_xactions, uint, 0644);
  59. MODULE_PARM_DESC(max_rd_xactions,
  60. "maximum number of read transactions (default: ACPI/DT value)");
  61. int hidma_mgmt_setup(struct hidma_mgmt_dev *mgmtdev)
  62. {
  63. unsigned int i;
  64. u32 val;
  65. if (!is_power_of_2(mgmtdev->max_write_request) ||
  66. (mgmtdev->max_write_request < 128) ||
  67. (mgmtdev->max_write_request > 1024)) {
  68. dev_err(&mgmtdev->pdev->dev, "invalid write request %d\n",
  69. mgmtdev->max_write_request);
  70. return -EINVAL;
  71. }
  72. if (!is_power_of_2(mgmtdev->max_read_request) ||
  73. (mgmtdev->max_read_request < 128) ||
  74. (mgmtdev->max_read_request > 1024)) {
  75. dev_err(&mgmtdev->pdev->dev, "invalid read request %d\n",
  76. mgmtdev->max_read_request);
  77. return -EINVAL;
  78. }
  79. if (mgmtdev->max_wr_xactions > HIDMA_MAX_WR_XACTIONS_MASK) {
  80. dev_err(&mgmtdev->pdev->dev,
  81. "max_wr_xactions cannot be bigger than %ld\n",
  82. HIDMA_MAX_WR_XACTIONS_MASK);
  83. return -EINVAL;
  84. }
  85. if (mgmtdev->max_rd_xactions > HIDMA_MAX_RD_XACTIONS_MASK) {
  86. dev_err(&mgmtdev->pdev->dev,
  87. "max_rd_xactions cannot be bigger than %ld\n",
  88. HIDMA_MAX_RD_XACTIONS_MASK);
  89. return -EINVAL;
  90. }
  91. for (i = 0; i < mgmtdev->dma_channels; i++) {
  92. if (mgmtdev->priority[i] > 1) {
  93. dev_err(&mgmtdev->pdev->dev,
  94. "priority can be 0 or 1\n");
  95. return -EINVAL;
  96. }
  97. if (mgmtdev->weight[i] > HIDMA_MAX_CHANNEL_WEIGHT) {
  98. dev_err(&mgmtdev->pdev->dev,
  99. "max value of weight can be %d.\n",
  100. HIDMA_MAX_CHANNEL_WEIGHT);
  101. return -EINVAL;
  102. }
  103. /* weight needs to be at least one */
  104. if (mgmtdev->weight[i] == 0)
  105. mgmtdev->weight[i] = 1;
  106. }
  107. pm_runtime_get_sync(&mgmtdev->pdev->dev);
  108. val = readl(mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  109. val &= ~(HIDMA_MAX_BUS_REQ_LEN_MASK << HIDMA_MAX_BUS_WR_REQ_BIT_POS);
  110. val |= mgmtdev->max_write_request << HIDMA_MAX_BUS_WR_REQ_BIT_POS;
  111. val &= ~HIDMA_MAX_BUS_REQ_LEN_MASK;
  112. val |= mgmtdev->max_read_request;
  113. writel(val, mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  114. val = readl(mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  115. val &= ~(HIDMA_MAX_WR_XACTIONS_MASK << HIDMA_MAX_WR_XACTIONS_BIT_POS);
  116. val |= mgmtdev->max_wr_xactions << HIDMA_MAX_WR_XACTIONS_BIT_POS;
  117. val &= ~HIDMA_MAX_RD_XACTIONS_MASK;
  118. val |= mgmtdev->max_rd_xactions;
  119. writel(val, mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  120. mgmtdev->hw_version =
  121. readl(mgmtdev->virtaddr + HIDMA_HW_VERSION_OFFSET);
  122. mgmtdev->hw_version_major = (mgmtdev->hw_version >> 28) & 0xF;
  123. mgmtdev->hw_version_minor = (mgmtdev->hw_version >> 16) & 0xF;
  124. for (i = 0; i < mgmtdev->dma_channels; i++) {
  125. u32 weight = mgmtdev->weight[i];
  126. u32 priority = mgmtdev->priority[i];
  127. val = readl(mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  128. val &= ~(1 << HIDMA_PRIORITY_BIT_POS);
  129. val |= (priority & 0x1) << HIDMA_PRIORITY_BIT_POS;
  130. val &= ~(HIDMA_WEIGHT_MASK << HIDMA_WRR_BIT_POS);
  131. val |= (weight & HIDMA_WEIGHT_MASK) << HIDMA_WRR_BIT_POS;
  132. writel(val, mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  133. }
  134. val = readl(mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  135. val &= ~HIDMA_CHRESET_TIMEOUT_MASK;
  136. val |= mgmtdev->chreset_timeout_cycles & HIDMA_CHRESET_TIMEOUT_MASK;
  137. writel(val, mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  138. pm_runtime_mark_last_busy(&mgmtdev->pdev->dev);
  139. pm_runtime_put_autosuspend(&mgmtdev->pdev->dev);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(hidma_mgmt_setup);
  143. static int hidma_mgmt_probe(struct platform_device *pdev)
  144. {
  145. struct hidma_mgmt_dev *mgmtdev;
  146. struct resource *res;
  147. void __iomem *virtaddr;
  148. int irq;
  149. int rc;
  150. u32 val;
  151. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  152. pm_runtime_use_autosuspend(&pdev->dev);
  153. pm_runtime_set_active(&pdev->dev);
  154. pm_runtime_enable(&pdev->dev);
  155. pm_runtime_get_sync(&pdev->dev);
  156. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157. virtaddr = devm_ioremap_resource(&pdev->dev, res);
  158. if (IS_ERR(virtaddr)) {
  159. rc = -ENOMEM;
  160. goto out;
  161. }
  162. irq = platform_get_irq(pdev, 0);
  163. if (irq < 0) {
  164. dev_err(&pdev->dev, "irq resources not found\n");
  165. rc = irq;
  166. goto out;
  167. }
  168. mgmtdev = devm_kzalloc(&pdev->dev, sizeof(*mgmtdev), GFP_KERNEL);
  169. if (!mgmtdev) {
  170. rc = -ENOMEM;
  171. goto out;
  172. }
  173. mgmtdev->pdev = pdev;
  174. mgmtdev->addrsize = resource_size(res);
  175. mgmtdev->virtaddr = virtaddr;
  176. rc = device_property_read_u32(&pdev->dev, "dma-channels",
  177. &mgmtdev->dma_channels);
  178. if (rc) {
  179. dev_err(&pdev->dev, "number of channels missing\n");
  180. goto out;
  181. }
  182. rc = device_property_read_u32(&pdev->dev,
  183. "channel-reset-timeout-cycles",
  184. &mgmtdev->chreset_timeout_cycles);
  185. if (rc) {
  186. dev_err(&pdev->dev, "channel reset timeout missing\n");
  187. goto out;
  188. }
  189. rc = device_property_read_u32(&pdev->dev, "max-write-burst-bytes",
  190. &mgmtdev->max_write_request);
  191. if (rc) {
  192. dev_err(&pdev->dev, "max-write-burst-bytes missing\n");
  193. goto out;
  194. }
  195. if (max_write_request &&
  196. (max_write_request != mgmtdev->max_write_request)) {
  197. dev_info(&pdev->dev, "overriding max-write-burst-bytes: %d\n",
  198. max_write_request);
  199. mgmtdev->max_write_request = max_write_request;
  200. } else
  201. max_write_request = mgmtdev->max_write_request;
  202. rc = device_property_read_u32(&pdev->dev, "max-read-burst-bytes",
  203. &mgmtdev->max_read_request);
  204. if (rc) {
  205. dev_err(&pdev->dev, "max-read-burst-bytes missing\n");
  206. goto out;
  207. }
  208. if (max_read_request &&
  209. (max_read_request != mgmtdev->max_read_request)) {
  210. dev_info(&pdev->dev, "overriding max-read-burst-bytes: %d\n",
  211. max_read_request);
  212. mgmtdev->max_read_request = max_read_request;
  213. } else
  214. max_read_request = mgmtdev->max_read_request;
  215. rc = device_property_read_u32(&pdev->dev, "max-write-transactions",
  216. &mgmtdev->max_wr_xactions);
  217. if (rc) {
  218. dev_err(&pdev->dev, "max-write-transactions missing\n");
  219. goto out;
  220. }
  221. if (max_wr_xactions &&
  222. (max_wr_xactions != mgmtdev->max_wr_xactions)) {
  223. dev_info(&pdev->dev, "overriding max-write-transactions: %d\n",
  224. max_wr_xactions);
  225. mgmtdev->max_wr_xactions = max_wr_xactions;
  226. } else
  227. max_wr_xactions = mgmtdev->max_wr_xactions;
  228. rc = device_property_read_u32(&pdev->dev, "max-read-transactions",
  229. &mgmtdev->max_rd_xactions);
  230. if (rc) {
  231. dev_err(&pdev->dev, "max-read-transactions missing\n");
  232. goto out;
  233. }
  234. if (max_rd_xactions &&
  235. (max_rd_xactions != mgmtdev->max_rd_xactions)) {
  236. dev_info(&pdev->dev, "overriding max-read-transactions: %d\n",
  237. max_rd_xactions);
  238. mgmtdev->max_rd_xactions = max_rd_xactions;
  239. } else
  240. max_rd_xactions = mgmtdev->max_rd_xactions;
  241. mgmtdev->priority = devm_kcalloc(&pdev->dev,
  242. mgmtdev->dma_channels,
  243. sizeof(*mgmtdev->priority),
  244. GFP_KERNEL);
  245. if (!mgmtdev->priority) {
  246. rc = -ENOMEM;
  247. goto out;
  248. }
  249. mgmtdev->weight = devm_kcalloc(&pdev->dev,
  250. mgmtdev->dma_channels,
  251. sizeof(*mgmtdev->weight), GFP_KERNEL);
  252. if (!mgmtdev->weight) {
  253. rc = -ENOMEM;
  254. goto out;
  255. }
  256. rc = hidma_mgmt_setup(mgmtdev);
  257. if (rc) {
  258. dev_err(&pdev->dev, "setup failed\n");
  259. goto out;
  260. }
  261. /* start the HW */
  262. val = readl(mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  263. val |= 1;
  264. writel(val, mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  265. rc = hidma_mgmt_init_sys(mgmtdev);
  266. if (rc) {
  267. dev_err(&pdev->dev, "sysfs setup failed\n");
  268. goto out;
  269. }
  270. dev_info(&pdev->dev,
  271. "HW rev: %d.%d @ %pa with %d physical channels\n",
  272. mgmtdev->hw_version_major, mgmtdev->hw_version_minor,
  273. &res->start, mgmtdev->dma_channels);
  274. platform_set_drvdata(pdev, mgmtdev);
  275. pm_runtime_mark_last_busy(&pdev->dev);
  276. pm_runtime_put_autosuspend(&pdev->dev);
  277. return 0;
  278. out:
  279. pm_runtime_put_sync_suspend(&pdev->dev);
  280. pm_runtime_disable(&pdev->dev);
  281. return rc;
  282. }
  283. #if IS_ENABLED(CONFIG_ACPI)
  284. static const struct acpi_device_id hidma_mgmt_acpi_ids[] = {
  285. {"QCOM8060"},
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(acpi, hidma_mgmt_acpi_ids);
  289. #endif
  290. static const struct of_device_id hidma_mgmt_match[] = {
  291. {.compatible = "qcom,hidma-mgmt-1.0",},
  292. {},
  293. };
  294. MODULE_DEVICE_TABLE(of, hidma_mgmt_match);
  295. static struct platform_driver hidma_mgmt_driver = {
  296. .probe = hidma_mgmt_probe,
  297. .driver = {
  298. .name = "hidma-mgmt",
  299. .of_match_table = hidma_mgmt_match,
  300. .acpi_match_table = ACPI_PTR(hidma_mgmt_acpi_ids),
  301. },
  302. };
  303. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  304. static int object_counter;
  305. static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
  306. {
  307. struct platform_device *pdev_parent = of_find_device_by_node(np);
  308. struct platform_device_info pdevinfo;
  309. struct of_phandle_args out_irq;
  310. struct device_node *child;
  311. struct resource *res = NULL;
  312. const __be32 *cell;
  313. int ret = 0, size, i, num;
  314. u64 addr, addr_size;
  315. for_each_available_child_of_node(np, child) {
  316. struct resource *res_iter;
  317. struct platform_device *new_pdev;
  318. cell = of_get_property(child, "reg", &size);
  319. if (!cell) {
  320. ret = -EINVAL;
  321. goto out;
  322. }
  323. size /= sizeof(*cell);
  324. num = size /
  325. (of_n_addr_cells(child) + of_n_size_cells(child)) + 1;
  326. /* allocate a resource array */
  327. res = kcalloc(num, sizeof(*res), GFP_KERNEL);
  328. if (!res) {
  329. ret = -ENOMEM;
  330. goto out;
  331. }
  332. /* read each reg value */
  333. i = 0;
  334. res_iter = res;
  335. while (i < size) {
  336. addr = of_read_number(&cell[i],
  337. of_n_addr_cells(child));
  338. i += of_n_addr_cells(child);
  339. addr_size = of_read_number(&cell[i],
  340. of_n_size_cells(child));
  341. i += of_n_size_cells(child);
  342. res_iter->start = addr;
  343. res_iter->end = res_iter->start + addr_size - 1;
  344. res_iter->flags = IORESOURCE_MEM;
  345. res_iter++;
  346. }
  347. ret = of_irq_parse_one(child, 0, &out_irq);
  348. if (ret)
  349. goto out;
  350. res_iter->start = irq_create_of_mapping(&out_irq);
  351. res_iter->name = "hidma event irq";
  352. res_iter->flags = IORESOURCE_IRQ;
  353. memset(&pdevinfo, 0, sizeof(pdevinfo));
  354. pdevinfo.fwnode = &child->fwnode;
  355. pdevinfo.parent = pdev_parent ? &pdev_parent->dev : NULL;
  356. pdevinfo.name = child->name;
  357. pdevinfo.id = object_counter++;
  358. pdevinfo.res = res;
  359. pdevinfo.num_res = num;
  360. pdevinfo.data = NULL;
  361. pdevinfo.size_data = 0;
  362. pdevinfo.dma_mask = DMA_BIT_MASK(64);
  363. new_pdev = platform_device_register_full(&pdevinfo);
  364. if (IS_ERR(new_pdev)) {
  365. ret = PTR_ERR(new_pdev);
  366. goto out;
  367. }
  368. of_node_get(child);
  369. new_pdev->dev.of_node = child;
  370. of_dma_configure(&new_pdev->dev, child);
  371. /*
  372. * It is assumed that calling of_msi_configure is safe on
  373. * platforms with or without MSI support.
  374. */
  375. of_msi_configure(&new_pdev->dev, child);
  376. of_node_put(child);
  377. kfree(res);
  378. res = NULL;
  379. }
  380. out:
  381. kfree(res);
  382. return ret;
  383. }
  384. #endif
  385. static int __init hidma_mgmt_init(void)
  386. {
  387. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  388. struct device_node *child;
  389. for_each_matching_node(child, hidma_mgmt_match) {
  390. /* device tree based firmware here */
  391. hidma_mgmt_of_populate_channels(child);
  392. }
  393. #endif
  394. /*
  395. * We do not check for return value here, as it is assumed that
  396. * platform_driver_register must not fail. The reason for this is that
  397. * the (potential) hidma_mgmt_of_populate_channels calls above are not
  398. * cleaned up if it does fail, and to do this work is quite
  399. * complicated. In particular, various calls of of_address_to_resource,
  400. * of_irq_to_resource, platform_device_register_full, of_dma_configure,
  401. * and of_msi_configure which then call other functions and so on, must
  402. * be cleaned up - this is not a trivial exercise.
  403. *
  404. * Currently, this module is not intended to be unloaded, and there is
  405. * no module_exit function defined which does the needed cleanup. For
  406. * this reason, we have to assume success here.
  407. */
  408. platform_driver_register(&hidma_mgmt_driver);
  409. return 0;
  410. }
  411. module_init(hidma_mgmt_init);
  412. MODULE_LICENSE("GPL v2");