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 static long ptp_sys_offset_precise(struct ptp_clock *ptp, void __user *arg) 289 { 290 struct ptp_sys_offset_precise precise_offset; 291 struct system_device_crosststamp xtstamp; 292 struct timespec64 ts; 293 int err; 294 295 if (!ptp->info->getcrosststamp) 296 return -EOPNOTSUPP; 297 298 err = ptp->info->getcrosststamp(ptp->info, &xtstamp); 299 if (err) 300 return err; 301 302 memset(&precise_offset, 0, sizeof(precise_offset)); 303 ts = ktime_to_timespec64(xtstamp.device); 304 precise_offset.device.sec = ts.tv_sec; 305 precise_offset.device.nsec = ts.tv_nsec; 306 ts = ktime_to_timespec64(xtstamp.sys_realtime); 307 precise_offset.sys_realtime.sec = ts.tv_sec; 308 precise_offset.sys_realtime.nsec = ts.tv_nsec; 309 ts = ktime_to_timespec64(xtstamp.sys_monoraw); 310 precise_offset.sys_monoraw.sec = ts.tv_sec; 311 precise_offset.sys_monoraw.nsec = ts.tv_nsec; 312 313 return copy_to_user(arg, &precise_offset, sizeof(precise_offset)) ? -EFAULT : 0; 314 } 315 316 static long ptp_sys_offset_extended(struct ptp_clock *ptp, void __user *arg) 317 { 318 struct ptp_sys_offset_extended *extoff __free(kfree) = NULL; 319 struct ptp_system_timestamp sts; 320 321 if (!ptp->info->gettimex64) 322 return -EOPNOTSUPP; 323 324 extoff = memdup_user(arg, sizeof(*extoff)); 325 if (IS_ERR(extoff)) 326 return PTR_ERR(extoff); 327 328 if (extoff->n_samples > PTP_MAX_SAMPLES || 329 extoff->rsv[0] || extoff->rsv[1] || 330 (extoff->clockid != CLOCK_REALTIME && 331 extoff->clockid != CLOCK_MONOTONIC && 332 extoff->clockid != CLOCK_MONOTONIC_RAW)) 333 return -EINVAL; 334 335 sts.clockid = extoff->clockid; 336 for (unsigned int i = 0; i < extoff->n_samples; i++) { 337 struct timespec64 ts; 338 int err; 339 340 err = ptp->info->gettimex64(ptp->info, &ts, &sts); 341 if (err) 342 return err; 343 extoff->ts[i][0].sec = sts.pre_ts.tv_sec; 344 extoff->ts[i][0].nsec = sts.pre_ts.tv_nsec; 345 extoff->ts[i][1].sec = ts.tv_sec; 346 extoff->ts[i][1].nsec = ts.tv_nsec; 347 extoff->ts[i][2].sec = sts.post_ts.tv_sec; 348 extoff->ts[i][2].nsec = sts.post_ts.tv_nsec; 349 } 350 351 return copy_to_user(arg, extoff, sizeof(*extoff)) ? -EFAULT : 0; 352 } 353 354 static long ptp_sys_offset(struct ptp_clock *ptp, void __user *arg) 355 { 356 struct ptp_sys_offset *sysoff __free(kfree) = NULL; 357 struct ptp_clock_time *pct; 358 struct timespec64 ts; 359 360 sysoff = memdup_user(arg, sizeof(*sysoff)); 361 if (IS_ERR(sysoff)) 362 return PTR_ERR(sysoff); 363 364 if (sysoff->n_samples > PTP_MAX_SAMPLES) 365 return -EINVAL; 366 367 pct = &sysoff->ts[0]; 368 for (unsigned int i = 0; i < sysoff->n_samples; i++) { 369 struct ptp_clock_info *ops = ptp->info; 370 int err; 371 372 ktime_get_real_ts64(&ts); 373 pct->sec = ts.tv_sec; 374 pct->nsec = ts.tv_nsec; 375 pct++; 376 if (ops->gettimex64) 377 err = ops->gettimex64(ops, &ts, NULL); 378 else 379 err = ops->gettime64(ops, &ts); 380 if (err) 381 return err; 382 pct->sec = ts.tv_sec; 383 pct->nsec = ts.tv_nsec; 384 pct++; 385 } 386 ktime_get_real_ts64(&ts); 387 pct->sec = ts.tv_sec; 388 pct->nsec = ts.tv_nsec; 389 390 return copy_to_user(arg, sysoff, sizeof(*sysoff)) ? -EFAULT : 0; 391 } 392 393 static long ptp_pin_getfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 394 { 395 struct ptp_clock_info *ops = ptp->info; 396 struct ptp_pin_desc pd; 397 398 if (copy_from_user(&pd, arg, sizeof(pd))) 399 return -EFAULT; 400 401 if (cmd == PTP_PIN_GETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 402 return -EINVAL; 403 404 if (pd.index >= ops->n_pins) 405 return -EINVAL; 406 407 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 408 pd = ops->pin_config[array_index_nospec(pd.index, ops->n_pins)]; 409 410 return copy_to_user(arg, &pd, sizeof(pd)) ? -EFAULT : 0; 411 } 412 413 static long ptp_pin_setfunc(struct ptp_clock *ptp, unsigned int cmd, void __user *arg) 414 { 415 struct ptp_clock_info *ops = ptp->info; 416 struct ptp_pin_desc pd; 417 unsigned int pin_index; 418 419 if (copy_from_user(&pd, arg, sizeof(pd))) 420 return -EFAULT; 421 422 if (cmd == PTP_PIN_SETFUNC2 && !mem_is_zero(pd.rsv, sizeof(pd.rsv))) 423 return -EINVAL; 424 425 if (pd.index >= ops->n_pins) 426 return -EINVAL; 427 428 pin_index = array_index_nospec(pd.index, ops->n_pins); 429 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &ptp->pincfg_mux) 430 return ptp_set_pinfunc(ptp, pin_index, pd.func, pd.chan); 431 } 432 433 static long ptp_mask_clear_all(struct timestamp_event_queue *tsevq) 434 { 435 bitmap_clear(tsevq->mask, 0, PTP_MAX_CHANNELS); 436 return 0; 437 } 438 439 static long ptp_mask_en_single(struct timestamp_event_queue *tsevq, void __user *arg) 440 { 441 unsigned int channel; 442 443 if (copy_from_user(&channel, arg, sizeof(channel))) 444 return -EFAULT; 445 if (channel >= PTP_MAX_CHANNELS) 446 return -EFAULT; 447 set_bit(channel, tsevq->mask); 448 return 0; 449 } 450 451 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, 452 unsigned long arg) 453 { 454 struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 455 void __user *argptr; 456 457 if (in_compat_syscall() && cmd != PTP_ENABLE_PPS && cmd != PTP_ENABLE_PPS2) 458 arg = (unsigned long)compat_ptr(arg); 459 argptr = (void __force __user *)arg; 460 461 switch (cmd) { 462 case PTP_CLOCK_GETCAPS: 463 case PTP_CLOCK_GETCAPS2: 464 return ptp_clock_getcaps(ptp, argptr); 465 466 case PTP_EXTTS_REQUEST: 467 case PTP_EXTTS_REQUEST2: 468 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 469 return -EACCES; 470 return ptp_extts_request(ptp, cmd, argptr); 471 472 case PTP_PEROUT_REQUEST: 473 case PTP_PEROUT_REQUEST2: 474 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 475 return -EACCES; 476 return ptp_perout_request(ptp, cmd, argptr); 477 478 case PTP_ENABLE_PPS: 479 case PTP_ENABLE_PPS2: 480 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 481 return -EACCES; 482 return ptp_enable_pps(ptp, !!arg); 483 484 case PTP_SYS_OFFSET_PRECISE: 485 case PTP_SYS_OFFSET_PRECISE2: 486 return ptp_sys_offset_precise(ptp, argptr); 487 488 case PTP_SYS_OFFSET_EXTENDED: 489 case PTP_SYS_OFFSET_EXTENDED2: 490 return ptp_sys_offset_extended(ptp, argptr); 491 492 case PTP_SYS_OFFSET: 493 case PTP_SYS_OFFSET2: 494 return ptp_sys_offset(ptp, argptr); 495 496 case PTP_PIN_GETFUNC: 497 case PTP_PIN_GETFUNC2: 498 return ptp_pin_getfunc(ptp, cmd, argptr); 499 500 case PTP_PIN_SETFUNC: 501 case PTP_PIN_SETFUNC2: 502 if ((pccontext->fp->f_mode & FMODE_WRITE) == 0) 503 return -EACCES; 504 return ptp_pin_setfunc(ptp, cmd, argptr); 505 506 case PTP_MASK_CLEAR_ALL: 507 return ptp_mask_clear_all(pccontext->private_clkdata); 508 509 case PTP_MASK_EN_SINGLE: 510 return ptp_mask_en_single(pccontext->private_clkdata, argptr); 511 512 default: 513 return -ENOTTY; 514 } 515 } 516 517 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp, 518 poll_table *wait) 519 { 520 struct ptp_clock *ptp = 521 container_of(pccontext->clk, struct ptp_clock, clock); 522 struct timestamp_event_queue *queue; 523 524 queue = pccontext->private_clkdata; 525 if (!queue) 526 return EPOLLERR; 527 528 poll_wait(fp, &ptp->tsev_wq, wait); 529 530 return queue_cnt(queue) ? EPOLLIN : 0; 531 } 532 533 #define EXTTS_BUFSIZE (PTP_BUF_TIMESTAMPS * sizeof(struct ptp_extts_event)) 534 535 ssize_t ptp_read(struct posix_clock_context *pccontext, uint rdflags, 536 char __user *buf, size_t cnt) 537 { 538 struct ptp_clock *ptp = container_of(pccontext->clk, struct ptp_clock, clock); 539 struct timestamp_event_queue *queue; 540 struct ptp_extts_event *event; 541 ssize_t result; 542 543 queue = pccontext->private_clkdata; 544 if (!queue) 545 return -EINVAL; 546 547 if (cnt % sizeof(*event) != 0) 548 return -EINVAL; 549 550 if (cnt > EXTTS_BUFSIZE) 551 cnt = EXTTS_BUFSIZE; 552 553 if (wait_event_interruptible(ptp->tsev_wq, ptp->defunct || queue_cnt(queue))) 554 return -ERESTARTSYS; 555 556 if (ptp->defunct) 557 return -ENODEV; 558 559 event = kmalloc(EXTTS_BUFSIZE, GFP_KERNEL); 560 if (!event) 561 return -ENOMEM; 562 563 scoped_guard(spinlock_irq, &queue->lock) { 564 size_t qcnt = min((size_t)queue_cnt(queue), cnt / sizeof(*event)); 565 566 for (size_t i = 0; i < qcnt; i++) { 567 event[i] = queue->buf[queue->head]; 568 /* Paired with READ_ONCE() in queue_cnt() */ 569 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS); 570 } 571 cnt = qcnt * sizeof(*event); 572 } 573 574 result = cnt; 575 if (copy_to_user(buf, event, cnt)) 576 result = -EFAULT; 577 578 kfree(event); 579 return result; 580 } 581