arm-cci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * CCI cache coherent interconnect driver
  3. *
  4. * Copyright (C) 2013 ARM Ltd.
  5. * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/arm-cci.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of_address.h>
  20. #include <linux/slab.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/smp_plat.h>
  23. #define CCI_PORT_CTRL 0x0
  24. #define CCI_CTRL_STATUS 0xc
  25. #define CCI_ENABLE_SNOOP_REQ 0x1
  26. #define CCI_ENABLE_DVM_REQ 0x2
  27. #define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
  28. struct cci_nb_ports {
  29. unsigned int nb_ace;
  30. unsigned int nb_ace_lite;
  31. };
  32. enum cci_ace_port_type {
  33. ACE_INVALID_PORT = 0x0,
  34. ACE_PORT,
  35. ACE_LITE_PORT,
  36. };
  37. struct cci_ace_port {
  38. void __iomem *base;
  39. unsigned long phys;
  40. enum cci_ace_port_type type;
  41. struct device_node *dn;
  42. };
  43. static struct cci_ace_port *ports;
  44. static unsigned int nb_cci_ports;
  45. static void __iomem *cci_ctrl_base;
  46. static unsigned long cci_ctrl_phys;
  47. struct cpu_port {
  48. u64 mpidr;
  49. u32 port;
  50. };
  51. /*
  52. * Use the port MSB as valid flag, shift can be made dynamic
  53. * by computing number of bits required for port indexes.
  54. * Code disabling CCI cpu ports runs with D-cache invalidated
  55. * and SCTLR bit clear so data accesses must be kept to a minimum
  56. * to improve performance; for now shift is left static to
  57. * avoid one more data access while disabling the CCI port.
  58. */
  59. #define PORT_VALID_SHIFT 31
  60. #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
  61. static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
  62. {
  63. port->port = PORT_VALID | index;
  64. port->mpidr = mpidr;
  65. }
  66. static inline bool cpu_port_is_valid(struct cpu_port *port)
  67. {
  68. return !!(port->port & PORT_VALID);
  69. }
  70. static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
  71. {
  72. return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
  73. }
  74. static struct cpu_port cpu_port[NR_CPUS];
  75. /**
  76. * __cci_ace_get_port - Function to retrieve the port index connected to
  77. * a cpu or device.
  78. *
  79. * @dn: device node of the device to look-up
  80. * @type: port type
  81. *
  82. * Return value:
  83. * - CCI port index if success
  84. * - -ENODEV if failure
  85. */
  86. static int __cci_ace_get_port(struct device_node *dn, int type)
  87. {
  88. int i;
  89. bool ace_match;
  90. struct device_node *cci_portn;
  91. cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
  92. for (i = 0; i < nb_cci_ports; i++) {
  93. ace_match = ports[i].type == type;
  94. if (ace_match && cci_portn == ports[i].dn)
  95. return i;
  96. }
  97. return -ENODEV;
  98. }
  99. int cci_ace_get_port(struct device_node *dn)
  100. {
  101. return __cci_ace_get_port(dn, ACE_LITE_PORT);
  102. }
  103. EXPORT_SYMBOL_GPL(cci_ace_get_port);
  104. static void __init cci_ace_init_ports(void)
  105. {
  106. int port, cpu;
  107. struct device_node *cpun;
  108. /*
  109. * Port index look-up speeds up the function disabling ports by CPU,
  110. * since the logical to port index mapping is done once and does
  111. * not change after system boot.
  112. * The stashed index array is initialized for all possible CPUs
  113. * at probe time.
  114. */
  115. for_each_possible_cpu(cpu) {
  116. /* too early to use cpu->of_node */
  117. cpun = of_get_cpu_node(cpu, NULL);
  118. if (WARN(!cpun, "Missing cpu device node\n"))
  119. continue;
  120. port = __cci_ace_get_port(cpun, ACE_PORT);
  121. if (port < 0)
  122. continue;
  123. init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
  124. }
  125. for_each_possible_cpu(cpu) {
  126. WARN(!cpu_port_is_valid(&cpu_port[cpu]),
  127. "CPU %u does not have an associated CCI port\n",
  128. cpu);
  129. }
  130. }
  131. /*
  132. * Functions to enable/disable a CCI interconnect slave port
  133. *
  134. * They are called by low-level power management code to disable slave
  135. * interfaces snoops and DVM broadcast.
  136. * Since they may execute with cache data allocation disabled and
  137. * after the caches have been cleaned and invalidated the functions provide
  138. * no explicit locking since they may run with D-cache disabled, so normal
  139. * cacheable kernel locks based on ldrex/strex may not work.
  140. * Locking has to be provided by BSP implementations to ensure proper
  141. * operations.
  142. */
  143. /**
  144. * cci_port_control() - function to control a CCI port
  145. *
  146. * @port: index of the port to setup
  147. * @enable: if true enables the port, if false disables it
  148. */
  149. static void notrace cci_port_control(unsigned int port, bool enable)
  150. {
  151. void __iomem *base = ports[port].base;
  152. writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
  153. /*
  154. * This function is called from power down procedures
  155. * and must not execute any instruction that might
  156. * cause the processor to be put in a quiescent state
  157. * (eg wfi). Hence, cpu_relax() can not be added to this
  158. * read loop to optimize power, since it might hide possibly
  159. * disruptive operations.
  160. */
  161. while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
  162. ;
  163. }
  164. /**
  165. * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
  166. * reference
  167. *
  168. * @mpidr: mpidr of the CPU whose CCI port should be disabled
  169. *
  170. * Disabling a CCI port for a CPU implies disabling the CCI port
  171. * controlling that CPU cluster. Code disabling CPU CCI ports
  172. * must make sure that the CPU running the code is the last active CPU
  173. * in the cluster ie all other CPUs are quiescent in a low power state.
  174. *
  175. * Return:
  176. * 0 on success
  177. * -ENODEV on port look-up failure
  178. */
  179. int notrace cci_disable_port_by_cpu(u64 mpidr)
  180. {
  181. int cpu;
  182. bool is_valid;
  183. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  184. is_valid = cpu_port_is_valid(&cpu_port[cpu]);
  185. if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
  186. cci_port_control(cpu_port[cpu].port, false);
  187. return 0;
  188. }
  189. }
  190. return -ENODEV;
  191. }
  192. EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
  193. /**
  194. * cci_enable_port_for_self() - enable a CCI port for calling CPU
  195. *
  196. * Enabling a CCI port for the calling CPU implies enabling the CCI
  197. * port controlling that CPU's cluster. Caller must make sure that the
  198. * CPU running the code is the first active CPU in the cluster and all
  199. * other CPUs are quiescent in a low power state or waiting for this CPU
  200. * to complete the CCI initialization.
  201. *
  202. * Because this is called when the MMU is still off and with no stack,
  203. * the code must be position independent and ideally rely on callee
  204. * clobbered registers only. To achieve this we must code this function
  205. * entirely in assembler.
  206. *
  207. * On success this returns with the proper CCI port enabled. In case of
  208. * any failure this never returns as the inability to enable the CCI is
  209. * fatal and there is no possible recovery at this stage.
  210. */
  211. asmlinkage void __naked cci_enable_port_for_self(void)
  212. {
  213. asm volatile ("\n"
  214. " .arch armv7-a\n"
  215. " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
  216. " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
  217. " adr r1, 5f \n"
  218. " ldr r2, [r1] \n"
  219. " add r1, r1, r2 @ &cpu_port \n"
  220. " add ip, r1, %[sizeof_cpu_port] \n"
  221. /* Loop over the cpu_port array looking for a matching MPIDR */
  222. "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
  223. " cmp r2, r0 @ compare MPIDR \n"
  224. " bne 2f \n"
  225. /* Found a match, now test port validity */
  226. " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
  227. " tst r3, #"__stringify(PORT_VALID)" \n"
  228. " bne 3f \n"
  229. /* no match, loop with the next cpu_port entry */
  230. "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
  231. " cmp r1, ip @ done? \n"
  232. " blo 1b \n"
  233. /* CCI port not found -- cheaply try to stall this CPU */
  234. "cci_port_not_found: \n"
  235. " wfi \n"
  236. " wfe \n"
  237. " b cci_port_not_found \n"
  238. /* Use matched port index to look up the corresponding ports entry */
  239. "3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
  240. " adr r0, 6f \n"
  241. " ldmia r0, {r1, r2} \n"
  242. " sub r1, r1, r0 @ virt - phys \n"
  243. " ldr r0, [r0, r2] @ *(&ports) \n"
  244. " mov r2, %[sizeof_struct_ace_port] \n"
  245. " mla r0, r2, r3, r0 @ &ports[index] \n"
  246. " sub r0, r0, r1 @ virt_to_phys() \n"
  247. /* Enable the CCI port */
  248. " ldr r0, [r0, %[offsetof_port_phys]] \n"
  249. " mov r3, #"__stringify(CCI_ENABLE_REQ)" \n"
  250. " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
  251. /* poll the status reg for completion */
  252. " adr r1, 7f \n"
  253. " ldr r0, [r1] \n"
  254. " ldr r0, [r0, r1] @ cci_ctrl_base \n"
  255. "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
  256. " tst r1, #1 \n"
  257. " bne 4b \n"
  258. " mov r0, #0 \n"
  259. " bx lr \n"
  260. " .align 2 \n"
  261. "5: .word cpu_port - . \n"
  262. "6: .word . \n"
  263. " .word ports - 6b \n"
  264. "7: .word cci_ctrl_phys - . \n"
  265. : :
  266. [sizeof_cpu_port] "i" (sizeof(cpu_port)),
  267. #ifndef __ARMEB__
  268. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  269. #else
  270. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  271. #endif
  272. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  273. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  274. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  275. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  276. unreachable();
  277. }
  278. /**
  279. * __cci_control_port_by_device() - function to control a CCI port by device
  280. * reference
  281. *
  282. * @dn: device node pointer of the device whose CCI port should be
  283. * controlled
  284. * @enable: if true enables the port, if false disables it
  285. *
  286. * Return:
  287. * 0 on success
  288. * -ENODEV on port look-up failure
  289. */
  290. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  291. {
  292. int port;
  293. if (!dn)
  294. return -ENODEV;
  295. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  296. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  297. dn->full_name))
  298. return -ENODEV;
  299. cci_port_control(port, enable);
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  303. /**
  304. * __cci_control_port_by_index() - function to control a CCI port by port index
  305. *
  306. * @port: port index previously retrieved with cci_ace_get_port()
  307. * @enable: if true enables the port, if false disables it
  308. *
  309. * Return:
  310. * 0 on success
  311. * -ENODEV on port index out of range
  312. * -EPERM if operation carried out on an ACE PORT
  313. */
  314. int notrace __cci_control_port_by_index(u32 port, bool enable)
  315. {
  316. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  317. return -ENODEV;
  318. /*
  319. * CCI control for ports connected to CPUS is extremely fragile
  320. * and must be made to go through a specific and controlled
  321. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  322. * indexing is therefore disabled for ACE ports.
  323. */
  324. if (ports[port].type == ACE_PORT)
  325. return -EPERM;
  326. cci_port_control(port, enable);
  327. return 0;
  328. }
  329. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  330. static const struct cci_nb_ports cci400_ports = {
  331. .nb_ace = 2,
  332. .nb_ace_lite = 3
  333. };
  334. static const struct of_device_id arm_cci_matches[] = {
  335. {.compatible = "arm,cci-400", .data = &cci400_ports },
  336. {},
  337. };
  338. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  339. {.compatible = "arm,cci-400-ctrl-if", },
  340. {},
  341. };
  342. static int __init cci_probe(void)
  343. {
  344. struct cci_nb_ports const *cci_config;
  345. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  346. struct device_node *np, *cp;
  347. struct resource res;
  348. const char *match_str;
  349. bool is_ace;
  350. np = of_find_matching_node(NULL, arm_cci_matches);
  351. if (!np)
  352. return -ENODEV;
  353. cci_config = of_match_node(arm_cci_matches, np)->data;
  354. if (!cci_config)
  355. return -ENODEV;
  356. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  357. ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
  358. if (!ports)
  359. return -ENOMEM;
  360. ret = of_address_to_resource(np, 0, &res);
  361. if (!ret) {
  362. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  363. cci_ctrl_phys = res.start;
  364. }
  365. if (ret || !cci_ctrl_base) {
  366. WARN(1, "unable to ioremap CCI ctrl\n");
  367. ret = -ENXIO;
  368. goto memalloc_err;
  369. }
  370. for_each_child_of_node(np, cp) {
  371. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  372. continue;
  373. i = nb_ace + nb_ace_lite;
  374. if (i >= nb_cci_ports)
  375. break;
  376. if (of_property_read_string(cp, "interface-type",
  377. &match_str)) {
  378. WARN(1, "node %s missing interface-type property\n",
  379. cp->full_name);
  380. continue;
  381. }
  382. is_ace = strcmp(match_str, "ace") == 0;
  383. if (!is_ace && strcmp(match_str, "ace-lite")) {
  384. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  385. cp->full_name);
  386. continue;
  387. }
  388. ret = of_address_to_resource(cp, 0, &res);
  389. if (!ret) {
  390. ports[i].base = ioremap(res.start, resource_size(&res));
  391. ports[i].phys = res.start;
  392. }
  393. if (ret || !ports[i].base) {
  394. WARN(1, "unable to ioremap CCI port %d\n", i);
  395. continue;
  396. }
  397. if (is_ace) {
  398. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  399. continue;
  400. ports[i].type = ACE_PORT;
  401. ++nb_ace;
  402. } else {
  403. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  404. continue;
  405. ports[i].type = ACE_LITE_PORT;
  406. ++nb_ace_lite;
  407. }
  408. ports[i].dn = cp;
  409. }
  410. /* initialize a stashed array of ACE ports to speed-up look-up */
  411. cci_ace_init_ports();
  412. /*
  413. * Multi-cluster systems may need this data when non-coherent, during
  414. * cluster power-up/power-down. Make sure it reaches main memory.
  415. */
  416. sync_cache_w(&cci_ctrl_base);
  417. sync_cache_w(&cci_ctrl_phys);
  418. sync_cache_w(&ports);
  419. sync_cache_w(&cpu_port);
  420. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  421. pr_info("ARM CCI driver probed\n");
  422. return 0;
  423. memalloc_err:
  424. kfree(ports);
  425. return ret;
  426. }
  427. static int cci_init_status = -EAGAIN;
  428. static DEFINE_MUTEX(cci_probing);
  429. static int __init cci_init(void)
  430. {
  431. if (cci_init_status != -EAGAIN)
  432. return cci_init_status;
  433. mutex_lock(&cci_probing);
  434. if (cci_init_status == -EAGAIN)
  435. cci_init_status = cci_probe();
  436. mutex_unlock(&cci_probing);
  437. return cci_init_status;
  438. }
  439. /*
  440. * To sort out early init calls ordering a helper function is provided to
  441. * check if the CCI driver has beed initialized. Function check if the driver
  442. * has been initialized, if not it calls the init function that probes
  443. * the driver and updates the return value.
  444. */
  445. bool __init cci_probed(void)
  446. {
  447. return cci_init() == 0;
  448. }
  449. EXPORT_SYMBOL_GPL(cci_probed);
  450. early_initcall(cci_init);
  451. MODULE_LICENSE("GPL");
  452. MODULE_DESCRIPTION("ARM CCI support");