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/of_device.h>
19 #include <asm/hypervisor.h>
23 #define DRV_MODULE_NAME "n2rng"
24 #define PFX DRV_MODULE_NAME ": "
25 #define DRV_MODULE_VERSION "0.3"
26 #define DRV_MODULE_RELDATE "Jan 7, 2017"
28 static char version
[] =
29 DRV_MODULE_NAME
" v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("Niagara2 RNG driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION
);
36 /* The Niagara2 RNG provides a 64-bit read-only random number
37 * register, plus a control register. Access to the RNG is
38 * virtualized through the hypervisor so that both guests and control
39 * nodes can access the device.
41 * The entropy source consists of raw entropy sources, each
42 * constructed from a voltage controlled oscillator whose phase is
43 * jittered by thermal noise sources.
45 * The oscillator in each of the three raw entropy sources run at
46 * different frequencies. Normally, all three generator outputs are
47 * gathered, xored together, and fed into a CRC circuit, the output of
48 * which is the 64-bit read-only register.
50 * Some time is necessary for all the necessary entropy to build up
51 * such that a full 64-bits of entropy are available in the register.
52 * In normal operating mode (RNG_CTL_LFSR is set), the chip implements
53 * an interlock which blocks register reads until sufficient entropy
56 * A control register is provided for adjusting various aspects of RNG
57 * operation, and to enable diagnostic modes. Each of the three raw
58 * entropy sources has an enable bit (RNG_CTL_ES{1,2,3}). Also
59 * provided are fields for controlling the minimum time in cycles
60 * between read accesses to the register (RNG_CTL_WAIT, this controls
61 * the interlock described in the previous paragraph).
63 * The standard setting is to have the mode bit (RNG_CTL_LFSR) set,
64 * all three entropy sources enabled, and the interlock time set
67 * The CRC polynomial used by the chip is:
69 * P(X) = x64 + x61 + x57 + x56 + x52 + x51 + x50 + x48 + x47 + x46 +
70 * x43 + x42 + x41 + x39 + x38 + x37 + x35 + x32 + x28 + x25 +
71 * x22 + x21 + x17 + x15 + x13 + x12 + x11 + x7 + x5 + x + 1
73 * The RNG_CTL_VCO value of each noise cell must be programmed
74 * separately. This is why 4 control register values must be provided
75 * to the hypervisor. During a write, the hypervisor writes them all,
76 * one at a time, to the actual RNG_CTL register. The first three
77 * values are used to setup the desired RNG_CTL_VCO for each entropy
78 * source, for example:
80 * control 0: (1 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES1
81 * control 1: (2 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES2
82 * control 2: (3 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES3
84 * And then the fourth value sets the final chip state and enables
88 static int n2rng_hv_err_trans(unsigned long hv_err
)
109 static unsigned long n2rng_generic_read_control_v2(unsigned long ra
,
112 unsigned long hv_err
, state
, ticks
, watchdog_delta
, watchdog_status
;
113 int block
= 0, busy
= 0;
116 hv_err
= sun4v_rng_ctl_read_v2(ra
, unit
, &state
,
120 if (hv_err
== HV_EOK
)
123 if (hv_err
== HV_EBUSY
) {
124 if (++busy
>= N2RNG_BUSY_LIMIT
)
128 } else if (hv_err
== HV_EWOULDBLOCK
) {
129 if (++block
>= N2RNG_BLOCK_LIMIT
)
140 /* In multi-socket situations, the hypervisor might need to
141 * queue up the RNG control register write if it's for a unit
142 * that is on a cpu socket other than the one we are executing on.
144 * We poll here waiting for a successful read of that control
145 * register to make sure the write has been actually performed.
147 static unsigned long n2rng_control_settle_v2(struct n2rng
*np
, int unit
)
149 unsigned long ra
= __pa(&np
->scratch_control
[0]);
151 return n2rng_generic_read_control_v2(ra
, unit
);
154 static unsigned long n2rng_write_ctl_one(struct n2rng
*np
, int unit
,
156 unsigned long control_ra
,
157 unsigned long watchdog_timeout
,
158 unsigned long *ticks
)
160 unsigned long hv_err
;
162 if (np
->hvapi_major
== 1) {
163 hv_err
= sun4v_rng_ctl_write_v1(control_ra
, state
,
164 watchdog_timeout
, ticks
);
166 hv_err
= sun4v_rng_ctl_write_v2(control_ra
, state
,
167 watchdog_timeout
, unit
);
168 if (hv_err
== HV_EOK
)
169 hv_err
= n2rng_control_settle_v2(np
, unit
);
170 *ticks
= N2RNG_ACCUM_CYCLES_DEFAULT
;
176 static int n2rng_generic_read_data(unsigned long data_ra
)
178 unsigned long ticks
, hv_err
;
179 int block
= 0, hcheck
= 0;
182 hv_err
= sun4v_rng_data_read(data_ra
, &ticks
);
183 if (hv_err
== HV_EOK
)
186 if (hv_err
== HV_EWOULDBLOCK
) {
187 if (++block
>= N2RNG_BLOCK_LIMIT
)
190 } else if (hv_err
== HV_ENOACCESS
) {
192 } else if (hv_err
== HV_EIO
) {
193 if (++hcheck
>= N2RNG_HCHECK_LIMIT
)
201 static unsigned long n2rng_read_diag_data_one(struct n2rng
*np
,
203 unsigned long data_ra
,
204 unsigned long data_len
,
205 unsigned long *ticks
)
207 unsigned long hv_err
;
209 if (np
->hvapi_major
== 1) {
210 hv_err
= sun4v_rng_data_read_diag_v1(data_ra
, data_len
, ticks
);
212 hv_err
= sun4v_rng_data_read_diag_v2(data_ra
, data_len
,
215 *ticks
= N2RNG_ACCUM_CYCLES_DEFAULT
;
220 static int n2rng_generic_read_diag_data(struct n2rng
*np
,
222 unsigned long data_ra
,
223 unsigned long data_len
)
225 unsigned long ticks
, hv_err
;
229 hv_err
= n2rng_read_diag_data_one(np
, unit
,
232 if (hv_err
== HV_EOK
)
235 if (hv_err
== HV_EWOULDBLOCK
) {
236 if (++block
>= N2RNG_BLOCK_LIMIT
)
239 } else if (hv_err
== HV_ENOACCESS
) {
241 } else if (hv_err
== HV_EIO
) {
249 static int n2rng_generic_write_control(struct n2rng
*np
,
250 unsigned long control_ra
,
254 unsigned long hv_err
, ticks
;
255 int block
= 0, busy
= 0;
258 hv_err
= n2rng_write_ctl_one(np
, unit
, state
, control_ra
,
259 np
->wd_timeo
, &ticks
);
260 if (hv_err
== HV_EOK
)
263 if (hv_err
== HV_EWOULDBLOCK
) {
264 if (++block
>= N2RNG_BLOCK_LIMIT
)
267 } else if (hv_err
== HV_EBUSY
) {
268 if (++busy
>= N2RNG_BUSY_LIMIT
)
276 /* Just try to see if we can successfully access the control register
277 * of the RNG on the domain on which we are currently executing.
279 static int n2rng_try_read_ctl(struct n2rng
*np
)
281 unsigned long hv_err
;
284 if (np
->hvapi_major
== 1) {
285 hv_err
= sun4v_rng_get_diag_ctl();
287 /* We purposefully give invalid arguments, HV_NOACCESS
288 * is higher priority than the errors we'd get from
289 * these other cases, and that's the error we are
290 * truly interested in.
292 hv_err
= sun4v_rng_ctl_read_v2(0UL, ~0UL, &x
, &x
, &x
, &x
);
303 return n2rng_hv_err_trans(hv_err
);
306 static u64
n2rng_control_default(struct n2rng
*np
, int ctl
)
310 if (np
->data
->chip_version
== 1) {
311 val
= ((2 << RNG_v1_CTL_ASEL_SHIFT
) |
312 (N2RNG_ACCUM_CYCLES_DEFAULT
<< RNG_v1_CTL_WAIT_SHIFT
) |
317 val
|= (1 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES1
;
320 val
|= (2 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES2
;
323 val
|= (3 << RNG_v1_CTL_VCO_SHIFT
) | RNG_CTL_ES3
;
326 val
|= RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
;
333 val
= ((2 << RNG_v2_CTL_ASEL_SHIFT
) |
334 (N2RNG_ACCUM_CYCLES_DEFAULT
<< RNG_v2_CTL_WAIT_SHIFT
) |
339 val
|= (1 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES1
;
342 val
|= (2 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES2
;
345 val
|= (3 << RNG_v2_CTL_VCO_SHIFT
) | RNG_CTL_ES3
;
348 val
|= RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
;
358 static void n2rng_control_swstate_init(struct n2rng
*np
)
362 np
->flags
|= N2RNG_FLAG_CONTROL
;
364 np
->health_check_sec
= N2RNG_HEALTH_CHECK_SEC_DEFAULT
;
365 np
->accum_cycles
= N2RNG_ACCUM_CYCLES_DEFAULT
;
366 np
->wd_timeo
= N2RNG_WD_TIMEO_DEFAULT
;
368 for (i
= 0; i
< np
->num_units
; i
++) {
369 struct n2rng_unit
*up
= &np
->units
[i
];
371 up
->control
[0] = n2rng_control_default(np
, 0);
372 up
->control
[1] = n2rng_control_default(np
, 1);
373 up
->control
[2] = n2rng_control_default(np
, 2);
374 up
->control
[3] = n2rng_control_default(np
, 3);
377 np
->hv_state
= HV_RNG_STATE_UNCONFIGURED
;
380 static int n2rng_grab_diag_control(struct n2rng
*np
)
382 int i
, busy_count
, err
= -ENODEV
;
385 for (i
= 0; i
< 100; i
++) {
386 err
= n2rng_try_read_ctl(np
);
390 if (++busy_count
> 100) {
391 dev_err(&np
->op
->dev
,
392 "Grab diag control timeout.\n");
402 static int n2rng_init_control(struct n2rng
*np
)
404 int err
= n2rng_grab_diag_control(np
);
406 /* Not in the control domain, that's OK we are only a consumer
407 * of the RNG data, we don't setup and program it.
414 n2rng_control_swstate_init(np
);
419 static int n2rng_data_read(struct hwrng
*rng
, u32
*data
)
421 struct n2rng
*np
= (struct n2rng
*) rng
->priv
;
422 unsigned long ra
= __pa(&np
->test_data
);
425 if (!(np
->flags
& N2RNG_FLAG_READY
)) {
427 } else if (np
->flags
& N2RNG_FLAG_BUFFER_VALID
) {
428 np
->flags
&= ~N2RNG_FLAG_BUFFER_VALID
;
432 int err
= n2rng_generic_read_data(ra
);
434 np
->flags
|= N2RNG_FLAG_BUFFER_VALID
;
435 np
->buffer
= np
->test_data
>> 32;
436 *data
= np
->test_data
& 0xffffffff;
439 dev_err(&np
->op
->dev
, "RNG error, retesting\n");
440 np
->flags
&= ~N2RNG_FLAG_READY
;
441 if (!(np
->flags
& N2RNG_FLAG_SHUTDOWN
))
442 schedule_delayed_work(&np
->work
, 0);
450 /* On a guest node, just make sure we can read random data properly.
451 * If a control node reboots or reloads it's n2rng driver, this won't
452 * work during that time. So we have to keep probing until the device
455 static int n2rng_guest_check(struct n2rng
*np
)
457 unsigned long ra
= __pa(&np
->test_data
);
459 return n2rng_generic_read_data(ra
);
462 static int n2rng_entropy_diag_read(struct n2rng
*np
, unsigned long unit
,
463 u64
*pre_control
, u64 pre_state
,
464 u64
*buffer
, unsigned long buf_len
,
465 u64
*post_control
, u64 post_state
)
467 unsigned long post_ctl_ra
= __pa(post_control
);
468 unsigned long pre_ctl_ra
= __pa(pre_control
);
469 unsigned long buffer_ra
= __pa(buffer
);
472 err
= n2rng_generic_write_control(np
, pre_ctl_ra
, unit
, pre_state
);
476 err
= n2rng_generic_read_diag_data(np
, unit
,
479 (void) n2rng_generic_write_control(np
, post_ctl_ra
, unit
,
485 static u64
advance_polynomial(u64 poly
, u64 val
, int count
)
489 for (i
= 0; i
< count
; i
++) {
490 int highbit_set
= ((s64
)val
< 0);
500 static int n2rng_test_buffer_find(struct n2rng
*np
, u64 val
)
504 /* Purposefully skip over the first word. */
505 for (i
= 1; i
< SELFTEST_BUFFER_WORDS
; i
++) {
506 if (np
->test_buffer
[i
] == val
)
512 static void n2rng_dump_test_buffer(struct n2rng
*np
)
516 for (i
= 0; i
< SELFTEST_BUFFER_WORDS
; i
++)
517 dev_err(&np
->op
->dev
, "Test buffer slot %d [0x%016llx]\n",
518 i
, np
->test_buffer
[i
]);
521 static int n2rng_check_selftest_buffer(struct n2rng
*np
, unsigned long unit
)
524 int err
, matches
, limit
;
526 switch (np
->data
->id
) {
530 case N2_m4_rng
: /* yes, m4 uses the old value */
531 val
= RNG_v1_SELFTEST_VAL
;
534 val
= RNG_v2_SELFTEST_VAL
;
539 for (limit
= 0; limit
< SELFTEST_LOOPS_MAX
; limit
++) {
540 matches
+= n2rng_test_buffer_find(np
, val
);
541 if (matches
>= SELFTEST_MATCH_GOAL
)
543 val
= advance_polynomial(SELFTEST_POLY
, val
, 1);
547 if (limit
>= SELFTEST_LOOPS_MAX
) {
549 dev_err(&np
->op
->dev
, "Selftest failed on unit %lu\n", unit
);
550 n2rng_dump_test_buffer(np
);
552 dev_info(&np
->op
->dev
, "Selftest passed on unit %lu\n", unit
);
557 static int n2rng_control_selftest(struct n2rng
*np
, unsigned long unit
)
562 switch (np
->data
->id
) {
566 base
= RNG_v1_CTL_ASEL_NOOUT
<< RNG_v1_CTL_ASEL_SHIFT
;
567 base3
= base
| RNG_CTL_LFSR
|
568 ((RNG_v1_SELFTEST_TICKS
- 2) << RNG_v1_CTL_WAIT_SHIFT
);
571 base
= RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
;
572 base3
= base
| RNG_CTL_LFSR
|
573 ((RNG_v1_SELFTEST_TICKS
- 2) << RNG_v2_CTL_WAIT_SHIFT
);
576 base
= RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
;
577 base3
= base
| RNG_CTL_LFSR
|
578 (RNG_v2_SELFTEST_TICKS
<< RNG_v2_CTL_WAIT_SHIFT
);
582 np
->test_control
[0] = base
;
583 np
->test_control
[1] = base
;
584 np
->test_control
[2] = base
;
585 np
->test_control
[3] = base3
;
587 err
= n2rng_entropy_diag_read(np
, unit
, np
->test_control
,
588 HV_RNG_STATE_HEALTHCHECK
,
590 sizeof(np
->test_buffer
),
591 &np
->units
[unit
].control
[0],
596 return n2rng_check_selftest_buffer(np
, unit
);
599 static int n2rng_control_check(struct n2rng
*np
)
603 for (i
= 0; i
< np
->num_units
; i
++) {
604 int err
= n2rng_control_selftest(np
, i
);
611 /* The sanity checks passed, install the final configuration into the
612 * chip, it's ready to use.
614 static int n2rng_control_configure_units(struct n2rng
*np
)
619 for (unit
= 0; unit
< np
->num_units
; unit
++) {
620 struct n2rng_unit
*up
= &np
->units
[unit
];
621 unsigned long ctl_ra
= __pa(&up
->control
[0]);
625 if (np
->data
->chip_version
== 1) {
626 base
= ((np
->accum_cycles
<< RNG_v1_CTL_WAIT_SHIFT
) |
627 (RNG_v1_CTL_ASEL_NOOUT
<< RNG_v1_CTL_ASEL_SHIFT
) |
629 shift
= RNG_v1_CTL_VCO_SHIFT
;
631 base
= ((np
->accum_cycles
<< RNG_v2_CTL_WAIT_SHIFT
) |
632 (RNG_v2_CTL_ASEL_NOOUT
<< RNG_v2_CTL_ASEL_SHIFT
) |
634 shift
= RNG_v2_CTL_VCO_SHIFT
;
637 /* XXX This isn't the best. We should fetch a bunch
638 * XXX of words using each entropy source combined XXX
639 * with each VCO setting, and see which combinations
640 * XXX give the best random data.
642 for (esrc
= 0; esrc
< 3; esrc
++)
643 up
->control
[esrc
] = base
|
645 (RNG_CTL_ES1
<< esrc
);
647 up
->control
[3] = base
|
648 (RNG_CTL_ES1
| RNG_CTL_ES2
| RNG_CTL_ES3
);
650 err
= n2rng_generic_write_control(np
, ctl_ra
, unit
,
651 HV_RNG_STATE_CONFIGURED
);
659 static void n2rng_work(struct work_struct
*work
)
661 struct n2rng
*np
= container_of(work
, struct n2rng
, work
.work
);
663 static int retries
= 4;
665 if (!(np
->flags
& N2RNG_FLAG_CONTROL
)) {
666 err
= n2rng_guest_check(np
);
669 err
= n2rng_control_check(np
);
673 err
= n2rng_control_configure_units(np
);
677 np
->flags
|= N2RNG_FLAG_READY
;
678 dev_info(&np
->op
->dev
, "RNG ready\n");
682 dev_err(&np
->op
->dev
, "Self-test retries failed, RNG not ready\n");
683 else if (err
&& !(np
->flags
& N2RNG_FLAG_SHUTDOWN
))
684 schedule_delayed_work(&np
->work
, HZ
* 2);
687 static void n2rng_driver_version(void)
689 static int n2rng_version_printed
;
691 if (n2rng_version_printed
++ == 0)
692 pr_info("%s", version
);
695 static const struct of_device_id n2rng_match
[];
696 static int n2rng_probe(struct platform_device
*op
)
698 const struct of_device_id
*match
;
702 match
= of_match_device(n2rng_match
, &op
->dev
);
706 n2rng_driver_version();
707 np
= devm_kzalloc(&op
->dev
, sizeof(*np
), GFP_KERNEL
);
711 np
->data
= (struct n2rng_template
*)match
->data
;
713 INIT_DELAYED_WORK(&np
->work
, n2rng_work
);
715 if (np
->data
->multi_capable
)
716 np
->flags
|= N2RNG_FLAG_MULTI
;
720 if (sun4v_hvapi_register(HV_GRP_RNG
,
724 if (sun4v_hvapi_register(HV_GRP_RNG
,
727 dev_err(&op
->dev
, "Cannot register suitable "
733 if (np
->flags
& N2RNG_FLAG_MULTI
) {
734 if (np
->hvapi_major
< 2) {
735 dev_err(&op
->dev
, "multi-unit-capable RNG requires "
736 "HVAPI major version 2 or later, got %lu\n",
738 goto out_hvapi_unregister
;
740 np
->num_units
= of_getintprop_default(op
->dev
.of_node
,
742 if (!np
->num_units
) {
743 dev_err(&op
->dev
, "VF RNG lacks rng-#units property\n");
744 goto out_hvapi_unregister
;
750 dev_info(&op
->dev
, "Registered RNG HVAPI major %lu minor %lu\n",
751 np
->hvapi_major
, np
->hvapi_minor
);
752 np
->units
= devm_kcalloc(&op
->dev
, np
->num_units
, sizeof(*np
->units
),
756 goto out_hvapi_unregister
;
758 err
= n2rng_init_control(np
);
760 goto out_hvapi_unregister
;
762 dev_info(&op
->dev
, "Found %s RNG, units: %d\n",
763 ((np
->flags
& N2RNG_FLAG_MULTI
) ?
764 "multi-unit-capable" : "single-unit"),
767 np
->hwrng
.name
= DRV_MODULE_NAME
;
768 np
->hwrng
.data_read
= n2rng_data_read
;
769 np
->hwrng
.priv
= (unsigned long) np
;
771 err
= devm_hwrng_register(&op
->dev
, &np
->hwrng
);
773 goto out_hvapi_unregister
;
775 platform_set_drvdata(op
, np
);
777 schedule_delayed_work(&np
->work
, 0);
781 out_hvapi_unregister
:
782 sun4v_hvapi_unregister(HV_GRP_RNG
);
788 static int n2rng_remove(struct platform_device
*op
)
790 struct n2rng
*np
= platform_get_drvdata(op
);
792 np
->flags
|= N2RNG_FLAG_SHUTDOWN
;
794 cancel_delayed_work_sync(&np
->work
);
796 sun4v_hvapi_unregister(HV_GRP_RNG
);
801 static struct n2rng_template n2_template
= {
807 static struct n2rng_template vf_template
= {
813 static struct n2rng_template kt_template
= {
819 static struct n2rng_template m4_template
= {
825 static struct n2rng_template m7_template
= {
831 static const struct of_device_id n2rng_match
[] = {
833 .name
= "random-number-generator",
834 .compatible
= "SUNW,n2-rng",
835 .data
= &n2_template
,
838 .name
= "random-number-generator",
839 .compatible
= "SUNW,vf-rng",
840 .data
= &vf_template
,
843 .name
= "random-number-generator",
844 .compatible
= "SUNW,kt-rng",
845 .data
= &kt_template
,
848 .name
= "random-number-generator",
849 .compatible
= "ORCL,m4-rng",
850 .data
= &m4_template
,
853 .name
= "random-number-generator",
854 .compatible
= "ORCL,m7-rng",
855 .data
= &m7_template
,
859 MODULE_DEVICE_TABLE(of
, n2rng_match
);
861 static struct platform_driver n2rng_driver
= {
864 .of_match_table
= n2rng_match
,
866 .probe
= n2rng_probe
,
867 .remove
= n2rng_remove
,
870 module_platform_driver(n2rng_driver
);