cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

qcom_common.c (13917B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * Qualcomm Peripheral Image Loader helpers
      4 *
      5 * Copyright (C) 2016 Linaro Ltd
      6 * Copyright (C) 2015 Sony Mobile Communications Inc
      7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
      8 */
      9
     10#include <linux/firmware.h>
     11#include <linux/kernel.h>
     12#include <linux/module.h>
     13#include <linux/notifier.h>
     14#include <linux/remoteproc.h>
     15#include <linux/remoteproc/qcom_rproc.h>
     16#include <linux/rpmsg/qcom_glink.h>
     17#include <linux/rpmsg/qcom_smd.h>
     18#include <linux/slab.h>
     19#include <linux/soc/qcom/mdt_loader.h>
     20#include <linux/soc/qcom/smem.h>
     21
     22#include "remoteproc_internal.h"
     23#include "qcom_common.h"
     24
     25#define to_glink_subdev(d) container_of(d, struct qcom_rproc_glink, subdev)
     26#define to_smd_subdev(d) container_of(d, struct qcom_rproc_subdev, subdev)
     27#define to_ssr_subdev(d) container_of(d, struct qcom_rproc_ssr, subdev)
     28
     29#define MAX_NUM_OF_SS           10
     30#define MAX_REGION_NAME_LENGTH  16
     31#define SBL_MINIDUMP_SMEM_ID	602
     32#define MD_REGION_VALID		('V' << 24 | 'A' << 16 | 'L' << 8 | 'I' << 0)
     33#define MD_SS_ENCR_DONE		('D' << 24 | 'O' << 16 | 'N' << 8 | 'E' << 0)
     34#define MD_SS_ENABLED		('E' << 24 | 'N' << 16 | 'B' << 8 | 'L' << 0)
     35
     36/**
     37 * struct minidump_region - Minidump region
     38 * @name		: Name of the region to be dumped
     39 * @seq_num:		: Use to differentiate regions with same name.
     40 * @valid		: This entry to be dumped (if set to 1)
     41 * @address		: Physical address of region to be dumped
     42 * @size		: Size of the region
     43 */
     44struct minidump_region {
     45	char	name[MAX_REGION_NAME_LENGTH];
     46	__le32	seq_num;
     47	__le32	valid;
     48	__le64	address;
     49	__le64	size;
     50};
     51
     52/**
     53 * struct minidump_subsystem_toc: Subsystem's SMEM Table of content
     54 * @status : Subsystem toc init status
     55 * @enabled : if set to 1, this region would be copied during coredump
     56 * @encryption_status: Encryption status for this subsystem
     57 * @encryption_required : Decides to encrypt the subsystem regions or not
     58 * @region_count : Number of regions added in this subsystem toc
     59 * @regions_baseptr : regions base pointer of the subsystem
     60 */
     61struct minidump_subsystem {
     62	__le32	status;
     63	__le32	enabled;
     64	__le32	encryption_status;
     65	__le32	encryption_required;
     66	__le32	region_count;
     67	__le64	regions_baseptr;
     68};
     69
     70/**
     71 * struct minidump_global_toc: Global Table of Content
     72 * @status : Global Minidump init status
     73 * @md_revision : Minidump revision
     74 * @enabled : Minidump enable status
     75 * @subsystems : Array of subsystems toc
     76 */
     77struct minidump_global_toc {
     78	__le32				status;
     79	__le32				md_revision;
     80	__le32				enabled;
     81	struct minidump_subsystem	subsystems[MAX_NUM_OF_SS];
     82};
     83
     84struct qcom_ssr_subsystem {
     85	const char *name;
     86	struct srcu_notifier_head notifier_list;
     87	struct list_head list;
     88};
     89
     90static LIST_HEAD(qcom_ssr_subsystem_list);
     91static DEFINE_MUTEX(qcom_ssr_subsys_lock);
     92
     93static void qcom_minidump_cleanup(struct rproc *rproc)
     94{
     95	struct rproc_dump_segment *entry, *tmp;
     96
     97	list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
     98		list_del(&entry->node);
     99		kfree(entry->priv);
    100		kfree(entry);
    101	}
    102}
    103
    104static int qcom_add_minidump_segments(struct rproc *rproc, struct minidump_subsystem *subsystem)
    105{
    106	struct minidump_region __iomem *ptr;
    107	struct minidump_region region;
    108	int seg_cnt, i;
    109	dma_addr_t da;
    110	size_t size;
    111	char *name;
    112
    113	if (WARN_ON(!list_empty(&rproc->dump_segments))) {
    114		dev_err(&rproc->dev, "dump segment list already populated\n");
    115		return -EUCLEAN;
    116	}
    117
    118	seg_cnt = le32_to_cpu(subsystem->region_count);
    119	ptr = ioremap((unsigned long)le64_to_cpu(subsystem->regions_baseptr),
    120		      seg_cnt * sizeof(struct minidump_region));
    121	if (!ptr)
    122		return -EFAULT;
    123
    124	for (i = 0; i < seg_cnt; i++) {
    125		memcpy_fromio(&region, ptr + i, sizeof(region));
    126		if (region.valid == MD_REGION_VALID) {
    127			name = kstrdup(region.name, GFP_KERNEL);
    128			if (!name) {
    129				iounmap(ptr);
    130				return -ENOMEM;
    131			}
    132			da = le64_to_cpu(region.address);
    133			size = le32_to_cpu(region.size);
    134			rproc_coredump_add_custom_segment(rproc, da, size, NULL, name);
    135		}
    136	}
    137
    138	iounmap(ptr);
    139	return 0;
    140}
    141
    142void qcom_minidump(struct rproc *rproc, unsigned int minidump_id)
    143{
    144	int ret;
    145	struct minidump_subsystem *subsystem;
    146	struct minidump_global_toc *toc;
    147
    148	/* Get Global minidump ToC*/
    149	toc = qcom_smem_get(QCOM_SMEM_HOST_ANY, SBL_MINIDUMP_SMEM_ID, NULL);
    150
    151	/* check if global table pointer exists and init is set */
    152	if (IS_ERR(toc) || !toc->status) {
    153		dev_err(&rproc->dev, "Minidump TOC not found in SMEM\n");
    154		return;
    155	}
    156
    157	/* Get subsystem table of contents using the minidump id */
    158	subsystem = &toc->subsystems[minidump_id];
    159
    160	/**
    161	 * Collect minidump if SS ToC is valid and segment table
    162	 * is initialized in memory and encryption status is set.
    163	 */
    164	if (subsystem->regions_baseptr == 0 ||
    165	    le32_to_cpu(subsystem->status) != 1 ||
    166	    le32_to_cpu(subsystem->enabled) != MD_SS_ENABLED ||
    167	    le32_to_cpu(subsystem->encryption_status) != MD_SS_ENCR_DONE) {
    168		dev_err(&rproc->dev, "Minidump not ready, skipping\n");
    169		return;
    170	}
    171
    172	ret = qcom_add_minidump_segments(rproc, subsystem);
    173	if (ret) {
    174		dev_err(&rproc->dev, "Failed with error: %d while adding minidump entries\n", ret);
    175		goto clean_minidump;
    176	}
    177	rproc_coredump_using_sections(rproc);
    178clean_minidump:
    179	qcom_minidump_cleanup(rproc);
    180}
    181EXPORT_SYMBOL_GPL(qcom_minidump);
    182
    183static int glink_subdev_start(struct rproc_subdev *subdev)
    184{
    185	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
    186
    187	glink->edge = qcom_glink_smem_register(glink->dev, glink->node);
    188
    189	return PTR_ERR_OR_ZERO(glink->edge);
    190}
    191
    192static void glink_subdev_stop(struct rproc_subdev *subdev, bool crashed)
    193{
    194	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
    195
    196	qcom_glink_smem_unregister(glink->edge);
    197	glink->edge = NULL;
    198}
    199
    200static void glink_subdev_unprepare(struct rproc_subdev *subdev)
    201{
    202	struct qcom_rproc_glink *glink = to_glink_subdev(subdev);
    203
    204	qcom_glink_ssr_notify(glink->ssr_name);
    205}
    206
    207/**
    208 * qcom_add_glink_subdev() - try to add a GLINK subdevice to rproc
    209 * @rproc:	rproc handle to parent the subdevice
    210 * @glink:	reference to a GLINK subdev context
    211 * @ssr_name:	identifier of the associated remoteproc for ssr notifications
    212 */
    213void qcom_add_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink,
    214			   const char *ssr_name)
    215{
    216	struct device *dev = &rproc->dev;
    217
    218	glink->node = of_get_child_by_name(dev->parent->of_node, "glink-edge");
    219	if (!glink->node)
    220		return;
    221
    222	glink->ssr_name = kstrdup_const(ssr_name, GFP_KERNEL);
    223	if (!glink->ssr_name)
    224		return;
    225
    226	glink->dev = dev;
    227	glink->subdev.start = glink_subdev_start;
    228	glink->subdev.stop = glink_subdev_stop;
    229	glink->subdev.unprepare = glink_subdev_unprepare;
    230
    231	rproc_add_subdev(rproc, &glink->subdev);
    232}
    233EXPORT_SYMBOL_GPL(qcom_add_glink_subdev);
    234
    235/**
    236 * qcom_remove_glink_subdev() - remove a GLINK subdevice from rproc
    237 * @rproc:	rproc handle
    238 * @glink:	reference to a GLINK subdev context
    239 */
    240void qcom_remove_glink_subdev(struct rproc *rproc, struct qcom_rproc_glink *glink)
    241{
    242	if (!glink->node)
    243		return;
    244
    245	rproc_remove_subdev(rproc, &glink->subdev);
    246	kfree_const(glink->ssr_name);
    247	of_node_put(glink->node);
    248}
    249EXPORT_SYMBOL_GPL(qcom_remove_glink_subdev);
    250
    251/**
    252 * qcom_register_dump_segments() - register segments for coredump
    253 * @rproc:	remoteproc handle
    254 * @fw:		firmware header
    255 *
    256 * Register all segments of the ELF in the remoteproc coredump segment list
    257 *
    258 * Return: 0 on success, negative errno on failure.
    259 */
    260int qcom_register_dump_segments(struct rproc *rproc,
    261				const struct firmware *fw)
    262{
    263	const struct elf32_phdr *phdrs;
    264	const struct elf32_phdr *phdr;
    265	const struct elf32_hdr *ehdr;
    266	int ret;
    267	int i;
    268
    269	ehdr = (struct elf32_hdr *)fw->data;
    270	phdrs = (struct elf32_phdr *)(ehdr + 1);
    271
    272	for (i = 0; i < ehdr->e_phnum; i++) {
    273		phdr = &phdrs[i];
    274
    275		if (phdr->p_type != PT_LOAD)
    276			continue;
    277
    278		if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
    279			continue;
    280
    281		if (!phdr->p_memsz)
    282			continue;
    283
    284		ret = rproc_coredump_add_segment(rproc, phdr->p_paddr,
    285						 phdr->p_memsz);
    286		if (ret)
    287			return ret;
    288	}
    289
    290	return 0;
    291}
    292EXPORT_SYMBOL_GPL(qcom_register_dump_segments);
    293
    294static int smd_subdev_start(struct rproc_subdev *subdev)
    295{
    296	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
    297
    298	smd->edge = qcom_smd_register_edge(smd->dev, smd->node);
    299
    300	return PTR_ERR_OR_ZERO(smd->edge);
    301}
    302
    303static void smd_subdev_stop(struct rproc_subdev *subdev, bool crashed)
    304{
    305	struct qcom_rproc_subdev *smd = to_smd_subdev(subdev);
    306
    307	qcom_smd_unregister_edge(smd->edge);
    308	smd->edge = NULL;
    309}
    310
    311/**
    312 * qcom_add_smd_subdev() - try to add a SMD subdevice to rproc
    313 * @rproc:	rproc handle to parent the subdevice
    314 * @smd:	reference to a Qualcomm subdev context
    315 */
    316void qcom_add_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
    317{
    318	struct device *dev = &rproc->dev;
    319
    320	smd->node = of_get_child_by_name(dev->parent->of_node, "smd-edge");
    321	if (!smd->node)
    322		return;
    323
    324	smd->dev = dev;
    325	smd->subdev.start = smd_subdev_start;
    326	smd->subdev.stop = smd_subdev_stop;
    327
    328	rproc_add_subdev(rproc, &smd->subdev);
    329}
    330EXPORT_SYMBOL_GPL(qcom_add_smd_subdev);
    331
    332/**
    333 * qcom_remove_smd_subdev() - remove the smd subdevice from rproc
    334 * @rproc:	rproc handle
    335 * @smd:	the SMD subdevice to remove
    336 */
    337void qcom_remove_smd_subdev(struct rproc *rproc, struct qcom_rproc_subdev *smd)
    338{
    339	if (!smd->node)
    340		return;
    341
    342	rproc_remove_subdev(rproc, &smd->subdev);
    343	of_node_put(smd->node);
    344}
    345EXPORT_SYMBOL_GPL(qcom_remove_smd_subdev);
    346
    347static struct qcom_ssr_subsystem *qcom_ssr_get_subsys(const char *name)
    348{
    349	struct qcom_ssr_subsystem *info;
    350
    351	mutex_lock(&qcom_ssr_subsys_lock);
    352	/* Match in the global qcom_ssr_subsystem_list with name */
    353	list_for_each_entry(info, &qcom_ssr_subsystem_list, list)
    354		if (!strcmp(info->name, name))
    355			goto out;
    356
    357	info = kzalloc(sizeof(*info), GFP_KERNEL);
    358	if (!info) {
    359		info = ERR_PTR(-ENOMEM);
    360		goto out;
    361	}
    362	info->name = kstrdup_const(name, GFP_KERNEL);
    363	srcu_init_notifier_head(&info->notifier_list);
    364
    365	/* Add to global notification list */
    366	list_add_tail(&info->list, &qcom_ssr_subsystem_list);
    367
    368out:
    369	mutex_unlock(&qcom_ssr_subsys_lock);
    370	return info;
    371}
    372
    373/**
    374 * qcom_register_ssr_notifier() - register SSR notification handler
    375 * @name:	Subsystem's SSR name
    376 * @nb:		notifier_block to be invoked upon subsystem's state change
    377 *
    378 * This registers the @nb notifier block as part the notifier chain for a
    379 * remoteproc associated with @name. The notifier block's callback
    380 * will be invoked when the remote processor's SSR events occur
    381 * (pre/post startup and pre/post shutdown).
    382 *
    383 * Return: a subsystem cookie on success, ERR_PTR on failure.
    384 */
    385void *qcom_register_ssr_notifier(const char *name, struct notifier_block *nb)
    386{
    387	struct qcom_ssr_subsystem *info;
    388
    389	info = qcom_ssr_get_subsys(name);
    390	if (IS_ERR(info))
    391		return info;
    392
    393	srcu_notifier_chain_register(&info->notifier_list, nb);
    394
    395	return &info->notifier_list;
    396}
    397EXPORT_SYMBOL_GPL(qcom_register_ssr_notifier);
    398
    399/**
    400 * qcom_unregister_ssr_notifier() - unregister SSR notification handler
    401 * @notify:	subsystem cookie returned from qcom_register_ssr_notifier
    402 * @nb:		notifier_block to unregister
    403 *
    404 * This function will unregister the notifier from the particular notifier
    405 * chain.
    406 *
    407 * Return: 0 on success, %ENOENT otherwise.
    408 */
    409int qcom_unregister_ssr_notifier(void *notify, struct notifier_block *nb)
    410{
    411	return srcu_notifier_chain_unregister(notify, nb);
    412}
    413EXPORT_SYMBOL_GPL(qcom_unregister_ssr_notifier);
    414
    415static int ssr_notify_prepare(struct rproc_subdev *subdev)
    416{
    417	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
    418	struct qcom_ssr_notify_data data = {
    419		.name = ssr->info->name,
    420		.crashed = false,
    421	};
    422
    423	srcu_notifier_call_chain(&ssr->info->notifier_list,
    424				 QCOM_SSR_BEFORE_POWERUP, &data);
    425	return 0;
    426}
    427
    428static int ssr_notify_start(struct rproc_subdev *subdev)
    429{
    430	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
    431	struct qcom_ssr_notify_data data = {
    432		.name = ssr->info->name,
    433		.crashed = false,
    434	};
    435
    436	srcu_notifier_call_chain(&ssr->info->notifier_list,
    437				 QCOM_SSR_AFTER_POWERUP, &data);
    438	return 0;
    439}
    440
    441static void ssr_notify_stop(struct rproc_subdev *subdev, bool crashed)
    442{
    443	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
    444	struct qcom_ssr_notify_data data = {
    445		.name = ssr->info->name,
    446		.crashed = crashed,
    447	};
    448
    449	srcu_notifier_call_chain(&ssr->info->notifier_list,
    450				 QCOM_SSR_BEFORE_SHUTDOWN, &data);
    451}
    452
    453static void ssr_notify_unprepare(struct rproc_subdev *subdev)
    454{
    455	struct qcom_rproc_ssr *ssr = to_ssr_subdev(subdev);
    456	struct qcom_ssr_notify_data data = {
    457		.name = ssr->info->name,
    458		.crashed = false,
    459	};
    460
    461	srcu_notifier_call_chain(&ssr->info->notifier_list,
    462				 QCOM_SSR_AFTER_SHUTDOWN, &data);
    463}
    464
    465/**
    466 * qcom_add_ssr_subdev() - register subdevice as restart notification source
    467 * @rproc:	rproc handle
    468 * @ssr:	SSR subdevice handle
    469 * @ssr_name:	identifier to use for notifications originating from @rproc
    470 *
    471 * As the @ssr is registered with the @rproc SSR events will be sent to all
    472 * registered listeners for the remoteproc when it's SSR events occur
    473 * (pre/post startup and pre/post shutdown).
    474 */
    475void qcom_add_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr,
    476			 const char *ssr_name)
    477{
    478	struct qcom_ssr_subsystem *info;
    479
    480	info = qcom_ssr_get_subsys(ssr_name);
    481	if (IS_ERR(info)) {
    482		dev_err(&rproc->dev, "Failed to add ssr subdevice\n");
    483		return;
    484	}
    485
    486	ssr->info = info;
    487	ssr->subdev.prepare = ssr_notify_prepare;
    488	ssr->subdev.start = ssr_notify_start;
    489	ssr->subdev.stop = ssr_notify_stop;
    490	ssr->subdev.unprepare = ssr_notify_unprepare;
    491
    492	rproc_add_subdev(rproc, &ssr->subdev);
    493}
    494EXPORT_SYMBOL_GPL(qcom_add_ssr_subdev);
    495
    496/**
    497 * qcom_remove_ssr_subdev() - remove subdevice as restart notification source
    498 * @rproc:	rproc handle
    499 * @ssr:	SSR subdevice handle
    500 */
    501void qcom_remove_ssr_subdev(struct rproc *rproc, struct qcom_rproc_ssr *ssr)
    502{
    503	rproc_remove_subdev(rproc, &ssr->subdev);
    504	ssr->info = NULL;
    505}
    506EXPORT_SYMBOL_GPL(qcom_remove_ssr_subdev);
    507
    508MODULE_DESCRIPTION("Qualcomm Remoteproc helper driver");
    509MODULE_LICENSE("GPL v2");