1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. ([email protected]). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
21 * permission.
22
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
26 * countries.
27
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39
40 /**
41 * @file
42 *
43 * Provides APIs for applications to register for hotplug. It also provides
44 * APIs for requesting shutdown of a running target application.
45 *
46 * <hr>$Revision: $<hr>
47 */
48
49 #include "cvmx-app-hotplug.h"
50 #include "cvmx-spinlock.h"
51 #include "cvmx-debug.h"
52
53 //#define DEBUG 1
54
55 static cvmx_app_hotplug_global_t *hotplug_global_ptr = 0;
56
57 #ifndef CVMX_BUILD_FOR_LINUX_USER
58
59 static CVMX_SHARED cvmx_spinlock_t cvmx_app_hotplug_sync_lock = { CVMX_SPINLOCK_UNLOCKED_VAL };
60 static CVMX_SHARED cvmx_spinlock_t cvmx_app_hotplug_lock = { CVMX_SPINLOCK_UNLOCKED_VAL };
61 static CVMX_SHARED cvmx_app_hotplug_info_t *cvmx_app_hotplug_info_ptr = NULL;
62
63 static void __cvmx_app_hotplug_shutdown(int irq_number, uint64_t registers[32], void *user_arg);
64 static void __cvmx_app_hotplug_sync(void);
65 static void __cvmx_app_hotplug_reset(void);
66
67 /* Declaring this array here is a compile time check to ensure that the
68 size of cvmx_app_hotplug_info_t is 1024. If the size is not 1024
69 the size of the array will be -1 and this results in a compilation
70 error */
71 char __hotplug_info_check[(sizeof(cvmx_app_hotplug_info_t) == 1024) ? 1 : -1];
72 /**
73 * This routine registers an application for hotplug. It installs a handler for
74 * any incoming shutdown request. It also registers a callback routine from the
75 * application. This callback is invoked when the application receives a
76 * shutdown notification.
77 *
78 * This routine only needs to be called once per application.
79 *
80 * @param fn Callback routine from the application.
81 * @param arg Argument to the application callback routine.
82 * @return Return 0 on success, -1 on failure
83 *
84 */
cvmx_app_hotplug_register(void (* fn)(void *),void * arg)85 int cvmx_app_hotplug_register(void(*fn)(void*), void* arg)
86 {
87 /* Find the list of applications launched by bootoct utility. */
88
89 if (!(cvmx_app_hotplug_info_ptr = cvmx_app_hotplug_get_info(cvmx_sysinfo_get()->core_mask)))
90 {
91 /* Application not launched by bootoct? */
92 printf("ERROR: cmvx_app_hotplug_register() failed\n");
93 return -1;
94 }
95
96 /* Register the callback */
97 cvmx_app_hotplug_info_ptr->data = CAST64(arg);
98 cvmx_app_hotplug_info_ptr->shutdown_callback = CAST64(fn);
99
100 #ifdef DEBUG
101 printf("cvmx_app_hotplug_register(): coremask 0x%x valid %d\n",
102 cvmx_app_hotplug_info_ptr->coremask, cvmx_app_hotplug_info_ptr->valid);
103 #endif
104
105 cvmx_interrupt_register(CVMX_IRQ_MBOX0, __cvmx_app_hotplug_shutdown, NULL);
106
107 return 0;
108 }
109
110 /**
111 * This routine deprecates the the cvmx_app_hotplug_register method. This
112 * registers application for hotplug and the application will have CPU
113 * hotplug callbacks. Various callbacks are specified in cb.
114 * cvmx_app_hotplug_callbacks_t documents the callbacks
115 *
116 * This routine only needs to be called once per application.
117 *
118 * @param cb Callback routine from the application.
119 * @param arg Argument to the application callback routins
120 * @param app_shutdown When set to 1 the application will invoke core_shutdown
121 on each core. When set to 0 core shutdown will be
122 called invoked automatically after invoking the
123 application callback.
124 * @return Return index of app on success, -1 on failure
125 *
126 */
cvmx_app_hotplug_register_cb(cvmx_app_hotplug_callbacks_t * cb,void * arg,int app_shutdown)127 int cvmx_app_hotplug_register_cb(cvmx_app_hotplug_callbacks_t *cb, void* arg,
128 int app_shutdown)
129 {
130 cvmx_app_hotplug_info_t *app_info;
131
132 /* Find the list of applications launched by bootoct utility. */
133 app_info = cvmx_app_hotplug_get_info(cvmx_sysinfo_get()->core_mask);
134 cvmx_app_hotplug_info_ptr = app_info;
135 if (!app_info)
136 {
137 /* Application not launched by bootoct? */
138 printf("ERROR: cmvx_app_hotplug_register() failed\n");
139 return -1;
140 }
141 /* Register the callback */
142 app_info->data = CAST64(arg);
143 app_info->shutdown_callback = CAST64(cb->shutdown_callback);
144 app_info->cores_added_callback = CAST64(cb->cores_added_callback);
145 app_info->cores_removed_callback = CAST64(cb->cores_removed_callback);
146 app_info->unplug_callback = CAST64(cb->unplug_core_callback);
147 app_info->hotplug_start = CAST64(cb->hotplug_start);
148 app_info->app_shutdown = app_shutdown;
149 #ifdef DEBUG
150 printf("cvmx_app_hotplug_register(): coremask 0x%x valid %d\n",
151 app_info->coremask, app_info->valid);
152 #endif
153
154 cvmx_interrupt_register(CVMX_IRQ_MBOX0, __cvmx_app_hotplug_shutdown, NULL);
155 return 0;
156
157 }
158
cvmx_app_hotplug_remove_self_from_core_mask(void)159 void cvmx_app_hotplug_remove_self_from_core_mask(void)
160 {
161 int core = cvmx_get_core_num();
162 uint32_t core_mask = 1ull << core;
163
164 cvmx_spinlock_lock(&cvmx_app_hotplug_lock);
165 cvmx_app_hotplug_info_ptr->coremask = cvmx_app_hotplug_info_ptr->coremask & ~core_mask ;
166 cvmx_app_hotplug_info_ptr->hotplug_activated_coremask =
167 cvmx_app_hotplug_info_ptr->hotplug_activated_coremask & ~core_mask ;
168 cvmx_spinlock_unlock(&cvmx_app_hotplug_lock);
169 }
170
171
172
173 /**
174 * Returns 1 if the running core is being unplugged, else it returns 0.
175 */
is_core_being_unplugged(void)176 int is_core_being_unplugged(void)
177 {
178 if (cvmx_app_hotplug_info_ptr->unplug_cores &
179 (1ull << cvmx_get_core_num()))
180 return 1;
181 return 0;
182 }
183
184
185 /**
186 * Activate the current application core for receiving hotplug shutdown requests.
187 *
188 * This routine makes sure that each core belonging to the application is enabled
189 * to receive the shutdown notification and also provides a barrier sync to make
190 * sure that all cores are ready.
191 */
cvmx_app_hotplug_activate(void)192 int cvmx_app_hotplug_activate(void)
193 {
194 uint64_t cnt = 0;
195 uint64_t cnt_interval = 10000000;
196
197 while (!cvmx_app_hotplug_info_ptr)
198 {
199 cnt++;
200 if ((cnt % cnt_interval) == 0)
201 printf("waiting for cnt=%lld\n", (unsigned long long)cnt);
202 }
203
204 if (cvmx_app_hotplug_info_ptr->hplugged_cores & (1ull << cvmx_get_core_num()))
205 {
206 #ifdef DEBUG
207 printf("core=%d : is being hotplugged \n", cvmx_get_core_num());
208 #endif
209 cvmx_sysinfo_t *sys_info_ptr = cvmx_sysinfo_get();
210 sys_info_ptr->core_mask |= 1ull << cvmx_get_core_num();
211 }
212 else
213 {
214 __cvmx_app_hotplug_sync();
215 }
216 cvmx_spinlock_lock(&cvmx_app_hotplug_lock);
217 if (!cvmx_app_hotplug_info_ptr)
218 {
219 cvmx_spinlock_unlock(&cvmx_app_hotplug_lock);
220 printf("ERROR: This application is not registered for hotplug\n");
221 return -1;
222 }
223 /* Enable the interrupt before we mark the core as activated */
224 cvmx_interrupt_unmask_irq(CVMX_IRQ_MBOX0);
225 cvmx_app_hotplug_info_ptr->hotplug_activated_coremask |= (1ull<<cvmx_get_core_num());
226
227 #ifdef DEBUG
228 printf("cvmx_app_hotplug_activate(): coremask 0x%x valid %d sizeof %d\n",
229 cvmx_app_hotplug_info_ptr->coremask, cvmx_app_hotplug_info_ptr->valid,
230 sizeof(*cvmx_app_hotplug_info_ptr));
231 #endif
232
233 cvmx_spinlock_unlock(&cvmx_app_hotplug_lock);
234
235 return 0;
236 }
237
238 /**
239 * This routine is only required if cvmx_app_hotplug_shutdown_request() was called
240 * with wait=0. This routine waits for the application shutdown to complete.
241 *
242 * @param coremask Coremask the application is running on.
243 * @return 0 on success, -1 on error
244 *
245 */
cvmx_app_hotplug_shutdown_complete(uint32_t coremask)246 int cvmx_app_hotplug_shutdown_complete(uint32_t coremask)
247 {
248 cvmx_app_hotplug_info_t *hotplug_info_ptr;
249
250 if (!(hotplug_info_ptr = cvmx_app_hotplug_get_info(coremask)))
251 {
252 printf("\nERROR: Failed to get hotplug info for coremask: 0x%x\n", (unsigned int)coremask);
253 return -1;
254 }
255
256 while(!hotplug_info_ptr->shutdown_done);
257
258 /* Clean up the hotplug info region for this app */
259 bzero(hotplug_info_ptr, sizeof(*hotplug_info_ptr));
260
261 return 0;
262 }
263
264 /**
265 * Disable recognition of any incoming shutdown request.
266 */
267
cvmx_app_hotplug_shutdown_disable(void)268 void cvmx_app_hotplug_shutdown_disable(void)
269 {
270 cvmx_interrupt_mask_irq(CVMX_IRQ_MBOX0);
271 }
272
273 /**
274 * Re-enable recognition of incoming shutdown requests.
275 */
276
cvmx_app_hotplug_shutdown_enable(void)277 void cvmx_app_hotplug_shutdown_enable(void)
278 {
279 cvmx_interrupt_unmask_irq(CVMX_IRQ_MBOX0);
280 }
281
282 /**
283 * Request shutdown of the currently running core. Should be
284 * called by the application when it has been registered with
285 * app_shutdown option set to 1.
286 */
cvmx_app_hotplug_core_shutdown(void)287 void cvmx_app_hotplug_core_shutdown(void)
288 {
289 uint32_t flags;
290 if (cvmx_app_hotplug_info_ptr->shutdown_cores)
291 {
292 cvmx_sysinfo_t *sys_info_ptr = cvmx_sysinfo_get();
293 __cvmx_app_hotplug_sync();
294 if (cvmx_coremask_first_core(sys_info_ptr->core_mask))
295 {
296 bzero(cvmx_app_hotplug_info_ptr,
297 sizeof(*cvmx_app_hotplug_info_ptr));
298 #ifdef DEBUG
299 printf("__cvmx_app_hotplug_shutdown(): setting shutdown done! \n");
300 #endif
301 cvmx_app_hotplug_info_ptr->shutdown_done = 1;
302 }
303 /* Tell the debugger that this application is finishing. */
304 cvmx_debug_finish ();
305 flags = cvmx_interrupt_disable_save();
306 __cvmx_app_hotplug_sync();
307 /* Reset the core */
308 __cvmx_app_hotplug_reset();
309 }
310 else
311 {
312 cvmx_sysinfo_remove_self_from_core_mask();
313 cvmx_app_hotplug_remove_self_from_core_mask();
314 flags = cvmx_interrupt_disable_save();
315 __cvmx_app_hotplug_reset();
316 }
317 }
318
319 /*
320 * ISR for the incoming shutdown request interrupt.
321 */
__cvmx_app_hotplug_shutdown(int irq_number,uint64_t registers[32],void * user_arg)322 static void __cvmx_app_hotplug_shutdown(int irq_number, uint64_t registers[32],
323 void *user_arg)
324 {
325 cvmx_sysinfo_t *sys_info_ptr = cvmx_sysinfo_get();
326 uint64_t mbox;
327 cvmx_app_hotplug_info_t *ai = cvmx_app_hotplug_info_ptr;
328 int dbg = 0;
329
330 #ifdef DEBUG
331 dbg = 1;
332 #endif
333 cvmx_interrupt_mask_irq(CVMX_IRQ_MBOX0);
334
335 mbox = cvmx_read_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()));
336 /* Clear the interrupt */
337 cvmx_write_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()), mbox);
338
339 /* Make sure the write above completes */
340 cvmx_read_csr(CVMX_CIU_MBOX_CLRX(cvmx_get_core_num()));
341
342
343 if (!cvmx_app_hotplug_info_ptr)
344 {
345 printf("ERROR: Application is not registered for hotplug!\n");
346 return;
347 }
348
349 if (ai->hotplug_activated_coremask != sys_info_ptr->core_mask)
350 {
351 printf("ERROR: Shutdown requested when not all app cores have "
352 "activated hotplug\n" "Application coremask: 0x%x Hotplug "
353 "coremask: 0x%x\n", (unsigned int)sys_info_ptr->core_mask,
354 (unsigned int)ai->hotplug_activated_coremask);
355 return;
356 }
357
358 if (mbox & 1ull)
359 {
360 int core = cvmx_get_core_num();
361 if (dbg)
362 printf("Shutting down application .\n");
363 /* Call the application's own callback function */
364 if (ai->shutdown_callback)
365 {
366 ((void(*)(void*))(long)ai->shutdown_callback)(CASTPTR(void *, ai->data));
367 }
368 else
369 {
370 printf("ERROR : Shutdown callback has not been registered\n");
371 }
372 if (!ai->app_shutdown)
373 {
374 if (dbg)
375 printf("%s : core = %d Invoking app shutdown\n", __FUNCTION__, core);
376 cvmx_app_hotplug_core_shutdown();
377 }
378 }
379 else if (mbox & 2ull)
380 {
381 int core = cvmx_get_core_num();
382 int unplug = is_core_being_unplugged();
383 if (dbg) printf("%s : core=%d Unplug event \n", __FUNCTION__, core);
384
385 if (unplug)
386 {
387 /* Call the application's own callback function */
388 if (ai->unplug_callback)
389 {
390 if (dbg) printf("%s : core=%d Calling unplug callback\n",
391 __FUNCTION__, core);
392 ((void(*)(void*))(long)ai->unplug_callback)(CASTPTR(void *,
393 ai->data));
394 }
395 if (!ai->app_shutdown)
396 {
397 if (dbg) printf("%s : core = %d Invoking app shutdown\n",
398 __FUNCTION__, core);
399 cvmx_app_hotplug_core_shutdown();
400 }
401 }
402 else
403 {
404 if (ai->cores_removed_callback)
405 {
406 if (dbg) printf("%s : core=%d Calling cores removed callback\n",
407 __FUNCTION__, core);
408 ((void(*)(uint32_t, void*))(long)ai->cores_removed_callback)
409 (ai->unplug_cores, CASTPTR(void *, ai->data));
410 }
411 cvmx_interrupt_unmask_irq(CVMX_IRQ_MBOX0);
412 }
413 }
414 else if (mbox & 4ull)
415 {
416 int core = cvmx_get_core_num();
417 if (dbg) printf("%s : core=%d Add cores event \n", __FUNCTION__, core);
418
419 if (ai->cores_added_callback)
420 {
421 if (dbg) printf("%s : core=%d Calling cores added callback\n",
422 __FUNCTION__, core);
423 ((void(*)(uint32_t, void*))(long)ai->cores_added_callback)
424 (ai->hplugged_cores, CASTPTR(void *, ai->data));
425 }
426 cvmx_interrupt_unmask_irq(CVMX_IRQ_MBOX0);
427 }
428 else
429 {
430 printf("ERROR: unexpected mbox=%llx\n", (unsigned long long)mbox);
431 }
432
433 }
434
__cvmx_app_hotplug_reset(void)435 void __cvmx_app_hotplug_reset(void)
436 {
437 #define IDLE_CORE_BLOCK_NAME "idle-core-loop"
438 #define HPLUG_MAKE_XKPHYS(x) ((1ULL << 63) | (x))
439 uint64_t reset_addr;
440 const cvmx_bootmem_named_block_desc_t *block_desc;
441
442 block_desc = cvmx_bootmem_find_named_block(IDLE_CORE_BLOCK_NAME);
443 if (!block_desc) {
444 cvmx_dprintf("Named block(%s) is not created\n", IDLE_CORE_BLOCK_NAME);
445 /* loop here, should not happen */
446 __asm__ volatile (
447 ".set noreorder \n"
448 "\tsync \n"
449 "\tnop \n"
450 "1:\twait \n"
451 "\tb 1b \n"
452 "\tnop \n"
453 ".set reorder \n"
454 ::
455 );
456 }
457
458 reset_addr = HPLUG_MAKE_XKPHYS(block_desc->base_addr);
459 asm volatile (" .set push \n"
460 " .set mips64 \n"
461 " .set noreorder \n"
462 " move $2, %[addr] \n"
463 " jr $2 \n"
464 " nop \n"
465 " .set pop "
466 :: [addr] "r"(reset_addr)
467 : "$2");
468
469 /*Should never reach here*/
470 while (1) ;
471
472 }
473
474 /*
475 * We need a separate sync operation from cvmx_coremask_barrier_sync() to
476 * avoid a deadlock on state.lock, since the application itself maybe doing a
477 * cvmx_coremask_barrier_sync().
478 */
__cvmx_app_hotplug_sync(void)479 static void __cvmx_app_hotplug_sync(void)
480 {
481 static CVMX_SHARED volatile uint32_t sync_coremask = 0;
482 cvmx_sysinfo_t *sys_info_ptr = cvmx_sysinfo_get();
483
484 cvmx_spinlock_lock(&cvmx_app_hotplug_sync_lock);
485
486 sync_coremask |= cvmx_coremask_core(cvmx_get_core_num());
487
488 cvmx_spinlock_unlock(&cvmx_app_hotplug_sync_lock);
489
490 while (sync_coremask != sys_info_ptr->core_mask);
491
492 cvmx_spinlock_lock(&cvmx_app_hotplug_sync_lock);
493 sync_coremask = 0;
494 cvmx_spinlock_unlock(&cvmx_app_hotplug_sync_lock);
495
496
497 }
498
499 #endif /* CVMX_BUILD_FOR_LINUX_USER */
500
501 /**
502 * Returns 1 if the running core is being hotplugged, else it returns 0.
503 */
is_core_being_hot_plugged(void)504 int is_core_being_hot_plugged(void)
505 {
506
507 #ifndef CVMX_BUILD_FOR_LINUX_USER
508 if (!cvmx_app_hotplug_info_ptr) return 0;
509 if (cvmx_app_hotplug_info_ptr->hplugged_cores &
510 (1ull << cvmx_get_core_num()))
511 return 1;
512 return 0;
513 #else
514 return 0;
515 #endif
516 }
517
cvmx_app_get_hotplug_global_ptr(void)518 static cvmx_app_hotplug_global_t *cvmx_app_get_hotplug_global_ptr(void)
519 {
520 const struct cvmx_bootmem_named_block_desc *block_desc;
521 cvmx_app_hotplug_global_t *hgp;
522
523 if(hotplug_global_ptr != 0) return hotplug_global_ptr;
524
525 block_desc = cvmx_bootmem_find_named_block(CVMX_APP_HOTPLUG_INFO_REGION_NAME);
526 if (!block_desc)
527 {
528 printf("ERROR: Hotplug info region is not setup\n");
529 return NULL;
530 }
531 else
532 #ifdef CVMX_BUILD_FOR_LINUX_USER
533 {
534 size_t pg_sz = sysconf(_SC_PAGESIZE), size;
535 off_t offset;
536 char *vaddr;
537 int fd;
538
539 if ((fd = open("/dev/mem", O_RDWR)) == -1) {
540 perror("open");
541 return NULL;
542 }
543
544 /*
545 * We need to mmap() this memory, since this was allocated from the
546 * kernel bootup code and does not reside in the RESERVE32 region.
547 */
548 size = CVMX_APP_HOTPLUG_INFO_REGION_SIZE + pg_sz-1;
549 offset = block_desc->base_addr & ~(pg_sz-1);
550 if ((vaddr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset))
551 == MAP_FAILED)
552 {
553 perror("mmap");
554 return NULL;
555 }
556
557 hgp = (cvmx_app_hotplug_global_t *)(vaddr + ( block_desc->base_addr & (pg_sz-1)));
558 }
559 #else
560 hgp = CASTPTR(void, CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, block_desc->base_addr));
561 #endif
562 hotplug_global_ptr = hgp;
563 return hgp;
564
565 }
566
567 /**
568 * Return the hotplug info structure (cvmx_app_hotplug_info_t) pointer for the
569 * application running on the given coremask.
570 *
571 * @param coremask Coremask of application.
572 * @return Returns hotplug info struct on success, NULL on failure
573 *
574 */
cvmx_app_hotplug_get_info(uint32_t coremask)575 cvmx_app_hotplug_info_t* cvmx_app_hotplug_get_info(uint32_t coremask)
576 {
577 cvmx_app_hotplug_info_t *hip;
578 cvmx_app_hotplug_global_t *hgp;
579 int i;
580 int dbg = 0;
581
582 #ifdef DEBUG
583 dbg = 1;
584 #endif
585 hgp = cvmx_app_get_hotplug_global_ptr();
586 if (!hgp) return NULL;
587 hip = hgp->hotplug_info_array;
588
589 /* Look for the current app's info */
590 for (i=0; i<CVMX_APP_HOTPLUG_MAX_APPS; i++)
591 {
592 if (hip[i].coremask == coremask)
593 {
594 if (dbg)
595 printf("cvmx_app_hotplug_get_info(): coremask match %d -- coremask 0x%x, valid %d\n", i, (unsigned int)hip[i].coremask, (unsigned int)hip[i].valid);
596 return &hip[i];
597 }
598 }
599 return NULL;
600 }
601
602 /**
603 * Return the hotplug application index structure for the application running on the
604 * given coremask.
605 *
606 * @param coremask Coremask of application.
607 * @return Returns hotplug application index on success. -1 on failure
608 *
609 */
cvmx_app_hotplug_get_index(uint32_t coremask)610 int cvmx_app_hotplug_get_index(uint32_t coremask)
611 {
612 cvmx_app_hotplug_info_t *hip;
613 cvmx_app_hotplug_global_t *hgp;
614 int i;
615 int dbg = 0;
616
617 #ifdef DEBUG
618 dbg = 1;
619 #endif
620 hgp = cvmx_app_get_hotplug_global_ptr();
621 if (!hgp) return -1;
622 hip = hgp->hotplug_info_array;
623
624 /* Look for the current app's info */
625 for (i=0; i<CVMX_APP_HOTPLUG_MAX_APPS; i++)
626 {
627 if (hip[i].coremask == coremask)
628 {
629 if (dbg)
630 printf("cvmx_app_hotplug_get_info(): coremask match %d -- coremask 0x%x valid %d\n", i, (unsigned int)hip[i].coremask, (unsigned int)hip[i].valid);
631 return i;
632 }
633 }
634 return -1;
635 }
636
print_hot_plug_info(cvmx_app_hotplug_info_t * hpinfo)637 void print_hot_plug_info(cvmx_app_hotplug_info_t* hpinfo)
638 {
639 printf("name=%s coremask=%08x hotplugged coremask=%08x valid=%d\n", hpinfo->app_name,
640 (unsigned int)hpinfo->coremask, (unsigned int)hpinfo->hotplug_activated_coremask, (unsigned int)hpinfo->valid);
641 }
642
643 /**
644 * Return the hotplug info structure (cvmx_app_hotplug_info_t) pointer for the
645 * application with the specified index.
646 *
647 * @param index index of application.
648 * @return Returns hotplug info struct on success, NULL on failure
649 *
650 */
cvmx_app_hotplug_get_info_at_index(int index)651 cvmx_app_hotplug_info_t* cvmx_app_hotplug_get_info_at_index(int index)
652 {
653 cvmx_app_hotplug_info_t *hip;
654 cvmx_app_hotplug_global_t *hgp;
655
656 hgp = cvmx_app_get_hotplug_global_ptr();
657 if (!hgp) return NULL;
658 hip = hgp->hotplug_info_array;
659
660 #ifdef DEBUG
661 printf("cvmx_app_hotplug_get_info(): hotplug_info phy addr 0x%llx ptr %p\n",
662 block_desc->base_addr, hgp);
663 #endif
664 if (index < CVMX_APP_HOTPLUG_MAX_APPS)
665 {
666 if (hip[index].valid)
667 {
668 //print_hot_plug_info( &hip[index] );
669 return &hip[index];
670 }
671 }
672 return NULL;
673 }
674
675 /**
676 * Determines if SE application at the index specified is hotpluggable.
677 *
678 * @param index index of application.
679 * @return Returns -1 on error.
680 * 0 -> The application is not hotpluggable
681 * 1 -> The application is hotpluggable
682 */
is_app_hotpluggable(int index)683 int is_app_hotpluggable(int index)
684 {
685 cvmx_app_hotplug_info_t *ai;
686
687 if (!(ai = cvmx_app_hotplug_get_info_at_index(index)))
688 {
689 printf("\nERROR: Failed to get hotplug info for app at index=%d\n", index);
690 return -1;
691 }
692 if (ai->hotplug_activated_coremask) return 1;
693 return 0;
694 }
695
696 /**
697 * This routine sends a shutdown request to a running target application.
698 *
699 * @param coremask Coremask the application is running on.
700 * @param wait 1 - Wait for shutdown completion
701 * 0 - Do not wait
702 * @return 0 on success, -1 on error
703 *
704 */
705
cvmx_app_hotplug_shutdown_request(uint32_t coremask,int wait)706 int cvmx_app_hotplug_shutdown_request(uint32_t coremask, int wait)
707 {
708 int i;
709 cvmx_app_hotplug_info_t *hotplug_info_ptr;
710
711 if (!(hotplug_info_ptr = cvmx_app_hotplug_get_info(coremask)))
712 {
713 printf("\nERROR: Failed to get hotplug info for coremask: 0x%x\n", (unsigned int)coremask);
714 return -1;
715 }
716 hotplug_info_ptr->shutdown_cores = coremask;
717 if (!hotplug_info_ptr->shutdown_callback)
718 {
719 printf("\nERROR: Target application has not registered for hotplug!\n");
720 return -1;
721 }
722
723 if (hotplug_info_ptr->hotplug_activated_coremask != coremask)
724 {
725 printf("\nERROR: Not all application cores have activated hotplug\n");
726 return -1;
727 }
728
729 /* Send IPIs to all application cores to request shutdown */
730 for (i=0; i<CVMX_MAX_CORES; i++) {
731 if (coremask & (1ull<<i))
732 cvmx_write_csr(CVMX_CIU_MBOX_SETX(i), 1);
733 }
734
735 if (wait)
736 {
737 while (!hotplug_info_ptr->shutdown_done);
738
739 /* Clean up the hotplug info region for this application */
740 bzero(hotplug_info_ptr, sizeof(*hotplug_info_ptr));
741 }
742
743 return 0;
744 }
745
746
747
748 /**
749 * This routine invokes the invoked the cores_added callbacks.
750 */
cvmx_app_hotplug_call_add_cores_callback(int index)751 int cvmx_app_hotplug_call_add_cores_callback(int index)
752 {
753 cvmx_app_hotplug_info_t *ai;
754 int i;
755 if (!(ai = cvmx_app_hotplug_get_info_at_index(index)))
756 {
757 printf("\nERROR: Failed to get hotplug info for app at index=%d\n", index);
758 return -1;
759 }
760 /* Send IPIs to all application cores to request add_cores callback*/
761 for (i=0; i<CVMX_MAX_CORES; i++) {
762 if (ai->coremask & (1ull<<i))
763 cvmx_write_csr(CVMX_CIU_MBOX_SETX(i), 4);
764 }
765 return 0;
766 }
767
768 /**
769 * This routine sends a request to a running target application
770 * to unplug a specified set cores
771 * @param index is the index of the target application
772 * @param coremask Coremask of the cores to be unplugged from the app.
773 * @param wait 1 - Wait for shutdown completion
774 * 0 - Do not wait
775 * @return 0 on success, -1 on error
776 *
777 */
cvmx_app_hotplug_unplug_cores(int index,uint32_t coremask,int wait)778 int cvmx_app_hotplug_unplug_cores(int index, uint32_t coremask, int wait)
779 {
780 cvmx_app_hotplug_info_t *ai;
781 int i;
782
783 if (!(ai = cvmx_app_hotplug_get_info_at_index(index)))
784 {
785 printf("\nERROR: Failed to get hotplug info for app at index=%d\n", index);
786 return -1;
787 }
788 ai->unplug_cores = coremask;
789 #if 0
790 if (!ai->shutdown_callback)
791 {
792 printf("\nERROR: Target application has not registered for hotplug!\n");
793 return -1;
794 }
795 #endif
796 if ( (ai->coremask | coremask ) != ai->coremask)
797 {
798 printf("\nERROR: Not all cores requested are a part of the app "
799 "r=%08x:%08x\n", (unsigned int)coremask, (unsigned int)ai->coremask);
800 return -1;
801 }
802 if (ai->coremask == coremask)
803 {
804 printf("\nERROR: Trying to remove all cores in app. "
805 "r=%08x:%08x\n", (unsigned int)coremask, (unsigned int)ai->coremask);
806 return -1;
807 }
808 /* Send IPIs to all application cores to request unplug/remove_cores
809 callback */
810 for (i=0; i<CVMX_MAX_CORES; i++) {
811 if (ai->coremask & (1ull<<i))
812 cvmx_write_csr(CVMX_CIU_MBOX_SETX(i), 2);
813 }
814
815 #if 0
816 if (wait)
817 {
818 while (!ai->shutdown_done);
819
820 /* Clean up the hotplug info region for this application */
821 bzero(ai, sizeof(*ai));
822 }
823 #endif
824 return 0;
825 }
826
827 /**
828 * Returns 1 if any app is currently being currently booted , hotplugged or
829 * shutdown. Only one app can be under a boot, hotplug or shutdown condition.
830 * Before booting an app this methods should be used to check whether boot or
831 * shutdown activity is in progress and proceed with the boot or shutdown only
832 * when there is no other activity.
833 *
834 */
is_app_under_boot_or_shutdown(void)835 int is_app_under_boot_or_shutdown(void)
836 {
837 int ret=0;
838 cvmx_app_hotplug_global_t *hgp;
839
840 hgp = cvmx_app_get_hotplug_global_ptr();
841 cvmx_spinlock_lock(&hgp->hotplug_global_lock);
842 if (hgp->app_under_boot || hgp->app_under_shutdown) ret=1;
843 cvmx_spinlock_unlock(&hgp->hotplug_global_lock);
844 return ret;
845
846 }
847
848 /**
849 * Sets or clear the app_under_boot value. This when set signifies that an app
850 * is being currently booted or hotplugged with a new core.
851 *
852 *
853 * @param val sets the app_under_boot to the specified value. This should be
854 * set to 1 while app any is being booted and cleared after the
855 * application has booted up.
856 *
857 */
set_app_unber_boot(int val)858 void set_app_unber_boot(int val)
859 {
860 cvmx_app_hotplug_global_t *hgp;
861
862 hgp = cvmx_app_get_hotplug_global_ptr();
863 cvmx_spinlock_lock(&hgp->hotplug_global_lock);
864 hgp->app_under_boot = val;
865 cvmx_spinlock_unlock(&hgp->hotplug_global_lock);
866 }
867
868 /**
869 * Sets or clear the app_under_shutdown value. This when set signifies that an
870 * app is being currently shutdown or some cores of an app are being shutdown.
871 *
872 * @param val sets the app_under_shutdown to the specified value. This
873 * should be set to 1 while any app is being shutdown and cleared
874 * after the shutdown of the app is complete.
875 *
876 */
set_app_under_shutdown(int val)877 void set_app_under_shutdown(int val)
878 {
879 cvmx_app_hotplug_global_t *hgp;
880
881 hgp = cvmx_app_get_hotplug_global_ptr();
882 cvmx_spinlock_lock(&hgp->hotplug_global_lock);
883 hgp->app_under_shutdown = val;
884 cvmx_spinlock_unlock(&hgp->hotplug_global_lock);
885 }
886