arm-cci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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, %[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, %[cci_control_status_bits] \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. [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
  268. [cci_control_status_bits] "i" cpu_to_le32(1),
  269. #ifndef __ARMEB__
  270. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  271. #else
  272. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  273. #endif
  274. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  275. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  276. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  277. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  278. unreachable();
  279. }
  280. /**
  281. * __cci_control_port_by_device() - function to control a CCI port by device
  282. * reference
  283. *
  284. * @dn: device node pointer of the device whose CCI port should be
  285. * controlled
  286. * @enable: if true enables the port, if false disables it
  287. *
  288. * Return:
  289. * 0 on success
  290. * -ENODEV on port look-up failure
  291. */
  292. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  293. {
  294. int port;
  295. if (!dn)
  296. return -ENODEV;
  297. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  298. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  299. dn->full_name))
  300. return -ENODEV;
  301. cci_port_control(port, enable);
  302. return 0;
  303. }
  304. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  305. /**
  306. * __cci_control_port_by_index() - function to control a CCI port by port index
  307. *
  308. * @port: port index previously retrieved with cci_ace_get_port()
  309. * @enable: if true enables the port, if false disables it
  310. *
  311. * Return:
  312. * 0 on success
  313. * -ENODEV on port index out of range
  314. * -EPERM if operation carried out on an ACE PORT
  315. */
  316. int notrace __cci_control_port_by_index(u32 port, bool enable)
  317. {
  318. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  319. return -ENODEV;
  320. /*
  321. * CCI control for ports connected to CPUS is extremely fragile
  322. * and must be made to go through a specific and controlled
  323. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  324. * indexing is therefore disabled for ACE ports.
  325. */
  326. if (ports[port].type == ACE_PORT)
  327. return -EPERM;
  328. cci_port_control(port, enable);
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  332. static const struct cci_nb_ports cci400_ports = {
  333. .nb_ace = 2,
  334. .nb_ace_lite = 3
  335. };
  336. static const struct of_device_id arm_cci_matches[] = {
  337. {.compatible = "arm,cci-400", .data = &cci400_ports },
  338. {},
  339. };
  340. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  341. {.compatible = "arm,cci-400-ctrl-if", },
  342. {},
  343. };
  344. static int __init cci_probe(void)
  345. {
  346. struct cci_nb_ports const *cci_config;
  347. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  348. struct device_node *np, *cp;
  349. struct resource res;
  350. const char *match_str;
  351. bool is_ace;
  352. np = of_find_matching_node(NULL, arm_cci_matches);
  353. if (!np)
  354. return -ENODEV;
  355. cci_config = of_match_node(arm_cci_matches, np)->data;
  356. if (!cci_config)
  357. return -ENODEV;
  358. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  359. ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
  360. if (!ports)
  361. return -ENOMEM;
  362. ret = of_address_to_resource(np, 0, &res);
  363. if (!ret) {
  364. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  365. cci_ctrl_phys = res.start;
  366. }
  367. if (ret || !cci_ctrl_base) {
  368. WARN(1, "unable to ioremap CCI ctrl\n");
  369. ret = -ENXIO;
  370. goto memalloc_err;
  371. }
  372. for_each_child_of_node(np, cp) {
  373. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  374. continue;
  375. i = nb_ace + nb_ace_lite;
  376. if (i >= nb_cci_ports)
  377. break;
  378. if (of_property_read_string(cp, "interface-type",
  379. &match_str)) {
  380. WARN(1, "node %s missing interface-type property\n",
  381. cp->full_name);
  382. continue;
  383. }
  384. is_ace = strcmp(match_str, "ace") == 0;
  385. if (!is_ace && strcmp(match_str, "ace-lite")) {
  386. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  387. cp->full_name);
  388. continue;
  389. }
  390. ret = of_address_to_resource(cp, 0, &res);
  391. if (!ret) {
  392. ports[i].base = ioremap(res.start, resource_size(&res));
  393. ports[i].phys = res.start;
  394. }
  395. if (ret || !ports[i].base) {
  396. WARN(1, "unable to ioremap CCI port %d\n", i);
  397. continue;
  398. }
  399. if (is_ace) {
  400. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  401. continue;
  402. ports[i].type = ACE_PORT;
  403. ++nb_ace;
  404. } else {
  405. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  406. continue;
  407. ports[i].type = ACE_LITE_PORT;
  408. ++nb_ace_lite;
  409. }
  410. ports[i].dn = cp;
  411. }
  412. /* initialize a stashed array of ACE ports to speed-up look-up */
  413. cci_ace_init_ports();
  414. /*
  415. * Multi-cluster systems may need this data when non-coherent, during
  416. * cluster power-up/power-down. Make sure it reaches main memory.
  417. */
  418. sync_cache_w(&cci_ctrl_base);
  419. sync_cache_w(&cci_ctrl_phys);
  420. sync_cache_w(&ports);
  421. sync_cache_w(&cpu_port);
  422. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  423. pr_info("ARM CCI driver probed\n");
  424. return 0;
  425. memalloc_err:
  426. kfree(ports);
  427. return ret;
  428. }
  429. static int cci_init_status = -EAGAIN;
  430. static DEFINE_MUTEX(cci_probing);
  431. static int __init cci_init(void)
  432. {
  433. if (cci_init_status != -EAGAIN)
  434. return cci_init_status;
  435. mutex_lock(&cci_probing);
  436. if (cci_init_status == -EAGAIN)
  437. cci_init_status = cci_probe();
  438. mutex_unlock(&cci_probing);
  439. return cci_init_status;
  440. }
  441. /*
  442. * To sort out early init calls ordering a helper function is provided to
  443. * check if the CCI driver has beed initialized. Function check if the driver
  444. * has been initialized, if not it calls the init function that probes
  445. * the driver and updates the return value.
  446. */
  447. bool __init cci_probed(void)
  448. {
  449. return cci_init() == 0;
  450. }
  451. EXPORT_SYMBOL_GPL(cci_probed);
  452. early_initcall(cci_init);
  453. MODULE_LICENSE("GPL");
  454. MODULE_DESCRIPTION("ARM CCI support");