hidma_mgmt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/module.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/bitops.h>
  27. #include <linux/dma-mapping.h>
  28. #include "hidma_mgmt.h"
  29. #define HIDMA_QOS_N_OFFSET 0x700
  30. #define HIDMA_CFG_OFFSET 0x400
  31. #define HIDMA_MAX_BUS_REQ_LEN_OFFSET 0x41C
  32. #define HIDMA_MAX_XACTIONS_OFFSET 0x420
  33. #define HIDMA_HW_VERSION_OFFSET 0x424
  34. #define HIDMA_CHRESET_TIMEOUT_OFFSET 0x418
  35. #define HIDMA_MAX_WR_XACTIONS_MASK GENMASK(4, 0)
  36. #define HIDMA_MAX_RD_XACTIONS_MASK GENMASK(4, 0)
  37. #define HIDMA_WEIGHT_MASK GENMASK(6, 0)
  38. #define HIDMA_MAX_BUS_REQ_LEN_MASK GENMASK(15, 0)
  39. #define HIDMA_CHRESET_TIMEOUT_MASK GENMASK(19, 0)
  40. #define HIDMA_MAX_WR_XACTIONS_BIT_POS 16
  41. #define HIDMA_MAX_BUS_WR_REQ_BIT_POS 16
  42. #define HIDMA_WRR_BIT_POS 8
  43. #define HIDMA_PRIORITY_BIT_POS 15
  44. #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
  45. #define HIDMA_MAX_CHANNEL_WEIGHT 15
  46. static unsigned int max_write_request;
  47. module_param(max_write_request, uint, 0644);
  48. MODULE_PARM_DESC(max_write_request,
  49. "maximum write burst (default: ACPI/DT value)");
  50. static unsigned int max_read_request;
  51. module_param(max_read_request, uint, 0644);
  52. MODULE_PARM_DESC(max_read_request,
  53. "maximum read burst (default: ACPI/DT value)");
  54. static unsigned int max_wr_xactions;
  55. module_param(max_wr_xactions, uint, 0644);
  56. MODULE_PARM_DESC(max_wr_xactions,
  57. "maximum number of write transactions (default: ACPI/DT value)");
  58. static unsigned int max_rd_xactions;
  59. module_param(max_rd_xactions, uint, 0644);
  60. MODULE_PARM_DESC(max_rd_xactions,
  61. "maximum number of read transactions (default: ACPI/DT value)");
  62. int hidma_mgmt_setup(struct hidma_mgmt_dev *mgmtdev)
  63. {
  64. unsigned int i;
  65. u32 val;
  66. if (!is_power_of_2(mgmtdev->max_write_request) ||
  67. (mgmtdev->max_write_request < 128) ||
  68. (mgmtdev->max_write_request > 1024)) {
  69. dev_err(&mgmtdev->pdev->dev, "invalid write request %d\n",
  70. mgmtdev->max_write_request);
  71. return -EINVAL;
  72. }
  73. if (!is_power_of_2(mgmtdev->max_read_request) ||
  74. (mgmtdev->max_read_request < 128) ||
  75. (mgmtdev->max_read_request > 1024)) {
  76. dev_err(&mgmtdev->pdev->dev, "invalid read request %d\n",
  77. mgmtdev->max_read_request);
  78. return -EINVAL;
  79. }
  80. if (mgmtdev->max_wr_xactions > HIDMA_MAX_WR_XACTIONS_MASK) {
  81. dev_err(&mgmtdev->pdev->dev,
  82. "max_wr_xactions cannot be bigger than %ld\n",
  83. HIDMA_MAX_WR_XACTIONS_MASK);
  84. return -EINVAL;
  85. }
  86. if (mgmtdev->max_rd_xactions > HIDMA_MAX_RD_XACTIONS_MASK) {
  87. dev_err(&mgmtdev->pdev->dev,
  88. "max_rd_xactions cannot be bigger than %ld\n",
  89. HIDMA_MAX_RD_XACTIONS_MASK);
  90. return -EINVAL;
  91. }
  92. for (i = 0; i < mgmtdev->dma_channels; i++) {
  93. if (mgmtdev->priority[i] > 1) {
  94. dev_err(&mgmtdev->pdev->dev,
  95. "priority can be 0 or 1\n");
  96. return -EINVAL;
  97. }
  98. if (mgmtdev->weight[i] > HIDMA_MAX_CHANNEL_WEIGHT) {
  99. dev_err(&mgmtdev->pdev->dev,
  100. "max value of weight can be %d.\n",
  101. HIDMA_MAX_CHANNEL_WEIGHT);
  102. return -EINVAL;
  103. }
  104. /* weight needs to be at least one */
  105. if (mgmtdev->weight[i] == 0)
  106. mgmtdev->weight[i] = 1;
  107. }
  108. pm_runtime_get_sync(&mgmtdev->pdev->dev);
  109. val = readl(mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  110. val &= ~(HIDMA_MAX_BUS_REQ_LEN_MASK << HIDMA_MAX_BUS_WR_REQ_BIT_POS);
  111. val |= mgmtdev->max_write_request << HIDMA_MAX_BUS_WR_REQ_BIT_POS;
  112. val &= ~HIDMA_MAX_BUS_REQ_LEN_MASK;
  113. val |= mgmtdev->max_read_request;
  114. writel(val, mgmtdev->virtaddr + HIDMA_MAX_BUS_REQ_LEN_OFFSET);
  115. val = readl(mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  116. val &= ~(HIDMA_MAX_WR_XACTIONS_MASK << HIDMA_MAX_WR_XACTIONS_BIT_POS);
  117. val |= mgmtdev->max_wr_xactions << HIDMA_MAX_WR_XACTIONS_BIT_POS;
  118. val &= ~HIDMA_MAX_RD_XACTIONS_MASK;
  119. val |= mgmtdev->max_rd_xactions;
  120. writel(val, mgmtdev->virtaddr + HIDMA_MAX_XACTIONS_OFFSET);
  121. mgmtdev->hw_version =
  122. readl(mgmtdev->virtaddr + HIDMA_HW_VERSION_OFFSET);
  123. mgmtdev->hw_version_major = (mgmtdev->hw_version >> 28) & 0xF;
  124. mgmtdev->hw_version_minor = (mgmtdev->hw_version >> 16) & 0xF;
  125. for (i = 0; i < mgmtdev->dma_channels; i++) {
  126. u32 weight = mgmtdev->weight[i];
  127. u32 priority = mgmtdev->priority[i];
  128. val = readl(mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  129. val &= ~(1 << HIDMA_PRIORITY_BIT_POS);
  130. val |= (priority & 0x1) << HIDMA_PRIORITY_BIT_POS;
  131. val &= ~(HIDMA_WEIGHT_MASK << HIDMA_WRR_BIT_POS);
  132. val |= (weight & HIDMA_WEIGHT_MASK) << HIDMA_WRR_BIT_POS;
  133. writel(val, mgmtdev->virtaddr + HIDMA_QOS_N_OFFSET + (4 * i));
  134. }
  135. val = readl(mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  136. val &= ~HIDMA_CHRESET_TIMEOUT_MASK;
  137. val |= mgmtdev->chreset_timeout_cycles & HIDMA_CHRESET_TIMEOUT_MASK;
  138. writel(val, mgmtdev->virtaddr + HIDMA_CHRESET_TIMEOUT_OFFSET);
  139. pm_runtime_mark_last_busy(&mgmtdev->pdev->dev);
  140. pm_runtime_put_autosuspend(&mgmtdev->pdev->dev);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(hidma_mgmt_setup);
  144. static int hidma_mgmt_probe(struct platform_device *pdev)
  145. {
  146. struct hidma_mgmt_dev *mgmtdev;
  147. struct resource *res;
  148. void __iomem *virtaddr;
  149. int irq;
  150. int rc;
  151. u32 val;
  152. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  153. pm_runtime_use_autosuspend(&pdev->dev);
  154. pm_runtime_set_active(&pdev->dev);
  155. pm_runtime_enable(&pdev->dev);
  156. pm_runtime_get_sync(&pdev->dev);
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. virtaddr = devm_ioremap_resource(&pdev->dev, res);
  159. if (IS_ERR(virtaddr)) {
  160. rc = -ENOMEM;
  161. goto out;
  162. }
  163. irq = platform_get_irq(pdev, 0);
  164. if (irq < 0) {
  165. dev_err(&pdev->dev, "irq resources not found\n");
  166. rc = irq;
  167. goto out;
  168. }
  169. mgmtdev = devm_kzalloc(&pdev->dev, sizeof(*mgmtdev), GFP_KERNEL);
  170. if (!mgmtdev) {
  171. rc = -ENOMEM;
  172. goto out;
  173. }
  174. mgmtdev->pdev = pdev;
  175. mgmtdev->addrsize = resource_size(res);
  176. mgmtdev->virtaddr = virtaddr;
  177. rc = device_property_read_u32(&pdev->dev, "dma-channels",
  178. &mgmtdev->dma_channels);
  179. if (rc) {
  180. dev_err(&pdev->dev, "number of channels missing\n");
  181. goto out;
  182. }
  183. rc = device_property_read_u32(&pdev->dev,
  184. "channel-reset-timeout-cycles",
  185. &mgmtdev->chreset_timeout_cycles);
  186. if (rc) {
  187. dev_err(&pdev->dev, "channel reset timeout missing\n");
  188. goto out;
  189. }
  190. rc = device_property_read_u32(&pdev->dev, "max-write-burst-bytes",
  191. &mgmtdev->max_write_request);
  192. if (rc) {
  193. dev_err(&pdev->dev, "max-write-burst-bytes missing\n");
  194. goto out;
  195. }
  196. if (max_write_request &&
  197. (max_write_request != mgmtdev->max_write_request)) {
  198. dev_info(&pdev->dev, "overriding max-write-burst-bytes: %d\n",
  199. max_write_request);
  200. mgmtdev->max_write_request = max_write_request;
  201. } else
  202. max_write_request = mgmtdev->max_write_request;
  203. rc = device_property_read_u32(&pdev->dev, "max-read-burst-bytes",
  204. &mgmtdev->max_read_request);
  205. if (rc) {
  206. dev_err(&pdev->dev, "max-read-burst-bytes missing\n");
  207. goto out;
  208. }
  209. if (max_read_request &&
  210. (max_read_request != mgmtdev->max_read_request)) {
  211. dev_info(&pdev->dev, "overriding max-read-burst-bytes: %d\n",
  212. max_read_request);
  213. mgmtdev->max_read_request = max_read_request;
  214. } else
  215. max_read_request = mgmtdev->max_read_request;
  216. rc = device_property_read_u32(&pdev->dev, "max-write-transactions",
  217. &mgmtdev->max_wr_xactions);
  218. if (rc) {
  219. dev_err(&pdev->dev, "max-write-transactions missing\n");
  220. goto out;
  221. }
  222. if (max_wr_xactions &&
  223. (max_wr_xactions != mgmtdev->max_wr_xactions)) {
  224. dev_info(&pdev->dev, "overriding max-write-transactions: %d\n",
  225. max_wr_xactions);
  226. mgmtdev->max_wr_xactions = max_wr_xactions;
  227. } else
  228. max_wr_xactions = mgmtdev->max_wr_xactions;
  229. rc = device_property_read_u32(&pdev->dev, "max-read-transactions",
  230. &mgmtdev->max_rd_xactions);
  231. if (rc) {
  232. dev_err(&pdev->dev, "max-read-transactions missing\n");
  233. goto out;
  234. }
  235. if (max_rd_xactions &&
  236. (max_rd_xactions != mgmtdev->max_rd_xactions)) {
  237. dev_info(&pdev->dev, "overriding max-read-transactions: %d\n",
  238. max_rd_xactions);
  239. mgmtdev->max_rd_xactions = max_rd_xactions;
  240. } else
  241. max_rd_xactions = mgmtdev->max_rd_xactions;
  242. mgmtdev->priority = devm_kcalloc(&pdev->dev,
  243. mgmtdev->dma_channels,
  244. sizeof(*mgmtdev->priority),
  245. GFP_KERNEL);
  246. if (!mgmtdev->priority) {
  247. rc = -ENOMEM;
  248. goto out;
  249. }
  250. mgmtdev->weight = devm_kcalloc(&pdev->dev,
  251. mgmtdev->dma_channels,
  252. sizeof(*mgmtdev->weight), GFP_KERNEL);
  253. if (!mgmtdev->weight) {
  254. rc = -ENOMEM;
  255. goto out;
  256. }
  257. rc = hidma_mgmt_setup(mgmtdev);
  258. if (rc) {
  259. dev_err(&pdev->dev, "setup failed\n");
  260. goto out;
  261. }
  262. /* start the HW */
  263. val = readl(mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  264. val |= 1;
  265. writel(val, mgmtdev->virtaddr + HIDMA_CFG_OFFSET);
  266. rc = hidma_mgmt_init_sys(mgmtdev);
  267. if (rc) {
  268. dev_err(&pdev->dev, "sysfs setup failed\n");
  269. goto out;
  270. }
  271. dev_info(&pdev->dev,
  272. "HW rev: %d.%d @ %pa with %d physical channels\n",
  273. mgmtdev->hw_version_major, mgmtdev->hw_version_minor,
  274. &res->start, mgmtdev->dma_channels);
  275. platform_set_drvdata(pdev, mgmtdev);
  276. pm_runtime_mark_last_busy(&pdev->dev);
  277. pm_runtime_put_autosuspend(&pdev->dev);
  278. return 0;
  279. out:
  280. pm_runtime_put_sync_suspend(&pdev->dev);
  281. pm_runtime_disable(&pdev->dev);
  282. return rc;
  283. }
  284. #if IS_ENABLED(CONFIG_ACPI)
  285. static const struct acpi_device_id hidma_mgmt_acpi_ids[] = {
  286. {"QCOM8060"},
  287. {},
  288. };
  289. MODULE_DEVICE_TABLE(acpi, hidma_mgmt_acpi_ids);
  290. #endif
  291. static const struct of_device_id hidma_mgmt_match[] = {
  292. {.compatible = "qcom,hidma-mgmt-1.0",},
  293. {},
  294. };
  295. MODULE_DEVICE_TABLE(of, hidma_mgmt_match);
  296. static struct platform_driver hidma_mgmt_driver = {
  297. .probe = hidma_mgmt_probe,
  298. .driver = {
  299. .name = "hidma-mgmt",
  300. .of_match_table = hidma_mgmt_match,
  301. .acpi_match_table = ACPI_PTR(hidma_mgmt_acpi_ids),
  302. },
  303. };
  304. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  305. static int object_counter;
  306. static int __init hidma_mgmt_of_populate_channels(struct device_node *np)
  307. {
  308. struct platform_device *pdev_parent = of_find_device_by_node(np);
  309. struct platform_device_info pdevinfo;
  310. struct device_node *child;
  311. struct resource *res;
  312. int ret = 0;
  313. /* allocate a resource array */
  314. res = kcalloc(3, sizeof(*res), GFP_KERNEL);
  315. if (!res)
  316. return -ENOMEM;
  317. for_each_available_child_of_node(np, child) {
  318. struct platform_device *new_pdev;
  319. ret = of_address_to_resource(child, 0, &res[0]);
  320. if (!ret)
  321. goto out;
  322. ret = of_address_to_resource(child, 1, &res[1]);
  323. if (!ret)
  324. goto out;
  325. ret = of_irq_to_resource(child, 0, &res[2]);
  326. if (ret <= 0)
  327. goto out;
  328. memset(&pdevinfo, 0, sizeof(pdevinfo));
  329. pdevinfo.fwnode = &child->fwnode;
  330. pdevinfo.parent = pdev_parent ? &pdev_parent->dev : NULL;
  331. pdevinfo.name = child->name;
  332. pdevinfo.id = object_counter++;
  333. pdevinfo.res = res;
  334. pdevinfo.num_res = 3;
  335. pdevinfo.data = NULL;
  336. pdevinfo.size_data = 0;
  337. pdevinfo.dma_mask = DMA_BIT_MASK(64);
  338. new_pdev = platform_device_register_full(&pdevinfo);
  339. if (IS_ERR(new_pdev)) {
  340. ret = PTR_ERR(new_pdev);
  341. goto out;
  342. }
  343. of_node_get(child);
  344. new_pdev->dev.of_node = child;
  345. of_dma_configure(&new_pdev->dev, child, true);
  346. /*
  347. * It is assumed that calling of_msi_configure is safe on
  348. * platforms with or without MSI support.
  349. */
  350. of_msi_configure(&new_pdev->dev, child);
  351. of_node_put(child);
  352. }
  353. out:
  354. kfree(res);
  355. return ret;
  356. }
  357. #endif
  358. static int __init hidma_mgmt_init(void)
  359. {
  360. #if defined(CONFIG_OF) && defined(CONFIG_OF_IRQ)
  361. struct device_node *child;
  362. for_each_matching_node(child, hidma_mgmt_match) {
  363. /* device tree based firmware here */
  364. hidma_mgmt_of_populate_channels(child);
  365. }
  366. #endif
  367. platform_driver_register(&hidma_mgmt_driver);
  368. return 0;
  369. }
  370. module_init(hidma_mgmt_init);
  371. MODULE_LICENSE("GPL v2");