@@ -293,6 +293,14 @@ static int device_early_init(struct hl_device *hdev)
293293 goto free_eq_wq ;
294294 }
295295
296+ hdev -> idle_busy_ts_arr = kmalloc_array (HL_IDLE_BUSY_TS_ARR_SIZE ,
297+ sizeof (struct hl_device_idle_busy_ts ),
298+ (GFP_KERNEL | __GFP_ZERO ));
299+ if (!hdev -> idle_busy_ts_arr ) {
300+ rc = - ENOMEM ;
301+ goto free_chip_info ;
302+ }
303+
296304 hl_cb_mgr_init (& hdev -> kernel_cb_mgr );
297305
298306 mutex_init (& hdev -> send_cpu_message_lock );
@@ -303,10 +311,11 @@ static int device_early_init(struct hl_device *hdev)
303311 INIT_LIST_HEAD (& hdev -> fpriv_list );
304312 mutex_init (& hdev -> fpriv_list_lock );
305313 atomic_set (& hdev -> in_reset , 0 );
306- atomic_set (& hdev -> cs_active_cnt , 0 );
307314
308315 return 0 ;
309316
317+ free_chip_info :
318+ kfree (hdev -> hl_chip_info );
310319free_eq_wq :
311320 destroy_workqueue (hdev -> eq_wq );
312321free_cq_wq :
@@ -336,6 +345,7 @@ static void device_early_fini(struct hl_device *hdev)
336345
337346 hl_cb_mgr_fini (hdev , & hdev -> kernel_cb_mgr );
338347
348+ kfree (hdev -> idle_busy_ts_arr );
339349 kfree (hdev -> hl_chip_info );
340350
341351 destroy_workqueue (hdev -> eq_wq );
@@ -451,6 +461,102 @@ static void device_late_fini(struct hl_device *hdev)
451461 hdev -> late_init_done = false;
452462}
453463
464+ uint32_t hl_device_utilization (struct hl_device * hdev , uint32_t period_ms )
465+ {
466+ struct hl_device_idle_busy_ts * ts ;
467+ ktime_t zero_ktime , curr = ktime_get ();
468+ u32 overlap_cnt = 0 , last_index = hdev -> idle_busy_ts_idx ;
469+ s64 period_us , last_start_us , last_end_us , last_busy_time_us ,
470+ total_busy_time_us = 0 , total_busy_time_ms ;
471+
472+ zero_ktime = ktime_set (0 , 0 );
473+ period_us = period_ms * USEC_PER_MSEC ;
474+ ts = & hdev -> idle_busy_ts_arr [last_index ];
475+
476+ /* check case that device is currently in idle */
477+ if (!ktime_compare (ts -> busy_to_idle_ts , zero_ktime ) &&
478+ !ktime_compare (ts -> idle_to_busy_ts , zero_ktime )) {
479+
480+ last_index -- ;
481+ /* Handle case idle_busy_ts_idx was 0 */
482+ if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE )
483+ last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1 ;
484+
485+ ts = & hdev -> idle_busy_ts_arr [last_index ];
486+ }
487+
488+ while (overlap_cnt < HL_IDLE_BUSY_TS_ARR_SIZE ) {
489+ /* Check if we are in last sample case. i.e. if the sample
490+ * begun before the sampling period. This could be a real
491+ * sample or 0 so need to handle both cases
492+ */
493+ last_start_us = ktime_to_us (
494+ ktime_sub (curr , ts -> idle_to_busy_ts ));
495+
496+ if (last_start_us > period_us ) {
497+
498+ /* First check two cases:
499+ * 1. If the device is currently busy
500+ * 2. If the device was idle during the whole sampling
501+ * period
502+ */
503+
504+ if (!ktime_compare (ts -> busy_to_idle_ts , zero_ktime )) {
505+ /* Check if the device is currently busy */
506+ if (ktime_compare (ts -> idle_to_busy_ts ,
507+ zero_ktime ))
508+ return 100 ;
509+
510+ /* We either didn't have any activity or we
511+ * reached an entry which is 0. Either way,
512+ * exit and return what was accumulated so far
513+ */
514+ break ;
515+ }
516+
517+ /* If sample has finished, check it is relevant */
518+ last_end_us = ktime_to_us (
519+ ktime_sub (curr , ts -> busy_to_idle_ts ));
520+
521+ if (last_end_us > period_us )
522+ break ;
523+
524+ /* It is relevant so add it but with adjustment */
525+ last_busy_time_us = ktime_to_us (
526+ ktime_sub (ts -> busy_to_idle_ts ,
527+ ts -> idle_to_busy_ts ));
528+ total_busy_time_us += last_busy_time_us -
529+ (last_start_us - period_us );
530+ break ;
531+ }
532+
533+ /* Check if the sample is finished or still open */
534+ if (ktime_compare (ts -> busy_to_idle_ts , zero_ktime ))
535+ last_busy_time_us = ktime_to_us (
536+ ktime_sub (ts -> busy_to_idle_ts ,
537+ ts -> idle_to_busy_ts ));
538+ else
539+ last_busy_time_us = ktime_to_us (
540+ ktime_sub (curr , ts -> idle_to_busy_ts ));
541+
542+ total_busy_time_us += last_busy_time_us ;
543+
544+ last_index -- ;
545+ /* Handle case idle_busy_ts_idx was 0 */
546+ if (last_index > HL_IDLE_BUSY_TS_ARR_SIZE )
547+ last_index = HL_IDLE_BUSY_TS_ARR_SIZE - 1 ;
548+
549+ ts = & hdev -> idle_busy_ts_arr [last_index ];
550+
551+ overlap_cnt ++ ;
552+ }
553+
554+ total_busy_time_ms = DIV_ROUND_UP_ULL (total_busy_time_us ,
555+ USEC_PER_MSEC );
556+
557+ return DIV_ROUND_UP_ULL (total_busy_time_ms * 100 , period_ms );
558+ }
559+
454560/*
455561 * hl_device_set_frequency - set the frequency of the device
456562 *
@@ -808,6 +914,14 @@ int hl_device_reset(struct hl_device *hdev, bool hard_reset,
808914 for (i = 0 ; i < hdev -> asic_prop .completion_queues_count ; i ++ )
809915 hl_cq_reset (hdev , & hdev -> completion_queue [i ]);
810916
917+ hdev -> idle_busy_ts_idx = 0 ;
918+ hdev -> idle_busy_ts_arr [0 ].busy_to_idle_ts = ktime_set (0 , 0 );
919+ hdev -> idle_busy_ts_arr [0 ].idle_to_busy_ts = ktime_set (0 , 0 );
920+
921+ if (hdev -> cs_active_cnt )
922+ dev_crit (hdev -> dev , "CS active cnt %d is not 0 during reset\n" ,
923+ hdev -> cs_active_cnt );
924+
811925 mutex_lock (& hdev -> fpriv_list_lock );
812926
813927 /* Make sure the context switch phase will run again */
0 commit comments