145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2649e9ea0SKent Yoder /* 3649e9ea0SKent Yoder * Copyright (C) 2010 Michael Neuling IBM Corporation 4649e9ea0SKent Yoder * 5649e9ea0SKent Yoder * Driver for the pseries hardware RNG for POWER7+ and above 6649e9ea0SKent Yoder */ 7649e9ea0SKent Yoder 8d319fe2aSMichael Ellerman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9d319fe2aSMichael Ellerman 10d319fe2aSMichael Ellerman #include <linux/kernel.h> 11649e9ea0SKent Yoder #include <linux/module.h> 12649e9ea0SKent Yoder #include <linux/hw_random.h> 13649e9ea0SKent Yoder #include <asm/vio.h> 14649e9ea0SKent Yoder 15649e9ea0SKent Yoder 1624c65bc7SGreg Kurz static int pseries_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 17649e9ea0SKent Yoder { 1824c65bc7SGreg Kurz u64 buffer[PLPAR_HCALL_BUFSIZE]; 19d319fe2aSMichael Ellerman int rc; 20d319fe2aSMichael Ellerman 2124c65bc7SGreg Kurz rc = plpar_hcall(H_RANDOM, (unsigned long *)buffer); 22d319fe2aSMichael Ellerman if (rc != H_SUCCESS) { 23d319fe2aSMichael Ellerman pr_err_ratelimited("H_RANDOM call failed %d\n", rc); 24d319fe2aSMichael Ellerman return -EIO; 25649e9ea0SKent Yoder } 26ed424bb3SPrasannaKumar Muralidharan memcpy(data, buffer, 8); 27d319fe2aSMichael Ellerman 28d319fe2aSMichael Ellerman /* The hypervisor interface returns 64 bits */ 29ed424bb3SPrasannaKumar Muralidharan return 8; 30649e9ea0SKent Yoder } 31649e9ea0SKent Yoder 32649e9ea0SKent Yoder /** 33649e9ea0SKent Yoder * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations 34649e9ea0SKent Yoder * 35649e9ea0SKent Yoder * This is a required function for a driver to operate in a CMO environment 36649e9ea0SKent Yoder * but this device does not make use of DMA allocations, return 0. 37649e9ea0SKent Yoder * 38649e9ea0SKent Yoder * Return value: 39649e9ea0SKent Yoder * Number of bytes of IO data the driver will need to perform well -> 0 40649e9ea0SKent Yoder */ 41649e9ea0SKent Yoder static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev) 42649e9ea0SKent Yoder { 43649e9ea0SKent Yoder return 0; 44649e9ea0SKent Yoder }; 45649e9ea0SKent Yoder 46649e9ea0SKent Yoder static struct hwrng pseries_rng = { 4749cca7e0SMichael Ellerman .name = KBUILD_MODNAME, 4824c65bc7SGreg Kurz .read = pseries_rng_read, 49649e9ea0SKent Yoder }; 50649e9ea0SKent Yoder 51257bedd4SDmitry Torokhov static int pseries_rng_probe(struct vio_dev *dev, 52649e9ea0SKent Yoder const struct vio_device_id *id) 53649e9ea0SKent Yoder { 54649e9ea0SKent Yoder return hwrng_register(&pseries_rng); 55649e9ea0SKent Yoder } 56649e9ea0SKent Yoder 57*386a966fSUwe Kleine-König static void pseries_rng_remove(struct vio_dev *dev) 58649e9ea0SKent Yoder { 59649e9ea0SKent Yoder hwrng_unregister(&pseries_rng); 60649e9ea0SKent Yoder } 61649e9ea0SKent Yoder 62677c4f4cSArvind Yadav static const struct vio_device_id pseries_rng_driver_ids[] = { 63649e9ea0SKent Yoder { "ibm,random-v1", "ibm,random"}, 64649e9ea0SKent Yoder { "", "" } 65649e9ea0SKent Yoder }; 66649e9ea0SKent Yoder MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids); 67649e9ea0SKent Yoder 68649e9ea0SKent Yoder static struct vio_driver pseries_rng_driver = { 6949cca7e0SMichael Ellerman .name = KBUILD_MODNAME, 70649e9ea0SKent Yoder .probe = pseries_rng_probe, 71649e9ea0SKent Yoder .remove = pseries_rng_remove, 72649e9ea0SKent Yoder .get_desired_dma = pseries_rng_get_desired_dma, 73649e9ea0SKent Yoder .id_table = pseries_rng_driver_ids 74649e9ea0SKent Yoder }; 75649e9ea0SKent Yoder 76649e9ea0SKent Yoder static int __init rng_init(void) 77649e9ea0SKent Yoder { 787a1ae9c0SSudip Mukherjee pr_info("Registering IBM pSeries RNG driver\n"); 79649e9ea0SKent Yoder return vio_register_driver(&pseries_rng_driver); 80649e9ea0SKent Yoder } 81649e9ea0SKent Yoder 82649e9ea0SKent Yoder module_init(rng_init); 83649e9ea0SKent Yoder 84649e9ea0SKent Yoder static void __exit rng_exit(void) 85649e9ea0SKent Yoder { 86649e9ea0SKent Yoder vio_unregister_driver(&pseries_rng_driver); 87649e9ea0SKent Yoder } 88649e9ea0SKent Yoder module_exit(rng_exit); 89649e9ea0SKent Yoder 90649e9ea0SKent Yoder MODULE_LICENSE("GPL"); 91649e9ea0SKent Yoder MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>"); 92649e9ea0SKent Yoder MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors"); 93