2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/clocksource.h>
34 #include <linux/highmem.h>
35 #include <linux/ptp_clock_kernel.h>
36 #include <rdma/mlx5-abi.h>
42 MLX5_CYCLES_SHIFT = 23
46 MLX5_PIN_MODE_IN = 0x0,
47 MLX5_PIN_MODE_OUT = 0x1,
51 MLX5_OUT_PATTERN_PULSE = 0x0,
52 MLX5_OUT_PATTERN_PERIODIC = 0x1,
56 MLX5_EVENT_MODE_DISABLE = 0x0,
57 MLX5_EVENT_MODE_REPETETIVE = 0x1,
58 MLX5_EVENT_MODE_ONCE_TILL_ARM = 0x2,
62 MLX5_MTPPS_FS_ENABLE = BIT(0x0),
63 MLX5_MTPPS_FS_PATTERN = BIT(0x2),
64 MLX5_MTPPS_FS_PIN_MODE = BIT(0x3),
65 MLX5_MTPPS_FS_TIME_STAMP = BIT(0x4),
66 MLX5_MTPPS_FS_OUT_PULSE_DURATION = BIT(0x5),
67 MLX5_MTPPS_FS_ENH_OUT_PER_ADJ = BIT(0x7),
70 static bool mlx5_real_time_mode(struct mlx5_core_dev *mdev)
72 return (mlx5_is_real_time_rq(mdev) || mlx5_is_real_time_sq(mdev));
75 static bool mlx5_modify_mtutc_allowed(struct mlx5_core_dev *mdev)
77 return MLX5_CAP_MCAM_FEATURE(mdev, ptpcyc2realtime_modify);
80 static int mlx5_set_mtutc(struct mlx5_core_dev *dev, u32 *mtutc, u32 size)
82 u32 out[MLX5_ST_SZ_DW(mtutc_reg)] = {};
84 if (!MLX5_CAP_MCAM_REG(dev, mtutc))
87 return mlx5_core_access_reg(dev, mtutc, size, out, sizeof(out),
88 MLX5_REG_MTUTC, 0, 1);
91 static u64 mlx5_read_time(struct mlx5_core_dev *dev,
92 struct ptp_system_timestamp *sts,
95 u32 timer_h, timer_h1, timer_l;
97 timer_h = ioread32be(real_time ? &dev->iseg->real_time_h :
98 &dev->iseg->internal_timer_h);
99 ptp_read_system_prets(sts);
100 timer_l = ioread32be(real_time ? &dev->iseg->real_time_l :
101 &dev->iseg->internal_timer_l);
102 ptp_read_system_postts(sts);
103 timer_h1 = ioread32be(real_time ? &dev->iseg->real_time_h :
104 &dev->iseg->internal_timer_h);
105 if (timer_h != timer_h1) {
107 ptp_read_system_prets(sts);
108 timer_l = ioread32be(real_time ? &dev->iseg->real_time_l :
109 &dev->iseg->internal_timer_l);
110 ptp_read_system_postts(sts);
113 return real_time ? REAL_TIME_TO_NS(timer_h1, timer_l) :
114 (u64)timer_l | (u64)timer_h1 << 32;
117 static u64 read_internal_timer(const struct cyclecounter *cc)
119 struct mlx5_timer *timer = container_of(cc, struct mlx5_timer, cycles);
120 struct mlx5_clock *clock = container_of(timer, struct mlx5_clock, timer);
121 struct mlx5_core_dev *mdev = container_of(clock, struct mlx5_core_dev,
124 return mlx5_read_time(mdev, NULL, false) & cc->mask;
127 static void mlx5_update_clock_info_page(struct mlx5_core_dev *mdev)
129 struct mlx5_ib_clock_info *clock_info = mdev->clock_info;
130 struct mlx5_clock *clock = &mdev->clock;
131 struct mlx5_timer *timer;
137 sign = smp_load_acquire(&clock_info->sign);
138 smp_store_mb(clock_info->sign,
139 sign | MLX5_IB_CLOCK_INFO_KERNEL_UPDATING);
141 timer = &clock->timer;
142 clock_info->cycles = timer->tc.cycle_last;
143 clock_info->mult = timer->cycles.mult;
144 clock_info->nsec = timer->tc.nsec;
145 clock_info->frac = timer->tc.frac;
147 smp_store_release(&clock_info->sign,
148 sign + MLX5_IB_CLOCK_INFO_KERNEL_UPDATING * 2);
151 static void mlx5_pps_out(struct work_struct *work)
153 struct mlx5_pps *pps_info = container_of(work, struct mlx5_pps,
155 struct mlx5_clock *clock = container_of(pps_info, struct mlx5_clock,
157 struct mlx5_core_dev *mdev = container_of(clock, struct mlx5_core_dev,
159 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
163 for (i = 0; i < clock->ptp_info.n_pins; i++) {
166 write_seqlock_irqsave(&clock->lock, flags);
167 tstart = clock->pps_info.start[i];
168 clock->pps_info.start[i] = 0;
169 write_sequnlock_irqrestore(&clock->lock, flags);
173 MLX5_SET(mtpps_reg, in, pin, i);
174 MLX5_SET64(mtpps_reg, in, time_stamp, tstart);
175 MLX5_SET(mtpps_reg, in, field_select, MLX5_MTPPS_FS_TIME_STAMP);
176 mlx5_set_mtpps(mdev, in, sizeof(in));
180 static void mlx5_timestamp_overflow(struct work_struct *work)
182 struct delayed_work *dwork = to_delayed_work(work);
183 struct mlx5_core_dev *mdev;
184 struct mlx5_timer *timer;
185 struct mlx5_clock *clock;
188 timer = container_of(dwork, struct mlx5_timer, overflow_work);
189 clock = container_of(timer, struct mlx5_clock, timer);
190 mdev = container_of(clock, struct mlx5_core_dev, clock);
192 write_seqlock_irqsave(&clock->lock, flags);
193 timecounter_read(&timer->tc);
194 mlx5_update_clock_info_page(mdev);
195 write_sequnlock_irqrestore(&clock->lock, flags);
196 schedule_delayed_work(&timer->overflow_work, timer->overflow_period);
199 static int mlx5_ptp_settime_real_time(struct mlx5_core_dev *mdev,
200 const struct timespec64 *ts)
202 u32 in[MLX5_ST_SZ_DW(mtutc_reg)] = {};
204 if (!mlx5_modify_mtutc_allowed(mdev))
207 if (ts->tv_sec < 0 || ts->tv_sec > U32_MAX ||
208 ts->tv_nsec < 0 || ts->tv_nsec > NSEC_PER_SEC)
211 MLX5_SET(mtutc_reg, in, operation, MLX5_MTUTC_OPERATION_SET_TIME_IMMEDIATE);
212 MLX5_SET(mtutc_reg, in, utc_sec, ts->tv_sec);
213 MLX5_SET(mtutc_reg, in, utc_nsec, ts->tv_nsec);
215 return mlx5_set_mtutc(mdev, in, sizeof(in));
218 static int mlx5_ptp_settime(struct ptp_clock_info *ptp, const struct timespec64 *ts)
220 struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock, ptp_info);
221 struct mlx5_timer *timer = &clock->timer;
222 struct mlx5_core_dev *mdev;
226 mdev = container_of(clock, struct mlx5_core_dev, clock);
227 err = mlx5_ptp_settime_real_time(mdev, ts);
231 write_seqlock_irqsave(&clock->lock, flags);
232 timecounter_init(&timer->tc, &timer->cycles, timespec64_to_ns(ts));
233 mlx5_update_clock_info_page(mdev);
234 write_sequnlock_irqrestore(&clock->lock, flags);
240 struct timespec64 mlx5_ptp_gettimex_real_time(struct mlx5_core_dev *mdev,
241 struct ptp_system_timestamp *sts)
243 struct timespec64 ts;
246 time = mlx5_read_time(mdev, sts, true);
247 ts = ns_to_timespec64(time);
251 static int mlx5_ptp_gettimex(struct ptp_clock_info *ptp, struct timespec64 *ts,
252 struct ptp_system_timestamp *sts)
254 struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock, ptp_info);
255 struct mlx5_timer *timer = &clock->timer;
256 struct mlx5_core_dev *mdev;
260 mdev = container_of(clock, struct mlx5_core_dev, clock);
261 if (mlx5_real_time_mode(mdev)) {
262 *ts = mlx5_ptp_gettimex_real_time(mdev, sts);
266 write_seqlock_irqsave(&clock->lock, flags);
267 cycles = mlx5_read_time(mdev, sts, false);
268 ns = timecounter_cyc2time(&timer->tc, cycles);
269 write_sequnlock_irqrestore(&clock->lock, flags);
270 *ts = ns_to_timespec64(ns);
275 static int mlx5_ptp_adjtime_real_time(struct mlx5_core_dev *mdev, s64 delta)
277 u32 in[MLX5_ST_SZ_DW(mtutc_reg)] = {};
279 if (!mlx5_modify_mtutc_allowed(mdev))
282 /* HW time adjustment range is s16. If out of range, settime instead */
283 if (delta < S16_MIN || delta > S16_MAX) {
284 struct timespec64 ts;
287 ts = mlx5_ptp_gettimex_real_time(mdev, NULL);
288 ns = timespec64_to_ns(&ts) + delta;
289 ts = ns_to_timespec64(ns);
290 return mlx5_ptp_settime_real_time(mdev, &ts);
293 MLX5_SET(mtutc_reg, in, operation, MLX5_MTUTC_OPERATION_ADJUST_TIME);
294 MLX5_SET(mtutc_reg, in, time_adjustment, delta);
296 return mlx5_set_mtutc(mdev, in, sizeof(in));
299 static int mlx5_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
301 struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock, ptp_info);
302 struct mlx5_timer *timer = &clock->timer;
303 struct mlx5_core_dev *mdev;
307 mdev = container_of(clock, struct mlx5_core_dev, clock);
309 err = mlx5_ptp_adjtime_real_time(mdev, delta);
312 write_seqlock_irqsave(&clock->lock, flags);
313 timecounter_adjtime(&timer->tc, delta);
314 mlx5_update_clock_info_page(mdev);
315 write_sequnlock_irqrestore(&clock->lock, flags);
320 static int mlx5_ptp_adjfreq_real_time(struct mlx5_core_dev *mdev, s32 freq)
322 u32 in[MLX5_ST_SZ_DW(mtutc_reg)] = {};
324 if (!mlx5_modify_mtutc_allowed(mdev))
327 MLX5_SET(mtutc_reg, in, operation, MLX5_MTUTC_OPERATION_ADJUST_FREQ_UTC);
328 MLX5_SET(mtutc_reg, in, freq_adjustment, freq);
330 return mlx5_set_mtutc(mdev, in, sizeof(in));
333 static int mlx5_ptp_adjfreq(struct ptp_clock_info *ptp, s32 delta)
335 struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock, ptp_info);
336 struct mlx5_timer *timer = &clock->timer;
337 struct mlx5_core_dev *mdev;
344 mdev = container_of(clock, struct mlx5_core_dev, clock);
345 err = mlx5_ptp_adjfreq_real_time(mdev, delta);
354 adj = timer->nominal_c_mult;
356 diff = div_u64(adj, 1000000000ULL);
358 write_seqlock_irqsave(&clock->lock, flags);
359 timecounter_read(&timer->tc);
360 timer->cycles.mult = neg_adj ? timer->nominal_c_mult - diff :
361 timer->nominal_c_mult + diff;
362 mlx5_update_clock_info_page(mdev);
363 write_sequnlock_irqrestore(&clock->lock, flags);
368 static int mlx5_extts_configure(struct ptp_clock_info *ptp,
369 struct ptp_clock_request *rq,
372 struct mlx5_clock *clock =
373 container_of(ptp, struct mlx5_clock, ptp_info);
374 struct mlx5_core_dev *mdev =
375 container_of(clock, struct mlx5_core_dev, clock);
376 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
377 u32 field_select = 0;
383 if (!MLX5_PPS_CAP(mdev))
386 /* Reject requests with unsupported flags */
387 if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
393 /* Reject requests to enable time stamping on both edges. */
394 if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
395 (rq->extts.flags & PTP_ENABLE_FEATURE) &&
396 (rq->extts.flags & PTP_EXTTS_EDGES) == PTP_EXTTS_EDGES)
399 if (rq->extts.index >= clock->ptp_info.n_pins)
402 pin = ptp_find_pin(clock->ptp, PTP_PF_EXTTS, rq->extts.index);
407 pin_mode = MLX5_PIN_MODE_IN;
408 pattern = !!(rq->extts.flags & PTP_FALLING_EDGE);
409 field_select = MLX5_MTPPS_FS_PIN_MODE |
410 MLX5_MTPPS_FS_PATTERN |
411 MLX5_MTPPS_FS_ENABLE;
413 field_select = MLX5_MTPPS_FS_ENABLE;
416 MLX5_SET(mtpps_reg, in, pin, pin);
417 MLX5_SET(mtpps_reg, in, pin_mode, pin_mode);
418 MLX5_SET(mtpps_reg, in, pattern, pattern);
419 MLX5_SET(mtpps_reg, in, enable, on);
420 MLX5_SET(mtpps_reg, in, field_select, field_select);
422 err = mlx5_set_mtpps(mdev, in, sizeof(in));
426 return mlx5_set_mtppse(mdev, pin, 0,
427 MLX5_EVENT_MODE_REPETETIVE & on);
430 static u64 find_target_cycles(struct mlx5_core_dev *mdev, s64 target_ns)
432 struct mlx5_clock *clock = &mdev->clock;
433 u64 cycles_now, cycles_delta;
434 u64 nsec_now, nsec_delta;
435 struct mlx5_timer *timer;
438 timer = &clock->timer;
440 cycles_now = mlx5_read_time(mdev, NULL, false);
441 write_seqlock_irqsave(&clock->lock, flags);
442 nsec_now = timecounter_cyc2time(&timer->tc, cycles_now);
443 nsec_delta = target_ns - nsec_now;
444 cycles_delta = div64_u64(nsec_delta << timer->cycles.shift,
446 write_sequnlock_irqrestore(&clock->lock, flags);
448 return cycles_now + cycles_delta;
451 static u64 perout_conf_internal_timer(struct mlx5_core_dev *mdev, s64 sec)
453 struct timespec64 ts = {};
457 target_ns = timespec64_to_ns(&ts);
459 return find_target_cycles(mdev, target_ns);
462 static u64 perout_conf_real_time(s64 sec)
464 return (u64)sec << 32;
467 static int mlx5_perout_configure(struct ptp_clock_info *ptp,
468 struct ptp_clock_request *rq,
471 struct mlx5_clock *clock =
472 container_of(ptp, struct mlx5_clock, ptp_info);
473 struct mlx5_core_dev *mdev =
474 container_of(clock, struct mlx5_core_dev, clock);
475 bool rt_mode = mlx5_real_time_mode(mdev);
476 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
477 struct timespec64 ts;
478 u32 field_select = 0;
486 if (!MLX5_PPS_CAP(mdev))
489 /* Reject requests with unsupported flags */
490 if (rq->perout.flags)
493 if (rq->perout.index >= clock->ptp_info.n_pins)
496 field_select = MLX5_MTPPS_FS_ENABLE;
497 pin = ptp_find_pin(clock->ptp, PTP_PF_PEROUT, rq->perout.index);
502 bool rt_mode = mlx5_real_time_mode(mdev);
503 s64 sec = rq->perout.start.sec;
505 if (rq->perout.start.nsec)
508 pin_mode = MLX5_PIN_MODE_OUT;
509 pattern = MLX5_OUT_PATTERN_PERIODIC;
510 ts.tv_sec = rq->perout.period.sec;
511 ts.tv_nsec = rq->perout.period.nsec;
512 ns = timespec64_to_ns(&ts);
514 if ((ns >> 1) != 500000000LL)
517 if (rt_mode && sec > U32_MAX)
520 time_stamp = rt_mode ? perout_conf_real_time(sec) :
521 perout_conf_internal_timer(mdev, sec);
523 field_select |= MLX5_MTPPS_FS_PIN_MODE |
524 MLX5_MTPPS_FS_PATTERN |
525 MLX5_MTPPS_FS_TIME_STAMP;
528 MLX5_SET(mtpps_reg, in, pin, pin);
529 MLX5_SET(mtpps_reg, in, pin_mode, pin_mode);
530 MLX5_SET(mtpps_reg, in, pattern, pattern);
531 MLX5_SET(mtpps_reg, in, enable, on);
532 MLX5_SET64(mtpps_reg, in, time_stamp, time_stamp);
533 MLX5_SET(mtpps_reg, in, field_select, field_select);
535 err = mlx5_set_mtpps(mdev, in, sizeof(in));
542 return mlx5_set_mtppse(mdev, pin, 0,
543 MLX5_EVENT_MODE_REPETETIVE & on);
546 static int mlx5_pps_configure(struct ptp_clock_info *ptp,
547 struct ptp_clock_request *rq,
550 struct mlx5_clock *clock =
551 container_of(ptp, struct mlx5_clock, ptp_info);
553 clock->pps_info.enabled = !!on;
557 static int mlx5_ptp_enable(struct ptp_clock_info *ptp,
558 struct ptp_clock_request *rq,
562 case PTP_CLK_REQ_EXTTS:
563 return mlx5_extts_configure(ptp, rq, on);
564 case PTP_CLK_REQ_PEROUT:
565 return mlx5_perout_configure(ptp, rq, on);
566 case PTP_CLK_REQ_PPS:
567 return mlx5_pps_configure(ptp, rq, on);
575 MLX5_MTPPS_REG_CAP_PIN_X_MODE_SUPPORT_PPS_IN = BIT(0),
576 MLX5_MTPPS_REG_CAP_PIN_X_MODE_SUPPORT_PPS_OUT = BIT(1),
579 static int mlx5_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
580 enum ptp_pin_function func, unsigned int chan)
582 struct mlx5_clock *clock = container_of(ptp, struct mlx5_clock,
589 return !(clock->pps_info.pin_caps[pin] &
590 MLX5_MTPPS_REG_CAP_PIN_X_MODE_SUPPORT_PPS_IN);
592 return !(clock->pps_info.pin_caps[pin] &
593 MLX5_MTPPS_REG_CAP_PIN_X_MODE_SUPPORT_PPS_OUT);
599 static const struct ptp_clock_info mlx5_ptp_clock_info = {
600 .owner = THIS_MODULE,
602 .max_adj = 100000000,
608 .adjfreq = mlx5_ptp_adjfreq,
609 .adjtime = mlx5_ptp_adjtime,
610 .gettimex64 = mlx5_ptp_gettimex,
611 .settime64 = mlx5_ptp_settime,
616 static int mlx5_query_mtpps_pin_mode(struct mlx5_core_dev *mdev, u8 pin,
617 u32 *mtpps, u32 mtpps_size)
619 u32 in[MLX5_ST_SZ_DW(mtpps_reg)] = {};
621 MLX5_SET(mtpps_reg, in, pin, pin);
623 return mlx5_core_access_reg(mdev, in, sizeof(in), mtpps,
624 mtpps_size, MLX5_REG_MTPPS, 0, 0);
627 static int mlx5_get_pps_pin_mode(struct mlx5_clock *clock, u8 pin)
629 struct mlx5_core_dev *mdev = container_of(clock, struct mlx5_core_dev, clock);
631 u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {};
635 err = mlx5_query_mtpps_pin_mode(mdev, pin, out, sizeof(out));
636 if (err || !MLX5_GET(mtpps_reg, out, enable))
639 mode = MLX5_GET(mtpps_reg, out, pin_mode);
641 if (mode == MLX5_PIN_MODE_IN)
643 else if (mode == MLX5_PIN_MODE_OUT)
644 return PTP_PF_PEROUT;
649 static void mlx5_init_pin_config(struct mlx5_clock *clock)
653 if (!clock->ptp_info.n_pins)
656 clock->ptp_info.pin_config =
657 kcalloc(clock->ptp_info.n_pins,
658 sizeof(*clock->ptp_info.pin_config),
660 if (!clock->ptp_info.pin_config)
662 clock->ptp_info.enable = mlx5_ptp_enable;
663 clock->ptp_info.verify = mlx5_ptp_verify;
664 clock->ptp_info.pps = 1;
666 for (i = 0; i < clock->ptp_info.n_pins; i++) {
667 snprintf(clock->ptp_info.pin_config[i].name,
668 sizeof(clock->ptp_info.pin_config[i].name),
670 clock->ptp_info.pin_config[i].index = i;
671 clock->ptp_info.pin_config[i].func = mlx5_get_pps_pin_mode(clock, i);
672 clock->ptp_info.pin_config[i].chan = 0;
676 static void mlx5_get_pps_caps(struct mlx5_core_dev *mdev)
678 struct mlx5_clock *clock = &mdev->clock;
679 u32 out[MLX5_ST_SZ_DW(mtpps_reg)] = {0};
681 mlx5_query_mtpps(mdev, out, sizeof(out));
683 clock->ptp_info.n_pins = MLX5_GET(mtpps_reg, out,
684 cap_number_of_pps_pins);
685 clock->ptp_info.n_ext_ts = MLX5_GET(mtpps_reg, out,
686 cap_max_num_of_pps_in_pins);
687 clock->ptp_info.n_per_out = MLX5_GET(mtpps_reg, out,
688 cap_max_num_of_pps_out_pins);
690 clock->pps_info.pin_caps[0] = MLX5_GET(mtpps_reg, out, cap_pin_0_mode);
691 clock->pps_info.pin_caps[1] = MLX5_GET(mtpps_reg, out, cap_pin_1_mode);
692 clock->pps_info.pin_caps[2] = MLX5_GET(mtpps_reg, out, cap_pin_2_mode);
693 clock->pps_info.pin_caps[3] = MLX5_GET(mtpps_reg, out, cap_pin_3_mode);
694 clock->pps_info.pin_caps[4] = MLX5_GET(mtpps_reg, out, cap_pin_4_mode);
695 clock->pps_info.pin_caps[5] = MLX5_GET(mtpps_reg, out, cap_pin_5_mode);
696 clock->pps_info.pin_caps[6] = MLX5_GET(mtpps_reg, out, cap_pin_6_mode);
697 clock->pps_info.pin_caps[7] = MLX5_GET(mtpps_reg, out, cap_pin_7_mode);
700 static void ts_next_sec(struct timespec64 *ts)
706 static u64 perout_conf_next_event_timer(struct mlx5_core_dev *mdev,
707 struct mlx5_clock *clock)
709 struct timespec64 ts;
712 mlx5_ptp_gettimex(&clock->ptp_info, &ts, NULL);
714 target_ns = timespec64_to_ns(&ts);
716 return find_target_cycles(mdev, target_ns);
719 static int mlx5_pps_event(struct notifier_block *nb,
720 unsigned long type, void *data)
722 struct mlx5_clock *clock = mlx5_nb_cof(nb, struct mlx5_clock, pps_nb);
723 struct ptp_clock_event ptp_event;
724 struct mlx5_eqe *eqe = data;
725 int pin = eqe->data.pps.pin;
726 struct mlx5_core_dev *mdev;
730 mdev = container_of(clock, struct mlx5_core_dev, clock);
732 switch (clock->ptp_info.pin_config[pin].func) {
734 ptp_event.index = pin;
735 ptp_event.timestamp = mlx5_real_time_mode(mdev) ?
736 mlx5_real_time_cyc2time(clock,
737 be64_to_cpu(eqe->data.pps.time_stamp)) :
738 mlx5_timecounter_cyc2time(clock,
739 be64_to_cpu(eqe->data.pps.time_stamp));
740 if (clock->pps_info.enabled) {
741 ptp_event.type = PTP_CLOCK_PPSUSR;
742 ptp_event.pps_times.ts_real =
743 ns_to_timespec64(ptp_event.timestamp);
745 ptp_event.type = PTP_CLOCK_EXTTS;
747 /* TODOL clock->ptp can be NULL if ptp_clock_register fails */
748 ptp_clock_event(clock->ptp, &ptp_event);
751 ns = perout_conf_next_event_timer(mdev, clock);
752 write_seqlock_irqsave(&clock->lock, flags);
753 clock->pps_info.start[pin] = ns;
754 write_sequnlock_irqrestore(&clock->lock, flags);
755 schedule_work(&clock->pps_info.out_work);
758 mlx5_core_err(mdev, " Unhandled clock PPS event, func %d\n",
759 clock->ptp_info.pin_config[pin].func);
765 static void mlx5_timecounter_init(struct mlx5_core_dev *mdev)
767 struct mlx5_clock *clock = &mdev->clock;
768 struct mlx5_timer *timer = &clock->timer;
771 dev_freq = MLX5_CAP_GEN(mdev, device_frequency_khz);
772 timer->cycles.read = read_internal_timer;
773 timer->cycles.shift = MLX5_CYCLES_SHIFT;
774 timer->cycles.mult = clocksource_khz2mult(dev_freq,
775 timer->cycles.shift);
776 timer->nominal_c_mult = timer->cycles.mult;
777 timer->cycles.mask = CLOCKSOURCE_MASK(41);
779 timecounter_init(&timer->tc, &timer->cycles,
780 ktime_to_ns(ktime_get_real()));
783 static void mlx5_init_overflow_period(struct mlx5_clock *clock)
785 struct mlx5_core_dev *mdev = container_of(clock, struct mlx5_core_dev, clock);
786 struct mlx5_ib_clock_info *clock_info = mdev->clock_info;
787 struct mlx5_timer *timer = &clock->timer;
792 /* Calculate period in seconds to call the overflow watchdog - to make
793 * sure counter is checked at least twice every wrap around.
794 * The period is calculated as the minimum between max HW cycles count
795 * (The clock source mask) and max amount of cycles that can be
796 * multiplied by clock multiplier where the result doesn't exceed
799 overflow_cycles = div64_u64(~0ULL >> 1, timer->cycles.mult);
800 overflow_cycles = min(overflow_cycles, div_u64(timer->cycles.mask, 3));
802 ns = cyclecounter_cyc2ns(&timer->cycles, overflow_cycles,
804 do_div(ns, NSEC_PER_SEC / HZ);
805 timer->overflow_period = ns;
807 INIT_DELAYED_WORK(&timer->overflow_work, mlx5_timestamp_overflow);
808 if (timer->overflow_period)
809 schedule_delayed_work(&timer->overflow_work, 0);
812 "invalid overflow period, overflow_work is not scheduled\n");
815 clock_info->overflow_period = timer->overflow_period;
818 static void mlx5_init_clock_info(struct mlx5_core_dev *mdev)
820 struct mlx5_clock *clock = &mdev->clock;
821 struct mlx5_ib_clock_info *info;
822 struct mlx5_timer *timer;
824 mdev->clock_info = (struct mlx5_ib_clock_info *)get_zeroed_page(GFP_KERNEL);
825 if (!mdev->clock_info) {
826 mlx5_core_warn(mdev, "Failed to allocate IB clock info page\n");
830 info = mdev->clock_info;
831 timer = &clock->timer;
833 info->nsec = timer->tc.nsec;
834 info->cycles = timer->tc.cycle_last;
835 info->mask = timer->cycles.mask;
836 info->mult = timer->nominal_c_mult;
837 info->shift = timer->cycles.shift;
838 info->frac = timer->tc.frac;
841 static void mlx5_init_timer_clock(struct mlx5_core_dev *mdev)
843 struct mlx5_clock *clock = &mdev->clock;
845 mlx5_timecounter_init(mdev);
846 mlx5_init_clock_info(mdev);
847 mlx5_init_overflow_period(clock);
848 clock->ptp_info = mlx5_ptp_clock_info;
850 if (mlx5_real_time_mode(mdev)) {
851 struct timespec64 ts;
853 ktime_get_real_ts64(&ts);
854 mlx5_ptp_settime(&clock->ptp_info, &ts);
858 static void mlx5_init_pps(struct mlx5_core_dev *mdev)
860 struct mlx5_clock *clock = &mdev->clock;
862 if (!MLX5_PPS_CAP(mdev))
865 mlx5_get_pps_caps(mdev);
866 mlx5_init_pin_config(clock);
869 void mlx5_init_clock(struct mlx5_core_dev *mdev)
871 struct mlx5_clock *clock = &mdev->clock;
873 if (!MLX5_CAP_GEN(mdev, device_frequency_khz)) {
874 mlx5_core_warn(mdev, "invalid device_frequency_khz, aborting HW clock init\n");
878 seqlock_init(&clock->lock);
879 mlx5_init_timer_clock(mdev);
880 INIT_WORK(&clock->pps_info.out_work, mlx5_pps_out);
882 /* Configure the PHC */
883 clock->ptp_info = mlx5_ptp_clock_info;
885 /* Initialize 1PPS data structures */
888 clock->ptp = ptp_clock_register(&clock->ptp_info,
890 if (IS_ERR(clock->ptp)) {
891 mlx5_core_warn(mdev, "ptp_clock_register failed %ld\n",
892 PTR_ERR(clock->ptp));
896 MLX5_NB_INIT(&clock->pps_nb, mlx5_pps_event, PPS_EVENT);
897 mlx5_eq_notifier_register(mdev, &clock->pps_nb);
900 void mlx5_cleanup_clock(struct mlx5_core_dev *mdev)
902 struct mlx5_clock *clock = &mdev->clock;
904 if (!MLX5_CAP_GEN(mdev, device_frequency_khz))
907 mlx5_eq_notifier_unregister(mdev, &clock->pps_nb);
909 ptp_clock_unregister(clock->ptp);
913 cancel_work_sync(&clock->pps_info.out_work);
914 cancel_delayed_work_sync(&clock->timer.overflow_work);
916 if (mdev->clock_info) {
917 free_page((unsigned long)mdev->clock_info);
918 mdev->clock_info = NULL;
921 kfree(clock->ptp_info.pin_config);