qman_portal.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * * Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * * Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. * * Neither the name of Freescale Semiconductor nor the
  11. * names of its contributors may be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * ALTERNATIVELY, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL") as published by the Free Software
  16. * Foundation, either version 2 of that License or (at your option) any
  17. * later version.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "qman_priv.h"
  31. /* Enable portal interupts (as opposed to polling mode) */
  32. #define CONFIG_FSL_DPA_PIRQ_SLOW 1
  33. #define CONFIG_FSL_DPA_PIRQ_FAST 1
  34. static struct cpumask portal_cpus;
  35. /* protect qman global registers and global data shared among portals */
  36. static DEFINE_SPINLOCK(qman_lock);
  37. static void portal_set_cpu(struct qm_portal_config *pcfg, int cpu)
  38. {
  39. #ifdef CONFIG_FSL_PAMU
  40. struct device *dev = pcfg->dev;
  41. int window_count = 1;
  42. struct iommu_domain_geometry geom_attr;
  43. struct pamu_stash_attribute stash_attr;
  44. int ret;
  45. pcfg->iommu_domain = iommu_domain_alloc(&platform_bus_type);
  46. if (!pcfg->iommu_domain) {
  47. dev_err(dev, "%s(): iommu_domain_alloc() failed", __func__);
  48. goto no_iommu;
  49. }
  50. geom_attr.aperture_start = 0;
  51. geom_attr.aperture_end =
  52. ((dma_addr_t)1 << min(8 * sizeof(dma_addr_t), (size_t)36)) - 1;
  53. geom_attr.force_aperture = true;
  54. ret = iommu_domain_set_attr(pcfg->iommu_domain, DOMAIN_ATTR_GEOMETRY,
  55. &geom_attr);
  56. if (ret < 0) {
  57. dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__,
  58. ret);
  59. goto out_domain_free;
  60. }
  61. ret = iommu_domain_set_attr(pcfg->iommu_domain, DOMAIN_ATTR_WINDOWS,
  62. &window_count);
  63. if (ret < 0) {
  64. dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__,
  65. ret);
  66. goto out_domain_free;
  67. }
  68. stash_attr.cpu = cpu;
  69. stash_attr.cache = PAMU_ATTR_CACHE_L1;
  70. ret = iommu_domain_set_attr(pcfg->iommu_domain,
  71. DOMAIN_ATTR_FSL_PAMU_STASH,
  72. &stash_attr);
  73. if (ret < 0) {
  74. dev_err(dev, "%s(): iommu_domain_set_attr() = %d",
  75. __func__, ret);
  76. goto out_domain_free;
  77. }
  78. ret = iommu_domain_window_enable(pcfg->iommu_domain, 0, 0, 1ULL << 36,
  79. IOMMU_READ | IOMMU_WRITE);
  80. if (ret < 0) {
  81. dev_err(dev, "%s(): iommu_domain_window_enable() = %d",
  82. __func__, ret);
  83. goto out_domain_free;
  84. }
  85. ret = iommu_attach_device(pcfg->iommu_domain, dev);
  86. if (ret < 0) {
  87. dev_err(dev, "%s(): iommu_device_attach() = %d", __func__,
  88. ret);
  89. goto out_domain_free;
  90. }
  91. ret = iommu_domain_set_attr(pcfg->iommu_domain,
  92. DOMAIN_ATTR_FSL_PAMU_ENABLE,
  93. &window_count);
  94. if (ret < 0) {
  95. dev_err(dev, "%s(): iommu_domain_set_attr() = %d", __func__,
  96. ret);
  97. goto out_detach_device;
  98. }
  99. no_iommu:
  100. #endif
  101. qman_set_sdest(pcfg->channel, cpu);
  102. return;
  103. #ifdef CONFIG_FSL_PAMU
  104. out_detach_device:
  105. iommu_detach_device(pcfg->iommu_domain, NULL);
  106. out_domain_free:
  107. iommu_domain_free(pcfg->iommu_domain);
  108. pcfg->iommu_domain = NULL;
  109. #endif
  110. }
  111. static struct qman_portal *init_pcfg(struct qm_portal_config *pcfg)
  112. {
  113. struct qman_portal *p;
  114. u32 irq_sources = 0;
  115. /* We need the same LIODN offset for all portals */
  116. qman_liodn_fixup(pcfg->channel);
  117. pcfg->iommu_domain = NULL;
  118. portal_set_cpu(pcfg, pcfg->cpu);
  119. p = qman_create_affine_portal(pcfg, NULL);
  120. if (!p) {
  121. dev_crit(pcfg->dev, "%s: Portal failure on cpu %d\n",
  122. __func__, pcfg->cpu);
  123. return NULL;
  124. }
  125. /* Determine what should be interrupt-vs-poll driven */
  126. #ifdef CONFIG_FSL_DPA_PIRQ_SLOW
  127. irq_sources |= QM_PIRQ_EQCI | QM_PIRQ_EQRI | QM_PIRQ_MRI |
  128. QM_PIRQ_CSCI;
  129. #endif
  130. #ifdef CONFIG_FSL_DPA_PIRQ_FAST
  131. irq_sources |= QM_PIRQ_DQRI;
  132. #endif
  133. qman_p_irqsource_add(p, irq_sources);
  134. spin_lock(&qman_lock);
  135. if (cpumask_equal(&portal_cpus, cpu_possible_mask)) {
  136. /* all assigned portals are initialized now */
  137. qman_init_cgr_all();
  138. }
  139. spin_unlock(&qman_lock);
  140. dev_info(pcfg->dev, "Portal initialised, cpu %d\n", pcfg->cpu);
  141. return p;
  142. }
  143. static void qman_portal_update_sdest(const struct qm_portal_config *pcfg,
  144. unsigned int cpu)
  145. {
  146. #ifdef CONFIG_FSL_PAMU /* TODO */
  147. struct pamu_stash_attribute stash_attr;
  148. int ret;
  149. if (pcfg->iommu_domain) {
  150. stash_attr.cpu = cpu;
  151. stash_attr.cache = PAMU_ATTR_CACHE_L1;
  152. ret = iommu_domain_set_attr(pcfg->iommu_domain,
  153. DOMAIN_ATTR_FSL_PAMU_STASH, &stash_attr);
  154. if (ret < 0) {
  155. dev_err(pcfg->dev,
  156. "Failed to update pamu stash setting\n");
  157. return;
  158. }
  159. }
  160. #endif
  161. qman_set_sdest(pcfg->channel, cpu);
  162. }
  163. static void qman_offline_cpu(unsigned int cpu)
  164. {
  165. struct qman_portal *p;
  166. const struct qm_portal_config *pcfg;
  167. p = affine_portals[cpu];
  168. if (p) {
  169. pcfg = qman_get_qm_portal_config(p);
  170. if (pcfg) {
  171. irq_set_affinity(pcfg->irq, cpumask_of(0));
  172. qman_portal_update_sdest(pcfg, 0);
  173. }
  174. }
  175. }
  176. static void qman_online_cpu(unsigned int cpu)
  177. {
  178. struct qman_portal *p;
  179. const struct qm_portal_config *pcfg;
  180. p = affine_portals[cpu];
  181. if (p) {
  182. pcfg = qman_get_qm_portal_config(p);
  183. if (pcfg) {
  184. irq_set_affinity(pcfg->irq, cpumask_of(cpu));
  185. qman_portal_update_sdest(pcfg, cpu);
  186. }
  187. }
  188. }
  189. static int qman_hotplug_cpu_callback(struct notifier_block *nfb,
  190. unsigned long action, void *hcpu)
  191. {
  192. unsigned int cpu = (unsigned long)hcpu;
  193. switch (action) {
  194. case CPU_ONLINE:
  195. case CPU_ONLINE_FROZEN:
  196. qman_online_cpu(cpu);
  197. break;
  198. case CPU_DOWN_PREPARE:
  199. case CPU_DOWN_PREPARE_FROZEN:
  200. qman_offline_cpu(cpu);
  201. default:
  202. break;
  203. }
  204. return NOTIFY_OK;
  205. }
  206. static struct notifier_block qman_hotplug_cpu_notifier = {
  207. .notifier_call = qman_hotplug_cpu_callback,
  208. };
  209. static int qman_portal_probe(struct platform_device *pdev)
  210. {
  211. struct device *dev = &pdev->dev;
  212. struct device_node *node = dev->of_node;
  213. struct qm_portal_config *pcfg;
  214. struct resource *addr_phys[2];
  215. const u32 *channel;
  216. void __iomem *va;
  217. int irq, len, cpu;
  218. pcfg = devm_kmalloc(dev, sizeof(*pcfg), GFP_KERNEL);
  219. if (!pcfg)
  220. return -ENOMEM;
  221. pcfg->dev = dev;
  222. addr_phys[0] = platform_get_resource(pdev, IORESOURCE_MEM,
  223. DPAA_PORTAL_CE);
  224. if (!addr_phys[0]) {
  225. dev_err(dev, "Can't get %s property 'reg::CE'\n",
  226. node->full_name);
  227. return -ENXIO;
  228. }
  229. addr_phys[1] = platform_get_resource(pdev, IORESOURCE_MEM,
  230. DPAA_PORTAL_CI);
  231. if (!addr_phys[1]) {
  232. dev_err(dev, "Can't get %s property 'reg::CI'\n",
  233. node->full_name);
  234. return -ENXIO;
  235. }
  236. channel = of_get_property(node, "cell-index", &len);
  237. if (!channel || (len != 4)) {
  238. dev_err(dev, "Can't get %s property 'cell-index'\n",
  239. node->full_name);
  240. return -ENXIO;
  241. }
  242. pcfg->channel = *channel;
  243. pcfg->cpu = -1;
  244. irq = platform_get_irq(pdev, 0);
  245. if (irq <= 0) {
  246. dev_err(dev, "Can't get %s IRQ\n", node->full_name);
  247. return -ENXIO;
  248. }
  249. pcfg->irq = irq;
  250. va = ioremap_prot(addr_phys[0]->start, resource_size(addr_phys[0]), 0);
  251. if (!va)
  252. goto err_ioremap1;
  253. pcfg->addr_virt[DPAA_PORTAL_CE] = va;
  254. va = ioremap_prot(addr_phys[1]->start, resource_size(addr_phys[1]),
  255. _PAGE_GUARDED | _PAGE_NO_CACHE);
  256. if (!va)
  257. goto err_ioremap2;
  258. pcfg->addr_virt[DPAA_PORTAL_CI] = va;
  259. pcfg->pools = qm_get_pools_sdqcr();
  260. spin_lock(&qman_lock);
  261. cpu = cpumask_next_zero(-1, &portal_cpus);
  262. if (cpu >= nr_cpu_ids) {
  263. /* unassigned portal, skip init */
  264. spin_unlock(&qman_lock);
  265. return 0;
  266. }
  267. cpumask_set_cpu(cpu, &portal_cpus);
  268. spin_unlock(&qman_lock);
  269. pcfg->cpu = cpu;
  270. if (!init_pcfg(pcfg))
  271. goto err_ioremap2;
  272. /* clear irq affinity if assigned cpu is offline */
  273. if (!cpu_online(cpu))
  274. qman_offline_cpu(cpu);
  275. return 0;
  276. err_ioremap2:
  277. iounmap(pcfg->addr_virt[DPAA_PORTAL_CE]);
  278. err_ioremap1:
  279. dev_err(dev, "ioremap failed\n");
  280. return -ENXIO;
  281. }
  282. static const struct of_device_id qman_portal_ids[] = {
  283. {
  284. .compatible = "fsl,qman-portal",
  285. },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, qman_portal_ids);
  289. static struct platform_driver qman_portal_driver = {
  290. .driver = {
  291. .name = KBUILD_MODNAME,
  292. .of_match_table = qman_portal_ids,
  293. },
  294. .probe = qman_portal_probe,
  295. };
  296. static int __init qman_portal_driver_register(struct platform_driver *drv)
  297. {
  298. int ret;
  299. ret = platform_driver_register(drv);
  300. if (ret < 0)
  301. return ret;
  302. register_hotcpu_notifier(&qman_hotplug_cpu_notifier);
  303. return 0;
  304. }
  305. module_driver(qman_portal_driver,
  306. qman_portal_driver_register, platform_driver_unregister);