Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* adi_64.c: support for ADI (Application Data Integrity) feature on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * sparc m7 and newer processors. This feature is also known as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * SSM (Silicon Secured Memory).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2016 Oracle and/or its affiliates. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Khalid Aziz (khalid.aziz@oracle.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/mm_types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/mdesc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/adi_64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/mmu_64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <asm/pgtable_64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* Each page of storage for ADI tags can accommodate tags for 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * pages. When ADI enabled pages are being swapped out, it would be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * prudent to allocate at least enough tag storage space to accommodate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * SWAPFILE_CLUSTER number of pages. Allocate enough tag storage to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * store tags for four SWAPFILE_CLUSTER pages to reduce need for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * further allocations for same vma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define TAG_STORAGE_PAGES	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) struct adi_config adi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) EXPORT_SYMBOL(adi_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* mdesc_adi_init() : Parse machine description provided by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *	hypervisor to detect ADI capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Hypervisor reports ADI capabilities of platform in "hwcap-list" property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * for "cpu" node. If the platform supports ADI, "hwcap-list" property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * contains the keyword "adp". If the platform supports ADI, "platform"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * node will contain "adp-blksz", "adp-nbits" and "ue-on-adp" properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * to describe the ADI capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) void __init mdesc_adi_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct mdesc_handle *hp = mdesc_grab();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	const char *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u64 pn, *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (!hp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "cpu");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (pn == MDESC_NODE_NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	prop = mdesc_get_property(hp, pn, "hwcap-list", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * Look for "adp" keyword in hwcap-list which would indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * ADI support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	adi_state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		int plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (!strcmp(prop, "adp")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			adi_state.enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		plen = strlen(prop) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		prop += plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		len -= plen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!adi_state.enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	/* Find the ADI properties in "platform" node. If all ADI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * properties are not found, ADI support is incomplete and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * do not enable ADI in the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	pn = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (pn == MDESC_NODE_NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	val = (u64 *) mdesc_get_property(hp, pn, "adp-blksz", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	adi_state.caps.blksz = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	val = (u64 *) mdesc_get_property(hp, pn, "adp-nbits", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	adi_state.caps.nbits = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	val = (u64 *) mdesc_get_property(hp, pn, "ue-on-adp", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		goto adi_not_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	adi_state.caps.ue_on_adi = *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* Some of the code to support swapping ADI tags is written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * assumption that two ADI tags can fit inside one byte. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 * this assumption is broken by a future architecture change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * that code will have to be revisited. If that were to happen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * disable ADI support so we do not get unpredictable results
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * with programs trying to use ADI and their pages getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 * swapped out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (adi_state.caps.nbits > 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		pr_warn("WARNING: ADI tag size >4 on this platform. Disabling AADI support\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		adi_state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	mdesc_release(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) adi_not_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	adi_state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	adi_state.caps.blksz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	adi_state.caps.nbits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (hp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		mdesc_release(hp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) tag_storage_desc_t *find_tag_store(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 				   struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				   unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	tag_storage_desc_t *tag_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	unsigned long i, max_desc, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/* Check if this vma already has tag storage descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * allocated for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (mm->context.tag_store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		tag_desc = mm->context.tag_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		spin_lock_irqsave(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		for (i = 0; i < max_desc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			if ((addr >= tag_desc->start) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			    ((addr + PAGE_SIZE - 1) <= tag_desc->end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			tag_desc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		spin_unlock_irqrestore(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		/* If no matching entries were found, this must be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		 * freshly allocated page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		if (i >= max_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			tag_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return tag_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) tag_storage_desc_t *alloc_tag_store(struct mm_struct *mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				    struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				    unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	unsigned char *tags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	unsigned long i, size, max_desc, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	tag_storage_desc_t *tag_desc, *open_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	unsigned long end_addr, hole_start, hole_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	max_desc = PAGE_SIZE/sizeof(tag_storage_desc_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	open_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	hole_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	hole_end = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	end_addr = addr + PAGE_SIZE - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* Check if this vma already has tag storage descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * allocated for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	spin_lock_irqsave(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (mm->context.tag_store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		tag_desc = mm->context.tag_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		/* Look for a matching entry for this address. While doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		 * that, look for the first open slot as well and find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		 * the hole in already allocated range where this request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		 * will fit in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		for (i = 0; i < max_desc; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			if (tag_desc->tag_users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				if (open_desc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 					open_desc = tag_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				if ((addr >= tag_desc->start) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				    (tag_desc->end >= (addr + PAGE_SIZE - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 					tag_desc->tag_users++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			if ((tag_desc->start > end_addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			    (tag_desc->start < hole_end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				hole_end = tag_desc->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			if ((tag_desc->end < addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			    (tag_desc->end > hole_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 				hole_start = tag_desc->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			tag_desc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		size = sizeof(tag_storage_desc_t)*max_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		mm->context.tag_store = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		if (mm->context.tag_store == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			tag_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		tag_desc = mm->context.tag_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		for (i = 0; i < max_desc; i++, tag_desc++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			tag_desc->tag_users = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		open_desc = mm->context.tag_store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/* Check if we ran out of tag storage descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	if (open_desc == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		tag_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* Mark this tag descriptor slot in use and then initialize it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	tag_desc = open_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	tag_desc->tag_users = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	/* Tag storage has not been allocated for this vma and space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	 * is available in tag storage descriptor. Since this page is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * being swapped out, there is high probability subsequent pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 * in the VMA will be swapped out as well. Allocate pages to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 * store tags for as many pages in this vma as possible but not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 * more than TAG_STORAGE_PAGES. Each byte in tag space holds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	 * two ADI tags since each ADI tag is 4 bits. Each ADI tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	 * covers adi_blksize() worth of addresses. Check if the hole is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	 * big enough to accommodate full address range for using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	 * TAG_STORAGE_PAGES number of tag pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	size = TAG_STORAGE_PAGES * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	end_addr = addr + (size*2*adi_blksize()) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	/* Check for overflow. If overflow occurs, allocate only one page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	if (end_addr < addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		end_addr = addr + (size*2*adi_blksize()) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		/* If overflow happens with the minimum tag storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		 * allocation as well, adjust ending address for this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		 * tag storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (end_addr < addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			end_addr = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (hole_end < end_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		/* Available hole is too small on the upper end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * address. Can we expand the range towards the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 * address and maximize use of this slot?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		unsigned long tmp_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		end_addr = hole_end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		tmp_addr = end_addr - (size*2*adi_blksize()) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		/* Check for underflow. If underflow occurs, allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * only one page for storing ADI tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		if (tmp_addr > addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			size = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			tmp_addr = end_addr - (size*2*adi_blksize()) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			/* If underflow happens with the minimum tag storage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			 * allocation as well, adjust starting address for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			 * this tag storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			if (tmp_addr > addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 				tmp_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if (tmp_addr < hole_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			/* Available hole is restricted on lower address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			 * end as well
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			tmp_addr = hole_start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		addr = tmp_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		size = (end_addr + 1 - addr)/(2*adi_blksize());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		size = (size + (PAGE_SIZE-adi_blksize()))/PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		size = size * PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	tags = kzalloc(size, GFP_NOWAIT|__GFP_NOWARN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (tags == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		tag_desc->tag_users = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		tag_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	tag_desc->start = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	tag_desc->tags = tags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	tag_desc->end = end_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	spin_unlock_irqrestore(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	return tag_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) void del_tag_store(tag_storage_desc_t *tag_desc, struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	unsigned char *tags = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	spin_lock_irqsave(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	tag_desc->tag_users--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (tag_desc->tag_users == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		tag_desc->start = tag_desc->end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		/* Do not free up the tag storage space allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		 * by the first descriptor. This is persistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		 * emergency tag storage space for the task.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		if (tag_desc != mm->context.tag_store) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			tags = tag_desc->tags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			tag_desc->tags = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	spin_unlock_irqrestore(&mm->context.tag_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	kfree(tags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) #define tag_start(addr, tag_desc)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	((tag_desc)->tags + ((addr - (tag_desc)->start)/(2*adi_blksize())))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) /* Retrieve any saved ADI tags for the page being swapped back in and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * restore these tags to the newly allocated physical page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) void adi_restore_tags(struct mm_struct *mm, struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		      unsigned long addr, pte_t pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	unsigned char *tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	tag_storage_desc_t *tag_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	unsigned long paddr, tmp, version1, version2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	/* Check if the swapped out page has an ADI version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	 * saved. If yes, restore version tag to the newly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	 * allocated page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	tag_desc = find_tag_store(mm, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (tag_desc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	tag = tag_start(addr, tag_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	paddr = pte_val(pte) & _PAGE_PADDR_4V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		version1 = (*tag) >> 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		version2 = (*tag) & 0x0f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		*tag++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		asm volatile("stxa %0, [%1] %2\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			: "r" (version1), "r" (tmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			  "i" (ASI_MCD_REAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		tmp += adi_blksize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		asm volatile("stxa %0, [%1] %2\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			: "r" (version2), "r" (tmp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			  "i" (ASI_MCD_REAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	asm volatile("membar #Sync\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	/* Check and mark this tag space for release later if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	 * the swapped in page was the last user of tag space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	del_tag_store(tag_desc, mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /* A page is about to be swapped out. Save any ADI tags associated with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * this physical page so they can be restored later when the page is swapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * back in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) int adi_save_tags(struct mm_struct *mm, struct vm_area_struct *vma,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		  unsigned long addr, pte_t oldpte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	unsigned char *tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	tag_storage_desc_t *tag_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	unsigned long version1, version2, paddr, tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	tag_desc = alloc_tag_store(mm, vma, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	if (tag_desc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	tag = tag_start(addr, tag_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	paddr = pte_val(oldpte) & _PAGE_PADDR_4V;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	for (tmp = paddr; tmp < (paddr+PAGE_SIZE); tmp += adi_blksize()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		asm volatile("ldxa [%1] %2, %0\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 				: "=r" (version1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				: "r" (tmp), "i" (ASI_MCD_REAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		tmp += adi_blksize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		asm volatile("ldxa [%1] %2, %0\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				: "=r" (version2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				: "r" (tmp), "i" (ASI_MCD_REAL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		*tag = (version1 << 4) | version2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		tag++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }