1 /* 2 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <stdbool.h> 9 10 #include <arch.h> 11 #include <common/debug.h> 12 #include <drivers/arm/ccn.h> 13 #include <lib/bakery_lock.h> 14 #include <lib/mmio.h> 15 16 #include "ccn_private.h" 17 18 static const ccn_desc_t *ccn_plat_desc; 19 #if defined(IMAGE_BL31) || (!defined(__aarch64__) && defined(IMAGE_BL32)) 20 DEFINE_BAKERY_LOCK(ccn_lock); 21 #endif 22 23 /******************************************************************************* 24 * This function takes the base address of the CCN's programmer's view (PV), a 25 * region ID of one of the 256 regions (0-255) and a register offset within the 26 * region. It converts the first two parameters into a base address and uses it 27 * to read the register at the offset. 28 ******************************************************************************/ 29 static inline unsigned long long ccn_reg_read(uintptr_t periphbase, 30 unsigned int region_id, 31 unsigned int register_offset) 32 { 33 uintptr_t region_base; 34 35 assert(periphbase); 36 assert(region_id < REGION_ID_LIMIT); 37 38 region_base = periphbase + region_id_to_base(region_id); 39 return mmio_read_64(region_base + register_offset); 40 } 41 42 /******************************************************************************* 43 * This function takes the base address of the CCN's programmer's view (PV), a 44 * region ID of one of the 256 regions (0-255), a register offset within the 45 * region and a value. It converts the first two parameters into a base address 46 * and uses it to write the value in the register at the offset. 47 ******************************************************************************/ 48 static inline void ccn_reg_write(uintptr_t periphbase, 49 unsigned int region_id, 50 unsigned int register_offset, 51 unsigned long long value) 52 { 53 uintptr_t region_base; 54 55 assert(periphbase); 56 assert(region_id < REGION_ID_LIMIT); 57 58 region_base = periphbase + region_id_to_base(region_id); 59 mmio_write_64(region_base + register_offset, value); 60 } 61 62 #if ENABLE_ASSERTIONS 63 64 typedef struct rn_info { 65 unsigned char node_desc[MAX_RN_NODES]; 66 } rn_info_t; 67 68 /******************************************************************************* 69 * This function takes the base address of the CCN's programmer's view (PV) and 70 * the node ID of a Request Node (RN-D or RN-I). It returns the maximum number 71 * of master interfaces resident on that node. This number is equal to the least 72 * significant two bits of the node type ID + 1. 73 ******************************************************************************/ 74 static unsigned int ccn_get_rni_mcount(uintptr_t periphbase, 75 unsigned int rn_id) 76 { 77 unsigned int rn_type_id; 78 79 /* Use the node id to find the type of RN-I/D node */ 80 rn_type_id = get_node_type(ccn_reg_read(periphbase, 81 rn_id + RNI_REGION_ID_START, 82 REGION_ID_OFFSET)); 83 84 /* Return the number master interfaces based on node type */ 85 return rn_type_id_to_master_cnt(rn_type_id); 86 } 87 88 /******************************************************************************* 89 * This function reads the CCN registers to find the following information about 90 * the ACE/ACELite/ACELite+DVM/CHI interfaces resident on the various types of 91 * Request Nodes (RN-Fs, RN-Is and RN-Ds) in the system: 92 * 93 * 1. The total number of such interfaces that this CCN IP supports. This is the 94 * cumulative number of interfaces across all Request node types. It is 95 * passed back as the return value of this function. 96 * 97 * 2. The maximum number of interfaces of a type resident on a Request node of 98 * one of the three types. This information is populated in the 'info' 99 * array provided by the caller as described next. 100 * 101 * The array has 64 entries. Each entry corresponds to a Request node. The 102 * Miscellaneous node's programmer's view has RN-F, RN-I and RN-D ID 103 * registers. For each RN-I and RN-D ID indicated as being present in these 104 * registers, its identification register (offset 0xFF00) is read. This 105 * register specifies the maximum number of master interfaces the node 106 * supports. For RN-Fs it is assumed that there can be only a single fully 107 * coherent master resident on each node. The counts for each type of node 108 * are use to populate the array entry at the index corresponding to the node 109 * ID i.e. rn_info[node ID] = <number of master interfaces> 110 ******************************************************************************/ 111 static unsigned int ccn_get_rn_master_info(uintptr_t periphbase, 112 rn_info_t *info) 113 { 114 unsigned int num_masters = 0; 115 rn_types_t rn_type; 116 117 assert (info); 118 119 for (rn_type = RN_TYPE_RNF; rn_type < NUM_RN_TYPES; rn_type++) { 120 unsigned int mn_reg_off, node_id; 121 unsigned long long rn_bitmap; 122 123 /* 124 * RN-F, RN-I, RN-D node registers in the MN region occupy 125 * contiguous 16 byte apart offsets. 126 */ 127 mn_reg_off = MN_RNF_NODEID_OFFSET + (rn_type << 4); 128 rn_bitmap = ccn_reg_read(periphbase, MN_REGION_ID, mn_reg_off); 129 130 FOR_EACH_PRESENT_NODE_ID(node_id, rn_bitmap) { 131 unsigned int node_mcount; 132 133 /* 134 * A RN-F does not have a node type since it does not 135 * export a programmer's interface. It can only have a 136 * single fully coherent master residing on it. If the 137 * offset of the MN(Miscellaneous Node) register points 138 * to a RN-I/D node then the master count is set to the 139 * maximum number of master interfaces that can possibly 140 * reside on the node. 141 */ 142 node_mcount = (mn_reg_off == MN_RNF_NODEID_OFFSET ? 1 : 143 ccn_get_rni_mcount(periphbase, node_id)); 144 145 /* 146 * Use this value to increment the maximum possible 147 * master interfaces in the system. 148 */ 149 num_masters += node_mcount; 150 151 /* 152 * Update the entry in 'info' for this node ID with 153 * the maximum number of masters than can sit on 154 * it. This information will be used to validate the 155 * node information passed by the platform later. 156 */ 157 info->node_desc[node_id] = node_mcount; 158 } 159 } 160 161 return num_masters; 162 } 163 164 /******************************************************************************* 165 * This function validates parameters passed by the platform (in a debug build). 166 * It collects information about the maximum number of master interfaces that: 167 * a) the CCN IP can accommodate and 168 * b) can exist on each Request node. 169 * It compares this with the information provided by the platform to determine 170 * the validity of the latter. 171 ******************************************************************************/ 172 static void __init ccn_validate_plat_params(const ccn_desc_t *plat_desc) 173 { 174 unsigned int master_id, num_rn_masters; 175 rn_info_t info = { {0} }; 176 177 assert(plat_desc); 178 assert(plat_desc->periphbase); 179 assert(plat_desc->master_to_rn_id_map); 180 assert(plat_desc->num_masters); 181 assert(plat_desc->num_masters < CCN_MAX_RN_MASTERS); 182 183 /* 184 * Find the number and properties of fully coherent, IO coherent and IO 185 * coherent + DVM master interfaces 186 */ 187 num_rn_masters = ccn_get_rn_master_info(plat_desc->periphbase, &info); 188 assert(plat_desc->num_masters < num_rn_masters); 189 190 /* 191 * Iterate through the Request nodes specified by the platform. 192 * Decrement the count of the masters in the 'info' array for each 193 * Request node encountered. If the count would drop below 0 then the 194 * platform's view of this aspect of CCN configuration is incorrect. 195 */ 196 for (master_id = 0; master_id < plat_desc->num_masters; master_id++) { 197 unsigned int node_id; 198 199 node_id = plat_desc->master_to_rn_id_map[master_id]; 200 assert(node_id < MAX_RN_NODES); 201 assert(info.node_desc[node_id]); 202 info.node_desc[node_id]--; 203 } 204 } 205 #endif /* ENABLE_ASSERTIONS */ 206 207 /******************************************************************************* 208 * This function validates parameters passed by the platform (in a debug build) 209 * and initialises its internal data structures. A lock is required to prevent 210 * simultaneous CCN operations at runtime (only BL31) to add and remove Request 211 * nodes from coherency. 212 ******************************************************************************/ 213 void __init ccn_init(const ccn_desc_t *plat_desc) 214 { 215 #if ENABLE_ASSERTIONS 216 ccn_validate_plat_params(plat_desc); 217 #endif 218 219 ccn_plat_desc = plat_desc; 220 } 221 222 /******************************************************************************* 223 * This function converts a bit map of master interface IDs to a bit map of the 224 * Request node IDs that they reside on. 225 ******************************************************************************/ 226 static unsigned long long ccn_master_to_rn_id_map(unsigned long long master_map) 227 { 228 unsigned long long rn_id_map = 0; 229 unsigned int node_id, iface_id; 230 231 assert(master_map); 232 assert(ccn_plat_desc); 233 234 FOR_EACH_PRESENT_MASTER_INTERFACE(iface_id, master_map) { 235 assert(iface_id < ccn_plat_desc->num_masters); 236 237 /* Convert the master ID into the node ID */ 238 node_id = ccn_plat_desc->master_to_rn_id_map[iface_id]; 239 240 /* Set the bit corresponding to this node ID */ 241 rn_id_map |= (1ULL << node_id); 242 } 243 244 return rn_id_map; 245 } 246 247 /******************************************************************************* 248 * This function executes the necessary operations to add or remove Request node 249 * IDs specified in the 'rn_id_map' bitmap from the snoop/DVM domains specified 250 * in the 'hn_id_map'. The 'region_id' specifies the ID of the first HN-F/MN 251 * on which the operation should be performed. 'op_reg_offset' specifies the 252 * type of operation (add/remove). 'stat_reg_offset' specifies the register 253 * which should be polled to determine if the operation has completed or not. 254 ******************************************************************************/ 255 static void ccn_snoop_dvm_do_op(unsigned long long rn_id_map, 256 unsigned long long hn_id_map, 257 unsigned int region_id, 258 unsigned int op_reg_offset, 259 unsigned int stat_reg_offset) 260 { 261 unsigned int start_region_id; 262 263 assert(ccn_plat_desc); 264 assert(ccn_plat_desc->periphbase); 265 266 #if defined(IMAGE_BL31) || (!defined(__aarch64__) && defined(IMAGE_BL32)) 267 bakery_lock_get(&ccn_lock); 268 #endif 269 start_region_id = region_id; 270 FOR_EACH_PRESENT_REGION_ID(start_region_id, hn_id_map) { 271 ccn_reg_write(ccn_plat_desc->periphbase, 272 start_region_id, 273 op_reg_offset, 274 rn_id_map); 275 } 276 277 start_region_id = region_id; 278 279 FOR_EACH_PRESENT_REGION_ID(start_region_id, hn_id_map) { 280 WAIT_FOR_DOMAIN_CTRL_OP_COMPLETION(start_region_id, 281 stat_reg_offset, 282 op_reg_offset, 283 rn_id_map); 284 } 285 286 #if defined(IMAGE_BL31) || (!defined(__aarch64__) && defined(IMAGE_BL32)) 287 bakery_lock_release(&ccn_lock); 288 #endif 289 } 290 291 /******************************************************************************* 292 * The following functions provide the boot and runtime API to the platform for 293 * adding and removing master interfaces from the snoop/DVM domains. A bitmap of 294 * master interfaces IDs is passed as a parameter. It is converted into a bitmap 295 * of Request node IDs using the mapping provided by the platform while 296 * initialising the driver. 297 * For example, consider a dual cluster system where the clusters have values 0 298 * & 1 in the affinity level 1 field of their respective MPIDRs. While 299 * initialising this driver, the platform provides the mapping between each 300 * cluster and the corresponding Request node. To add or remove a cluster from 301 * the snoop and dvm domain, the bit position corresponding to the cluster ID 302 * should be set in the 'master_iface_map' i.e. to remove both clusters the 303 * bitmap would equal 0x11. 304 ******************************************************************************/ 305 void ccn_enter_snoop_dvm_domain(unsigned long long master_iface_map) 306 { 307 unsigned long long rn_id_map; 308 309 rn_id_map = ccn_master_to_rn_id_map(master_iface_map); 310 ccn_snoop_dvm_do_op(rn_id_map, 311 CCN_GET_HN_NODEID_MAP(ccn_plat_desc->periphbase, 312 MN_HNF_NODEID_OFFSET), 313 HNF_REGION_ID_START, 314 HNF_SDC_SET_OFFSET, 315 HNF_SDC_STAT_OFFSET); 316 317 ccn_snoop_dvm_do_op(rn_id_map, 318 CCN_GET_MN_NODEID_MAP(ccn_plat_desc->periphbase), 319 MN_REGION_ID, 320 MN_DDC_SET_OFFSET, 321 MN_DDC_STAT_OFFSET); 322 } 323 324 void ccn_exit_snoop_dvm_domain(unsigned long long master_iface_map) 325 { 326 unsigned long long rn_id_map; 327 328 rn_id_map = ccn_master_to_rn_id_map(master_iface_map); 329 ccn_snoop_dvm_do_op(rn_id_map, 330 CCN_GET_HN_NODEID_MAP(ccn_plat_desc->periphbase, 331 MN_HNF_NODEID_OFFSET), 332 HNF_REGION_ID_START, 333 HNF_SDC_CLR_OFFSET, 334 HNF_SDC_STAT_OFFSET); 335 336 ccn_snoop_dvm_do_op(rn_id_map, 337 CCN_GET_MN_NODEID_MAP(ccn_plat_desc->periphbase), 338 MN_REGION_ID, 339 MN_DDC_CLR_OFFSET, 340 MN_DDC_STAT_OFFSET); 341 } 342 343 void ccn_enter_dvm_domain(unsigned long long master_iface_map) 344 { 345 unsigned long long rn_id_map; 346 347 rn_id_map = ccn_master_to_rn_id_map(master_iface_map); 348 ccn_snoop_dvm_do_op(rn_id_map, 349 CCN_GET_MN_NODEID_MAP(ccn_plat_desc->periphbase), 350 MN_REGION_ID, 351 MN_DDC_SET_OFFSET, 352 MN_DDC_STAT_OFFSET); 353 } 354 355 void ccn_exit_dvm_domain(unsigned long long master_iface_map) 356 { 357 unsigned long long rn_id_map; 358 359 rn_id_map = ccn_master_to_rn_id_map(master_iface_map); 360 ccn_snoop_dvm_do_op(rn_id_map, 361 CCN_GET_MN_NODEID_MAP(ccn_plat_desc->periphbase), 362 MN_REGION_ID, 363 MN_DDC_CLR_OFFSET, 364 MN_DDC_STAT_OFFSET); 365 } 366 367 /******************************************************************************* 368 * This function returns the run mode of all the L3 cache partitions in the 369 * system. The state is expected to be one of NO_L3, SF_ONLY, L3_HAM or 370 * L3_FAM. Instead of comparing the states reported by all HN-Fs, the state of 371 * the first present HN-F node is reported. Since the driver does not export an 372 * interface to program them separately, there is no reason to perform this 373 * check. An HN-F could report that the L3 cache is transitioning from one mode 374 * to another e.g. HNF_PM_NOL3_2_SFONLY. In this case, the function waits for 375 * the transition to complete and reports the final state. 376 ******************************************************************************/ 377 unsigned int ccn_get_l3_run_mode(void) 378 { 379 unsigned long long hnf_pstate_stat; 380 381 assert(ccn_plat_desc); 382 assert(ccn_plat_desc->periphbase); 383 384 /* 385 * Wait for a L3 cache partition to enter any run mode. The pstate 386 * parameter is read from an HN-F P-state status register. A non-zero 387 * value in bits[1:0] means that the cache is transitioning to a run 388 * mode. 389 */ 390 do { 391 hnf_pstate_stat = ccn_reg_read(ccn_plat_desc->periphbase, 392 HNF_REGION_ID_START, 393 HNF_PSTATE_STAT_OFFSET); 394 } while (hnf_pstate_stat & 0x3); 395 396 return PSTATE_TO_RUN_MODE(hnf_pstate_stat); 397 } 398 399 /******************************************************************************* 400 * This function sets the run mode of all the L3 cache partitions in the 401 * system to one of NO_L3, SF_ONLY, L3_HAM or L3_FAM depending upon the state 402 * specified by the 'mode' argument. 403 ******************************************************************************/ 404 void ccn_set_l3_run_mode(unsigned int mode) 405 { 406 unsigned long long mn_hnf_id_map, hnf_pstate_stat; 407 unsigned int region_id; 408 409 assert(ccn_plat_desc); 410 assert(ccn_plat_desc->periphbase); 411 assert(mode <= CCN_L3_RUN_MODE_FAM); 412 413 mn_hnf_id_map = ccn_reg_read(ccn_plat_desc->periphbase, 414 MN_REGION_ID, 415 MN_HNF_NODEID_OFFSET); 416 region_id = HNF_REGION_ID_START; 417 418 /* Program the desired run mode */ 419 FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) { 420 ccn_reg_write(ccn_plat_desc->periphbase, 421 region_id, 422 HNF_PSTATE_REQ_OFFSET, 423 mode); 424 } 425 426 /* Wait for the caches to transition to the run mode */ 427 region_id = HNF_REGION_ID_START; 428 FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) { 429 /* 430 * Wait for a L3 cache partition to enter a target run 431 * mode. The pstate parameter is read from an HN-F P-state 432 * status register. 433 */ 434 do { 435 hnf_pstate_stat = ccn_reg_read(ccn_plat_desc->periphbase, 436 region_id, 437 HNF_PSTATE_STAT_OFFSET); 438 } while (((hnf_pstate_stat & HNF_PSTATE_MASK) >> 2) != mode); 439 } 440 } 441 442 /******************************************************************************* 443 * This function configures system address map and provides option to enable the 444 * 3SN striping mode of Slave node operation. The Slave node IDs and the Top 445 * Address bit1 and bit0 are provided as parameters to this function. This 446 * configuration is needed only if network contains a single SN-F or 3 SN-F and 447 * must be completed before the first request by the system to normal memory. 448 ******************************************************************************/ 449 void ccn_program_sys_addrmap(unsigned int sn0_id, 450 unsigned int sn1_id, 451 unsigned int sn2_id, 452 unsigned int top_addr_bit0, 453 unsigned int top_addr_bit1, 454 unsigned char three_sn_en) 455 { 456 unsigned long long mn_hnf_id_map, hnf_sam_ctrl_value; 457 unsigned int region_id; 458 459 assert(ccn_plat_desc); 460 assert(ccn_plat_desc->periphbase); 461 462 mn_hnf_id_map = ccn_reg_read(ccn_plat_desc->periphbase, 463 MN_REGION_ID, 464 MN_HNF_NODEID_OFFSET); 465 region_id = HNF_REGION_ID_START; 466 hnf_sam_ctrl_value = MAKE_HNF_SAM_CTRL_VALUE(sn0_id, 467 sn1_id, 468 sn2_id, 469 top_addr_bit0, 470 top_addr_bit1, 471 three_sn_en); 472 473 FOR_EACH_PRESENT_REGION_ID(region_id, mn_hnf_id_map) { 474 475 /* Program the SAM control register */ 476 ccn_reg_write(ccn_plat_desc->periphbase, 477 region_id, 478 HNF_SAM_CTRL_OFFSET, 479 hnf_sam_ctrl_value); 480 } 481 482 } 483 484 /******************************************************************************* 485 * This function returns the part0 id from the peripheralID 0 register 486 * in CCN. This id can be used to distinguish the CCN variant present in the 487 * system. 488 ******************************************************************************/ 489 int ccn_get_part0_id(uintptr_t periphbase) 490 { 491 assert(periphbase); 492 return (int)(mmio_read_64(periphbase 493 + MN_PERIPH_ID_0_1_OFFSET) & 0xFF); 494 } 495 496 /******************************************************************************* 497 * This function returns the region id corresponding to a node_id of node_type. 498 ******************************************************************************/ 499 static unsigned int get_region_id_for_node(node_types_t node_type, 500 unsigned int node_id) 501 { 502 unsigned int mn_reg_off, region_id; 503 unsigned long long node_bitmap; 504 unsigned int loc_node_id, node_pos_in_map = 0; 505 506 assert(node_type < NUM_NODE_TYPES); 507 assert(node_id < MAX_RN_NODES); 508 509 switch (node_type) { 510 case NODE_TYPE_RNI: 511 region_id = RNI_REGION_ID_START; 512 break; 513 case NODE_TYPE_HNF: 514 region_id = HNF_REGION_ID_START; 515 break; 516 case NODE_TYPE_HNI: 517 region_id = HNI_REGION_ID_START; 518 break; 519 case NODE_TYPE_SN: 520 region_id = SBSX_REGION_ID_START; 521 break; 522 default: 523 ERROR("Un-supported Node Type = %d.\n", node_type); 524 assert(false); 525 return REGION_ID_LIMIT; 526 } 527 /* 528 * RN-I, HN-F, HN-I, SN node registers in the MN region 529 * occupy contiguous 16 byte apart offsets. 530 * 531 * RN-F and RN-D node are not supported as 532 * none of them exposes any memory map to 533 * configure any of their offset registers. 534 */ 535 536 mn_reg_off = MN_RNF_NODEID_OFFSET + (node_type << 4); 537 node_bitmap = ccn_reg_read(ccn_plat_desc->periphbase, 538 MN_REGION_ID, mn_reg_off); 539 540 assert((node_bitmap & (1ULL << (node_id))) != 0U); 541 542 543 FOR_EACH_PRESENT_NODE_ID(loc_node_id, node_bitmap) { 544 INFO("Index = %u with loc_nod=%u and input nod=%u\n", 545 node_pos_in_map, loc_node_id, node_id); 546 if (loc_node_id == node_id) 547 break; 548 node_pos_in_map++; 549 } 550 551 if (node_pos_in_map == CCN_MAX_RN_MASTERS) { 552 ERROR("Node Id = %d, is not found.\n", node_id); 553 assert(false); 554 return REGION_ID_LIMIT; 555 } 556 557 /* 558 * According to section 3.1.1 in CCN specification, region offset for 559 * the RN-I components is calculated as (128 + NodeID of RN-I). 560 */ 561 if (node_type == NODE_TYPE_RNI) 562 region_id += node_id; 563 else 564 region_id += node_pos_in_map; 565 566 return region_id; 567 } 568 569 /******************************************************************************* 570 * This function sets the value 'val' to the register at register_offset from 571 * the base address pointed to by the region_id. 572 * where, region id is mapped to a node_id of node_type. 573 ******************************************************************************/ 574 void ccn_write_node_reg(node_types_t node_type, unsigned int node_id, 575 unsigned int reg_offset, unsigned long long val) 576 { 577 unsigned int region_id = get_region_id_for_node(node_type, node_id); 578 579 if (reg_offset > REGION_ID_OFFSET) { 580 ERROR("Invalid Register offset 0x%x is provided.\n", 581 reg_offset); 582 assert(false); 583 return; 584 } 585 586 /* Setting the value of Auxiliary Control Register of the Node */ 587 ccn_reg_write(ccn_plat_desc->periphbase, region_id, reg_offset, val); 588 VERBOSE("Value is successfully written at address 0x%lx.\n", 589 (ccn_plat_desc->periphbase 590 + region_id_to_base(region_id)) 591 + reg_offset); 592 } 593 594 /******************************************************************************* 595 * This function read the value 'val' stored in the register at register_offset 596 * from the base address pointed to by the region_id. 597 * where, region id is mapped to a node_id of node_type. 598 ******************************************************************************/ 599 unsigned long long ccn_read_node_reg(node_types_t node_type, 600 unsigned int node_id, 601 unsigned int reg_offset) 602 { 603 unsigned long long val; 604 unsigned int region_id = get_region_id_for_node(node_type, node_id); 605 606 if (reg_offset > REGION_ID_OFFSET) { 607 ERROR("Invalid Register offset 0x%x is provided.\n", 608 reg_offset); 609 assert(false); 610 return ULL(0); 611 } 612 613 /* Setting the value of Auxiliary Control Register of the Node */ 614 val = ccn_reg_read(ccn_plat_desc->periphbase, region_id, reg_offset); 615 VERBOSE("Value is successfully read from address 0x%lx.\n", 616 (ccn_plat_desc->periphbase 617 + region_id_to_base(region_id)) 618 + reg_offset); 619 620 return val; 621 } 622