1 // SPDX-License-Identifier: GPL-2.0-only
2 /* n2-drv.c: Niagara-2 RNG driver.
4 * Copyright (C) 2008, 2011 David S. Miller <davem@davemloft.net>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/workqueue.h>
13 #include <linux/preempt.h>
14 #include <linux/hw_random.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
20 #include <asm/hypervisor.h>
24 #define DRV_MODULE_NAME "n2rng"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "0.3"
27 #define DRV_MODULE_RELDATE "Jan 7, 2017"
29 static char version
[] =
30 DRV_MODULE_NAME
" v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
32 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
33 MODULE_DESCRIPTION("Niagara2 RNG driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(DRV_MODULE_VERSION
);
37 /* The Niagara2 RNG provides a 64-bit read-only random number
38 * register, plus a control register. Access to the RNG is
39 * virtualized through the hypervisor so that both guests and control
40 * nodes can access the device.
42 * The entropy source consists of raw entropy sources, each
43 * constructed from a voltage controlled oscillator whose phase is
44 * jittered by thermal noise sources.
46 * The oscillator in each of the three raw entropy sources run at
47 * different frequencies. Normally, all three generator outputs are
48 * gathered, xored together, and fed into a CRC circuit, the output of
49 * which is the 64-bit read-only register.
51 * Some time is necessary for all the necessary entropy to build up
52 * such that a full 64-bits of entropy are available in the register.
53 * In normal operating mode (RNG_CTL_LFSR is set), the chip implements
54 * an interlock which blocks register reads until sufficient entropy
57 * A control register is provided for adjusting various aspects of RNG
58 * operation, and to enable diagnostic modes. Each of the three raw
59 * entropy sources has an enable bit (RNG_CTL_ES{1,2,3}). Also
60 * provided are fields for controlling the minimum time in cycles
61 * between read accesses to the register (RNG_CTL_WAIT, this controls
62 * the interlock described in the previous paragraph).
64 * The standard setting is to have the mode bit (RNG_CTL_LFSR) set,
65 * all three entropy sources enabled, and the interlock time set
68 * The CRC polynomial used by the chip is:
70 * P(X) = x64 + x61 + x57 + x56 + x52 + x51 + x50 + x48 + x47 + x46 +
71 * x43 + x42 + x41 + x39 + x38 + x37 + x35 + x32 + x28 + x25 +
72 * x22 + x21 + x17 + x15 + x13 + x12 + x11 + x7 + x5 + x + 1
74 * The RNG_CTL_VCO value of each noise cell must be programmed
75 * separately. This is why 4 control register values must be provided
76 * to the hypervisor. During a write, the hypervisor writes them all,
77 * one at a time, to the actual RNG_CTL register. The first three
78 * values are used to setup the desired RNG_CTL_VCO for each entropy
79 * source, for example:
81 * control 0: (1 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES1
82 * control 1: (2 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES2
83 * control 2: (3 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES3
85 * And then the fourth value sets the final chip state and enables
89 static int n2rng_hv_err_trans(unsigned long hv_err
)
110 static unsigned long n2rng_generic_read_control_v2(unsigned long ra
,
113 unsigned long hv_err
, state
, ticks
, watchdog_delta
, watchdog_status
;
114 int block
= 0, busy
= 0;
117 hv_err
= sun4v_rng_ctl_read_v2(ra
, unit
, &state
,
121 if (hv_err
== HV_EOK
)
124 if (hv_err
== HV_EBUSY
) {
125 if (++busy
>= N2RNG_BUSY_LIMIT
)
129 } else if (hv_err
== HV_EWOULDBLOCK
) {
130 if (++block
>= N2RNG_BLOCK_LIMIT
)
141 /* In multi-socket situations, the hypervisor might need to
142 * queue up the RNG control register write if it's for a unit
143 * that is on a cpu socket other than the one we are executing on.
145 * We poll here waiting for a successful read of that control
146 * register to make sure the write has been actually performed.
148 static unsigned long n2rng_control_settle_v2(struct n2rng
*np
, int unit
)
150 unsigned long ra
= __pa(&np
->scratch_control
[0]);
152 return n2rng_generic_read_control_v2(ra
, unit
);
155 static unsigned long n2rng_write_ctl_one(struct n2rng
*np
, int unit
,
157 unsigned long control_ra
,
158 unsigned long watchdog_timeout
,
159 unsigned long *ticks
)
161 unsigned long hv_err
;
163 if (np
->hvapi_major
== 1) {
164 hv_err
= sun4v_rng_ctl_write_v1(control_ra
, state
,
165 watchdog_timeout
, ticks
);
167 hv_err
= sun4v_rng_ctl_write_v2(control_ra
, state
,
168 watchdog_timeout
, unit
);
169 if (hv_err
== HV_EOK
)
170 hv_err
= n2rng_control_settle_v2(np
, unit
);
171 *ticks
= N2RNG_ACCUM_CYCLES_DEFAULT
;
177 static int n2rng_generic_read_data(unsigned long data_ra
)
179 unsigned long ticks
, hv_err
;
180 int block
= 0, hcheck
= 0;
183 hv_err
= sun4v_rng_data_read(data_ra
, &ticks
);
184 if (hv_err
== HV_EOK
)
187 if (hv_err
== HV_EWOULDBLOCK
) {
188 if (++block
>= N2RNG_BLOCK_LIMIT
)
191 } else if (hv_err
== HV_ENOACCESS
) {
193 } else if (hv_err
== HV_EIO
) {
194 if (++hcheck
>= N2RNG_HCHECK_LIMIT
)
202 static unsigned long n2rng_read_diag_data_one(struct n2rng
*np
,
204 unsigned long data_ra
,
205 unsigned long data_len
,
206 unsigned long *ticks
)
208 unsigned long hv_err
;
210 if (np
->hvapi_major
== 1) {
211 hv_err
= sun4v_rng_data_read_diag_v1(data_ra
, data_len
, ticks
);
213 hv_err
= sun4v_rng_data_read_diag_v2(data_ra
, data_len
,
216 *ticks
= N2RNG_ACCUM_CYCLES_DEFAULT
;
221 static int n2rng_generic_read_diag_data(struct n2rng
*np
,
223 unsigned long data_ra
,
224 unsigned long data_len
)
226 unsigned long ticks
, hv_err
;
230 hv_err
= n2rng_read_diag_data_one(np
, unit
,
233 if (hv_err
== HV_EOK
)
236 if (hv_err
== HV_EWOULDBLOCK
) {
237 if (++block
>= N2RNG_BLOCK_LIMIT
)
240 } else if (hv_err
== HV_ENOACCESS
) {
242 } else if (hv_err
== HV_EIO
) {
250 static int n2rng_generic_write_control(struct n2rng
*np
,
251 unsigned long control_ra
,
255 unsigned long hv_err
, ticks
;
256 int block
= 0, busy
= 0;
259 hv_err
= n2rng_write_ctl_one(np
, unit
, state
, control_ra
,
260 np
->wd_timeo
, &ticks
);
261 if (hv_err
== HV_EOK
)
264 if (hv_err
== HV_EWOULDBLOCK
) {
265 if (++block
>= N2RNG_BLOCK_LIMIT
)
268 } else if (hv_err
== HV_EBUSY
) {
269 if (++busy
>= N2RNG_BUSY_LIMIT
)
277 /* Just try to see if we can successfully access the control register
278 * of the RNG on the domain on which we are currently executing.
280 static int n2rng_try_read_ctl(struct n2rng
*np
)
282 unsigned long hv_err
;
285 if (np
->hvapi_major
== 1) {
286 hv_err
= sun4v_rng_get_diag_ctl();
288 /* We purposefully give invalid arguments, HV_NOACCESS
289 * is higher priority than the errors we'd get from
290 * these other cases, and that's the error we are
291 * truly interested in.
293 hv_err
= sun4v_rng_ctl_read_v2(0UL, ~0UL, &x
, &x
, &x
, &x
);
304 return n2rng_hv_err_trans(hv_err
);
307 static u64
n2rng_control_default(struct n2rng
*np
, int ctl
)
311 if (np
->data
->chip_version
== 1) {
312 val
= ((2 << RNG_v1_CTL_ASEL_SHIFT
) |
313 (N2RNG_ACCUM_CYCLES_DEFAULT
<< RNG_v1_CTL_WAIT_SHIFT
) |
318 val
|= (1 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES1
;
321 val
|= (2 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES2
;
324 val
|= (3 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES3
;
327 val
|= RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
;
334 val
= ((2 << RNG_v2_CTL_ASEL_SHIFT
) |
335 (N2RNG_ACCUM_CYCLES_DEFAULT
<< RNG_v2_CTL_WAIT_SHIFT
) |
340 val
|= (1 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES1
;
343 val
|= (2 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES2
;
346 val
|= (3 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES3
;
349 val
|= RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
;
359 static void n2rng_control_swstate_init(struct n2rng
*np
)
363 np
->flags
|= N2RNG_FLAG_CONTROL
;
365 np
->health_check_sec
= N2RNG_HEALTH_CHECK_SEC_DEFAULT
;
366 np
->accum_cycles
= N2RNG_ACCUM_CYCLES_DEFAULT
;
367 np
->wd_timeo
= N2RNG_WD_TIMEO_DEFAULT
;
369 for (i
= 0; i
< np
->num_units
; i
++) {
370 struct n2rng_unit
*up
= &np
->units
[i
];
372 up
->control
[0] = n2rng_control_default(np
, 0);
373 up
->control
[1] = n2rng_control_default(np
, 1);
374 up
->control
[2] = n2rng_control_default(np
, 2);
375 up
->control
[3] = n2rng_control_default(np
, 3);
378 np
->hv_state
= HV_RNG_STATE_UNCONFIGURED
;
381 static int n2rng_grab_diag_control(struct n2rng
*np
)
383 int i
, busy_count
, err
= -ENODEV
;
386 for (i
= 0; i
< 100; i
++) {
387 err
= n2rng_try_read_ctl(np
);
391 if (++busy_count
> 100) {
392 dev_err(&np
->op
->dev
,
393 "Grab diag control timeout.\n");
403 static int n2rng_init_control(struct n2rng
*np
)
405 int err
= n2rng_grab_diag_control(np
);
407 /* Not in the control domain, that's OK we are only a consumer
408 * of the RNG data, we don't setup and program it.
415 n2rng_control_swstate_init(np
);
420 static int n2rng_data_read(struct hwrng
*rng
, u32
*data
)
422 struct n2rng
*np
= (struct n2rng
*) rng
->priv
;
423 unsigned long ra
= __pa(&np
->test_data
);
426 if (!(np
->flags
& N2RNG_FLAG_READY
)) {
428 } else if (np
->flags
& N2RNG_FLAG_BUFFER_VALID
) {
429 np
->flags
&= ~N2RNG_FLAG_BUFFER_VALID
;
433 int err
= n2rng_generic_read_data(ra
);
435 np
->flags
|= N2RNG_FLAG_BUFFER_VALID
;
436 np
->buffer
= np
->test_data
>> 32;
437 *data
= np
->test_data
& 0xffffffff;
440 dev_err(&np
->op
->dev
, "RNG error, retesting\n");
441 np
->flags
&= ~N2RNG_FLAG_READY
;
442 if (!(np
->flags
& N2RNG_FLAG_SHUTDOWN
))
443 schedule_delayed_work(&np
->work
, 0);
451 /* On a guest node, just make sure we can read random data properly.
452 * If a control node reboots or reloads it's n2rng driver, this won't
453 * work during that time. So we have to keep probing until the device
456 static int n2rng_guest_check(struct n2rng
*np
)
458 unsigned long ra
= __pa(&np
->test_data
);
460 return n2rng_generic_read_data(ra
);
463 static int n2rng_entropy_diag_read(struct n2rng
*np
, unsigned long unit
,
464 u64
*pre_control
, u64 pre_state
,
465 u64
*buffer
, unsigned long buf_len
,
466 u64
*post_control
, u64 post_state
)
468 unsigned long post_ctl_ra
= __pa(post_control
);
469 unsigned long pre_ctl_ra
= __pa(pre_control
);
470 unsigned long buffer_ra
= __pa(buffer
);
473 err
= n2rng_generic_write_control(np
, pre_ctl_ra
, unit
, pre_state
);
477 err
= n2rng_generic_read_diag_data(np
, unit
,
480 (void) n2rng_generic_write_control(np
, post_ctl_ra
, unit
,
486 static u64
advance_polynomial(u64 poly
, u64 val
, int count
)
490 for (i
= 0; i
< count
; i
++) {
491 int highbit_set
= ((s64
)val
< 0);
501 static int n2rng_test_buffer_find(struct n2rng
*np
, u64 val
)
505 /* Purposefully skip over the first word. */
506 for (i
= 1; i
< SELFTEST_BUFFER_WORDS
; i
++) {
507 if (np
->test_buffer
[i
] == val
)
513 static void n2rng_dump_test_buffer(struct n2rng
*np
)
517 for (i
= 0; i
< SELFTEST_BUFFER_WORDS
; i
++)
518 dev_err(&np
->op
->dev
, "Test buffer slot %d [0x%016llx]\n",
519 i
, np
->test_buffer
[i
]);
522 static int n2rng_check_selftest_buffer(struct n2rng
*np
, unsigned long unit
)
525 int err
, matches
, limit
;
527 switch (np
->data
->id
) {
531 case N2_m4_rng
: /* yes, m4 uses the old value */
532 val
= RNG_v1_SELFTEST_VAL
;
535 val
= RNG_v2_SELFTEST_VAL
;
540 for (limit
= 0; limit
< SELFTEST_LOOPS_MAX
; limit
++) {
541 matches
+= n2rng_test_buffer_find(np
, val
);
542 if (matches
>= SELFTEST_MATCH_GOAL
)
544 val
= advance_polynomial(SELFTEST_POLY
, val
, 1);
548 if (limit
>= SELFTEST_LOOPS_MAX
) {
550 dev_err(&np
->op
->dev
, "Selftest failed on unit %lu\n", unit
);
551 n2rng_dump_test_buffer(np
);
553 dev_info(&np
->op
->dev
, "Selftest passed on unit %lu\n", unit
);
558 static int n2rng_control_selftest(struct n2rng
*np
, unsigned long unit
)
563 switch (np
->data
->id
) {
567 base
= RNG_v1_CTL_ASEL_NOOUT
<< RNG_v1_CTL_ASEL_SHIFT
;
568 base3
= base
| RNG_CTL_LFSR
|
569 ((RNG_v1_SELFTEST_TICKS
- 2) << RNG_v1_CTL_WAIT_SHIFT
);
572 base
= RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
;
573 base3
= base
| RNG_CTL_LFSR
|
574 ((RNG_v1_SELFTEST_TICKS
- 2) << RNG_v2_CTL_WAIT_SHIFT
);
577 base
= RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
;
578 base3
= base
| RNG_CTL_LFSR
|
579 (RNG_v2_SELFTEST_TICKS
<< RNG_v2_CTL_WAIT_SHIFT
);
583 np
->test_control
[0] = base
;
584 np
->test_control
[1] = base
;
585 np
->test_control
[2] = base
;
586 np
->test_control
[3] = base3
;
588 err
= n2rng_entropy_diag_read(np
, unit
, np
->test_control
,
589 HV_RNG_STATE_HEALTHCHECK
,
591 sizeof(np
->test_buffer
),
592 &np
->units
[unit
].control
[0],
597 return n2rng_check_selftest_buffer(np
, unit
);
600 static int n2rng_control_check(struct n2rng
*np
)
604 for (i
= 0; i
< np
->num_units
; i
++) {
605 int err
= n2rng_control_selftest(np
, i
);
612 /* The sanity checks passed, install the final configuration into the
613 * chip, it's ready to use.
615 static int n2rng_control_configure_units(struct n2rng
*np
)
620 for (unit
= 0; unit
< np
->num_units
; unit
++) {
621 struct n2rng_unit
*up
= &np
->units
[unit
];
622 unsigned long ctl_ra
= __pa(&up
->control
[0]);
626 if (np
->data
->chip_version
== 1) {
627 base
= ((np
->accum_cycles
<< RNG_v1_CTL_WAIT_SHIFT
) |
628 (RNG_v1_CTL_ASEL_NOOUT
<< RNG_v1_CTL_ASEL_SHIFT
) |
630 shift
= RNG_v1_CTL_VCO_SHIFT
;
632 base
= ((np
->accum_cycles
<< RNG_v2_CTL_WAIT_SHIFT
) |
633 (RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
) |
635 shift
= RNG_v2_CTL_VCO_SHIFT
;
638 /* XXX This isn't the best. We should fetch a bunch
639 * XXX of words using each entropy source combined XXX
640 * with each VCO setting, and see which combinations
641 * XXX give the best random data.
643 for (esrc
= 0; esrc
< 3; esrc
++)
644 up
->control
[esrc
] = base
|
646 (RNG_CTL_ES1
<< esrc
);
648 up
->control
[3] = base
|
649 (RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
);
651 err
= n2rng_generic_write_control(np
, ctl_ra
, unit
,
652 HV_RNG_STATE_CONFIGURED
);
660 static void n2rng_work(struct work_struct
*work
)
662 struct n2rng
*np
= container_of(work
, struct n2rng
, work
.work
);
664 static int retries
= 4;
666 if (!(np
->flags
& N2RNG_FLAG_CONTROL
)) {
667 err
= n2rng_guest_check(np
);
670 err
= n2rng_control_check(np
);
674 err
= n2rng_control_configure_units(np
);
678 np
->flags
|= N2RNG_FLAG_READY
;
679 dev_info(&np
->op
->dev
, "RNG ready\n");
683 dev_err(&np
->op
->dev
, "Self-test retries failed, RNG not ready\n");
684 else if (err
&& !(np
->flags
& N2RNG_FLAG_SHUTDOWN
))
685 schedule_delayed_work(&np
->work
, HZ
* 2);
688 static void n2rng_driver_version(void)
690 static int n2rng_version_printed
;
692 if (n2rng_version_printed
++ == 0)
693 pr_info("%s", version
);
696 static const struct of_device_id n2rng_match
[];
697 static int n2rng_probe(struct platform_device
*op
)
702 n2rng_driver_version();
703 np
= devm_kzalloc(&op
->dev
, sizeof(*np
), GFP_KERNEL
);
707 np
->data
= (struct n2rng_template
*)device_get_match_data(&op
->dev
);
709 INIT_DELAYED_WORK(&np
->work
, n2rng_work
);
711 if (np
->data
->multi_capable
)
712 np
->flags
|= N2RNG_FLAG_MULTI
;
716 if (sun4v_hvapi_register(HV_GRP_RNG
,
720 if (sun4v_hvapi_register(HV_GRP_RNG
,
723 dev_err(&op
->dev
, "Cannot register suitable "
729 if (np
->flags
& N2RNG_FLAG_MULTI
) {
730 if (np
->hvapi_major
< 2) {
731 dev_err(&op
->dev
, "multi-unit-capable RNG requires "
732 "HVAPI major version 2 or later, got %lu\n",
734 goto out_hvapi_unregister
;
736 np
->num_units
= of_getintprop_default(op
->dev
.of_node
,
738 if (!np
->num_units
) {
739 dev_err(&op
->dev
, "VF RNG lacks rng-#units property\n");
740 goto out_hvapi_unregister
;
746 dev_info(&op
->dev
, "Registered RNG HVAPI major %lu minor %lu\n",
747 np
->hvapi_major
, np
->hvapi_minor
);
748 np
->units
= devm_kcalloc(&op
->dev
, np
->num_units
, sizeof(*np
->units
),
752 goto out_hvapi_unregister
;
754 err
= n2rng_init_control(np
);
756 goto out_hvapi_unregister
;
758 dev_info(&op
->dev
, "Found %s RNG, units: %d\n",
759 ((np
->flags
& N2RNG_FLAG_MULTI
) ?
760 "multi-unit-capable" : "single-unit"),
763 np
->hwrng
.name
= DRV_MODULE_NAME
;
764 np
->hwrng
.data_read
= n2rng_data_read
;
765 np
->hwrng
.priv
= (unsigned long) np
;
767 err
= devm_hwrng_register(&op
->dev
, &np
->hwrng
);
769 goto out_hvapi_unregister
;
771 platform_set_drvdata(op
, np
);
773 schedule_delayed_work(&np
->work
, 0);
777 out_hvapi_unregister
:
778 sun4v_hvapi_unregister(HV_GRP_RNG
);
784 static void n2rng_remove(struct platform_device
*op
)
786 struct n2rng
*np
= platform_get_drvdata(op
);
788 np
->flags
|= N2RNG_FLAG_SHUTDOWN
;
790 cancel_delayed_work_sync(&np
->work
);
792 sun4v_hvapi_unregister(HV_GRP_RNG
);
795 static struct n2rng_template n2_template
= {
801 static struct n2rng_template vf_template
= {
807 static struct n2rng_template kt_template
= {
813 static struct n2rng_template m4_template
= {
819 static struct n2rng_template m7_template
= {
825 static const struct of_device_id n2rng_match
[] = {
827 .name
= "random-number-generator",
828 .compatible
= "SUNW,n2-rng",
829 .data
= &n2_template
,
832 .name
= "random-number-generator",
833 .compatible
= "SUNW,vf-rng",
834 .data
= &vf_template
,
837 .name
= "random-number-generator",
838 .compatible
= "SUNW,kt-rng",
839 .data
= &kt_template
,
842 .name
= "random-number-generator",
843 .compatible
= "ORCL,m4-rng",
844 .data
= &m4_template
,
847 .name
= "random-number-generator",
848 .compatible
= "ORCL,m7-rng",
849 .data
= &m7_template
,
853 MODULE_DEVICE_TABLE(of
, n2rng_match
);
855 static struct platform_driver n2rng_driver
= {
858 .of_match_table
= n2rng_match
,
860 .probe
= n2rng_probe
,
861 .remove
= n2rng_remove
,
864 module_platform_driver(n2rng_driver
);