arm-cci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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, ac, cpu;
  107. u64 hwid;
  108. const u32 *cell;
  109. struct device_node *cpun, *cpus;
  110. cpus = of_find_node_by_path("/cpus");
  111. if (WARN(!cpus, "Missing cpus node, bailing out\n"))
  112. return;
  113. if (WARN_ON(of_property_read_u32(cpus, "#address-cells", &ac)))
  114. ac = of_n_addr_cells(cpus);
  115. /*
  116. * Port index look-up speeds up the function disabling ports by CPU,
  117. * since the logical to port index mapping is done once and does
  118. * not change after system boot.
  119. * The stashed index array is initialized for all possible CPUs
  120. * at probe time.
  121. */
  122. for_each_child_of_node(cpus, cpun) {
  123. if (of_node_cmp(cpun->type, "cpu"))
  124. continue;
  125. cell = of_get_property(cpun, "reg", NULL);
  126. if (WARN(!cell, "%s: missing reg property\n", cpun->full_name))
  127. continue;
  128. hwid = of_read_number(cell, ac);
  129. cpu = get_logical_index(hwid & MPIDR_HWID_BITMASK);
  130. if (cpu < 0 || !cpu_possible(cpu))
  131. continue;
  132. port = __cci_ace_get_port(cpun, ACE_PORT);
  133. if (port < 0)
  134. continue;
  135. init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
  136. }
  137. for_each_possible_cpu(cpu) {
  138. WARN(!cpu_port_is_valid(&cpu_port[cpu]),
  139. "CPU %u does not have an associated CCI port\n",
  140. cpu);
  141. }
  142. }
  143. /*
  144. * Functions to enable/disable a CCI interconnect slave port
  145. *
  146. * They are called by low-level power management code to disable slave
  147. * interfaces snoops and DVM broadcast.
  148. * Since they may execute with cache data allocation disabled and
  149. * after the caches have been cleaned and invalidated the functions provide
  150. * no explicit locking since they may run with D-cache disabled, so normal
  151. * cacheable kernel locks based on ldrex/strex may not work.
  152. * Locking has to be provided by BSP implementations to ensure proper
  153. * operations.
  154. */
  155. /**
  156. * cci_port_control() - function to control a CCI port
  157. *
  158. * @port: index of the port to setup
  159. * @enable: if true enables the port, if false disables it
  160. */
  161. static void notrace cci_port_control(unsigned int port, bool enable)
  162. {
  163. void __iomem *base = ports[port].base;
  164. writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
  165. /*
  166. * This function is called from power down procedures
  167. * and must not execute any instruction that might
  168. * cause the processor to be put in a quiescent state
  169. * (eg wfi). Hence, cpu_relax() can not be added to this
  170. * read loop to optimize power, since it might hide possibly
  171. * disruptive operations.
  172. */
  173. while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
  174. ;
  175. }
  176. /**
  177. * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
  178. * reference
  179. *
  180. * @mpidr: mpidr of the CPU whose CCI port should be disabled
  181. *
  182. * Disabling a CCI port for a CPU implies disabling the CCI port
  183. * controlling that CPU cluster. Code disabling CPU CCI ports
  184. * must make sure that the CPU running the code is the last active CPU
  185. * in the cluster ie all other CPUs are quiescent in a low power state.
  186. *
  187. * Return:
  188. * 0 on success
  189. * -ENODEV on port look-up failure
  190. */
  191. int notrace cci_disable_port_by_cpu(u64 mpidr)
  192. {
  193. int cpu;
  194. bool is_valid;
  195. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  196. is_valid = cpu_port_is_valid(&cpu_port[cpu]);
  197. if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
  198. cci_port_control(cpu_port[cpu].port, false);
  199. return 0;
  200. }
  201. }
  202. return -ENODEV;
  203. }
  204. EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
  205. /**
  206. * cci_enable_port_for_self() - enable a CCI port for calling CPU
  207. *
  208. * Enabling a CCI port for the calling CPU implies enabling the CCI
  209. * port controlling that CPU's cluster. Caller must make sure that the
  210. * CPU running the code is the first active CPU in the cluster and all
  211. * other CPUs are quiescent in a low power state or waiting for this CPU
  212. * to complete the CCI initialization.
  213. *
  214. * Because this is called when the MMU is still off and with no stack,
  215. * the code must be position independent and ideally rely on callee
  216. * clobbered registers only. To achieve this we must code this function
  217. * entirely in assembler.
  218. *
  219. * On success this returns with the proper CCI port enabled. In case of
  220. * any failure this never returns as the inability to enable the CCI is
  221. * fatal and there is no possible recovery at this stage.
  222. */
  223. asmlinkage void __naked cci_enable_port_for_self(void)
  224. {
  225. asm volatile ("\n"
  226. " .arch armv7-a\n"
  227. " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
  228. " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
  229. " adr r1, 5f \n"
  230. " ldr r2, [r1] \n"
  231. " add r1, r1, r2 @ &cpu_port \n"
  232. " add ip, r1, %[sizeof_cpu_port] \n"
  233. /* Loop over the cpu_port array looking for a matching MPIDR */
  234. "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
  235. " cmp r2, r0 @ compare MPIDR \n"
  236. " bne 2f \n"
  237. /* Found a match, now test port validity */
  238. " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
  239. " tst r3, #"__stringify(PORT_VALID)" \n"
  240. " bne 3f \n"
  241. /* no match, loop with the next cpu_port entry */
  242. "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
  243. " cmp r1, ip @ done? \n"
  244. " blo 1b \n"
  245. /* CCI port not found -- cheaply try to stall this CPU */
  246. "cci_port_not_found: \n"
  247. " wfi \n"
  248. " wfe \n"
  249. " b cci_port_not_found \n"
  250. /* Use matched port index to look up the corresponding ports entry */
  251. "3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
  252. " adr r0, 6f \n"
  253. " ldmia r0, {r1, r2} \n"
  254. " sub r1, r1, r0 @ virt - phys \n"
  255. " ldr r0, [r0, r2] @ *(&ports) \n"
  256. " mov r2, %[sizeof_struct_ace_port] \n"
  257. " mla r0, r2, r3, r0 @ &ports[index] \n"
  258. " sub r0, r0, r1 @ virt_to_phys() \n"
  259. /* Enable the CCI port */
  260. " ldr r0, [r0, %[offsetof_port_phys]] \n"
  261. " mov r3, #"__stringify(CCI_ENABLE_REQ)" \n"
  262. " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
  263. /* poll the status reg for completion */
  264. " adr r1, 7f \n"
  265. " ldr r0, [r1] \n"
  266. " ldr r0, [r0, r1] @ cci_ctrl_base \n"
  267. "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
  268. " tst r1, #1 \n"
  269. " bne 4b \n"
  270. " mov r0, #0 \n"
  271. " bx lr \n"
  272. " .align 2 \n"
  273. "5: .word cpu_port - . \n"
  274. "6: .word . \n"
  275. " .word ports - 6b \n"
  276. "7: .word cci_ctrl_phys - . \n"
  277. : :
  278. [sizeof_cpu_port] "i" (sizeof(cpu_port)),
  279. #ifndef __ARMEB__
  280. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  281. #else
  282. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  283. #endif
  284. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  285. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  286. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  287. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  288. unreachable();
  289. }
  290. /**
  291. * __cci_control_port_by_device() - function to control a CCI port by device
  292. * reference
  293. *
  294. * @dn: device node pointer of the device whose CCI port should be
  295. * controlled
  296. * @enable: if true enables the port, if false disables it
  297. *
  298. * Return:
  299. * 0 on success
  300. * -ENODEV on port look-up failure
  301. */
  302. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  303. {
  304. int port;
  305. if (!dn)
  306. return -ENODEV;
  307. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  308. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  309. dn->full_name))
  310. return -ENODEV;
  311. cci_port_control(port, enable);
  312. return 0;
  313. }
  314. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  315. /**
  316. * __cci_control_port_by_index() - function to control a CCI port by port index
  317. *
  318. * @port: port index previously retrieved with cci_ace_get_port()
  319. * @enable: if true enables the port, if false disables it
  320. *
  321. * Return:
  322. * 0 on success
  323. * -ENODEV on port index out of range
  324. * -EPERM if operation carried out on an ACE PORT
  325. */
  326. int notrace __cci_control_port_by_index(u32 port, bool enable)
  327. {
  328. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  329. return -ENODEV;
  330. /*
  331. * CCI control for ports connected to CPUS is extremely fragile
  332. * and must be made to go through a specific and controlled
  333. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  334. * indexing is therefore disabled for ACE ports.
  335. */
  336. if (ports[port].type == ACE_PORT)
  337. return -EPERM;
  338. cci_port_control(port, enable);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  342. static const struct cci_nb_ports cci400_ports = {
  343. .nb_ace = 2,
  344. .nb_ace_lite = 3
  345. };
  346. static const struct of_device_id arm_cci_matches[] = {
  347. {.compatible = "arm,cci-400", .data = &cci400_ports },
  348. {},
  349. };
  350. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  351. {.compatible = "arm,cci-400-ctrl-if", },
  352. {},
  353. };
  354. static int __init cci_probe(void)
  355. {
  356. struct cci_nb_ports const *cci_config;
  357. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  358. struct device_node *np, *cp;
  359. struct resource res;
  360. const char *match_str;
  361. bool is_ace;
  362. np = of_find_matching_node(NULL, arm_cci_matches);
  363. if (!np)
  364. return -ENODEV;
  365. cci_config = of_match_node(arm_cci_matches, np)->data;
  366. if (!cci_config)
  367. return -ENODEV;
  368. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  369. ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
  370. if (!ports)
  371. return -ENOMEM;
  372. ret = of_address_to_resource(np, 0, &res);
  373. if (!ret) {
  374. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  375. cci_ctrl_phys = res.start;
  376. }
  377. if (ret || !cci_ctrl_base) {
  378. WARN(1, "unable to ioremap CCI ctrl\n");
  379. ret = -ENXIO;
  380. goto memalloc_err;
  381. }
  382. for_each_child_of_node(np, cp) {
  383. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  384. continue;
  385. i = nb_ace + nb_ace_lite;
  386. if (i >= nb_cci_ports)
  387. break;
  388. if (of_property_read_string(cp, "interface-type",
  389. &match_str)) {
  390. WARN(1, "node %s missing interface-type property\n",
  391. cp->full_name);
  392. continue;
  393. }
  394. is_ace = strcmp(match_str, "ace") == 0;
  395. if (!is_ace && strcmp(match_str, "ace-lite")) {
  396. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  397. cp->full_name);
  398. continue;
  399. }
  400. ret = of_address_to_resource(cp, 0, &res);
  401. if (!ret) {
  402. ports[i].base = ioremap(res.start, resource_size(&res));
  403. ports[i].phys = res.start;
  404. }
  405. if (ret || !ports[i].base) {
  406. WARN(1, "unable to ioremap CCI port %d\n", i);
  407. continue;
  408. }
  409. if (is_ace) {
  410. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  411. continue;
  412. ports[i].type = ACE_PORT;
  413. ++nb_ace;
  414. } else {
  415. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  416. continue;
  417. ports[i].type = ACE_LITE_PORT;
  418. ++nb_ace_lite;
  419. }
  420. ports[i].dn = cp;
  421. }
  422. /* initialize a stashed array of ACE ports to speed-up look-up */
  423. cci_ace_init_ports();
  424. /*
  425. * Multi-cluster systems may need this data when non-coherent, during
  426. * cluster power-up/power-down. Make sure it reaches main memory.
  427. */
  428. sync_cache_w(&cci_ctrl_base);
  429. sync_cache_w(&cci_ctrl_phys);
  430. sync_cache_w(&ports);
  431. sync_cache_w(&cpu_port);
  432. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  433. pr_info("ARM CCI driver probed\n");
  434. return 0;
  435. memalloc_err:
  436. kfree(ports);
  437. return ret;
  438. }
  439. static int cci_init_status = -EAGAIN;
  440. static DEFINE_MUTEX(cci_probing);
  441. static int __init cci_init(void)
  442. {
  443. if (cci_init_status != -EAGAIN)
  444. return cci_init_status;
  445. mutex_lock(&cci_probing);
  446. if (cci_init_status == -EAGAIN)
  447. cci_init_status = cci_probe();
  448. mutex_unlock(&cci_probing);
  449. return cci_init_status;
  450. }
  451. /*
  452. * To sort out early init calls ordering a helper function is provided to
  453. * check if the CCI driver has beed initialized. Function check if the driver
  454. * has been initialized, if not it calls the init function that probes
  455. * the driver and updates the return value.
  456. */
  457. bool __init cci_probed(void)
  458. {
  459. return cci_init() == 0;
  460. }
  461. EXPORT_SYMBOL_GPL(cci_probed);
  462. early_initcall(cci_init);
  463. MODULE_LICENSE("GPL");
  464. MODULE_DESCRIPTION("ARM CCI support");