1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PTP 1588 clock support - character device implementation. 4 * 5 * Copyright (C) 2010 OMICRON electronics GmbH 6 */ 7 #include <linux/compat.h> 8 #include <linux/module.h> 9 #include <linux/posix-clock.h> 10 #include <linux/poll.h> 11 #include <linux/sched.h> 12 #include <linux/slab.h> 13 #include <linux/timekeeping.h> 14 #include <linux/debugfs.h> 15 16 #include <linux/nospec.h> 17 18 #include "ptp_private.h" 19 20 static int ptp_disable_pinfunc(struct ptp_clock_info *ops, 21 enum ptp_pin_function func, unsigned int chan) 22 { 23 struct ptp_clock_request rq; 24 int err = 0; 25 26 memset(&rq, 0, sizeof(rq)); 27 28 switch (func) { 29 case PTP_PF_NONE: 30 break; 31 case PTP_PF_EXTTS: 32 rq.type = PTP_CLK_REQ_EXTTS; 33 rq.extts.index = chan; 34 err = ops->enable(ops, &rq, 0); 35 break; 36 case PTP_PF_PEROUT: 37 rq.type = PTP_CLK_REQ_PEROUT; 38 rq.perout.index = chan; 39 err = ops->enable(ops, &rq, 0); 40 break; 41 case PTP_PF_PHYSYNC: 42 break; 43 default: 44 return -EINVAL; 45 } 46 47 return err; 48 } 49 50 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin, 51 enum ptp_pin_function func, unsigned int chan) 52 { 53 struct ptp_clock_info *info = ptp->info; 54 struct ptp_pin_desc *pin1 = NULL, *pin2 = &info->pin_config[pin]; 55 unsigned int i; 56 57 /* Check to see if any other pin previously had this function. */ 58 for (i = 0; i < info->n_pins; i++) { 59 if (info->pin_config[i].func == func && 60 info->pin_config[i].chan == chan) { 61 pin1 = &info->pin_config[i]; 62 break; 63 } 64 } 65 if (pin1 && i == pin) 66 return 0; 67 68 /* Check the desired function and channel. */ 69 switch (func) { 70 case PTP_PF_NONE: 71 break; 72 case PTP_PF_EXTTS: 73 if (chan >= info->n_ext_ts) 74 return -EINVAL; 75 break; 76 case PTP_PF_PEROUT: 77 if (chan >= info->n_per_out) 78 return -EINVAL; 79 break; 80 case PTP_PF_PHYSYNC: 81 if (chan != 0) 82 return -EINVAL; 83 break; 84 default: 85 return -EINVAL; 86 } 87 88 if (info->verify(info, pin, func, chan)) { 89 pr_err("driver cannot use function %u and channel %u on pin %u\n", 90 func, chan, pin); 91 return -EOPNOTSUPP; 92 } 93 94 /* Disable whatever function was previously assigned. */ 95 if (pin1) { 96 ptp_disable_pinfunc(info, func, chan); 97 pin1->func = PTP_PF_NONE; 98 pin1->chan = 0; 99 } 100 ptp_disable_pinfunc(info, pin2->func, pin2->chan); 101 pin2->func = func; 102 pin2->chan = chan; 103 104 return 0; 105 } 106 107 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode) 108 { 109 struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 110 struct timestamp_event_queue *queue; 111 char debugfsname[32]; 112 113 queue = kzalloc(sizeof(*queue), GFP_KERNEL); 114 if (!queue) 115 return -EINVAL; 116 queue->mask = bitmap_alloc(PTP_MAX_CHANNELS, GFP_KERNEL); 117 if (!queue->mask) { 118 kfree(queue); 119 return -EINVAL; 120 } 121 bitmap_set(queue->mask, 0, PTP_MAX_CHANNELS); 122 spin_lock_init(&queue->lock); 123 scoped_guard(spinlock_irq, &ptp->tsevqs_lock) 124 list_add_tail(&queue->qlist, &ptp->tsevqs); 125 pccontext->private_clkdata = queue; 126 127 /* Debugfs contents */ 128 sprintf(debugfsname, "0x%p", queue); 129 queue->debugfs_instance = 130 debugfs_create_dir(debugfsname, ptp->debugfs_root); 131 queue->dfs_bitmap.array = (u32 *)queue->mask; 132 queue->dfs_bitmap.n_elements = 133 DIV_ROUND_UP(PTP_MAX_CHANNELS, BITS_PER_BYTE * sizeof(u32)); 134 debugfs_create_u32_array("mask", 0444, queue->debugfs_instance, 135 &queue->dfs_bitmap); 136 137 return 0; 138 } 139 140 int ptp_release(struct posix_clock_context *pccontext) 141 { 142 struct timestamp_event_queue *queue = pccontext->private_clkdata; 143 struct ptp_clock *ptp = 144 container_of(pccontext->clk, struct ptp_clock, clock); 145 146 debugfs_remove(queue->debugfs_instance); 147 pccontext->private_clkdata = NULL; 148 scoped_guard(spinlock_irq, &ptp->tsevqs_lock) 149 list_del(&queue->qlist); 150 bitmap_free(queue->mask); 151 kfree(queue); 152 return 0; 153 } 154 155 static long ptp_clock_getcaps(struct ptp_clock *ptp, void __user *arg) 156 { 157 struct ptp_clock_caps caps = { 158 .max_adj = ptp->info->max_adj, 159 .n_alarm = ptp->info->n_alarm, 160 .n_ext_ts = ptp->info->n_ext_ts, 161 .n_per_out = ptp->info->n_per_out, 162 .pps = ptp->info->pps, 163 .n_pins = ptp->info->n_pins, 164 .cross_timestamping = ptp->info->getcrosststamp != NULL, 165 .adjust_phase = ptp->info->adjphase != NULL && 166 ptp->info->getmaxphase != NULL, 167 }; 168 169 if (caps.adjust_phase) 170 caps.max_phase_adj = ptp->info->getmaxphase(ptp->info); 171 172 return copy_to_user(arg, &caps, sizeof(caps)) ? -EFAULT : 0; 173 } 174 175 static long ptp_extts_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 176 { 177 struct ptp_clock_request req = { .type = PTP_CLK_REQ_EXTTS }; 178 struct ptp_clock_info *ops = ptp->info; 179 unsigned int supported_extts_flags; 180 181 if (copy_from_user(&req.extts, arg, sizeof(req.extts))) 182 return -EFAULT; 183 184 if (cmd == PTP_EXTTS_REQUEST2) { 185 /* Tell the drivers to check the flags carefully. */ 186 req.extts.flags |= PTP_STRICT_FLAGS; 187 /* Make sure no reserved bit is set. */ 188 if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) || 189 req.extts.rsv[0] || req.extts.rsv[1]) 190 return -EINVAL; 191 192 /* Ensure one of the rising/falling edge bits is set. */ 193 if ((req.extts.flags & PTP_ENABLE_FEATURE) && 194 (req.extts.flags & PTP_EXTTS_EDGES) == 0) 195 return -EINVAL; 196 } else { 197 req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS; 198 memset(req.extts.rsv, 0, sizeof(req.extts.rsv)); 199 } 200 201 if (req.extts.index >= ops->n_ext_ts) 202 return -EINVAL; 203 204 supported_extts_flags = ptp->info->supported_extts_flags; 205 /* The PTP_ENABLE_FEATURE flag is always supported. */ 206 supported_extts_flags |= PTP_ENABLE_FEATURE; 207 /* If the driver does not support strictly checking flags, the 208 * PTP_RISING_EDGE and PTP_FALLING_EDGE flags are merely hints 209 * which are not enforced. 210 */ 211 if (!(supported_extts_flags & PTP_STRICT_FLAGS)) 212 supported_extts_flags |= PTP_EXTTS_EDGES; 213 /* Reject unsupported flags */ 214 if (req.extts.flags & ~supported_extts_flags) 215 return -EOPNOTSUPP; 216 217 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 218 return ops->enable(ops, &req, req.extts.flags & PTP_ENABLE_FEATURE ? 1 : 0); 219 } 220 221 static long ptp_perout_request(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 222 { 223 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PEROUT }; 224 struct ptp_perout_request *perout = &req.perout; 225 struct ptp_clock_info *ops = ptp->info; 226 227 if (copy_from_user(perout, arg, sizeof(*perout))) 228 return -EFAULT; 229 230 if (cmd == PTP_PEROUT_REQUEST2) { 231 if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) 232 return -EINVAL; 233 234 /* 235 * The "on" field has undefined meaning if 236 * PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat it 237 * as reserved, which must be set to zero. 238 */ 239 if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) && 240 !mem_is_zero(perout->rsv, sizeof(perout->rsv))) 241 return -EINVAL; 242 243 if (perout->flags & PTP_PEROUT_DUTY_CYCLE) { 244 /* The duty cycle must be subunitary. */ 245 if (perout->on.sec > perout->period.sec || 246 (perout->on.sec == perout->period.sec && 247 perout->on.nsec > perout->period.nsec)) 248 return -ERANGE; 249 } 250 251 if (perout->flags & PTP_PEROUT_PHASE) { 252 /* 253 * The phase should be specified modulo the period, 254 * therefore anything equal or larger than 1 period 255 * is invalid. 256 */ 257 if (perout->phase.sec > perout->period.sec || 258 (perout->phase.sec == perout->period.sec && 259 perout->phase.nsec >= perout->period.nsec)) 260 return -ERANGE; 261 } 262 } else { 263 perout->flags &= PTP_PEROUT_V1_VALID_FLAGS; 264 memset(perout->rsv, 0, sizeof(perout->rsv)); 265 } 266 267 if (perout->index >= ops->n_per_out) 268 return -EINVAL; 269 if (perout->flags & ~ops->supported_perout_flags) 270 return -EOPNOTSUPP; 271 272 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 273 return ops->enable(ops, &req, perout->period.sec || perout->period.nsec); 274 } 275 276 static long ptp_enable_pps(struct ptp_clock *ptp, bool enable) 277 { 278 struct ptp_clock_request req = { .type = PTP_CLK_REQ_PPS }; 279 struct ptp_clock_info *ops = ptp->info; 280 281 if (!capable(CAP_SYS_TIME)) 282 return -EPERM; 283 284 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 285 return ops->enable(ops, &req, enable); 286 } 287 288 typedef int (*ptp_crosststamp_fn)(struct ptp_clock_info *, 289 struct system_device_crosststamp *); 290 291 static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg, 292 ptp_crosststamp_fn crosststamp_fn) 293 { 294 struct ptp_sys_offset_precise precise_offset; 295 struct system_device_crosststamp xtstamp; 296 struct timespec64 ts; 297 int err; 298 299 if (!crosststamp_fn) 300 return -EOPNOTSUPP; 301 302 err = crosststamp_fn(ptp->info, &xtstamp); 303 if (err) 304 return err; 305 306 memset(&precise_offset, 0, sizeof(precise_offset)); 307 ts = ktime_to_timespec64(xtstamp.device); 308 precise_offset.device.sec = ts.tv_sec; 309 precise_offset.device.nsec = ts.tv_nsec; 310 ts = ktime_to_timespec64(xtstamp.sys_realtime); 311 precise_offset.sys_realtime.sec = ts.tv_sec; 312 precise_offset.sys_realtime.nsec = ts.tv_nsec; 313 ts = ktime_to_timespec64(xtstamp.sys_monoraw); 314 precise_offset.sys_monoraw.sec = ts.tv_sec; 315 precise_offset.sys_monoraw.nsec = ts.tv_nsec; 316 317 return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAULT : 0; 318 } 319 320 typedef int (*ptp_gettimex_fn)(struct ptp_clock_info *, 321 struct timespec64 *, 322 struct ptp_system_timestamp *); 323 324 static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg, 325 ptp_gettimex_fn gettimex_fn) 326 { 327 struct ptp_sys_offset_extended *extoff __free(kfree) = NULL; 328 struct ptp_system_timestamp sts; 329 330 if (!gettimex_fn) 331 return -EOPNOTSUPP; 332 333 extoff = memdup_user(arg, sizeof(*extoff)); 334 if (IS_ERR(extoff)) 335 return PTR_ERR(extoff); 336 337 if (extoff->n_samples > PTP_MAX_SAMPLES || extoff->rsv[0] || extoff->rsv[1]) 338 return -EINVAL; 339 340 switch (extoff->clockid) { 341 case CLOCK_REALTIME: 342 case CLOCK_MONOTONIC: 343 case CLOCK_MONOTONIC_RAW: 344 break; 345 case CLOCK_AUX ... CLOCK_AUX_LAST: 346 if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS)) 347 break; 348 fallthrough; 349 default: 350 return -EINVAL; 351 } 352 353 sts.clockid = extoff->clockid; 354 for (unsigned int i = 0; i < extoff->n_samples; i++) { 355 struct timespec64 ts; 356 int err; 357 358 err = gettimex_fn(ptp->info, &ts, &sts); 359 if (err) 360 return err; 361 362 /* Filter out disabled or unavailable clocks */ 363 if (sts.pre_ts.tv_sec < 0 || sts.post_ts.tv_sec < 0) 364 return -EINVAL; 365 366 extoff->ts[i][0].sec = sts.pre_ts.tv_sec; 367 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; 368 extoff->ts[i][1].sec = ts.tv_sec; 369 extoff->ts[i][1].nsec = ts.tv_nsec; 370 extoff->ts[i][2].sec = sts.post_ts.tv_sec; 371 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; 372 } 373 374 return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0; 375 } 376 377 static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg) 378 { 379 struct ptp_sys_offset *sysoff __free(kfree) = NULL; 380 struct ptp_clock_time *pct; 381 struct timespec64 ts; 382 383 sysoff = memdup_user(arg, sizeof(*sysoff)); 384 if (IS_ERR(sysoff)) 385 return PTR_ERR(sysoff); 386 387 if (sysoff->n_samples > PTP_MAX_SAMPLES) 388 return -EINVAL; 389 390 pct = &sysoff->ts[0]; 391 for (unsigned int i = 0; i < sysoff->n_samples; i++) { 392 struct ptp_clock_info *ops = ptp->info; 393 int err; 394 395 ktime_get_real_ts64(&ts); 396 pct->sec = ts.tv_sec; 397 pct->nsec = ts.tv_nsec; 398 pct++; 399 if (ops->gettimex64) 400 err = ops->gettimex64(ops, &ts, NULL); 401 else 402 err = ops->gettime64(ops, &ts); 403 if (err) 404 return err; 405 pct->sec = ts.tv_sec; 406 pct->nsec = ts.tv_nsec; 407 pct++; 408 } 409 ktime_get_real_ts64(&ts); 410 pct->sec = ts.tv_sec; 411 pct->nsec = ts.tv_nsec; 412 413 return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0; 414 } 415 416 static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 417 { 418 struct ptp_clock_info *ops = ptp->info; 419 struct ptp_pin_desc pd; 420 421 if (copy_from_user(&pd, arg, sizeof(pd))) 422 return -EFAULT; 423 424 if (cmd == PTP_PIN_GETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 425 return -EINVAL; 426 427 if (pd.index >= ops->n_pins) 428 return -EINVAL; 429 430 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 431 pd = ops->pin_config[array_index_nospec(pd.index, ops->n_pins)]; 432 433 return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0; 434 } 435 436 static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 437 { 438 struct ptp_clock_info *ops = ptp->info; 439 struct ptp_pin_desc pd; 440 unsigned int pin_index; 441 442 if (copy_from_user(&pd, arg, sizeof(pd))) 443 return -EFAULT; 444 445 if (cmd == PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 446 return -EINVAL; 447 448 if (pd.index >= ops->n_pins) 449 return -EINVAL; 450 451 pin_index = array_index_nospec(pd.index, ops->n_pins); 452 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 453 return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 454 } 455 456 static long ptp_mask_clear_all(struct timestamp_event_queue *tsevq) 457 { 458 bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); 459 return 0; 460 } 461 462 static long ptp_mask_en_single(struct timestamp_event_queue *tsevq, void __user *arg) 463 { 464 unsigned int channel; 465 466 if (copy_from_user(&channel, arg, sizeof(channel))) 467 return -EFAULT; 468 if (channel >= PTP_MAX_CHANNELS) 469 return -EFAULT; 470 set_bit(channel, tsevq->mask); 471 return 0; 472 } 473 474 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, 475 unsigned long arg) 476 { 477 struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 478 void __user *argptr; 479 480 if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2) 481 arg = (unsigned long)compat_ptr(arg); 482 argptr = (void __force __user *)arg; 483 484 switch (cmd) { 485 case PTP_CLOCK_GETCAPS: 486 case PTP_CLOCK_GETCAPS2: 487 return ptp_clock_getcaps(ptp, argptr); 488 489 case PTP_EXTTS_REQUEST: 490 case PTP_EXTTS_REQUEST2: 491 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 492 return -EACCES; 493 return ptp_extts_request(ptp, cmd, argptr); 494 495 case PTP_PEROUT_REQUEST: 496 case PTP_PEROUT_REQUEST2: 497 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 498 return -EACCES; 499 return ptp_perout_request(ptp, cmd, argptr); 500 501 case PTP_ENABLE_PPS: 502 case PTP_ENABLE_PPS2: 503 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 504 return -EACCES; 505 return ptp_enable_pps(ptp, !!arg); 506 507 case PTP_SYS_OFFSET_PRECISE: 508 case PTP_SYS_OFFSET_PRECISE2: 509 return ptp_sys_offset_precise(ptp, argptr, 510 ptp->info->getcrosststamp); 511 512 case PTP_SYS_OFFSET_EXTENDED: 513 case PTP_SYS_OFFSET_EXTENDED2: 514 return ptp_sys_offset_extended(ptp, argptr, 515 ptp->info->gettimex64); 516 517 case PTP_SYS_OFFSET: 518 case PTP_SYS_OFFSET2: 519 return ptp_sys_offset(ptp, argptr); 520 521 case PTP_PIN_GETFUNC: 522 case PTP_PIN_GETFUNC2: 523 return ptp_pin_getfunc(ptp, cmd, argptr); 524 525 case PTP_PIN_SETFUNC: 526 case PTP_PIN_SETFUNC2: 527 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 528 return -EACCES; 529 return ptp_pin_setfunc(ptp, cmd, argptr); 530 531 case PTP_MASK_CLEAR_ALL: 532 return ptp_mask_clear_all(pccontext->private_clkdata); 533 534 case PTP_MASK_EN_SINGLE: 535 return ptp_mask_en_single(pccontext->private_clkdata, argptr); 536 537 case PTP_SYS_OFFSET_PRECISE_CYCLES: 538 return ptp_sys_offset_precise(ptp, argptr, 539 ptp->info->getcrosscycles); 540 541 case PTP_SYS_OFFSET_EXTENDED_CYCLES: 542 return ptp_sys_offset_extended(ptp, argptr, 543 ptp->info->getcyclesx64); 544 default: 545 return -ENOTTY; 546 } 547 } 548 549 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, 550 poll_table *wait) 551 { 552 struct ptp_clock *ptp = 553 container_of(pccontext->clk, struct ptp_clock, clock); 554 struct timestamp_event_queue *queue; 555 556 queue = pccontext->private_clkdata; 557 if (!queue) 558 return EPOLLERR; 559 560 poll_wait(fp, &ptp->tsev_wq, wait); 561 562 return queue_cnt(queue) ? EPOLLIN : 0; 563 } 564 565 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) 566 567 ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, 568 char __user *buf, size_t cnt) 569 { 570 struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 571 struct timestamp_event_queue *queue; 572 struct ptp_extts_event *event; 573 ssize_t result; 574 575 queue = pccontext->private_clkdata; 576 if (!queue) 577 return -EINVAL; 578 579 if (cnt % sizeof(*event) != 0) 580 return -EINVAL; 581 582 if (cnt > EXTTS_BUFSIZE) 583 cnt = EXTTS_BUFSIZE; 584 585 if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queue))) 586 return -ERESTARTSYS; 587 588 if (ptp->defunct) 589 return -ENODEV; 590 591 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 592 if (!event) 593 return -ENOMEM; 594 595 scoped_guard(spinlock_irq, &queue->lock) { 596 size_t qcnt = min((size_t)queue_cnt(queue), cnt / sizeof(*event)); 597 598 for (size_t i = 0; i < qcnt; i++) { 599 event[i] = queue->buf[queue->head]; 600 /* Paired with READ_ONCE() in queue_cnt() */ 601 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 602 } 603 cnt = qcnt * sizeof(*event); 604 } 605 606 result = cnt; 607 if (copy_to_user(buf, event, cnt)) 608 result = -EFAULT; 609 610 kfree(event); 611 return result; 612 } 613